Skip to content

Commit

Permalink
Support deleting symlinks in Lwt_io.with_temp_dir on Windows
Browse files Browse the repository at this point in the history
If a Windows symlink points to a directory, then rmdir is the correct
function to call. If it points to a file, then unlink is the correct
function. When pointing to a file, the symlink can also have the
read-only attribute, which makes it subject of the little chmod dance.
  • Loading branch information
MisterDA committed Oct 6, 2021
1 parent e12504e commit ce82439
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
the behavior of Lwt_io.with_temp_dir following symlinks to
directories on Win32. (#883, Antonin Décimo)

* Support deleting symlinks on Windows during cleanup of Lwt_io.with_temp_dir (#886, Antonin Décimo)

====== Additions ======

* Lwt_bytes.blit_from_string: string complement of Lwt_bytes.blit (#882, Hugo Heuzard).
Expand Down
15 changes: 10 additions & 5 deletions src/unix/lwt_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1509,11 +1509,16 @@ let rec delete_recursively directory =
Lwt.return ()
else
let path = Filename.concat directory entry in
Lwt_unix.lstat path >>= fun stat ->
if stat.Lwt_unix.st_kind = Lwt_unix.S_DIR then
delete_recursively path
else
unlink path
Lwt_unix.lstat path >>= fun {Lwt_unix.st_kind; _} ->
match st_kind with
| S_DIR -> delete_recursively path
| S_LNK when (Sys.win32 || Sys.cygwin) ->
Lwt_unix.stat path >>= fun {Lwt_unix.st_kind; _} ->
begin match st_kind with
| S_DIR -> Lwt_unix.rmdir path
| _ -> unlink path
end
| _ -> unlink path
end >>= fun () ->
Lwt_unix.rmdir directory

Expand Down

0 comments on commit ce82439

Please sign in to comment.