Skip to content

Commit

Permalink
Portably delete files on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahbeckford committed Nov 30, 2023
1 parent ffdaeb6 commit 94946c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/runtimescripts/dkml_runtimescripts.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
open Bos

let portable_delete_file target_fp =
let ( let* ) = Result.bind in
(* [doc from diskuvbox]
[tracks https://github.com/dbuenzli/bos/issues/98]
For Windows, can't write without turning off read-only flag.
In fact, you can still get Permission Denied even after turning
off read-only flag, perhaps because Windows has a richer
permissions model than POSIX. So we remove the file
after turning off read-only *)
if Sys.win32 then
let* exists = OS.File.exists target_fp in
if exists then
let* () = OS.Path.Mode.set target_fp 0o644 in
OS.File.delete target_fp
else Ok ()
else OS.File.delete target_fp

let extract_dkml_scripts ~dkmlversion dir_fp =
let ( let* ) = Result.bind in
let file_list_helper file_list read subdir_fp =
Expand Down
4 changes: 4 additions & 0 deletions src/runtimescripts/dkml_runtimescripts.mli
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
val extract_dkml_scripts :
dkmlversion:string -> Fpath.t -> (unit, Rresult.R.msg) result

(** [portable_delete_file] deletes the file even it is read-only on Windows.
Works around https://github.com/dbuenzli/bos/issues/98 *)
val portable_delete_file : Fpath.t -> (unit, Rresult.R.msg) result

0 comments on commit 94946c8

Please sign in to comment.