+ (dynamic-wind
+ (const #t)
+ (lambda ()
+ (close parent)
+ (call-with-temporary-directory
+ (lambda (root)
+ (display "ready\n" child)
+ (read child) ;wait for "go!"
+ (let ((put-old (string-append root "/real-root")))
+ (mount "none" root "tmpfs")
+ (mkdir put-old)
+ (call-with-output-file (string-append root "/test")
+ (lambda (port)
+ (display "testing\n" port)))
+ (pivot-root root put-old)
+ ;; The test file should now be located inside the root directory.
+ (write (and (file-exists? "/test") 'success!) child)
+ (close child)))))
+ (lambda ()
+ (primitive-exit 0))))
+ (pid
+ (close child)
+ (match (read parent)
+ ('ready
+ ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
+ ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
+ (call-with-output-file (format #f "/proc/~d/setgroups" pid)
+ (lambda (port)
+ (display "deny" port)))
+ (call-with-output-file (format #f "/proc/~d/uid_map" pid)
+ (lambda (port)
+ (format port "0 ~d 1" (getuid))))
+ (call-with-output-file (format #f "/proc/~d/gid_map" pid)
+ (lambda (port)
+ (format port "0 ~d 1" (getgid))))
+ (display "go!\n" parent)
+ (let ((result (read parent)))
+ (close parent)
+ (and (zero? (match (waitpid pid)
+ ((_ . status)
+ (status:exit-val status))))
+ result)))))))))
+
+(test-equal "scandir*, ENOENT"
+ ENOENT
+ (catch 'system-error
+ (lambda ()
+ (scandir* "/does/not/exist"))
+ (lambda args
+ (system-error-errno args))))
+
+(test-equal "scandir*, ASCII file names"
+ (scandir (dirname (search-path %load-path "guix/base32.scm"))
+ (const #t) string<?)
+ (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
+ (((names . properties) ...)
+ names)))
+
+(test-equal "scandir*, UTF-8 file names"
+ '("." ".." "α" "λ")
+ (call-with-temporary-directory
+ (lambda (directory)
+ ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
+ ;; name to the system call.
+ (let ((creat (pointer->procedure int
+ (dynamic-func "creat" (dynamic-link))
+ (list '* int))))
+ (creat (string->pointer (string-append directory "/α")
+ "UTF-8")
+ #o644)
+ (creat (string->pointer (string-append directory "/λ")
+ "UTF-8")
+ #o644)
+ (let ((locale (setlocale LC_ALL)))
+ (dynamic-wind
+ (lambda ()
+ ;; Make sure that even in a C locale we get the right result.
+ (setlocale LC_ALL "C"))
+ (lambda ()
+ (match (scandir* directory)
+ (((names . properties) ...)
+ names)))
+ (lambda ()
+ (setlocale LC_ALL locale))))))))
+
+(test-assert "scandir*, properties"
+ (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
+ (every (lambda (entry name)
+ (match entry
+ ((name2 . properties)
+ (and (string=? name2 name)
+ (let* ((full (string-append directory "/" name))
+ (stat (lstat full))
+ (inode (assoc-ref properties 'inode))
+ (type (assoc-ref properties 'type)))
+ (and (= inode (stat:ino stat))
+ (or (eq? type 'unknown)
+ (eq? type (stat:type stat)))))))))
+ (scandir* directory)
+ (scandir directory (const #t) string<?))))
+
+(false-if-exception (delete-file temp-file))
+(test-equal "fcntl-flock wait"
+ 42 ; the child's exit status
+ (let ((file (open-file temp-file "w0b")))
+ ;; Acquire an exclusive lock.
+ (fcntl-flock file 'write-lock)
+ (match (primitive-fork)
+ (0
+ (dynamic-wind
+ (const #t)
+ (lambda ()
+ ;; Reopen FILE read-only so we can have a read lock.
+ (let ((file (open-file temp-file "r0b")))
+ ;; Wait until we can acquire the lock.
+ (fcntl-flock file 'read-lock)
+ (primitive-exit (read file)))
+ (primitive-exit 1))
+ (lambda ()
+ (primitive-exit 2))))
+ (pid
+ ;; Write garbage and wait.
+ (display "hello, world!" file)
+ (force-output file)
+ (sleep 1)
+
+ ;; Write the real answer.
+ (seek file 0 SEEK_SET)
+ (truncate-file file 0)
+ (write 42 file)
+ (force-output file)
+
+ ;; Unlock, which should let the child continue.
+ (fcntl-flock file 'unlock)
+
+ (match (waitpid pid)
+ ((_ . status)
+ (let ((result (status:exit-val status)))
+ (close-port file)
+ result)))))))
+
+(test-equal "fcntl-flock non-blocking"
+ EAGAIN ; the child's exit status
+ (match (pipe)
+ ((input . output)
+ (match (primitive-fork)
+ (0
+ (dynamic-wind
+ (const #t)
+ (lambda ()
+ (close-port output)
+
+ ;; Wait for the green light.
+ (read-char input)
+
+ ;; Open FILE read-only so we can have a read lock.
+ (let ((file (open-file temp-file "w0")))
+ (catch 'flock-error
+ (lambda ()
+ ;; This attempt should throw EAGAIN.
+ (fcntl-flock file 'write-lock #:wait? #f))
+ (lambda (key errno)
+ (primitive-exit (pk 'errno errno)))))
+ (primitive-exit -1))
+ (lambda ()
+ (primitive-exit -2))))