unsable code rework, doesnt run

This commit is contained in:
2026-02-23 17:15:28 +01:00
parent 1faab39849
commit 9ac27f5fbd
19 changed files with 287 additions and 189 deletions

View File

@@ -1,68 +1,60 @@
import { COMMANDS, GROUP, PASSWD, type CommandArgs, type ICommand, type Result } from './static';
import { VirtualFS } from './fs';
import { VirtualFS, type TreeNode } from './fs';
import { Terminal, type PrintData } from '../terminal/terminal';
import { Stack } from '../stack';
export type Permission = {
r: boolean;
w: boolean;
x: boolean;
};
import { PASSWD, type User } from './etc/userData';
import { ExitCode } from './metadata';
import { COMMANDS, type CommandArgs, type CommandResultData, type ICommand } from './commandRegistry';
import { GROUP, type Group } from './etc/groupData';
export type BashInitArgs = {
stdio?: Terminal;
user: User;
fs: any;
io?: Terminal;
instanceId: number;
user: {username: string, password: string};
fs?: VirtualFS;
};
export type TimeStamps = {
modified: Date;
changed: Date;
accessed: Date;
};
// TODO: Finish this
// TODO: Change into a type instead of an enum for performance (low priority)
export enum ExitCode {
SUCCESS = 0,
ERROR = 1
}
export type User = {
username: string;
passwd: string; //HASHED PASSWORD //TODO: Make a formated type
readonly uid: number; // Normal user 1000+ System user 1-999 root - 0 //TODO: Make a formated type
readonly gid: number; // Primary group | 'Users' 1000 - Others - 1000+ root - 0 //TODO: Make a formated type
home: string; //TODO: Make a formated type
history: string[];
cwd?: number; //TODO: Make a formated type
pwd?: number; //TODO: Make a formated type
};
export type Group = {
groupname: string;
gid: number; // Primary group 'Users' 1000 - Others - 1000+ root - 0
members: number[]; //TODO: Make a formated type UID
export type Result = {
exitCode: ExitCode;
path: number; //the inode of the place that the command was executed in
resultData?: CommandResultData;
};
export class Bash {
private readonly _instanceId: number;
private vfs: VirtualFS;
private _passwd: User[];
private _instances: Stack<User>;
private _userInstances: Stack<User>;
private _group: Group[];
private _terminal!: Terminal;
private user: User;
private readonly _commands: Record<string, ICommand>;
constructor(args: BashInitArgs) {
this.user = args.user;
this._instanceId = args.instanceId
this._commands = COMMANDS;
this._passwd = PASSWD;
this._group = GROUP;
this._terminal = args.stdio!;
this._instances = new Stack<User>();
this._userInstances = new Stack<User>();
this._terminal = args.io!;
this.vfs = new VirtualFS({ fs: args.fs, user: args.user });
const loginResult = this.userLogin(args.user.username, args.user.password);
if(loginResult == ExitCode.ERROR)
this._terminal.throwExeption(
`Failed to initialize bash instance - access denied for user ${args.user.username}`,
ExitCode.ERROR
)
this.user = this._userInstances.peek()!
this.vfs = this._terminal.fileSystem;
}
private _initNewUserSession(user: User): User {
if(!this._passwd.includes(user))
this._terminal.throwExeption(`user not found under the name ${user.username}`, ExitCode.ERROR)
this._userInstances.push(user);
return user
}
private _appendNewResult(inode: number, output: any, cmd: string) {
@@ -85,6 +77,10 @@ export class Bash {
}
}
clearTerminal(): void {
this._terminal.clearTerminal();
}
getCwd(): number {
return this.vfs.cwd;
}
@@ -113,7 +109,7 @@ export class Bash {
return this._group[1].members.includes(uid);
}
executeCommand(commandName: string, args: CommandArgs): void {
async executeCommand(commandName: string, args: CommandArgs) {
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
const command = this._commands[commandName];
if (!command) this.throwError(result);
@@ -128,7 +124,7 @@ export class Bash {
let out: Result = command.method.call(this, args);
console.log(out);
this._appendNewResult(out.path, out.data?.data, this.user.history[0]);
this._appendNewResult(out.path, out.resultData?.data, this.user.history[0]);
}
throwError(result: Result): void {
@@ -140,14 +136,25 @@ export class Bash {
}
userLogout() {
this._instances.pop();
if (this._instances.size() === 0) {
this._userInstances.pop();
if (this._userInstances.size() === 0) {
//TODO: Implement system logout
} else {
//this.changeUser(this._instances.peek()!);
}
}
userLogin(username: string, passwd: string): ExitCode {
const user: User | undefined =
this._passwd.find((user) => user.username === username);
if(user && user.passwd === passwd) {
this._initNewUserSession(user);
return ExitCode.SUCCESS;
}
return ExitCode.ERROR;
}
formatBytes(bytes: number, dPoint?: number, pow: 1024 | 1000 = 1024): string {
if (!+bytes) return '0';