Skip to content

Commit

Permalink
refactor: new link table for file name
Browse files Browse the repository at this point in the history
  • Loading branch information
divyenduz committed Nov 1, 2023
1 parent 9a3a42c commit 3002580
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 190 deletions.
29 changes: 13 additions & 16 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { File } from "@prisma/client";
import { File, Link } from "@prisma/client";

type FileType = "file" | "dir" | "symlink";

type Result<T> =
| {
type Result<T, K extends string = "file"> =
| ({
status: "ok";
file: T;
}
} & { [P in K]: T })
| {
status: "not_found";
};

// TODO: bump this based on the latest state of the actual backend!

export interface Backend {
getFiles: (dir: string) => Promise<File[]>;
getFileRaw: (filepath: string) => Promise<Result<File>>;
getFileResolved: (filepath: string) => Promise<Result<File>>;
// TODO: use resule
getLinks: (dir: string) => Promise<Link[]>;
getLink: (dir: string) => Promise<Result<Link, "link">>;
getFile: (filepath: string) => Promise<Result<File>>;

createFile: (
filepath: string,
Expand All @@ -27,13 +27,10 @@ export interface Backend {
targetPath: string
) => Promise<Result<File>>;

writeFile: (
filepath: string,
uid: number,
gid: number
) => Promise<Result<File>>;

deleteFile: (filepath: string) => Promise<Result<number>>;
renameFile: (srcPath: string, destPath: string) => Promise<Result<File>>;
deleteFile: (filepath: string) => Promise<Result<number, "count">>;
renameFile: (
srcPath: string,
destPath: string
) => Promise<Result<Link, "link">>;
updateMode: (filepath: string, mode: number) => Promise<Result<File>>;
}
2 changes: 1 addition & 1 deletion packages/fuse-client/syscalls/getattr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getattr: (backend: SQLiteBackend) => MountOptions["getattr"] = (
) => {
return async (path, cb) => {
console.info("getattr(%s)", path);
const r = await backend.getFileResolved(path);
const r = await backend.getFile(path);
await match(r)
.with({ status: "ok" }, async (r) => {
const rSize = await backend.getFileSize(path);
Expand Down
2 changes: 1 addition & 1 deletion packages/fuse-client/syscalls/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const init: (backend: SQLiteBackend) => MountOptions["init"] = (
const context = fuse.context();
const { uid, gid } = context;

const rootFolder = await backend.getFileResolved("/");
const rootFolder = await backend.getFile("/");
match(rootFolder)
.with({ status: "ok" }, () => {})
.with({ status: "not_found" }, async () => {
Expand Down
14 changes: 1 addition & 13 deletions packages/fuse-client/syscalls/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,9 @@ export const link: (backend: SQLiteBackend) => MountOptions["link"] = (

// TODO: throw if destination doesn't exist

//@ts-expect-error fix types
const context = fuse.context();
const { uid, gid } = context;

// TODO: double check if mode for link is correct
// https://unix.stackexchange.com/questions/193465/what-file-mode-is-a-link
const r = await backend.createFile(
destPath,
"link",
41453, // Link's mode??? from node-fuse-binding source, why though?
uid,
gid,
srcPath
);
console.log({ r });
const r = await backend.createLink(srcPath, destPath);
match(r)
.with({ status: "ok" }, () => {
cb(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/fuse-client/syscalls/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const open: (backend: SQLiteBackend) => MountOptions["open"] = (
) => {
return async (path, flags, cb) => {
console.info("open(%s, %d)", path, flags);
const r = await backend.getFileResolved(path);
const r = await backend.getFile(path);

match(r)
.with({ status: "ok" }, (r) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/fuse-client/syscalls/opendir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const opendir: (backend: SQLiteBackend) => MountOptions["opendir"] = (
return;
}

const r = await backend.getFileResolved(path);
const r = await backend.getFile(path);
match(r)
.with({ status: "ok" }, (r) => {
cb(0, r.file.id);
Expand Down
4 changes: 2 additions & 2 deletions packages/fuse-client/syscalls/readdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const readdir: (backend: SQLiteBackend) => MountOptions["readdir"] = (
// TODO: figure out how are these directories in output of ls -la
const dotDirs = [".", ".."];

const files = await backend.getFiles(path);
const fileNames = dotDirs.concat(files.map((file) => file.name));
const links = await backend.getLinks(path);
const fileNames = dotDirs.concat(links.map((link) => link.name));

return cb(0, fileNames);
};
Expand Down
27 changes: 17 additions & 10 deletions packages/fuse-client/syscalls/readlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ export const readlink: (backend: SQLiteBackend) => MountOptions["readlink"] = (
) => {
return async (path, cb) => {
console.info("readlink(%s)", path);
const r = await backend.getFileRaw(path);
match(r)
.with({ status: "ok" }, (r) => {
cb(0, r.file.targetPath);
})
.with({ status: "not_found" }, () => {
//@ts-expect-error fix types, what to do if readlink fails?
cb(fuse.ENOENT);
})
.exhaustive();
try {
const r = await backend.getLink(path);
match(r)
.with({ status: "ok" }, (r) => {
cb(0, r.link.targetPath);
})
.with({ status: "not_found" }, () => {
//@ts-expect-error fix types, what to do if readlink fails?
cb(fuse.ENOENT);
})
.exhaustive();
} catch (e) {
console.error(e);
return {
status: "not_found" as const,
};
}
};
};
17 changes: 16 additions & 1 deletion packages/fuse-client/syscalls/symlink.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { SQLiteBackend } from "@zoid-fs/sqlite-backend";
import fuse, { MountOptions } from "@zoid-fs/node-fuse-bindings";
import { match } from "ts-pattern";
import { constants } from "fs";
import path from "path";

export const symlink: (backend: SQLiteBackend) => MountOptions["symlink"] = (
backend
) => {
return async (srcPath, destPath, cb) => {
console.info("symlink(%s, %s)", srcPath, destPath);

const parsedDestPath = path.parse(destPath);
// Note: actually 255 as per the spec but we get an extra / in the dest path
if (parsedDestPath.base.length > 255) {
cb(fuse.ENAMETOOLONG);
return;
}

// Note: actually 1023 as per the spec but we get an extra / in the dest path
if (srcPath.length > 1023 || destPath.length > 1023) {
cb(fuse.ENAMETOOLONG);
return;
}

//@ts-expect-error fix types
const context = fuse.context();
const { uid, gid } = context;
Expand All @@ -17,7 +32,7 @@ export const symlink: (backend: SQLiteBackend) => MountOptions["symlink"] = (
const r = await backend.createFile(
destPath,
"symlink",
33188,
constants.S_IFLNK,
uid,
gid,
srcPath
Expand Down
Loading

0 comments on commit 3002580

Please sign in to comment.