summaryrefslogtreecommitdiff
path: root/tests/syscalls.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2017-05-30 12:03:54 +0200
committerLudovic Courtès <ludo@gnu.org>2017-06-16 17:08:22 +0200
commitfa73c1937364872560c509f02b3d7648a5bed006 (patch)
tree9b7c54b4c60ff8bab560ff3948fb74de4aed8b25 /tests/syscalls.scm
parent8cdbaebcbd34259793cdfb34b03f2f84db82a825 (diff)
syscalls: Add 'scandir*'.
* guix/build/syscalls.scm (%struct-dirent-header): New C struct. (string->pointer/utf-8, pointer->string/utf-8): New procedures. (opendir*, closedir*, readdir*, scandir*): New procedures. * tests/syscalls.scm ("scandir*, ENOENT") ("scandir*, ASCII file names", "scandir*, UTF-8 file names") ("scandir*, properties): New tests.
Diffstat (limited to 'tests/syscalls.scm')
-rw-r--r--tests/syscalls.scm60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/syscalls.scm b/tests/syscalls.scm
index e20f0600bc..8c048e6109 100644
--- a/tests/syscalls.scm
+++ b/tests/syscalls.scm
@@ -24,6 +24,8 @@
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-64)
+ #:use-module (system foreign)
+ #:use-module ((ice-9 ftw) #:select (scandir))
#:use-module (ice-9 match))
;; Test the (guix build syscalls) module, although there's not much that can
@@ -184,6 +186,64 @@
(status:exit-val status))))
(eq? #t 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