Skip to content

Commit 3b3a163

Browse files
tanmaykmJeffBezanson
authored andcommitted
avoid file ownership requirement in touch (#28819)
Using `futimes` and passing NULL for the `times` parameter sets the current and access file times, but does not require file ownwership. From man page: > If times is NULL, the access and modification times are set to the current time. The caller must be the owner of the file, have permission to write the file, or be the superuser.
1 parent e1f42b1 commit 3b3a163

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

base/file.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,13 @@ We can see the [`mtime`](@ref) has been modified by `touch`.
403403
function touch(path::AbstractString)
404404
f = open(path, JL_O_WRONLY | JL_O_CREAT, 0o0666)
405405
try
406-
t = time()
407-
futime(f,t,t)
406+
if Sys.isunix()
407+
ret = ccall(:futimes, Cint, (Cint, Ptr{Cvoid}), fd(f), C_NULL)
408+
systemerror(:futimes, ret != 0, extrainfo=path)
409+
else
410+
t = time()
411+
futime(f,t,t)
412+
end
408413
finally
409414
close(f)
410415
end

0 commit comments

Comments
 (0)