Skip to content

Commit f169082

Browse files
authored
Merge pull request #623 from talex5/kind
Add Eio.Path.{kind, is_file, is_directory}
2 parents 9256754 + e14ebca commit f169082

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

lib_eio/path.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ let stat ~follow t =
6969
let bt = Printexc.get_raw_backtrace () in
7070
Exn.reraise_with_context ex bt "examining %a" pp t
7171

72+
let kind ~follow t =
73+
try ((stat ~follow t).kind :> [File.Stat.kind | `Not_found])
74+
with Exn.Io (Fs.E Not_found _, _) -> `Not_found
75+
76+
let is_file t =
77+
kind ~follow:true t = `Regular_file
78+
79+
let is_directory t =
80+
kind ~follow:true t = `Directory
81+
7282
let with_open_in path fn =
7383
Switch.run @@ fun sw -> fn (open_in ~sw path)
7484

lib_eio/path.mli

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,29 @@ val read_dir : _ t -> string list
128128
129129
Note: The special Unix entries "." and ".." are not included in the results. *)
130130

131-
(** {2 Metadata} *)
131+
(** {1 Metadata} *)
132132

133133
val stat : follow:bool -> _ t -> File.Stat.t
134134
(** [stat ~follow t] returns metadata about the file [t].
135135
136136
If [t] is a symlink, the information returned is about the target if [follow = true],
137137
otherwise it is about the link itself. *)
138138

139+
val kind : follow:bool -> _ t -> [ File.Stat.kind | `Not_found ]
140+
(** [kind ~follow t] is the type of [t], or [`Not_found] if it doesn't exist.
141+
142+
@param follow If [true] and [t] is a symlink, return the type of the target rather than [`Symbolic_link]. *)
143+
144+
val is_file : _ t -> bool
145+
(** [is_file t] is [true] if [t] is a regular file, and [false] if it doesn't exist or has a different type.
146+
147+
[is_file t] is [kind ~follow:true t = `Regular_file]. *)
148+
149+
val is_directory : _ t -> bool
150+
(** [is_directory t] is [true] if [t] is a directory, and [false] if it doesn't exist or has a different type.
151+
152+
[is_directory t] is [kind ~follow:true t = `Directory]. *)
153+
139154
(** {1 Other} *)
140155

141156
val unlink : _ t -> unit

tests/fs.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ let try_rmdir path =
5959
let chdir path =
6060
traceln "chdir %S" path;
6161
Unix.chdir path
62-
63-
let assert_kind path kind =
64-
Path.with_open_in path @@ fun file ->
65-
assert ((Eio.File.stat file).kind = kind)
6662
```
6763

6864
# Basic test cases
@@ -224,7 +220,9 @@ You can remove a file using unlink:
224220
Path.save ~create:(`Exclusive 0o600) (cwd / "subdir/file2") "data2";
225221
try_read_file (cwd / "file");
226222
try_read_file (cwd / "subdir/file2");
223+
assert (Eio.Path.kind ~follow:true (cwd / "file") = `Regular_file);
227224
try_unlink (cwd / "file");
225+
assert (Eio.Path.kind ~follow:true (cwd / "file") = `Not_found);
228226
try_unlink (cwd / "subdir/file2");
229227
try_read_file (cwd / "file");
230228
try_read_file (cwd / "subdir/file2");
@@ -541,9 +539,9 @@ let try_stat path =
541539
let cwd = Eio.Stdenv.cwd env in
542540
Switch.run @@ fun sw ->
543541
try_mkdir (cwd / "stat_subdir");
544-
assert_kind (cwd / "stat_subdir") `Directory;
542+
assert (Eio.Path.is_directory (cwd / "stat_subdir"));
545543
try_write_file (cwd / "stat_reg") "kingbula" ~create:(`Exclusive 0o600);
546-
assert_kind (cwd / "stat_reg") `Regular_file;;
544+
assert (Eio.Path.is_file (cwd / "stat_reg"));
547545
+mkdir <cwd:stat_subdir> -> ok
548546
+write <cwd:stat_reg> -> ok
549547
- : unit = ()
@@ -565,13 +563,15 @@ Fstatat:
565563
try_stat (cwd / "..");
566564
Unix.symlink ".." "parent-symlink";
567565
try_stat (cwd / "parent-symlink");
566+
try_stat (cwd / "missing1" / "missing2");
568567
+mkdir <cwd:stat_subdir2> -> ok
569568
+<cwd:stat_subdir2> -> directory
570569
+<cwd:symlink> -> symbolic link / directory
571570
+<cwd:broken-symlink> -> symbolic link / Fs Not_found _
572571
+<cwd> -> directory
573572
+<cwd:..> -> Fs Permission_denied _
574573
+<cwd:parent-symlink> -> symbolic link / Fs Permission_denied _
574+
+<cwd:missing1/missing2> -> Fs Not_found _
575575
- : unit = ()
576576
```
577577

0 commit comments

Comments
 (0)