From 0f31294fe7eb361f7bfe1b9c4bc8cb54e8ad7f41 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 20 Apr 2021 22:20:40 -0700 Subject: [PATCH] Move Path.rm_rf to Fpath Signed-off-by: Rudi Grinberg --- otherlibs/stdune-unstable/fpath.ml | 26 ++++++++++++++++++++++++++ otherlibs/stdune-unstable/fpath.mli | 4 ++++ otherlibs/stdune-unstable/path.ml | 26 ++------------------------ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/otherlibs/stdune-unstable/fpath.ml b/otherlibs/stdune-unstable/fpath.ml index 3a3a4a45846..48fba799874 100644 --- a/otherlibs/stdune-unstable/fpath.ml +++ b/otherlibs/stdune-unstable/fpath.ml @@ -90,3 +90,29 @@ let unlink = let unlink_no_err t = try unlink t with | _ -> () + +let rec clear_dir dir = + match Dune_filesystem_stubs.read_directory_with_kinds dir with + | Error ENOENT -> () + | Error error -> + raise + (Unix.Unix_error + (error, dir, "Stdune.Path.rm_rf: read_directory_with_kinds")) + | Ok listing -> + List.iter listing ~f:(fun (fn, kind) -> + let fn = Filename.concat dir fn in + match kind with + | Unix.S_DIR -> rm_rf_dir fn + | _ -> unlink fn) + +and rm_rf_dir path = + clear_dir path; + Unix.rmdir path + +let rm_rf ?(allow_external = false) fn = + if (not allow_external) && not (Filename.is_relative fn) then + Code_error.raise "Path.rm_rf called on external dir" [ ("fn", String fn) ]; + match Unix.lstat fn with + | exception Unix.Unix_error (ENOENT, _, _) -> () + | { Unix.st_kind = S_DIR; _ } -> rm_rf_dir fn + | _ -> unlink fn diff --git a/otherlibs/stdune-unstable/fpath.mli b/otherlibs/stdune-unstable/fpath.mli index f821930ae50..e59a47ad578 100644 --- a/otherlibs/stdune-unstable/fpath.mli +++ b/otherlibs/stdune-unstable/fpath.mli @@ -26,3 +26,7 @@ val unlink : string -> unit val unlink_no_err : string -> unit val initial_cwd : string + +val clear_dir : string -> unit + +val rm_rf : ?allow_external:bool -> string -> unit diff --git a/otherlibs/stdune-unstable/path.ml b/otherlibs/stdune-unstable/path.ml index 5d171961a3c..735fa941aa2 100644 --- a/otherlibs/stdune-unstable/path.ml +++ b/otherlibs/stdune-unstable/path.ml @@ -1142,34 +1142,12 @@ let insert_after_build_dir_exn = | External _ -> error a b -let rec clear_dir dir = - match Dune_filesystem_stubs.read_directory_with_kinds dir with - | Error ENOENT -> () - | Error error -> - raise - (Unix.Unix_error - (error, dir, "Stdune.Path.rm_rf: read_directory_with_kinds")) - | Ok listing -> - List.iter listing ~f:(fun (fn, kind) -> - let fn = Filename.concat dir fn in - match kind with - | Unix.S_DIR -> rm_rf_dir fn - | _ -> Fpath.unlink fn) - -and rm_rf_dir path = - clear_dir path; - Unix.rmdir path +let clear_dir dir = Fpath.clear_dir (to_string dir) let rm_rf ?(allow_external = false) t = if (not allow_external) && not (is_managed t) then Code_error.raise "Path.rm_rf called on external dir" [ ("t", to_dyn t) ]; - let fn = to_string t in - match Unix.lstat fn with - | exception Unix.Unix_error (ENOENT, _, _) -> () - | { Unix.st_kind = S_DIR; _ } -> rm_rf_dir fn - | _ -> Fpath.unlink fn - -let clear_dir dir = clear_dir (to_string dir) + Fpath.rm_rf ~allow_external (to_string t) let mkdir_p ?perms = function | External s -> External.mkdir_p s ?perms