Skip to content

Commit

Permalink
libuv_readlink_function
Browse files Browse the repository at this point in the history
Adds wrapper function in C in jl_uv.c as well as the necessary
documentations and tests.
  • Loading branch information
peter1000 committed Apr 17, 2015
1 parent a6782a5 commit b3c9ce1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ export
mv,
operm,
pwd,
readlink,
rm,
stat,
symlink,
Expand Down
13 changes: 13 additions & 0 deletions base/fs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export File,
rename,
sendfile,
symlink,
readlink,
chmod,
futime,
JL_O_WRONLY,
Expand Down Expand Up @@ -163,6 +164,18 @@ end
@windowsxp_only symlink(p::AbstractString, np::AbstractString) =
error("WindowsXP does not support soft symlinks")

function readlink(path::AbstractString)
req = Libc.malloc(_sizeof_uv_fs)
ret = ccall(:uv_fs_readlink, Int32,
(Ptr{Void}, Ptr{Void}, Ptr{UInt8}, Ptr{Void}),
eventloop(), req, path, C_NULL)
uv_error("readlink", ret)
tgt = bytestring(ccall(:jl_uv_fs_t_ptr, Ptr{Cchar}, (Ptr{Void}, ), req))
ccall(:uv_fs_req_cleanup, Void, (Ptr{Void}, ), req)
Libc.free(req)
tgt
end

function chmod(p::AbstractString, mode::Integer)
err = ccall(:jl_fs_chmod, Int32, (Ptr{UInt8}, Cint), p, mode)
uv_error("chmod",err)
Expand Down
6 changes: 6 additions & 0 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5021,6 +5021,12 @@ Millisecond(v)
"),

("Base","readlink","readlink(path) -> AbstractString
Returns the value of a symbolic link \"path\".
"),

("Base","chmod","chmod(path, mode)
Change the permissions mode of \"path\" to \"mode\". Only integer
Expand Down
4 changes: 4 additions & 0 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
This function raises an error under operating systems that do not support
soft symbolic links, such as Windows XP.

.. function:: readlink(path) -> AbstractString

Returns the value of a symbolic link ``path``.

.. function:: chmod(path, mode)

Change the permissions mode of ``path`` to ``mode``. Only integer ``mode``\ s
Expand Down
43 changes: 34 additions & 9 deletions test/file.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
#############################################
# Create some temporary files & directories #
#############################################

starttime = time()
pwd_ = pwd()
dir = mktempdir()
file = joinpath(dir, "afile.txt")
close(open(file,"w")) # like touch, but lets the operating system update the timestamp for greater precision on some platforms (windows)

@unix_only begin
link = joinpath(dir, "afilelink.txt")
symlink(file, link)
end

subdir = joinpath(dir, "adir")
mkdir(subdir)
subdir2 = joinpath(dir, "adir2")
mkdir(subdir2)

@non_windowsxp_only begin
dirlink = joinpath(dir, "dirlink")
symlink(subdir, dirlink)
# relative link
cd(subdir)
relsubdirlink = joinpath(subdir, "rel_subdirlink")
reldir = joinpath("..", "adir2")
symlink(reldir, relsubdirlink)
cd(pwd_)
end

@unix_only begin
link = joinpath(dir, "afilelink.txt")
symlink(file, link)
# relative link
cd(subdir)
rellink = joinpath(subdir, "rel_afilelink.txt")
relfile = joinpath("..", "afile.txt")
symlink(relfile, rellink)
cd(pwd_)
end


#######################################################################
# This section tests some of the features of the stat-based file info #
#######################################################################
Expand Down Expand Up @@ -50,9 +66,12 @@ end

# test links
@unix_only @test islink(link) == true
@unix_only @test readlink(link) == file

@non_windowsxp_only begin
@test islink(dirlink) == true
@test isdir(dirlink) == true
@test readlink(dirlink) == subdir * @windows? "\\" : ""
end

# rename file
Expand Down Expand Up @@ -412,11 +431,17 @@ test_LibcFILE(Libc.FILE(f,Libc.modestr(true,false)))
############
# Clean up #
############
@unix_only rm(link)
@non_windowsxp_only rm(dirlink)

@unix_only begin
rm(link)
rm(rellink)
end
@non_windowsxp_only begin
rm(dirlink)
rm(relsubdirlink)
end
rm(file)
rm(subdir)
rm(subdir2)
rm(dir)

# The following fail on Windows with "stat: operation not permitted (EPERM)"
Expand Down

0 comments on commit b3c9ce1

Please sign in to comment.