Skip to content

Commit

Permalink
writeFileSync should also support mode optional property, and added t…
Browse files Browse the repository at this point in the history
…est for both appendFileSync and writeFileSync
  • Loading branch information
CMCDragonkai committed Nov 20, 2017
1 parent 2ec684e commit d4c517d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/VirtualFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -1856,14 +1856,18 @@ class VirtualFS {
}

writeFileSync (file: file, data: data = 'undefined', options?: options) {
options = this._getOptions({encoding: 'utf8', flag: 'w'}, options);
options = this._getOptions({
encoding: 'utf8',
mode: DEFAULT_FILE_PERM,
flag: 'w'
}, options);
let fdIndex;
try {
const buffer = this._getBuffer(data, options.encoding);
if (typeof file === 'number') {
this.writeSync(file, buffer, 0, buffer.length, 0);
} else {
fdIndex = this.openSync(file, options.flag);
fdIndex = this.openSync(file, options.flag, options.mode);
this.writeSync(fdIndex, buffer, 0, buffer.length, 0);
}
} finally {
Expand Down
22 changes: 22 additions & 0 deletions test/VirtualFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,28 @@ test('changing file permissions does not affect already opened file descriptor',
fs.closeSync(fd);
});

test('writeFileSync and appendFileSync respects the mode', t => {
const fs = new VirtualFS;
let stat;
let error;
// allow others to read only
fs.writeFileSync('/test1', '', { mode: 0o004 });
fs.appendFileSync('/test2', '', { mode: 0o004 });
// become the other
fs.setUid(1000);
fs.setGid(1000);
fs.accessSync('/test1', fs.constants.R_OK);
error = t.throws(() => {
fs.accessSync('/test1', fs.constants.W_OK);
});
t.is(error.code, 'EACCES');
fs.accessSync('/test2', fs.constants.R_OK);
error = t.throws(() => {
fs.accessSync('/test1', fs.constants.W_OK);
});
t.is(error.code, 'EACCES');
});

/////////////////////////////
// Uint8Array data support //
/////////////////////////////
Expand Down

0 comments on commit d4c517d

Please sign in to comment.