Skip to content

Commit d3f78e8

Browse files
fuzzyTewbilliegoose
authored andcommitted
fix: reading through symlinks to http-backed files (#36)
1 parent 0524f11 commit d3f78e8

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

src/CacheFS.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ module.exports = class CacheFS {
117117
if (follow || i < parts.length - 1) {
118118
const stat = dir.get(STAT)
119119
if (stat.type === 'symlink') {
120-
let target = stat.target
121-
if (!target.startsWith('/')) {
122-
target = path.normalize(path.join(partialPath, target))
123-
}
120+
let target = path.resolve(partialPath, stat.target)
124121
dir = this._lookup(target)
125122
}
126123
if (!partialPath) {

src/PromisifiedFS.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ module.exports = class PromisifiedFS {
168168
if (!this._urlauto) throw e
169169
}
170170
if (!data && this._http) {
171+
let lstat = this._cache.lstat(filepath)
172+
while (lstat.type === 'symlink') {
173+
filepath = path.resolve(path.dirname(filepath), lstat.target)
174+
lstat = this._cache.lstat(filepath)
175+
}
171176
data = await this._http.readFile(filepath)
172177
}
173178
if (data) {

src/__tests__/__fixtures__/test-folder/.superblock.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
d.txt 100644 14 1545621340683.4724
77
e.txt 100644 14 1545621349775.4539
88
f.txt 100644 14 1545621356008.1582
9+
2 40775
10+
a.txt 100664 14 1572953161955.4033
911
a.txt 100644 14 1545621375109
1012
b.txt 100644 14 1545621255760.9512
1113
c.txt 100644 14 1545621290070.7742
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from "a"

src/__tests__/fallback.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe("http fallback", () => {
1818
it("read root dir", done => {
1919
fs.readdir("/", (err, data) => {
2020
expect(err).toBe(null);
21-
expect(data).toEqual(['0', '1', 'a.txt', 'b.txt', 'c.txt'])
21+
expect(data).toEqual(['0', '1', '2', 'a.txt', 'b.txt', 'c.txt'])
2222
done();
2323
});
2424
});
@@ -58,6 +58,16 @@ describe("http fallback", () => {
5858
done();
5959
});
6060
});
61+
it("make a symlink and read file /2/a.txt through it", done => {
62+
fs.symlink("a.txt", "/2/symlink.txt", (err) => {
63+
expect(err).toBe(null);
64+
fs.readFile("/2/symlink.txt", 'utf8', (err, data) => {
65+
expect(err).toBe(null);
66+
expect(data).toEqual('Hello from "a"');
67+
done();
68+
});
69+
});
70+
});
6171
});
6272

6373
describe("writeFile", () => {

src/path.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ function normalizePath(path) {
77
return joinPath(...parts);
88
}
99

10+
function resolvePath(...paths) {
11+
let result = '';
12+
for (let path of paths) {
13+
if (path.startsWith('/')) {
14+
result = path;
15+
} else {
16+
result = normalizePath(joinPath(result, path));
17+
}
18+
}
19+
return result;
20+
}
21+
1022
function joinPath(...parts) {
1123
if (parts.length === 0) return "";
1224
let path = parts.join("/");
@@ -91,4 +103,5 @@ module.exports = {
91103
split: splitPath,
92104
basename,
93105
dirname,
106+
resolve: resolvePath,
94107
};

0 commit comments

Comments
 (0)