@@ -18,6 +18,7 @@ export interface UserInfo {
|
||||
|
||||
export interface SessionState {
|
||||
readonly user: UserInfo;
|
||||
readonly envVars: ReadonlyMap<string, string>;
|
||||
readonly history: HistoryState;
|
||||
readonly cwd: number;
|
||||
readonly pwd: number;
|
||||
@@ -27,7 +28,7 @@ export interface SessionState {
|
||||
export function createInitialSessionState(user: UserInfo): SessionState {
|
||||
return Object.freeze({
|
||||
user,
|
||||
//envVars: Object.freeze(new Map()),
|
||||
envVars: Object.freeze(new Map()),
|
||||
history: Object.freeze([]),
|
||||
cwd: user.home,
|
||||
pwd: user.home,
|
||||
@@ -64,7 +65,7 @@ export function login(
|
||||
|
||||
const stack = [...state.sessionStack];
|
||||
stack.push(createInitialSessionState(user));
|
||||
return createShellState(state.filesystem, state.users, state.groups, stack, state.env);
|
||||
return createShellState(state.filesystem, state.users, state.groups, stack);
|
||||
}
|
||||
|
||||
export function logout(state: ShellState): ShellState {
|
||||
@@ -72,7 +73,7 @@ export function logout(state: ShellState): ShellState {
|
||||
const stack = [...state.sessionStack];
|
||||
stack.pop();
|
||||
|
||||
return createShellState(state.filesystem, state.users, state.groups, stack, state.env);
|
||||
return createShellState(state.filesystem, state.users, state.groups, stack);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
+6
-14
@@ -8,7 +8,6 @@ export interface ShellState {
|
||||
readonly users: ReadonlyMap<number, UserInfo>;
|
||||
readonly groups: ReadonlyMap<number, GroupInfo>;
|
||||
readonly sessionStack: ReadonlyArray<SessionState>;
|
||||
readonly env: ReadonlyMap<string, string>;
|
||||
}
|
||||
|
||||
export function createInitialShellState(
|
||||
@@ -16,14 +15,12 @@ export function createInitialShellState(
|
||||
users: ReadonlyMap<number, UserInfo>,
|
||||
groups: ReadonlyMap<number, GroupInfo>,
|
||||
user: UserInfo,
|
||||
env: ReadonlyMap<string, string>,
|
||||
): ShellState {
|
||||
return Object.freeze({
|
||||
filesystem,
|
||||
users,
|
||||
groups,
|
||||
sessionStack: Object.freeze([createInitialSessionState(user)]),
|
||||
env,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -32,14 +29,12 @@ export function createShellState(
|
||||
users: ReadonlyMap<number, UserInfo>,
|
||||
groups: ReadonlyMap<number, GroupInfo>,
|
||||
sessionStack: ReadonlyArray<SessionState>,
|
||||
env: ReadonlyMap<string, string>,
|
||||
): ShellState {
|
||||
return Object.freeze({
|
||||
filesystem,
|
||||
users,
|
||||
groups,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
env,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -48,7 +43,7 @@ export function withCwd(state: ShellState, inode: number): ShellState {
|
||||
|
||||
const newSession = Object.freeze({
|
||||
user: session.user,
|
||||
//envVars: Object.freeze(session.envVars),
|
||||
envVars: Object.freeze(session.envVars),
|
||||
history: Object.freeze(session.history),
|
||||
cwd: inode,
|
||||
pwd: session.cwd,
|
||||
@@ -64,7 +59,6 @@ export function withCwd(state: ShellState, inode: number): ShellState {
|
||||
users: state.users,
|
||||
groups: state.groups,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
env: state.env,
|
||||
} satisfies ShellState);
|
||||
}
|
||||
|
||||
@@ -73,7 +67,7 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
||||
|
||||
const newSession = Object.freeze({
|
||||
user: session.user,
|
||||
//envVars: Object.freeze(session.envVars),
|
||||
envVars: Object.freeze(session.envVars),
|
||||
history: Object.freeze(addHistoryEntry(session.history, input)),
|
||||
cwd: session.cwd,
|
||||
pwd: session.pwd,
|
||||
@@ -89,11 +83,10 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
||||
users: state.users,
|
||||
groups: state.groups,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
env: state.env,
|
||||
} satisfies ShellState);
|
||||
}
|
||||
|
||||
/* export function withEnv(state: ShellState, key: string, value: string): ShellState {
|
||||
export function withEnv(state: ShellState, key: string, value: string): ShellState {
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
const envVars = new Map(session.envVars);
|
||||
@@ -101,7 +94,7 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
||||
|
||||
const newSession = Object.freeze({
|
||||
user: session.user,
|
||||
//envVars: Object.freeze(envVars),
|
||||
envVars: Object.freeze(envVars),
|
||||
history: Object.freeze(session.history),
|
||||
cwd: session.cwd,
|
||||
pwd: session.pwd,
|
||||
@@ -118,14 +111,14 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
||||
groups: state.groups,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
} satisfies ShellState);
|
||||
} */
|
||||
}
|
||||
|
||||
export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
const newSession = Object.freeze({
|
||||
user: session.user,
|
||||
//envVars: Object.freeze(session.envVars),
|
||||
envVars: Object.freeze(session.envVars),
|
||||
history: Object.freeze(session.history),
|
||||
cwd: session.cwd,
|
||||
pwd: session.pwd,
|
||||
@@ -141,6 +134,5 @@ export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
||||
users: state.users,
|
||||
groups: state.groups,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
env: state.env,
|
||||
} satisfies ShellState);
|
||||
}
|
||||
|
||||
+1
-4
@@ -45,10 +45,7 @@ const group: GroupInfo = {
|
||||
users.set(1000, user);
|
||||
groups.set(1000, group);
|
||||
|
||||
const env: Map<string, string> = new Map();
|
||||
env.set("PATH", "/folder1");
|
||||
|
||||
const state: ShellState = createInitialShellState(fs, users, groups, user, env);
|
||||
const state: ShellState = createInitialShellState(fs, users, groups, user);
|
||||
Shell.init(state);
|
||||
|
||||
//console.log(resolveInode(Shell.Instance?.State!, 6));
|
||||
|
||||
+3
-14
@@ -1,4 +1,4 @@
|
||||
import { type DirectoryNode } from "@core/filesystem/treenode";
|
||||
import { COMMAND_REGISTRY } from "@core/command_registry";
|
||||
import { createErrorResult, type Result } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
||||
@@ -6,7 +6,6 @@ import { SIGNAL_MAP } from "@core/signal";
|
||||
import type { ShellError } from "@core/util/errors";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { tokenize } from "@core/util/parser";
|
||||
import { resolvePath } from "@core/util/path_resolver";
|
||||
import { EventEmitter } from "./event_emitter";
|
||||
import { ProcessManager } from "./process_manager";
|
||||
|
||||
@@ -64,12 +63,7 @@ export class Shell {
|
||||
if (!parsed[0]) return;
|
||||
|
||||
this._State = withHistory(this._State, input);
|
||||
|
||||
const inode = resolvePath(this._State, this._State.env.get("PATH")!);
|
||||
const node = this._State.filesystem.getNode(inode) as DirectoryNode;
|
||||
const command = node.children[parsed[0]];
|
||||
|
||||
//const command = COMMAND_REGISTRY.get(parsed[0]);
|
||||
const command = COMMAND_REGISTRY.get(parsed[0]);
|
||||
|
||||
if (!command) {
|
||||
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
||||
@@ -88,7 +82,7 @@ export class Shell {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._State.filesystem.canExecute(command, getCurrentSession(this._State).user)) {
|
||||
if (command.requiresRoot && getCurrentSession(this._State).user.uid != 0) {
|
||||
const result = createErrorResult(
|
||||
"PermissionDenied",
|
||||
`Permission Denied`,
|
||||
@@ -101,11 +95,6 @@ export class Shell {
|
||||
return;
|
||||
}
|
||||
|
||||
const bin = this._State.filesystem.getNode(command);
|
||||
// read file content
|
||||
// try to serialize it to a function
|
||||
// call that function with a new process
|
||||
|
||||
parsed.shift();
|
||||
|
||||
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
||||
|
||||
Reference in New Issue
Block a user