Corrected the "-dump" report of fragment start position.
[rrq/fusefile.git] / overlaytest.lsp
1 #!/usr/bin/newlisp
2 #
3 # This is a test script for the overlay function of fusefile.
4 #
5 # 1) prepare a base image
6 # 2) set up a fusefile overlay
7 # 3) run tests
8 # 4) dismantle the fusefile overlay
9 # 5) remove test images
10
11 ; ID is hour, minute and second values packed into a string
12 (constant
13  'ID (apply string (3 3 (now)))
14  'BASE (format "%s.raw" ID)
15  'OLY (format "%s.oly" ID)
16  'SEGSZ 17000
17  'SEGN 40
18  )
19
20 (constant
21  'LIBC6 "/lib/x86_64-linux-gnu/libc.so.6"
22  'MINE "mine"
23  )
24
25 (import LIBC6 "on_exit" "int" "void*" "void*")
26
27 ;; Set up a fusefile
28 (define (onexit x y)
29   (write-line 2 (string "terminating: " x " " (get-string y)))
30   (! (format "fusermount -u %s" BASE))
31   (delete-file OLY)
32   (delete-file BASE)
33   )
34 ## note: BASE is set up as a holes file with SEGN segments of size SEGSZ
35 (! (format "dd if=/dev/zero of=%s bs=%d seek=%d count=0 status=none"
36            BASE SEGSZ SEGN))
37 (unless (= (! (format "fusefile %s %s -overlay:%s %s"
38                       "-ononempty -oallow_other" BASE OLY BASE)))
39   (exit 1))
40 (on_exit (callback 'onexit "void" "int" "void*") MINE)
41
42 (println (list BASE OLY))
43
44 (define (die) (write-line 2 (apply string (args))))
45
46 (define (prog1 x) x)
47
48 (define (pos X (OFF 0))
49   (+ (* SEGSZ X) OFF))
50
51 (define (read-segment FILE X (OFF 0) (N SEGSZ))
52   (let ((FD (open FILE "r")) (BUFFER ""))
53     (seek FD (pos X OFF))
54     (prog1 (when (= N (read FD BUFFER N)) BUFFER)
55       (close FD))))
56
57 (define (write-segment FILE X DATA (OFF 0))
58   (let ((FD (open FILE "u")))
59     (seek FD (pos X OFF))
60     (write FD DATA)
61     ;(seek FD -1)
62     (close FD)))
63
64 (define (read-ulong FD)
65   (let ((BUFFER ""))
66     (when (= 8 (read FD BUFFER 8)) ((unpack "ld" BUFFER) 0))))
67
68 (define (read-table)
69   (let ((AT (file-info BASE 0)) (FD (open OLY "r")) (COUNT 0) (OUT '()))
70     (seek FD AT)
71     (unless (setf COUNT (read-ulong FD))
72       (write-line 2 "** Bad count")
73       (exit 1))
74     (push COUNT OUT -1)
75     (dotimes (i COUNT)
76       (push (list (read-ulong FD) (read-ulong FD)) OUT -1))
77     OUT))
78
79 (define (check-segment AT DATA (OFF 0))
80   (write-segment BASE AT DATA OFF)
81   (println
82    (format "check %2d %d: %s %s %s" AT
83            (length DATA)
84            (if (= (read-segment BASE AT OFF (length DATA)) DATA) "ok" "error")
85            (if (= (read-segment OLY AT OFF (length DATA)) DATA) "ok" "error")
86            (string (read-table))))
87   )
88   
89 ;; Test 1
90 (seed (date-value))
91 (setf
92  DATA (pack (dup "b" SEGSZ) (rand 256 SEGSZ))
93  DATB (pack (dup "b" (* 4 SEGSZ)) (rand 256 (* 4 SEGSZ)))
94  AT (- SEGN 4))
95 (check-segment 0 DATA 0)
96
97 (check-segment AT DATA)
98 (check-segment (+ AT 2) DATA)
99 (check-segment (+ AT 1) DATA)
100 (check-segment (- AT 1) DATA -10)
101 (check-segment (- AT 1) DATA 10)
102
103 (check-segment 0 DATA 0)
104 (check-segment 1 DATA 1)
105 (check-segment 2 DATA 2)
106 (check-segment 0 DATB 10)
107
108 (check-segment (- SEGN 1) DATA 0)
109
110 ;(setf DATA (pack (dup "b" SEGSZ) (rand 256 SEGSZ)) AT (- SEGN 4))
111
112 (exit 0)