blob: 5c87b10feb98ae0e34946a89df752c229006f85e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
(eval-only
(defsymacro copy-size 65536))
(defstruct copy-path-opts ()
perms times owner symlinks (euid (geteuid)))
(defstruct copy-path-stack-node ()
path stat new-p)
(defun make-copy-path-opts (opt-list)
(if opt-list
(let (opts)
(each ((opt opt-list))
(if (structp opt)
(set opts opt)
(progn
(unless opts
(set opts (new copy-path-opts)))
(caseql opt
(:perms (set opts.perms t))
(:times (set opts.times t))
(:owner (set opts.owner t))
(:symlinks (set opts.symlinks t))
(:all (set opts.perms t
opts.times t
opts.owner t
opts.symlinks t))
(t (error "~s: unrecognized option ~s" 'copy-path opt))))))
opts)
(load-time (new copy-path-opts))))
(defun copy-file (from-path to-path : preserve-perms preserve-times)
(with-resources ((buf (make-buf copy-size)
(buf-set-length buf 0) (buf-trim buf))
(ist (open-file from-path "b") (close-stream ist))
(ista (fstat ist))
(ost (if (path-dir-p ista)
(throwf 'path-permission `~s: ~a is a directory`
'copy-file from-path)
(open-file to-path "wb"))
(close-stream ost)))
(while (eql (len buf) copy-size)
(fill-buf-adjust buf 0 ist)
(put-buf buf 0 ost))
(when preserve-perms
(chmod ost ista.mode))
(when preserve-times
(flush-stream ost)
(utimes ost
ista.atime (or ista.atime-nsec 0)
ista.mtime (or ista.mtime-nsec 0)))
nil))
(defun copy-files (paths dest-dir : preserve-perms preserve-times)
(each ((path paths))
(while t
(catch**
(return (copy-file path (path-cat dest-dir (base-name path))
preserve-perms preserve-times))
(skip `skip copying @path` (exc . args) (return))
(retry `retry copying @path` (exc . args))))))
(defun do-tweak-obj (to-path st opts link-p)
(when (and opts.perms (not link-p))
(chmod to-path st.mode))
(when opts.times
(lutimes to-path
st.atime (or st.atime-nsec 0)
st.mtime (or st.mtime-nsec 0)))
(when (and opts.owner
(or (zerop opts.euid)
(and (path-mine-p st)
(path-my-group-p st))))
(lchown to-path st.uid st.gid)))
(defun do-copy-obj (from-path to-path st opts)
(let ((type (logand st.mode s-ifmt))
(initial-perms (if opts.perms #o700 #o777))
(tweak t))
(caseql* type
(s-ifreg
(copy-file from-path to-path opts.perms opts.times))
(s-ifsock
(mknod to-path (logior type initial-perms)))
(s-ififo
(mkfifo to-path initial-perms))
(s-iflnk
(if opts.symlinks
(symlink (readlink from-path) to-path)
(progn
(do-copy-obj from-path to-path (stat from-path) opts)
(set tweak nil))))
((s-ifblk s-ifchr)
(mknod to-path (logior type initial-perms) st.rdev))
(s-ifdir
(ensure-dir to-path)))
(when tweak
(do-tweak-obj to-path st opts (eq type s-iflnk)))))
(defun copy-path-rec (from-dir to-dir . opt-list)
(let* ((opts (make-copy-path-opts opt-list))
(dir-stack nil))
(unwind-protect
(ftw from-dir
(lambda (path type stat . rest)
(while t
(catch**
(let* ((rel-path (let ((p [path (len from-dir)..:]))
(if (pure-rel-path-p p) p [p 1..:])))
(tgt-path (path-cat to-dir rel-path)))
(unless (starts-with from-dir path)
(error "~s: problem with directory traversal" 'copy-path))
(caseql* type
((ftw-dnr ftw-ns) (error "~s: unable to access ~s"
'copy-path path))
(ftw-d (let ((new-p (ensure-dir tgt-path)))
(whilet ((top (car dir-stack))
((and top
(not (starts-with tgt-path
top.path)))))
(do-tweak-obj top.path top.stat opts nil)
(pop dir-stack))
(push (new copy-path-stack-node
path tgt-path
stat stat
new-p new-p)
dir-stack)))
(t (iflet ((cur (car dir-stack)))
(unless cur.new-p
(remove-path tgt-path)))
(do-copy-obj path tgt-path stat opts)))
(return))
(skip `skip copying @path` (exc . args) (return))
(retry `retry copying @path` (exc . args)))))
ftw-phys)
(whilet ((top (pop dir-stack)))
(do-tweak-obj top.path top.stat opts nil)))))
(defun remove-path-rec (path)
(ftw path
(lambda (path type stat . rest)
(while t
(catch**
(return
(caseql* type
((ftw-dnr ftw-ns) (error "~s: unable to access ~s"
'remove-rec path))
(ftw-dp (rmdir path))
(t (remove-path path))))
(skip `skip removing @path` (exc . args) (return))
(retry `retry copying @path` (exc . args)))))
(logior ftw-phys ftw-depth)))
(defun chmod-rec (path perm)
(ftw path
(lambda (path type stat . rest)
(while t
(catch**
(return
(caseql* type
((ftw-dnr ftw-ns) (error "~s: unable to access ~s"
'remove-rec path))
(ftw-sl)
(t (chmod path perm))))
(skip `skip chmod @path` (exc . args) (return))
(retry `retry chmod @path` (exc . args)))))
(logior ftw-phys)))
(defun chown-rec (path uid gid)
(ftw path
(lambda (path type stat . rest)
(while t
(catch**
(return
(caseql* type
((ftw-dnr ftw-ns) (error "~s: unable to access ~s"
'remove-rec path))
(t (lchown path uid gid))))
(skip `skip chown @path` (exc . args) (return))
(retry `retry chown @path` (exc . args)))))
(logior ftw-phys)))
(defun touch (path : ref-path)
(with-stream (s (or (ignerr (open-file path "mn")) (open-file path "n")))
(if ref-path
(let ((rst (stat ref-path)))
(utimes s 0 nil rst.mtime rst.mtime-nsec))
(utimes s 0 nil 0 t))))
(defun path-simplify (comp)
(let (out)
(each ((c comp))
(casequal c
(".." (if (and out (nequal (car out) ".."))
(pop out)
(push c out)))
(("." ""))
(t (push c out))))
(nreverse out)))
(defun path-split (str)
(let ((spl0 (sspl path-sep-chars str)))
(if (macro-time (find #\\ path-sep-chars))
(iflet ((head (car spl0))
(p (pos #\: head)))
(list* [head 0..(succ p)]
[head (succ p)..:]
(cdr spl0))
spl0)
spl0)))
(defun path-volume (comp)
(let ((head (car comp))
(next (cadr comp)))
(if (macro-time (find #\\ path-sep-chars))
(cond
((and (equal head "") (equal next ""))
(let ((vol (caddr comp)))
(if (nequal "" vol) vol :abs)))
((equal head "") :abs)
((and (m^ #/[A-Za-z0-9]+:/ head) head)
(if (equal "" next)
^(:abs . ,head)
^(:rel . ,head))))
(if (equal head "") :abs))))
(defun rel-path (from to)
(let* ((fspl (path-split from))
(tspl (path-split to))
(fvol (path-volume fspl))
(tvol (path-volume tspl)))
(when (nequal fvol tvol)
(if (and (meq :abs fvol tvol) (meq nil fvol tvol))
(error "~s: mixture of absolute and relative paths ~s ~s given"
'rel-path from to))
(if (meq :abs fvol tvol)
(error "~s: mixture of absolute and volume paths ~s ~s given"
'rel-path from to))
(when (and (consp fvol) (consp tvol))
(if (neq (car fvol) (car tvol))
(error "~s: mixture of volume absolute and relative paths ~s ~s given"
'rel-path from to)
(error "~s: paths on different volumes ~s ~s given"
'rel-path from to))))
(let* ((fcomp (path-simplify fspl))
(tcomp (path-simplify tspl))
(ncommon (mismatch fcomp tcomp)))
(cond
((null ncommon) ".")
((find ".." (nthcdr ncommon fcomp))
(error "~s: from path uses .. to escape common prefix: ~s ~s"
'rel-path from to))
(t (let ((nup (- (len fcomp) ncommon))
(down [tcomp ncommon..:]))
(cat-str (append (repeat '("..") nup) down)
[path-sep-chars 0])))))))
(defun path-equal (left right)
(if (and (stringp left) (equal left right))
t
(let* ((lspl (path-split left))
(rspl (path-split right))
(lvol (path-volume lspl))
(rvol (path-volume rspl)))
(if (nequal lvol rvol)
nil
(equal (path-simplify lspl)
(path-simplify rspl))))))
|