Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: added chown #15007

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,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 @@ -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
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.

.. 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 @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be after the end for the test to actually run on windows? would be preferable to use something like if @unix? true : false for blocks rather than extended ternary across lines.

#######################################################################
# This section tests file watchers. #
Expand Down