Skip to content

Commit

Permalink
fix syncing utimes when copying files
Browse files Browse the repository at this point in the history
fs.utimesSync saves times with one second precision, while fs.statSync gives us microsecond precision.
This makes utimes not synchronized on filesystems with sub-second atime/ctime/mtime precision.
Upgraded to use newer fs.futimesSync interface providing correct precision.
  • Loading branch information
nalajcie committed Feb 24, 2016
1 parent e950787 commit 217819f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function(grunt) {
var fs = require('fs');
var chalk = require('chalk');
var fileSyncCmp = require('file-sync-cmp');
var isWindows = /^win/.test(process.platform);

grunt.registerMultiTask('copy', 'Copy files.', function() {

Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = function(grunt) {
};

var unixifyPath = function(filepath) {
if (process.platform === 'win32') {
if (isWindows) {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
Expand All @@ -119,6 +120,8 @@ module.exports = function(grunt) {
return;
}

fs.utimesSync(dest, stat.atime, stat.mtime);
var fd = fs.openSync(dest, isWindows ? 'r+' : 'r');
fs.futimesSync(fd, stat.atime, stat.mtime);
fs.closeSync(fd);
};
};
7 changes: 5 additions & 2 deletions test/copy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var grunt = require('grunt');
var fs = require('fs');
var isWindows = /^win/.test(process.platform);

exports.copy = {
main: function(test) {
Expand Down Expand Up @@ -64,8 +65,10 @@ exports.copy = {

modeDir: function(test) {
test.expect(2);
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder').mode.toString(8).slice(-3), '777');
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder/sub_folder').mode.toString(8).slice(-3), '777');
// on windows DIRs do not have 'executable' flag, see: https://github.com/nodejs/node/blob/master/deps/uv/src/win/fs.c#L1064
var expectedMode = isWindows ? '666' : '777';
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder').mode.toString(8).slice(-3), expectedMode);
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder/sub_folder').mode.toString(8).slice(-3), expectedMode);
test.done();
},

Expand Down

0 comments on commit 217819f

Please sign in to comment.