71
71
72
72
73
73
# The following use Unix command line facilites
74
-
75
- function cptree (src:: AbstractString , dst:: AbstractString ; remove_destination:: Bool = false ,
76
- follow_symlinks:: Bool = false )
77
- isdir (src) || throw (ArgumentError (" '$src ' is not a directory. Use `cp(src, dst)`" ))
74
+ function checkfor_mv_cp_cptree (src:: AbstractString , dst:: AbstractString , txt:: AbstractString ;
75
+ remove_destination:: Bool = false )
78
76
if ispath (dst)
79
77
if remove_destination
78
+ # Check for issue when: (src == dst) or when one is a link to the other
79
+ # https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
80
+ if Base. samefile (src, dst)
81
+ abs_src = islink (src) ? abspath (readlink (src)) : abspath (src)
82
+ abs_dst = islink (dst) ? abspath (readlink (dst)) : abspath (dst)
83
+ throw (ArgumentError (string (" 'src' and 'dst' refer to the same file/dir." ,
84
+ " This is not supported.\n " ,
85
+ " `src` refers to: $(abs_src) \n " ,
86
+ " `dst` refers to: $(abs_dst) \n " )))
87
+ end
80
88
rm (dst; recursive= true )
81
89
else
82
90
throw (ArgumentError (string (" '$dst ' exists. `remove_destination=true` " ,
83
- " is required to remove '$dst ' before copying ." )))
91
+ " is required to remove '$dst ' before $(txt) ." )))
84
92
end
85
93
end
94
+ end
95
+
96
+ function cptree (src:: AbstractString , dst:: AbstractString ; remove_destination:: Bool = false ,
97
+ follow_symlinks:: Bool = false )
98
+ isdir (src) || throw (ArgumentError (" '$src ' is not a directory. Use `cp(src, dst)`" ))
99
+ checkfor_mv_cp_cptree (src, dst, " copying" ; remove_destination= remove_destination)
86
100
mkdir (dst)
87
101
for name in readdir (src)
88
102
srcname = joinpath (src, name)
99
113
100
114
function cp (src:: AbstractString , dst:: AbstractString ; remove_destination:: Bool = false ,
101
115
follow_symlinks:: Bool = false )
102
- if ispath (dst)
103
- if remove_destination
104
- rm (dst; recursive= true )
105
- else
106
- throw (ArgumentError (string (" '$dst ' exists. `remove_destination=true` " ,
107
- " is required to remove '$dst ' before copying." )))
108
- end
109
- end
116
+ checkfor_mv_cp_cptree (src, dst, " copying" ; remove_destination= remove_destination)
110
117
if ! follow_symlinks && islink (src)
111
118
symlink (readlink (src), dst)
112
119
elseif isdir (src)
@@ -115,7 +122,11 @@ function cp(src::AbstractString, dst::AbstractString; remove_destination::Bool=f
115
122
FS. sendfile (src, dst)
116
123
end
117
124
end
118
- mv (src:: AbstractString , dst:: AbstractString ) = FS. rename (src, dst)
125
+
126
+ function mv (src:: AbstractString , dst:: AbstractString ; remove_destination:: Bool = false )
127
+ checkfor_mv_cp_cptree (src, dst, " moving" ; remove_destination= remove_destination)
128
+ FS. rename (src, dst)
129
+ end
119
130
120
131
function touch (path:: AbstractString )
121
132
f = FS. open (path,JL_O_WRONLY | JL_O_CREAT, 0o0666 )
0 commit comments