Skip to content

Commit 35ac3a6

Browse files
committed
Implement size and permissions using libuv
1 parent 05ca6cc commit 35ac3a6

3 files changed

Lines changed: 37 additions & 46 deletions

File tree

pixie/fs.pxi

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
(ns pixie.fs
22
(:require [pixie.path :as path]
33
[pixie.string :as string]
4-
[pixie.ffi-infer :as f]))
5-
6-
(f/with-config {:library "c"
7-
:cxx-flags ["-lc"]
8-
:includes ["sys/stat.h"]}
9-
(f/defc-raw-struct stat [:st_mode :st_size]))
10-
11-
(def stat-struct stat)
12-
13-
(f/with-config {:library "c"
14-
:cxx-flags ["-lc"]
15-
:includes ["sys/stat.h"]}
16-
(f/defcfn stat))
4+
[pixie.uv :as uv]))
175

186
(defprotocol IFSPath
197
(path [this]
@@ -96,16 +84,12 @@
9684
(defn- assert-existence [f]
9785
(assert (exists? f) (str "No file or directory at \"" (abs f) "\"")))
9886

99-
(defn- read-stat-field [path field]
100-
(assert-existence path)
101-
(let [s (stat-struct)
102-
_ (stat (abs path) s)
103-
result (field s)]
104-
(dispose! s)
105-
result))
87+
(uv/defuvfsfn fs-size pixie.uv/uv_fs_stat [file] :statbuf.st_size)
88+
(uv/defuvfsfn fs-mode pixie.uv/uv_fs_stat [file] :statbuf.st_mode)
10689

10790
(defn- size-of-path [path]
108-
(read-stat-field path :st_size))
91+
(assert-existence path)
92+
(fs-size (abs path)))
10993

11094
(defn- permission-string [n]
11195
(apply str
@@ -115,7 +99,8 @@
11599
(bit-shift-right masked shift)))))
116100

117101
(defn- permissions-of-path [path]
118-
(permission-string (read-stat-field path :st_mode)))
102+
(assert-existence path)
103+
(permission-string (fs-mode (abs path))))
119104

120105
;; File and Dir are just wrappers around paths.
121106
(deftype File [pathz]

pixie/io.pxi

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,10 @@
66
[pixie.ffi :as ffi]
77
[pixie.ffi-infer :as ffi-infer]))
88

9-
(defmacro defuvfsfn [nm args return]
10-
`(defn ~nm ~args
11-
(let [f (fn [k#]
12-
(let [cb# (atom nil)]
13-
(reset! cb# (ffi/ffi-prep-callback uv/uv_fs_cb
14-
(fn [req#]
15-
(try
16-
(st/run-and-process k# (~return (pixie.ffi/cast req# uv/uv_fs_t)))
17-
(uv/uv_fs_req_cleanup req#)
18-
(-dispose! @cb#)
19-
(catch e (println e))))))
20-
(~(symbol (str "pixie.uv/uv_" (name nm)))
21-
(uv/uv_default_loop)
22-
(uv/uv_fs_t)
23-
~@args
24-
@cb#)))]
25-
(st/call-cc f))))
26-
27-
(defuvfsfn fs_open [path flags mode] :result)
28-
(defuvfsfn fs_read [file bufs nbufs offset] :result)
29-
(defuvfsfn fs_write [file bufs nbufs offset] :result)
30-
(defuvfsfn fs_close [file] :result)
9+
(uv/defuvfsfn fs_open [path flags mode] :result)
10+
(uv/defuvfsfn fs_read [file bufs nbufs offset] :result)
11+
(uv/defuvfsfn fs_write [file bufs nbufs offset] :result)
12+
(uv/defuvfsfn fs_close [file] :result)
3113

3214

3315
(def DEFAULT-BUFFER-SIZE 1024)

pixie/uv.pxi

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns pixie.uv
2-
(:require [pixie.ffi-infer :as f]))
2+
(:require [pixie.ffi :as ffi]
3+
[pixie.ffi-infer :as f]))
34

45
(f/with-config {:library "uv"
56
:includes ["uv.h"]}
@@ -58,7 +59,9 @@
5859
:fs_type
5960
:path
6061
:result
61-
:ptr])
62+
:ptr
63+
:statbuf.st_size
64+
:statbuf.st_mode])
6265
(f/defcstruct uv_timespec_t [:tv_sec
6366
:tv_nsec])
6467
(f/defcstruct uv_stat_t [:st_dev
@@ -214,3 +217,24 @@
214217
(if (neg? result)
215218
(throw (str "UV Error: " (uv_err_name result)))
216219
result))
220+
221+
(defmacro defuvfsfn
222+
([nm args return]
223+
(defuvfsfn nm (symbol (str "pixie.uv/uv_" (name nm))) args return))
224+
([nm uv-fn args return]
225+
`(defn ~nm ~args
226+
(let [f (fn [k#]
227+
(let [cb# (atom nil)]
228+
(reset! cb# (ffi/ffi-prep-callback uv_fs_cb
229+
(fn [req#]
230+
(try
231+
(pixie.stacklets/run-and-process k# (~return (pixie.ffi/cast req# uv_fs_t)))
232+
(uv_fs_req_cleanup req#)
233+
(-dispose! @cb#)
234+
(catch e (println e))))))
235+
(~uv-fn
236+
(uv_default_loop)
237+
(uv_fs_t)
238+
~@args
239+
@cb#)))]
240+
(pixie.stacklets/call-cc f)))))

0 commit comments

Comments
 (0)