Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Avoid error and reflect filesystem stat if futimes not implemented #341

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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