diff --git a/src/core/command_registry.ts b/src/core/command_registry.ts index 22d2b1b..b9768a1 100644 --- a/src/core/command_registry.ts +++ b/src/core/command_registry.ts @@ -6,12 +6,14 @@ import { descriptor as echo } from "./commands/echo"; import { descriptor as ls } from "./commands/ls"; import { descriptor as pwd } from "./commands/pwd"; import { descriptor as testasync } from "./commands/testasync"; +import { descriptor as touch } from "./commands/touch"; const reg = new Map(); //reg.set(descriptor.name, descriptor <- imported const from command implementation file); reg.set(echo.name, echo); reg.set(cd.name, cd); reg.set(testasync.name, testasync); +reg.set(touch.name, touch); reg.set(ls.name, ls); reg.set(cat.name, cat); reg.set(pwd.name, pwd); diff --git a/src/core/commands/ls.ts b/src/core/commands/ls.ts index bcd0cd7..ed5a0fd 100644 --- a/src/core/commands/ls.ts +++ b/src/core/commands/ls.ts @@ -219,8 +219,10 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { + const session = getCurrentSession(state); + for (const arg of args) { - const inode = resolvePath(state, arg); - const session = getCurrentSession(state); - - let newState; - - if (inode === -1) { + try { + resolvePath(state, arg); + } catch { const index = arg.lastIndexOf("/"); - let parentPath; - if (index === -1) { - parentPath = "./"; - } else { - parentPath = arg.slice(0, index); - } const fileName = arg.slice(index + 1); - const parent = resolvePath(state, parentPath); + const parentPath = index === -1 ? "./" : arg.slice(0, index + 1); + const pptr = resolvePath(state, parentPath); - newState = state.filesystem.createNode( - parent, - fileName, - TreeNodeType.File, - 0o644, - session.user.uid, - session.user.gid, - ); - } else { + state.filesystem.createNode(pptr, fileName, { + type: NodeType.File, + mode: 0o644, + owner: session.user.uid, + group: session.user.gid, + content: new Blob(), + size: 0, + }); + + continue; } + return { + state, + result: createErrorResult("AlreadyExists", "The file already exists", {}), + } satisfies CommandOutput; } - const result: ResultSuccess = createSuccessResult(COMMAND_NAME); return { diff --git a/src/shell/serializer.ts b/src/shell/serializer.ts index d31daf1..045d92a 100644 --- a/src/shell/serializer.ts +++ b/src/shell/serializer.ts @@ -10,6 +10,7 @@ import { NodeType, type DirectoryNode, type FileNode, type Node, type SymlinkNod export function deserializeFileSystem(json: string): { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] { const raw = JSON.parse(json); + let index = 0; // prettier-ignore function parseNode(tree: any, parent: number, store: MemDataStore, map: Map): void { // eslint-disable-line @typescript-eslint/no-explicit-any @@ -31,6 +32,8 @@ export function deserializeFileSystem(json: string): { dstore: IDataStore; mstor return; } + if(tree.inode > index) index = tree.inode; + switch(tree.type) { case "directory": { const children: Map = new Map(); @@ -121,7 +124,7 @@ export function deserializeFileSystem(json: string): { dstore: IDataStore; mstor const config: VDiskConfig = { id: dev.id, label: `disk-${dev.id}`, - index: 0, + index, readonly: false, size: 1024 * 1024 * 1024 * 8, //8 GiB base: map,