-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
fs: add fs.copy, fs.copySync, fs.delete and fs.deleteSync with docs and tests. #12583
Conversation
…nd tests. So I and many other often need to recursively copy or delete directory's with node and copy file that keep their permissions and properties etc. Many use an external library or write their own and that takes up a lot of time and adds deps to your project which you might not want. I have added fs.copy and fs.copySync to recursively copy directory's or just copy files, all files/directory's copied with fs.copy and fs.copySync retain they owners, mode and times. fs.delete and fs.deleteSync either delete a file (basically an alias for fs.unlink) or of a directory is passed into the path recursively remove the content of the directory and remove the directory itself. There are however a few notes, this is writen in JavaScript only and thus calls other functions in the fs module, I have placed the code at the bottom of lib/fs.js however there may be a better place to put the code. I also change open the directory's for copying in different modes dependent on the platform, I use 'process.platform==="win32"?"r+":"r"' to determine the mode to open the directory in, I have done this because I found during testing that on Windows, you need to have the directory open in r+ mode to modify it's properties whereas on Linux it throws when you attempt to open a directory in r+ mode & you can open it in r mode and still change the properties. I don't have a Mac OS computer & thus cannot test it on Mac so this mode could be wrong on Mac. I also use read streams and write streams in fs.copy however I use fs.readFileSync to read the file in fs.copySync, I know that this could load the entire file into the main memory and thus not be a great way of doing it however I am not sure how to do this better. So advise on this would be helpful. I have also used readable.on('end') to determine when the copying of the file contents has ended, however I have seen the finish event used in some other cases, the end event seems to work to I haven't changed it but again that is something that might need changing. My tests are relatively simplistic however they do test the copying. I haven't got a test for the overwrite mode being a function however I can write that if it is needed. Hope this is OK, Jacob
spelling from us to is and included the default value for overwrite_mode.
Perhaps you can pass ``make -j4 test```. The linter is giving 481 problems. Also, try to follow the style that the code already have. Use spacing 2 instead of tabs. |
Is it actually desirable to include this in the node.js core, considering that it does not contain native bindings and the overhead of npm dependencies is relatively small?
According to this logic, we should integrate all of the "Most depended-upon packages" from npm into node itself. |
Not a bad idea 😉. |
@JOT85 this is a valid point. Do you think this PR can be improved by using native bindings? |
Style issues aside, I don't think this needs to be in core. Please just use something like |
-1 on this. I agree this is better to have outside of core because it's not terribly difficult to do and the implementation of such functions is/could be quite opinionated. |
This isn't really a great idea though -- it puts an unreasonable burden on core to maintain more and more. These are not difficult to do outside of core, so they should belong outside of core. It could be conceivable that we may want them if they pose a large barrier to newcomers. However, we could also point out modules in that case that people could use for that functionality. Generally, we should actually be integrating some things, but typically only those that are particularly painful to do outside of core. :) |
(I see this is you're first PR! thanks for including everything, including documentation; hopefully you'll continue to contribute, even if this doesn't make it in.) |
I am sorry if I was not clear about this part, I oppose this idea, I do not support it. |
Hi, sorry for the late response, I have been busy all day. |
@JOT85 I like this, and I think it makes allot of sense, but try and think if some native binding could make this better, more organic to incorporate. If it's a pure |
I think I speak for most people here; we are understanding of rookie mistakes, and we appreciate your effort. We do not consider this a waste of time! |
It's never a waste of time ^.^ |
OK, so I have sorted out the tabs and replaced them all with 2 spaces. Sorry about that. |
@JOT85 You can lint your files with this commands from repository root folder:
Currently, the logs are:
|
…stead of throw in test-fs-copy-and-delete.js
Thanks for that, I was just using vscode... :( |
CI: https://ci.nodejs.org/job/node-test-pull-request/7609/ Maybe this will be helpful for you even if this PR is not approved and you want to make an npm module. |
From the first failed build:
|
I think the issue was actually with my test code, I never created the directory to run the tests in. |
Still something wrong)
|
…hat they are on aix.
@JOT85 output on AIX: > process.platform
'aix' |
See how that runs on aix, now everything passes except aix, so the tests now log process.platform so I can figure out what it is. |
@gibfahn Thanks! No need for that last commit then... :( |
Cool, fingers crossed this works. |
I'm -1 on this for the reasons mentioned above. Basically having a small core allows us to provide a consistent and stable platform for others to build upon. If it can just as well be implemented outside, then the problems outweigh the benefits. Some links on the subject: cc/ @sam-github |
If something doesn't work it should be in the docs: https://nodejs.org/api/fs.html If not that's a documentation issue and we should fix it. So no, nothing else that I'm aware of. |
@gibfahn thanks. |
Hmm, by the looks of it the aix test that failed wasn't on the latest commit that fixes the issue. |
Also, the windows fail wasn't caused by this PR. |
for any other platform when it should have been the other way round.
All the fails are because I did things the opposite way round with dirs in fs.copySync. Fixed now. And yeah, I only push that now because I entered the wrong password and didn't check... :( |
@vsemozhetbyt or someone else can you please run the tests with the latest commit, I think everything is good now :) |
Out of interest, why don't you want deps added to a project (but are happy with the same deps being added to every Node.js project by being bundled into Node)? |
@gibfahn I have a few reasons for this. Just to make this clear, I am not against deps at all I just don't like having to many of them. |
Couple more things, firstly, the previous windows test was 'unstable' and that wasn't down to test-fs-copy-and-delete, it was down to something else. |
Given that the collaborators thus far have been pretty -1 on this, I think we should close the issue and stop running CI jobs on it. Or at least, we could come to a decision of whether or not we're going to take the PR. I totally get the not wanting dependencies written by other people thing. But it looks like the code is mostly done, so you could just publish your own module. |
Just to throw in my opinion: I think composite operations (such as recursive copies and deletions) pose new challenges. For example, what if you wanted to report the progress of a recursive operation to a user? What if something fails within a recursive operation, how do you handle that error? Can you resume recursive copy operations if they fail? Can you selectively copy directories? Can you determine the progress of a file copying operation? Another thing to consider is that there are existing modules for these operations, some of which even allow to use arbitrary
We are talking about very common features only, I think it is safe to ignore "smaller ones". If an npm module does not have a "large user-base", it should not even be considered for inclusion in node.
So does node, if you don't need the
No module should have side effects apart from those explicitely required. Side effects are usually considered bad and most popular modules refrain from having any.
Licensing is a fair point, but I guess if a problem is popular enough to be included in the node core, there will be an npm module doing the same thing with a compatible license ( If these functions are as common as you say they are, I am sure there are well-tested existing libraries that do the job as good as possible within JavaScript, maybe even with native bindings. |
Well, the code is there, the docs are there, the test scripts are mostly there (however more could be written). |
Although we greatly appreciate the effort @JOT85, I'm also -1 on this. I think this should be published in a package on npm, not included in core. |
I'm also -1. I definitely do appreciate the work, but these are functions that are best left to userland. Given the -1's, I'm closing. |
So I and many other often need to recursively copy or delete
directory's with node and copy file that keep their permissions and
properties etc. Many use an external library or write their own and
that takes up a lot of time and adds deps to your project which you
might not want.
I have added fs.copy and fs.copySync to recursively copy directory's or
just copy files, all files/directory's copied with fs.copy and
fs.copySync retain they owners, mode and times.
fs.delete and fs.deleteSync either delete a file (basically an alias
for fs.unlink) or of a directory is passed into the path recursively
remove the content of the directory and remove the directory itself.
There are however a few notes, this is writen in JavaScript only and
thus calls other functions in the fs module, I have placed the code at
the bottom of lib/fs.js however there may be a better place to put the
code.
I also change open the directory's for copying in different modes
dependent on the platform, I use 'process.platform==="win32"?"r+":"r"'
to determine the mode to open the directory in, I have done this
because I found during testing that on Windows, you need to have the
directory open in r+ mode to modify it's properties whereas on Linux it
throws when you attempt to open a directory in r+ mode & you can open
it in r mode and still change the properties. I don't have a Mac OS
computer & thus cannot test it on Mac so this mode could be wrong on
Mac.
I also use read streams and write streams in fs.copy however I use
fs.readFileSync to read the file in fs.copySync, I know that this could
load the entire file into the main memory and thus not be a great way
of doing it however I am not sure how to do this better. So advise on
this would be helpful.
I have also used readable.on('end') to determine when the copying of
the file contents has ended, however I have seen the finish event used
in some other cases, the end event seems to work to I haven't changed
it but again that is something that might need changing.
My tests are relatively simplistic however they do test the copying. I
haven't got a test for the overwrite mode being a function however I
can write that if it is needed.
Hope this is OK,
Jacob
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows passes, but I cannot test on linux)Affected core subsystem(s)
It changes the fs module, and adds docs.