Skip to content

Commit 2fa8987

Browse files
committed
mkdir, unlink and rmdir
1 parent 42d4d9e commit 2fa8987

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

lib_eio_windows/eio_windows_stubs.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ CAMLprim value caml_eio_windows_openat(value v_dirfd, value v_pathname, value v_
149149
CAMLreturn(caml_win32_alloc_handle(h));
150150
}
151151

152-
CAMLprim value caml_eio_windows_mkdirat(value v_fd, value v_path, value v_perm)
153-
{
154-
uerror("mkdirat is not supported on windows yet", Nothing);
155-
}
156-
157152
CAMLprim value caml_eio_windows_unlinkat(value v_dirfd, value v_pathname, value v_dir)
158153
{
159154
CAMLparam2(v_dirfd, v_pathname);

lib_eio_windows/low_level.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,10 @@ let openat ?dirfd ~sw path flags dis create =
207207
in_worker_thread (fun () -> eio_openat dirfd path Flags.Open.(flags + cloexec (* + nonblock *)) dis create)
208208
|> Fd.of_unix ~sw ~blocking:false ~close_unix:true
209209

210-
external eio_mkdirat : Unix.file_descr option -> string -> Unix.file_perm -> unit = "caml_eio_windows_mkdirat"
211-
212-
let mkdir ?dirfd ~mode path =
213-
with_dirfd "mkdirat" dirfd @@ fun dirfd ->
214-
in_worker_thread @@ fun () ->
215-
eio_mkdirat dirfd path mode
210+
let mkdir ?dirfd ~mode:_ path =
211+
Switch.run @@ fun sw ->
212+
let _ : Fd.t = openat ?dirfd ~sw path Flags.Open.(generic_write + synchronise) Flags.Disposition.(create) Flags.Create.(directory) in
213+
()
216214

217215
external eio_unlinkat : Unix.file_descr option -> string -> bool -> unit = "caml_eio_windows_unlinkat"
218216

lib_eio_windows/test/test_fs.ml

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ let ( / ) = Path.( / )
1010
let try_read_file path =
1111
match Path.load path with
1212
| s -> traceln "read %a -> %S" Path.pp path s
13-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
13+
| exception ex -> raise ex
1414

1515
let try_write_file ~create ?append path content =
1616
match Path.save ~create ?append path content with
1717
| () -> traceln "write %a -> ok" Path.pp path
18-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
18+
| exception ex -> raise ex
1919

2020
let try_mkdir path =
2121
match Path.mkdir path ~perm:0o700 with
2222
| () -> traceln "mkdir %a -> ok" Path.pp path
23-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
23+
| exception ex -> raise ex
2424

2525
let try_rename p1 p2 =
2626
match Path.rename p1 p2 with
2727
| () -> traceln "rename %a to %a -> ok" Path.pp p1 Path.pp p2
28-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
28+
| exception ex -> raise ex
2929

3030
let try_read_dir path =
3131
match Path.read_dir path with
3232
| names -> traceln "read_dir %a -> %a" Path.pp path Fmt.Dump.(list string) names
33-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
33+
| exception ex -> raise ex
3434

3535
let try_unlink path =
3636
match Path.unlink path with
3737
| () -> traceln "unlink %a -> ok" Path.pp path
38-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
38+
| exception ex -> raise ex
3939

4040
let try_rmdir path =
4141
match Path.rmdir path with
4242
| () -> traceln "rmdir %a -> ok" Path.pp path
43-
| exception ex -> traceln "@[<h>%a@]" Eio.Exn.pp ex
43+
| exception ex -> raise ex
4444

4545
let with_temp_file path fn =
4646
Fun.protect (fun () -> fn path) ~finally:(fun () -> Eio.Path.unlink path)
@@ -110,6 +110,91 @@ let test_append env () =
110110
Path.save ~create:`Never ~append:true test_file "2nd-write";
111111
Alcotest.(check string) "append" "1st-write-original2nd-write" (Path.load test_file)
112112

113+
let test_mkdir env () =
114+
let cwd = Eio.Stdenv.cwd env in
115+
try_mkdir (cwd / "subdir");
116+
try_mkdir (cwd / "subdir\\nested");
117+
let test_file = cwd / "subdir\\nested\\test-file" in
118+
Path.save ~create:(`Exclusive 0o600) test_file "data";
119+
Alcotest.(check string) "mkdir" "data" (Path.load test_file);
120+
Unix.unlink "subdir\\nested\\test-file";
121+
Unix.rmdir "subdir\\nested";
122+
Unix.rmdir "subdir"
123+
124+
let test_symlink _env () =
125+
(* TODO *)
126+
()
127+
128+
let test_unlink env () =
129+
let cwd = Eio.Stdenv.cwd env in
130+
Path.save ~create:(`Exclusive 0o600) (cwd / "file") "data";
131+
try_mkdir (cwd / "subdir");
132+
Path.save ~create:(`Exclusive 0o600) (cwd / "subdir\\file2") "data2";
133+
try_read_file (cwd / "file");
134+
try_read_file (cwd / "subdir\\file2");
135+
try_unlink (cwd / "file");
136+
try_unlink (cwd / "subdir\\file2");
137+
let () =
138+
try
139+
try_read_file (cwd / "file");
140+
failwith "file should not exist"
141+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
142+
in
143+
let () =
144+
try
145+
try_read_file (cwd / "subdir\\file2");
146+
failwith "file should not exist"
147+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
148+
in
149+
try_write_file ~create:(`Exclusive 0o600) (cwd / "subdir\\file2") "data2";
150+
(* Supposed to use symlinks here. *)
151+
try_unlink (cwd / "subdir\\file2");
152+
let () =
153+
try
154+
try_read_file (cwd / "subdir\\file2");
155+
failwith "file should not exist"
156+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
157+
in
158+
()
159+
160+
let try_failing_unlink env () =
161+
let cwd = Eio.Stdenv.cwd env in
162+
let () =
163+
try
164+
try_unlink (cwd / "missing");
165+
failwith "Expected not found!"
166+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
167+
in
168+
let () =
169+
try
170+
try_unlink (cwd / "..\\foo");
171+
failwith "Expected permission denied!"
172+
with Eio.Io (Eio.Fs.E (Permission_denied _), _) -> ()
173+
in
174+
()
175+
176+
let test_remove_dir env () =
177+
let cwd = Eio.Stdenv.cwd env in
178+
try_mkdir (cwd / "d1");
179+
try_mkdir (cwd / "subdir\\d2");
180+
try_read_dir (cwd / "d1");
181+
try_read_dir (cwd / "subdir\\d2");
182+
try_rmdir (cwd / "d1");
183+
try_rmdir (cwd / "subdir\\d2");
184+
let () =
185+
try
186+
try_read_dir (cwd / "d1");
187+
failwith "Expected not found"
188+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
189+
in
190+
let () =
191+
try
192+
try_read_dir (cwd / "subdir\\d2");
193+
failwith "Expected not found"
194+
with Eio.Io (Eio.Fs.E (Not_found _), _) -> ()
195+
in
196+
()
197+
113198
let tests env = [
114199
"create-write-read", `Quick, test_create_and_read env;
115200
"cwd-abs-path", `Quick, test_cwd_no_access_abs env;
@@ -118,4 +203,9 @@ let tests env = [
118203
"create-trunc", `Quick, test_trunc env;
119204
"create-empty", `Quick, test_empty env;
120205
"append", `Quick, test_append env;
206+
"mkdir", `Quick, test_mkdir env;
207+
"symlinks", `Quick, test_symlink env;
208+
"unlink", `Quick, test_unlink env;
209+
"failing-unlink", `Quick, try_failing_unlink env;
210+
"rmdir", `Quick, test_remove_dir env;
121211
]

0 commit comments

Comments
 (0)