Skip to content

Commit

Permalink
Symlink dereference option for cp
Browse files Browse the repository at this point in the history
  • Loading branch information
eisnerd committed Feb 4, 2014
1 parent 284fc1c commit 2e0ab9d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ var os = require('os');
// Buffered file copy, synchronous
// (Using readFileSync() + writeFileSync() could easily cause a memory overflow
// with large files)
function copyFileSync(srcFile, destFile) {
function copyFileSync(srcFile, destFile, dereference) {
if (dereference) {
var srcFileStat = fs.lstatSync(srcFile);

if (srcFileStat.isSymbolicLink())
srcFile = fs.realpathSync(srcFile);
}

if (!fs.existsSync(srcFile))
common.error('copyFileSync: no such file or directory: ' + srcFile);

Expand Down Expand Up @@ -71,15 +78,15 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
if (srcFileStat.isDirectory()) {
/* recursion this thing right on back. */
cpdirSyncRecursive(srcFile, destFile, opts);
} else if (srcFileStat.isSymbolicLink()) {
} else if (!opts.dereference && srcFileStat.isSymbolicLink()) {
var symlinkFull = fs.readlinkSync(srcFile);
fs.symlinkSync(symlinkFull, destFile, os.platform() === "win32" ? "junction" : null);
} else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */
if (fs.existsSync(destFile) && !opts.force) {
common.log('skipping existing file: ' + files[i]);
} else {
copyFileSync(srcFile, destFile);
copyFileSync(srcFile, destFile, opts.dereference);
}
}

Expand All @@ -93,6 +100,7 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
//@ Available options:
//@
//@ + `-f`: force
//@ + `-L`: dereference; always follow symbolic links in source
//@ + `-r, -R`: recursive
//@
//@ Examples:
Expand All @@ -107,6 +115,7 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
function _cp(options, sources, dest) {
options = common.parseOptions(options, {
'f': 'force',
'L': 'dereference',
'R': 'recursive',
'r': 'recursive'
});
Expand Down Expand Up @@ -177,7 +186,7 @@ function _cp(options, sources, dest) {
if (e.code !== 'EEXIST') throw e;
}

cpdirSyncRecursive(src, newDest, {force: options.force});
cpdirSyncRecursive(src, newDest, {force: options.force, dereference: options.dereference});
}
return; // done with dir
}
Expand All @@ -195,7 +204,7 @@ function _cp(options, sources, dest) {
return; // skip file
}

copyFileSync(src, thisDest);
copyFileSync(src, thisDest, options.dereference);
}); // forEach(src)
}
module.exports = _cp;

1 comment on commit 2e0ab9d

@sparkleholic
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eisnerd: Is this published to npm ?
I just check shelljs@0.2.6 by npm install, but this is not applied in that version.

Please sign in to comment.