From 8732f6780ada92cd24616932dd2b8c730f4c36b5 Mon Sep 17 00:00:00 2001 From: Sam Morrison Date: Tue, 9 Feb 2016 10:27:05 -0600 Subject: [PATCH] adding chown to base filesystem functions --- base/docs/helpdb/Base.jl | 8 ++++++++ base/exports.jl | 1 + base/file.jl | 7 +++++++ doc/stdlib/file.rst | 6 ++++++ src/jl_uv.c | 8 ++++++++ test/file.jl | 16 ++++++++++++++++ 6 files changed, 46 insertions(+) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 389ea45787406..402d4695692fa 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -5427,6 +5427,14 @@ Change the permissions mode of `path` to `mode`. Only integer `mode`s (e.g. 0o77 """ chmod +""" + chown(path, owner, group=-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. +""" +chown + """ gamma(x) diff --git a/base/exports.jl b/base/exports.jl index fc03a1ccf7b47..be742ead2b830 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1273,6 +1273,7 @@ export # filesystem operations cd, chmod, + chown, cp, ctime, download, diff --git a/base/file.jl b/base/file.jl index 8ca14f1807620..c89b28bebe278 100644 --- a/base/file.jl +++ b/base/file.jl @@ -5,6 +5,7 @@ export cd, chmod, + chown, cp, cptree, mkdir, @@ -441,3 +442,9 @@ function chmod(p::AbstractString, mode::Integer) uv_error("chmod",err) nothing end + +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 +end diff --git a/doc/stdlib/file.rst b/doc/stdlib/file.rst index 51501c5867d26..4f18267fa3a65 100644 --- a/doc/stdlib/file.rst +++ b/doc/stdlib/file.rst @@ -81,6 +81,12 @@ Change the permissions mode of ``path`` to ``mode``\ . Only integer ``mode``\ s (e.g. 0o777) are currently supported. +.. function:: chown(path, owner, group=-1) + + .. Docstring generated from Julia source + + 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. + .. function:: stat(file) .. Docstring generated from Julia source diff --git a/src/jl_uv.c b/src/jl_uv.c index bcbc8f650b881..ad99bd1fec3de 100644 --- a/src/jl_uv.c +++ b/src/jl_uv.c @@ -306,6 +306,14 @@ JL_DLLEXPORT int jl_fs_chmod(char *path, int mode) return ret; } +JL_DLLEXPORT int jl_fs_chown(char *path, int uid, int gid) +{ + uv_fs_t req; + int ret = uv_fs_chown(jl_io_loop, &req, path, uid, gid, NULL); + uv_fs_req_cleanup(&req); + return ret; +} + JL_DLLEXPORT int jl_fs_write(int handle, const char *data, size_t len, int64_t offset) { diff --git a/test/file.jl b/test/file.jl index a28e0e888b46e..da09e96fa727c 100644 --- a/test/file.jl +++ b/test/file.jl @@ -119,6 +119,22 @@ rm(c_tmpdir, recursive=true) @test_throws Base.UVError rm(c_tmpdir, recursive=true) @test rm(c_tmpdir, force=true, recursive=true) === nothing +# chown will give an error if the user does not have permissions to change files +@unix_only if ENV["USER"] =="root" + chown(file, -2, -1) # Change the file owner to nobody + @test stat(file).uid !=0 + 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 stat(file).gid ==0 + @test stat(file).uid ==0 +else + @test_throws Base.UVError chown(file, -2, -1) # Non-root user cannot change ownership to another user + @test_throws Base.UVError chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup) +end + +@windows_only @test chown(file, -2, -2) == nothing # chown shouldn't cause any errors for Windows ####################################################################### # This section tests file watchers. #