From 9fac21bf517d5bf68953880e3bfe9da4ef765e87 Mon Sep 17 00:00:00 2001 From: tkelman Date: Sun, 13 Jul 2014 15:16:28 -0700 Subject: [PATCH] Simple implementation of chmod(path, mode) currently only integer modes, can do string modes like +x later fixes #7574 Conflicts: src/jl_uv.c --- base/exports.jl | 1 + base/fs.jl | 6 ++++++ doc/stdlib/base.rst | 5 +++++ src/jl_uv.c | 8 ++++++++ test/file.jl | 5 ++--- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index a3210d2b0b45d..c516e04ff5984 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1235,6 +1235,7 @@ export # filesystem operations cd, + chmod, cp, ctime, download, diff --git a/base/fs.jl b/base/fs.jl index 126566c481b71..88d87eb5c723a 100644 --- a/base/fs.jl +++ b/base/fs.jl @@ -23,6 +23,7 @@ export File, rename, sendfile, symlink, + chmod, JL_O_WRONLY, JL_O_RDONLY, JL_O_RDWR, @@ -160,6 +161,11 @@ end @windowsxp_only symlink(p::String, np::String) = error("WindowsXP does not support soft symlinks") +function chmod(p::String, mode::Integer) + err = ccall(:jl_fs_chmod, Int32, (Ptr{Uint8}, Cint), p, mode) + uv_error("chmod",err) +end + function write(f::File, buf::Ptr{Uint8}, len::Integer, offset::Integer=-1) if !f.open error("file is not open") diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 2f477c2f7c64d..440bfaf2711d7 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -5383,6 +5383,11 @@ System This function raises an error under operating systems that do not support soft symbolic links, such as Windows XP. +.. function:: chmod(path, mode) + + Change the permissions mode of ``path`` to ``mode``. Only integer ``mode``s + (e.g. 0o777) are currently supported. + .. function:: getpid() -> Int32 Get julia's process ID. diff --git a/src/jl_uv.c b/src/jl_uv.c index 9a94d1a9b2562..23e283b316d72 100644 --- a/src/jl_uv.c +++ b/src/jl_uv.c @@ -442,6 +442,14 @@ DLLEXPORT int jl_fs_symlink(char *path, char *new_path, int flags) return ret; } +DLLEXPORT int jl_fs_chmod(char *path, int mode) +{ + uv_fs_t req; + int ret = uv_fs_chmod(jl_io_loop, &req, path, mode, NULL); + uv_fs_req_cleanup(&req); + return ret; +} + DLLEXPORT int jl_fs_write(int handle, char *data, size_t len, int64_t offset) { uv_fs_t req; diff --git a/test/file.jl b/test/file.jl index 28ee91fe72254..c451785eab051 100644 --- a/test/file.jl +++ b/test/file.jl @@ -28,10 +28,9 @@ end @test !islink(file) @test isreadable(file) @test iswritable(file) -# Here's something else that might be UNIX-specific? -run(`chmod -w $file`) +chmod(file, filemode(file) & 0o7555) @test !iswritable(file) -run(`chmod +w $file`) +chmod(file, filemode(file) | 0o222) @test !isexecutable(file) @test filesize(file) == 0 # On windows the filesize of a folder is the accumulation of all the contained