WIP: new abstraction layer, file system now a singleton, shell should be too
This commit is contained in:
+45
-43
@@ -1,12 +1,17 @@
|
||||
import { VirtualFS, type TreeNode } from './fs';
|
||||
import { Terminal, type PrintData } from '../terminal/terminal';
|
||||
import { Stack } from '../stack';
|
||||
import { PASSWD, type User } from './etc/userData';
|
||||
import { ExitCode } from './metadata';
|
||||
import { COMMANDS, type CommandArgs, type CommandResultData, type ICommand } from './commandRegistry';
|
||||
import { Terminal, type PrintData } from '../terminal/terminal';
|
||||
import {
|
||||
COMMANDS,
|
||||
type CommandArgs,
|
||||
type CommandResultData,
|
||||
type ICommand
|
||||
} from './commandRegistry';
|
||||
import { GROUP, type Group } from './etc/groupData';
|
||||
import { Process } from './process';
|
||||
import { PASSWD, type User } from './etc/userData';
|
||||
import { VirtualFS, type TreeNode } from './fs';
|
||||
import type { KeybindInhibitor } from './keybindInhibitor';
|
||||
import { ExitCode } from './metadata';
|
||||
import { Process } from './process';
|
||||
|
||||
export enum Signal {
|
||||
SIGINT,
|
||||
@@ -16,7 +21,7 @@ export enum Signal {
|
||||
export type BashInitArgs = {
|
||||
io?: Terminal;
|
||||
fs: Map<number, TreeNode>;
|
||||
user: {username: string, password: string};
|
||||
user: { username: string; password: string };
|
||||
};
|
||||
|
||||
export type Result = {
|
||||
@@ -31,13 +36,12 @@ export class Bash {
|
||||
private readonly _group: Group[];
|
||||
private readonly _terminal!: Terminal;
|
||||
private readonly _commands: Record<string, ICommand>;
|
||||
|
||||
|
||||
private _userInstances: Stack<User>;
|
||||
|
||||
|
||||
private user: User;
|
||||
private _process?: Process;
|
||||
|
||||
|
||||
|
||||
constructor(args: BashInitArgs) {
|
||||
this._commands = COMMANDS;
|
||||
this._passwd = PASSWD;
|
||||
@@ -46,33 +50,29 @@ export class Bash {
|
||||
this._terminal = args.io!;
|
||||
|
||||
const loginResult = this.userLogin(args.user.username, args.user.password);
|
||||
if(loginResult == ExitCode.ERROR)
|
||||
if (loginResult == ExitCode.ERROR)
|
||||
this._terminal.throwExeption(
|
||||
`Failed to initialize bash instance - access denied for user ${args.user.username}`,
|
||||
ExitCode.ERROR
|
||||
)
|
||||
`Failed to initialize bash instance - access denied for user ${args.user.username}`,
|
||||
ExitCode.ERROR
|
||||
);
|
||||
|
||||
|
||||
this.user = this._userInstances.peek()!
|
||||
this.vfs = new VirtualFS({ fs: args.fs, user: this.user});
|
||||
this.user = this._userInstances.peek()!;
|
||||
this.vfs = new VirtualFS({ fs: args.fs, user: this.user });
|
||||
}
|
||||
|
||||
private _initNewUserSession(user: User): User {
|
||||
if(!this._passwd.includes(user))
|
||||
this._terminal.throwExeption(`user not found under the name ${user.username}`, ExitCode.ERROR)
|
||||
if (!this._passwd.includes(user))
|
||||
this._terminal.throwExeption(
|
||||
`user not found under the name ${user.username}`,
|
||||
ExitCode.ERROR
|
||||
);
|
||||
|
||||
this._userInstances.push(user);
|
||||
return user
|
||||
return user;
|
||||
}
|
||||
|
||||
private async _initNewProcess(command: ICommand, args: CommandArgs) {
|
||||
this._process = new Process(
|
||||
this,
|
||||
this.user.uid,
|
||||
'TEMP PROCESS NAME',
|
||||
command,
|
||||
args
|
||||
);
|
||||
private async _initNewProcess(command: ICommand, args: CommandArgs) {
|
||||
this._process = new Process(this, this.user.uid, 'TEMP PROCESS NAME', command, args);
|
||||
|
||||
//this._terminal.isProcessActive = true;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ export class Bash {
|
||||
output: output,
|
||||
cmd: cmd
|
||||
};
|
||||
console.log(this.vfs.formatPath(this.vfs.getPathByInode(inode)), "TEST TEST !@#");
|
||||
console.log(this.vfs.formatPath(this.vfs.getPathByInode(inode)), 'TEST TEST !@#');
|
||||
this._terminal.PrintOutput(data);
|
||||
}
|
||||
|
||||
@@ -118,17 +118,16 @@ export class Bash {
|
||||
}
|
||||
|
||||
getProcess(): Process | ExitCode.ERROR {
|
||||
if(this._process) {
|
||||
if (this._process) {
|
||||
return this._process;
|
||||
}
|
||||
return ExitCode.ERROR;
|
||||
}
|
||||
|
||||
getProcessKeybindInhibitor(): KeybindInhibitor | null {
|
||||
if(this._process) {
|
||||
if (this._process) {
|
||||
return this._process.getKeybindInhibitor();
|
||||
}
|
||||
else return null;
|
||||
} else return null;
|
||||
}
|
||||
|
||||
getTerminalWidth(): number {
|
||||
@@ -140,20 +139,20 @@ export class Bash {
|
||||
}
|
||||
|
||||
hasSudoPerms(uid: number): boolean {
|
||||
return this._group[1].members.includes(uid);
|
||||
return this._group[0].members.includes(uid);
|
||||
}
|
||||
|
||||
async executeCommand(commandName: string, args: CommandArgs): Promise<Result> {
|
||||
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
||||
const command: ICommand = this._commands[commandName];
|
||||
|
||||
|
||||
if (!command) this.throwError(result);
|
||||
|
||||
if (command.root) {
|
||||
if (this.hasSudoPerms(this.user.uid)) {
|
||||
await this._initNewProcess(command, args);
|
||||
if(!this._process) throw new Error('failed to init new process');
|
||||
|
||||
if (!this._process) throw new Error('failed to init new process');
|
||||
|
||||
const out: Result = await this._process.getResult();
|
||||
this._appendNewResult(this.getCwd(), out, this.user.history[0]);
|
||||
}
|
||||
@@ -161,7 +160,7 @@ export class Bash {
|
||||
}
|
||||
|
||||
await this._initNewProcess(command, args);
|
||||
if(!this._process) throw new Error('failed to init new process');
|
||||
if (!this._process) throw new Error('failed to init new process');
|
||||
|
||||
const out: Result = await this._process.getResult();
|
||||
console.log(out);
|
||||
@@ -170,6 +169,10 @@ export class Bash {
|
||||
return out;
|
||||
}
|
||||
|
||||
static testMethod() {
|
||||
//
|
||||
}
|
||||
|
||||
throwError(result: Result): void {
|
||||
switch (result.exitCode) {
|
||||
default: {
|
||||
@@ -188,13 +191,12 @@ export class Bash {
|
||||
}
|
||||
|
||||
userLogin(username: string, passwd: string): ExitCode {
|
||||
const user: User | undefined =
|
||||
this._passwd.find((user) => user.username === username);
|
||||
const user: User | undefined = this._passwd.find((user) => user.username === username);
|
||||
|
||||
if(user && user.passwd === passwd) {
|
||||
if (user && user.passwd === passwd) {
|
||||
this._initNewUserSession(user);
|
||||
return ExitCode.SUCCESS;
|
||||
}
|
||||
}
|
||||
return ExitCode.ERROR;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user