Skip to content

Commit

Permalink
Merge pull request #690 from talex5/fd-bits
Browse files Browse the repository at this point in the history
Minor file-descriptor improvements
  • Loading branch information
talex5 authored Feb 14, 2024
2 parents 666c523 + 91ca880 commit 82c47a2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib_eio/unix/fd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ let is_seekable t =
t.seekable <- if seekable then Yes else No;
seekable

let is_open t = Rcfd.is_open t.fd

let rec use_exn_list op xs k =
match xs with
| [] -> k []
Expand Down
6 changes: 6 additions & 0 deletions lib_eio/unix/fd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ val remove : t -> Unix.file_descr option
Returns [None] if [t] is closed by another fiber first. *)

val is_open : t -> bool
(** [is_open t] returns [true] until [t] has been marked as closing, after which it returns [false].
This is mostly useful inside the callback of {!use}, to test whether
another fiber has started closing [t] (in which case you may decide to stop early). *)

(** {2 Flags} *)

val is_blocking : t -> bool
Expand Down
2 changes: 1 addition & 1 deletion lib_eio_posix/include/discover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let optional_flags = [

let () =
C.main ~name:"discover" (fun c ->
let c_flags = ["-D_LARGEFILE64_SOURCE"; "-D_XOPEN_SOURCE=700"; "-D_DARWIN_C_SOURCE"] in
let c_flags = ["-D_LARGEFILE64_SOURCE"; "-D_XOPEN_SOURCE=700"; "-D_DARWIN_C_SOURCE"; "-D_GNU_SOURCE"] in
let includes = ["sys/types.h"; "sys/stat.h"; "fcntl.h"] in
let extra_flags, missing_defs =
C.C_define.import c ~c_flags ~includes
Expand Down
41 changes: 40 additions & 1 deletion tests/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,45 @@ Create a sandbox, write a file with it, then read it from outside:
- : unit = ()
```

```ocaml
# run ~clear:["foo"] @@ fun env ->
let fs = env#fs in
let cwd = env#cwd in
Path.mkdirs (cwd / "foo/bar") ~perm:0o700;
let test ?(succeeds=true) path =
Eio.Exn.Backend.show := succeeds;
try
Switch.run @@ fun sw ->
let _ : _ Path.t = Path.open_dir ~sw path in
traceln "open_dir %a -> OK" Path.pp path
with ex ->
traceln "@[<h>%a@]" Eio.Exn.pp ex
in
let reject = test ~succeeds:false in
test (cwd / "foo/bar");
reject (cwd / "..");
test (cwd / ".");
reject (cwd / "/");
test (cwd / "foo/bar/..");
test (fs / "foo/bar");
Unix.symlink ".." "foo/up";
test (cwd / "foo/up/foo/bar");
Unix.symlink "/" "foo/root";
reject (cwd / "foo/root/..");
+open_dir <cwd:foo/bar> -> OK
+Eio.Io Fs Permission_denied _, opening directory <cwd:..>
+open_dir <cwd:.> -> OK
+Eio.Io Fs Permission_denied _, opening directory <cwd:/>
+open_dir <cwd:foo/bar/..> -> OK
+open_dir <fs:foo/bar> -> OK
+open_dir <cwd:foo/up/foo/bar> -> OK
+Eio.Io Fs Permission_denied _, opening directory <cwd:foo/root/..>
- : unit = ()
# Eio.Exn.Backend.show := false
- : unit = ()
```

# Unconfined FS access

We create a directory and chdir into it.
Expand Down Expand Up @@ -684,7 +723,7 @@ let try_rename t =
Confined:

```ocaml
# run ~clear:["tmp"; "dir"] @@ fun env -> try_rename env#cwd;;
# run ~clear:["tmp"; "dir"; "foo"] @@ fun env -> try_rename env#cwd;;
+mkdir <cwd:tmp> -> ok
+rename <cwd:tmp> to <cwd:dir> -> ok
+write <cwd:foo> -> ok
Expand Down

0 comments on commit 82c47a2

Please sign in to comment.