1 | (module ugarit-core |
---|
2 | (open-archive |
---|
3 | archive? |
---|
4 | archive-hash |
---|
5 | archive-global-directory-rules |
---|
6 | archive-file-cache-hits |
---|
7 | archive-writable? |
---|
8 | archive-unlinkable? |
---|
9 | archive-exists? |
---|
10 | archive-get |
---|
11 | archive-put! |
---|
12 | archive-remove-tag! |
---|
13 | archive-set-tag! |
---|
14 | archive-tag |
---|
15 | archive-all-tags |
---|
16 | archive-lock-tag! |
---|
17 | archive-unlock-tag! |
---|
18 | archive-tag-locked? |
---|
19 | archive-link! |
---|
20 | archive-unlink! |
---|
21 | archive-close! |
---|
22 | |
---|
23 | archive-store-block! |
---|
24 | |
---|
25 | make-key-stream-writer* |
---|
26 | key-stream-writer? |
---|
27 | key-stream-writer-write! |
---|
28 | key-stream-writer-finish! |
---|
29 | unlink-key-stream! |
---|
30 | fold-key-stream |
---|
31 | |
---|
32 | make-sexpr-stream-writer* |
---|
33 | sexpr-stream-writer? |
---|
34 | sexpr-stream-writer-write! |
---|
35 | sexpr-stream-writer-finish! |
---|
36 | unlink-sexpr-stream! |
---|
37 | fold-sexpr-stream |
---|
38 | |
---|
39 | store-sexpr! |
---|
40 | read-sexpr |
---|
41 | |
---|
42 | epochtime->string |
---|
43 | |
---|
44 | store-file! |
---|
45 | write-file-contents |
---|
46 | unlink-file! |
---|
47 | |
---|
48 | store-directory! |
---|
49 | unlink-directory! |
---|
50 | extract-directory! |
---|
51 | extract-object! |
---|
52 | snapshot-directory-tree! |
---|
53 | tag-snapshot! |
---|
54 | fold-history |
---|
55 | fold-archive-node) |
---|
56 | |
---|
57 | (import scheme) |
---|
58 | (import chicken) |
---|
59 | |
---|
60 | (use autoload) |
---|
61 | |
---|
62 | (define ((deny-autoload module)) (error (sprintf "Autoload does not seem to be working, so optional components from module ~s are not working" module) module)) |
---|
63 | |
---|
64 | (define-syntax no-autoload |
---|
65 | (er-macro-transformer |
---|
66 | (lambda (expr rename compare) |
---|
67 | (let ((module (cadr expr)) |
---|
68 | (procs (cddr expr)) |
---|
69 | (_begin (rename 'begin)) |
---|
70 | (_define (rename 'define)) |
---|
71 | (_deny-autoload (rename 'deny-autoload))) |
---|
72 | (cons _begin |
---|
73 | (map (lambda (x) |
---|
74 | (let ((orig-binding (if (pair? x) (car x) x)) |
---|
75 | (new-binding (if (pair? x) (cadr x) x))) |
---|
76 | `(,_define ,new-binding (,_deny-autoload ',module)))) |
---|
77 | procs)))))) |
---|
78 | |
---|
79 | (no-autoload lzma (compress lzma:compress) (decompress lzma:decompress)) |
---|
80 | (no-autoload z3 z3:encode-buffer z3:decode-buffer) |
---|
81 | (autoload tiger-hash tiger192-digest tiger192-binary-digest) |
---|
82 | (no-autoload sha2 sha256-digest sha384-digest sha512-digest sha512-binary-digest) |
---|
83 | (no-autoload aes make-aes128-encryptor make-aes128-decryptor make-aes192-encryptor make-aes192-decryptor make-aes256-encryptor make-aes256-decryptor) |
---|
84 | |
---|
85 | (use srfi-1) |
---|
86 | (use srfi-4) |
---|
87 | (use srfi-13) |
---|
88 | (use srfi-18) |
---|
89 | (use extras) |
---|
90 | (use ports) |
---|
91 | (use files) |
---|
92 | (use lolevel) |
---|
93 | (use data-structures) |
---|
94 | (use directory-rules) |
---|
95 | (use miscmacros) |
---|
96 | (use posix) |
---|
97 | (use crypto-tools) |
---|
98 | (use stty) |
---|
99 | (use matchable) |
---|
100 | (use regex) |
---|
101 | (use ugarit-backend) |
---|
102 | (use sql-de-lite) |
---|
103 | |
---|
104 | |
---|
105 | ;; |
---|
106 | ;; THE ARCHIVE |
---|
107 | ;; |
---|
108 | |
---|
109 | (define-record archive |
---|
110 | storage ; The storage instance we use |
---|
111 | check-correctness? ; boolean flag |
---|
112 | store-atime? ; boolean flag |
---|
113 | store-ctime? ; boolean flag |
---|
114 | hash ; the hash function, u8vector+type symbol->hex string |
---|
115 | compress ; the compressor, u8vector->smaller u8vector |
---|
116 | decompress ; the decompressor, inverse of the above |
---|
117 | encrypt ; the encryptor, u8vector -> u8vector |
---|
118 | decrypt ; the decryptor, inverse of the above |
---|
119 | global-directory-rules ; top-level directory rules |
---|
120 | file-cache ; sqlite db storing filesystem cache (see store-file! procedure); #f if not enabled |
---|
121 | file-cache-hits ; count of file cache hits |
---|
122 | ) |
---|
123 | |
---|
124 | (include "posixextras.scm") |
---|
125 | |
---|
126 | (define (file-cache-put! archive file-path mtime size key) |
---|
127 | (exec (sql (archive-file-cache archive) |
---|
128 | "INSERT OR REPLACE INTO files (path,mtime,size,key) VALUES (?,?,?,?)") |
---|
129 | file-path mtime size key)) |
---|
130 | |
---|
131 | (define (file-cache-get archive file-path mtime size) |
---|
132 | (let ((data (query fetch (sql (archive-file-cache archive) |
---|
133 | "SELECT key FROM files WHERE path = ? AND mtime = ? AND size = ?") |
---|
134 | file-path mtime size))) |
---|
135 | (if (pair? data) |
---|
136 | (car data) |
---|
137 | #f))) |
---|
138 | |
---|
139 | (define (prepend-type-byte b v) |
---|
140 | (let* ((v-len (u8vector-length v)) |
---|
141 | (v2 (make-u8vector (+ 1 v-len)))) |
---|
142 | (set! (u8vector-ref v2 0) b) |
---|
143 | |
---|
144 | (move-memory! v v2 v-len 0 1) |
---|
145 | v2)) |
---|
146 | |
---|
147 | (define (choose-compression-function config) |
---|
148 | (match config |
---|
149 | (#f (lambda (block) (prepend-type-byte 0 block))) ; No compression |
---|
150 | (('deflate) (lambda (block) (prepend-type-byte 1 (blob->u8vector/shared (string->blob (z3:encode-buffer (blob->string (u8vector->blob/shared block)))))))) ; deflate compression |
---|
151 | (('lzma) (lambda (block) (prepend-type-byte 2 (blob->u8vector/shared (lzma:compress (u8vector->blob/shared block)))))) |
---|
152 | (else (signal (make-property-condition 'exn 'location 'open-archive 'message "Unknown compression type" 'arguments (list config)))))) |
---|
153 | |
---|
154 | (define (decompress block) |
---|
155 | (case (u8vector-ref block 0) |
---|
156 | ((0) (subu8vector block 1 (u8vector-length block))) ; No compression |
---|
157 | ((1) (blob->u8vector/shared (string->blob (z3:decode-buffer (blob->string (u8vector->blob/shared (subu8vector block 1 (u8vector-length block)))))))) ; deflate |
---|
158 | ((2) (blob->u8vector/shared (lzma:decompress (u8vector->blob/shared (subu8vector block 1 (u8vector-length block)))))))) ; lzma |
---|
159 | |
---|
160 | (define (choose-hash-function config) |
---|
161 | (let ((make-basic-hash (lambda (hash) (lambda (block type) (string-append (hash block) (symbol->string type))))) |
---|
162 | (make-keyed-hash (lambda (hash key) (lambda (block type) (hash (string-append key (hash block) (symbol->string type))))))) |
---|
163 | (match config |
---|
164 | ((or #f ('tiger)) (make-basic-hash tiger192-digest)) |
---|
165 | (('tiger key) (make-keyed-hash tiger192-digest key)) |
---|
166 | (('sha256) (make-basic-hash sha256-digest)) |
---|
167 | (('sha256 key) (make-keyed-hash sha256-digest key)) |
---|
168 | (('sha384) (make-basic-hash sha384-digest)) |
---|
169 | (('sha384 key) (make-keyed-hash sha384-digest key)) |
---|
170 | (('sha512) (make-basic-hash sha512-digest)) |
---|
171 | (('sha512 key) (make-keyed-hash sha512-digest key)) |
---|
172 | (else (signal (make-property-condition 'exn 'location 'open-archive 'message "Unknown hash algorithm" 'arguments (list config))))))) |
---|
173 | |
---|
174 | (define (read-password prompt) |
---|
175 | (display prompt) |
---|
176 | (with-stty '(not echo) read-line)) |
---|
177 | |
---|
178 | ; Key specs are "hexhexhex" or (number-of-bytes "passphrase") |
---|
179 | (define (key->blob keyspec) |
---|
180 | (cond |
---|
181 | ((string? keyspec) |
---|
182 | (hexstring->blob keyspec)) |
---|
183 | ((pair? keyspec) |
---|
184 | (let* ((get-passphrase (lambda (maybe-passphrase) |
---|
185 | (if (eq? maybe-passphrase 'prompt) |
---|
186 | (read-password "Passphrase: ") |
---|
187 | maybe-passphrase))) |
---|
188 | (length (car keyspec)) |
---|
189 | (passphrase (get-passphrase (cadr keyspec))) |
---|
190 | (key (sha512-binary-digest passphrase))) |
---|
191 | (if (> length 64) ; 512 bits = 64 bytes |
---|
192 | (signal (make-property-condition 'exn 'location 'open-archive 'message "Cannot generate a key that large due to a shortage of a big enough hash function (max 64)" 'arguments (list keyspec))) |
---|
193 | (string->blob (substring/shared key 0 length))))))) |
---|
194 | |
---|
195 | |
---|
196 | (define (choose-crypto-functions config) |
---|
197 | (match config |
---|
198 | (#f (values |
---|
199 | (lambda (block) block) |
---|
200 | (lambda (block) block))) ; No encryption |
---|
201 | (('aes keyspec) |
---|
202 | (let ((key (key->blob keyspec)) |
---|
203 | (iv (make-blob 16)) ; IV is pseudo-randomly generated based on the blocks we are fed as an entropy source |
---|
204 | (stir-iv! (lambda (iv block) |
---|
205 | (move-memory! (string->blob |
---|
206 | (tiger192-binary-digest (string-append (tiger192-binary-digest block) (blob->string iv)))) |
---|
207 | iv 16)))) |
---|
208 | |
---|
209 | ; Generate initial IV from the key and current time |
---|
210 | (move-memory! (string->blob (tiger192-binary-digest |
---|
211 | (string-append (blob->string key) (number->string (current-seconds))))) iv 16) |
---|
212 | |
---|
213 | (let-values (((encryptor decryptor) |
---|
214 | (case (blob-size key) |
---|
215 | ((16) (values |
---|
216 | (make-aes128-encryptor key) |
---|
217 | (make-aes128-decryptor key))) |
---|
218 | ((24) (values |
---|
219 | (make-aes192-encryptor key) |
---|
220 | (make-aes192-decryptor key))) |
---|
221 | ((32) (values |
---|
222 | (make-aes256-encryptor key) |
---|
223 | (make-aes256-decryptor key))) |
---|
224 | (else |
---|
225 | (signal (make-property-condition 'exn 'location 'open-archive 'message "AES keys must be 16, 24, or 32 bytes long" 'arguments (list keyspec))))))) |
---|
226 | (let ((cbc-encryptor (make-cbc*-encryptor encryptor 16)) |
---|
227 | (cbc-decryptor (make-cbc*-decryptor decryptor 16))) |
---|
228 | (values |
---|
229 | (lambda (block) |
---|
230 | (stir-iv! iv block) |
---|
231 | (blob->u8vector/shared (cbc-encryptor (u8vector->blob/shared block) iv))) |
---|
232 | (lambda (block) (blob->u8vector/shared (cbc-decryptor (u8vector->blob/shared block))))))))) |
---|
233 | (else (signal (make-property-condition 'exn 'location 'open-archive 'message "Unknown encryption type" 'arguments (list config)))))) |
---|
234 | |
---|
235 | ; A config is an sexpr of the form: |
---|
236 | ; ((<key> <value>)|<flag>...) |
---|
237 | ; Valid keys: |
---|
238 | ; storage (expression to create a storage backend) |
---|
239 | ; compression algorithm name |
---|
240 | ; encryption (algorithm-name "key") |
---|
241 | ; Valid flags: |
---|
242 | ; double-check - check correctness lots, even if it costs efficiency |
---|
243 | (define (open-archive config store-atime? store-ctime?) |
---|
244 | (let ((*storage* #f) |
---|
245 | (*compression* #f) |
---|
246 | (*crypto* #f) |
---|
247 | (*hash* #f) |
---|
248 | (*double-check?* #f) |
---|
249 | (*file-cache* #f) |
---|
250 | (*global-rules* '())) |
---|
251 | |
---|
252 | (for-each (lambda (confentry) |
---|
253 | (match confentry |
---|
254 | ('double-check (set! *double-check?* #t)) |
---|
255 | (('storage command-line) |
---|
256 | (set! *storage* (import-storage command-line))) |
---|
257 | (('hash . conf) (set! *hash* conf)) |
---|
258 | (('compression . conf) (set! *compression* conf)) |
---|
259 | (('encryption . conf) (set! *crypto* conf)) |
---|
260 | (('file-cache path) |
---|
261 | (set! *file-cache* (open-database path)) |
---|
262 | (when (null? (schema *file-cache*)) |
---|
263 | (exec (sql *file-cache* "CREATE TABLE files (path TEXT PRIMARY KEY, mtime INTEGER, size INTEGER, key TEXT);")))) |
---|
264 | (('rule . conf) (set! *global-rules* (cons conf *global-rules*))) |
---|
265 | (_ (signal (make-property-condition 'exn 'location 'open-archive 'message "Unknown configuration entry" 'arguments (list confentry)))))) |
---|
266 | config) |
---|
267 | |
---|
268 | (if (not *storage*) |
---|
269 | (signal (make-property-condition 'exn 'location 'open-archive 'message "No archive storage was specified in the configuration!" 'arguments (list config)))) |
---|
270 | |
---|
271 | (let-values |
---|
272 | (((compress) (choose-compression-function *compression*)) |
---|
273 | ((hash) (choose-hash-function *hash*)) |
---|
274 | ((encrypt decrypt) (choose-crypto-functions *crypto*))) |
---|
275 | |
---|
276 | (make-archive |
---|
277 | *storage* |
---|
278 | *double-check?* |
---|
279 | store-atime? |
---|
280 | store-ctime? |
---|
281 | hash |
---|
282 | compress |
---|
283 | decompress |
---|
284 | encrypt |
---|
285 | decrypt |
---|
286 | *global-rules* |
---|
287 | *file-cache* 0)))) |
---|
288 | |
---|
289 | ; Take a block, and return a compressed and encrypted block |
---|
290 | (define (wrap-block archive block) |
---|
291 | ((archive-encrypt archive) |
---|
292 | ((archive-compress archive) block))) |
---|
293 | |
---|
294 | ;; Take a compressed and encrypted block, and recover the original data |
---|
295 | (define (unwrap-block archive block) |
---|
296 | ((archive-decompress archive) |
---|
297 | ((archive-decrypt archive) block))) |
---|
298 | |
---|
299 | (define (archive-max-block-size archive) |
---|
300 | (storage-max-block-size (archive-storage archive))) |
---|
301 | |
---|
302 | (define (archive-writable? archive) |
---|
303 | (storage-writable? (archive-storage archive))) |
---|
304 | |
---|
305 | (define (archive-unlinkable? archive) |
---|
306 | (storage-unlinkable? (archive-storage archive))) |
---|
307 | |
---|
308 | (define (check-archive-writable archive) |
---|
309 | (if (not (archive-writable? archive)) |
---|
310 | (signal (make-property-condition 'exn 'location 'check-archive-writable 'message "This isn't a writable archive")))) |
---|
311 | |
---|
312 | (define (check-archive-unlinkable archive) |
---|
313 | (if (not (archive-writable? archive)) |
---|
314 | (signal (make-property-condition 'exn 'location 'check-archive-unlinkable 'message "This isn't an unlinkable archive - it's append-only")))) |
---|
315 | |
---|
316 | (define (archive-put! archive key data type) |
---|
317 | (if (not (archive-writable? archive)) |
---|
318 | (signal (make-property-condition 'exn 'location 'archive-put! 'message "This isn't a writable archive"))) |
---|
319 | ((storage-put! (archive-storage archive)) key (wrap-block archive data) type)) |
---|
320 | |
---|
321 | (define (archive-exists? archive key) |
---|
322 | ((storage-exists? (archive-storage archive)) key)) |
---|
323 | |
---|
324 | (define (archive-get archive key) |
---|
325 | (let ((data (unwrap-block archive ((storage-get (archive-storage archive)) key)))) |
---|
326 | (if (archive-check-correctness? archive) |
---|
327 | (if (string=? key ((archive-hash archive) data (archive-exists? archive key))) |
---|
328 | data |
---|
329 | (begin |
---|
330 | (printf "CONSISTENCY CHECK FAILURE: Block ~A comes back as ~A which has hash ~A\n" key data ((archive-hash archive) data (archive-exists? archive key))) |
---|
331 | (assert #f)))) |
---|
332 | data)) |
---|
333 | |
---|
334 | (define (archive-link! archive key) |
---|
335 | (if (not (archive-writable? archive)) |
---|
336 | (signal (make-property-condition 'exn 'location 'archive-link! 'message "This isn't a writable archive"))) |
---|
337 | ((storage-link! (archive-storage archive)) key)) |
---|
338 | |
---|
339 | (define (archive-unlink! archive key) |
---|
340 | (if (not (archive-writable? archive)) |
---|
341 | (signal (make-property-condition 'exn 'location 'archive-link! 'message "This isn't an unlinkable archive - it's append-only"))) |
---|
342 | (let ((result ((storage-unlink! (archive-storage archive)) key))) |
---|
343 | (if result |
---|
344 | (unwrap-block archive result) |
---|
345 | #f))) |
---|
346 | |
---|
347 | (define (archive-set-tag! archive tag key) |
---|
348 | (if (not (archive-writable? archive)) |
---|
349 | (signal (make-property-condition 'exn 'location 'archive-set-tag! 'message "This isn't a writable archive"))) |
---|
350 | ((storage-set-tag! (archive-storage archive)) tag key)) |
---|
351 | |
---|
352 | (define (archive-tag archive tag) |
---|
353 | ((storage-tag (archive-storage archive)) tag)) |
---|
354 | |
---|
355 | (define (archive-all-tags archive) |
---|
356 | ((storage-all-tags (archive-storage archive)))) |
---|
357 | |
---|
358 | (define (archive-remove-tag! archive tag) |
---|
359 | (if (not (archive-writable? archive)) |
---|
360 | (signal (make-property-condition 'exn 'location 'archive-remove-tag! 'message "This isn't a writable archive"))) |
---|
361 | ((storage-remove-tag! (archive-storage archive)) tag)) |
---|
362 | |
---|
363 | (define (archive-lock-tag! archive tag) |
---|
364 | (if (not (archive-writable? archive)) |
---|
365 | (signal (make-property-condition 'exn 'location 'archive-lock-tag! 'message "This isn't a writable archive"))) |
---|
366 | ((storage-lock-tag! (archive-storage archive)) tag)) |
---|
367 | |
---|
368 | (define (archive-tag-locked? archive tag) |
---|
369 | (if (not (archive-writable? archive)) |
---|
370 | #f) |
---|
371 | ((storage-tag-locked? (archive-storage archive)) tag)) |
---|
372 | |
---|
373 | (define (archive-unlock-tag! archive tag) |
---|
374 | (if (not (archive-writable? archive)) |
---|
375 | (signal (make-property-condition 'exn 'location 'archive-unlock-tag! 'message "This isn't a writable archive"))) |
---|
376 | ((storage-unlock-tag! (archive-storage archive)) tag)) |
---|
377 | |
---|
378 | (define (archive-close! archive) |
---|
379 | (when (archive-file-cache archive) |
---|
380 | (close-database (archive-file-cache archive))) |
---|
381 | ((storage-close! (archive-storage archive)))) |
---|
382 | |
---|
383 | ;; |
---|
384 | ;; CORE ALGORITHMS |
---|
385 | ;; |
---|
386 | |
---|
387 | ;; Philosophy: insertion routines |
---|
388 | |
---|
389 | ;; Insertion routines insert an object into the archive, correctly |
---|
390 | ;; managing reference counts. In order to do this, they all return |
---|
391 | ;; two values: the key the object went in under, and a boolean flag |
---|
392 | ;; that is true if the object was already in the archive. This is so |
---|
393 | ;; that a parent object that calls that function can construct its |
---|
394 | ;; data block from the supplied child keys, then do an exists? check |
---|
395 | ;; to see if it already exists in the archive itself, if all of its |
---|
396 | ;; children were already in the archive. If it was, then it in turn |
---|
397 | ;; can just return the key and #t But if not, then it can link! every |
---|
398 | ;; child that WAS already in the archive, and then put! its own value |
---|
399 | ;; into the archive and return that with #f Thus, the reference counts |
---|
400 | ;; are maintained correctly. |
---|
401 | |
---|
402 | (define (reusing hash) |
---|
403 | ; (printf "REUSING: ~A\n" hash) |
---|
404 | hash) |
---|
405 | |
---|
406 | (define (virgin hash) |
---|
407 | ; (printf "CREATED: ~A\n" hash) |
---|
408 | hash) |
---|
409 | |
---|
410 | ;; BLOCKS OF RAW DATA THAT CANNOT CONTAIN CHILD KEYS |
---|
411 | ;; We never have any child keys to link!, so the not-reused case is simple. |
---|
412 | (define (archive-store-block! archive data type) |
---|
413 | (check-archive-writable archive) |
---|
414 | |
---|
415 | (let ((hash ((archive-hash archive) data type))) |
---|
416 | |
---|
417 | (if (archive-exists? archive hash) |
---|
418 | (values (reusing hash) #t) |
---|
419 | (begin |
---|
420 | (archive-put! archive hash data type) |
---|
421 | (values (virgin hash) #f))))) |
---|
422 | |
---|
423 | ;; GENERIC STREAMS OF KEYS |
---|
424 | ;; Both file and directory storage involve storing an arbitrary list of keys, in order |
---|
425 | ;; to string together a load of data blocks into one. |
---|
426 | ;; If they all fit into one block, then so be it. Otherwise, we have to split them |
---|
427 | ;; into blocks then create a higher-level stream of keys to store the keys of those blocks... |
---|
428 | |
---|
429 | (define-record key-stream-writer |
---|
430 | write! ;; Write a single string key to the stream. Accepts the key, and the already-existed boolean for proper reference counting. |
---|
431 | finish!) ;; Terminate the stream. Returns two values: key of the stream, and an already-existed boolean. |
---|
432 | |
---|
433 | (define (copy-string-into-place! u8v offset string string-offs string-len) |
---|
434 | (move-memory! string u8v (- string-len string-offs) string-offs offset) |
---|
435 | (void)) |
---|
436 | |
---|
437 | (define (serialise-strings! u8v offset strings) |
---|
438 | (if (null? strings) |
---|
439 | (void) |
---|
440 | (begin |
---|
441 | (let* ((string (blob->u8vector/shared (string->blob (string-append (car strings) "\n")))) |
---|
442 | (string-len (u8vector-length string))) |
---|
443 | (copy-string-into-place! u8v (- offset string-len) string 0 string-len) |
---|
444 | (serialise-strings! u8v (- offset string-len) (cdr strings)))))) |
---|
445 | |
---|
446 | (define (make-key-stream-writer* archive type) |
---|
447 | (check-archive-writable archive) |
---|
448 | |
---|
449 | (let* ((*key-buffer* '()) |
---|
450 | (*key-buffer-bytes* 0) |
---|
451 | (*key-buffer-reused?* #t) |
---|
452 | (*parent-stream* #f) |
---|
453 | |
---|
454 | (next-write-will-overflow? (lambda (key) |
---|
455 | (assert (< (string-length key) (archive-max-block-size archive))) |
---|
456 | (> (+ *key-buffer-bytes* (string-length key) 1) (archive-max-block-size archive)))) |
---|
457 | |
---|
458 | (flush! (lambda () |
---|
459 | (let ((keys-serialised (make-u8vector *key-buffer-bytes*))) |
---|
460 | (serialise-strings! keys-serialised *key-buffer-bytes* (map car *key-buffer*)) |
---|
461 | |
---|
462 | (let ((hash ((archive-hash archive) keys-serialised type))) |
---|
463 | |
---|
464 | (if (and *key-buffer-reused?* (archive-exists? archive hash)) |
---|
465 | (begin |
---|
466 | (set! *key-buffer* '()) |
---|
467 | (set! *key-buffer-bytes* 0) |
---|
468 | (set! *key-buffer-reused?* #t) |
---|
469 | (values (reusing hash) #t)) ; We, too, are reused |
---|
470 | (begin ; We are unique and new and precious! |
---|
471 | (for-each (lambda (x) ; link! all reused children |
---|
472 | (let ((key (car x)) |
---|
473 | (reused? (cdr x))) |
---|
474 | (if reused? |
---|
475 | (archive-link! archive key)))) |
---|
476 | *key-buffer*) |
---|
477 | |
---|
478 | (archive-put! archive hash keys-serialised type) |
---|
479 | |
---|
480 | (set! *key-buffer* '()) |
---|
481 | (set! *key-buffer-bytes* 0) |
---|
482 | (set! *key-buffer-reused?* #t) |
---|
483 | |
---|
484 | (values (virgin hash) #f))))))) |
---|
485 | |
---|
486 | (write! (lambda (key reused?) |
---|
487 | (if (next-write-will-overflow? key) |
---|
488 | (let-values (((flush-key flush-reused?) (flush!))) |
---|
489 | (if (not *parent-stream*) |
---|
490 | (set! *parent-stream* (make-key-stream-writer* archive type))) |
---|
491 | ((key-stream-writer-write! *parent-stream*) flush-key flush-reused?))) |
---|
492 | |
---|
493 | ;; What happens if the same key comes up twice, eh? |
---|
494 | (set! *key-buffer* (cons (cons key reused?) *key-buffer*)) |
---|
495 | (set! *key-buffer-reused?* (and *key-buffer-reused?* reused?)) |
---|
496 | (set! *key-buffer-bytes* (+ *key-buffer-bytes* (string-length key) 1)) |
---|
497 | (void))) |
---|
498 | |
---|
499 | (finish! (lambda () |
---|
500 | (cond |
---|
501 | (*parent-stream* |
---|
502 | (begin |
---|
503 | (if (not (null? *key-buffer*)) |
---|
504 | (let-values (((flush-key flush-reused?) (flush!))) |
---|
505 | ((key-stream-writer-write! *parent-stream*) flush-key flush-reused?))) |
---|
506 | ((key-stream-writer-finish! *parent-stream*)))) |
---|
507 | ((null? *key-buffer*) ; Empty stream |
---|
508 | (archive-store-block! archive (make-u8vector 0) type)) |
---|
509 | ((null? (cdr *key-buffer*)) ; Single-element stream |
---|
510 | (values (caar *key-buffer*) (cdar *key-buffer*))) ; Just return the one element! |
---|
511 | (else ; More than one key, but not enough to have flushed before |
---|
512 | (flush!)))))) |
---|
513 | |
---|
514 | (make-key-stream-writer |
---|
515 | write! |
---|
516 | finish!))) |
---|
517 | |
---|
518 | (define (deserialise-key-stream block) ; Convert a key stream block to a list of key strings |
---|
519 | (string-split (blob->string (u8vector->blob/shared block)) "\n")) |
---|
520 | |
---|
521 | ;; kons is called on (key type accumulator) for every key in the stream, in order |
---|
522 | (define (fold-key-stream archive key ks-type kons knil) |
---|
523 | (let ((type (archive-exists? archive key))) |
---|
524 | (if (eq? ks-type type) |
---|
525 | ; Recurse |
---|
526 | (begin |
---|
527 | (let ((subkeys (deserialise-key-stream (archive-get archive key)))) |
---|
528 | (fold |
---|
529 | (lambda (subkey acc) (fold-key-stream archive subkey ks-type kons acc)) |
---|
530 | knil |
---|
531 | subkeys))) |
---|
532 | ; Leaf node |
---|
533 | (kons key type knil)))) |
---|
534 | |
---|
535 | ; (child-unlink! archive key type) is called on every child key of a deleted block |
---|
536 | (define (unlink-key-stream! archive key type child-unlink!) |
---|
537 | (check-archive-unlinkable archive) |
---|
538 | |
---|
539 | (let ((result (archive-unlink! archive key))) |
---|
540 | (if result ; result is now list of keys, \n separated, to recursively unlink |
---|
541 | (for-each (lambda (subkey) |
---|
542 | (let ((child-type (archive-exists? archive subkey))) |
---|
543 | (if child-type ; The child may not actually exist any more, in which case, job done! |
---|
544 | (if (eq? child-type type) |
---|
545 | (unlink-key-stream! archive subkey type child-unlink!) |
---|
546 | (child-unlink! archive subkey child-type))))) |
---|
547 | (deserialise-key-stream result))))) |
---|
548 | |
---|
549 | ;; FILE STORAGE |
---|
550 | ;; Files are stored as either: |
---|
551 | ;; 1) A direct block of type "f" containing the file data |
---|
552 | ;; 2) An indirect block of type "fi" that's a keystream of keys of direct or indirect blocks |
---|
553 | |
---|
554 | ;; Uses standard input port for the file data |
---|
555 | ;; Returns key and reused? |
---|
556 | (define (store-file! archive file-path file-stat) |
---|
557 | (let* ((store-file-without-caching! |
---|
558 | (lambda () |
---|
559 | ;; Actually upload the file |
---|
560 | ;; FIXME: memory-map the file in 1MB chunks, and copy them into u8vectors? |
---|
561 | (letrec ((blocksize (archive-max-block-size archive)) |
---|
562 | (*buffer* (make-u8vector blocksize)) |
---|
563 | (ksw (make-key-stream-writer* archive 'fi)) |
---|
564 | (upload-file (lambda () |
---|
565 | (let ((bytes-read (read-u8vector! blocksize *buffer*))) |
---|
566 | (if (not (zero? bytes-read)) |
---|
567 | (let-values (((data-key data-reused?) |
---|
568 | (archive-store-block! archive (subu8vector *buffer* 0 bytes-read) 'f))) |
---|
569 | ((key-stream-writer-write! ksw) data-key data-reused?) |
---|
570 | (upload-file)) |
---|
571 | ((key-stream-writer-finish! ksw))))))) |
---|
572 | (upload-file)))) |
---|
573 | (store-file-and-cache! |
---|
574 | (lambda (mtime size) |
---|
575 | (let-values (((key reused?) (store-file-without-caching!))) |
---|
576 | (file-cache-put! archive file-path mtime size key) |
---|
577 | (values key reused?))))) |
---|
578 | |
---|
579 | (check-archive-writable archive) |
---|
580 | |
---|
581 | ;; Firstly, if we have an mtime cache, use it to see if the file is already in the archive |
---|
582 | ;; The cache is keyed on file paths, and the contents are |
---|
583 | ;; sexprs of the form (mtime hash) |
---|
584 | (if (archive-file-cache archive) |
---|
585 | (let* ((mtime (vector-ref file-stat 8)) ; Should have used and-let* |
---|
586 | (size (vector-ref file-stat 5)) |
---|
587 | (cache-result (file-cache-get archive file-path mtime size))) |
---|
588 | (if cache-result |
---|
589 | (begin |
---|
590 | (archive-file-cache-hits-set! archive |
---|
591 | (+ (archive-file-cache-hits archive) 1)) |
---|
592 | (values cache-result #t)) ; Found in cache! Woot! |
---|
593 | (store-file-and-cache! mtime size))) ; not in cache |
---|
594 | (store-file-without-caching!)))) ; no mtime cache |
---|
595 | |
---|
596 | ;; Call kons on each u8vector block of the file in turn |
---|
597 | ;; with an accumulator that starts as knil as a second argument |
---|
598 | (define (fold-file archive key kons knil) |
---|
599 | (fold-key-stream archive key 'fi |
---|
600 | (lambda (key type acc) |
---|
601 | (kons (archive-get archive key) acc)) |
---|
602 | knil)) |
---|
603 | |
---|
604 | ;; Write the contents of the file to the standard output port |
---|
605 | (define (write-file-contents archive key) |
---|
606 | (fold-file archive key |
---|
607 | (lambda (block acc) |
---|
608 | (begin |
---|
609 | (write-u8vector block) |
---|
610 | #f)) |
---|
611 | #f)) |
---|
612 | |
---|
613 | (define (unlink-file! archive key) |
---|
614 | (check-archive-unlinkable archive) |
---|
615 | |
---|
616 | (unlink-key-stream! archive key 'fi (lambda (archive key type) |
---|
617 | (archive-unlink! archive key)))) |
---|
618 | |
---|
619 | ;; GENERIC STREAMS OF S-EXPRESSIONS |
---|
620 | ;; These are to be used to implement directories |
---|
621 | ;; But might be useful for other complex structures in future |
---|
622 | |
---|
623 | (define-record sexpr-stream-writer |
---|
624 | write! ;; Write an sexpr to the stream. Second argument is a list of pairs, one per key mentioned in the sexpr, car is the key and cdr is the reused? flag. |
---|
625 | finish!) ;; Return the key and reused? flag for the whole thing |
---|
626 | |
---|
627 | ;; FIXME: Examine this and make-key-stream-writer* |
---|
628 | ;; and try and merge them to use a common string-stream-writer abstraction |
---|
629 | ;; if it's worth it. They share a lot, yet also differ a lot. |
---|
630 | (define (make-sexpr-stream-writer* archive type ks-type) |
---|
631 | (check-archive-writable archive) |
---|
632 | (let* ((*sexpr-buffer* '()) ; List of strings |
---|
633 | (*sexpr-buffer-bytes* 0) ; Bytes used so far |
---|
634 | (*key-buffer* '()) ; List of key-reused? pairs |
---|
635 | (*key-buffer-reused?* #t) ; All reused in the buffer so far? |
---|
636 | (*parent-stream* #f) ; Key stream |
---|
637 | |
---|
638 | (flush! (lambda () |
---|
639 | (let ((serialised-buffer (make-u8vector *sexpr-buffer-bytes*))) |
---|
640 | (begin |
---|
641 | (serialise-strings! serialised-buffer *sexpr-buffer-bytes* *sexpr-buffer*) |
---|
642 | (let ((hash ((archive-hash archive) serialised-buffer type))) |
---|
643 | (begin |
---|
644 | |
---|
645 | (if (archive-check-correctness? archive) |
---|
646 | (if *key-buffer-reused?* |
---|
647 | (assert (every cdr *key-buffer*) "Key buffer thinks it's all reused, but it isn't:" *key-buffer*) |
---|
648 | ; else |
---|
649 | (assert (not (every cdr *key-buffer*)) "Key buffer thinks it's not all reused, but it is:" *key-buffer*))) |
---|
650 | |
---|
651 | (if (and *key-buffer-reused?* (archive-exists? archive hash)) |
---|
652 | (begin |
---|
653 | (set! *sexpr-buffer* '()) |
---|
654 | (set! *sexpr-buffer-bytes* 0) |
---|
655 | (set! *key-buffer* '()) |
---|
656 | (set! *key-buffer-reused?* #t) |
---|
657 | (values (reusing hash) #t)) ; We, too, are reused |
---|
658 | (begin ; We are unique and new and precious! |
---|
659 | (for-each (lambda (x) ; link! all reused children |
---|
660 | (let ((key (car x)) |
---|
661 | (reused? (cdr x))) |
---|
662 | (if reused? |
---|
663 | (archive-link! archive key)))) |
---|
664 | *key-buffer*) |
---|
665 | |
---|
666 | (archive-put! archive hash serialised-buffer type) |
---|
667 | |
---|
668 | (set! *sexpr-buffer* '()) |
---|
669 | (set! *sexpr-buffer-bytes* 0) |
---|
670 | (set! *key-buffer* '()) |
---|
671 | (set! *key-buffer-reused?* #t) |
---|
672 | |
---|
673 | (values (virgin hash) #f))))))))) |
---|
674 | |
---|
675 | (write! (lambda (sexpr keys) |
---|
676 | (let* ((sexpr-string |
---|
677 | (with-output-to-string (lambda () |
---|
678 | (write sexpr)))) |
---|
679 | (sexpr-len (string-length sexpr-string))) |
---|
680 | |
---|
681 | (assert (< sexpr-len (archive-max-block-size archive))) |
---|
682 | |
---|
683 | (if (> (+ *sexpr-buffer-bytes* sexpr-len 1) (archive-max-block-size archive)) |
---|
684 | (let-values (((flush-key flush-reused?) (flush!))) |
---|
685 | (if (not *parent-stream*) |
---|
686 | (set! *parent-stream* (make-key-stream-writer* archive ks-type))) |
---|
687 | ((key-stream-writer-write! *parent-stream*) flush-key flush-reused?))) |
---|
688 | |
---|
689 | (set! *sexpr-buffer* (cons sexpr-string *sexpr-buffer*)) |
---|
690 | (set! *key-buffer* (append keys *key-buffer*)) |
---|
691 | (set! *key-buffer-reused?* (and *key-buffer-reused?* (every cdr keys))) |
---|
692 | (set! *sexpr-buffer-bytes* (+ *sexpr-buffer-bytes* sexpr-len 1)) |
---|
693 | (void)))) |
---|
694 | |
---|
695 | (finish! (lambda () |
---|
696 | (cond |
---|
697 | (*parent-stream* |
---|
698 | (begin |
---|
699 | (if (not (null? *sexpr-buffer*)) |
---|
700 | (let-values (((flush-key flush-reused?) (flush!))) |
---|
701 | ((key-stream-writer-write! *parent-stream*) flush-key flush-reused?))) |
---|
702 | ((key-stream-writer-finish! *parent-stream*)))) |
---|
703 | ((null? *sexpr-buffer*) ; Empty stream |
---|
704 | (archive-store-block! archive (make-u8vector 0) type)) |
---|
705 | (else ; Some sexprs, but not enough to have flushed before |
---|
706 | (flush!)))))) |
---|
707 | |
---|
708 | (make-sexpr-stream-writer write! finish!))) |
---|
709 | |
---|
710 | (define (deserialise-sexpr-stream block) ; Convert a sexpr stream block to a list of sexprs |
---|
711 | (map |
---|
712 | (lambda (string) |
---|
713 | (with-input-from-string string read)) |
---|
714 | (string-split (blob->string (u8vector->blob/shared block)) "\n"))) |
---|
715 | |
---|
716 | (define (fold-sexpr-stream archive key leaf-type ks-type kons knil) |
---|
717 | (fold-key-stream archive key ks-type |
---|
718 | (lambda (key found-leaf-type acc) |
---|
719 | (assert (eq? found-leaf-type leaf-type)) |
---|
720 | (let ((sexprs (deserialise-sexpr-stream (archive-get archive key)))) |
---|
721 | (fold |
---|
722 | kons |
---|
723 | acc |
---|
724 | sexprs))) |
---|
725 | knil)) |
---|
726 | |
---|
727 | (define (unlink-sexpr-stream-block! archive key sexpr-unlink!) |
---|
728 | (let ((result (archive-unlink! archive key))) |
---|
729 | (if result |
---|
730 | (for-each sexpr-unlink! (deserialise-sexpr-stream result))))) |
---|
731 | |
---|
732 | (define (unlink-sexpr-stream! archive key leaf-type ks-type sexpr-unlink!) |
---|
733 | (check-archive-unlinkable archive) |
---|
734 | (let ((type (archive-exists? archive key))) |
---|
735 | (cond |
---|
736 | ((eq? type ks-type) |
---|
737 | (unlink-key-stream! archive key ks-type |
---|
738 | (lambda (archive leaf-key found-leaf-type) |
---|
739 | (assert (eq? found-leaf-type leaf-type)) |
---|
740 | (unlink-sexpr-stream-block! archive leaf-key sexpr-unlink!)))) |
---|
741 | ((eq? type leaf-type) |
---|
742 | (unlink-sexpr-stream-block! archive key sexpr-unlink!)) |
---|
743 | (else |
---|
744 | (assert (or (eq? type leaf-type) (eq? type ks-type)) (sprintf "unlink-sexpr-stream!: Invalid block type (expected ~a)" (list leaf-type ks-type)) type))))) |
---|
745 | |
---|
746 | ;; DIRECTORY STORAGE |
---|
747 | ;; Directories are stored as either; |
---|
748 | ;; 1) A direct block of type "d" containing a list of file/directory entries, each of which is an s-expr |
---|
749 | ;; The car of the s-expr is the file name |
---|
750 | ;; The cadr is a type symbol - file, dir, symlink, chardev, blockdev, fifo, socket |
---|
751 | ;; The cddr is an alist of other properties |
---|
752 | ;; Regular files have a 'content entry containing a key, for example. |
---|
753 | ;; Also look out for 'mode 'uid 'gid 'atime 'mtime 'ctime |
---|
754 | ;; Symlinks have 'target |
---|
755 | ;; Directories have 'content, too |
---|
756 | ;; Files with streams or forks or whatnot can have more than one content key, of course... |
---|
757 | ;; 2) An indirect block of type "di" that's a keystream of keys to direct or indirect blocks |
---|
758 | |
---|
759 | ;; Look for a .ugarit file in the given directory |
---|
760 | ;; If one is found, return its contents |
---|
761 | (define (read-local-rules archive path) |
---|
762 | (let ((conf-file (make-pathname path ".ugarit"))) |
---|
763 | (if (file-exists? conf-file) |
---|
764 | (with-input-from-file conf-file read-file) |
---|
765 | '()))) |
---|
766 | |
---|
767 | ;; Do the rules list say to ignore the file? |
---|
768 | ;; Statements towards the head of the list take priority |
---|
769 | ;; And we want to accept the most recent 'ignore' or 'include', |
---|
770 | ;; defaulting to 'include' if neither is found |
---|
771 | (define (rules-say-ignore rules) |
---|
772 | (match rules |
---|
773 | ('() #f) |
---|
774 | ((('exclude) . _) #t) |
---|
775 | ((('include) . _) #f) |
---|
776 | ((_ . more) (rules-say-ignore more)))) |
---|
777 | |
---|
778 | ;; Store a directory |
---|
779 | ;; Returns the usual key and reused? values |
---|
780 | (define (store-directory! archive path) |
---|
781 | (call-with-context |
---|
782 | (read-local-rules archive path) |
---|
783 | path |
---|
784 | (lambda () |
---|
785 | (check-archive-writable archive) |
---|
786 | |
---|
787 | (let ((ssw (make-sexpr-stream-writer* archive 'd 'di)) |
---|
788 | (rules-checker (make-filesystem-object-pattern-checker path))) |
---|
789 | |
---|
790 | (for-each (lambda (filename) |
---|
791 | (handle-exceptions exn |
---|
792 | (printf "ERROR: Could not store ~a into the archive, skipping it...\n" (make-pathname path filename)) |
---|
793 | (let* ((file-path (make-pathname path filename)) |
---|
794 | (stats (file-stat file-path #t)) |
---|
795 | (mode (bitwise-and (vector-ref stats 1) (bitwise-not stat/ifmt))) |
---|
796 | (uid (vector-ref stats 3)) |
---|
797 | (gid (vector-ref stats 4)) |
---|
798 | (atime (vector-ref stats 6)) |
---|
799 | (ctime (vector-ref stats 7)) |
---|
800 | (mtime (vector-ref stats 8)) |
---|
801 | (type (bitwise-and (vector-ref stats 1) stat/ifmt)) |
---|
802 | (standard-file-attributes |
---|
803 | (list (cons 'mode mode) (cons 'uid uid) (cons 'gid gid) (cons 'mtime mtime))) |
---|
804 | (file-rules |
---|
805 | (object-matches filename rules-checker))) |
---|
806 | (if (archive-store-ctime? archive) |
---|
807 | (set! standard-file-attributes (cons (cons 'ctime ctime) standard-file-attributes))) |
---|
808 | (if (archive-store-atime? archive) |
---|
809 | (set! standard-file-attributes (cons (cons 'atime atime) standard-file-attributes))) |
---|
810 | (if (not (rules-say-ignore file-rules)) |
---|
811 | (cond |
---|
812 | ((eq? type stat/ifsock) |
---|
813 | (printf "WARNING: ~A is a socket, ignoring...\n" file-path)) |
---|
814 | ((eq? type stat/ifreg) |
---|
815 | (let-values (((content-key content-reused?) (with-input-from-file file-path (lambda () (store-file! archive file-path stats))))) |
---|
816 | ((sexpr-stream-writer-write! ssw) |
---|
817 | (append (list filename 'file (cons 'contents content-key) (cons 'size (vector-ref stats 5))) standard-file-attributes) |
---|
818 | (list (cons content-key content-reused?))))) |
---|
819 | ((eq? type stat/ifdir) |
---|
820 | (let-values (((content-key content-reused?) (store-directory! archive file-path))) |
---|
821 | ((sexpr-stream-writer-write! ssw) |
---|
822 | (append (list filename 'dir (cons 'contents content-key)) standard-file-attributes) |
---|
823 | (list (cons content-key content-reused?))))) |
---|
824 | ((eq? type stat/iflnk) |
---|
825 | ((sexpr-stream-writer-write! ssw) |
---|
826 | (append (list filename 'symlink (cons 'target (read-symbolic-link file-path))) standard-file-attributes) |
---|
827 | '())) |
---|
828 | ((eq? type stat/ifblk) |
---|
829 | (let ((devnum (vector-ref stats 10))) |
---|
830 | ((sexpr-stream-writer-write! ssw) |
---|
831 | (append (list filename 'block-device (cons 'number devnum)) standard-file-attributes) |
---|
832 | '()))) |
---|
833 | ((eq? type stat/ifchr) |
---|
834 | (let ((devnum (vector-ref stats 10))) |
---|
835 | ((sexpr-stream-writer-write! ssw) |
---|
836 | (append (list filename 'character-device (cons 'number devnum)) standard-file-attributes) |
---|
837 | '()))) |
---|
838 | ((eq? type stat/ififo) |
---|
839 | ((sexpr-stream-writer-write! ssw) |
---|
840 | (append (list filename 'fifo) standard-file-attributes) |
---|
841 | '())) |
---|
842 | (else |
---|
843 | ; WTF? |
---|
844 | (printf "ERROR: I can't ascertain the type of ~A. Skipping it...\n" file-path))))))) |
---|
845 | (sort! (directory path #t) string>?)) |
---|
846 | |
---|
847 | ((sexpr-stream-writer-finish! ssw)))))) |
---|
848 | |
---|
849 | (define (unlink-directory! archive key) |
---|
850 | (check-archive-unlinkable archive) |
---|
851 | |
---|
852 | (unlink-sexpr-stream! archive key 'd 'di |
---|
853 | (lambda (dirent) |
---|
854 | (let ((type (cadr dirent)) |
---|
855 | (name (car dirent)) |
---|
856 | (props (cddr dirent))) |
---|
857 | (cond |
---|
858 | ((eq? type 'file) |
---|
859 | (unlink-file! archive (cdr (assq 'contents props)))) |
---|
860 | ((eq? type 'dir) |
---|
861 | (unlink-directory! archive (cdr (assq 'contents props))))))))) |
---|
862 | |
---|
863 | (define (set-standard-file-metadata! path props) |
---|
864 | (let ((mode (assq 'mode props)) |
---|
865 | (uid (assq 'uid props)) |
---|
866 | (gid (assq 'gid props)) |
---|
867 | (mtime (assq 'mtime props)) |
---|
868 | (atime (assq 'atime props))) |
---|
869 | |
---|
870 | (if mode |
---|
871 | (change-file-mode path (cdr mode))) |
---|
872 | |
---|
873 | (if (or uid gid) |
---|
874 | (handle-exceptions exn |
---|
875 | (printf "WARNING: It was not possible to set the uid/gid of ~a\n" path) |
---|
876 | (change-file-owner path |
---|
877 | (if uid (cdr uid) (current-user-id)) |
---|
878 | (if gid (cdr gid) (current-group-id))))) |
---|
879 | |
---|
880 | (if (or mtime atime) |
---|
881 | (change-file-times path |
---|
882 | (if atime (cdr atime) (current-seconds)) |
---|
883 | (if mtime (cdr mtime) (current-seconds)))) |
---|
884 | |
---|
885 | (void))) |
---|
886 | |
---|
887 | (define (extract-file! archive props path) |
---|
888 | (let ((contents-key (cdr (assq 'contents props)))) |
---|
889 | (with-output-to-file path |
---|
890 | (lambda () |
---|
891 | (write-file-contents archive contents-key))) |
---|
892 | (set-standard-file-metadata! path props))) |
---|
893 | |
---|
894 | (define (extract-subdirectory! archive props path) |
---|
895 | (if (not (directory? path)) |
---|
896 | (create-directory path)) |
---|
897 | |
---|
898 | (let ((contents-key (cdr (assq 'contents props)))) |
---|
899 | |
---|
900 | (extract-directory! archive contents-key path) |
---|
901 | |
---|
902 | (set-standard-file-metadata! path props))) |
---|
903 | |
---|
904 | (define (extract-symlink! archive props path) |
---|
905 | (let ((target (cdr (assq 'target props))) |
---|
906 | (mode (assq 'mode props)) |
---|
907 | (uid (assq 'uid props)) |
---|
908 | (gid (assq 'gid props)) |
---|
909 | (mtime (assq 'mtime props)) |
---|
910 | (atime (assq 'atime props))) |
---|
911 | |
---|
912 | (create-symbolic-link target path) |
---|
913 | ;; Alas, there is no portable way to set the atime/mtime on a link. |
---|
914 | ;; I think, somehow, we will manage to live our lives without the atime and mtime on links... |
---|
915 | (if mode |
---|
916 | (change-link-mode path (cdr mode))) |
---|
917 | |
---|
918 | (if (or uid gid) |
---|
919 | (handle-exceptions exn |
---|
920 | (printf "WARNING: It was not possible to set the uid/gid of ~a\n" path) |
---|
921 | (change-link-owner path |
---|
922 | (if uid (cdr uid) (current-user-id)) |
---|
923 | (if gid (cdr gid) (current-group-id))))))) |
---|
924 | |
---|
925 | (define (extract-fifo! archive props path) |
---|
926 | |
---|
927 | (create-fifo path) |
---|
928 | |
---|
929 | (set-standard-file-metadata! path props)) |
---|
930 | |
---|
931 | (define (extract-block-device! archive props path) |
---|
932 | (let ((number (cdr (assq 'number props)))) |
---|
933 | |
---|
934 | (handle-exceptions exn |
---|
935 | (printf "WARNING: It was not possible to recreate block device ~a\n" path) |
---|
936 | |
---|
937 | (create-special-file path stat/ifblk number) |
---|
938 | (set-standard-file-metadata! path props)))) |
---|
939 | |
---|
940 | (define (extract-character-device! archive props path) |
---|
941 | (let ((number (cdr (assq 'number props)))) |
---|
942 | |
---|
943 | (handle-exceptions exn |
---|
944 | (printf "WARNING: It was not possible to recreate character device ~a\n" path) |
---|
945 | |
---|
946 | (create-special-file path stat/ifchr number) |
---|
947 | (set-standard-file-metadata! path props)))) |
---|
948 | |
---|
949 | (define (extract-object! archive dirent target-path) |
---|
950 | (let ((type (cadr dirent)) |
---|
951 | (name (car dirent)) |
---|
952 | (props (cddr dirent))) |
---|
953 | (cond |
---|
954 | ((eq? type 'file) |
---|
955 | (extract-file! archive props (make-pathname target-path name))) |
---|
956 | ((eq? type 'dir) |
---|
957 | (extract-subdirectory! archive props (make-pathname target-path name))) |
---|
958 | ((eq? type 'symlink) |
---|
959 | (extract-symlink! archive props (make-pathname target-path name))) |
---|
960 | ((eq? type 'fifo) |
---|
961 | (extract-fifo! archive props (make-pathname target-path name))) |
---|
962 | ((eq? type 'block-device) |
---|
963 | (extract-block-device! archive props (make-pathname target-path name))) |
---|
964 | ((eq? type 'character-device) |
---|
965 | (extract-character-device! archive props (make-pathname target-path name))) |
---|
966 | (else |
---|
967 | (printf "ERROR: Found an object (~A) of unknown type (~A), skipping...\n" name type))))) |
---|
968 | |
---|
969 | (define (extract-directory! archive key target-path) |
---|
970 | (fold-sexpr-stream archive key 'd 'di |
---|
971 | (lambda (dirent acc) |
---|
972 | (extract-object! archive dirent target-path) |
---|
973 | (void)) |
---|
974 | '())) |
---|
975 | |
---|
976 | ;; SINGLE SEXPRS |
---|
977 | ;; A sexpr in a block. Simple, really. |
---|
978 | ;; Given an sexpr, a type and a list of (key . reused?) pairs, returns a key and a reused? flag. |
---|
979 | (define (store-sexpr! archive sexpr type keys) |
---|
980 | (let* ((data (blob->u8vector/shared (string->blob (with-output-to-string (lambda () (write sexpr)))))) |
---|
981 | (hash ((archive-hash archive) data type))) |
---|
982 | |
---|
983 | (if (archive-exists? archive hash) |
---|
984 | (values (reusing hash) #t) |
---|
985 | (begin |
---|
986 | (for-each (lambda (key) |
---|
987 | (if (cdr key) ; reused? |
---|
988 | (archive-link! archive (car key)))) |
---|
989 | keys) |
---|
990 | (archive-put! archive hash data type) |
---|
991 | (values (virgin hash) #f))))) |
---|
992 | |
---|
993 | (define (read-sexpr archive key) |
---|
994 | (let ((data (archive-get archive key))) |
---|
995 | (with-input-from-string |
---|
996 | (blob->string (u8vector->blob/shared data)) |
---|
997 | (lambda () |
---|
998 | (read))))) |
---|
999 | |
---|
1000 | ;; SNAPSHOT STORAGE |
---|
1001 | ;; A snapshot is a single block containing an alist |
---|
1002 | ;; Keys are 'ctime (in seconds since the epoch), |
---|
1003 | ;; 'contents (hash of root directory), |
---|
1004 | ;; 'hostname (name of host snapshotted) |
---|
1005 | ;; 'prefix (prefix of filesystem on host) |
---|
1006 | ;; 'notes (user-supplied notes) |
---|
1007 | ;; 'previous (hash of previous snapshot) |
---|
1008 | ;; Returns the snapshot's key. |
---|
1009 | (define (tag-snapshot! archive tag contents-key contents-reused? snapshot-properties) |
---|
1010 | (check-archive-writable archive) |
---|
1011 | (archive-lock-tag! archive tag) |
---|
1012 | (let ((previous (archive-tag archive tag)) |
---|
1013 | (snapshot |
---|
1014 | (append |
---|
1015 | (list |
---|
1016 | (cons 'mtime (current-seconds)) |
---|
1017 | (cons 'contents contents-key)) |
---|
1018 | snapshot-properties)) |
---|
1019 | (keys |
---|
1020 | (list ; We do not list the previous snapshot - since we are about to overwrite the tag that points to it, which would be a decrement. |
---|
1021 | (cons contents-key contents-reused?)))) |
---|
1022 | (if previous |
---|
1023 | (begin |
---|
1024 | (set! snapshot (cons |
---|
1025 | (cons 'previous previous) |
---|
1026 | snapshot)))) |
---|
1027 | (let-values (((snapshot-key snapshot-reused?) |
---|
1028 | (store-sexpr! archive snapshot 'snapshot keys))) |
---|
1029 | (archive-set-tag! archive tag snapshot-key) |
---|
1030 | (archive-unlock-tag! archive tag) |
---|
1031 | snapshot-key))) |
---|
1032 | |
---|
1033 | (define (fold-history archive snapshot-key kons knil) |
---|
1034 | (let ((snapshot (read-sexpr archive snapshot-key))) |
---|
1035 | (if (assq 'previous snapshot) |
---|
1036 | (kons snapshot-key snapshot |
---|
1037 | (fold-history archive (cdr (assq 'previous snapshot)) kons knil)) |
---|
1038 | (kons snapshot-key snapshot knil)))) |
---|
1039 | |
---|
1040 | ;; BRING IT ALL TOGETHER |
---|
1041 | |
---|
1042 | (define (snapshot-directory-tree! archive tag path props) |
---|
1043 | (check-archive-writable archive) |
---|
1044 | (let-values (((root-key root-reused?) |
---|
1045 | (call-with-context-support |
---|
1046 | (archive-global-directory-rules archive) |
---|
1047 | (lambda () (store-directory! archive path))))) |
---|
1048 | (tag-snapshot! archive tag root-key root-reused? |
---|
1049 | (append |
---|
1050 | (list |
---|
1051 | (cons 'hostname (get-host-name)) |
---|
1052 | (cons 'prefix path)) |
---|
1053 | props)))) |
---|
1054 | |
---|
1055 | (define (epochtime->string e) |
---|
1056 | (let ((localtime (seconds->local-time e))) |
---|
1057 | (string-append |
---|
1058 | (string-pad (number->string (+ 1900 (vector-ref localtime 5))) 4 #\0) |
---|
1059 | "-" |
---|
1060 | (string-pad (number->string (+ 1 (vector-ref localtime 4))) 2 #\0) |
---|
1061 | "-" |
---|
1062 | (string-pad (number->string (vector-ref localtime 3)) 2 #\0) |
---|
1063 | " " |
---|
1064 | (string-pad (number->string (vector-ref localtime 2)) 2 #\0) |
---|
1065 | ":" |
---|
1066 | (string-pad (number->string (vector-ref localtime 1)) 2 #\0) |
---|
1067 | ":" |
---|
1068 | (string-pad (number->string (vector-ref localtime 0)) 2 #\0)))) |
---|
1069 | |
---|
1070 | |
---|
1071 | ; If given '() as the directory-key, makes a list of all tags |
---|
1072 | ; If given '(tag . "tag-name"), makes a list of snapshots of that tag |
---|
1073 | ; If given a key, if that key points to a directory, makes a list of the contents of that directory |
---|
1074 | ; Either way, the list of results are folded into the provided kons and knil functions |
---|
1075 | ; kons is called with three arguments: a directory-key for the object, a directory entry in the usual format, and the accumulator. |
---|
1076 | (define (fold-archive-node archive directory-key kons knil) |
---|
1077 | (cond |
---|
1078 | ((null? directory-key) |
---|
1079 | ; List tags |
---|
1080 | (fold (lambda (tag acc) |
---|
1081 | (kons (cons 'tag tag) (list tag 'tag (cons 'current (archive-tag archive tag))) acc)) |
---|
1082 | knil (archive-all-tags archive))) |
---|
1083 | ((and (pair? directory-key) (eq? (car directory-key) 'tag)) |
---|
1084 | ; List a tag's snapshots |
---|
1085 | (let* ((tag (cdr directory-key)) |
---|
1086 | (current (archive-tag archive tag)) |
---|
1087 | (current-contents (read-sexpr archive current))) |
---|
1088 | (kons |
---|
1089 | (cdr (assq 'contents current-contents)) |
---|
1090 | (cons "current" (cons 'snapshot current-contents)) |
---|
1091 | (fold-history archive current |
---|
1092 | (lambda (key snapshot acc) |
---|
1093 | (kons |
---|
1094 | (cdr (assq 'contents snapshot)) |
---|
1095 | (append |
---|
1096 | (list (epochtime->string (cdr (assq 'mtime snapshot))) |
---|
1097 | 'snapshot) |
---|
1098 | snapshot) |
---|
1099 | acc)) |
---|
1100 | knil)))) |
---|
1101 | ((string? directory-key) |
---|
1102 | ; List directory |
---|
1103 | (fold-sexpr-stream archive directory-key 'd 'di |
---|
1104 | (lambda (dirent acc) |
---|
1105 | (let ((name (car dirent)) |
---|
1106 | (type (cadr dirent)) |
---|
1107 | (props (cddr dirent))) |
---|
1108 | (cond |
---|
1109 | ((eq? type 'file) |
---|
1110 | (kons #f dirent acc)) |
---|
1111 | ((eq? type 'dir) |
---|
1112 | (kons (cdr (assq 'contents props)) dirent acc)) |
---|
1113 | ((eq? type 'symlink) |
---|
1114 | (kons #f dirent acc)) |
---|
1115 | (else |
---|
1116 | (kons #f dirent acc))))) |
---|
1117 | knil))))) |
---|
1118 | |
---|