Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Path.mkdirs #594

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib_eio/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ let mkdir ~perm ((t:#Fs.dir), path) =
let bt = Printexc.get_raw_backtrace () in
Exn.reraise_with_context ex bt "creating directory %a" pp (t, path)

let mkdirs ?(exists_ok=false) ~perm ((t:#Fs.dir), path) =
let separate = String.split_on_char (Filename.dir_sep.[0]) in
patricoferris marked this conversation as resolved.
Show resolved Hide resolved
let make_directory p =
try (
if exists_ok then
try t#mkdir ~perm p with Exn.Io ((Fs.E Fs.Already_exists _), _) -> ()
else
t#mkdir ~perm p
) with Exn.Io _ as ex ->
let bt = Printexc.get_raw_backtrace () in
Exn.reraise_with_context ex bt "creating directory %a" pp (t, p)
in
let rec loop acc = function
| [] -> ()
| x :: xs ->
let dir = Filename.concat acc x in
make_directory dir;
loop dir xs
in
loop "" (separate path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably more efficient to start by checking the direct parent. If that exists (which is the common) case, there's no need to check further up (see e.g. Python's makedirs or Bos's create).


let read_dir ((t:#Fs.dir), path) =
try List.sort String.compare (t#read_dir path)
with Exn.Io _ as ex ->
Expand Down
6 changes: 5 additions & 1 deletion lib_eio/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ val with_open_out :
(** {1 Directories} *)

val mkdir : perm:File.Unix_perm.t -> _ t -> unit
(** [mkdir ~perm t] creates a new directory [t] with permissions [perm]. *)
(** [mkdir ?exist_ok ~perm t] creates a new directory [t] with permissions [perm]. *)

val mkdirs : ?exists_ok:bool -> perm:File.Unix_perm.t -> _ t -> unit
(** Recursively create directories. If [exist_ok] is [false] (the default) then we raise
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists_ok should only affect the final directory, not the intermediate ones (most of which will exist).

{! Fs.Already_exists}. *)

val open_dir : sw:Switch.t -> _ t -> <dir; Flow.close> t
(** [open_dir ~sw t] opens [t].
Expand Down
46 changes: 32 additions & 14 deletions lib_eio_windows/test/test_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ let try_mkdir path =
| () -> traceln "mkdir %a -> ok" Path.pp path
| exception ex -> raise ex

let try_mkdirs ?exists_ok path =
match Path.mkdirs ?exists_ok path ~perm:0o700 with
| () -> traceln "mkdirs %a -> ok" Path.pp path
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex

let try_rename p1 p2 =
match Path.rename p1 p2 with
| () -> traceln "rename %a to %a -> ok" Path.pp p1 Path.pp p2
Expand Down Expand Up @@ -75,7 +80,7 @@ let test_exclusive env () =
Eio.traceln "fiest";
Path.save ~create:(`Exclusive 0o666) path "first-write";
Eio.traceln "next";
try
try
Path.save ~create:(`Exclusive 0o666) path "first-write";
Eio.traceln "nope";
failwith "Should have failed"
Expand All @@ -84,15 +89,15 @@ let test_exclusive env () =
let test_if_missing env () =
let cwd = Eio.Stdenv.cwd env in
let test_file = (cwd / "test-file") in
with_temp_file test_file @@ fun test_file ->
with_temp_file test_file @@ fun test_file ->
Path.save ~create:(`If_missing 0o666) test_file "1st-write-original";
Path.save ~create:(`If_missing 0o666) test_file "2nd-write";
Alcotest.(check string) "same contents" "2nd-write-original" (Path.load test_file)

let test_trunc env () =
let cwd = Eio.Stdenv.cwd env in
let test_file = (cwd / "test-file") in
with_temp_file test_file @@ fun test_file ->
with_temp_file test_file @@ fun test_file ->
Path.save ~create:(`Or_truncate 0o666) test_file "1st-write-original";
Path.save ~create:(`Or_truncate 0o666) test_file "2nd-write";
Alcotest.(check string) "same contents" "2nd-write" (Path.load test_file)
Expand Down Expand Up @@ -125,20 +130,33 @@ let test_mkdir env () =
Unix.rmdir "subdir\\nested";
Unix.rmdir "subdir"

let test_mkdirs env () =
let cwd = Eio.Stdenv.cwd env in
let nested = cwd / "subdir1" / "subdir2" / "subdir3" in
try_mkdirs nested;
let one_more = Path.(nested / "subdir4") in
(try
try_mkdirs one_more
with Eio.Io (Eio.Fs.E (Already_exists _), _) -> ());
try_mkdirs ~exists_ok:true one_more;
try
try_mkdirs (cwd / ".." / "outside")
with Eio.Io (Eio.Fs.E (Permission_denied _), _) -> ()

let test_symlink env () =
(*
(*
Important note: assuming that neither "another" nor
"to-subdir" exist, the following program will behave
differently if you don't have the ~to_dir flag.

With [to_dir] set to [true] we get the desired UNIX behaviour,
without it [Unix.realpath] will actually show the parent directory
of "another". Presumably this is because Windows distinguishes
between file symlinks and directory symlinks. Fun.
between file symlinks and directory symlinks. Fun.

{[ Unix.symlink ~to_dir:true "another" "to-subdir";
Unix.mkdir "another" 0o700;
print_endline @@ Unix.realpath "to-subdir" |}
print_endline @@ Unix.realpath "to-subdir" |}
*)
let cwd = Eio.Stdenv.cwd env in
try_mkdir (cwd / "sandbox");
Expand Down Expand Up @@ -186,13 +204,13 @@ let test_unlink env () =
try_unlink (cwd / "file");
try_unlink (cwd / "subdir\\file2");
let () =
try
try
try_read_file (cwd / "file");
failwith "file should not exist"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
in
let () =
try
try
try_read_file (cwd / "subdir\\file2");
failwith "file should not exist"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
Expand All @@ -201,7 +219,7 @@ let test_unlink env () =
(* Supposed to use symlinks here. *)
try_unlink (cwd / "subdir\\file2");
let () =
try
try
try_read_file (cwd / "subdir\\file2");
failwith "file should not exist"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
Expand All @@ -211,13 +229,13 @@ let test_unlink env () =
let try_failing_unlink env () =
let cwd = Eio.Stdenv.cwd env in
let () =
try
try
try_unlink (cwd / "missing");
failwith "Expected not found!"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
in
let () =
try
try
try_unlink (cwd / "..\\foo");
failwith "Expected permission denied!"
with Eio.Io (Eio.Fs.E (Permission_denied _), _) -> ()
Expand All @@ -233,13 +251,13 @@ let test_remove_dir env () =
try_rmdir (cwd / "d1");
try_rmdir (cwd / "subdir\\d2");
let () =
try
try
try_read_dir (cwd / "d1");
failwith "Expected not found"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
in
in
let () =
try
try
try_read_dir (cwd / "subdir\\d2");
failwith "Expected not found"
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
Expand Down
25 changes: 25 additions & 0 deletions tests/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ let try_mkdir path =
| () -> traceln "mkdir %a -> ok" Path.pp path
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex

let try_mkdirs ?exists_ok path =
match Path.mkdirs ?exists_ok path ~perm:0o700 with
| () -> traceln "mkdirs %a -> ok" Path.pp path
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex

let try_rename p1 p2 =
match Path.rename p1 p2 with
| () -> traceln "rename %a to %a -> ok" Path.pp p1 Path.pp p2
Expand Down Expand Up @@ -212,6 +217,26 @@ Creating directories with nesting, symlinks, etc:
- : unit = ()
```

# Mkdirs

Recursively creating directories with `mkdirs`.

```ocaml
# run @@ fun env ->
let cwd = Eio.Stdenv.cwd env in
let nested = cwd / "subdir1" / "subdir2" / "subdir3" in
try_mkdirs nested;
let one_more = Path.(nested / "subdir4") in
try_mkdirs one_more;
try_mkdirs ~exists_ok:true one_more;
try_mkdirs (cwd / ".." / "outside");;
+mkdirs <cwd:subdir1/subdir2/subdir3> -> ok
+Eio.Io Fs Already_exists _, creating directory <cwd:subdir1>
+mkdirs <cwd:subdir1/subdir2/subdir3/subdir4> -> ok
+Eio.Io Fs Permission_denied _, creating directory <cwd:..>
- : unit = ()
```

# Unlink

You can remove a file using unlink:
Expand Down
Loading