Files
termos/src/core/commands/touch.ts
T
ChaosMaker 8f93f3e43f - Fixed a bug in serializer that didnt account for parsed inodes to the inode index on the vdisk
- Fixed the touch command (for now only creates files)
- Fixed a minor bug in ls where it wouldnt return the correct result when a path provided in the args wouldnt resolve
2026-11-20 10:31:00 +01:00

54 lines
1.8 KiB
TypeScript

import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import { NodeType } from "@core/filesystem/node";
import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
import { getCurrentSession } from "@core/session_manager";
import type { ShellState } from "@core/shell_state";
import type { ShellError } from "@core/util/errors";
import { resolvePath } from "@core/util/path_resolver";
import { Blob } from "node:buffer";
export const COMMAND_NAME = "touch";
//Need to implement escape sequences
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
const session = getCurrentSession(state);
for (const arg of args) {
try {
resolvePath(state, arg);
} catch {
const index = arg.lastIndexOf("/");
const fileName = arg.slice(index + 1);
const parentPath = index === -1 ? "./" : arg.slice(0, index + 1);
const pptr = resolvePath(state, parentPath);
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<ShellError>;
}
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
return {
state: state,
result,
};
}
export const descriptor: CommandDescriptor = {
fn: main,
name: COMMAND_NAME,
};