~ chicken-core (master) /file.scm
Trap1;;;; file.scm - File operations2;3; Copyright (c) 2008-2022, The CHICKEN Team4; Copyright (c) 2000-2007, Felix L. Winkelmann5; All rights reserved.6;7; Redistribution and use in source and binary forms, with or without8; modification, are permitted provided that the following conditions are9; met:10;11; Redistributions of source code must retain the above copyright12; notice, this list of conditions and the following disclaimer.13;14; Redistributions in binary form must reproduce the above copyright15; notice, this list of conditions and the following disclaimer in the16; documentation and/or other materials provided with the distribution.17;18; Neither the name of the author nor the names of its contributors may19; be used to endorse or promote products derived from this software20; without specific prior written permission.21;22; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS23; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT24; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR25; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT26; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,27; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,28; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS29; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND30; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR31; TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE32; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH33; DAMAGE.343536(declare37 (unit file)38 (uses extras irregex pathname)39 (fixnum)40 (disable-interrupts)41 (foreign-declare #<<EOF42#include <errno.h>4344/* For Windows */45#ifndef R_OK46# define R_OK 247#endif48#ifndef W_OK49# define W_OK 450#endif51#ifndef X_OK52# define X_OK 253#endif5455#if defined(_WIN32) && !defined(__CYGWIN__)56# include <direct.h>57# define C_test_access(fn, m) C_fix(_waccess(C_utf16(fn, 0), C_unfix(m)))5859static C_word C_rename(C_word old, C_word new) {60 wchar_t *s = C_utf16(old, 0), *s2 = C_utf16(new, 1);61 _wremove(s2);62 return(C_fix(_wrename(s, s2)));63}6465# define C_remove(str) C_fix(_wremove(C_utf16(str, 0)))66# define C_rmdir(str) C_fix(_wrmdir(C_utf16(str, 0)))67# define C_mkdir(str) C_fix(_wmkdir(C_utf16(str, 0)))68#else69# include <sys/stat.h>70# define C_test_access(fn, m) C_fix(access(C_c_string(fn), C_unfix(m)))71# define C_rename(old, new) C_fix(rename(C_c_string(old), C_c_string(new)))72# define C_remove(str) C_fix(remove(C_c_string(str)))73# define C_rmdir(str) C_fix(rmdir(C_c_string(str)))74# define C_mkdir(str) C_fix(mkdir(C_c_string(str), S_IRWXU | S_IRWXG | S_IRWXO))75#endif7677#include <sys/types.h>78#include <dirent.h>7980#if defined(_WIN32) && !defined(__CYGWIN__)81# define C_opendir(s,h) C_set_block_item(h, 0, (C_word) _wopendir(C_utf16(s, 0)))8283static C_word C_foundfile(C_word e,C_word b,C_word l) {84 C_char *s = C_utf8(((struct _wdirent *)C_block_item(e, 0))->d_name);85 C_char *p = s;86 while(*p != 0) {87 *p = *p == '\\' ? '/' : *p;88 ++p;89 }90 C_strlcpy(C_c_string(b), s, C_unfix(l));91 return(C_fix(C_strlen(s)));92}93# define C_readdir(h,e) C_set_block_item(e, 0, (C_word) _wreaddir((_WDIR *)C_block_item(h, 0)))94# define C_closedir(h) (_wclosedir((_WDIR *)C_block_item(h, 0)), C_SCHEME_UNDEFINED)95#else96# define C_opendir(s,h) C_set_block_item(h, 0, (C_word) opendir(C_c_string(s)))97# define C_foundfile(e,b,l) (C_strlcpy(C_c_string(b), ((struct dirent *)C_block_item(e, 0))->d_name, C_unfix(l)), C_fix(strlen(((struct dirent *) C_block_item(e, 0))->d_name)))9899# define C_readdir(h,e) C_set_block_item(e, 0, (C_word) readdir((DIR *)C_block_item(h, 0)))100# define C_closedir(h) (closedir((DIR *)C_block_item(h, 0)), C_SCHEME_UNDEFINED)101#endif102103static C_word C_u_i_symbolic_linkp(C_word path)104{105#if !defined(_WIN32) || defined(__CYGWIN__)106 struct stat buf;107 if (lstat(C_c_string(path), &buf) == 0)108 return C_mk_bool(S_ISLNK(buf.st_mode));109#endif110 return C_SCHEME_FALSE;111}112113EOF114))115116(module chicken.file117 (create-directory delete-directory118 create-temporary-file create-temporary-directory119 delete-file delete-file* copy-file move-file rename-file120 file-exists? directory-exists?121 file-readable? file-writable? file-executable?122 directory find-files glob)123124(import scheme125 chicken.base126 chicken.condition127 chicken.fixnum128 chicken.foreign129 chicken.io130 chicken.irregex131 chicken.pathname132 chicken.process-context)133134(include "common-declarations.scm")135136(define-foreign-variable strerror c-string "strerror(errno)")137138;; TODO: Some duplication from POSIX, to give better error messages.139;; This really isn't so much posix-specific, and code like this is140;; also in library.scm. This should be deduplicated across the board.141(define posix-error142 (let ([strerror (foreign-lambda c-string "strerror" int)]143 [string-append string-append] )144 (lambda (type loc msg . args)145 (let ([rn (##sys#update-errno)])146 (apply ##sys#signal-hook/errno147 type rn loc (string-append msg " - " (strerror rn)) args)))))148149150;;; Existence checks:151152(define (file-exists? name)153 (##sys#check-string name 'file-exists?)154 (and (##sys#file-exists? name #f #f 'file-exists?) name))155156(define (directory-exists? name)157 (##sys#check-string name 'directory-exists?)158 (and (##sys#file-exists? name #f #t 'directory-exists?) name))159160161;;; Permissions:162163(define-foreign-variable _r_ok int "R_OK")164(define-foreign-variable _w_ok int "W_OK")165(define-foreign-variable _x_ok int "X_OK")166167(define (test-access filename acc loc)168 (##sys#check-string filename loc)169 (let ((r (##core#inline "C_test_access" (##sys#make-c-string filename loc) acc)))170 (or (fx= r 0)171 (if (fx= (##sys#update-errno) (foreign-value "EACCES" int))172 #f173 (posix-error #:file-error loc "cannot access file" filename)))))174175(define (file-readable? filename) (test-access filename _r_ok 'file-readable?))176(define (file-writable? filename) (test-access filename _w_ok 'file-writable?))177(define (file-executable? filename) (test-access filename _x_ok 'file-executable?))178179180;;; Directories:181182(define (directory #!optional (spec (current-directory)) show-dotfiles?)183 (##sys#check-string spec 'directory)184 (let ((buffer (##sys#make-bytevector 256))185 (handle (##sys#make-pointer))186 (entry (##sys#make-pointer)))187 (##core#inline188 "C_opendir"189 (##sys#make-c-string spec 'directory) handle)190 (if (##sys#null-pointer? handle)191 (posix-error #:file-error 'directory "cannot open directory" spec)192 (let loop ()193 (##core#inline "C_readdir" handle entry)194 (if (##sys#null-pointer? entry)195 (begin (##core#inline "C_closedir" handle) '())196 (let* ((flen (##core#inline "C_foundfile" entry buffer (##sys#size buffer)))197 (file (##sys#buffer->string buffer 0 flen))198 (char1 (string-ref file 0))199 (char2 (and (fx> flen 1) (string-ref file 1))))200 (if (and (eq? #\. char1)201 (or (not char2)202 (and (eq? #\. char2) (eq? 2 flen))203 (not show-dotfiles?)))204 (loop)205 (cons file (loop)))))))))206207(define-inline (*symbolic-link? name loc)208 (##core#inline "C_u_i_symbolic_linkp" (##sys#make-c-string name loc)))209210(define-inline (*create-directory loc name)211 (unless (fx= 0 (##core#inline "C_mkdir" (##sys#make-c-string name loc)))212 (posix-error #:file-error loc "cannot create directory" name)))213214(define create-directory215 (lambda (name #!optional recursive)216 (##sys#check-string name 'create-directory)217 (unless (or (fx= 0 (string-length name))218 (file-exists? name))219 (if recursive220 (let loop ((dir (let-values (((dir file ext) (decompose-pathname name)))221 (if file (make-pathname dir file ext) dir))))222 (when (and dir (not (directory-exists? dir)))223 (loop (pathname-directory dir))224 (*create-directory 'create-directory dir)))225 (*create-directory 'create-directory name)))226 name))227228(define delete-directory229 (lambda (name #!optional recursive)230 (define (rmdir dir)231 (let ((sname (##sys#make-c-string dir)))232 (unless (fx= 0 (##core#inline "C_rmdir" sname))233 (posix-error #:file-error 'delete-directory "cannot delete directory" dir))))234 (##sys#check-string name 'delete-directory)235 (if recursive236 (let ((files (find-files ; relies on `find-files' to list dir-contents before dir237 name238 dotfiles: #t239 follow-symlinks: #f)))240 (for-each241 (lambda (f)242 ((cond ((*symbolic-link? f 'delete-directory) delete-file)243 ((directory-exists? f) rmdir)244 (else delete-file))245 f))246 files)247 (rmdir name))248 (rmdir name))))249250251;;; File management:252253(define (delete-file filename)254 (##sys#check-string filename 'delete-file)255 (unless (eq? 0 (##core#inline "C_remove" (##sys#make-c-string filename 'delete-file)))256 (##sys#signal-hook/errno257 #:file-error (##sys#update-errno) 'delete-file258 (##sys#string-append "cannot delete file - " strerror) filename))259 filename)260261(define (delete-file* file)262 (and (file-exists? file) (delete-file file)))263264(define (rename-file oldfile newfile #!optional (clobber #f))265 (##sys#check-string oldfile 'rename-file)266 (##sys#check-string newfile 'rename-file)267 (when (and (not clobber) (file-exists? newfile))268 (##sys#error 'rename-file "newfile exists but clobber is false" newfile))269 (unless (eq? 0 (##core#inline270 "C_rename"271 (##sys#make-c-string oldfile 'rename-file)272 (##sys#make-c-string newfile 'rename-file)))273 (##sys#signal-hook/errno274 #:file-error (##sys#update-errno) 'rename-file275 (##sys#string-append "cannot rename file - " strerror) oldfile newfile))276 newfile)277278(define (copy-file oldfile newfile #!optional (clobber #f) (blocksize 1024))279 (##sys#check-string oldfile 'copy-file)280 (##sys#check-string newfile 'copy-file)281 (##sys#check-number blocksize 'copy-file)282 (unless (and (integer? blocksize) (> blocksize 0))283 (##sys#error 'copy-file "invalid blocksize - not a positive integer" blocksize))284 (when (directory-exists? oldfile)285 (##sys#error 'copy-file "cannot copy directories" oldfile))286 (when (and (not clobber) (file-exists? newfile))287 (##sys#error 'copy-file "newfile exists but clobber is false" newfile))288 (let* ((i (open-input-file oldfile #:binary))289 (o (open-output-file newfile #:binary))290 (s (##sys#make-bytevector blocksize)))291 (let loop ((d (read-bytevector! s i))292 (l 0))293 (if (fx= 0 d)294 (begin295 (close-input-port i)296 (close-output-port o)297 l)298 (begin299 (write-bytevector s o 0 d)300 (loop (read-bytevector! s i) (fx+ d l)))))))301302(define (move-file oldfile newfile #!optional (clobber #f) (blocksize 1024))303 (##sys#check-string oldfile 'move-file)304 (##sys#check-string newfile 'move-file)305 (##sys#check-number blocksize 'move-file)306 (unless (and (integer? blocksize) (> blocksize 0))307 (##sys#error 'move-file "invalid blocksize - not a positive integer" blocksize))308 (when (directory-exists? oldfile)309 (##sys#error 'move-file "cannot move directories" oldfile))310 (when (and (not clobber) (file-exists? newfile))311 (##sys#error 'move-file "newfile exists but clobber is false" newfile))312 (let* ((i (open-input-file oldfile #:binary))313 (o (open-output-file newfile #:binary))314 (s (##sys#make-bytevector blocksize)))315 (let loop ((d (read-bytevector! s i))316 (l 0))317 (if (fx= 0 d)318 (begin319 (close-input-port i)320 (close-output-port o)321 (delete-file oldfile)322 l)323 (begin324 (write-bytevector s o 0 d)325 (loop (read-bytevector! s i) (fx+ d l)))))))326327328;;; Temporary file creation:329330(define create-temporary-file)331(define create-temporary-directory)332333(let ((temp-prefix "temp")334 (string-append string-append))335 (define (tempdir)336 (or (get-environment-variable "TMPDIR")337 (get-environment-variable "TEMP")338 (get-environment-variable "TMP")339 (if ##sys#windows-platform340 (let ((up (get-environment-variable "USERPROFILE")))341 (if up342 (string-append up "/AppData/Local/Temp")343 "."))344 "/tmp")))345 (set! create-temporary-file346 (lambda (#!optional (ext "tmp"))347 (##sys#check-string ext 'create-temporary-file)348 (let loop ()349 (let* ((n (##core#inline "C_random_fixnum" #x10000))350 (getpid (foreign-lambda int "C_getpid"))351 (pn (make-pathname352 (tempdir)353 (string-append354 temp-prefix355 (number->string n 16)356 "."357 (##sys#number->string (getpid)))358 ext)))359 (if (file-exists? pn)360 (loop)361 (call-with-output-file pn (lambda (p) pn)))))))362 (set! create-temporary-directory363 (lambda ()364 (let loop ()365 (let* ((n (##core#inline "C_random_fixnum" #x10000))366 (getpid (foreign-lambda int "C_getpid"))367 (pn (make-pathname368 (tempdir)369 (string-append370 temp-prefix371 (number->string n 16)372 "."373 (##sys#number->string (getpid))))))374 (if (file-exists? pn)375 (loop)376 (let ((r (##core#inline "C_mkdir" (##sys#make-c-string pn 'create-temporary-directory))))377 (if (eq? r 0)378 pn379 (##sys#signal-hook380 #:file-error 'create-temporary-directory381 (##sys#string-append "cannot create temporary directory - " strerror)382 pn)))))))))383384385;;; Filename globbing:386387(define (glob . paths)388 (let conc-loop ((paths paths))389 (if (null? paths)390 '()391 (let ((path (car paths)))392 (let-values (((dir fil ext) (decompose-pathname path)))393 (let ((dir* (or dir "."))394 (rx (irregex (glob->sre (make-pathname #f (or fil "*") ext)))))395 (let loop ((fns (condition-case (directory dir* #t)396 ((exn i/o file) #f))))397 (cond ((not (pair? fns)) (conc-loop (cdr paths)))398 ((irregex-match rx (car fns)) =>399 (lambda (m)400 (cons (make-pathname dir (irregex-match-substring m))401 (loop (cdr fns)))))402 (else (loop (cdr fns)))))))))))403404405;;; Find matching files:406407(define (find-files dir #!key (test (lambda _ #t))408 (action (lambda (x y) (cons x y)))409 (seed '())410 (limit #f)411 (dotfiles #f)412 (follow-symlinks #f))413 (##sys#check-string dir 'find-files)414 (let* ((depth 0)415 (lproc416 (cond ((not limit) (lambda _ #t))417 ((fixnum? limit) (lambda _ (fx< depth limit)))418 (else limit)))419 (pproc420 (if (procedure? test)421 test422 (let ((test (irregex test))) ; force compilation423 (lambda (x) (irregex-match test x))))))424 (let loop ((dir dir)425 (fs (directory dir dotfiles))426 (r seed))427 (if (null? fs)428 r429 (let* ((filename (##sys#slot fs 0))430 (f (make-pathname dir filename))431 (rest (##sys#slot fs 1)))432 (cond ((directory-exists? f)433 (cond ((member filename '("." "..")) (loop dir rest r))434 ((and (*symbolic-link? f 'find-files) (not follow-symlinks))435 (loop dir rest (if (pproc f) (action f r) r)))436 ((lproc f)437 (loop dir438 rest439 (fluid-let ((depth (fx+ depth 1)))440 (loop f441 (directory f dotfiles)442 (if (pproc f) (action f r) r)))))443 (else (loop dir rest (if (pproc f) (action f r) r)))))444 ((pproc f) (loop dir rest (action f r)))445 (else (loop dir rest r))))))))446447)