1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465(*
* 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!HermeticopenEio_fs_mem_coretypeEio.Exn.Backend.t+=Eio_fs=Low_level.Eio_fstypeuser=Low_level.User.t={uid:int64;gid:int64;groups:int64list;umask:int;}letroot=Low_level.User.rootletv=Memfs.vletas_user=Memfs.as_userletload?os?clock?filterpath=Image.load?os?clock(Image.scan?filter~at:(sndpath)path)moduleCrunch=Crunchtypestdenv=<fs:Eio.Fs.dir_tyEio.Path.t;cwd:Eio.Fs.dir_tyEio.Path.t>(* Root owns the tree and makes the current directory, which it gives to
[user]. *)letabsoluteoscwd=matchoswith|Some`Windows->Eio_utils.Nt_path.classifycwd=`Absolute|Some(`Linux|`Macos)|None->String.starts_with~prefix:"/"cwdletrun?os?(user=root)?clock?drives?cwdfn=lett=Low_level.v?clock?cwd?os?drives()inletcwd=Option.value~default:(Low_level.root_patht)cwdinifnot(absoluteoscwd)thenFmt.invalid_arg"Eio_fs_mem.run: cwd is %S, must be an absolute path"cwd;letadmin=(Eio.Path.of_dir(Memfs.fst):>Eio.Fs.dir_tyEio.Path.t)inEio.Path.mkdirs~exists_ok:true~perm:0o755Eio.Path.(admin/cwd);ifnot(Int64.equaluser.uid0L||Low_level.root_patht<>"/")thenEio.Path.chown~follow:true~uid:user.uid~gid:user.gidEio.Path.(admin/cwd);letfs=(Eio.Path.of_dir(Memfs.fs~usert):>Eio.Fs.dir_tyEio.Path.t)inletcwd=(Eio.Path.of_dir(Memfs.cwd~usert):>Eio.Fs.dir_tyEio.Path.t)in(* Resolve the current directory now: it then follows its directory. *)ignore(Eio.Path.stat~follow:truecwd:Eio.File.Stat.t);fnobjectmethodfs=fsmethodcwd=cwdend