Skip to content

Commit

Permalink
Create consistent behavior for fs module across platforms
Browse files Browse the repository at this point in the history
Throw Error if path ends with `/` only on windows linux is behaving as expected.
As suggested by issues nodejs#17801
  • Loading branch information
timotew committed Jan 11, 2018
1 parent e9b532b commit db24f04
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ fs.readFile = function(path, options, callback) {
if (!nullCheck(path, callback))
return;

//doing justice between linux and windows for `/` char at the end of file path
if(isWindows && (path.slice(-1)==='/')){
throw new errors.Error('ERR_FS_EISDIR_PATH', path);
return;
}

var context = new ReadFileContext(callback, options.encoding);
context.isUserFd = isFd(path); // file descriptor ownership
var req = new FSReqWrap();
Expand Down Expand Up @@ -667,6 +673,12 @@ fs.readFileSync = function(path, options) {
var isUserFd = isFd(path); // file descriptor ownership
var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666);

//doing justice between linux and windows for `/` char at the end of file path
if(isWindows && (path.slice(-1)==='/')){
throw new errors.Error('ERR_FS_EISDIR_PATH', path);
return;
}

tryStatSync(fd, isUserFd);
// Use stats array directly to avoid creating an fs.Stats instance just for
// our internal use.
Expand Down Expand Up @@ -1517,6 +1529,12 @@ fs.writeFile = function(path, data, options, callback) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

//doing justice between linux and windows for `/` char at the end of file path
if(isWindows && (path.slice(-1)==='/')){
throw new errors.Error('ERR_FS_EISDIR_PATH', path);
return;
}

if (isFd(path)) {
writeFd(path, true);
return;
Expand All @@ -1543,6 +1561,12 @@ fs.writeFileSync = function(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

//doing justice between linux and windows for `/` char at the end of file path
if(isWindows && (path.slice(-1)==='/')){
throw new errors.Error('ERR_FS_EISDIR_PATH', path);
return;
}

var isUserFd = isFd(path); // file descriptor ownership
var fd = isUserFd ? path : fs.openSync(path, flag, options.mode);

Expand Down

0 comments on commit db24f04

Please sign in to comment.