Skip to content

Commit 587c741

Browse files
committed
fix: fix awful inode autoincrement bug
1 parent d7ab3c0 commit 587c741

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/CacheFS.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = class CacheFS {
88
this._root = new Map([["/", this._makeRoot()]]);
99
}
1010
_makeRoot(root = new Map()) {
11-
root.set(STAT, { mode: 0o777, type: "dir", size: 0, mtimeMs: Date.now() });
11+
root.set(STAT, { mode: 0o777, type: "dir", size: 0, ino: 0, mtimeMs: Date.now() });
1212
return root
1313
}
1414
loadSuperBlock(superblock) {
@@ -35,10 +35,10 @@ module.exports = class CacheFS {
3535
return val;
3636
}
3737
_maxInode(map) {
38-
let max = 0;
38+
let max = map.get(STAT).ino;
3939
for (let [key, val] of map) {
4040
if (key === STAT) continue;
41-
max = Math.max(max, val.get(STAT).ino);
41+
max = Math.max(max, this._maxInode(val));
4242
}
4343
return max;
4444
}
@@ -137,23 +137,28 @@ module.exports = class CacheFS {
137137
return [...dir.keys()].filter(key => typeof key === "string");
138138
}
139139
writeFile(filepath, data, { mode }) {
140-
let oldStat;
140+
let ino;
141141
try {
142-
oldStat = this.stat(filepath);
142+
let oldStat = this.stat(filepath);
143+
if (mode === null) {
144+
mode = oldStat.mode;
145+
}
146+
ino = oldStat.ino;
143147
} catch (err) {}
144-
if (oldStat && mode == null) {
145-
mode = oldStat.mode;
146-
} else if (mode == null) {
148+
if (mode == null) {
147149
mode = 0o666;
148150
}
151+
if (ino == null) {
152+
ino = this.autoinc();
153+
}
149154
let dir = this._lookup(path.dirname(filepath));
150155
let basename = path.basename(filepath);
151156
let stat = {
152157
mode,
153158
type: "file",
154159
size: data.length,
155160
mtimeMs: Date.now(),
156-
ino: this.autoinc(),
161+
ino,
157162
};
158163
let entry = new Map();
159164
entry.set(STAT, stat);

src/__tests__/CacheFS.spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import CacheFS from "../CacheFS";
22

3-
const fs = new CacheFS();
4-
53
const treeText = require('./__fixtures__/tree.txt.js');
64

75
describe("CacheFS module", () => {
86
it("print ∘ parse == id", () => {
7+
const fs = new CacheFS();
98
let parsed = fs.parse(treeText)
109
let text = fs.print(parsed)
1110
expect(text).toEqual(treeText)
1211
});
1312
it("size()", () => {
13+
const fs = new CacheFS();
1414
expect(fs.size()).toEqual(0)
1515
fs.loadSuperBlock(treeText)
1616
let inodeCount = treeText.trim().split('\n').length
1717
expect(fs.size()).toEqual(inodeCount)
1818
});
19+
it("autoinc()", () => {
20+
const fs = new CacheFS();
21+
expect(fs.autoinc()).toEqual(1)
22+
fs.writeFile('/foo', 'bar', {})
23+
expect(fs.autoinc()).toEqual(2)
24+
fs.mkdir('/bar', {})
25+
expect(fs.autoinc()).toEqual(3)
26+
fs.unlink('/foo')
27+
expect(fs.autoinc()).toEqual(3)
28+
fs.mkdir('/bar/baz', {})
29+
expect(fs.autoinc()).toEqual(4)
30+
fs.rmdir('/bar/baz')
31+
expect(fs.autoinc()).toEqual(3)
32+
fs.mkdir('/bar/bar', {})
33+
expect(fs.autoinc()).toEqual(4)
34+
fs.writeFile('/bar/bar/boo', 'bar', {})
35+
expect(fs.autoinc()).toEqual(5)
36+
fs.unlink('/bar/bar/boo')
37+
expect(fs.autoinc()).toEqual(4)
38+
});
1939
});

src/__tests__/fs.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ describe("fs module", () => {
5252
});
5353
});
5454
});
55+
it("write file perserves old inode", done => {
56+
fs.mkdir("/writeFile", err => {
57+
fs.writeFile("/writeFile/writeFile-inode.txt", "HELLO", err => {
58+
expect(err).toBe(null);
59+
fs.stat("/writeFile/writeFile-inode.txt", (err, stats) => {
60+
expect(err).toBe(null);
61+
let inode = stats.ino;
62+
fs.writeFile("/writeFile/writeFile-inode.txt", "WORLD", err => {
63+
expect(err).toBe(null);
64+
fs.stat("/writeFile/writeFile-inode.txt", (err, stats) => {
65+
expect(err).toBe(null);
66+
expect(stats.ino).toEqual(inode);
67+
done();
68+
});
69+
});
70+
});
71+
});
72+
});
73+
});
5574
});
5675

5776
describe("readFile", () => {

0 commit comments

Comments
 (0)