1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
open Testo_util
open Fpath_.Operators
module P = Promise
let with_open_temp_file ?contents ?get_random_key ?perms ?(persist = false)
?(prefix = "testo-") ?suffix ?temp_dir ?windows_binary
?windows_file_share_delete func =
let path, oc =
Create_temp_file.open_temp_file ?get_random_key ?perms ~prefix ?suffix
?temp_dir ?windows_binary ?windows_file_share_delete ()
in
P.protect
(fun () ->
(match contents with
| None -> ()
| Some data ->
output_string oc data;
flush oc);
func path oc)
~finally:(fun () ->
close_out_noerr oc;
(if not persist then
try if Sys.file_exists !!path then Sys.remove !!path with
| Sys_error _ -> ());
P.return ())
let with_temp_file ?contents ?get_random_key ?perms ?persist ?prefix ?suffix
?temp_dir ?windows_binary ?windows_file_share_delete func =
with_open_temp_file ?contents ?get_random_key ?perms ?persist ?prefix ?suffix
?temp_dir ?windows_binary ?windows_file_share_delete (fun path _oc ->
func path)