Skip to content

Commit

Permalink
fix: reading through symlinks to http-backed files (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzyTew authored and billiegoose committed Nov 16, 2019
1 parent 0524f11 commit d3f78e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/CacheFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ module.exports = class CacheFS {
if (follow || i < parts.length - 1) {
const stat = dir.get(STAT)
if (stat.type === 'symlink') {
let target = stat.target
if (!target.startsWith('/')) {
target = path.normalize(path.join(partialPath, target))
}
let target = path.resolve(partialPath, stat.target)
dir = this._lookup(target)
}
if (!partialPath) {
Expand Down
5 changes: 5 additions & 0 deletions src/PromisifiedFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ module.exports = class PromisifiedFS {
if (!this._urlauto) throw e
}
if (!data && this._http) {
let lstat = this._cache.lstat(filepath)
while (lstat.type === 'symlink') {
filepath = path.resolve(path.dirname(filepath), lstat.target)
lstat = this._cache.lstat(filepath)
}
data = await this._http.readFile(filepath)
}
if (data) {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__fixtures__/test-folder/.superblock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
d.txt 100644 14 1545621340683.4724
e.txt 100644 14 1545621349775.4539
f.txt 100644 14 1545621356008.1582
2 40775
a.txt 100664 14 1572953161955.4033
a.txt 100644 14 1545621375109
b.txt 100644 14 1545621255760.9512
c.txt 100644 14 1545621290070.7742
1 change: 1 addition & 0 deletions src/__tests__/__fixtures__/test-folder/2/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from "a"
12 changes: 11 additions & 1 deletion src/__tests__/fallback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("http fallback", () => {
it("read root dir", done => {
fs.readdir("/", (err, data) => {
expect(err).toBe(null);
expect(data).toEqual(['0', '1', 'a.txt', 'b.txt', 'c.txt'])
expect(data).toEqual(['0', '1', '2', 'a.txt', 'b.txt', 'c.txt'])
done();
});
});
Expand Down Expand Up @@ -58,6 +58,16 @@ describe("http fallback", () => {
done();
});
});
it("make a symlink and read file /2/a.txt through it", done => {
fs.symlink("a.txt", "/2/symlink.txt", (err) => {
expect(err).toBe(null);
fs.readFile("/2/symlink.txt", 'utf8', (err, data) => {
expect(err).toBe(null);
expect(data).toEqual('Hello from "a"');
done();
});
});
});
});

describe("writeFile", () => {
Expand Down
13 changes: 13 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ function normalizePath(path) {
return joinPath(...parts);
}

function resolvePath(...paths) {
let result = '';
for (let path of paths) {
if (path.startsWith('/')) {
result = path;
} else {
result = normalizePath(joinPath(result, path));
}
}
return result;
}

function joinPath(...parts) {
if (parts.length === 0) return "";
let path = parts.join("/");
Expand Down Expand Up @@ -91,4 +103,5 @@ module.exports = {
split: splitPath,
basename,
dirname,
resolve: resolvePath,
};

0 comments on commit d3f78e8

Please sign in to comment.