diff --git a/example/load.ml b/example/load.ml index 15edacd..c15b466 100644 --- a/example/load.ml +++ b/example/load.ml @@ -1,9 +1,16 @@ let ( / ) = Eio.Path.( / ) +let filter v = + let p = + Eio.Path.native_exn v |> String.split_on_char '/' + |> List.filter (fun s -> not (String.equal "" s)) + in + not @@ List.exists (function "_build" | ".git" -> true | _ -> false) p + let () = Eio_main.run @@ fun env -> Eio_mem.run @@ fun _ -> - let cwd = Eio.Stdenv.cwd env / "src" |> Eio_mem.Fs.load in + let cwd = Eio.Stdenv.cwd env |> Eio_mem.Fs.load ~filter in Eio.traceln "The following filesystem has been loaded into memory: %a..." Eio.Path.pp cwd; Eio_mem.Fs.tree ~depth:max_int Fmt.stdout cwd diff --git a/src/eio_mem.ml b/src/eio_mem.ml index 2fc4a03..fb029d2 100644 --- a/src/eio_mem.ml +++ b/src/eio_mem.ml @@ -121,7 +121,7 @@ module Fs = struct let dir = make_dir config in (dir, "/") - let load path = + let load ?(filter = fun _ -> true) path = Low_level.reset (); let start = Eio.Path.native_exn path in let stat = Eio.Path.stat ~follow:true path in @@ -131,17 +131,24 @@ module Fs = struct let stat = Eio.Path.stat ~follow:false v in match stat.kind with | `Regular_file -> - Eio.Path.with_open_out ~create:(`If_missing stat.perm) - (switch_filesystem v) - @@ fun o -> - Eio.Path.with_open_in v @@ fun i -> Eio.Flow.copy i o + if not (filter v) then () + else + Eio.Path.with_open_out ~create:(`If_missing stat.perm) + (switch_filesystem v) + @@ fun o -> + Eio.Path.with_open_in v @@ fun i -> Eio.Flow.copy i o | `Directory -> - Eio.Path.mkdirs ~exists_ok:true ~perm:stat.perm (switch_filesystem v); + if not (filter v) then () + else + Eio.Path.mkdirs ~exists_ok:true ~perm:stat.perm + (switch_filesystem v); let fnames = Eio.Path.read_dir v in List.iter (fun fname -> make_fs Eio.Path.(v / fname)) fnames | `Symbolic_link -> - let link_to = Eio.Path.read_link v in - Eio.Path.symlink ~link_to (switch_filesystem v) + if not (filter v) then () + else + let link_to = Eio.Path.read_link v in + Eio.Path.symlink ~link_to (switch_filesystem v) | _ -> Eio.traceln "Skipping %a" Eio.Path.pp v in make_fs path; diff --git a/src/eio_mem.mli b/src/eio_mem.mli index d4bd5c0..885e916 100644 --- a/src/eio_mem.mli +++ b/src/eio_mem.mli @@ -11,9 +11,15 @@ module Fs : sig val make : ?config:Config.t -> unit -> [ `Close | Eio.Fs.dir_ty ] Eio.Path.t (** Construct a fresh file-system using an optional configuration. *) - val load : Eio.Fs.dir_ty Eio.Path.t -> Eio.Fs.dir_ty Eio.Path.t + val load : + ?filter:(Eio.Fs.dir_ty Eio.Path.t -> bool) -> + Eio.Fs.dir_ty Eio.Path.t -> + Eio.Fs.dir_ty Eio.Path.t (** [load path] will reset the in-memory filesystem and fill it with the - directory tree at [path]. *) + directory tree at [path]. + + @param filter + Called on every path, if it returns [true] then this file is kept. *) val tree : depth:int -> _ Eio.Path.t Fmt.t (** A [tree]-like pretty printer. *)