-
-
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
Make file operations return the path they created #27071
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5bc2392
=make creation functions return the path the thing they created
oxinabox 2005702
=update news.md
oxinabox a6c7853
=punctuation
oxinabox 9464873
Don't change chown or chmod
oxinabox c5a5510
=also chown and chmod
oxinabox 4709cf5
=imperative
oxinabox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,7 @@ modified by the current file creation mask. This function never creates more tha | |
directory. If the directory already exists, or some intermediate directories do not exist, | ||
this function throws an error. See [`mkpath`](@ref) for a function which creates all | ||
required intermediate directories. | ||
Returns `path` | ||
""" | ||
function mkdir(path::AbstractString; mode::Integer = 0o777) | ||
@static if Sys.iswindows() | ||
|
@@ -106,13 +107,15 @@ function mkdir(path::AbstractString; mode::Integer = 0o777) | |
ret = ccall(:mkdir, Int32, (Cstring, UInt32), path, checkmode(mode)) | ||
end | ||
systemerror(:mkdir, ret != 0; extrainfo=path) | ||
path | ||
end | ||
|
||
""" | ||
mkpath(path::AbstractString; mode::Unsigned = 0o777) | ||
|
||
Create all directories in the given `path`, with permissions `mode`. `mode` defaults to | ||
`0o777`, modified by the current file creation mask. | ||
Returns `path`. | ||
""" | ||
function mkpath(path::AbstractString; mode::Integer = 0o777) | ||
isdirpath(path) && (path = dirname(path)) | ||
|
@@ -124,12 +127,11 @@ function mkpath(path::AbstractString; mode::Integer = 0o777) | |
# If there is a problem with making the directory, but the directory | ||
# does in fact exist, then ignore the error. Else re-throw it. | ||
catch err | ||
if isa(err, SystemError) && isdir(path) | ||
return | ||
else | ||
if !isa(err, SystemError) || !isdir(path) | ||
rethrow() | ||
end | ||
end | ||
path | ||
end | ||
|
||
""" | ||
|
@@ -228,6 +230,7 @@ Copy the file, link, or directory from `src` to `dest`. | |
If `follow_symlinks=false`, and `src` is a symbolic link, `dst` will be created as a | ||
symbolic link. If `follow_symlinks=true` and `src` is a symbolic link, `dst` will be a copy | ||
of the file or directory `src` refers to. | ||
Returns `dst`. | ||
""" | ||
function cp(src::AbstractString, dst::AbstractString; force::Bool=false, | ||
follow_symlinks::Bool=false, | ||
|
@@ -246,13 +249,15 @@ function cp(src::AbstractString, dst::AbstractString; force::Bool=false, | |
else | ||
sendfile(src, dst) | ||
end | ||
dst | ||
end | ||
|
||
""" | ||
mv(src::AbstractString, dst::AbstractString; force::Bool=false) | ||
|
||
Move the file, link, or directory from `src` to `dst`. | ||
`force=true` will first remove an existing `dst`. | ||
Returns `dst`. | ||
""" | ||
function mv(src::AbstractString, dst::AbstractString; force::Bool=false, | ||
remove_destination::Union{Bool,Nothing}=nothing) | ||
|
@@ -264,12 +269,14 @@ function mv(src::AbstractString, dst::AbstractString; force::Bool=false, | |
end | ||
checkfor_mv_cp_cptree(src, dst, "moving"; force=force) | ||
rename(src, dst) | ||
dst | ||
end | ||
|
||
""" | ||
touch(path::AbstractString) | ||
|
||
Update the last-modified timestamp on a file to the current time. | ||
Returns `path`. | ||
""" | ||
function touch(path::AbstractString) | ||
f = open(path, JL_O_WRONLY | JL_O_CREAT, 0o0666) | ||
|
@@ -279,6 +286,7 @@ function touch(path::AbstractString) | |
finally | ||
close(f) | ||
end | ||
path | ||
end | ||
|
||
if Sys.iswindows() | ||
|
@@ -650,6 +658,7 @@ end | |
Change the permissions mode of `path` to `mode`. Only integer `mode`s (e.g. `0o777`) are | ||
currently supported. If `recursive=true` and the path is a directory all permissions in | ||
that directory will be recursively changed. | ||
Returns `path`. | ||
""" | ||
function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) | ||
err = ccall(:jl_fs_chmod, Int32, (Cstring, Cint), path, mode) | ||
|
@@ -661,17 +670,18 @@ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) | |
end | ||
end | ||
end | ||
nothing | ||
path | ||
end | ||
|
||
""" | ||
chown(path::AbstractString, owner::Integer, group::Integer=-1) | ||
|
||
Change the owner and/or group of `path` to `owner` and/or `group`. If the value entered for `owner` or `group` | ||
is `-1` the corresponding ID will not change. Only integer `owner`s and `group`s are currently supported. | ||
Returns `path` | ||
""" | ||
function chown(path::AbstractString, owner::Integer, group::Integer=-1) | ||
err = ccall(:jl_fs_chown, Int32, (Cstring, Cint, Cint), path, owner, group) | ||
uv_error("chown",err) | ||
nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
path | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Returns -> Return I think.
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 think Returns, as in "This function returns
path
"Overly complicated explanation for why:
It is a sentence incorporating ellipsis. which is short for "This function returns
path
"Returns is the verb in 3rd person present tense (I had to look that up).
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.
We usually use imperative form though, from point 2 in the manual (https://docs.julialang.org/en/latest/manual/documentation/):
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.
Fair enough, amazing and excellent that the manual is that specific
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.
However, from searching a bit in the doc strings, we are very inconsistent regarding this.
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.
The imperative mode guideline was introduced in #15136, among several other guidelines, by @nalimilan. However, I can't seem to find discussion of that particular aspect in the PR. Was it discussed elsewhere, e.g. on discourse or slack?
(FWIW, I do agree with @KristofferC that indicative mode sounds more natural, as neither the author nor the reader of the documentation will be the actor performing the returning action... but I may be missing something.)
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 don't remember where that rule comes from, but I don't think I invented it. It's a know problem that our docs are inconsistent, and I don't really care about which convention is chosen as long as one exists.
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 do know of that convention/recommendation for git commits, but not for function documentation. I do think it makes more sense in the former context than the latter, but don't meant to discuss this here. Should I open an issue for that, or is everyone else ok with the current convention?
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.
Note that the cited rule applies to the one-line sentence in the following sense:
So no need to discuss the rule here, because it doesn't apply.