Add test target for figfs_test.
[figfs.git] / fuse / Fuse_lib.ml
blob846753ce798af1401726f74853984a505c953051
1 (*
2 This file is part of the "OCamlFuse" library.
4 OCamlFuse is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation (version 2 of the License).
8 OCamlFuse is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with OCamlFuse. See the file LICENSE. If you haven't received
15 a copy of the GNU General Public License, write to:
17 Free Software Foundation, Inc.,
18 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA
21 Vincenzo Ciancia
23 applejack@users.sf.net
24 vincenzo_ml@yahoo.it
27 open Fuse_bindings
28 open String
29 open Thread
30 open Result
32 let _ = Callback.register "ocaml_list_length" List.length
34 external is_null : 'a Com.opaque -> bool = "ocaml_fuse_is_null"
36 let undefined _ = raise (Unix.Unix_error (Unix.ENOSYS,"undefined",""))
38 let fuse_loop fuse (multithreaded) =
39 let f =
40 if multithreaded
41 then fun x y -> ignore (Thread.create x y)
42 else fun x y -> ignore (x y)
44 while not (__fuse_exited fuse) do
45 let cmd = __fuse_read_cmd fuse in
46 if not (is_null cmd)
47 then f (__fuse_process_cmd fuse) cmd
48 done
50 let _ = Callback.register "ocaml_fuse_loop" fuse_loop
52 let default_op_names = {
53 getattr = None;
54 readlink = None;
55 readdir = None;
56 opendir = None;
57 releasedir = None;
58 fsyncdir = None;
59 mknod = None;
60 mkdir = None;
61 unlink = None;
62 rmdir = None;
63 symlink = None;
64 rename = None;
65 link = None;
66 chmod = None;
67 chown = None;
68 truncate = None;
69 utime = None;
70 fopen = None;
71 read = None;
72 write = None;
73 statfs = None;
74 flush = None;
75 release = None;
76 fsync = None;
77 setxattr = None;
78 getxattr = None;
79 listxattr = None;
80 removexattr = None;
83 let start = ref 0
84 let supply () =
85 let r = !start in
86 start := !start + 1;
87 "__caml_cb_" ^ (string_of_int r)
89 let named_op f =
90 if f == undefined
91 then None
92 else
93 let cb x =
94 try Ok (f x)
95 with
96 Unix.Unix_error (err,_,_) -> Bad err
97 | _ -> Bad Unix.ERANGE (* TODO: find a better way to signal the user and log this *)
99 let name = supply () in
100 Callback.register name cb;
101 Some name
103 let named_op_2 f =
104 if f == undefined
105 then None
106 else
107 let cb x y =
108 try Ok (f x y)
109 with
110 Unix.Unix_error (err,_,_) -> Bad err
111 | _ -> Bad Unix.ERANGE in
112 let name = supply () in
113 Callback.register name cb;
114 Some name
116 let named_op_3 f =
117 if f == undefined
118 then None
119 else
120 let cb x y z =
121 try Ok (f x y z)
122 with
123 Unix.Unix_error (err,_,_) -> Bad err
124 | _ -> Bad Unix.ERANGE in
125 let name = supply () in
126 Callback.register name cb;
127 Some name
129 let named_op_4 f =
130 if f == undefined
131 then None
132 else
133 let cb x y z t =
134 try Ok (f x y z t)
135 with
136 Unix.Unix_error (err,_,_) -> Bad err
137 | _ -> Bad Unix.ERANGE in
138 let name = supply () in
139 Callback.register name cb;
140 Some name