Source file crunch.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
(*
 * Copyright (C) 2023 Thomas Leonard
 * Copyright (C) 2026 Memfs developers
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)
open! Hermetic
open Eio_fs_mem_core

type entry = Image.entry =
  | Dir of { path : string; perm : int }
  | File of { path : string; perm : int; contents : string }
  | Symlink of { path : string; target : string }

let find path entries =
  List.find_map
    (function
      | File f when String.equal f.path path -> Some f.contents | _ -> None)
    entries

let load = Image.load

let pp_ml ppf entries =
  Fmt.pf ppf "(* Generated by eio-fs-crunch. Do not edit. *)@.@.";
  Fmt.pf ppf "let entries : Eio_fs_mem.Crunch.entry list =@.";
  Fmt.pf ppf "  Eio_fs_mem.Crunch.@.";
  Fmt.pf ppf "    [@.";
  List.iter
    (function
      | Dir { path; perm } ->
          Fmt.pf ppf "      Dir { path = %S; perm = 0o%03o };@." path perm
      | File { path; perm; contents } ->
          Fmt.pf ppf "      File { path = %S; perm = 0o%03o; contents = %S };@."
            path perm contents
      | Symlink { path; target } ->
          Fmt.pf ppf "      Symlink { path = %S; target = %S };@." path target)
    entries;
  Fmt.pf ppf "    ]@.@.";
  Fmt.pf ppf
    "let load ?os ?clock () = Eio_fs_mem.Crunch.load ?os ?clock entries@."

let interface =
  "(* Generated by eio-fs-crunch. Do not edit. *)\n\n\
   val entries : Eio_fs_mem.Crunch.entry list\n\
   (** The embedded file tree. *)\n\n\
   val load :\n\
  \  ?os:[< `Linux | `Macos | `Windows ] ->\n\
  \  ?clock:_ Eio.Time.clock ->\n\
  \  unit ->\n\
  \  [ `Close | Eio.Fs.dir_ty ] Eio.Path.t\n\
   (** [load ~os ~clock ()] is the root of a new in-memory filesystem holding\n\
  \    {!entries}, as {!Eio_fs_mem.Crunch.load} makes it. *)\n"