Skip to content

Commit

Permalink
fix!: Avoid error and reflect filesystem stat if futimes not implemen…
Browse files Browse the repository at this point in the history
…ted (#341)
  • Loading branch information
phated authored Jun 11, 2023
1 parent 9f907ba commit 9ba20fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ function updateMetadata(fd, file, callback) {
file.stat.atime = timesDiff.atime;
file.stat.mtime = timesDiff.mtime;
}
// If a filesystem doesn't implement futimes, we don't callback with the error.
// Instead we update the stats to match filesystem and clear the error.
if (futimesErr && futimesErr.code === 'ENOSYS') {
file.stat.atime = stat.atime;
file.stat.mtime = stat.mtime;
futimesErr = null;
}
if (ownerDiff) {
return owner(propagatedErr || futimesErr);
}
Expand Down
41 changes: 41 additions & 0 deletions test/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,47 @@ describe('updateMetadata', function () {
});
});

it('sets stats to filesystem stats if futimes is not implemented (issue 302)', function (done) {
if (isWindows) {
this.skip();
return;
}

var futimesSpy = sinon.stub(fs, 'futimes').callsFake(function () {
var callback = arguments[arguments.length - 1];
var err = new Error('ENOSYS: function not implemented, futime');
err.errno = -109;
err.code = 'ENOSYS';
err.syscall = 'futime';
callback(err);
});

var fd = fs.openSync(outputPath, 'w+');
expect(typeof fd === 'number').toEqual(true);

var stat = fs.fstatSync(fd);

var file = new File({
base: outputBase,
path: outputPath,
contents: null,
stat: {
mtime: new Date(stat.mtime - 1000),
atime: new Date(stat.atime - 1000),
},
});

updateMetadata(fd, file, function (err) {
expect(err).not.toEqual(expect.anything());
expect(futimesSpy.callCount).toEqual(1);
// Times were updated to match the filesystem since futimes wasn't implemented
expect(file.stat.mtime).toEqual(stat.mtime);
expect(file.stat.atime).toEqual(stat.atime);

fs.close(fd, done);
});
});

it('updates the mode on fs and vinyl object if there is a diff', function (done) {
if (isWindows) {
this.skip();
Expand Down

0 comments on commit 9ba20fd

Please sign in to comment.