Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback is called before file is copied #143

Open
Soulike opened this issue Nov 26, 2021 · 0 comments
Open

Callback is called before file is copied #143

Soulike opened this issue Nov 26, 2021 · 0 comments

Comments

@Soulike
Copy link

Soulike commented Nov 26, 2021

The bug is caused by following code:

ncp/lib/ncp.js

Lines 89 to 109 in 6820b0f

isWritable(target, function (writable) {
if (writable) {
return copyFile(file, target);
}
if(clobber) {
rmFile(target, function () {
copyFile(file, target);
});
}
if (modified) {
var stat = dereference ? fs.stat : fs.lstat;
stat(target, function(err, stats) {
//if souce modified time greater to target modified time copy file
if (file.mtime.getTime()>stats.mtime.getTime())
copyFile(file, target);
else return cb();
});
}
else {
return cb();
}

As we can see, in line 108, cb() can be called before rmFile() and copyFile() finish their jobs. So if target file exists, cb() will be called before file is copied.

The test case below can reproduce the bug:

const destination = path.join(os.tmpdir(), 'bigFile');

ncp(someFile, destination, function (err)
{
    if (err)
    {
        return console.error(err);
    }
    ncp(someFile, destination, function (err)
    {
        if (err)
        {
            return console.error(err);
        }
        fs.readFileSync(destination);    // Error: ENOENT: no such file or directory, open '/tmp/bigFile'
    });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant