-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesystem.rkt
67 lines (52 loc) · 1.81 KB
/
filesystem.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#lang racket/base
(require
racket/contract)
(provide
(contract-out
[path-on-disk? (-> path? boolean?)]
[recursive-file-list (->* () (directory-exists? procedure?) (listof path?))]
[transparent-filesystem-change-evt (-> path? evt?)]
[bulk-filesystem-change-evt (->* () ((listof path?)) evt?)]
[file-kind (-> path? symbol?)]
[ls (-> directory-exists? (listof path?))]))
;; ------------------------------------------------------------------
;; Implementation
(require
racket/file
racket/format
racket/sequence)
(define (path-on-disk? path)
(or (file-exists? path)
(directory-exists? path)
(link-exists? path)))
(define (recursive-file-list [path (current-directory)] [use-dir? (λ (p) #t)])
(sequence->list (in-directory path use-dir?)))
(define (transparent-filesystem-change-evt path)
(wrap-evt (filesystem-change-evt path) (λ (v) path)))
(define (bulk-filesystem-change-evt [targets (recursive-file-list)])
(apply choice-evt
(map transparent-filesystem-change-evt
targets)))
(define (file-kind path)
(cond
[(file-exists? path) 'file]
[(directory-exists? path) 'directory]
[(link-exists? path) 'link]
[else 'unsupported]))
(define (ls path)
(directory-list path #:build? #t))
(module+ test-lib
(require rackunit)
(provide
(contract-out
[create-temp-directory (-> path?)]
[create-file (->* (path-string?) (string?) void?)]))
(define (create-file path [data ""])
(with-output-to-file #:exists 'truncate
path
(lambda () (displayln data))))
(define (create-temp-directory)
(define p (make-temporary-file))
(delete-file p)
(make-directory p)
p))