Skip to content

Commit

Permalink
Revert "Prevent copying a directory into itself. Links to #4"
Browse files Browse the repository at this point in the history
This reverts commit 73eca73.
  • Loading branch information
AvianFlu committed Oct 2, 2014
1 parent 81b3d3b commit 1b94da1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 54 deletions.
18 changes: 3 additions & 15 deletions lib/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,12 @@ function ncp (source, dest, options, callback) {
started = 0,
finished = 0,
running = 0,
limit = options.limit || ncp.limit || 16,
selfCopyError = new Error('can\'t copy `' + targetPath + '` into `' + currentPath + '` itself');

selfCopyError.code = 'ESELF';

limit = (limit < 1) ? 1 : (limit > 512) ? 512 : limit;
limit = options.limit || ncp.limit || 16;

function startsWith(target, source) {
target = target === null ? '' : String(target);
return target.lastIndexOf(source, 0) === 0;
}

if (startsWith(targetPath, currentPath)) {
return cback(selfCopyError);
}
limit = (limit < 1) ? 1 : (limit > 512) ? 512 : limit;

startCopy(currentPath);

function startCopy(source) {
started++;
if (filter) {
Expand Down
59 changes: 20 additions & 39 deletions test/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,47 +170,28 @@ describe('ncp', function () {
});

describe('modified files copies', function () {
var fixtures = path.join(__dirname, 'modified-files'),
src = path.join(fixtures, 'src'),
out = path.join(fixtures, 'out');

it('if file not exists copy file to target', function(cb) {
rimraf(out, function() {
ncp(src, out, {modified: true, clobber: false}, function (err) {
assert.equal(fs.existsSync(out), true);
cb();
});
});
});

it('change source file mtime and copy', function(cb) {
fs.utimesSync(src+"/a", new Date().getTime()/1000, new Date('2015-01-01 00:00:00').getTime()/1000);
ncp(src, out, {modified: true, clobber: false}, function (err) {
fs.stat(out+"/a", function(err, stats) {
assert.equal(stats.mtime.getTime(), new Date('2015-01-01 00:00:00').getTime());
cb();
});
var fixtures = path.join(__dirname, 'modified-files'),
src = path.join(fixtures, 'src'),
out = path.join(fixtures, 'out');

it('if file not exists copy file to target', function(cb) {
rimraf(out, function() {
ncp(src, out, {modified: true, clobber: false}, function (err) {
assert.equal(fs.existsSync(out), true);
cb();
});
});
});
});
});

describe('sub directory handling', function () {
var fixtures = path.join(__dirname, 'sub-directory-fixtures'),
src = path.join(fixtures, 'src'),
out = path.join(fixtures, 'src/out');

beforeEach(function (cb) {
rimraf(out, function(){
fs.mkdir(out,cb);
it('change source file mtime and copy', function(cb) {
fs.utimesSync(src+"/a", new Date().getTime()/1000, new Date('2015-01-01 00:00:00').getTime()/1000);
ncp(src, out, {modified: true, clobber: false}, function (err) {
fs.stat(out+"/a", function(err, stats) {
assert.equal(stats.mtime.getTime(), new Date('2015-01-01 00:00:00').getTime());
cb();
});
});
});
});

it('returns an error when user copy the parent to itself', function (cb) {
ncp(src, out, function (err) {
assert.equal(err.code, 'ESELF');
cb();
})
});

});
});
});
Empty file removed test/sub-directory-fixtures/src/a
Empty file.
Empty file removed test/sub-directory-fixtures/src/b
Empty file.

0 comments on commit 1b94da1

Please sign in to comment.