Compare commits
2 Commits
9b492b52fe
...
df188a31f6
| Author | SHA1 | Date | |
|---|---|---|---|
| df188a31f6 | |||
| 0eb1282af7 |
69
palps.rkt
69
palps.rkt
@@ -9,6 +9,26 @@
|
|||||||
;; directory of the current user of the program
|
;; directory of the current user of the program
|
||||||
(define call-dir (make-parameter (string->path "/nobody")))
|
(define call-dir (make-parameter (string->path "/nobody")))
|
||||||
|
|
||||||
|
;; raised by assert-confined-to-call-dir when attemping to traverse
|
||||||
|
;; outside of (call-dir)
|
||||||
|
(struct exn:fail:contract:illegal-path exn:fail:contract ()
|
||||||
|
#:extra-constructor-name make-exn:fail:contract:illegal-path
|
||||||
|
#:transparent)
|
||||||
|
|
||||||
|
;; check if path, once resolved, is a child of base
|
||||||
|
(define (confined-path? base path)
|
||||||
|
(string-prefix?
|
||||||
|
(path->string (simplify-path (build-path base path)))
|
||||||
|
(path->string (simplify-path base))))
|
||||||
|
|
||||||
|
;; asserts that path is within (call-dir), and raises
|
||||||
|
;; exn:fail:contract:illegal-path otherwise.
|
||||||
|
(define (assert-confined-to-call-dir path)
|
||||||
|
(when (not (confined-path? (call-dir) path))
|
||||||
|
(raise (make-exn:fail:contract:illegal-path
|
||||||
|
"assert-confined-to-call-dir: traversed outside of root"
|
||||||
|
(current-continuation-marks)))))
|
||||||
|
|
||||||
(define (call-valid? call)
|
(define (call-valid? call)
|
||||||
(and
|
(and
|
||||||
(<= (string-length call) 9)
|
(<= (string-length call) 9)
|
||||||
@@ -75,26 +95,6 @@
|
|||||||
path])))
|
path])))
|
||||||
(loop))
|
(loop))
|
||||||
|
|
||||||
;; raised by assert-confined-to-call-dir when attemping to traverse
|
|
||||||
;; outside of (call-dir)
|
|
||||||
(struct exn:fail:contract:illegal-path exn:fail:contract ()
|
|
||||||
#:extra-constructor-name make-exn:fail:contract:illegal-path
|
|
||||||
#:transparent)
|
|
||||||
|
|
||||||
;; check if path, once resolved, is a child of base
|
|
||||||
(define (confined-path? base path)
|
|
||||||
(string-prefix?
|
|
||||||
(path->string (simplify-path (build-path base path)))
|
|
||||||
(path->string (simplify-path base))))
|
|
||||||
|
|
||||||
;; asserts that path is within (call-dir), and raises
|
|
||||||
;; exn:fail:contract:illegal-path otherwise.
|
|
||||||
(define (assert-confined-to-call-dir path)
|
|
||||||
(when (not (confined-path? (call-dir) path))
|
|
||||||
(raise (make-exn:fail:contract:illegal-path
|
|
||||||
"assert-confined-to-call-dir: traversed outside of root"
|
|
||||||
(current-continuation-marks)))))
|
|
||||||
|
|
||||||
;; "main" procedure to be executed in an interactive context i.e. for
|
;; "main" procedure to be executed in an interactive context i.e. for
|
||||||
;; BPQ
|
;; BPQ
|
||||||
(define (session-startup)
|
(define (session-startup)
|
||||||
@@ -121,6 +121,11 @@
|
|||||||
(session))))
|
(session))))
|
||||||
|
|
||||||
(define (session)
|
(define (session)
|
||||||
|
(with-handlers
|
||||||
|
([exn:fail:contract:illegal-path?
|
||||||
|
(λ (exn)
|
||||||
|
(displayln "cannot traverse higher than root"))])
|
||||||
|
|
||||||
(display "palps > ")
|
(display "palps > ")
|
||||||
(flush-output)
|
(flush-output)
|
||||||
|
|
||||||
@@ -132,21 +137,21 @@
|
|||||||
(flush-output)]
|
(flush-output)]
|
||||||
|
|
||||||
[(list _ "ed" name)
|
[(list _ "ed" name)
|
||||||
(let ([path (build-path (current-directory)
|
(assert-confined-to-call-dir name)
|
||||||
(string->path-element name))])
|
|
||||||
(let ([path (if (file-exists? path)
|
(let ([path (if (file-exists? name)
|
||||||
(ed (our-make-temporary-file path))
|
(ed (our-make-temporary-file name))
|
||||||
(ed))])
|
(ed))])
|
||||||
(copy-file path name #:exists-ok? #t)))]
|
(copy-file path name #:exists-ok? #t))]
|
||||||
|
|
||||||
[(list _ "rm" name)
|
[(list _ "rm" name)
|
||||||
(let ([path (build-path (current-directory)
|
(assert-confined-to-call-dir name)
|
||||||
(string->path-element name))])
|
|
||||||
(if (file-exists? path)
|
(if (file-exists? name)
|
||||||
(delete-file path)
|
(delete-file name)
|
||||||
(displayln "file does not exist"))
|
(displayln "file does not exist"))
|
||||||
|
|
||||||
(flush-output))]
|
(flush-output)]
|
||||||
|
|
||||||
[(or (list _ "bye" _)
|
[(or (list _ "bye" _)
|
||||||
(list _ "b" _)
|
(list _ "b" _)
|
||||||
@@ -158,8 +163,8 @@
|
|||||||
|
|
||||||
[else
|
[else
|
||||||
(displayln "no such command")
|
(displayln "no such command")
|
||||||
(flush-output)])
|
(flush-output)]))
|
||||||
|
|
||||||
(session))
|
(session))
|
||||||
|
|
||||||
(session-startup)
|
;(session-startup)
|
||||||
|
|||||||
Reference in New Issue
Block a user