Skip to content

Commit c8591bb

Browse files
committed
Consolidate updates to fs.md
1 parent 1234a0b commit c8591bb

File tree

7 files changed

+64
-39
lines changed

7 files changed

+64
-39
lines changed

lib_eio/path.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,18 @@ let symlink ~link_to source =
214214
let (Resource.T (dir, ops), path) = source in
215215
let module X = (val (Resource.get ops Fs.Pi.Dir)) in
216216
try X.symlink dir path ~link_to
217-
with Exn.Io _ as ex ->
218-
let bt = Printexc.get_raw_backtrace () in
219-
Exn.reraise_with_context ex bt "creating symlink %a -> %s" pp source link_to
220-
221-
let chmod ~follow ~perm t =
222-
let (Resource.T (dir, ops), path) = t in
223-
let module X = (val (Resource.get ops Fs.Pi.Dir)) in
224-
try X.chmod dir ~follow ~perm path
225-
with Exn.Io _ as ex ->
226-
let bt = Printexc.get_raw_backtrace () in
227-
Exn.reraise_with_context ex bt "chmoding file %a" pp t
217+
with Exn.Io _ as ex ->
218+
let bt = Printexc.get_raw_backtrace () in
219+
Exn.reraise_with_context ex bt "creating symlink %a -> %s" pp source link_to
220+
221+
let chmod ~follow ~perm t =
222+
let (Resource.T (dir, ops), path) = t in
223+
let module X = (val (Resource.get ops Fs.Pi.Dir)) in
224+
try
225+
X.chmod dir ~follow ~perm path
226+
with Exn.Io _ as ex ->
227+
let bt = Printexc.get_raw_backtrace () in
228+
Exn.reraise_with_context ex bt "chmoding file %a" pp t
228229

229230
let rec mkdirs ?(exists_ok=false) ~perm t =
230231
(* Check parent exists first. *)

lib_eio_linux/low_level.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,12 @@ let symlink ~link_to dir path =
486486
with_parent_dir "symlinkat-new" dir path @@ fun parent leaf ->
487487
try
488488
eio_symlinkat link_to parent leaf
489-
with Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap_fs code name arg
489+
with Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap_fs code name arg
490490

491491
let chmod ~follow ~perm dir path =
492492
let module X = Uring.Statx in
493493
with_parent_dir "chmodat" dir path @@ fun parent leaf ->
494-
let flags = if follow then 0 else (* at_symlink_nofollow *) 0x100 in
494+
let flags = if follow then 0 else 0x100 in
495495
try
496496
eio_fchmodat parent leaf perm flags
497497
with Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap_fs code name arg

lib_eio_posix/low_level.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ val symlink : link_to:string -> dir_fd -> string -> unit
8282
(** [symlink ~link_to dir path] will create a new symlink at [dir / path]
8383
linking to [link_to]. *)
8484

85-
val chmod : follow:bool -> mode:int -> dir_fd -> string -> unit
85+
val chmod : follow:bool -> mode:int -> dir_fd -> string -> unit
8686

8787
val readdir : dir_fd -> string -> string array
8888

lib_eio_windows/eio_windows_stubs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ CAMLprim value caml_eio_windows_symlinkat(value v_old_path, value v_new_fd, valu
257257
uerror("symlinkat is not supported on windows yet", Nothing);
258258
}
259259

260+
CAMLprim value eio_windows_chmod(value path, value perm) {
261+
caml_failwith("chmod is not implemented on Windows");
262+
}
260263

261264
CAMLprim value caml_eio_windows_spawn(value v_errors, value v_actions)
262265
{

lib_eio_windows/fs.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ end = struct
8989

9090
let v ~label ~sandbox dir_path = { dir_path; sandbox; label; closed = false }
9191

92+
let chmod (_t : t) ~(follow : bool) ~(perm : int) (_path : string) : unit =
93+
ignore (follow);
94+
ignore (perm);
95+
failwith "chmod not implemented on Windows yet"
96+
9297
(* Sandboxes use [O_NOFOLLOW] when opening files ([resolve] already removed any symlinks).
9398
This avoids a race where symlink might be added after [realpath] returns.
9499
TODO: Emulate [O_NOFOLLOW] here. *)

tests/fs.md

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ module Path = Eio.Path
1414
let () = Eio.Exn.Backend.show := false
1515
1616
open Eio.Std
17+
open Eio.Exn
1718
1819
let ( / ) = Path.( / )
1920
2021
let run ?clear:(paths = []) fn =
22+
Eio_main.run @@ fun env ->
2123
let cwd = Eio.Stdenv.cwd env in
2224
List.iter (fun p -> Eio.Path.rmtree ~missing_ok:true (cwd / p)) paths;
2325
fn env
@@ -76,12 +78,13 @@ let chdir path =
7678
traceln "chdir %S" path;
7779
Unix.chdir path
7880
79-
let try_stat path =
81+
let try_stat ?(info_type=`Kind) path =
8082
let stat ~follow =
81-
match Eio.Path.stat ~follow path with
82-
| info -> Fmt.str "@[<h>%a@]" Eio.File.Stat.pp_kind info.kind
83-
| exception Eio.Io (e, _) -> Fmt.str "@[<h>%a@]" Eio.Exn.pp_err e
84-
in
83+
match Eio.Path.stat ~follow path, info_type with
84+
| info, `Perm -> Fmt.str "@[<h>%o@]" info.perm
85+
| info, `Kind -> Fmt.str "@[<h>%a@]" Eio.File.Stat.pp_kind info.kind
86+
| exception Eio.Io (e, _) -> Fmt.str "@[<h>%a@]" Eio.Exn.pp_err e
87+
in
8588
let a = stat ~follow:false in
8689
let b = stat ~follow:true in
8790
if a = b then
@@ -94,10 +97,10 @@ let try_symlink ~link_to path =
9497
| s -> traceln "symlink %a -> %S" Path.pp path link_to
9598
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
9699
97-
let try_chmod ~perm path =
98-
match Path.chmod ~perm path with
99-
| () -> Eio.traceln "chmod %o -> %a" perm Path.pp path
100-
| exception ex -> Eio.traceln "@[<h>%a@]" Eio.Exn.pp ex
100+
let try_chmod path ~follow ~perm =
101+
match Eio.Path.chmod ~follow path ~perm with
102+
| () -> traceln "chmod %a to %o -> ok" Path.pp path perm
103+
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
101104
102105
```
103106

@@ -834,6 +837,7 @@ Unconfined:
834837
try_stat cwd;
835838
try_stat (cwd / "..");
836839
try_stat (cwd / "stat_subdir2/..");
840+
837841
Path.symlink ~link_to:".." (cwd / "parent-symlink");
838842
try_stat (cwd / "parent-symlink");
839843
try_stat (cwd / "missing1" / "missing2");
@@ -849,7 +853,6 @@ Unconfined:
849853
- : unit = ()
850854
```
851855

852-
853856
# read_link
854857

855858
```ocaml
@@ -1019,21 +1022,34 @@ Exception: Failure "Simulated error".
10191022
```
10201023

10211024
```ocaml
1022-
run ~clear:["stat_subdir2"; "symlink"; "broken-symlink"; "parent-symlink"] @@ fun env ->
1025+
# run ~clear:["test-file"] @@ fun env ->
10231026
let cwd = Eio.Stdenv.cwd env in
10241027
Switch.run @@ fun sw ->
1025-
Path.mkdir (cwd / "stat_subdir2");
1026-
Path.symlink ~link_to:"stat_subdir2" (cwd / "symlink");
1027-
Path.symlink ~link_to:"missing" (cwd / "broken-symlink");
1028-
try_stat (cwd / "stat_subdir2");
1029-
try_stat (cwd / "symlink");
1030-
try_stat (cwd / "broken-symlink");
1031-
try_chmod ~perm:0o777 (cwd / "stat_subdir2");
1032-
try_stat cwd;
1033-
try_stat (cwd / "..");
1034-
try_stat (cwd / "stat_subdir2/..");
1035-
Path.symlink ~link_to:".." (cwd / "parent-symlink");
1036-
try_stat (cwd / "parent-symlink");
1037-
try_stat (cwd / "missing1" / "missing2");
1038-
();
1028+
1029+
let file_path = cwd / "test-file" in
1030+
Path.save ~create:(`Exclusive 0o644) file_path "test data";
1031+
traceln "+create <cwd:test-file> with permissions 0o644 -> ok";
1032+
1033+
let initial_perm = (Path.stat ~follow:true file_path).perm in
1034+
traceln "+<cwd:test-file> initial permissions = %o" initial_perm;
1035+
assert (initial_perm = 0o644);
1036+
1037+
try_chmod ~follow:true ~perm:0o400 file_path;
1038+
1039+
try_stat ~info_type:`Perm file_path;
1040+
1041+
try_chmod ~follow:true ~perm:0o600 file_path;
1042+
try_stat ~info_type:`Perm file_path;
1043+
1044+
Eio.Path.unlink file_path;
1045+
traceln "+unlink <cwd:test-file> -> ok";
1046+
()
1047+
++create <cwd:test-file> with permissions 0o644 -> ok
1048+
++<cwd:test-file> initial permissions = 644
1049+
+chmod <cwd:test-file> to 400 -> ok
1050+
+<cwd:test-file> -> 400
1051+
+chmod <cwd:test-file> to 600 -> ok
1052+
+<cwd:test-file> -> 600
1053+
++unlink <cwd:test-file> -> ok
1054+
- : unit = ()
10391055
```

tmp/test_file

Whitespace-only changes.

0 commit comments

Comments
 (0)