Skip to content

Commit 9b12547

Browse files
vouillonOlivierNicole
authored andcommitted
Fs: additional functions
1 parent ab5aead commit 9b12547

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

compiler/lib/fs.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,29 @@ let read_file f =
4848
Bytes.unsafe_to_string s
4949
with e ->
5050
failwith (Printf.sprintf "Cannot read content of %s.\n%s" f (Printexc.to_string e))
51+
52+
let write_file ~name ~contents =
53+
let ch = open_out_bin name in
54+
output_string ch contents;
55+
close_out ch
56+
57+
let remove_file file = try Sys.remove file with Sys_error _ -> ()
58+
59+
let gen_file file f =
60+
let f_tmp =
61+
Filename.temp_file_name
62+
~temp_dir:(Filename.dirname file)
63+
(Filename.basename file)
64+
".tmp"
65+
in
66+
try
67+
let res = f f_tmp in
68+
remove_file file;
69+
Sys.rename f_tmp file;
70+
res
71+
with exc ->
72+
remove_file f_tmp;
73+
raise exc
74+
75+
let with_intermediate_file name f =
76+
Fun.protect ~finally:(fun () -> remove_file name) (fun () -> f name)

compiler/lib/fs.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ val find_in_path : string list -> string -> string option
2121
val absolute_path : string -> string
2222

2323
val read_file : string -> string
24+
25+
val write_file : name:string -> contents:string -> unit
26+
27+
val gen_file : string -> (string -> 'a) -> 'a
28+
29+
val with_intermediate_file : string -> (string -> 'a) -> 'a

0 commit comments

Comments
 (0)