From 5bc239283277ee96bd72a2f8d94394ab206d4058 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Fri, 11 May 2018 15:14:46 +0800 Subject: [PATCH 1/6] =make creation functions return the path the thing they created --- base/file.jl | 20 +++++++++++++++----- test/file.jl | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/base/file.jl b/base/file.jl index 0a76ca64fe2fc..3d3dbc0a2ae76 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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,6 +107,7 @@ 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 """ @@ -113,6 +115,7 @@ end 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,6 +249,7 @@ function cp(src::AbstractString, dst::AbstractString; force::Bool=false, else sendfile(src, dst) end + dst end """ @@ -253,6 +257,7 @@ end 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,7 +670,7 @@ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) end end end - nothing + path end """ @@ -669,9 +678,10 @@ end 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 + path end diff --git a/test/file.jl b/test/file.jl index 33c4ccd172cfd..1f0829f7a634f 100644 --- a/test/file.jl +++ b/test/file.jl @@ -978,6 +978,26 @@ rm(dir) @test !ispath(file) @test !ispath(dir) + + +################## +# Return values of mkpath, mkdir, cp, mv and touch +#################### +mktempdir() do dir + name1 = joinpath(dir, "apples") + name2 = joinpath(dir, "bannanas") + @test touch(name1)==name1 + @test mv(name1, name2) == name2 + @test cp(name2, name1) == name1 + namedir = joinpath(dir, "chalk") + namepath = joinpath(dir, "chalk","cheese","fresh") + @test mkdir(namedir) == namedir + @test mkpath(namepath) == namepath +end + + + + # issue #9687 let n = tempname() w = open(n, "a") From 2005702b7916fc5376a19def78fd9ec5ae4dc0d2 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Fri, 11 May 2018 15:34:54 +0800 Subject: [PATCH 2/6] =update news.md --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index d5e9da05761a8..866b7e584b09c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -469,6 +469,9 @@ This section lists changes that do not have deprecation warnings. * `widen` on 8- and 16-bit integer types now widens to the platform word size (`Int`) instead of to a 32-bit type ([#26859]). + * `mv`,`cp`, `touch`, `mkdir`, `mkpath` now return the path that was created/modified + rather than `nothing` ([#27071]). + Library improvements -------------------- From a6c7853bc231da96188f34cbfbc52377590c1b7b Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Fri, 11 May 2018 15:36:37 +0800 Subject: [PATCH 3/6] =punctuation --- base/file.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/file.jl b/base/file.jl index 3d3dbc0a2ae76..a2dc9ec6bda83 100644 --- a/base/file.jl +++ b/base/file.jl @@ -230,7 +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` +Returns `dst`. """ function cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false, @@ -276,7 +276,7 @@ end touch(path::AbstractString) Update the last-modified timestamp on a file to the current time. -Returns `path` +Returns `path`. """ function touch(path::AbstractString) f = open(path, JL_O_WRONLY | JL_O_CREAT, 0o0666) From 946487324ca0f527d242f4ec38cc8f452d791b52 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Fri, 11 May 2018 19:12:26 +0800 Subject: [PATCH 4/6] Don't change chown or chmod --- base/file.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/base/file.jl b/base/file.jl index a2dc9ec6bda83..f77552b155f58 100644 --- a/base/file.jl +++ b/base/file.jl @@ -658,7 +658,6 @@ 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) @@ -670,7 +669,6 @@ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) end end end - path end """ @@ -678,10 +676,8 @@ end 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) - path end From c5a55107b94f632f8e5c9844c4b1e0ee9ab96551 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 14 May 2018 17:03:06 +0800 Subject: [PATCH 5/6] =also chown and chmod --- base/file.jl | 4 ++++ test/file.jl | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/base/file.jl b/base/file.jl index f77552b155f58..52c576ab86f1b 100644 --- a/base/file.jl +++ b/base/file.jl @@ -658,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) @@ -669,6 +670,7 @@ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) end end end + path end """ @@ -676,8 +678,10 @@ end 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) + path end diff --git a/test/file.jl b/test/file.jl index 1f0829f7a634f..2996bd6650db4 100644 --- a/test/file.jl +++ b/test/file.jl @@ -62,7 +62,7 @@ end @test filemode(file) & 0o444 > 0 # readable @test filemode(file) & 0o222 > 0 # writable -chmod(file, filemode(file) & 0o7555) +@test chmod(file, filemode(file) & 0o7555) == file @test filemode(file) & 0o222 == 0 chmod(file, filemode(file) | 0o222) @test filemode(file) & 0o111 == 0 @@ -171,7 +171,7 @@ if !Sys.iswindows() chown(file, 0, -2) # Change the file group to nogroup (and owner back to root) @test stat(file).gid !=0 @test stat(file).uid ==0 - chown(file, -1, 0) + @test chown(file, -1, 0) == file @test stat(file).gid ==0 @test stat(file).uid ==0 else @@ -180,7 +180,7 @@ if !Sys.iswindows() end else # test that chown doesn't cause any errors for Windows - @test chown(file, -2, -2) === nothing + @test chown(file, -2, -2) == file end ############## From 4709cf5fd88cf05fe32f8aaa0f5d531466c23892 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 14 May 2018 17:25:21 +0800 Subject: [PATCH 6/6] =imperative --- base/file.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/base/file.jl b/base/file.jl index 52c576ab86f1b..038550abde7f6 100644 --- a/base/file.jl +++ b/base/file.jl @@ -98,7 +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` +Return `path`. """ function mkdir(path::AbstractString; mode::Integer = 0o777) @static if Sys.iswindows() @@ -115,7 +115,7 @@ end Create all directories in the given `path`, with permissions `mode`. `mode` defaults to `0o777`, modified by the current file creation mask. -Returns `path`. +Return `path`. """ function mkpath(path::AbstractString; mode::Integer = 0o777) isdirpath(path) && (path = dirname(path)) @@ -230,7 +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`. +Return `dst`. """ function cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false, @@ -257,7 +257,7 @@ end Move the file, link, or directory from `src` to `dst`. `force=true` will first remove an existing `dst`. -Returns `dst`. +Return `dst`. """ function mv(src::AbstractString, dst::AbstractString; force::Bool=false, remove_destination::Union{Bool,Nothing}=nothing) @@ -276,7 +276,7 @@ end touch(path::AbstractString) Update the last-modified timestamp on a file to the current time. -Returns `path`. +Return `path`. """ function touch(path::AbstractString) f = open(path, JL_O_WRONLY | JL_O_CREAT, 0o0666) @@ -658,7 +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`. +Return `path`. """ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false) err = ccall(:jl_fs_chmod, Int32, (Cstring, Cint), path, mode) @@ -678,7 +678,7 @@ end 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` +Return `path` """ function chown(path::AbstractString, owner::Integer, group::Integer=-1) err = ccall(:jl_fs_chown, Int32, (Cstring, Cint, Cint), path, owner, group)