chore: make webfs usable

This commit is contained in:
binekrasik
2026-05-17 23:52:31 +02:00
parent f8335dcf5f
commit 4f4b72b915
14 changed files with 81 additions and 34 deletions

View File

@@ -3,10 +3,6 @@ import type { SimpleStream } from '../utils/SimpleStream'
import { Program } from './Program'
export class Cat extends Program {
constructor() {
super()
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
let item: Item = await Item.open(args[1])

View File

@@ -7,17 +7,24 @@ export class Echo extends Program {
super()
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, ___: Item, args: string[]): Promise<number> {
let item: Item = await Item.open(args[1])
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
if (args.length < 2) {
stdout.emit("echo: error: missing path argument\n")
return 1
}
const item = await Item.open(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
if (!(await item.Exists())) {
stdout.emit(`echo: error: item ${item.GetPath()} doesn't exist.\n`)
return 1
return 2
}
if (item.IsDirectory()) {
stdout.emit(`echo: error: can't write data to a directory; item ${item.GetPath()} is a directory.\n`)
return 2
return 3
}
await item.Write(args.slice(2).join(' '))

View File

@@ -8,7 +8,12 @@ export class Ls extends Program {
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
let item: Item = args[1] ? await Item.openDir(args[1]) : workdir
const item = args[1]
? await Item.openDir(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
: workdir
if (args[1] && !item.IsDirectory()) {
stdout.emit("ls: error: the provided path is not a directory\n")
return 1
@@ -22,10 +27,11 @@ export class Ls extends Program {
console.log(item)
stdout.emit(`-> Listing contents of item: '${item.GetPath()}'\n`)
stdout.emit(` [ attr name ]\n`)
stdout.emit(` [ drwx name ]\n`)
const items = await item.List()
items.forEach((entry, i) => {
stdout.emit(` | ${''.padEnd(4, '-').padEnd(8, ' ')} ${entry.GetName()}\n`)
items.forEach(entry => {
stdout.emit(item.IsDirectory().toString())
stdout.emit(` | ${(item.IsDirectory() ? 'd' : '').padEnd(4, '-').padEnd(8, ' ')} ${entry.GetName()}\n`)
})
return 0

View File

@@ -8,12 +8,20 @@ export class Mkdir extends Program {
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
const item = await Item.openDir(args[1])
if (args.length < 2) {
stdout.emit("mkdir: error: missing path argument\n")
return 1
}
const item = await Item.openDir(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
if (await item.Exists()) {
stdout.emit(`mkdir: directory ${item.GetPath()} already exists.\n`)
return 1
return 2
}
item.Create()

View File

@@ -16,9 +16,13 @@ export class Rm extends Program {
let item: Item
try {
item = await Item.openDir(args[1])
item = await Item.openDir(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
} catch {
item = await Item.open(args[1])
item = await Item.open(args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
}
if (!(await item.Exists())) {

View File

@@ -10,8 +10,9 @@ export class Touch extends Program {
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
stdout.emit(`touching children, please wait...\n`)
const item = await Item.open(args[1])
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
const path = args.slice(1).join()
const item = await Item.open(path.includes('/') ? path : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${path}`)
stdout.emit(`does ${item.GetPath()} exist? ${await item.Exists()}\n`)
if (await item.Exists()) {
stdout.emit("touch: the file already exists.\n")