Skip to content

Commit 8ae38ec

Browse files
authored
Merge pull request #120 from elegaanz/fix-enotdir
Throw an error if writing a file where there is already a directory
2 parents fc3d25c + 77c3cf4 commit 8ae38ec

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/CacheFS.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require("./path.js");
2-
const { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY } = require("./errors.js");
2+
const { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY, EISDIR } = require("./errors.js");
33

44
const STAT = 0;
55

@@ -164,13 +164,22 @@ module.exports = class CacheFS {
164164
}
165165
writeStat(filepath, size, { mode }) {
166166
let ino;
167+
let oldStat;
167168
try {
168-
let oldStat = this.stat(filepath);
169+
oldStat = this.stat(filepath);
170+
} catch (err) {}
171+
172+
if (oldStat !== undefined) {
173+
if (oldStat.type === 'dir') {
174+
throw new EISDIR();
175+
}
176+
169177
if (mode == null) {
170178
mode = oldStat.mode;
171179
}
172180
ino = oldStat.ino;
173-
} catch (err) {}
181+
}
182+
174183
if (mode == null) {
175184
mode = 0o666;
176185
}

src/__tests__/fs.promises.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ describe("fs.promises module", () => {
8585
});
8686
});
8787
});
88+
it("write file in place of an existing directory throws", done => {
89+
fs.mkdir("/writeFile").finally(() => {
90+
fs.writeFile("/writeFile", "HELLO")
91+
.then(() => {
92+
fail();
93+
done();
94+
})
95+
.catch(err => {
96+
expect(err).not.toBe(null);
97+
done();
98+
});
99+
});
100+
});
88101
});
89102

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

src/__tests__/fs.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ describe("fs module", () => {
7171
});
7272
});
7373
});
74+
it("write file in place of an existing directory throws", done => {
75+
fs.mkdir("/writeFile", err => {
76+
fs.writeFile("/writeFile", "HELLO", err => {
77+
expect(err).not.toBe(null);
78+
done();
79+
});
80+
});
81+
});
7482
});
7583

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

src/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ const ENOENT = Err("ENOENT");
1717
const ENOTDIR = Err("ENOTDIR");
1818
const ENOTEMPTY = Err("ENOTEMPTY");
1919
const ETIMEDOUT = Err("ETIMEDOUT");
20+
const EISDIR = Err("EISDIR");
2021

21-
module.exports = { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY, ETIMEDOUT };
22+
module.exports = { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY, ETIMEDOUT, EISDIR };

0 commit comments

Comments
 (0)