Skip to content

Commit

Permalink
Merge pull request #15007 from morris25/add_chown
Browse files Browse the repository at this point in the history
RFC: added chown
  • Loading branch information
tkelman committed Feb 23, 2016
2 parents 451f3e1 + 8732f67 commit 199034c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5358,6 +5358,14 @@ that directory will be recursively changed.
"""
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)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ export
# filesystem operations
cd,
chmod,
chown,
cp,
ctime,
download,
Expand Down
7 changes: 7 additions & 0 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export
cd,
chmod,
chown,
cp,
cptree,
mkdir,
Expand Down Expand Up @@ -450,3 +451,9 @@ function chmod(path::AbstractString, mode::Integer; recursive::Bool=false)
end
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
6 changes: 6 additions & 0 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
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.

.. 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
Expand Down
8 changes: 8 additions & 0 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 16 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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. #
Expand Down

0 comments on commit 199034c

Please sign in to comment.