Skip to content

Commit

Permalink
Use native fs.copyFileSync in supported envs, add utimesMillisSync fu…
Browse files Browse the repository at this point in the history
…nction to util/utimes.js
  • Loading branch information
manidlou committed Nov 1, 2017
1 parent 0bd5279 commit 1abc2a3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ describeIf64('copySync', () => {
require(process.cwd()).emptyDir(TEST_DIR, done)
})

describe('> modification option', () => {
describe('> preserveTimestamps option', () => {
const SRC_FIXTURES_DIR = path.join(__dirname, './fixtures')
const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]

describe('> when modified option is turned off', () => {
describe('> when preserveTimestamps option is false', () => {
it('should have different timestamps on copy', () => {
const from = path.join(SRC_FIXTURES_DIR)
copySync(from, TEST_DIR, {preserveTimestamps: false})
FILES.forEach(testFile({preserveTimestamps: false}))
})
})

describe('> when modified option is turned on', () => {
describe('> when preserveTimestamps option is true', () => {
it('should have the same timestamps on copy', () => {
const from = path.join(SRC_FIXTURES_DIR)
copySync(from, TEST_DIR, {preserveTimestamps: true})
Expand Down
17 changes: 13 additions & 4 deletions lib/copy-sync/copy-file-sync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const fs = require('graceful-fs')
const utimesSync = require('../util/utimes.js').utimesMillisSync

const BUF_LENGTH = 64 * 1024
const _buff = require('../util/buffer')(BUF_LENGTH)
Expand All @@ -18,6 +19,17 @@ function copyFileSync (srcFile, destFile, options) {
} else return
}

if (typeof fs.copyFileSync === 'function') {
fs.copyFileSync(srcFile, destFile)
const st = fs.lstatSync(srcFile)
fs.chmodSync(destFile, st.mode)
if (preserveTimestamps) utimesSync(destFile, st.atime, st.mtime)
return undefined
}
return copyFileSyncFallback(srcFile, destFile, preserveTimestamps)
}

function copyFileSyncFallback (srcFile, destFile, preserveTimestamps) {
const fdr = fs.openSync(srcFile, 'r')
const stat = fs.fstatSync(fdr)
const fdw = fs.openSync(destFile, 'w', stat.mode)
Expand All @@ -30,10 +42,7 @@ function copyFileSync (srcFile, destFile, options) {
pos += bytesRead
}

if (preserveTimestamps) {
fs.futimesSync(fdw, stat.atime, stat.mtime)
}

if (preserveTimestamps) fs.futimesSync(fdw, stat.atime, stat.mtime)
fs.closeSync(fdr)
fs.closeSync(fdw)
}
Expand Down
6 changes: 3 additions & 3 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function copyFile (srcStat, src, dest, opts, cb) {
if (typeof fs.copyFile === 'function') {
return fs.copyFile(src, dest, err => {
if (err) return cb(err)
return handleDestModeAndTimestamps(srcStat, dest, opts, cb)
return setDestModeAndTimestamps(srcStat, dest, opts, cb)
})
}
return copyFileFallback(srcStat, src, dest, opts, cb)
Expand All @@ -114,10 +114,10 @@ function copyFileFallback (srcStat, src, dest, opts, cb) {
ws.on('error', err => cb(err))

ws.on('open', () => rs.pipe(ws))
.once('close', () => handleDestModeAndTimestamps(srcStat, dest, opts, cb))
.once('close', () => setDestModeAndTimestamps(srcStat, dest, opts, cb))
}

function handleDestModeAndTimestamps (srcStat, dest, opts, cb) {
function setDestModeAndTimestamps (srcStat, dest, opts, cb) {
fs.chmod(dest, srcStat.mode, err => {
if (err) return cb(err)
if (opts.preserveTimestamps) {
Expand Down
9 changes: 8 additions & 1 deletion lib/util/utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ function utimesMillis (path, atime, mtime, callback) {
})
}

function utimesMillisSync (path, atime, mtime) {
const fd = fs.openSync(path, 'r+')
fs.futimesSync(fd, atime, mtime)
return fs.closeSync(fd)
}

module.exports = {
hasMillisRes,
hasMillisResSync,
timeRemoveMillis,
utimesMillis
utimesMillis,
utimesMillisSync
}

0 comments on commit 1abc2a3

Please sign in to comment.