Source file law.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
open! Hermetic

type kind = Op.kind
type cls = Forwarded | Intercepted of kind | Synthesized | Unsupported
type iface = Dir | Read | Write | Dir_handler | File_handler
type t = { iface : iface; name : string; cls : cls; why : string }
type none = |

let absurd : none -> 'a = function _ -> .

(* The entries of an interface's operations are made where a module of the
   interface's signature defines them, over an empty type: an operation Eio
   adds to the signature leaves the module short of a value. *)
let operations =
  let entries = ref [] in
  let op iface name cls why v =
    entries := { iface; name; cls; why } :: !entries;
    v
  in
  let dir name cls why v = op Dir name cls why v
  and read name cls why v = op Read name cls why v
  and write name cls why v = op Write name cls why v in
  let module _ : Eio.Fs.Pi.DIR = struct
    type t = none

    let open_in =
      dir "open_in" (Intercepted `Open_read)
        "A fault refuses the open before the base opens, or swaps the name for \
         a link after; the file is wrapped."
        absurd

    let open_out =
      dir "open_out" (Intercepted `Open_write)
        "A fault refuses the open before the base opens, or swaps the name for \
         a link after; the file is wrapped."
        absurd

    let mkdir =
      dir "mkdir" (Intercepted `Change)
        "A fault refuses the change before the base makes it." absurd

    let open_subtree =
      dir "open_subtree" (Intercepted `Open_read)
        "A fault refuses the open before the base opens; the directory is \
         wrapped under the same schedule."
        absurd

    let read_dir =
      dir "read_dir" (Intercepted `Read_dir)
        "A fault refuses the listing before the base lists." absurd

    let with_dir_entries =
      dir "with_dir_entries" (Intercepted `Read_dir)
        "A fault refuses the listing before the base lists." absurd

    let stat =
      dir "stat" (Intercepted `Stat)
        "A fault refuses the stat before the base answers, or swaps the name \
         for a link after."
        absurd

    let unlink =
      dir "unlink" (Intercepted `Change)
        "A fault refuses the change before the base makes it." absurd

    let rmdir =
      dir "rmdir" (Intercepted `Change)
        "A fault refuses the change before the base makes it." absurd

    let rename =
      dir "rename" (Intercepted `Change)
        "A fault refuses the change before the base makes it; a target \
         directory that is a wrapper is unwrapped for the base."
        absurd

    let read_link =
      dir "read_link" Forwarded
        "Reading a link changes nothing and fails only as the base does." absurd

    let symlink =
      dir "symlink" (Intercepted `Change)
        "A fault refuses the change before the base makes it."
        (fun ~link_to:_ -> absurd)

    let chmod =
      dir "chmod" (Intercepted `Change)
        "A fault refuses the change before the base makes it." absurd

    let chown =
      dir "chown" (Intercepted `Change)
        "A fault refuses the change before the base makes it."
        (fun ~follow:_ ?uid:_ ?gid:_ -> absurd)

    let sync_dir =
      dir "sync_dir" (Intercepted `Sync)
        "A fault refuses the barrier before the base's barrier runs." absurd

    let pp =
      dir "pp" Forwarded "A path through the wrapper is named as the base's."
        (fun _ -> absurd)

    let native =
      dir "native" Forwarded
        "The base's native path, so a lock or a barrier taken natively reaches \
         the same file."
        absurd

    let split =
      dir "split" Synthesized
        "POSIX syntax, which no instance carries and every base here uses."
        (fun _ -> None)

    let join =
      dir "join" Synthesized
        "POSIX syntax, which no instance carries and every base here uses."
        (fun a _ -> a)
  end
  in
  let module Read : Eio.File.Pi.READ with type t = none = struct
    type t = none

    let read_methods =
      read "read_methods" Synthesized
        "None, so every read goes through single_read and its fault." []

    let single_read =
      read "single_read" (Intercepted `Read)
        "A fault refuses the read or shortens the buffer before the base reads."
        absurd

    let pread =
      read "pread" (Intercepted `Read)
        "A fault refuses the read or shortens the buffers before the base \
         reads."
        absurd

    let stat =
      read "stat" (Intercepted `Stat)
        "A fault refuses the stat before the base answers." absurd

    let seek =
      read "seek" Forwarded
        "The base moves its offset; the wrapper keeps the one it reports \
         operations at."
        absurd

    let close =
      read "close" Forwarded
        "The base closes, at most once, as it would; the switch the file was \
         opened in closes it too."
        absurd
  end
  in
  let module _ : Eio.File.Pi.WRITE = struct
    include Read

    let single_write =
      write "single_write" (Intercepted `Write)
        "A fault refuses the write, shortens it, or tears it after part \
         reached the base."
        absurd

    let copy =
      write "copy" Synthesized
        "Eio's simple copy through the wrapper's single_write, so each chunk \
         is a write a fault may reach."
        absurd

    let pwrite =
      write "pwrite" (Intercepted `Write)
        "A fault refuses the write, shortens it, or tears it after part \
         reached the base."
        absurd

    let sync =
      write "sync" (Intercepted `Sync)
        "A fault refuses the sync and tells the base it failed, before the \
         base syncs."
        absurd

    let truncate =
      write "truncate" (Intercepted `Truncate)
        "A fault refuses the truncation before the base truncates." absurd
  end
  in
  List.rev !entries

let dir_handler name cls why = { iface = Dir_handler; name; cls; why }
let file_handler name cls why = { iface = File_handler; name; cls; why }

let close_dir =
  dir_handler "close" Forwarded
    "The base closes, at most once, as it would; the switch the directory was \
     opened in closes it too."

let admin =
  dir_handler "Eio_fs_internal.Admin" Synthesized
    "The base's root rights, wrapped under the same schedule, for Crash to \
     read and rebuild the tree through."

let base_dir =
  dir_handler "Eio_fs_internal.Base_dir" Synthesized
    "The base, for this package's own code alone: a rename into a wrapper, and \
     eio-fs.unix's host descriptor."

let rebase =
  dir_handler "Eio_fs_internal.Rebase" Synthesized
    "The same wrapper, under the same schedule, over another base: \
     Eio_fs_mem.as_user runs it as another user."

let exclusive =
  dir_handler "Eio_fs.Sync.Rename.Exclusive" (Intercepted `Change)
    "A fault refuses the rename before the base makes it."

let process_local =
  dir_handler "Eio_fs.Sync.Process_local" Forwarded
    "The wrapper adds no opener, so who can open the files is the base's to \
     say."

let modeless =
  dir_handler "Eio_fs.Sync.Modeless" Forwarded
    "Whether modes are kept is the base's to say."

let dir_sync =
  dir_handler "Eio_fs.Sync.Dir_sync" (Intercepted `Sync)
    "A fault refuses the barrier before the base's barrier runs."

let dir_other =
  dir_handler "a base's own interface" Unsupported
    "Not carried over, as anything bound to the base steps around the \
     schedule: a wrapper over a host directory is no native directory a child \
     process starts in."

let base_file =
  file_handler "Eio_fs_internal.Base_file" Synthesized
    "The base, for this package's own code alone: eio-fs.unix takes the host \
     descriptor a lock is taken on from it."

let file_sync =
  file_handler "Eio_fs.Sync.File_sync" (Intercepted `Sync)
    "A fault refuses the barrier and tells the base it failed, before the \
     base's barrier runs; a file opened for reading answers it too, as fsync \
     does."

let sync_failed =
  file_handler "Eio_fs_internal.Sync_failed" Forwarded
    "A sync a wrapper above failed is told on to the base, so stacked wrappers \
     reach the power-loss model at the bottom."

let shutdown =
  file_handler "Eio.Flow.Pi.Shutdown" Unsupported
    "A file is no socket: neither the in-memory backend's handle nor \
     eio_posix's binds it."

(* Eio hides this one, so [binding] cannot name it. *)
let socket =
  file_handler "Eio.Net.Pi.Socket" Unsupported
    "A file is no socket: neither the in-memory backend's handle nor \
     eio_posix's binds it."

let read_only_write =
  file_handler "write side of a file opened for reading" Unsupported
    "A file opened for reading is no sink: a backend's handle binds the write \
     side and refuses each write with EBADF, where the wrapper binds none; it \
     syncs through File_sync."

let file_other =
  file_handler "a base's own interface" Unsupported
    "Not carried over, as anything bound to the base steps around the \
     schedule: a file opened through a wrapper hands out no host descriptor."

let handlers =
  [
    close_dir;
    admin;
    base_dir;
    rebase;
    exclusive;
    process_local;
    modeless;
    dir_sync;
    dir_other;
    base_file;
    file_sync;
    sync_failed;
    shutdown;
    socket;
    read_only_write;
    file_other;
  ]

let all = operations @ handlers

let get iface name =
  List.find (fun e -> e.iface = iface && String.equal e.name name) all

(* A file's Close is its READ close. *)
let binding (type a) iface (Eio.Resource.H (pi, _) : a Eio.Resource.binding) =
  match pi with
  | Eio.Fs.Pi.Dir -> `Interface Dir
  | Eio.File.Pi.Read -> `Interface Read
  | Eio.Flow.Pi.Source -> `Interface Read
  | Eio.File.Pi.Write -> `Interface Write
  | Eio.Flow.Pi.Sink -> `Interface Write
  | Eio.Resource.Close when iface = Dir_handler -> `Op close_dir
  | Eio.Resource.Close -> `Op (get Read "close")
  | Eio.Flow.Pi.Shutdown -> `Op shutdown
  | Eio_fs_internal.Admin -> `Op admin
  | Eio_fs_internal.Base_dir -> `Op base_dir
  | Eio_fs_internal.Rebase -> `Op rebase
  | Eio_fs_internal.Base_file -> `Op base_file
  | Eio_fs_internal.Sync_failed -> `Op sync_failed
  | Eio_fs_sync.Rename.Exclusive -> `Op exclusive
  | Eio_fs_sync.Process_local -> `Op process_local
  | Eio_fs_sync.Modeless -> `Op modeless
  | Eio_fs_sync.Dir_sync -> `Op dir_sync
  | Eio_fs_sync.File_sync -> `Op file_sync
  | _ -> `Other

let pp_iface ppf = function
  | Dir -> Fmt.string ppf "Eio.Fs.Pi.DIR"
  | Read -> Fmt.string ppf "Eio.File.Pi.READ"
  | Write -> Fmt.string ppf "Eio.File.Pi.WRITE"
  | Dir_handler -> Fmt.string ppf "directory handler"
  | File_handler -> Fmt.string ppf "file handler"

let pp_cls ppf = function
  | Forwarded -> Fmt.string ppf "forwarded"
  | Intercepted k -> Fmt.pf ppf "intercepted (%s)" (Op.kind_name k)
  | Synthesized -> Fmt.string ppf "synthesized"
  | Unsupported -> Fmt.string ppf "unsupported"

let pp ppf e = Fmt.pf ppf "%a %s" pp_iface e.iface e.name