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

libuv_readlink_function #10714

Merged
merged 1 commit into from
Apr 17, 2015
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
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