diff --git a/packages/fuse-client/syscalls/getattr.ts b/packages/fuse-client/syscalls/getattr.ts index a0c74df..269d1f3 100644 --- a/packages/fuse-client/syscalls/getattr.ts +++ b/packages/fuse-client/syscalls/getattr.ts @@ -7,20 +7,6 @@ export const getattr: (backend: SQLiteBackend) => MountOptions["getattr"] = ( ) => { return async (path, cb) => { console.log("getattr(%s)", path); - if (path === "/") { - cb(0, { - mtime: new Date(), - atime: new Date(), - ctime: new Date(), - nlink: 1, - size: 100, // Directory size, chosen arbitrarily - mode: 16877, - uid: process.getuid ? process.getuid() : 0, - gid: process.getgid ? process.getgid() : 0, - }); - return; - } - const r = await backend.getFile(path); await match(r) @@ -39,6 +25,7 @@ export const getattr: (backend: SQLiteBackend) => MountOptions["getattr"] = ( nlink: 1, size: rSize.size, mode: mode, + // TODO: enable posix mode where real uid/gid are returned uid: process.getuid ? process.getuid() : 0, gid: process.getgid ? process.getgid() : 0, }); diff --git a/packages/fuse-client/syscalls/init.ts b/packages/fuse-client/syscalls/init.ts index 4237592..94fd556 100644 --- a/packages/fuse-client/syscalls/init.ts +++ b/packages/fuse-client/syscalls/init.ts @@ -1,11 +1,24 @@ import { SQLiteBackend } from "@zoid-fs/sqlite-backend"; -import { MountOptions } from "@zoid-fs/node-fuse-bindings"; +import fuse, { MountOptions } from "@zoid-fs/node-fuse-bindings"; +import { match } from "ts-pattern"; export const init: (backend: SQLiteBackend) => MountOptions["init"] = ( backend ) => { return async (cb) => { console.log("init"); + + //@ts-expect-error fix types + const context = fuse.context(); + const { uid, gid } = context; + + const rootFolder = await backend.getFile("/"); + match(rootFolder) + .with({ status: "ok" }, () => {}) + .with({ status: "not_found" }, async () => { + await backend.createFile("/", "dir", 16877, uid, gid); + }) + .exhaustive(); cb(0); }; }; diff --git a/packages/fuse-client/syscalls/rename.ts b/packages/fuse-client/syscalls/rename.ts index 8e70b82..31314ac 100644 --- a/packages/fuse-client/syscalls/rename.ts +++ b/packages/fuse-client/syscalls/rename.ts @@ -1,6 +1,5 @@ import { SQLiteBackend } from "@zoid-fs/sqlite-backend"; import fuse, { MountOptions } from "@zoid-fs/node-fuse-bindings"; -import path from "path"; export const rename: (backend: SQLiteBackend) => MountOptions["rename"] = ( backend @@ -9,7 +8,7 @@ export const rename: (backend: SQLiteBackend) => MountOptions["rename"] = ( console.log("rename(%s, %s)", srcPath, destPath); const r = await backend.renameFile(srcPath, destPath); if (r.status === "ok") { - console.log("rename(%s, %s) success", srcPath, destPath); + console.log("rename(%s, %s)", srcPath, destPath); cb(0); } else { // TODO: can move fail, if yes, when? diff --git a/packages/fuse-client/syscalls/utimens.ts b/packages/fuse-client/syscalls/utimens.ts index b721b1c..14109ef 100644 --- a/packages/fuse-client/syscalls/utimens.ts +++ b/packages/fuse-client/syscalls/utimens.ts @@ -6,6 +6,11 @@ export const utimens: (backend: SQLiteBackend) => MountOptions["utimens"] = ( ) => { return async (path, atime, mtime, cb) => { console.log("utimens(%s, %s, %s)", path, atime, mtime); + try { + await backend.updateTimes(path, atime, mtime); + } catch (e) { + console.error(e); + } cb(0); }; }; diff --git a/packages/sqlite-backend/SQLiteBackend.ts b/packages/sqlite-backend/SQLiteBackend.ts index b8d18f7..02a1407 100644 --- a/packages/sqlite-backend/SQLiteBackend.ts +++ b/packages/sqlite-backend/SQLiteBackend.ts @@ -322,6 +322,14 @@ export class SQLiteBackend implements Backend { try { const parsedSrcPath = path.parse(srcPath); const parsedDestPath = path.parse(destPath); + + // Note: Delete if the destiantion path already exists + await this.prisma.file.deleteMany({ + where: { + path: destPath, + }, + }); + const file = await this.prisma.file.update({ where: { name: parsedSrcPath.base, @@ -339,6 +347,7 @@ export class SQLiteBackend implements Backend { file: file, }; } catch (e) { + console.error(e); return { // TODO: not_found is not the truth, it can fail for other reasons status: "not_found" as const, @@ -366,4 +375,26 @@ export class SQLiteBackend implements Backend { }; } } + + async updateTimes(filepath: string, atime: number, mtime: number) { + try { + const file = await this.prisma.file.update({ + where: { + path: filepath, + }, + data: { + atime: new Date(atime), + mtime: new Date(mtime), + }, + }); + return { + status: "ok" as const, + file: file, + }; + } catch (e) { + return { + status: "not_found" as const, + }; + } + } } diff --git a/packages/zoid-fs-client/package.json b/packages/zoid-fs-client/package.json index 6525036..4d65706 100644 --- a/packages/zoid-fs-client/package.json +++ b/packages/zoid-fs-client/package.json @@ -4,7 +4,7 @@ "license": "MIT", "type": "module", "scripts": { - "start": "vite-node --watch index.ts /home/divyenduz/Documents/zoid/vfs/1", + "start": "vite-node --watch index.ts /home/divyendusingh/zoid/vfs/1", "test:prepare": "vite-node --watch index.ts /home/div/code/vfs/test-fs --tenant test", "ci:setup-fuse": "vite-node --watch index.ts", "test": "vitest",