Add Sys.force_remove

Sys.force_remove is Sys.remove, except on Windows, where it will remove
the "read-only" attribute and re-try a failed Sys.remove
This commit is contained in:
David Allsopp 2017-06-13 14:06:53 +02:00
parent 8dd66ed3d8
commit bf3fa0831d
3 changed files with 19 additions and 3 deletions

View File

@ -537,7 +537,7 @@ let rec exec t ~ectx ~dir ~env_extra ~stdout_to ~stderr_to =
return ()
| Create_file fn ->
let fn = Path.to_string fn in
if Sys.file_exists fn then Sys.remove fn;
if Sys.file_exists fn then Sys.force_remove fn;
Unix.close (Unix.openfile fn [O_CREAT; O_TRUNC; O_WRONLY] 0o666);
return ()
| Copy (src, dst) ->

View File

@ -225,7 +225,7 @@ module Temp = struct
let fns = !tmp_files in
tmp_files := String_set.empty;
String_set.iter fns ~f:(fun fn ->
try Sys.remove fn with _ -> ()))
try Sys.force_remove fn with _ -> ()))
let create prefix suffix =
let fn = Filename.temp_file prefix suffix in
@ -233,7 +233,7 @@ module Temp = struct
fn
let destroy fn =
Sys.remove fn;
Sys.force_remove fn;
tmp_files := String_set.remove fn !tmp_files
end

View File

@ -293,6 +293,22 @@ module String = struct
loop 0 0 ~last_is_cr:false
end
module Sys = struct
include Sys
let force_remove =
if win32 then
fun fn ->
try
remove fn
with Sys_error _ ->
(* Try to remove the "read-only" attribute, then retry. *)
(try Unix.chmod fn 0o666 with Unix.Unix_error _ -> ());
remove fn
else
remove
end
module Filename = struct
include Filename