Skip to content
55 changes: 42 additions & 13 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,12 @@ fs.openSync = function(path, flags, mode) {
validatePath(path);
validateUint32(mode, 'mode');

return binding.open(pathModule.toNamespacedPath(path),
stringToFlags(flags), mode);
const ctx = { path };
const result = binding.open(pathModule.toNamespacedPath(path),
stringToFlags(flags), mode,
undefined, ctx);
handleErrorFromBinding(ctx);
return result;
};

fs.read = function(fd, buffer, offset, length, position, callback) {
Expand Down Expand Up @@ -802,7 +806,9 @@ fs.rmdir = function(path, callback) {
fs.rmdirSync = function(path) {
path = getPathFromURL(path);
validatePath(path);
return binding.rmdir(pathModule.toNamespacedPath(path));
const ctx = { path };
binding.rmdir(pathModule.toNamespacedPath(path), undefined, ctx);
handleErrorFromBinding(ctx);
};

fs.fdatasync = function(fd, callback) {
Expand Down Expand Up @@ -851,7 +857,9 @@ fs.mkdirSync = function(path, mode) {
validatePath(path);
mode = modeNum(mode, 0o777);
validateUint32(mode, 'mode');
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
const ctx = { path };
binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx);
handleErrorFromBinding(ctx);
};

fs.readdir = function(path, options, callback) {
Expand All @@ -869,7 +877,11 @@ fs.readdirSync = function(path, options) {
options = getOptions(options, {});
path = getPathFromURL(path);
validatePath(path);
return binding.readdir(pathModule.toNamespacedPath(path), options.encoding);
const ctx = { path };
const result = binding.readdir(pathModule.toNamespacedPath(path),
options.encoding, undefined, ctx);
handleErrorFromBinding(ctx);
return result;
};

fs.fstat = function(fd, callback) {
Expand Down Expand Up @@ -1096,7 +1108,9 @@ fs.chmodSync = function(path, mode) {
validatePath(path);
mode = modeNum(mode);
validateUint32(mode, 'mode');
return binding.chmod(pathModule.toNamespacedPath(path), mode);
const ctx = { path };
binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx);
handleErrorFromBinding(ctx);
};

if (constants.O_SYMLINK !== undefined) {
Expand Down Expand Up @@ -1164,7 +1178,9 @@ fs.chownSync = function(path, uid, gid) {
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
};

// exported for unit tests, not for public consumption
Expand All @@ -1186,9 +1202,11 @@ fs.utimes = function(path, atime, mtime, callback) {
fs.utimesSync = function(path, atime, mtime) {
path = getPathFromURL(path);
validatePath(path);
const ctx = { path };
binding.utimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime));
toUnixTimestamp(atime), toUnixTimestamp(mtime),
undefined, ctx);
handleErrorFromBinding(ctx);
};

fs.futimes = function(fd, atime, mtime, callback) {
Expand Down Expand Up @@ -1699,7 +1717,10 @@ fs.realpathSync.native = function(path, options) {
options = getOptions(options, {});
path = getPathFromURL(path);
validatePath(path);
return binding.realpath(path, options.encoding);
const ctx = { path };
const result = binding.realpath(path, options.encoding, undefined, ctx);
handleErrorFromBinding(ctx);
return result;
};


Expand Down Expand Up @@ -1872,7 +1893,12 @@ fs.mkdtempSync = function(prefix, options) {
prefix);
}
nullCheck(prefix, 'prefix');
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding);
const path = `${prefix}XXXXXX`;
const ctx = { path };
const result = binding.mkdtemp(path, options.encoding,
undefined, ctx);
handleErrorFromBinding(ctx);
return result;
};


Expand All @@ -1887,7 +1913,7 @@ fs.copyFile = function(src, dest, flags, callback) {
callback = flags;
flags = 0;
} else if (typeof callback !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'callback', 'Function');
throw new errors.TypeError('ERR_INVALID_CALLBACK');
}

src = getPathFromURL(src);
Expand All @@ -1910,10 +1936,13 @@ fs.copyFileSync = function(src, dest, flags) {
validatePath(src, 'src');
validatePath(dest, 'dest');

const ctx = { path: src, dest }; // non-prefixed

src = pathModule._makeLong(src);
dest = pathModule._makeLong(dest);
flags = flags | 0;
binding.copyFile(src, dest, flags);
binding.copyFile(src, dest, flags, undefined, ctx);
handleErrorFromBinding(ctx);
};


Expand Down
Loading