11# This file is a part of Julia. License is MIT: http://julialang.org/license
22
3+ # Operations with the file system (paths) ##
4+
5+ export
6+ cd,
7+ chmod,
8+ cp,
9+ cptree,
10+ mkdir,
11+ mkpath,
12+ mktemp,
13+ mktempdir,
14+ mv,
15+ pwd,
16+ rename,
17+ readlink,
18+ readdir,
19+ rm,
20+ samefile,
21+ sendfile,
22+ symlink,
23+ tempdir,
24+ tempname,
25+ touch,
26+ walkdir
27+
328# get and set current directory
429
530function pwd ()
@@ -65,8 +90,8 @@ mkpath(path::AbstractString, mode::Signed) = throw(ArgumentError("mode must be a
6590
6691function rm (path:: AbstractString ; recursive:: Bool = false )
6792 if islink (path) || ! isdir (path)
68- @windows_only if ! iswritable ( path); chmod (path, 0o777 ); end
69- FS . unlink (path)
93+ @windows_only if ( filemode ( path) & 0o222 ) == 0 ; chmod (path, 0o777 ); end # is writable on windows actually means "is deletable"
94+ unlink (path)
7095 else
7196 if recursive
7297 for p in readdir (path)
@@ -116,7 +141,7 @@ function cptree(src::AbstractString, dst::AbstractString; remove_destination::Bo
116141 cptree (srcname, joinpath (dst, name); remove_destination= remove_destination,
117142 follow_symlinks= follow_symlinks)
118143 else
119- FS . sendfile (srcname, joinpath (dst, name))
144+ sendfile (srcname, joinpath (dst, name))
120145 end
121146 end
122147end
@@ -129,23 +154,22 @@ function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=f
129154 elseif isdir (src)
130155 cptree (src, dst; remove_destination= remove_destination, follow_symlinks= follow_symlinks)
131156 else
132- FS . sendfile (src, dst)
157+ sendfile (src, dst)
133158 end
134159end
135160
136161function mv (src:: AbstractString , dst:: AbstractString ; remove_destination:: Bool = false )
137162 checkfor_mv_cp_cptree (src, dst, " moving" ; remove_destination= remove_destination)
138- FS . rename (src, dst)
163+ rename (src, dst)
139164end
140165
141166function touch (path:: AbstractString )
142- f = FS. open (path,JL_O_WRONLY | JL_O_CREAT, 0o0666 )
143- @assert f. handle >= 0
167+ f = open (path, JL_O_WRONLY | JL_O_CREAT, 0o0666 )
144168 try
145169 t = time ()
146170 futime (f,t,t)
147171 finally
148- FS . close (f)
172+ close (f)
149173 end
150174end
151175
@@ -203,7 +227,7 @@ function tempname(temppath::AbstractString,uunique::UInt32)
203227end
204228function mktemp (parent= tempdir ())
205229 filename = tempname (parent, UInt32 (0 ))
206- return (filename, open (filename," r+" ))
230+ return (filename, Base . open (filename, " r+" ))
207231end
208232function mktempdir (parent= tempdir ())
209233 seed:: UInt32 = rand (UInt32)
@@ -330,3 +354,83 @@ function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw)
330354 Task (_it)
331355end
332356
357+ function unlink (p:: AbstractString )
358+ err = ccall (:jl_fs_unlink , Int32, (Cstring,), p)
359+ uv_error (" unlink" , err)
360+ nothing
361+ end
362+
363+ # For move command
364+ function rename (src:: AbstractString , dst:: AbstractString )
365+ err = ccall (:jl_fs_rename , Int32, (Cstring, Cstring), src, dst)
366+ # on error, default to cp && rm
367+ if err < 0
368+ # remove_destination: is already done in the mv function
369+ cp (src, dst; remove_destination= false , follow_symlinks= false )
370+ rm (src; recursive= true )
371+ end
372+ nothing
373+ end
374+
375+ function sendfile (src:: AbstractString , dst:: AbstractString )
376+ local src_open = false ,
377+ dst_open = false ,
378+ src_file,
379+ dst_file
380+ try
381+ src_file = open (src, JL_O_RDONLY)
382+ src_open = true
383+ dst_file = open (dst, JL_O_CREAT | JL_O_TRUNC | JL_O_WRONLY,
384+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP| S_IROTH | S_IWOTH)
385+ dst_open = true
386+
387+ bytes = filesize (stat (src_file))
388+ sendfile (dst_file, src_file, Int64 (0 ), Int (bytes))
389+ finally
390+ if src_open && isopen (src_file)
391+ close (src_file)
392+ end
393+ if dst_open && isopen (dst_file)
394+ close (dst_file)
395+ end
396+ end
397+ end
398+
399+ @windows_only const UV_FS_SYMLINK_JUNCTION = 0x0002
400+ function symlink (p:: AbstractString , np:: AbstractString )
401+ @windows_only if Base. windows_version () < Base. WINDOWS_VISTA_VER
402+ error (" Windows XP does not support soft symlinks" )
403+ end
404+ flags = 0
405+ @windows_only if isdir (p); flags |= UV_FS_SYMLINK_JUNCTION; p = abspath (p); end
406+ err = ccall (:jl_fs_symlink , Int32, (Cstring, Cstring, Cint), p, np, flags)
407+ @windows_only if err < 0
408+ Base. warn_once (" Note: on Windows, creating file symlinks requires Administrator privileges." )
409+ end
410+ uv_error (" symlink" ,err)
411+ end
412+
413+ function readlink (path:: AbstractString )
414+ req = Libc. malloc (_sizeof_uv_fs)
415+ try
416+ ret = ccall (:uv_fs_readlink , Int32,
417+ (Ptr{Void}, Ptr{Void}, Cstring, Ptr{Void}),
418+ eventloop (), req, path, C_NULL )
419+ if ret < 0
420+ ccall (:uv_fs_req_cleanup , Void, (Ptr{Void}, ), req)
421+ uv_error (" readlink" , ret)
422+ assert (false )
423+ end
424+ tgt = bytestring (ccall (:jl_uv_fs_t_ptr , Ptr{Cchar}, (Ptr{Void}, ), req))
425+ ccall (:uv_fs_req_cleanup , Void, (Ptr{Void}, ), req)
426+ return tgt
427+ finally
428+ Libc. free (req)
429+ end
430+ end
431+
432+ function chmod (p:: AbstractString , mode:: Integer )
433+ err = ccall (:jl_fs_chmod , Int32, (Cstring, Cint), p, mode)
434+ uv_error (" chmod" ,err)
435+ nothing
436+ end
0 commit comments