Skip to content

Commit

Permalink
fix: throw EEXIST when opening file that already exists with 'wx' flag (
Browse files Browse the repository at this point in the history
#853)

* fix: throw EEXIST if open exisiting file with exclude flag

* fix: check exclude flag before creating link in openFile
  • Loading branch information
MrSquaare committed Jun 18, 2022
1 parent e2b0adb commit 8b021b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ describe('volume', () => {
done();
});
});
it('Error with exclude flag if file exists', (done) => {
vol.writeFileSync('/existing-file.txt', 'foo');
vol.open('/existing-file.txt', 'wx', (err) => {
expect(err).toHaveProperty('code', 'EEXIST');
done();
});
});
it('Invalid path correct error code thrown synchronously', done => {
try {
(vol as any).open(123, 'r', (err, fd) => {
Expand Down Expand Up @@ -599,6 +606,14 @@ describe('volume', () => {
done();
});
});
it('Create a file at root (/writeFile2.json) with exclude flag', done => {
vol.writeFile('/writeFile2.json', data, { flag: "wx" }, err => {
expect(err).toBe(null);
const str = tryGetChildNode(vol.root, 'writeFile2.json').getString();
expect(str).toBe(data);
done();
});
});
it('Throws error when no callback provided', () => {
try {
vol.writeFile('/asdf.txt', 'asdf', 'utf8', undefined as any);
Expand Down
2 changes: 2 additions & 0 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ export class Volume {
const steps = filenameToSteps(filename);
let link: Link | null = resolveSymlinks ? this.getResolvedLink(steps) : this.getLink(steps);

if (link && flagsNum & O_EXCL) throw createError(EEXIST, 'open', filename);

// Try creating a new file, if it does not exist.
if (!link && flagsNum & O_CREAT) {
// const dirLink: Link = this.getLinkParent(steps);
Expand Down

0 comments on commit 8b021b3

Please sign in to comment.