1999-09-14 09:32:05 -04:00
|
|
|
;;; Ops that create objects in the file system:
|
|
|
|
;;; create-{directory,fifo,hard-link,symlink}
|
|
|
|
;;; Copyright (c) 1993 by Olin Shivers.
|
|
|
|
|
|
|
|
;;; This procedure nukes FNAME, whatever it may be: directory, file, fifo,
|
|
|
|
;;; symlink.
|
|
|
|
;;;
|
|
|
|
;;; We can't probe FNAME to find out what it is and then do the right
|
|
|
|
;;; delete operation because there's a window in-between the probe and the
|
|
|
|
;;; delete where the file system can be altered -- the probe and delete
|
|
|
|
;;; aren't atomic. In order to deliver on our contract, we have to spin
|
|
|
|
;;; in a funny loop until we win. In practice, the loop will probably never
|
|
|
|
;;; execute more than once.
|
|
|
|
|
|
|
|
(define (delete-filesys-object fname)
|
|
|
|
(let loop ()
|
|
|
|
(or (with-errno-handler ; Assume it's a file and try.
|
|
|
|
((err data)
|
|
|
|
((errno/perm) #f) ; Return #f if directory
|
2002-02-19 12:18:45 -05:00
|
|
|
((errno/isdir) #f)
|
1999-09-14 09:32:05 -04:00
|
|
|
((errno/noent) #t))
|
|
|
|
(delete-file fname)
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(with-errno-handler ; Assume it's a directory and try.
|
|
|
|
((err data)
|
|
|
|
((errno/notdir) #f) ; Return #f if fname is not a directory.
|
|
|
|
((errno/noent) #t))
|
|
|
|
(delete-directory fname)
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(loop)))) ; Strange things are happening. Try again.
|
|
|
|
|
|
|
|
|
|
|
|
;;; For similar reasons, all of these ops must loop.
|
|
|
|
|
|
|
|
;;; Abstract out common code for create-{directory,fifo,hard-link,symlink}:
|
|
|
|
|
|
|
|
(define (create-file-thing fname makeit override? op-name syscall)
|
2002-03-05 11:49:58 -05:00
|
|
|
(let ((query (lambda ()
|
|
|
|
(y-or-n? (string-append op-name ": " fname
|
|
|
|
" already exists. Delete")))))
|
|
|
|
(let ((result
|
|
|
|
(let loop ((override? override?))
|
|
|
|
(with-errno-handler
|
|
|
|
((err data)
|
|
|
|
((errno/exist)
|
|
|
|
(cond ((if (eq? override? 'query)
|
|
|
|
(query)
|
|
|
|
override?)
|
|
|
|
(delete-filesys-object fname)
|
|
|
|
(loop #t))
|
2001-08-08 05:21:20 -04:00
|
|
|
;;; raising an error here won't work due to S48's
|
|
|
|
;;; broken exception system
|
2002-03-05 11:49:58 -05:00
|
|
|
(else (list err syscall fname)))))
|
2002-08-13 02:49:22 -04:00
|
|
|
(with-resources-aligned
|
2002-09-06 05:57:32 -04:00
|
|
|
(list cwd-resource umask-resource euid-resource egid-resource)
|
2002-08-13 02:49:22 -04:00
|
|
|
(lambda ()
|
|
|
|
(makeit fname)))
|
2002-03-05 11:49:58 -05:00
|
|
|
#f))))
|
|
|
|
(if (list? result)
|
|
|
|
(apply errno-error result)
|
2002-05-15 13:05:02 -04:00
|
|
|
(if #f #f)))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
;;;;;;;
|
|
|
|
|
2003-04-28 04:33:46 -04:00
|
|
|
(define (%create-directory path . maybe-mode)
|
|
|
|
(let ((mode (:optional maybe-mode #o777))
|
|
|
|
(fname (ensure-file-name-is-nondirectory path)))
|
|
|
|
(%%create-directory fname mode)))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define (create-directory dir . rest)
|
|
|
|
(let ((perms (if (null? rest) #o777 (car rest)))
|
|
|
|
(override? (if (or (null? rest) (null? (cdr rest))) #f
|
|
|
|
(cadr rest))))
|
|
|
|
(create-file-thing dir
|
2001-08-08 05:21:20 -04:00
|
|
|
(lambda (dir) (%create-directory dir perms))
|
1999-09-14 09:32:05 -04:00
|
|
|
override?
|
|
|
|
"create-directory"
|
|
|
|
create-directory)))
|
|
|
|
|
|
|
|
(define (create-fifo fifo . rest)
|
|
|
|
(let ((perms (if (null? rest) #o777 (car rest)))
|
|
|
|
(override? (if (or (null? rest) (null? (cdr rest))) #f
|
|
|
|
(cadr rest))))
|
|
|
|
(create-file-thing fifo
|
2001-08-08 05:21:20 -04:00
|
|
|
(lambda (fifo) (%create-fifo fifo perms))
|
1999-09-14 09:32:05 -04:00
|
|
|
override?
|
|
|
|
"create-fifo"
|
|
|
|
create-fifo)))
|
|
|
|
|
|
|
|
(define (create-hard-link old-fname new-fname . maybe-override?)
|
|
|
|
(create-file-thing new-fname
|
|
|
|
(lambda (new-fname)
|
2001-08-08 05:21:20 -04:00
|
|
|
(%create-hard-link old-fname new-fname))
|
1999-09-14 09:32:05 -04:00
|
|
|
(:optional maybe-override? #f)
|
|
|
|
"create-hard-link"
|
|
|
|
create-hard-link))
|
|
|
|
|
|
|
|
(define (create-symlink old-fname new-fname . maybe-override?)
|
|
|
|
(create-file-thing new-fname
|
|
|
|
(lambda (symlink)
|
2001-08-08 05:21:20 -04:00
|
|
|
(%create-symlink old-fname symlink))
|
1999-09-14 09:32:05 -04:00
|
|
|
(:optional maybe-override? #f)
|
|
|
|
"create-symlink"
|
|
|
|
create-symlink))
|
|
|
|
|
|
|
|
;;; Unix rename() works backwards from mkdir(), mkfifo(), link(), and
|
|
|
|
;;; symlink() -- it overrides by default, (though insisting on a type
|
|
|
|
;;; match between the old and new object). So we can't use create-file-thing.
|
|
|
|
;;; Note that this loop has a tiny atomicity problem -- if someone
|
|
|
|
;;; creates a file *after* we do our existence check, but *before* we
|
|
|
|
;;; do the rename, we could end up overriding it, when the user asked
|
|
|
|
;;; us not to. That's life in the food chain.
|
|
|
|
|
|
|
|
(define (rename-file old-fname new-fname . maybe-override?)
|
2002-05-15 13:05:02 -04:00
|
|
|
(let ((override? (:optional maybe-override? #f)))
|
|
|
|
(if (or (and override? (not (eq? override? 'query)))
|
|
|
|
(file-not-exists? new-fname)
|
|
|
|
(and override?
|
|
|
|
(y-or-n? (string-append "rename-file:" new-fname
|
|
|
|
" already exists. Delete"))))
|
2002-08-13 02:49:22 -04:00
|
|
|
(with-resources-aligned
|
2002-09-06 05:57:32 -04:00
|
|
|
(list cwd-resource euid-resource egid-resource)
|
2002-08-13 02:49:22 -04:00
|
|
|
(lambda ()
|
|
|
|
(%rename-file old-fname new-fname))))))
|
2003-02-07 08:01:54 -05:00
|
|
|
|
2003-04-28 04:33:46 -04:00
|
|
|
(define (y-or-n? question . maybe-eof-value)
|
|
|
|
(let loop ((count *y-or-n-eof-count*))
|
|
|
|
(display question)
|
|
|
|
(display " (y/n)? ")
|
|
|
|
(let ((line (read-line)))
|
|
|
|
(cond ((eof-object? line)
|
|
|
|
(newline)
|
|
|
|
(if (= count 0)
|
|
|
|
(:optional maybe-eof-value (error "EOF in y-or-n?"))
|
|
|
|
(begin (display "I'll only ask another ")
|
|
|
|
(write count)
|
|
|
|
(display " times.")
|
|
|
|
(newline)
|
|
|
|
(loop (- count 1)))))
|
|
|
|
((< (string-length line) 1) (loop count))
|
|
|
|
((char=? (string-ref line 0) #\y) #t)
|
|
|
|
((char=? (string-ref line 0) #\n) #f)
|
|
|
|
(else (loop count))))))
|
|
|
|
|
|
|
|
(define *y-or-n-eof-count* 100)
|
|
|
|
|
2003-02-07 08:01:54 -05:00
|
|
|
(define (read-symlink path)
|
|
|
|
(with-resources-aligned
|
|
|
|
(list cwd-resource euid-resource egid-resource)
|
|
|
|
(lambda ()
|
|
|
|
(%read-symlink path))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (delete-directory path)
|
|
|
|
(with-resources-aligned
|
|
|
|
(list cwd-resource euid-resource egid-resource)
|
|
|
|
(lambda ()
|
|
|
|
(%delete-directory path))))
|