-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.lisp
70 lines (63 loc) · 2.3 KB
/
filesystem.lisp
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
68
69
70
(in-package :c-parse)
;;;;Make a subdirectory that mimics the real directories
(defparameter *cache* (merge-pathnames "shadowroot/" *path*))
(defun re-root-real-path (path &optional (base *cache*))
"change the root of path to base, making sure that therer can be no outside reference.
only works if path actually exists."
(let ((truename (uiop:truename* path)))
(unless truename
(error "does not exist ~s" path))
(unless (uiop:absolute-pathname-p path)
(error "not absolute:~s" path))
;;FIXME::hack?
(let ((base-directory (pathname-directory base))
(path-directory (pathname-directory truename)))
(make-pathname :directory
(append base-directory (rest path-directory))
:name (pathname-name truename)
:type (pathname-type truename)))))
(defun reroot (path &key (suffix "")
(prefix "")
(create nil))
(let ((reroot (re-root-real-path path)))
(ensure-directories-exist reroot)
;;if its a file, touch it
(let ((new (add-file-suffix suffix (add-file-prefix prefix reroot))))
(when create
(unless (uiop:directory-pathname-p new)
(touch-file new)))
new)))
(defparameter *touch-test-path* (merge-pathnames "touch.txt" *path*))
(defun touch-file (&optional (path *touch-test-path*))
(with-open-file (stream path :if-does-not-exist :create)))
;;be able to make a derived filename
#+nil
(defun pathname-name-and-type (&optional (path *touch-test-path*))
(let ((name (pathname-name path))
(type (pathname-type path)))
(if (or name type)
(concatenate-string
name
(if type
"."
nil)
type))))
(defun get-directory (&optional (path *testpath*))
(make-pathname :directory (pathname-directory path)))
(defun add-file-extension (extension-fun &optional (path *testpath*))
(let ((dir (get-directory path)))
(merge-pathnames
(make-pathname :name
(funcall extension-fun (pathname-name path))
:type (pathname-type path))
dir)))
;;(ADD-FILE-SUFFIX "~") lisp.h -> ~lisp.h
(defun add-file-suffix (suffix &optional (path *testpath*))
(add-file-extension (lambda (x)
(concatenate-string x suffix))
path))
;;(ADD-FILE-SUFFIX ".directive") lisp.h -> lisp.h.directive
(defun add-file-prefix (prefix &optional (path *testpath*))
(add-file-extension (lambda (x)
(concatenate-string prefix x))
path))