implement shell command interface and add basic commands (echo, ls, pwd, cd, touch, cat, rm) with syscall integration
This commit is contained in:
109
src/shell/FileSystem.ts
Normal file
109
src/shell/FileSystem.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
interface INode {
|
||||
name: string;
|
||||
isDirectory: boolean;
|
||||
content?: string;
|
||||
children?: Map<string, INode>;
|
||||
}
|
||||
|
||||
export class FileSystem {
|
||||
private root: INode;
|
||||
|
||||
constructor() {
|
||||
console.log('[FileSystem] Initializing new filesystem');
|
||||
this.root = {
|
||||
name: '/',
|
||||
isDirectory: true,
|
||||
children: new Map()
|
||||
};
|
||||
}
|
||||
|
||||
createDirectory(path: string): void {
|
||||
console.log(`[FileSystem] Creating directory: ${path}`);
|
||||
const parts = path.split('/').filter(p => p);
|
||||
let current = this.root;
|
||||
|
||||
for (const part of parts) {
|
||||
if (!current.children!.has(part)) {
|
||||
console.log(`[FileSystem] Creating new directory node: ${part}`);
|
||||
current.children!.set(part, {
|
||||
name: part,
|
||||
isDirectory: true,
|
||||
children: new Map()
|
||||
});
|
||||
}
|
||||
current = current.children!.get(part)!;
|
||||
}
|
||||
}
|
||||
|
||||
writeFile(path: string, content: string): void {
|
||||
console.log(`[FileSystem] Writing file: ${path}`);
|
||||
const parts = path.split('/').filter(p => p);
|
||||
const fileName = parts.pop()!;
|
||||
let current = this.root;
|
||||
|
||||
for (const part of parts) {
|
||||
if (!current.children!.has(part)) {
|
||||
console.log(`[FileSystem] Creating parent directory: ${part}`);
|
||||
this.createDirectory(part);
|
||||
}
|
||||
current = current.children!.get(part)!;
|
||||
}
|
||||
|
||||
current.children!.set(fileName, {
|
||||
name: fileName,
|
||||
isDirectory: false,
|
||||
content
|
||||
});
|
||||
console.log(`[FileSystem] File written: ${fileName}`);
|
||||
}
|
||||
|
||||
listDirectory(path: string): string[] {
|
||||
console.log(`[FileSystem] Listing directory: ${path}`);
|
||||
const parts = path.split('/').filter(p => p);
|
||||
let current = this.root;
|
||||
|
||||
for (const part of parts) {
|
||||
if (!current.children!.has(part)) return [];
|
||||
current = current.children!.get(part)!;
|
||||
}
|
||||
|
||||
const result = Array.from(current.children!.keys());
|
||||
console.log(`[FileSystem] Found ${result.length} entries`);
|
||||
return result;
|
||||
}
|
||||
|
||||
readFile(path: string): string | null {
|
||||
console.log(`[FileSystem] Reading file: ${path}`);
|
||||
const parts = path.split('/').filter(p => p);
|
||||
const fileName = parts.pop()!;
|
||||
let current = this.root;
|
||||
|
||||
for (const part of parts) {
|
||||
if (!current.children!.has(part)) return null;
|
||||
current = current.children!.get(part)!;
|
||||
}
|
||||
|
||||
const file = current.children!.get(fileName);
|
||||
if (!file || file.isDirectory) {
|
||||
console.log(`[FileSystem] File not found or is directory: ${path}`);
|
||||
return null;
|
||||
}
|
||||
console.log(`[FileSystem] File read successfully: ${path}`);
|
||||
return file.content!;
|
||||
}
|
||||
|
||||
deleteFile(path: string): void {
|
||||
console.log(`[FileSystem] Deleting file: ${path}`);
|
||||
const parts = path.split('/').filter(p => p);
|
||||
const fileName = parts.pop()!;
|
||||
let current = this.root;
|
||||
|
||||
for (const part of parts) {
|
||||
if (!current.children!.has(part)) return;
|
||||
current = current.children!.get(part)!;
|
||||
}
|
||||
|
||||
current.children!.delete(fileName);
|
||||
console.log(`[FileSystem] File deleted: ${path}`);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user