| 1 |
;;; |
| 2 |
;;; SRFI-170 - POSIX API |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2021-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 |
(define-module srfi.170 |
| 35 |
(use gauche.fcntl) |
| 36 |
(use gauche.generator) |
| 37 |
(use data.random) |
| 38 |
(use srfi.13) |
| 39 |
(use srfi.19) |
| 40 |
(use file.util) |
| 41 |
(export posix-error? posix-error-name posix-error-message |
| 42 |
|
| 43 |
binary-input textual-input |
| 44 |
binary-output textual-output |
| 45 |
binary-input/output |
| 46 |
buffer-none buffer-block buffer-line |
| 47 |
open/append open/create open/exclusive open/nofollow open/truncate |
| 48 |
open-file fd->port |
| 49 |
|
| 50 |
create-directory |
| 51 |
create-fifo |
| 52 |
create-hard-link |
| 53 |
create-symlink |
| 54 |
read-symlink |
| 55 |
rename-file |
| 56 |
delete-directory |
| 57 |
set-file-mode |
| 58 |
set-file-owner owner/unchanged group/unchanged |
| 59 |
set-file-timespecs time/now time/unchanged |
| 60 |
truncate-file |
| 61 |
|
| 62 |
file-info |
| 63 |
file-info? |
| 64 |
file-info:device |
| 65 |
file-info:inode |
| 66 |
file-info:mode |
| 67 |
file-info:nlinks |
| 68 |
file-info:uid |
| 69 |
file-info:gid |
| 70 |
file-info:rdev |
| 71 |
file-info:size |
| 72 |
file-info:blksize |
| 73 |
file-info:blocks |
| 74 |
file-info:atime |
| 75 |
file-info:mtime |
| 76 |
file-info:ctime |
| 77 |
file-info-directory? |
| 78 |
file-info-fifo? |
| 79 |
file-info-symlink? |
| 80 |
file-info-regular? |
| 81 |
file-info-socket? |
| 82 |
file-info-device? |
| 83 |
|
| 84 |
directory-files |
| 85 |
make-directory-files-generator |
| 86 |
open-directory |
| 87 |
read-directory |
| 88 |
close-directory |
| 89 |
|
| 90 |
real-path |
| 91 |
file-space |
| 92 |
|
| 93 |
temp-file-prefix |
| 94 |
create-temp-file |
| 95 |
call-with-temporary-filename |
| 96 |
|
| 97 |
umask set-umask! |
| 98 |
current-directory set-current-directory! |
| 99 |
pid |
| 100 |
nice |
| 101 |
user-uid user-gid |
| 102 |
user-effective-uid user-effective-gid |
| 103 |
user-supplementary-gids |
| 104 |
|
| 105 |
user-info |
| 106 |
user-info? |
| 107 |
user-info:name |
| 108 |
user-info:uid |
| 109 |
user-info:gid |
| 110 |
user-info:home-dir |
| 111 |
user-info:shell |
| 112 |
user-info:full-name |
| 113 |
user-info:parsed-full-name |
| 114 |
|
| 115 |
group-info |
| 116 |
group-info? |
| 117 |
group-info:name |
| 118 |
group-info:gid |
| 119 |
|
| 120 |
posix-time |
| 121 |
monotonic-time |
| 122 |
|
| 123 |
set-environment-variable! |
| 124 |
delete-environment-variable! |
| 125 |
|
| 126 |
terminal? |
| 127 |
)) |
| 128 |
(select-module srfi.170) |
| 129 |
|
| 130 |
;; Errors |
| 131 |
|
| 132 |
(define (posix-error? obj) (<system-error> obj)) |
| 133 |
|
| 134 |
(define (posix-error-name obj) |
| 135 |
(assume (posix-error? obj)) |
| 136 |
(sys-errno->symbol (condition-ref obj 'errno))) |
| 137 |
|
| 138 |
(define (posix-error-message obj) |
| 139 |
(assume (posix-error? obj)) |
| 140 |
(sys-strerror (condition-ref obj 'errno))) |
| 141 |
|
| 142 |
;; 3.2 I/O |
| 143 |
|
| 144 |
(define-constant binary-input 'binary-input) |
| 145 |
(define-constant binary-output 'binary-output) |
| 146 |
(define-constant textual-input 'textual-input) |
| 147 |
(define-constant textual-output 'textual-output) |
| 148 |
(define-constant binary-input/output 'binary-input/output) |
| 149 |
(define-constant buffer-none 'buffer-none) |
| 150 |
(define-constant buffer-block 'buffer-block) |
| 151 |
(define-constant buffer-line 'buffer-line) |
| 152 |
|
| 153 |
(define-constant open/append O_APPEND) |
| 154 |
(define-constant open/create O_CREAT) |
| 155 |
(define-constant open/exclusive O_EXCL) |
| 156 |
(define-constant open/nofollow (module-binding-ref 'gauche.fcntl 'O_NOFOLLOW 0)) |
| 157 |
(define-constant open/truncate O_TRUNC) |
| 158 |
|
| 159 |
(define (open-file fname port-type flags |
| 160 |
:optional (permission-bits #o666) |
| 161 |
(buffer-mode buffer-block)) |
| 162 |
(define xflags |
| 163 |
(case port-type |
| 164 |
[(binary-input textual-input) (logior flags O_RDONLY)] |
| 165 |
[(binary-output textual-output) (logior flags O_WRONLY)] |
| 166 |
[(binary-input/output) (logior flags O_RDWR)])) |
| 167 |
(fd->port (sys-open fname xflags permission-bits) port-type buffer-mode)) |
| 168 |
|
| 169 |
(define (fd->port fd port-type :optional (buffer-mode buffer-block)) |
| 170 |
(define (%bufmode sym in?) |
| 171 |
(ecase sym |
| 172 |
[(buffer-none) :none] |
| 173 |
[(buffer-block) :full] |
| 174 |
[(buffer-line) (if in? :modest :line)])) |
| 175 |
;; We don't distinguish textual/binary port. |
| 176 |
(ecase port-type |
| 177 |
[(binary-input textual-input) |
| 178 |
(open-input-fd-port fd :buffering (%bufmode buffer-mode #t) :owner? #t)] |
| 179 |
[(binary-output textual-output) |
| 180 |
(open-output-fd-port fd :buffering (%bufmode buffer-mode #f) :owner? #t)] |
| 181 |
[(binary-input/output) |
| 182 |
(error "Bidirectional port is not supported yet.")])) |
| 183 |
|
| 184 |
;; 3.3 File system |
| 185 |
|
| 186 |
(define (create-directory name :optional (perm #o775)) |
| 187 |
(sys-mkdir name perm)) |
| 188 |
(define (create-fifo name :optional (perm #o664)) |
| 189 |
(cond-expand |
| 190 |
[gauche.os.windows (error "create-fifo is not supported on this platform.")] |
| 191 |
[else (sys-mkfifo name perm)])) |
| 192 |
(define (create-hard-link old new) |
| 193 |
(sys-link old new)) |
| 194 |
(define (create-symlink old new) |
| 195 |
(cond-expand |
| 196 |
[gauche.os.windows (error "create-symlink is not supported on this platform")] |
| 197 |
[else (sys-symlink old new)])) |
| 198 |
|
| 199 |
(define (read-symlink name) |
| 200 |
(cond-expand |
| 201 |
[gauche.os.windows (error "read-symlink is not supported on this platform")] |
| 202 |
[else (sys-readlink name)])) |
| 203 |
|
| 204 |
(define (rename-file old new) (sys-rename old new)) |
| 205 |
|
| 206 |
(define (delete-directory name) (sys-rmdir name)) |
| 207 |
|
| 208 |
(define-constant owner/unchanged -1) |
| 209 |
(define-constant group/unchanged -1) |
| 210 |
(define (set-file-owner name uid gid) (sys-chown name uid gid)) |
| 211 |
|
| 212 |
(define-constant time/now 'time/now) |
| 213 |
(define-constant time/unchanged 'time/unchanged) |
| 214 |
(define (set-file-timespecs fname :optional (atime 'time/now) |
| 215 |
(mtime 'time/now)) |
| 216 |
(define-syntax argcheck |
| 217 |
(syntax-rules () |
| 218 |
[(_ x) |
| 219 |
(cond [(eq? x 'time/now) #f] |
| 220 |
[(eq? x 'time/unchanged) #t] |
| 221 |
[else (assume-type x <time>)])])) |
| 222 |
(sys-utime fname (argcheck atime) (argcheck mtime))) |
| 223 |
|
| 224 |
(define (truncate-file fname/port len) |
| 225 |
(assume (or (string? fname/port) (port? fname/port))) |
| 226 |
(cond [(string? fname/port) |
| 227 |
(sys-truncate fname/port len)] |
| 228 |
[(port? fname/port) |
| 229 |
(sys-ftruncate fname/port len)])) |
| 230 |
|
| 231 |
(define (file-info fname/port follow?) |
| 232 |
(assume (or (string? fname/port) (port? fname/port))) |
| 233 |
(if (string? fname/port) |
| 234 |
(if follow? |
| 235 |
(sys-stat fname/port) |
| 236 |
(sys-lstat fname/port)) |
| 237 |
(sys-fstat fname/port))) |
| 238 |
|
| 239 |
(define (file-info? obj) (is-a? obj <sys-stat>)) |
| 240 |
(define (file-info:device stat) |
| 241 |
(assume-type stat <sys-stat>) |
| 242 |
(~ stat'dev)) |
| 243 |
(define (file-info:inode stat) |
| 244 |
(assume-type stat <sys-stat>) |
| 245 |
(~ stat'ino)) |
| 246 |
(define (file-info:mode stat) |
| 247 |
(assume-type stat <sys-stat>) |
| 248 |
(~ stat'mode)) |
| 249 |
(define (file-info:nlinks stat) |
| 250 |
(assume-type stat <sys-stat>) |
| 251 |
(~ stat'nlink)) |
| 252 |
(define (file-info:uid stat) |
| 253 |
(assume-type stat <sys-stat>) |
| 254 |
(~ stat'uid)) |
| 255 |
(define (file-info:gid stat) |
| 256 |
(assume-type stat <sys-stat>) |
| 257 |
(~ stat'gid)) |
| 258 |
(define (file-info:rdev stat) |
| 259 |
(assume-type stat <sys-stat>) |
| 260 |
(~ stat'rdev)) |
| 261 |
(define (file-info:size stat) |
| 262 |
(assume-type stat <sys-stat>) |
| 263 |
(~ stat'size)) |
| 264 |
(define (file-info:blksize stat) |
| 265 |
(assume-type stat <sys-stat>) |
| 266 |
4096) |
| 267 |
(define (file-info:blocks stat) |
| 268 |
(assume-type stat <sys-stat>) |
| 269 |
(quotient (+ (~ stat'size) 511) 512)) |
| 270 |
(define (file-info:atime stat) |
| 271 |
(assume-type stat <sys-stat>) |
| 272 |
(~ stat'atim)) |
| 273 |
(define (file-info:mtime stat) |
| 274 |
(assume-type stat <sys-stat>) |
| 275 |
(~ stat'mtim)) |
| 276 |
(define (file-info:ctime stat) |
| 277 |
(assume-type stat <sys-stat>) |
| 278 |
(~ stat'ctim)) |
| 279 |
|
| 280 |
(define (file-info-directory? stat) |
| 281 |
(assume-type stat <sys-stat>) |
| 282 |
(eq? (~ stat'type) 'directory)) |
| 283 |
(define (file-info-fifo? stat) |
| 284 |
(assume-type stat <sys-stat>) |
| 285 |
(eq? (~ stat'type) 'fifo)) |
| 286 |
(define (file-info-symlink? stat) |
| 287 |
(assume-type stat <sys-stat>) |
| 288 |
(eq? (~ stat'type) 'symlink)) |
| 289 |
(define (file-info-regular? stat) |
| 290 |
(assume-type stat <sys-stat>) |
| 291 |
(eq? (~ stat'type) 'regular)) |
| 292 |
(define (file-info-socket? stat) |
| 293 |
(assume-type stat <sys-stat>) |
| 294 |
(eq? (~ stat'type) 'socket)) |
| 295 |
(define (file-info-device? stat) |
| 296 |
(assume-type stat <sys-stat>) |
| 297 |
(memq (~ stat'type) '(block character))) |
| 298 |
|
| 299 |
(define (set-file-mode name bits) (sys-chmod name bits)) |
| 300 |
|
| 301 |
(define (directory-files dir :optional (dot? #f)) |
| 302 |
(directory-list dir |
| 303 |
:children? #t |
| 304 |
:filter (if dot? #f #/^[^\.]/))) |
| 305 |
|
| 306 |
(define (make-directory-files-generator dir :optional (dot? #f)) |
| 307 |
;; can be more efficient |
| 308 |
(list->generator (directory-files dir dot?))) |
| 309 |
|
| 310 |
;; Gauche don't have a direct interface to opendir etc. |
| 311 |
;; This is just an emulation. |
| 312 |
(define-class <DIR> () |
| 313 |
((gen :init-keyword :gen))) |
| 314 |
|
| 315 |
(define (open-directory dir :optional (dot? #f)) |
| 316 |
(make <DIR> :gen (make-directory-files-generator dir dot?))) |
| 317 |
(define (read-directory dirobj) |
| 318 |
(assume-type dirobj <DIR>) |
| 319 |
((~ dirobj'gen))) |
| 320 |
(define (close-directory dirobj) |
| 321 |
(assume-type dirobj <DIR>) |
| 322 |
(undefined)) |
| 323 |
|
| 324 |
(define (real-path path) (sys-realpath path)) |
| 325 |
|
| 326 |
(define (file-space path-or-port) |
| 327 |
(cond-expand |
| 328 |
[gauche.sys.statvfs |
| 329 |
(let1 statvfs (if (string? path-or-port) |
| 330 |
(sys-statvfs path-or-port) |
| 331 |
(sys-fstatvfs path-or-port)) |
| 332 |
(* (~ statvfs'frsize) |
| 333 |
(~ statvfs'bfree)))] |
| 334 |
[gauche.os.windows |
| 335 |
(use os.windows) |
| 336 |
(let1 path (cond [(string? path-or-port) path-or-port] |
| 337 |
[(and (port? path-or-port) |
| 338 |
(port-file-number path-or-port)) |
| 339 |
(port-name path-or-port)] |
| 340 |
[else |
| 341 |
(error "file-space: Invalid or unsupported path-or-port:" |
| 342 |
path-or-port)]) |
| 343 |
(list-ref (sys-get-disk-free-space-ex path) 2))] |
| 344 |
[else |
| 345 |
(error "file-space isn't supported on this platform yet.")])) |
| 346 |
|
| 347 |
(define temp-file-prefix |
| 348 |
(make-parameter (build-path (temporary-directory) |
| 349 |
(x->string (sys-getpid))))) |
| 350 |
|
| 351 |
(define suffix-generator (strings-of 8 (chars$))) |
| 352 |
|
| 353 |
(define-constant TEMP_RETRY_MAX 256) |
| 354 |
|
| 355 |
(define (call-with-temporary-filename maker |
| 356 |
:optional (prefix (temp-file-prefix))) |
| 357 |
(let loop ([i 0]) |
| 358 |
(let1 f #"~|prefix|~(suffix-generator)" |
| 359 |
(receive (success . rest) (guard (e [else #f]) |
| 360 |
(maker f)) |
| 361 |
(if success |
| 362 |
(apply values success rest) |
| 363 |
(if (>= i TEMP_RETRY_MAX) |
| 364 |
(errorf "Couldn't create temporary file with prefix ~s" prefix) |
| 365 |
(loop (+ i 1)))))))) |
| 366 |
|
| 367 |
(define (create-temp-file :optional (prefix (temp-file-prefix))) |
| 368 |
(receive (port name) (sys-mkstemp prefix) |
| 369 |
(close-port port) |
| 370 |
name)) |
| 371 |
|
| 372 |
;; |
| 373 |
;; 3.5 Process state |
| 374 |
;; |
| 375 |
|
| 376 |
(define (umask) (sys-umask)) |
| 377 |
|
| 378 |
(define (set-umask! umask) (sys-umask umask) (undefined)) |
| 379 |
|
| 380 |
;; srfi.170#current-directory is a subset of file.util#current-directory |
| 381 |
|
| 382 |
(define (set-current-directory! dir) (current-directory dir) (undefined)) |
| 383 |
|
| 384 |
(define (pid) (sys-getpid)) |
| 385 |
|
| 386 |
(define (nice :optional (delta 1)) |
| 387 |
(cond-expand |
| 388 |
[gauche.os.windows (error "nice is not supported on this platform")] |
| 389 |
[else (sys-nice delta)])) |
| 390 |
|
| 391 |
(define (user-uid) (sys-getuid)) |
| 392 |
(define (user-gid) (sys-getgid)) |
| 393 |
(define (user-effective-uid) (sys-geteuid)) |
| 394 |
(define (user-effective-gid) (sys-getegid)) |
| 395 |
(define (user-supplementary-gids) |
| 396 |
(cond-expand |
| 397 |
[gauche.os.windows (list (sys-getgid))] |
| 398 |
[else (sys-getgroups)])) |
| 399 |
|
| 400 |
;; |
| 401 |
;; 3.6 User and group database access |
| 402 |
;; |
| 403 |
|
| 404 |
(define (user-info uid/name) |
| 405 |
(assume (or (exact-integer? uid/name) (string? uid/name))) |
| 406 |
(if (string? uid/name) |
| 407 |
(sys-getpwnam uid/name) |
| 408 |
(sys-getpwuid uid/name))) |
| 409 |
|
| 410 |
(define (user-info? obj) (is-a? obj <sys-passwd>)) |
| 411 |
(define (user-info:name uinfo) |
| 412 |
(assume-type uinfo <sys-passwd>) |
| 413 |
(~ uinfo'name)) |
| 414 |
(define (user-info:uid uinfo) |
| 415 |
(assume-type uinfo <sys-passwd>) |
| 416 |
(~ uinfo'uid)) |
| 417 |
(define (user-info:gid uinfo) |
| 418 |
(assume-type uinfo <sys-passwd>) |
| 419 |
(~ uinfo'gid)) |
| 420 |
(define (user-info:home-dir uinfo) |
| 421 |
(assume-type uinfo <sys-passwd>) |
| 422 |
(~ uinfo'dir)) |
| 423 |
(define (user-info:shell uinfo) |
| 424 |
(assume-type uinfo <sys-passwd>) |
| 425 |
(~ uinfo'shell)) |
| 426 |
(define (user-info:full-name uinfo) |
| 427 |
(assume-type uinfo <sys-passwd>) |
| 428 |
(~ uinfo'gecos)) |
| 429 |
(define (user-info:parsed-full-name uinfo) |
| 430 |
(assume-type uinfo <sys-passwd>) |
| 431 |
;; This cond-expand is based on the SRFI-170 spec |
| 432 |
(cond-expand |
| 433 |
[gauche.os.windows (list (~ uinfo'gecos))] |
| 434 |
[else (let1 fields (string-split (~ uinfo'gecos) ",") |
| 435 |
(if (null? fields) |
| 436 |
fields |
| 437 |
(cons (regexp-replace-all #/&/ (car fields) |
| 438 |
(^_ (string-titlecase (~ uinfo'name)))) |
| 439 |
(cdr fields))))])) |
| 440 |
|
| 441 |
(define (group-info gid/name) |
| 442 |
(assume (or (exact-integer? gid/name) (string? gid/name))) |
| 443 |
(if (string? gid/name) |
| 444 |
(sys-getgrnam gid/name) |
| 445 |
(sys-getgrgid gid/name))) |
| 446 |
(define (group-info? obj) (is-a? obj <sys-group>)) |
| 447 |
(define (group-info:name ginfo) |
| 448 |
(assume-type ginfo <sys-group>) |
| 449 |
(~ ginfo'name)) |
| 450 |
(define (group-info:gid ginfo) |
| 451 |
(assume-type ginfo <sys-group>) |
| 452 |
(~ ginfo'gid)) |
| 453 |
|
| 454 |
;; |
| 455 |
;; 3.10 Time |
| 456 |
;; |
| 457 |
|
| 458 |
(define (posix-time) (current-time)) |
| 459 |
|
| 460 |
(define (monotonic-time) |
| 461 |
(receive (s ns) (sys-clock-gettime-monotonic) |
| 462 |
(make-time 'time-monotonic ns s))) |
| 463 |
|
| 464 |
;; |
| 465 |
;; 3.11 Environment variables |
| 466 |
;; |
| 467 |
|
| 468 |
(define (set-environment-variable! name value) |
| 469 |
(sys-setenv name value #t)) |
| 470 |
(define (delete-environment-variable! name) |
| 471 |
(sys-unsetenv name)) |
| 472 |
|
| 473 |
;; |
| 474 |
;; 3.12 Terminal device control |
| 475 |
;; |
| 476 |
|
| 477 |
(define (terminal? port) (sys-isatty port)) |