Skip to content

Commit 0777723

Browse files
committed
implement native for windows
1 parent 77d8810 commit 0777723

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lib_eio_windows/fs.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,20 @@ end = struct
187187

188188
let pp f t = Fmt.string f (String.escaped t.label)
189189

190-
let native _t _path =
191-
failwith "TODO: Windows native"
190+
let native_internal t path =
191+
if Filename.is_relative path then (
192+
let p =
193+
if t.dir_path = "." then path
194+
else Filename.concat t.dir_path path
195+
in
196+
if p = "" then "."
197+
else if p = "." then p
198+
else if Filename.is_implicit p then ".\\" ^ p
199+
else p
200+
) else path
201+
202+
let native t path =
203+
Some (native_internal t path)
192204
end
193205
and Handler : sig
194206
val v : (Dir.t, [`Dir | `Close]) Eio.Resource.handler

lib_eio_windows/test/test_fs.ml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ let test_remove_dir env () =
264264
in
265265
()
266266

267+
let test_native env () =
268+
let cwd = Sys.getcwd () ^ "\\" in
269+
let test x =
270+
let native = Eio.Path.native x in
271+
let result =
272+
native |> Option.map @@ fun native ->
273+
if String.starts_with ~prefix:cwd native then
274+
".\\" ^ String.sub native (String.length cwd) (String.length native - String.length cwd)
275+
else native
276+
in
277+
traceln "%a -> %a" Eio.Path.pp x Fmt.(Dump.option string) result
278+
in
279+
test env#fs;
280+
test (env#fs / "\\");
281+
test (env#fs / "\\etc\\hosts");
282+
test (env#fs / ".");
283+
test (env#fs / "foo\\bar");
284+
test env#cwd;
285+
test (env#cwd / "..");
286+
let sub = env#cwd / "native-sub" in
287+
Eio.Path.mkdir sub ~perm:0o700;
288+
Eio.Path.with_open_dir sub @@ fun sub ->
289+
test sub;
290+
test (sub / "foo.txt");
291+
test (sub / ".");
292+
test (sub / "..");
293+
test (sub / "\\etc\\passwd")
294+
267295
let tests env = [
268296
"create-write-read", `Quick, test_create_and_read env;
269297
"cwd-abs-path", `Quick, test_cwd_no_access_abs env;
@@ -277,4 +305,5 @@ let tests env = [
277305
"unlink", `Quick, test_unlink env;
278306
"failing-unlink", `Quick, try_failing_unlink env;
279307
"rmdir", `Quick, test_remove_dir env;
280-
]
308+
"native", `Quick, test_native env;
309+
]

0 commit comments

Comments
 (0)