;;; ;;; SRFI-170 - POSIX API ;;; ;;; Copyright (c) 2021-2024 Shiro Kawai ;;; ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; ;;; 1. Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; ;;; 2. Redistributions in binary form must reproduce the above copyright ;;; notice, this list of conditions and the following disclaimer in the ;;; documentation and/or other materials provided with the distribution. ;;; ;;; 3. Neither the name of the authors nor the names of its contributors ;;; may be used to endorse or promote products derived from this ;;; software without specific prior written permission. ;;; ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;;; (define-module srfi.170 (use gauche.fcntl) (use gauche.generator) (use data.random) (use srfi.13) (use srfi.19) (use file.util) (export posix-error? posix-error-name posix-error-message binary-input textual-input binary-output textual-output binary-input/output buffer-none buffer-block buffer-line open/append open/create open/exclusive open/nofollow open/truncate open-file fd->port create-directory create-fifo create-hard-link create-symlink read-symlink rename-file delete-directory set-file-mode set-file-owner owner/unchanged group/unchanged set-file-timespecs time/now time/unchanged truncate-file file-info file-info? file-info:device file-info:inode file-info:mode file-info:nlinks file-info:uid file-info:gid file-info:rdev file-info:size file-info:blksize file-info:blocks file-info:atime file-info:mtime file-info:ctime file-info-directory? file-info-fifo? file-info-symlink? file-info-regular? file-info-socket? file-info-device? directory-files make-directory-files-generator open-directory read-directory close-directory real-path file-space temp-file-prefix create-temp-file call-with-temporary-filename umask set-umask! current-directory set-current-directory! pid nice user-uid user-gid user-effective-uid user-effective-gid user-supplementary-gids user-info user-info? user-info:name user-info:uid user-info:gid user-info:home-dir user-info:shell user-info:full-name user-info:parsed-full-name group-info group-info? group-info:name group-info:gid posix-time monotonic-time set-environment-variable! delete-environment-variable! terminal? )) (select-module srfi.170) ;; Errors (define (posix-error? obj) ( obj)) (define (posix-error-name obj) (assume (posix-error? obj)) (sys-errno->symbol (condition-ref obj 'errno))) (define (posix-error-message obj) (assume (posix-error? obj)) (sys-strerror (condition-ref obj 'errno))) ;; 3.2 I/O (define-constant binary-input 'binary-input) (define-constant binary-output 'binary-output) (define-constant textual-input 'textual-input) (define-constant textual-output 'textual-output) (define-constant binary-input/output 'binary-input/output) (define-constant buffer-none 'buffer-none) (define-constant buffer-block 'buffer-block) (define-constant buffer-line 'buffer-line) (define-constant open/append O_APPEND) (define-constant open/create O_CREAT) (define-constant open/exclusive O_EXCL) (define-constant open/nofollow (module-binding-ref 'gauche.fcntl 'O_NOFOLLOW 0)) (define-constant open/truncate O_TRUNC) (define (open-file fname port-type flags :optional (permission-bits #o666) (buffer-mode buffer-block)) (define xflags (case port-type [(binary-input textual-input) (logior flags O_RDONLY)] [(binary-output textual-output) (logior flags O_WRONLY)] [(binary-input/output) (logior flags O_RDWR)])) (fd->port (sys-open fname xflags permission-bits) port-type buffer-mode)) (define (fd->port fd port-type :optional (buffer-mode buffer-block)) (define (%bufmode sym in?) (ecase sym [(buffer-none) :none] [(buffer-block) :full] [(buffer-line) (if in? :modest :line)])) ;; We don't distinguish textual/binary port. (ecase port-type [(binary-input textual-input) (open-input-fd-port fd :buffering (%bufmode buffer-mode #t) :owner? #t)] [(binary-output textual-output) (open-output-fd-port fd :buffering (%bufmode buffer-mode #f) :owner? #t)] [(binary-input/output) (error "Bidirectional port is not supported yet.")])) ;; 3.3 File system (define (create-directory name :optional (perm #o775)) (sys-mkdir name perm)) (define (create-fifo name :optional (perm #o664)) (cond-expand [gauche.os.windows (error "create-fifo is not supported on this platform.")] [else (sys-mkfifo name perm)])) (define (create-hard-link old new) (sys-link old new)) (define (create-symlink old new) (cond-expand [gauche.os.windows (error "create-symlink is not supported on this platform")] [else (sys-symlink old new)])) (define (read-symlink name) (cond-expand [gauche.os.windows (error "read-symlink is not supported on this platform")] [else (sys-readlink name)])) (define (rename-file old new) (sys-rename old new)) (define (delete-directory name) (sys-rmdir name)) (define-constant owner/unchanged -1) (define-constant group/unchanged -1) (define (set-file-owner name uid gid) (sys-chown name uid gid)) (define-constant time/now 'time/now) (define-constant time/unchanged 'time/unchanged) (define (set-file-timespecs fname :optional (atime 'time/now) (mtime 'time/now)) (define-syntax argcheck (syntax-rules () [(_ x) (cond [(eq? x 'time/now) #f] [(eq? x 'time/unchanged) #t] [else (assume-type x