Skip to content

Commit

Permalink
fix(rsync): utimens, rename, init root folder
Browse files Browse the repository at this point in the history
  • Loading branch information
divyenduz committed Oct 27, 2023
1 parent bfe42f9 commit dcc12c0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
15 changes: 1 addition & 14 deletions packages/fuse-client/syscalls/getattr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
});
Expand Down
15 changes: 14 additions & 1 deletion packages/fuse-client/syscalls/init.ts
Original file line number Diff line number Diff line change
@@ -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);
};
};
3 changes: 1 addition & 2 deletions packages/fuse-client/syscalls/rename.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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?
Expand Down
5 changes: 5 additions & 0 deletions packages/fuse-client/syscalls/utimens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};
31 changes: 31 additions & 0 deletions packages/sqlite-backend/SQLiteBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
};
}
}
}
2 changes: 1 addition & 1 deletion packages/zoid-fs-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit dcc12c0

Please sign in to comment.