From 47d54854e4e24d412db66822f4fa919a6351658c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 8 May 2018 16:58:38 +0700 Subject: [PATCH] Safety feature for Path.rm_rf --- src/stdune/path.ml | 4 ++++ test/unit-tests/path.mlt | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/stdune/path.ml b/src/stdune/path.ml index 96961419..3a907e6d 100644 --- a/src/stdune/path.ml +++ b/src/stdune/path.ml @@ -489,6 +489,10 @@ let rm_rf = Unix.rmdir dir in fun t -> + if not (is_local t) then ( + Exn.code_error "Path.rm_rf called on external dir" + ["t", sexp_of_t t] + ); let fn = to_string t in match Unix.lstat fn with | exception Unix.Unix_error(ENOENT, _, _) -> () diff --git a/test/unit-tests/path.mlt b/test/unit-tests/path.mlt index bc6b7ba0..a5fca503 100644 --- a/test/unit-tests/path.mlt +++ b/test/unit-tests/path.mlt @@ -161,5 +161,8 @@ Path.append (Path.of_string "/root") (Path.relative Path.build_dir "foo") Path.rm_rf (Path.of_string "/does/not/exist/foo/bar/baz") [%%expect{| -- : unit = () +Exception: Stdune__Exn.Code_error . +Raised at file "src/stdune/exn.ml", line 30, characters 37-131 +Called from file "src/stdune/path.ml", line 496, characters 4-127 +Called from file "toplevel/toploop.ml", line 180, characters 17-56 |}]