Skip to content
TurtleKitty edited this page May 21, 2019 · 5 revisions

sys.fs

This object exists to interact with the local file system.

read-from and write-to open the given pathname as a port. Passing an applicative to the with: option will pass the opened port to said procedure.
mv, cp, ln, rm, stat, pwd, cd, chroot, ls, mkdir, rmdir, are gateways to their Linux equivalents.
tmp behaves like mktmp; tmp-dir behaves like mktmp -d.
listen creates a unix socket listener. connect connects to a unix socket. socket-pair returns a pair of connected sockets.
exists? returns true if a file exists.
dir? returns true if the path is a directory.
symlink? returns true if the path is a symlink. For real.

(def file-w (sys.fs.write-to "foo.txt"))  ; #<output port "foo.txt">
(def file-r (sys.fs.read-from "foo.txt")) ; #<input port "foo.txt">
(def tmp sys.fs.tmp)                      ; "/tmp/tempb737.13031.tmp"

(def nu-tmp "/tmp/tmp.txt")
(def fbb "/tmp/foobarbaz.txt")

(sys.fs.write-to
    tmp
    with: (proc (p) (p.write '(foo bar baz)) p.flush))

 ; null

(sys.fs.read-from
    tmp
    with: (proc (p) p.to-text))

 ; "(foo bar baz)"

(sys.fs.stat tmp)

; #(vector 173155293 33188 1 1000 1000 13 1558138698
;          1558138698 1558138698 36 0 4096 8)

(sys.fs.mv tmp nu-tmp)           ; 13 (bytes transferred)
(sys.fs.cp nu-tmp fbb)           ; 13
(sys.fs.ln fbb "/tmp/fbb.txt")   ; null

(sys.fs.symlink? fbb)            ; false
(sys.fs.symlink? "/tmp/fbb.txt") ; true

(sys.fs.rm nu-tmp)         ; null
(sys.fs.rm fbb)            ; null
(sys.fs.rm "/tmp.fbb.txt") ; null

sys.fs.pwd  ; "/home/turtlekitty/dev/vaquero"
sys.fs.home ; "/home/turtlekitty"

(def t sys.fs.tmp-dir) ; "/tmp/tempcbec.4412"

(sys.fs.cd t)        ; null
(sys.fs.mkdir "foo") ; null

(def t-foo (cat t "foo" with: "/")) ; "/tmp/tempcbec.4412/foo"

(sys.fs.ls t)        ; ("foo")
(sys.fs.ls t-foo)    ; ()
(sys.fs.rmdir t-foo) ; null
(sys.fs.ls t)        ; ()

; Note:
; unix domain sockets are broken at the moment because
; the egg I used hasn't been migrated to Chicken 5

(def tmp-sock sys.fs.tmp)              ; "/tmp/temp2133.9373.tmp"
(def server (sys.fs.listen tmp-sock))  ; (fs-listener "/tmp/temp2133.9373.tmp")
(def client (sys.fs.connect tmp-sock)) ; (socket "/tmp/temp2133.9373.tmp")
(def s-sock server.accept)             ; (socket "/tmp/temp2133.9373.tmp")

(s-sock.say "Yo.")   ; null
client.read-line     ; "Yo."

(def socks sys.fs.socket-pair) ; ((socket ?) . (socket ?))

(socks.head.say "Hi!")  ; null
socks.tail.read-line    ; "Hi!"
Clone this wiki locally