Skip to content

Commit

Permalink
bad broken symlink suppot I'll fix later
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerIn-wonderland committed Oct 30, 2024
1 parent 1858169 commit b73e018
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api/Filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ class AnuraFilesystem implements AnuraFSOperations<any> {
rmdir: (path: string) => this.processPath(path).promises.rmdir(path),
stat: (path: string) => this.processPath(path).promises.stat(path),
symlink: (srcPath: string, dstPath: string, type?: string) =>
this.processPath(srcPath).promises.symlink(srcPath, dstPath, type),
this.processPath(dstPath).promises.symlink(srcPath, dstPath, type),
truncate: (path: string, len: number) =>
this.processPath(path).promises.truncate(path, len),
unlink: (path: string) => this.processPath(path).promises.unlink(path),
Expand Down
58 changes: 54 additions & 4 deletions src/api/LocalFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class LocalFS extends AFSProvider<LocalFSStats> {
path: string,
recurseCounter = 0,
): Promise<[FileSystemDirectoryHandle, string]> {
console.log("Fetching dir handle for " + path);
if (recurseCounter > 20) {
throw {
name: "ELOOP",
Expand All @@ -89,6 +90,7 @@ class LocalFS extends AFSProvider<LocalFSStats> {
let acc = this.dirHandle;
let curr = "";
for await (const part of path.split("/")) {
if (part === "") continue;
curr += "/" + part;
if ((this.stats.get(curr)?.mode & 0o170000) === 0o120000) {
// We ran into a path symlink, we're storing symlinks of all types as files who's content is the target.
Expand All @@ -106,7 +108,12 @@ class LocalFS extends AFSProvider<LocalFSStats> {
);
}
}

console.log(
"Getting dir handle " +
part +
" on " +
curr.split("/").slice(0, -1).join("/"),
);
acc = await acc.getDirectoryHandle(part);
}
return [acc, curr];
Expand All @@ -116,6 +123,10 @@ class LocalFS extends AFSProvider<LocalFSStats> {
options?: FileSystemGetFileOptions,
recurseCounter = 0,
): Promise<[FileSystemFileHandle, string]> {
if (!path.includes("/")) {
path = "/" + path;
}

const parentFolder = path.slice(0, path.lastIndexOf("/"));
const [parentHandle, realPath] =
await this.getChildDirHandle(parentFolder);
Expand Down Expand Up @@ -660,9 +671,12 @@ class LocalFS extends AFSProvider<LocalFSStats> {
readlink: async (path: string) => {
// Check if the path exists in stats
path = this.relativizePath(path);
path = Filer.Path.normalize(path);
const fileName = path.slice(path.lastIndexOf("/") + 1);
const [parentHandle, realParent] = await this.getChildDirHandle(
path.slice(0, path.lastIndexOf("/")),
);
const stats = this.stats.get(realParent + "/" + fileName);

const stats = this.stats.get(path);
if (!stats) {
throw {
name: "ENOENT",
Expand All @@ -672,13 +686,49 @@ class LocalFS extends AFSProvider<LocalFSStats> {
stack: "Error: No such file",
} as Error;
}
if (!((stats.mode & 0o170000) === 0o120000)) {
throw {
// I think this is the wrong error type
name: "EINVAL",
code: "EINVAL",
errno: -22,
message: `Is not a symbolic link: ${path}`,
stack: "Error: Is not a symbolic link",
} as Error;
}

// Return the target path
return stats.target;
return await (
await (await parentHandle.getFileHandle(fileName)).getFile()
).text();
},
symlink: async (target: string, path: string) => {
// await this.promises.stat(path);
// Save stats and resolve the promise
path = this.relativizePath(path);
if (target.startsWith("/")) {
target = this.relativizePath(target);
}

const fileName = path.slice(path.lastIndexOf("/") + 1);
const [parentHandle, realParent] = await this.getChildDirHandle(
path.slice(0, path.lastIndexOf("/")),
);
const fileHandleWritable = await (
await parentHandle.getFileHandle(fileName, { create: true })
).createWritable();
fileHandleWritable.write(target);
fileHandleWritable.close();

const fullPath = realParent + "/" + fileName;
const fileStats = this.stats.get(fullPath) || {};
if (fullPath) {
fileStats.mode = 41380;
fileStats.ctimeMs = Date.now();
fileStats.mtimeMs = Date.now();
this.stats.set(path, fileStats);
}

await this.promises.saveStats();
return;
},
Expand Down

0 comments on commit b73e018

Please sign in to comment.