Debian Codegraph @

libsrc/file/util.scm

raw
1 ;;;
2 ;;; file/util.scm - filesystem utility functions
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;;; This module provides convenient utility functions to handle
35 ;;; files and directories. Some functions are provided with
36 ;;; different names for compatibility with existing Scheme
37 ;;; implementations.
38
39 #!no-fold-case
40
41 (define-module file.util
42 (use srfi.13)
43 (use util.match)
44 (export current-directory directory-list directory-list2 directory-fold
45 home-directory temporary-directory
46 make-directory* create-directory* remove-directory* delete-directory*
47 copy-directory*
48 call-with-temporary-file call-with-temporary-directory
49 create-directory-tree check-directory-tree
50 build-path resolve-path expand-path simplify-path decompose-path
51 absolute-path? relative-path? find-file-in-paths
52 path-separator
53 path-extension path-sans-extension path-swap-extension
54 file-is-readable? file-is-writable? file-is-executable?
55 file-is-symlink?
56 file-type file-perm file-mode file-ino file-dev file-rdev file-nlink
57 file-uid file-gid file-size file-mtime file-atime file-ctime
58 file-eq? file-eqv? file-equal? file-device=?
59 file-mtime=? file-mtime<? file-mtime<=? file-mtime>? file-mtime>=?
60 file-atime=? file-atime<? file-atime<=? file-atime>? file-atime>=?
61 file-ctime=? file-ctime<? file-ctime<=? file-ctime>? file-ctime>=?
62 touch-file touch-files copy-file move-file
63 ;copy-files move-files
64 remove-file delete-file
65 remove-files delete-files
66 null-device console-device
67 file->string file->string-list file->list file->sexp-list
68 file->bytevector
69 string->file string-list->file list->file sexp-list->file
70 <lock-file-failure> with-lock-file
71 ))
72 (select-module file.util)
73
74 (autoload gauche.uvector port->uvector)
75
76 ;; Common util. Returns #f if PATH does not exist.
77
78 (define (safe-stat path follow-link?)
79 (if (cond-expand [gauche.os.windows #t]
80 [else follow-link?])
81 (and (sys-access path F_OK) (sys-stat path))
82 ;; When we don't follow symlink, we can't use sys-access since it returns
83 ;; #f if PATH is a dangling symlink.
84 (guard (e [(and (<system-error> e) (eq? (ref e 'errno) ENOENT)) #f])
85 (sys-lstat path))))
86
87 (define (%stat path follow-link?)
88 ((if follow-link? sys-stat sys-lstat) path))
89
90 ;;;=============================================================
91 ;;; Directory entries
92
93 ;; "current-directory" is found in ChezScheme, MzScheme, etc.
94 (define (current-directory :optional (newdir #f))
95 (match newdir
96 [#f (sys-getcwd)]
97 [(? string?) (sys-chdir newdir)]
98 [_ (error "directory name should be a string" newdir)]))
99
100 (define (home-directory :optional (user (sys-getuid)))
101 (cond-expand
102 [gauche.os.windows
103 (unless (eqv? user (sys-getuid))
104 (error "On windows native platforms, getting other user's home directory \
105 is not supported (yet)."))
106 ;; See src/system.c for the way to determine the home directory.
107 (sys-normalize-pathname "~" :expand #t)]
108 [else
109 (and-let* ([ent (cond [(integer? user) (sys-getpwuid user)]
110 [(string? user) (sys-getpwnam user)]
111 [else (error "bad user" user)])])
112 (slot-ref ent 'dir))]))
113
114 (define temporary-directory
115 (make-parameter (sys-tmpdir)))
116
117 (define (call-with-temporary-file proc
118 :key (directory (temporary-directory))
119 (prefix "gtemp"))
120 (receive (oport name) (sys-mkstemp (build-path directory prefix))
121 (unwind-protect (proc oport name)
122 (close-output-port oport)
123 (sys-unlink name))))
124
125 (define (call-with-temporary-directory proc
126 :key (directory (temporary-directory))
127 (prefix "gtemp"))
128 (let1 dir (sys-mkdtemp (build-path directory prefix))
129 (unwind-protect (proc dir)
130 (remove-directory* dir))))
131
132 (define (null-device)
133 (cond-expand
134 [gauche.os.windows "NUL"]
135 [else "/dev/null"]))
136
137 (define (console-device)
138 (cond-expand
139 [gauche.os.windows "CON"]
140 [else "/dev/tty"]))
141
142 ;; utility for directory-list and directory-list2
143 (define (%directory-filter dir pred filter-add-path?)
144 (if filter-add-path?
145 (filter (^e (pred (build-path dir e))) (sys-readdir dir))
146 (filter pred (sys-readdir dir))))
147
148 (define (%directory-filter-compose opts)
149 (let-keywords opts ((children? #f)
150 (filter #f))
151 (apply every-pred
152 (cond-list
153 [children? (^e (not (member (sys-basename e) '("." ".."))))]
154 [filter]))))
155
156 ;; directory-list DIR &keyword ADD-PATH? FILTER-ADD-PATH? CHILDREN? FILTER
157 (define (directory-list dir :key (add-path? #f)
158 (filter-add-path? #f)
159 :allow-other-keys other-keys)
160 (let1 entries
161 (sort (%directory-filter dir (%directory-filter-compose other-keys)
162 filter-add-path?))
163 (if add-path?
164 (map (cut build-path dir <>) entries)
165 entries)))
166
167 ;; directory-list2 DIR &optional ADD-DIR? FILTER-ADD-PATH? CHILDREN? FILTER FOLLOW-LINK?
168 (define (directory-list2 dir :key (add-path? #f)
169 (filter-add-path? #f)
170 (follow-link? #t)
171 :allow-other-keys other-keys)
172 (let* ([filters (%directory-filter-compose other-keys)]
173 [selector (lambda (e)
174 (and-let* ([s (safe-stat e follow-link?)])
175 (eq? (slot-ref s 'type) 'directory)))]
176 [entries (sort (%directory-filter dir filters filter-add-path?))])
177 (if add-path?
178 (partition selector (map (cut build-path dir <>) entries))
179 (partition (^e (selector (build-path dir e))) entries))))
180
181 ;; directory-fold DIR PROC KNIL &keyword LISTER FOLDER FOLLOW-LINK?
182 (define (directory-fold dir proc knil
183 :key (lister (lambda (path knil)
184 (values (directory-list path
185 :add-path? #t
186 :children? #t)
187 knil)))
188 (folder fold)
189 (follow-link? #t))
190 (define (selector e)
191 (and (file-exists? e)
192 (eq? (slot-ref (%stat e follow-link?) 'type) 'directory)))
193 (define (rec path knil)
194 (if (selector path)
195 ;; [TODO]: For the backward compatibiliy, we allow LISTER to return
196 ;; only a single value. Should be removed, probably in 0.9.
197 (receive res (lister path knil)
198 (folder rec (get-optional (cdr res) knil) (car res)))
199 (proc path knil)))
200 (rec dir knil))
201
202 ;; mkdir -p
203 (define (make-directory* dir :optional (mode #o755))
204 (define (err-notdir p)
205 (errorf "non-directory ~s is found while creating a directory ~s"
206 (sys-basename p) dir))
207
208 (define (rec p)
209 (if (file-exists? p)
210 (unless (file-is-directory? p) (err-notdir p))
211 (let1 d (sys-dirname p)
212 (rec d)
213 (unless (file-is-writable? d)
214 (errorf "directory ~s unwritable during creating a directory ~s"
215 d dir))
216 (unless (equal? (sys-basename p) ".") ; omit the last component in "/a/b/c/."
217 ;; Directory p might have been created by other process after
218 ;; we checked it doesn't exist. We rescue such case.
219 (guard (e [(and (is-a? e <system-error>)
220 (eqv? (~ e'errno) EEXIST))
221 (unless (file-is-directory? p) (err-notdir p))]
222 [else (raise e)])
223 (sys-mkdir p mode)))
224 )))
225 ;; some platform complains the last "/"
226 (rec (string-trim-right dir #[/])))
227
228 ;; synonym
229 (define create-directory* make-directory*)
230
231 ;; rm -rf
232 (define (remove-directory* dir :key (if-does-not-exist :error))
233 (define (rec d)
234 (receive (dirs files)
235 (directory-list2 d :add-path? #t :children? #t :follow-link? #f)
236 (for-each rec dirs)
237 (for-each sys-rmdir dirs)
238 (for-each sys-unlink files)))
239 (case if-does-not-exist
240 [(:error #f)]
241 [else (error ":if-does-not-exist must be either :error or #f, bot got:"
242 if-does-not-exist)])
243 (if (file-exists? dir)
244 (if (file-is-directory? dir)
245 (begin (rec dir) (sys-rmdir dir) #t)
246 (error "Not a directory:" dir))
247 (if if-does-not-exist
248 (error "No such directory:" dir)
249 #f)))
250
251 (define delete-directory* remove-directory*)
252
253 ;; cp -r
254 ;; if-exists - :error :supersede :backup :overwrite #f
255 ;; backup-suffix
256 ;; safe
257 ;; follow-link? - follow symlink. the default is #f
258 ;; keep-timestamp
259 ;; keep-mode
260 (define (copy-directory* src dst :key (if-exists :error)
261 ((:backup-suffix backsfx) ".orig")
262 (safe #f)
263 (follow-link? #f)
264 ((:keep-timestamp keeptime) #f)
265 ((:keep-mode keepmode) #f))
266
267 (define (postprocess src dst)
268 (let1 stat (sys-stat src)
269 (when keeptime (sys-utime dst (ref stat'atime) (ref stat'mtime)))
270 (when keepmode (sys-chmod dst (ref stat'perm)))))
271
272 (define (do-copy src dst)
273 (copy-file src dst :if-exists if-exists :backup-suffix backsfx
274 :follow-link? follow-link?
275 :keep-timestamp keeptime :keep-mode keepmode))
276
277 (define (rec src dst)
278 (cond [(file-is-directory? src)
279 (let1 exists? (file-exists? dst)
280 (if (not exists?)
281 (sys-mkdir dst #o777)
282 (case if-exists
283 [(:error) (error "destination file exists" dst)]
284 [(:supersede)
285 (remove-files dst)
286 (sys-mkdir dst #o777)]
287 [(:overwrite)
288 (unless (file-is-directory? dst)
289 (remove-files dst))
290 ;; TODO: if dst directory exists, check the permission
291 ]
292 ;; NB: :backup is handled by toplevel
293 [else (error "unknown if-exists value:" if-exists)]))
294 (dolist [s (directory-list src :add-path? #t :children? #t)]
295 (rec s (build-path dst (sys-basename s))))
296 (unless exists? (postprocess src dst))
297 )]
298 [else (do-copy src dst)]))
299
300 (cond [(file-is-symlink? src) ; we check this first, for src may be a dangling symlink.
301 (do-copy src dst)]
302 [(not (file-exists? src))
303 (error "source file does not exist:" src)]
304 [(file-exists? dst)
305 (case if-exists
306 [(:backup)
307 (sys-rename dst (string-append dst backsfx))
308 (rec src dst)]
309 [(#f) #f]
310 [else ; other if-exists options are handled later
311 (rec src dst)])]
312 [else (rec src dst)])
313 )
314
315 ;; Create/check certain directory structure at once
316 ;;
317 ;; Directory Tree
318 ;; <tree> : <name> ; empty file
319 ;; | (<name> [options]) ; empty file
320 ;; | (<name> [options] <string>) ; file with content
321 ;; | (<name> [options] <proc>) ; using output of <proc>
322 ;; | (<name> [options] (<tree> ...)) ; directory
323 ;; <name> : string
324 ;;
325 ;; options is alternating list of keywords and values.
326 ;;
327 ;; :mode <integer> ; file/dir mode bits
328 ;; :owner <id> ; file/dir owner
329 ;; :group <id> ; file/dir group
330 ;; :symlink <path> ; file is actually a symlink to <path>
331
332 (define-values (create-directory-tree
333 check-directory-tree)
334 (let ()
335 (define (name? x) (or (string? x) (symbol? x)))
336 (define (collect-options args)
337 (let loop ([args args] [r '()])
338 (match args
339 [() (values (reverse r) #f)]
340 [([? keyword? k] val . rest) (loop rest (list* val k r))]
341 [(arg) (values (reverse r) arg)]
342 [_ (error "invalid option list:" args)])))
343 (define (mkpath dir name) (build-path dir (x->string name)))
344 (define chown (cond-expand
345 [gauche.sys.lchown sys-lchown]
346 [else sys-chown]))
347 (define (walk dir node do-file do-dir)
348 (match node
349 [[? name?] (do-file (mkpath dir node) #f)]
350 [([? name? n] . args)
351 (receive (opts content) (collect-options args)
352 (if (list? content)
353 (apply do-dir (mkpath dir n) content opts)
354 (apply do-file (mkpath dir n) content opts)))]
355 [_ (error "invalid tree node:" node)]))
356
357 (define (ensure-file path content
358 :key (mode #f) (owner -1) (group -1) (symlink #f))
359 (if symlink
360 (sys-symlink symlink path)
361 (cond
362 [(not content) (touch-file path)]
363 [(string? content) (with-output-to-file path (cut display content))]
364 [else (with-output-to-file path (cut content path))]))
365 ;; NB: BSD systems has lchmod, so we may support :mode with :symlink
366 ;; in future.
367 (when (and mode (not symlink)) (sys-chmod path mode))
368 (when (or (>= owner 0) (>= group 0)) (chown path owner group)))
369 (define (ensure-dir path children :key (mode #o755) (owner -1) (group -1))
370 (make-directory* path mode)
371 (for-each (cut ensure path <>) children)
372 (when (or (>= owner 0) (>= group 0)) (sys-chown path owner group)))
373 (define (ensure dir node) (walk dir node ensure-file ensure-dir))
374 (define (create-directory-tree start tree) (ensure start tree))
375
376 (define (check-file path content
377 :key (mode #f) (owner -1) (group -1) (symlink #f))
378 (if symlink
379 (cond-expand
380 [gauche.sys.symlink
381 (and (file-is-symlink? path)
382 (check-attrs path mode owner group)
383 (equal? symlink (sys-readlink path)))]
384 [else #f])
385 (and (file-is-regular? path)
386 (check-attrs path mode owner group)
387 (cond
388 [(not content) #t]
389 [(string? content) (string=? content (file->string path))]
390 [else (equal? (with-output-to-string (cut content path))
391 (file->string path))]))))
392 (define (check-dir path children :key (mode #f) (owner -1) (group -1))
393 (and (file-is-directory? path)
394 (check-attrs path mode owner group)
395 (every (cut check path <>) children)))
396 (define (check-attrs path mode owner group)
397 (let1 s (sys-lstat path)
398 (and (or (not mode) (= (~ s'perm) mode))
399 (or (negative? owner) (= (~ s'uid) owner))
400 (or (negative? group) (= (~ s'gid) group)))))
401 (define (check dir node) (walk dir node check-file check-dir))
402 (define (check-directory-tree start tree) (check start tree))
403
404 (values create-directory-tree check-directory-tree)))
405
406 ;;;=============================================================
407 ;;; Pathnames
408
409 (define (build-path base-path . components)
410 (define path-separator-string (string (path-separator)))
411 (define real-base-path
412 (case base-path
413 [(cwd) (current-directory)]
414 [(cld) (if-let1 clp (current-load-path)
415 (sys-dirname clp)
416 (current-directory))]
417 [(#f) "."]
418 [else (unless (string? base-path)
419 (error "base-path must be a string, #f, \
420 or a symbol 'cwd or 'cld, \
421 but got:" base-path))
422 base-path]))
423 (define (rec base components)
424 (if (null? components)
425 base
426 (let1 component
427 (match (car components)
428 ['up ".."]
429 ['same "."]
430 ['#f "."]
431 [(? absolute-path? p)
432 (error "can't append absolute path after other path" p)]
433 [p (sys-normalize-pathname p)])
434 (rec (cond [(string-null? base) component]
435 [(string-null? component) base]
436 [(#/[\/\\]$/ base) (string-append base component)]
437 [else
438 (string-append base path-separator-string component)])
439 (cdr components)))))
440 (rec (sys-normalize-pathname real-base-path) components))
441
442 (define (expand-path path)
443 (sys-normalize-pathname path :expand #t))
444
445 (define (resolve-path path)
446 (define (pathcat dir base) (simplify-path (build-path dir base)))
447 (define (rec pat)
448 (let ([dir (sys-dirname pat)]
449 [base (sys-basename pat)])
450 (receive (ndir p) (if (or (string=? dir "/") (string=? dir "."))
451 (values dir pat)
452 (let1 nd (rec dir) (values nd (pathcat nd base))))
453 (unless (sys-access p F_OK)
454 (error "path doesn't exist" path))
455 (let loop ((count 0) (p p))
456 (cond [(>= count 8) ;; arbitrary upper bound to detect infinite loop
457 (error "possibly looping symlink" pat)]
458 [(eq? (file-type p :follow-link? #f) 'symlink)
459 (cond-expand
460 [gauche.sys.symlink
461 (loop (+ count 1)
462 (let1 np (sys-readlink p)
463 (if (absolute-path? np) np (pathcat ndir np))))]
464 [else p])]
465 [else p])))))
466 (rec (expand-path path)))
467
468 (define (simplify-path path)
469 (sys-normalize-pathname path :canonicalize #t))
470
471 (define (decompose-path path)
472 (if (#/[\/\\]$/ path)
473 (values (string-trim-right path #[\\/]) #f #f)
474 (let ([dir (sys-dirname path)]
475 [base (sys-basename path)])
476 (cond [(string-index-right base #\.)
477 => (lambda (pos)
478 (if (zero? pos)
479 (values dir base #f) ; '.' at the beginning doesn't delimit extension
480 (values dir
481 (string-take base pos)
482 (string-drop base (+ pos 1)))))]
483 [else (values dir base #f)]))))
484
485 (define (relative-path? path)
486 (cond-expand
487 [gauche.os.windows (not (#/^[\/\\]|^[A-Za-z]:/ path))]
488 [else (not (#/^\// path))]))
489
490 (define (absolute-path? path)
491 (not (relative-path? path)))
492
493 (define (path-separator)
494 (cond-expand
495 [gauche.os.windows #\\]
496 [else #\/]))
497
498 (define (path-extension path)
499 (receive (dir file ext) (decompose-path path) ext))
500
501 (define (path-sans-extension path)
502 (cond [(path-extension path)
503 => (lambda (ext)
504 (substring path 0
505 (- (string-length path) (string-length ext) 1)))]
506 [else path]))
507
508 (define (path-swap-extension path ext)
509 (if ext
510 (string-append (path-sans-extension path) "." ext)
511 (path-sans-extension path)))
512
513 ;; find-file-in-paths is now in core. This is for the backward compatibility.
514 (define find-file-in-paths sys-find-file)
515
516 ;;;=============================================================
517 ;;; File attributes
518
519 ;; convenient accessors for file stats. accepts string file name or
520 ;; <sys-stat>. If the named file doesn't exist, returns #f.
521 ;; accepts keyword argument FOLLOW-LINK?
522 (define-syntax define-stat-accessor
523 (syntax-rules ()
524 [(_ name slot)
525 (define (name path :key (follow-link? #t))
526 (and-let* ([s (safe-stat path follow-link?)])
527 (slot-ref s slot)))]))
528
529 (define-stat-accessor file-type 'type)
530 (define-stat-accessor file-perm 'perm)
531 (define-stat-accessor file-mode 'mode)
532 (define-stat-accessor file-ino 'ino)
533 (define-stat-accessor file-dev 'dev)
534 (define-stat-accessor file-rdev 'rdev)
535 (define-stat-accessor file-nlink 'nlink)
536 (define-stat-accessor file-uid 'uid)
537 (define-stat-accessor file-gid 'gid)
538 (define-stat-accessor file-size 'size)
539 (define-stat-accessor file-atime 'atime)
540 (define-stat-accessor file-mtime 'mtime)
541 (define-stat-accessor file-ctime 'ctime)
542
543 ;; file permissions
544 (define (file-is-readable? path) (sys-access path R_OK))
545 (define (file-is-writable? path) (sys-access path W_OK))
546 (define (file-is-executable? path) (sys-access path X_OK))
547
548 (define (file-is-symlink? path)
549 (and-let* ([s (safe-stat path #f)])
550 (eq? (slot-ref s 'type) 'symlink)))
551
552 ;; compares two files are identical, in the sense that:
553 ;; file-eq? - two files (or directories) are the same entity. symbolic
554 ;; links are not followed. The files must exist.
555 ;; file-eqv? - two files are the same entity, after resolving symbolic
556 ;; links. The files must exist.
557 ;; file-equal? - the content of two files are the same.
558
559 ;; NB: on Windows/MinGW, we cannot obtain inode number correctly.
560 ;; We compare canonicalized pathnames (this doesn't work if there's
561 ;; an alias. We should call Windows API to check for sure in future).
562
563 (cond-expand
564 [gauche.os.windows
565 (define (%stat-compare s1 s2 f1 f2)
566 (let ([p1 (sys-normalize-pathname f1 :absolute #t :canonicalize #t)]
567 [p2 (sys-normalize-pathname f2 :absolute #t :canonicalize #t)])
568 (equal? p1 p2)))
569 (define (file-eq? f1 f2) (%stat-compare #f #f f1 f2))
570 (define (file-eqv? f1 f2) (%stat-compare #f #f f1 f2))
571 ]
572 [else
573 (define (%stat-compare s1 s2 f1 f2)
574 (and (eqv? (slot-ref s1 'dev) (slot-ref s2 'dev))
575 (eqv? (slot-ref s1 'ino) (slot-ref s2 'ino))))
576 (define (file-eq? f1 f2) (%stat-compare (sys-lstat f1) (sys-lstat f2) f1 f2))
577 (define (file-eqv? f1 f2) (%stat-compare (sys-stat f1) (sys-stat f2) f1 f2))
578 ])
579
580 (define (file-equal? f1 f2)
581 (let ([s1 (sys-stat f1)]
582 [s2 (sys-stat f2)])
583 (cond [(%stat-compare s1 s2 f1 f2)]
584 [(not (eq? (slot-ref s1 'type) (slot-ref s2 'type))) #f]
585 [(not (= (slot-ref s1 'size) (slot-ref s2 'size))) #f]
586 [(eq? (slot-ref s1 'type) 'directory)
587 (error "directory comparison is not supported yet" s1)]
588 [else
589 (call-with-input-file f1
590 (lambda (p1)
591 (call-with-input-file f2
592 (lambda (p2)
593 (let loop ([b1 (read-block 8192 p1)]
594 [b2 (read-block 8192 p2)])
595 (cond [(eof-object? b1) #t]
596 [(string=? b1 b2)
597 (loop (read-block 8192 p1) (read-block 8192 p2))]
598 [else #f]))))))]
599 )))
600
601 ;; see if two files or directories exist on the same device.
602 (define (file-device=? f1 f2)
603 (eqv? (slot-ref (sys-stat f1) 'dev) (slot-ref (sys-stat f2) 'dev)))
604
605 ;; comparing file timestamp. accepts string file name, <sys-stat>,
606 ;; <time>, or number.
607 (define-syntax define-time-comparer
608 (syntax-rules ()
609 [(_ name slot cmp)
610 (begin
611 (define-method name ((a <sys-stat>) (b <sys-stat>))
612 (cmp (slot-ref a slot) (slot-ref b slot)))
613 (define-method name ((a <sys-stat>) (b <number>))
614 (cmp (slot-ref a slot) b))
615 (define-method name ((a <number>) (b <sys-stat>))
616 (cmp a (slot-ref b slot)))
617 (define-method name ((a <string>) (b <string>))
618 (name (sys-stat a) (sys-stat b)))
619 (define-method name ((a <string>) b)
620 (name (sys-stat a) b))
621 (define-method name (a (b <string>))
622 (name a (sys-stat b)))
623 (define-method name ((a <time>) b)
624 (name (slot-ref a 'second) b))
625 (define-method name (a (b <time>))
626 (name a (slot-ref b 'second)))
627 )]))
628
629 (define-time-comparer file-mtime=? 'mtime =)
630 (define-time-comparer file-mtime<? 'mtime <)
631 (define-time-comparer file-mtime<=? 'mtime <=)
632 (define-time-comparer file-mtime>? 'mtime >)
633 (define-time-comparer file-mtime>=? 'mtime >=)
634
635 (define-time-comparer file-ctime=? 'ctime =)
636 (define-time-comparer file-ctime<? 'ctime <)
637 (define-time-comparer file-ctime<=? 'ctime <=)
638 (define-time-comparer file-ctime>? 'ctime >)
639 (define-time-comparer file-ctime>=? 'ctime >=)
640
641 (define-time-comparer file-atime=? 'atime =)
642 (define-time-comparer file-atime<? 'atime <)
643 (define-time-comparer file-atime<=? 'atime <=)
644 (define-time-comparer file-atime>? 'atime >)
645 (define-time-comparer file-atime>=? 'atime >=)
646
647 ;;;=============================================================
648 ;;; File operation
649 ;;;
650
651 (define (touch-file pathname :key (time #f) (type #f) (create #t))
652 (unless (or (not time) (and (real? time) (not (negative? time))))
653 (error "bad value for time: #f or nonnegative real is expected, got:" time))
654 (cond
655 [(sys-access pathname F_OK)
656 (case type
657 [(#f) (sys-utime pathname time time)]
658 [(mtime) (let1 s (sys-stat pathname)
659 (sys-utime pathname (~ s'atime) time))]
660 [(atime) (let1 s (sys-stat pathname)
661 (sys-utime pathname time (~ s'mtime)))]
662 [else (error "bad value for type: #f, 'atime, or 'mtime is expected, got:"
663 type)])]
664 [create
665 (close-output-port (open-output-file pathname))
666 (when time (touch-file pathname :time time :type type))])
667 (values))
668
669 (define (touch-files pathnames :key (time (undefined))
670 (type (undefined))
671 (create (undefined)))
672 (for-each (cut touch-file <> :time time :type type :create create) pathnames))
673
674 ;; copy-file
675 ;; if-exists - :error :supersede :backup :append #f
676 ;; follow-link? - follow symlinks (default #t)
677 ;; backup-suffix
678 ;; safe
679 ;; keep-timestamp
680 ;; keep-mode
681 (define (copy-file src dst :key (if-exists :error)
682 ((:backup-suffix backsfx) ".orig")
683 (follow-link? #t)
684 (safe #f)
685 ((:keep-timestamp keeptime) #f)
686 ((:keep-mode keepmode) #f))
687 (let ([backfile (string-append dst backsfx)]
688 [times '()]
689 [tmpfile #f]
690 [inport #f]
691 [outport #f]
692 [dst-exists #f]
693 [default-perm (logand #o666 (lognot (sys-umask)))])
694 (define (rollback)
695 (cond [inport => close-input-port])
696 (cond [outport => close-output-port])
697 (cond [tmpfile => sys-unlink]))
698 (define (commit)
699 (cond [inport => close-input-port])
700 (cond [outport => close-output-port])
701 (cond [tmpfile
702 (safe-chmod tmpfile (if keepmode (file-perm src) default-perm))
703 (when (eq? if-exists :backup) (sys-rename dst backfile))
704 (sys-rename tmpfile dst)]
705 [keepmode (safe-chmod dst (file-perm src))]
706 [(not dst-exists) (safe-chmod dst default-perm)])
707 (unless (null? times) (apply sys-utime dst times)))
708 (define (safe-chmod path mode)
709 (and (not (file-is-symlink? path)) (sys-chmod path mode)))
710 (define (open-destination)
711 (if safe
712 (cond [(and (eq? if-exists :error) (file-exists? dst))
713 (error "destination file exists" dst)]
714 [(and (not if-exists) (file-exists? dst)) #f]
715 [(and (eq? if-exists :append) (file-exists? dst))
716 (set!-values (outport tmpfile) (sys-mkstemp dst))
717 (call-with-input-file dst (cut copy-port <> outport))
718 #t]
719 [else
720 (set!-values (outport tmpfile) (sys-mkstemp dst)) #t])
721 (ecase if-exists
722 [(#f) (set! outport (open-output-file dst :if-exists #f)) outport]
723 [(:backup)
724 (when (file-exists? dst) (sys-rename dst backfile))
725 (set! outport (open-output-file dst)) #t]
726 [(:supersede :append :error)
727 (set! outport (open-output-file dst :if-exists if-exists)) #t])))
728 (define (get-times stat)
729 (map (cut slot-ref stat <>) '(atime mtime)))
730 (define (do-symlink)
731 (define (doit)
732 (cond-expand
733 [gauche.sys.symlink (sys-symlink (sys-readlink src) dst)]
734 [else #f]));NB: if system doesn't support symlink, we can't be here.
735 (when keeptime (set! times (get-times (sys-lstat src))))
736 (if (file-exists? dst)
737 (case if-exists
738 [(:error :append) (error <system-error> :errno EEXIST
739 "destination file exists" dst)]
740 [(#f) #f]
741 [(:backup) (sys-rename dst backfile) (doit)]
742 [else (sys-unlink dst) (doit)])
743 (doit))
744 (commit))
745 (define (do-copy)
746 (guard (e (else (rollback) (raise e)))
747 (set! inport (open-input-file src))
748 (when keeptime (set! times (get-times (sys-fstat inport))))
749 (set! dst-exists (file-exists? dst))
750 (begin0
751 (and (open-destination)
752 (copy-port inport outport :unit 65536)
753 #t)
754 (commit))))
755
756 ;; body of copy-file
757 (unless (memq if-exists '(#f :error :supersede :backup :append))
758 (error "argument for :if-exists must be either :error, :supersede, :backup, :append or #f, but got" if-exists))
759 (when (and (file-exists? src) (file-exists? dst)
760 ((if follow-link? file-eqv? file-eq?) src dst))
761 (errorf "source ~s and destination ~s are the same file" src dst))
762 (if (and (not follow-link?) (file-is-symlink? src))
763 (do-symlink)
764 (do-copy))
765 ))
766
767 ;; move-file
768 ;; if-exists - :error :supersede :backup #f
769 ;; backup-suffix
770 (define (move-file src dst :key (if-exists :error)
771 ((:backup-suffix backsfx) ".orig"))
772
773 (define (do-rename)
774 (if (file-exists? dst)
775 (cond [(eq? if-exists :error) (error "destination file exists" dst)]
776 [(eq? if-exists :supersede) (sys-rename src dst) #t]
777 [(eq? if-exists :backup)
778 (sys-rename dst (string-append dst backsfx))
779 (sys-rename src dst) #t]
780 [else #f])
781 (begin (sys-rename src dst) #t)))
782 (define (do-copying)
783 (and (copy-file src dst :if-exists if-exists
784 :backup-suffix backsfx
785 :safe #t :keep-timestamp #t :keep-mode #t)
786 (begin (sys-unlink src) #t)))
787
788 ;; body of move-file
789 (unless (memq if-exists '(#f :error :supersede :backup))
790 (error "argument for :if-exists must be either :error, :supersede, :backup or #f, but got" if-exists))
791 (unless (file-exists? src)
792 (error "source file does not exist" src))
793 (when (and (file-exists? dst) (file-eqv? src dst))
794 (errorf "source ~s and destination ~s are the same file" src dst))
795 (let1 dstdir (sys-dirname dst)
796 (unless (file-exists? dstdir)
797 (errorf "can't move to ~s: path does not exist" dst))
798 (if (file-device=? src dstdir)
799 (do-rename)
800 (do-copying)))
801 )
802
803 ;; copy-files & move files
804 ;; NB: need to think more about argument order. Should it be
805 ;; (copy-files file1 file2 ... destdir) ;; cp(1) style
806 ;; or
807 ;; (copy-files destdir file1 file2 ...)
808 ;; And how shall we specify keyword args to pass to copy-file?
809
810 ; (define (%multifile-cpmv op)
811 ; (lambda (files dst . opts)
812 ; (unless (file-exists? dst)
813 ; (error "destination does not exist" dst))
814 ; (unless (file-is-directory? dst)
815 ; (error "destination is not a directory" dst))
816 ; (dolist (f files)
817 ; (apply op f (build-path dst (sys-basename f)) opts))))
818
819 ; (define copy-files (%multifile-cpmv copy-file))
820 ; (define move-files (%multifile-cpmv move-file))
821
822 ;; removing.
823 ;; Unlike sys-unlink, we throw an error if FILE doesn't exist,
824 ;; to align with R7RS. (Technically we should throw a file-error
825 ;; object. We'll fix that later.)
826 (define (remove-file file)
827 (or (sys-unlink file)
828 (error <system-error> :errno ENOENT "File does not exist:" file)))
829 (define delete-file remove-file)
830
831 (define (remove-files . paths)
832 (dolist (p paths)
833 (cond [(list? p) (apply remove-files p)] ; for the convenience
834 [(file-is-directory? p) (remove-directory* p)]
835 [else (sys-unlink p)])))
836
837 (define delete-files remove-files)
838
839 ;; file->string, file->list, file->string-list, file->sexp-list
840 ;; shortcuts of port->string etc.
841 ;; NB: call-with-input-file may pass #f to the proc if :if-does-not-exist #f
842 ;; is given. We return #f in such case.
843
844 (define (%maybe proc)
845 (lambda (maybe-port) (and maybe-port (proc maybe-port))))
846
847 (define (file->string file . opts)
848 (apply call-with-input-file file (%maybe port->string) opts))
849
850 (define (file->bytevector file . opts)
851 (apply call-with-input-file file (%maybe port->uvector) opts))
852
853 (define (file->list reader file . opts)
854 (apply call-with-input-file file (%maybe (cut port->list reader <>)) opts))
855
856 (define (file->string-list file . opts)
857 (apply call-with-input-file file (%maybe (cut port->list read-line <>)) opts))
858
859 (define (file->sexp-list file . opts)
860 (apply call-with-input-file file (%maybe (cut port->list read <>)) opts))
861
862 ;; inverse of file->*
863 (define (string->file file str . opts)
864 (apply call-with-output-file file (cut display str <>) opts))
865
866 (define (list->file writer file lis . opts)
867 (apply call-with-output-file file (^p (dolist [s lis] (writer s p))) opts))
868
869 (define string-list->file
870 (cute list->file (^[s p] (display s p) (newline p)) <> <> <...>))
871
872 (define sexp-list->file
873 (cute list->file (^[s p] (write s p) (newline p)) <> <> <...>))
874
875 ;;;=============================================================
876 ;;; File locking
877 ;;;
878
879 ;; fcntl lock is unreliable in MT environment (It's process-wide lock,
880 ;; so we need mutex to avoid inter-thread races. However, it is not
881 ;; trivial to provide a mutex per arbitrary file to be locked.)
882 ;; So we just provide file/directory based locks.
883 ;;
884 ;; The drawback is that if a process holding the lock dies, the lock file
885 ;; may remain, blocking all other processes. We support 'stealing' the
886 ;; lock: If the existing lock file is too old to be possibly held by the
887 ;; application, the attempted locker can grab the lock.
888 ;;
889 ;; Stealing makes things a bit complicated, since there can be a race
890 ;; if more than two processes tries to steal the lock at the same time.
891 ;; We use a secondary lock file to avoid the race.
892
893 ;; TODO: If we find the lock is held for a certain time,
894 ;; we want to check if the process is actually alive---using (sys-kill pid 0)
895 ;; or something. If there's no process, we can steal the lock right away,
896 ;; not waiting for abandon-timeout. This doesn't work for the lock for
897 ;; a resource that can be shared between multiple machines, so the feature
898 ;; should be optional.
899
900 (define-condition-type <lock-file-failure> <error> #f
901 (lock-file-name #f))
902
903 (define (with-lock-file lock-name proc
904 :key (type 'file)
905 (retry-interval 1)
906 (retry-limit 10)
907 (abandon-timeout 600)
908 (secondary-lock-name #"~|lock-name|.2")
909 (retry2-interval 1)
910 (retry2-limit 10)
911 (perms (if (eq? type 'directory)
912 #o755
913 #o644)))
914 ;; returns #t on success, #f of failure
915 (define (acquire path)
916 (case type
917 [(file)
918 (with-output-to-file path
919 (^() (print (sys-getpid)) (sys-chmod path perms) #t)
920 :if-exists #f)]
921 [(directory)
922 (guard (e [(and (<system-error> e) (eqv? (~ e'errno) EEXIST)) #f])
923 (sys-mkdir path perms) #t)]))
924
925 ;; the simplest way is (remove-files path); but it will eradicate anything
926 ;; under PATH, which is a bit too radical.
927 (define (release path)
928 (guard (e [(<system-error> e)
929 (unless (eq? (~ e'errno) ENOENT)
930 (errorf <lock-file-failure> :lock-file-name lock-name
931 "couldn't remove lock file ~s: ~a"
932 lock-name (sys-strerror (~ e'errno))))])
933 (case type
934 [(file) (sys-unlink path)]
935 [(directory) (sys-rmdir path)])))
936
937 ;; Common retry logic
938 ;; interval and timeout should be in nanosecs
939 (define (try acquirer releaser interval timeout check success failure)
940 (let loop ([elapsed 0])
941 (cond [(acquirer) (unwind-protect (success) (releaser))]
942 [(< elapsed timeout)
943 (let wait ([w interval])
944 (if-let1 w1 (sys-nanosleep w)
945 (wait w1)))
946 (check)
947 (loop (+ interval elapsed))]
948 [else (failure)])))
949
950 ;; scale seconds to nanoseconds
951 (define (nsec secs) (* secs 1e9))
952
953 (define (primary-lock)
954 (try (cut acquire secondary-lock-name)
955 (cut release secondary-lock-name)
956 (nsec retry2-interval) (nsec retry2-limit)
957 values
958 (cut acquire lock-name)
959 secondary-lock-failure))
960
961 (define (primary-unlock)
962 (try (cut acquire secondary-lock-name)
963 (cut release secondary-lock-name)
964 (nsec retry2-interval) (nsec retry2-limit)
965 values
966 (cut release lock-name)
967 secondary-lock-failure))
968
969 (define (secondary-lock-failure)
970 (errorf <lock-file-failure> :lock-file-name lock-name
971 "with-lock-file: secondary lock cannot be acquired (~a). \
972 If no process is locking it, remove it and try again."
973 secondary-lock-name))
974
975 (define (primary-lock-failure)
976 (errorf <lock-file-failure> :lock-file-name lock-name
977 "with-lock-file: couldn't acquire lock file (~a). \
978 If no process is locking it, remove the file and try again."
979 lock-name))
980
981 (define primary-lock-steal
982 (if abandon-timeout
983 (lambda ()
984 (if (and-let* ([m (file-mtime lock-name)])
985 (< (+ m abandon-timeout) (time->seconds (current-time))))
986 (primary-unlock)))
987 values))
988
989 (unless (memq type '(file directory))
990 (error "unsupported lockfile type:" type))
991 ;; main locker
992 (try primary-lock
993 primary-unlock
994 (nsec retry-interval) (nsec retry-limit)
995 primary-lock-steal
996 proc
997 primary-lock-failure))