| 1 |
;;;; filesys.test --- test file system functions -*- scheme -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2004, 2006, 2013, 2019, 2021 Free Software Foundation, Inc. |
| 4 |
;;;; Copyright (C) 2021 Maxime Devos <[email protected]> |
| 5 |
;;;; |
| 6 |
;;;; This library is free software; you can redistribute it and/or |
| 7 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 8 |
;;;; License as published by the Free Software Foundation; either |
| 9 |
;;;; version 3 of the License, or (at your option) any later version. |
| 10 |
;;;; |
| 11 |
;;;; This library is distributed in the hope that it will be useful, |
| 12 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
;;;; Lesser General Public License for more details. |
| 15 |
;;;; |
| 16 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;;;; License along with this library; if not, write to the Free Software |
| 18 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
|
| 20 |
(define-module (test-suite test-filesys) |
| 21 |
#:use-module (test-suite lib) |
| 22 |
#:use-module (test-suite guile-test) |
| 23 |
#:use-module (ice-9 threads) |
| 24 |
#:use-module (ice-9 match) |
| 25 |
#:use-module (rnrs io ports) |
| 26 |
#:use-module (rnrs bytevectors)) |
| 27 |
|
| 28 |
(define (test-file) |
| 29 |
(data-file-name "filesys-test.tmp")) |
| 30 |
(define (test-symlink) |
| 31 |
(data-file-name "filesys-test-link.tmp")) |
| 32 |
(define (test-directory) |
| 33 |
(data-file-name "filesys-test-dir.tmp")) |
| 34 |
(define (test-directory2) |
| 35 |
(data-file-name "filesys-test-dir2.tmp")) |
| 36 |
|
| 37 |
(define (skip-on-darwin) |
| 38 |
(when (string-ci=? "darwin" (utsname:sysname (uname))) |
| 39 |
(throw 'untested))) |
| 40 |
|
| 41 |
(define (skip-on-freebsd) |
| 42 |
(when (string-ci=? "FreeBSD" (utsname:sysname (uname))) |
| 43 |
(throw 'untested))) |
| 44 |
|
| 45 |
;;; |
| 46 |
;;; copy-file |
| 47 |
;;; |
| 48 |
|
| 49 |
(with-test-prefix "copy-file" |
| 50 |
|
| 51 |
(with-test-prefix "missing source" |
| 52 |
(with-exception-handler |
| 53 |
(λ (ex) (pass-if-equal "errno is ENOENT" ENOENT (exception-errno ex))) |
| 54 |
(λ () (copy-file "this-file-should-not-exist" "somewhere")) |
| 55 |
#:unwind? #t)) |
| 56 |
|
| 57 |
(call-with-temp-dir |
| 58 |
"copy-file-XXXXXX" |
| 59 |
(λ (tmpdir) |
| 60 |
(let ((msg "this is a test, this is only a test...\n") |
| 61 |
(src (string-append tmpdir "/" "source")) |
| 62 |
(dest (string-append tmpdir "/" "read-only-dest"))) |
| 63 |
(call-with-output-file src (λ (p) (display msg p))) |
| 64 |
(call-with-output-file dest identity) |
| 65 |
(with-test-prefix "successful copy" |
| 66 |
(copy-file src dest) |
| 67 |
(pass-if-equal "copy-file dest content" msg |
| 68 |
(call-with-input-file dest get-string-all))) |
| 69 |
(unless (zero? (geteuid)) |
| 70 |
(with-test-prefix "read only dest" |
| 71 |
(chmod dest #o444) |
| 72 |
(with-exception-handler |
| 73 |
(λ (ex) |
| 74 |
(pass-if-equal "errno is EACCES" EACCES (exception-errno ex))) |
| 75 |
(λ () (copy-file src dest)) |
| 76 |
#:unwind? #t)))))) |
| 77 |
|
| 78 |
;; return next prospective file descriptor number |
| 79 |
(define (next-fd) |
| 80 |
(let ((fd (dup 0))) |
| 81 |
(close fd) |
| 82 |
fd)) |
| 83 |
|
| 84 |
;; in guile 1.6.4 and earlier, copy-file didn't close the input fd when |
| 85 |
;; the output could not be opened |
| 86 |
(pass-if "fd leak when dest unwritable" |
| 87 |
(let ((old-next (next-fd))) |
| 88 |
(false-if-exception (copy-file "/dev/null" "no/such/dir/foo")) |
| 89 |
(= old-next (next-fd))))) |
| 90 |
|
| 91 |
;;; |
| 92 |
;;; lstat |
| 93 |
;;; |
| 94 |
|
| 95 |
(with-test-prefix "lstat" |
| 96 |
|
| 97 |
(pass-if "normal file" |
| 98 |
(call-with-output-file (test-file) |
| 99 |
(lambda (port) |
| 100 |
(display "hello" port))) |
| 101 |
(eqv? 5 (stat:size (lstat (test-file))))) |
| 102 |
|
| 103 |
(call-with-output-file (test-file) |
| 104 |
(lambda (port) |
| 105 |
(display "hello" port))) |
| 106 |
(false-if-exception (delete-file (test-symlink))) |
| 107 |
(if (not (false-if-exception |
| 108 |
(begin (symlink (test-file) (test-symlink)) #t))) |
| 109 |
(display "cannot create symlink, lstat test skipped\n") |
| 110 |
(pass-if "symlink" |
| 111 |
;; not much to test, except that it works |
| 112 |
(->bool (lstat (test-symlink)))))) |
| 113 |
|
| 114 |
;;; |
| 115 |
;;; opendir and friends |
| 116 |
;;; |
| 117 |
|
| 118 |
(with-test-prefix "opendir" |
| 119 |
|
| 120 |
(with-test-prefix "root directory" |
| 121 |
(let ((d (opendir "/"))) |
| 122 |
(pass-if "not empty" |
| 123 |
(string? (readdir d))) |
| 124 |
(pass-if "all entries are strings" |
| 125 |
(let more () |
| 126 |
(let ((f (readdir d))) |
| 127 |
(cond ((string? f) |
| 128 |
(more)) |
| 129 |
((eof-object? f) |
| 130 |
#t) |
| 131 |
(else |
| 132 |
#f))))) |
| 133 |
(closedir d)))) |
| 134 |
|
| 135 |
;;; |
| 136 |
;;; stat |
| 137 |
;;; |
| 138 |
|
| 139 |
(with-test-prefix "stat" |
| 140 |
|
| 141 |
(with-test-prefix "filename" |
| 142 |
|
| 143 |
(pass-if "size" |
| 144 |
(call-with-output-file (test-file) |
| 145 |
(lambda (port) |
| 146 |
(display "hello" port))) |
| 147 |
(eqv? 5 (stat:size (stat (test-file)))))) |
| 148 |
|
| 149 |
(with-test-prefix "file descriptor" |
| 150 |
|
| 151 |
(pass-if "size" |
| 152 |
(call-with-output-file (test-file) |
| 153 |
(lambda (port) |
| 154 |
(display "hello" port))) |
| 155 |
(let* ((fd (open-fdes (test-file) O_RDONLY)) |
| 156 |
(st (stat fd))) |
| 157 |
(close-fdes fd) |
| 158 |
(eqv? 5 (stat:size st))))) |
| 159 |
|
| 160 |
(with-test-prefix "port" |
| 161 |
|
| 162 |
(pass-if "size" |
| 163 |
(call-with-output-file (test-file) |
| 164 |
(lambda (port) |
| 165 |
(display "hello" port))) |
| 166 |
(let* ((port (open-file (test-file) "r+")) |
| 167 |
(st (stat port))) |
| 168 |
(close-port port) |
| 169 |
(eqv? 5 (stat:size st)))))) |
| 170 |
|
| 171 |
(with-test-prefix "statat" |
| 172 |
;; file-exists? from (ice-9 boot) dereferences symbolic links |
| 173 |
;; (a bug?). |
| 174 |
(define (file-exists? filename) |
| 175 |
(catch 'system-error |
| 176 |
(lambda () (lstat filename) #t) |
| 177 |
(lambda args |
| 178 |
(if (= (system-error-errno args) ENOENT) |
| 179 |
;; For the purposes of the following tests, |
| 180 |
;; it is safe to ignore errors like EPERM, but a correct |
| 181 |
;; implementation would return #t for that error. |
| 182 |
#f |
| 183 |
(apply throw args))))) |
| 184 |
(define (maybe-delete-directory) |
| 185 |
(when (file-exists? (test-directory)) |
| 186 |
(for-each |
| 187 |
(lambda (filename) |
| 188 |
(define full-name (in-vicinity (test-directory) filename)) |
| 189 |
(when (file-exists? full-name) |
| 190 |
(delete-file full-name))) |
| 191 |
'("test-file" "test-symlink")) |
| 192 |
(rmdir (test-directory)))) |
| 193 |
(define (skip-unless-defined . things) |
| 194 |
(for-each (lambda (thing) |
| 195 |
(unless (defined? thing) |
| 196 |
(throw 'unsupported))) |
| 197 |
things)) |
| 198 |
(maybe-delete-directory) |
| 199 |
(mkdir (test-directory)) |
| 200 |
(call-with-output-file (in-vicinity (test-directory) "test-file") |
| 201 |
(lambda (port) |
| 202 |
(display "hello" port))) |
| 203 |
|
| 204 |
;; Return #true if the symlink was created, #false otherwise. |
| 205 |
(define (maybe-create-symlink) |
| 206 |
(if (file-exists? (in-vicinity (test-directory) "test-symlink")) |
| 207 |
#t |
| 208 |
(false-if-exception |
| 209 |
(symlink "test-file" |
| 210 |
(in-vicinity (test-directory) "test-symlink"))))) |
| 211 |
|
| 212 |
(pass-if-equal "regular file" 5 |
| 213 |
(skip-unless-defined 'statat) |
| 214 |
(call-with-port |
| 215 |
(open (test-directory) O_RDONLY) |
| 216 |
(lambda (port) |
| 217 |
(stat:size (statat port "test-file"))))) |
| 218 |
|
| 219 |
(pass-if-equal "regular file, AT_SYMLINK_NOFOLLOW" 5 |
| 220 |
(skip-unless-defined 'statat 'AT_SYMLINK_NOFOLLOW) |
| 221 |
(call-with-port |
| 222 |
(open (test-directory) O_RDONLY) |
| 223 |
(lambda (port) |
| 224 |
(stat:size (statat port "test-file" AT_SYMLINK_NOFOLLOW))))) |
| 225 |
|
| 226 |
(pass-if-equal "symbolic links are dereferenced" '(regular 5) |
| 227 |
;; Not all systems support symlinks. |
| 228 |
(skip-unless-defined 'statat 'symlink) |
| 229 |
(unless (maybe-create-symlink) |
| 230 |
(throw 'unresolved)) |
| 231 |
(call-with-port |
| 232 |
(open (test-directory) O_RDONLY) |
| 233 |
(lambda (port) |
| 234 |
(define result (statat port "test-symlink")) |
| 235 |
(list (stat:type result) (stat:size result))))) |
| 236 |
|
| 237 |
(pass-if-equal "symbolic links are not dereferenced" |
| 238 |
`(symlink ,(string-length "test-file")) |
| 239 |
;; Not all systems support symlinks. |
| 240 |
(skip-unless-defined 'statat 'symlink) |
| 241 |
(unless (maybe-create-symlink) |
| 242 |
(throw 'unresolved)) |
| 243 |
(call-with-port |
| 244 |
(open (test-directory) O_RDONLY) |
| 245 |
(lambda (port) |
| 246 |
(define result (statat port "test-symlink" AT_SYMLINK_NOFOLLOW)) |
| 247 |
(list (stat:type result) (stat:size result))))) |
| 248 |
|
| 249 |
(maybe-delete-directory)) |
| 250 |
|
| 251 |
(with-test-prefix "sendfile" |
| 252 |
|
| 253 |
(let* ((file (search-path %load-path "ice-9/boot-9.scm")) |
| 254 |
(len (stat:size (stat file))) |
| 255 |
(ref (call-with-input-file file get-bytevector-all))) |
| 256 |
|
| 257 |
(pass-if-equal "file" (cons len ref) |
| 258 |
(let* ((result (call-with-input-file file |
| 259 |
(lambda (input) |
| 260 |
(call-with-output-file (test-file) |
| 261 |
(lambda (output) |
| 262 |
(sendfile output input len 0)))))) |
| 263 |
(out (call-with-input-file (test-file) get-bytevector-all))) |
| 264 |
(cons result out))) |
| 265 |
|
| 266 |
(pass-if-equal "file with offset" |
| 267 |
(cons (- len 777) (call-with-input-file file |
| 268 |
(lambda (input) |
| 269 |
(seek input 777 SEEK_SET) |
| 270 |
(get-bytevector-all input)))) |
| 271 |
(let* ((result (call-with-input-file file |
| 272 |
(lambda (input) |
| 273 |
(call-with-output-file (test-file) |
| 274 |
(lambda (output) |
| 275 |
(sendfile output input (- len 777) 777)))))) |
| 276 |
(out (call-with-input-file (test-file) get-bytevector-all))) |
| 277 |
(cons result out))) |
| 278 |
|
| 279 |
(pass-if-equal "file with offset past the end" |
| 280 |
(cons (- len 777) (call-with-input-file file |
| 281 |
(lambda (input) |
| 282 |
(seek input 777 SEEK_SET) |
| 283 |
(get-bytevector-all input)))) |
| 284 |
(let* ((result (call-with-input-file file |
| 285 |
(lambda (input) |
| 286 |
(call-with-output-file (test-file) |
| 287 |
(lambda (output) |
| 288 |
(sendfile output input len 777)))))) |
| 289 |
(out (call-with-input-file (test-file) get-bytevector-all))) |
| 290 |
(cons result out))) |
| 291 |
|
| 292 |
(pass-if-equal "file with offset near the end" |
| 293 |
(cons 77 (call-with-input-file file |
| 294 |
(lambda (input) |
| 295 |
(seek input (- len 77) SEEK_SET) |
| 296 |
(get-bytevector-all input)))) |
| 297 |
(let* ((result (call-with-input-file file |
| 298 |
(lambda (input) |
| 299 |
(call-with-output-file (test-file) |
| 300 |
(lambda (output) |
| 301 |
(sendfile output input len (- len 77))))))) |
| 302 |
(out (call-with-input-file (test-file) get-bytevector-all))) |
| 303 |
(cons result out))) |
| 304 |
|
| 305 |
(pass-if-equal "pipe" (cons len ref) |
| 306 |
(if (provided? 'threads) |
| 307 |
(let* ((in+out (pipe)) |
| 308 |
(child (call-with-new-thread |
| 309 |
(lambda () |
| 310 |
(call-with-input-file file |
| 311 |
(lambda (input) |
| 312 |
(let ((result (sendfile (cdr in+out) |
| 313 |
(fileno input) |
| 314 |
len 0))) |
| 315 |
(close-port (cdr in+out)) |
| 316 |
result))))))) |
| 317 |
(let ((out (get-bytevector-all (car in+out)))) |
| 318 |
(close-port (car in+out)) |
| 319 |
(cons (join-thread child) out))) |
| 320 |
(throw 'unresolved))) |
| 321 |
|
| 322 |
(pass-if-equal "pipe with offset" |
| 323 |
(cons (- len 777) (call-with-input-file file |
| 324 |
(lambda (input) |
| 325 |
(seek input 777 SEEK_SET) |
| 326 |
(get-bytevector-all input)))) |
| 327 |
(if (provided? 'threads) |
| 328 |
(let* ((in+out (pipe)) |
| 329 |
(child (call-with-new-thread |
| 330 |
(lambda () |
| 331 |
(call-with-input-file file |
| 332 |
(lambda (input) |
| 333 |
(let ((result (sendfile (cdr in+out) |
| 334 |
(fileno input) |
| 335 |
(- len 777) |
| 336 |
777))) |
| 337 |
(close-port (cdr in+out)) |
| 338 |
result))))))) |
| 339 |
(let ((out (get-bytevector-all (car in+out)))) |
| 340 |
(close-port (car in+out)) |
| 341 |
(cons (join-thread child) out))) |
| 342 |
(throw 'unresolved))))) |
| 343 |
|
| 344 |
(with-test-prefix "basename" |
| 345 |
|
| 346 |
(pass-if-equal "/" "/" (basename "/")) |
| 347 |
(pass-if-equal "//" "/" (basename "//")) |
| 348 |
(pass-if-equal "a/b/c" "c" (basename "a/b/c")) |
| 349 |
|
| 350 |
(pass-if-equal "a.b" (basename "a.b" "a.b")) |
| 351 |
(pass-if-equal "a.b" (basename "/a.b" "a.b")) |
| 352 |
(pass-if-equal "a" (basename "a.b" ".b")) |
| 353 |
(pass-if-equal "a" (basename "/a.b" ".b")) |
| 354 |
|
| 355 |
;; https://debbugs.gnu.org/69437 |
| 356 |
(pass-if-equal "bar" (basename "foo/bar" "o/bar"))) |
| 357 |
|
| 358 |
(delete-file (test-file)) |
| 359 |
(when (file-exists? (test-symlink)) |
| 360 |
(delete-file (test-symlink))) |
| 361 |
|
| 362 |
|
| 363 |
(with-test-prefix "mkdtemp" |
| 364 |
|
| 365 |
(pass-if-exception "number arg" exception:wrong-type-arg |
| 366 |
(if (not (defined? 'mkdtemp)) |
| 367 |
(throw 'unresolved) |
| 368 |
(mkdtemp 123))) |
| 369 |
|
| 370 |
(pass-if "template prefix is preserved" |
| 371 |
(if (not (defined? 'mkdtemp)) |
| 372 |
(throw 'unresolved) |
| 373 |
(let* ((template "T-XXXXXX") |
| 374 |
(name (mkdtemp template))) |
| 375 |
(false-if-exception (rmdir name)) |
| 376 |
(and |
| 377 |
(string? name) |
| 378 |
(string-contains name "T-") |
| 379 |
(= (string-length name) 8))))) |
| 380 |
|
| 381 |
(pass-if-exception "invalid template" exception:system-error |
| 382 |
;; MacOS accepts any template and just does no replacements. |
| 383 |
(skip-on-darwin) |
| 384 |
(skip-on-freebsd) |
| 385 |
(if (not (defined? 'mkdtemp)) |
| 386 |
(throw 'unresolved) |
| 387 |
(mkdtemp "T-AAAAAA"))) |
| 388 |
|
| 389 |
(pass-if "directory created" |
| 390 |
(if (not (defined? 'mkdtemp)) |
| 391 |
(throw 'unresolved) |
| 392 |
(let* ((template "T-XXXXXX") |
| 393 |
(name (mkdtemp template))) |
| 394 |
(let* ((_stat (stat name)) |
| 395 |
(result (eqv? 'directory (stat:type _stat)))) |
| 396 |
(false-if-exception (rmdir name)) |
| 397 |
result))))) |
| 398 |
|
| 399 |
;;; |
| 400 |
;;; chmodat |
| 401 |
;;; |
| 402 |
|
| 403 |
(with-test-prefix "chmodat" |
| 404 |
(call-with-output-file (test-file) (const #f)) |
| 405 |
(chmod (test-file) #o000) |
| 406 |
|
| 407 |
(pass-if-equal "regular file" |
| 408 |
#o300 |
| 409 |
(unless (defined? 'chmodat) |
| 410 |
(throw 'unsupported)) |
| 411 |
(call-with-port |
| 412 |
(open (dirname (test-file)) O_RDONLY) |
| 413 |
(lambda (port) |
| 414 |
(chmodat port (test-file) #o300))) |
| 415 |
(stat:perms (stat (test-file)))) |
| 416 |
|
| 417 |
(chmod (test-file) #o000) |
| 418 |
|
| 419 |
(pass-if-equal "regular file, AT_SYMLINK_NOFOLLOW" |
| 420 |
#o300 |
| 421 |
(unless (and (defined? 'chmodat) |
| 422 |
(defined? 'AT_SYMLINK_NOFOLLOW)) |
| 423 |
(throw 'unsupported)) |
| 424 |
(call-with-port |
| 425 |
(open (dirname (test-file)) O_RDONLY) |
| 426 |
(lambda (port) |
| 427 |
(catch 'system-error |
| 428 |
(lambda () |
| 429 |
(chmodat port (basename (test-file)) #o300 AT_SYMLINK_NOFOLLOW)) |
| 430 |
(lambda args |
| 431 |
(close-port port) |
| 432 |
;; AT_SYMLINK_NOFOLLOW is not supported on Linux (at least Linux |
| 433 |
;; 5.11.2 with the btrfs file system), even for regular files. |
| 434 |
(if (= ENOTSUP (system-error-errno args)) |
| 435 |
(begin |
| 436 |
(display "fchmodat doesn't support AT_SYMLINK_NOFOLLOW\n") |
| 437 |
(throw 'unresolved)) |
| 438 |
(apply throw args)))))) |
| 439 |
(stat:perms (stat (test-file)))) |
| 440 |
|
| 441 |
(pass-if-exception "not a port" exception:wrong-type-arg |
| 442 |
(chmodat "bogus" (test-file) #o300)) |
| 443 |
|
| 444 |
(pass-if-exception "not a file port" exception:wrong-type-arg |
| 445 |
(chmodat (open-input-string "") (test-file) #o300)) |
| 446 |
|
| 447 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 448 |
(chmodat (call-with-port (open "." O_RDONLY) identity) (test-file) #o300)) |
| 449 |
|
| 450 |
(delete-file (test-file))) |
| 451 |
|
| 452 |
(with-test-prefix "chdir" |
| 453 |
(pass-if-equal "current directory" (getcwd) |
| 454 |
(begin (chdir ".") (getcwd))) |
| 455 |
(define file (search-path %load-path "ice-9/boot-9.scm")) |
| 456 |
|
| 457 |
|
| 458 |
;; canonicalize-path is used to not stumble over differences due to links |
| 459 |
(pass-if-equal "test directory" (canonicalize-path (dirname file)) |
| 460 |
(let ((olddir (getcwd)) |
| 461 |
(dir #f)) |
| 462 |
(chdir (canonicalize-path (dirname file))) |
| 463 |
(set! dir (getcwd)) |
| 464 |
(chdir olddir) |
| 465 |
dir)) |
| 466 |
|
| 467 |
(pass-if-equal "test directory, via port" (canonicalize-path (dirname file)) |
| 468 |
(unless (provided? 'chdir-port) |
| 469 |
(throw 'unresolved)) |
| 470 |
(let ((olddir (getcwd)) |
| 471 |
(port (open (canonicalize-path (dirname file)) O_RDONLY)) |
| 472 |
(dir #f)) |
| 473 |
(chdir port) |
| 474 |
(set! dir (getcwd)) |
| 475 |
(chdir olddir) |
| 476 |
dir)) |
| 477 |
|
| 478 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 479 |
(unless (provided? 'chdir-port) |
| 480 |
(throw 'unresolved)) |
| 481 |
(let ((port (open (dirname file) O_RDONLY)) |
| 482 |
(olddir (getcwd))) |
| 483 |
(close-port port) |
| 484 |
(chdir port) |
| 485 |
(chdir olddir))) ; should not be reached |
| 486 |
|
| 487 |
(pass-if-exception "not a port or file name" exception:wrong-type-arg |
| 488 |
(chdir '(stuff))) |
| 489 |
|
| 490 |
(pass-if-exception "non-file port" exception:wrong-type-arg |
| 491 |
(chdir (open-input-string "")))) |
| 492 |
|
| 493 |
(with-test-prefix "readlink" |
| 494 |
(false-if-exception (delete-file (test-symlink))) |
| 495 |
(false-if-exception (delete-file (test-file))) |
| 496 |
(call-with-output-file (test-file) |
| 497 |
(lambda (port) |
| 498 |
(display "hello" port))) |
| 499 |
(if (not (false-if-exception |
| 500 |
(begin (symlink (test-file) (test-symlink)) #t))) |
| 501 |
(display "cannot create symlink, some readlink tests skipped\n") |
| 502 |
(let () |
| 503 |
(pass-if-equal "file name of symlink" (test-file) |
| 504 |
(readlink (test-symlink))) |
| 505 |
|
| 506 |
(pass-if-equal "port representing a symlink" (test-file) |
| 507 |
(let () |
| 508 |
(unless (and (provided? 'readlink-port) |
| 509 |
(defined? 'O_NOFOLLOW) |
| 510 |
(defined? 'O_PATH) |
| 511 |
(not (= 0 O_NOFOLLOW)) |
| 512 |
(not (= 0 O_PATH))) |
| 513 |
(throw 'unsupported)) |
| 514 |
(define port (open (test-symlink) (logior O_NOFOLLOW O_PATH))) |
| 515 |
(define points-to (false-if-exception (readlink port))) |
| 516 |
(close-port port) |
| 517 |
points-to)) |
| 518 |
|
| 519 |
(pass-if-exception "not a port or file name" exception:wrong-type-arg |
| 520 |
(readlink '(stuff))))) |
| 521 |
|
| 522 |
(pass-if-equal "port representing a regular file" EINVAL |
| 523 |
(call-with-input-file (test-file) |
| 524 |
(lambda (port) |
| 525 |
(unless (provided? 'readlink-port) |
| 526 |
(throw 'unsupported)) |
| 527 |
(catch 'system-error |
| 528 |
(lambda () |
| 529 |
(readlink port) |
| 530 |
(close-port port) ; should be unreachable |
| 531 |
#f) |
| 532 |
(lambda args |
| 533 |
(close-port port) |
| 534 |
;; At least Linux 5.10.46 returns ENOENT instead of EINVAL. |
| 535 |
;; Possibly surprising, but it is documented in some man |
| 536 |
;; pages and it doesn't appear to be an accident: |
| 537 |
;; <https://elixir.bootlin.com/linux/v5.10.46/source/fs/stat.c#L419>. |
| 538 |
(define error (system-error-errno args)) |
| 539 |
(if (= error ENOENT) |
| 540 |
EINVAL |
| 541 |
error)))))) |
| 542 |
|
| 543 |
(pass-if-exception "non-file port" exception:wrong-type-arg |
| 544 |
(readlink (open-input-string ""))) |
| 545 |
|
| 546 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 547 |
(let ((port (open-file (test-file) "r"))) |
| 548 |
(close-port port) |
| 549 |
(readlink port))) |
| 550 |
|
| 551 |
(false-if-exception (delete-file (test-symlink))) |
| 552 |
(false-if-exception (delete-file (test-file)))) |
| 553 |
|
| 554 |
(with-test-prefix "symlinkat" |
| 555 |
(pass-if-equal "create" (test-file) |
| 556 |
(unless (defined? 'symlinkat) |
| 557 |
(throw 'unsupported)) |
| 558 |
(call-with-port |
| 559 |
(open "." O_RDONLY) |
| 560 |
(lambda (port) |
| 561 |
(symlinkat port (test-file) (test-symlink)) |
| 562 |
(readlink (test-symlink))))) |
| 563 |
(false-if-exception (delete-file (test-symlink))) |
| 564 |
|
| 565 |
(pass-if-exception "not a port" exception:wrong-type-arg |
| 566 |
(unless (defined? 'symlinkat) |
| 567 |
(throw 'unsupported)) |
| 568 |
(symlinkat "bogus" (test-file) (test-symlink))) |
| 569 |
|
| 570 |
(pass-if-exception "not a file port" exception:wrong-type-arg |
| 571 |
(unless (defined? 'symlinkat) |
| 572 |
(throw 'unsupported)) |
| 573 |
(symlinkat (open-input-string "") (test-file) (test-symlink))) |
| 574 |
|
| 575 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 576 |
(unless (defined? 'symlinkat) |
| 577 |
(throw 'unsupported)) |
| 578 |
(symlinkat (call-with-port (open "." O_RDONLY) identity) |
| 579 |
(test-file) (test-symlink)))) |
| 580 |
|
| 581 |
(with-test-prefix "mkdirat" |
| 582 |
(define (skip-if-unsupported) |
| 583 |
(unless (defined? 'mkdirat) |
| 584 |
(throw 'unsupported))) |
| 585 |
(define (maybe-delete-directory) |
| 586 |
(when (file-exists? (test-directory)) |
| 587 |
(rmdir (test-directory)))) |
| 588 |
(maybe-delete-directory) |
| 589 |
|
| 590 |
(pass-if-equal "create" 'directory |
| 591 |
(skip-if-unsupported) |
| 592 |
(call-with-port |
| 593 |
(open "." O_RDONLY) |
| 594 |
(lambda (port) |
| 595 |
(mkdirat port (test-directory)) |
| 596 |
(stat:type (stat (test-directory)))))) |
| 597 |
(maybe-delete-directory) |
| 598 |
|
| 599 |
(pass-if-equal "explicit perms" (logand #o111 (lognot (umask))) |
| 600 |
(skip-if-unsupported) |
| 601 |
(call-with-port |
| 602 |
(open "." O_RDONLY) |
| 603 |
(lambda (port) |
| 604 |
(mkdirat port (test-directory) #o111) |
| 605 |
(stat:perms (stat (test-directory)))))) |
| 606 |
(maybe-delete-directory) |
| 607 |
|
| 608 |
(pass-if-equal "create, implicit perms" (logand #o777 (lognot (umask))) |
| 609 |
(skip-if-unsupported) |
| 610 |
(call-with-port |
| 611 |
(open "." O_RDONLY) |
| 612 |
(lambda (port) |
| 613 |
(mkdirat port (test-directory)) |
| 614 |
(stat:perms (stat (test-directory)))))) |
| 615 |
(maybe-delete-directory)) |
| 616 |
|
| 617 |
(with-test-prefix "rename-file-at" |
| 618 |
(define (skip-if-unsupported) |
| 619 |
(unless (defined? 'rename-file-at) |
| 620 |
(throw 'unsupported))) |
| 621 |
(pass-if-equal "current working directory" '(#f "hello") |
| 622 |
(skip-if-unsupported) |
| 623 |
;; Create a file in the test directory |
| 624 |
(call-with-output-file "filesys-test-a.tmp" |
| 625 |
(lambda (port) (display "hello" port))) |
| 626 |
;; Try to rename it |
| 627 |
(rename-file-at #f "filesys-test-a.tmp" #f "filesys-test-b.tmp") |
| 628 |
;; Verify it exists under the new name, and not under the old name |
| 629 |
(list (file-exists? "filesys-test-a.tmp") |
| 630 |
(call-with-input-file "filesys-test-b.tmp" get-string-all))) |
| 631 |
|
| 632 |
(false-if-exception (delete-file "filesys-test-a.tmp")) |
| 633 |
(false-if-exception (delete-file "filesys-test-b.tmp")) |
| 634 |
|
| 635 |
(pass-if-equal "two ports" '(#f "hello") |
| 636 |
(skip-if-unsupported) |
| 637 |
(mkdir (test-directory)) |
| 638 |
(mkdir (test-directory2)) |
| 639 |
;; Create a file in the first directory |
| 640 |
(call-with-output-file (in-vicinity (test-directory) "a") |
| 641 |
(lambda (port) (display "hello" port))) |
| 642 |
(let ((port1 (open (test-directory) O_RDONLY)) |
| 643 |
(port2 (open (test-directory2) O_RDONLY))) |
| 644 |
;; Try to rename it |
| 645 |
(rename-file-at port1 "a" port2 "b") |
| 646 |
(close-port port1) |
| 647 |
(close-port port2) |
| 648 |
;; Verify it exists under the new name, and not under the old name |
| 649 |
(list (file-exists? (in-vicinity (test-directory) "a")) |
| 650 |
(call-with-input-file (in-vicinity (test-directory2) "b") |
| 651 |
get-string-all)))) |
| 652 |
(false-if-exception (delete-file (in-vicinity (test-directory) "a"))) |
| 653 |
(false-if-exception (delete-file (in-vicinity (test-directory2) "b"))) |
| 654 |
(false-if-exception (rmdir (test-directory))) |
| 655 |
(false-if-exception (rmdir (test-directory2))) |
| 656 |
|
| 657 |
(pass-if-equal "port and current working directory" '(#f "hello") |
| 658 |
(skip-if-unsupported) |
| 659 |
(mkdir (test-directory)) |
| 660 |
;; Create a file in (test-directory) |
| 661 |
(call-with-output-file (in-vicinity (test-directory) "a") |
| 662 |
(lambda (port) (display "hello" port))) |
| 663 |
(let ((port (open (test-directory) O_RDONLY))) |
| 664 |
;; Try to rename it |
| 665 |
(rename-file-at port "a" #f (basename (test-file))) |
| 666 |
(close-port port) |
| 667 |
;; Verify it exists under the new name, and not under the old name. |
| 668 |
(list (file-exists? (in-vicinity (test-directory) "a")) |
| 669 |
(call-with-input-file (test-file) get-string-all)))) |
| 670 |
(false-if-exception (delete-file (in-vicinity (test-directory) "a"))) |
| 671 |
(false-if-exception (rmdir (test-directory))) |
| 672 |
(false-if-exception (delete-file (test-file))) |
| 673 |
|
| 674 |
(pass-if-equal "current working directory and port" '(#f "hello") |
| 675 |
(skip-if-unsupported) |
| 676 |
(mkdir (test-directory)) |
| 677 |
;; Create a file in the working directory |
| 678 |
(call-with-output-file (test-file) |
| 679 |
(lambda (port) (display "hello" port))) |
| 680 |
(let ((port (open (test-directory) O_RDONLY))) |
| 681 |
;; Try to rename it |
| 682 |
(rename-file-at #f (basename (test-file)) port "b") |
| 683 |
(close-port port) |
| 684 |
;; Verify it exists under the new name, and not under the old name. |
| 685 |
(list (file-exists? (test-file)) |
| 686 |
(call-with-input-file (in-vicinity (test-directory) "b") |
| 687 |
get-string-all)))) |
| 688 |
|
| 689 |
(false-if-exception (delete-file (in-vicinity (test-directory) "b"))) |
| 690 |
(false-if-exception (delete-file (test-file))) |
| 691 |
(false-if-exception (rmdir (test-directory))) |
| 692 |
|
| 693 |
(pass-if-exception "not a file port (1)" exception:wrong-type-arg |
| 694 |
(skip-if-unsupported) |
| 695 |
(rename-file-at (open-input-string "") "some" #f "thing")) |
| 696 |
|
| 697 |
(pass-if-exception "not a file port (2)" exception:wrong-type-arg |
| 698 |
(skip-if-unsupported) |
| 699 |
(rename-file-at #f "some" (open-input-string "") "thing")) |
| 700 |
|
| 701 |
(pass-if-exception "closed port (1)" exception:wrong-type-arg |
| 702 |
(skip-if-unsupported) |
| 703 |
(rename-file-at (call-with-port (open "." O_RDONLY) identity) |
| 704 |
"some" #f "thing")) |
| 705 |
|
| 706 |
(pass-if-exception "closed port (2)" exception:wrong-type-arg |
| 707 |
(skip-if-unsupported) |
| 708 |
(rename-file-at #f "some" (call-with-port (open "." O_RDONLY) identity) |
| 709 |
"thing")) |
| 710 |
|
| 711 |
(pass-if-exception "not a string (1)" exception:wrong-type-arg |
| 712 |
(skip-if-unsupported) |
| 713 |
(rename-file-at #f 'what #f "thing")) |
| 714 |
|
| 715 |
(pass-if-exception "not a string (2)" exception:wrong-type-arg |
| 716 |
(skip-if-unsupported) |
| 717 |
(rename-file-at #f "some" #f 'what))) |
| 718 |
|
| 719 |
(with-test-prefix "delete-file-at" |
| 720 |
(define (skip-if-unsupported) |
| 721 |
(when (not (and (defined? 'delete-file-at) |
| 722 |
(defined? 'AT_REMOVEDIR))) |
| 723 |
(throw 'unsupported))) |
| 724 |
(define (create-test-file) |
| 725 |
(call-with-output-file (test-file) identity)) |
| 726 |
(define (create-test-directory) |
| 727 |
(mkdir (test-directory))) |
| 728 |
(define (delete-test-file) |
| 729 |
(when (file-exists? (test-file)) |
| 730 |
(delete-file (test-file)))) |
| 731 |
(define (delete-test-directory) |
| 732 |
(when (file-exists? (test-directory)) |
| 733 |
(rmdir (test-directory)))) |
| 734 |
|
| 735 |
(pass-if-equal "regular file" #f |
| 736 |
(skip-if-unsupported) |
| 737 |
(create-test-file) |
| 738 |
(call-with-port |
| 739 |
(open (dirname (test-file)) O_RDONLY) |
| 740 |
(lambda (port) |
| 741 |
(delete-file-at port (basename (test-file))))) |
| 742 |
(file-exists? (test-file))) |
| 743 |
(delete-test-file) |
| 744 |
|
| 745 |
(pass-if-equal "regular file, explicit flags" #f |
| 746 |
(skip-if-unsupported) |
| 747 |
(create-test-file) |
| 748 |
(call-with-port |
| 749 |
(open (dirname (test-file)) O_RDONLY) |
| 750 |
(lambda (port) |
| 751 |
(delete-file-at port (basename (test-file)) 0))) |
| 752 |
(file-exists? (test-file))) |
| 753 |
(delete-test-file) |
| 754 |
|
| 755 |
(pass-if-equal "directory, explicit flags" #f |
| 756 |
(skip-if-unsupported) |
| 757 |
(create-test-directory) |
| 758 |
(call-with-port |
| 759 |
(open (dirname (test-directory)) O_RDONLY) |
| 760 |
(lambda (port) |
| 761 |
(delete-file-at port (basename (test-directory)) AT_REMOVEDIR))) |
| 762 |
(file-exists? (test-directory))) |
| 763 |
(delete-test-directory) |
| 764 |
|
| 765 |
(pass-if-exception "not a port" exception:wrong-type-arg |
| 766 |
(skip-if-unsupported) |
| 767 |
(delete-file-at 'bogus "irrelevant")) |
| 768 |
|
| 769 |
(pass-if-exception "not a file port" exception:wrong-type-arg |
| 770 |
(skip-if-unsupported) |
| 771 |
(delete-file-at (open-input-string "") "irrelevant")) |
| 772 |
|
| 773 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 774 |
(skip-if-unsupported) |
| 775 |
(delete-file-at (call-with-port (open "." O_RDONLY) identity) |
| 776 |
"irrelevant"))) |
| 777 |
|
| 778 |
(with-test-prefix "openat" |
| 779 |
(define (skip-if-unsupported) |
| 780 |
(unless (defined? 'openat) |
| 781 |
(throw 'unsupported))) |
| 782 |
|
| 783 |
(define file (search-path %load-path "ice-9/boot-9.scm")) |
| 784 |
|
| 785 |
(define (call-with-relatively-opened-file directory-arguments file-arguments |
| 786 |
proc) |
| 787 |
(call-with-port |
| 788 |
(apply open directory-arguments) |
| 789 |
(lambda (directory) |
| 790 |
(call-with-port |
| 791 |
(apply openat directory file-arguments) |
| 792 |
(lambda (port) |
| 793 |
(proc port)))))) |
| 794 |
|
| 795 |
(pass-if-equal "mode read-only" "r" |
| 796 |
(skip-if-unsupported) |
| 797 |
(call-with-relatively-opened-file |
| 798 |
(list (dirname file) O_RDONLY) |
| 799 |
(list (basename file) O_RDONLY) |
| 800 |
(lambda (port) (port-mode port)))) |
| 801 |
|
| 802 |
(pass-if-equal "port-revealed count" 0 |
| 803 |
(skip-if-unsupported) |
| 804 |
(call-with-relatively-opened-file |
| 805 |
(list (dirname file) O_RDONLY) |
| 806 |
(list (basename file) O_RDONLY) |
| 807 |
(lambda (port) (port-revealed port)))) |
| 808 |
|
| 809 |
(when (file-exists? (test-file)) |
| 810 |
(delete-file (test-file))) |
| 811 |
|
| 812 |
(pass-if-equal "O_CREAT/O_WRONLY" (list #t (logand (lognot (umask)) #o666) "w") |
| 813 |
(skip-if-unsupported) |
| 814 |
(call-with-relatively-opened-file |
| 815 |
(list (dirname (test-file)) O_RDONLY) |
| 816 |
(list (basename (test-file)) (logior O_WRONLY O_CREAT)) |
| 817 |
(lambda (port) |
| 818 |
(list (file-exists? (test-file)) |
| 819 |
(stat:perms (stat (test-file))) |
| 820 |
(port-mode port))))) |
| 821 |
|
| 822 |
(when (file-exists? (test-file)) |
| 823 |
(delete-file (test-file))) |
| 824 |
|
| 825 |
(pass-if-equal "O_CREAT/O_WRONLY, non-default mode" |
| 826 |
(list #t (logand (lognot (umask)) #o700) "w") |
| 827 |
(skip-if-unsupported) |
| 828 |
(call-with-relatively-opened-file |
| 829 |
(list (dirname (test-file)) O_RDONLY) |
| 830 |
(list (basename (test-file)) (logior O_WRONLY O_CREAT) #o700) |
| 831 |
(lambda (port) |
| 832 |
(list (file-exists? (test-file)) |
| 833 |
(stat:perms (stat (test-file))) |
| 834 |
(port-mode port))))) |
| 835 |
|
| 836 |
(pass-if-exception "closed port" exception:wrong-type-arg |
| 837 |
(skip-if-unsupported) |
| 838 |
(openat (call-with-port (open "." O_RDONLY) identity) "." O_RDONLY)) |
| 839 |
|
| 840 |
(pass-if-exception "non-file port" exception:wrong-type-arg |
| 841 |
(skip-if-unsupported) |
| 842 |
(openat (open-input-string "") "." O_RDONLY)) |
| 843 |
|
| 844 |
(pass-if-exception "not a port" exception:wrong-type-arg |
| 845 |
(skip-if-unsupported) |
| 846 |
(openat "not a port" "." O_RDONLY)) |
| 847 |
|
| 848 |
(when (file-exists? (test-file)) |
| 849 |
(delete-file (test-file)))) |