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

mv slow, always uses copyFile, even if fs.rename is possible #158

Closed
anneb opened this issue Aug 11, 2019 · 5 comments
Closed

mv slow, always uses copyFile, even if fs.rename is possible #158

anneb opened this issue Aug 11, 2019 · 5 comments

Comments

@anneb
Copy link
Contributor

anneb commented Aug 11, 2019

After uploading a large file (using tempfiles), the mv function may take quite some time, because the tempfile is being copied instead of being renamed. This takes more time and requires more available disk space.

Pull request #157 tries to rename the file and resort to copyFile only if the rename should fail.

@RomanBurunkov
Copy link
Collaborator

Hi @anneb , from the best of my knowledge that was changed from move to copy because mv method doesn't work if you try to mv file between different partitions.

Does your change work for such cases?

@anneb
Copy link
Contributor Author

anneb commented Sep 9, 2019

Indeed, the standard rename method does not work across partitions. For this reason, the code in the pull request first tries a rename and resorts to 'copy' if the rename should fail.

If this code is applied, It is advisable (not required) to store temp files on the same partition as uploaded files to improve performance.

@RomanBurunkov
Copy link
Collaborator

One thing I'm not sure that rename throws an error in case of different partitions.
As far as I remember it doesn't do that and that is the reason why code had been changed to use copy instead of rename.

Do you now something about that?

@anneb
Copy link
Contributor Author

anneb commented Sep 10, 2019

According to documentation, fs.rename throws error code 'EXDEV' if it cannot rename across devices.

In this commit: a4f13ff, I see that fs.rename gets replaced by copyFile to solve the 'mv across filesystems' problem. The problem of not throwing an error is not mentioned.

I just tested with both Ubuntu and Windows:

app.use(fileUpload({
    useTempFiles: true,
    tempFileDir: `/media/path_to_externaldisk/temp`
  }));

app.use(fileUpload({
    useTempFiles: true,
    tempFileDir: `d:/temp`
  }));

In both cases, fs.rename throws an error with code 'EXDEV' and the code from the pull request then calls the original copyFile code:

const moveFile = (src, dst, callback) => {
  fs.rename(src, dst, err => {
    if (err) {
        // Copy file to dst and delete src whether success.
        copyFile(src, dst, err => err ? callback(err) : deleteFile(src, callback));      
        return;
      }
      callback();
    });
};

@RomanBurunkov
Copy link
Collaborator

Hi @anneb ,

Thanks for checking that!
I'm going to merge that fix tomorrow morning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants