-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Option for recursive rmdir #7055
Conversation
if recursive | ||
for p=readdir(path) | ||
p = joinpath(path, p) | ||
isfile(p) && rm(p) || rmdir(p, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how this will work, because rm(p)
doesn't return a boolean, but its value is needed in order for Julia whether to evaluate the rmdir
in the ||
. (On my machine with Julia 0.3, it gives ERROR: type: non-boolean (Nothing) used in boolean context
.)
Better to just use a plain if ... else ... end
expression here or a isfile(p) ? rm(p) : rmdir(p,true)
if you want a one-liner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also doesn't work for symbolic links, since isfile(p)
currently returns false
for a symbolic link to a directory, and yet rmdir(p)
doesn't work for symbolic links to directories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! you're right of course! I will break into a for loop.
If I use isdir here instead would this work with sym links (are they are counted as files - so we just want to rm them) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you want to just rm
links. But isdir
won't work: it returns true
for a symlink to a directory.
I think you need isfile(p) || islink(p) ? rm(p) : rmdir(p, true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, to avoid a second call to stat
, it might be useful to add something like
isfileorlink(st::StatStruct) = st.mode & 0xd000 == 0x8000
isfileorlink(path...) = isfileorlink(lstat(path...))
to stat.jl
Also, you should add a test case to |
@stevengj I've added isfileorlink to stat like you suggest and a test of This now works for me with links/files/dirs, but ATM there aren't any tests which use linked files. I could add that some tests for islink (but don't know how to do this without shelling out: |
@@ -150,7 +150,7 @@ function clone(url::String, pkg::String) | |||
Git.run(`clone -q $url $pkg`) | |||
Git.set_remote_url(url, dir=pkg) | |||
catch | |||
run(`rm -rf $pkg`) | |||
rmdir(pkg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing true here
Note that you also need to update the docs for |
Bump. Looks pretty good. |
Two API issues:
|
Yeah, maybe the right test is |
About 2., I'd rather add a |
There's a combinatorial explosion of things you can test a file stat for. We provide convenience accessors for the basic ones, but this is starting down the path of providing all 2^n of them. When performance is an issue, you can use the |
@nalimilan, you may be right that it's best not to recursively delete directories without some insistence. |
Happy to remove the IMO the API is quite nice atm that |
I was suggesting removing |
And yet that's the situation of all Linux (POSIX, even?) systems I know of. :-) I'm OK with keeping only |
I keep not being able to find this because it doesn't link to #7041 anywhere. Now it does. |
I too suggest an |
How about having |
Works for me. |
There is now symlink functionality in base (#4396), so the tests here should probably be updated to account for that. Will want to test that this doesn't recurse into the targets of symlinks or NTFS junctions on Windows, either. |
Yeah? You decided isfileorlink belongs in Base? |
He merged then fixed it a2d4dc9 |
Ah, cool. |
At the moment to
rm -rf
you need to shell out, with this:see also #7046.