Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Available options:
+ `-f`: force (default behavior)
+ `-n`: no-clobber
+ `-r`, `-R`: recursive
+ `-L`, `-L`: followsymlink

Examples:

Expand Down
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var config = {
silent: false,
fatal: false,
verbose: false,
noglob: false
noglob: false,
maxdepth: 255
};
exports.config = config;

Expand Down
49 changes: 45 additions & 4 deletions src/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ function copyFileSync(srcFile, destFile) {
function cpdirSyncRecursive(sourceDir, destDir, opts) {
if (!opts) opts = {};

/* Ensure there is not a run away recursive copy. */
if (typeof opts.depth === 'undefined') {
opts.depth = 0;
}
if (opts.depth >= common.config.maxdepth) {
// Max depth has been reached, end copy.
return;
} else {
opts.depth++;
}

/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
try {
var checkDir = fs.statSync(sourceDir);
Expand All @@ -68,12 +79,24 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
var destFile = destDir + "/" + files[i];
var srcFileStat = fs.lstatSync(srcFile);

var symlinkFull;
if (opts.followsymlink) {
if (cpcheckcycle(sourceDir, srcFile)) {
// Cycle link found.
console.log('Cycle link found.');
symlinkFull = fs.readlinkSync(srcFile);
fs.symlinkSync(symlinkFull, destFile, os.platform() === "win32" ? "junction" : null);
continue;
}
}
if (srcFileStat.isDirectory()) {
/* recursion this thing right on back. */
cpdirSyncRecursive(srcFile, destFile, opts);
} else if (srcFileStat.isSymbolicLink()) {
var symlinkFull = fs.readlinkSync(srcFile);
} else if (srcFileStat.isSymbolicLink() && !opts.followsymlink) {
symlinkFull = fs.readlinkSync(srcFile);
fs.symlinkSync(symlinkFull, destFile, os.platform() === "win32" ? "junction" : null);
} else if (srcFileStat.isSymbolicLink() && opts.followsymlink) {
cpdirSyncRecursive(srcFile, destFile, opts);
} else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */
if (fs.existsSync(destFile) && opts.no_force) {
Expand All @@ -86,6 +109,22 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
} // for files
} // cpdirSyncRecursive

function cpcheckcycle(sourceDir, srcFile) {
var srcFileStat = fs.lstatSync(srcFile);
if (srcFileStat.isSymbolicLink()) {
// Do cycle check. For example mkdir -p 1/2/3/4 ; cd 1/2/3/4; ln -s ../../3 link ; cd ../../../.. ; cp -RL 1 copy
var cyclecheck = fs.statSync(srcFile);
if (cyclecheck.isDirectory()) {
var sourcerealpath = fs.realpathSync(sourceDir);
var symlinkrealpath = fs.realpathSync(srcFile);
var re = new RegExp(symlinkrealpath);
if (re.test(sourcerealpath)) {
return true;
}
}
}
return false;
}

//@
//@ ### cp([options,] source [, source ...], dest)
Expand All @@ -95,6 +134,7 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
//@ + `-f`: force (default behavior)
//@ + `-n`: no-clobber
//@ + `-r`, `-R`: recursive
//@ + `-L`, `-L`: followsymlink
//@
//@ Examples:
//@
Expand All @@ -111,7 +151,8 @@ function _cp(options, sources, dest) {
'f': '!no_force',
'n': 'no_force',
'R': 'recursive',
'r': 'recursive'
'r': 'recursive',
'L': 'followsymlink',
});

// Get sources, dest
Expand Down Expand Up @@ -152,7 +193,7 @@ function _cp(options, sources, dest) {

try {
fs.statSync(path.dirname(dest));
cpdirSyncRecursive(src, newDest, {no_force: options.no_force});
cpdirSyncRecursive(src, newDest, {no_force: options.no_force, followsymlink: options.followsymlink});
} catch(e) {
common.error("cannot create directory '" + dest + "': No such file or directory");
}
Expand Down
33 changes: 33 additions & 0 deletions test/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,37 @@ assert.ok(!fs.existsSync('tmp/cp')); // doesn't copy dir itself
assert.ok(fs.existsSync('tmp/file1.txt'));
assert.ok(fs.existsSync('tmp/file2.txt'));

// Test max depth.
shell.rm('-rf', 'tmp/');
shell.mkdir('tmp/');
shell.config.maxdepth = 32;
var directory = '';
for (var i = 1; i < 40; i++) {
directory += '/'+i;
}
var directory32deep = '';
for (var i = 1; i < 32; i++) {
directory32deep += '/'+i;
}
shell.mkdir('-p', 'tmp/0' + directory);
shell.cp('-r', 'tmp/0', 'tmp/copytestdepth');
// Check full directory exists.
assert.ok(shell.test('-d', 'tmp/0/' + directory));
// Check full copy of directory does not exist.
assert.ok(!shell.test('-d', 'tmp/copytestdepth'+directory));
// Check last directory to exist is bellow maxdepth.
assert.ok(shell.test('-d', 'tmp/copytestdepth'+directory32deep));
assert.ok(!shell.test('-d', 'tmp/copytestdepth'+directory32deep+'/32'));

// Create sym links to check for cycle.
shell.cd('tmp/0/1/2/3/4');
shell.ln('-s', '../../../2', 'link');
shell.ln('-s', './5/6/7', 'link1');
shell.cd('../../../../../..');
assert.ok(shell.test('-d', 'tmp/'));

shell.rm('-fr', 'tmp/copytestdepth');
shell.cp('-r', 'tmp/0', 'tmp/copytestdepth');
assert.ok(shell.test('-d', 'tmp/copytestdepth/1/2/3/4/link/3/4/link/3/4'));

shell.exit(123);