Skip to content

Commit f226f81

Browse files
nhz2KristofferC
authored andcommitted
Make rename public (#55652)
Fixes #41584. Follow up of #55503 I think `rename` is a very useful low-level file system operation. Many other programming languages have this function, so it is useful when porting IO code to Julia. One use case is to improve the Zarr.jl package to be more compatible with zarr-python. https://github.com/zarr-developers/zarr-python/blob/0b5483a7958e2ae5512a14eb424a84b2a75dd727/src/zarr/v2/storage.py#L994 uses the `os.replace` function. It would be nice to be able to directly use `Base.rename` as a replacement for `os.replace` to ensure compatibility. Another use case is writing a safe zip file extractor in pure Julia. https://github.com/madler/sunzip/blob/34107fa9e2a2e36e7e72725dc4c58c9ad6179898/sunzip.c#L365 uses the `rename` function to do this in C. Lastly in https://github.com/medyan-dev/MEDYANSimRunner.jl/blob/67d5b42cc599670486d5d640260a95e951091f7a/src/file-saving.jl#L83 I am using `ccall(:jl_fs_rename` to save files, because I have large numbers of Julia processes creating and reading these files at the same time on a distributed file system on a cluster, so I don't want data to become corrupted if one of the nodes crashes (which happens fairly regularly). However `jl_fs_rename` is not public, and might break in a future release. This PR also adds a note to `mv` comparing it to the `mv` command, similar to the note on the `cp` function.
1 parent c3b281b commit f226f81

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

base/file.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ of the file or directory `src` refers to.
385385
Return `dst`.
386386
387387
!!! note
388-
The `cp` function is different from the `cp` command. The `cp` function always operates on
388+
The `cp` function is different from the `cp` Unix command. The `cp` function always operates on
389389
the assumption that `dst` is a file, while the command does different things depending
390390
on whether `dst` is a directory or a file.
391391
Using `force=true` when `dst` is a directory will result in loss of all the contents present
@@ -438,6 +438,16 @@ julia> mv("hello.txt", "goodbye.txt", force=true)
438438
julia> rm("goodbye.txt");
439439
440440
```
441+
442+
!!! note
443+
The `mv` function is different from the `mv` Unix command. The `mv` function by
444+
default will error if `dst` exists, while the command will delete
445+
an existing `dst` file by default.
446+
Also the `mv` function always operates on
447+
the assumption that `dst` is a file, while the command does different things depending
448+
on whether `dst` is a directory or a file.
449+
Using `force=true` when `dst` is a directory will result in loss of all the contents present
450+
in the `dst` directory, and `dst` will become a file that has the contents of `src` instead.
441451
"""
442452
function mv(src::AbstractString, dst::AbstractString; force::Bool=false)
443453
if force
@@ -1192,6 +1202,8 @@ If a path contains a "\\0" throw an `ArgumentError`.
11921202
On other failures throw an `IOError`.
11931203
Return `newpath`.
11941204
1205+
This is a lower level filesystem operation used to implement [`mv`](@ref).
1206+
11951207
OS-specific restrictions may apply when `oldpath` and `newpath` are in different directories.
11961208
11971209
Currently there are a few differences in behavior on Windows which may be resolved in a future release.
@@ -1202,6 +1214,9 @@ Specifically, currently on Windows:
12021214
4. `rename` may remove `oldpath` if it is a hardlink to `newpath`.
12031215
12041216
See also: [`mv`](@ref).
1217+
1218+
!!! compat "Julia 1.12"
1219+
This method was made public in Julia 1.12.
12051220
"""
12061221
function rename(oldpath::AbstractString, newpath::AbstractString)
12071222
err = ccall(:jl_fs_rename, Int32, (Cstring, Cstring), oldpath, newpath)

base/public.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public
110110
reseteof,
111111
link_pipe!,
112112

113+
# filesystem operations
114+
rename,
115+
113116
# misc
114117
notnothing,
115118
runtests,

doc/src/base/file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Base.Filesystem.operm
2929
Base.Filesystem.cp
3030
Base.download
3131
Base.Filesystem.mv
32+
Base.Filesystem.rename
3233
Base.Filesystem.rm
3334
Base.Filesystem.touch
3435
Base.Filesystem.tempname

0 commit comments

Comments
 (0)