Source file irmin_fs_unix.ml
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
include Irmin.Export_for_backends
let src = Logs.Src.create "fs.unix" ~doc:"logs fs unix events"
module Log = (val Logs.src_log src : Logs.LOG)
module IO = struct
let mkdir_pool = Lwt_pool.create 1 (fun () -> Lwt.return_unit)
let mmap_threshold = 4096
let openfile_pool = Lwt_pool.create 200 (fun () -> Lwt.return_unit)
let protect_unix_exn = function
| Unix.Unix_error _ as e -> Lwt.fail (Failure (Printexc.to_string e))
| e -> Lwt.fail e
let ignore_enoent = function
| Unix.Unix_error (Unix.ENOENT, _, _) -> Lwt.return_unit
| e -> Lwt.fail e
let protect f x = Lwt.catch (fun () -> f x) protect_unix_exn
let safe f x = Lwt.catch (fun () -> f x) ignore_enoent
let mkdir dirname =
let rec aux dir =
if Sys.file_exists dir && Sys.is_directory dir then Lwt.return_unit
else
let clear =
if Sys.file_exists dir then (
[%log.debug "%s already exists but is a file, removing." dir];
safe Lwt_unix.unlink dir)
else Lwt.return_unit
in
clear >>= fun () ->
aux (Filename.dirname dir) >>= fun () ->
[%log.debug "mkdir %s" dir];
protect (Lwt_unix.mkdir dir) 0o755
in
Lwt_pool.use mkdir_pool (fun () -> aux dirname)
let file_exists f =
Lwt.catch
(fun () -> Lwt_unix.file_exists f)
(function
| Unix.Unix_error (Unix.ENOTDIR, _, _) -> Lwt.return_false
| e -> Lwt.fail e)
module Lock = struct
let is_stale max_age file =
Lwt.catch
(fun () ->
let+ s = Lwt_unix.stat file in
if s.Unix.st_mtime < 1.0 then false
else Unix.gettimeofday () -. s.Unix.st_mtime > max_age)
(function
| Unix.Unix_error (Unix.ENOENT, _, _) -> Lwt.return_false
| e -> Lwt.fail e)
let unlock file = Lwt_unix.unlink file
let lock ?(max_age = 10. *. 60. ) ?(sleep = 0.001) file =
let rec aux i =
[%log.debug "lock %s %d" file i];
let* is_stale = is_stale max_age file in
if is_stale then (
[%log.err "%s is stale, removing it." file];
unlock file >>= fun () -> aux 1)
else
let create () =
let pid = Unix.getpid () in
mkdir (Filename.dirname file) >>= fun () ->
let* fd =
Lwt_unix.openfile file
[ Unix.O_CREAT; Unix.O_RDWR; Unix.O_EXCL ]
0o600
in
let oc = Lwt_io.of_fd ~mode:Lwt_io.Output fd in
Lwt_io.write_int oc pid >>= fun () -> Lwt_unix.close fd
in
Lwt.catch create (function
| Unix.Unix_error (Unix.EEXIST, _, _) ->
let backoff =
1.
+. Random.float
(let i = float i in
i *. i)
in
Lwt_unix.sleep (sleep *. backoff) >>= fun () -> aux (i + 1)
| e -> Lwt.fail e)
in
aux 1
let with_lock file fn =
match file with
| None -> fn ()
| Some f -> lock f >>= fun () -> Lwt.finalize fn (fun () -> unlock f)
end
type path = string
type lock = path
let lock_file x = x
let file_exists = file_exists
let list_files kind dir =
if Sys.file_exists dir && Sys.is_directory dir then
let d = Sys.readdir dir in
let d = Array.to_list d in
let d = List.map (Filename.concat dir) d in
let d = List.filter kind d in
let d = List.sort String.compare d in
Lwt.return d
else Lwt.return_nil
let directories dir =
list_files (fun f -> try Sys.is_directory f with Sys_error _ -> false) dir
let files dir =
list_files
(fun f -> try not (Sys.is_directory f) with Sys_error _ -> false)
dir
let write_string fd b =
let rec rwrite fd buf ofs len =
let* n = Lwt_unix.write_string fd buf ofs len in
if len = 0 then Lwt.fail End_of_file
else if n < len then rwrite fd buf (ofs + n) (len - n)
else Lwt.return_unit
in
match String.length b with 0 -> Lwt.return_unit | len -> rwrite fd b 0 len
let delays = Array.init 20 (fun i -> 0.1 *. (float i ** 2.))
let command fmt =
Printf.ksprintf
(fun str ->
[%log.debug "[exec] %s" str];
let i = Sys.command str in
if i <> 0 then [%log.debug "[exec] error %d" i];
Lwt.return_unit)
fmt
let remove_dir dir =
if Sys.os_type = "Win32" then command "cmd /d /v:off /c rd /s /q %S" dir
else command "rm -rf %S" dir
let remove_file ?lock file =
Lock.with_lock lock (fun () ->
Lwt.catch
(fun () -> Lwt_unix.unlink file)
(function
| Unix.Unix_error (Unix.EACCES, _, _)
| Unix.Unix_error (Unix.EISDIR, _, _) ->
remove_dir file
| Unix.Unix_error (Unix.ENOENT, _, _) -> Lwt.return_unit
| e -> Lwt.fail e))
let rename =
if Sys.os_type <> "Win32" then Lwt_unix.rename
else fun tmp file ->
let rec aux i =
Lwt.catch
(fun () -> Lwt_unix.rename tmp file)
(function
| Unix.Unix_error (Unix.EACCES, _, _) as e ->
if i >= Array.length delays then Lwt.fail e
else
let* exists = file_exists file in
if exists && Sys.is_directory file then
remove_dir file >>= fun () -> aux (i + 1)
else (
[%log.debug "Got EACCES, retrying in %.1fs" delays.(i)];
Lwt_unix.sleep delays.(i) >>= fun () -> aux (i + 1))
| e -> Lwt.fail e)
in
aux 0
let with_write_file ?temp_dir file fn =
let* () =
match temp_dir with None -> Lwt.return_unit | Some d -> mkdir d
in
let dir = Filename.dirname file in
mkdir dir >>= fun () ->
let tmp = Filename.temp_file ?temp_dir (Filename.basename file) "write" in
Lwt_pool.use openfile_pool (fun () ->
[%log.debug "Writing %s (%s)" file tmp];
let* fd =
let open Lwt_unix in
openfile tmp [ O_WRONLY; O_NONBLOCK; O_CREAT; O_TRUNC ] 0o644
in
let* () =
Lwt.finalize (fun () -> protect fn fd) (fun () -> Lwt_unix.close fd)
in
rename tmp file)
let read_file_with_read file size =
let chunk_size = max 4096 (min size 0x100000) in
let buf = Bytes.create size in
let flags = [ Unix.O_RDONLY ] in
let perm = 0o0 in
let* fd = Lwt_unix.openfile file flags perm in
let rec aux off =
let read_size = min chunk_size (size - off) in
let* read = Lwt_unix.read fd buf off read_size in
let off = off + read in
if off >= size then Lwt.return (Bytes.unsafe_to_string buf) else aux off
in
Lwt.finalize (fun () -> aux 0) (fun () -> Lwt_unix.close fd)
let read_file_with_mmap file =
let fd = Unix.(openfile file [ O_RDONLY; O_NONBLOCK ] 0o644) in
let ba = Lwt_bytes.map_file ~fd ~shared:false () in
Unix.close fd;
Lwt.return (Lwt_bytes.to_string ba)
let read_file file =
Lwt.catch
(fun () ->
Lwt_pool.use openfile_pool (fun () ->
[%log.debug "Reading %s" file];
let* stats = Lwt_unix.stat file in
let size = stats.Lwt_unix.st_size in
let+ buf =
if size >= mmap_threshold then read_file_with_mmap file
else read_file_with_read file size
in
Some buf))
(function
| Unix.Unix_error _ | Sys_error _ -> Lwt.return_none | e -> Lwt.fail e)
let write_file ?temp_dir ?lock file b =
let write () =
with_write_file file ?temp_dir (fun fd -> write_string fd b)
in
Lock.with_lock lock (fun () ->
Lwt.catch write (function
| Unix.Unix_error (Unix.EISDIR, _, _) -> remove_dir file >>= write
| e -> Lwt.fail e))
let test_and_set_file ?temp_dir ~lock file ~test ~set =
Lock.with_lock (Some lock) (fun () ->
let* v = read_file file in
let equal =
match (test, v) with
| None, None -> true
| Some x, Some y -> String.equal x y
| _ -> false
in
if not equal then Lwt.return_false
else
let+ () =
match set with
| None -> remove_file file
| Some v -> write_file ?temp_dir file v
in
true)
let rec_files dir =
let rec aux accu dir =
let* ds = directories dir in
let* fs = files dir in
Lwt_list.fold_left_s aux (fs @ accu) ds
in
aux [] dir
end
module Append_only = Irmin_fs.Append_only (IO)
module Atomic_write = Irmin_fs.Atomic_write (IO)
include Irmin_fs.Maker (IO)
module KV = Irmin_fs.KV (IO)
module Append_only_ext = Irmin_fs.Append_only_ext (IO)
module Atomic_write_ext = Irmin_fs.Atomic_write_ext (IO)
module Maker_ext = Irmin_fs.Maker_ext (IO)
include Irmin_unix