File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,6 +55,18 @@ let () =
5555 iter_files "/etc" (fun s _ -> print_endline s)
5656*)
5757
58+ let mkdir_p ?(perm =0o755 ) path =
59+ let rec aux path =
60+ if Sys. file_exists path then begin
61+ if not (Sys. is_directory path) then
62+ Exn. fail " mkdir_p: %s exists but is not a directory" path
63+ end else begin
64+ aux (Filename. dirname path);
65+ try Unix. mkdir path perm with Unix. Unix_error (Unix. EEXIST, _ , _ ) -> ()
66+ end
67+ in
68+ aux path
69+
5870let save_as name ?(mode =0o644 ) f =
5971 (* not using make_temp_file cause same dir is needed for atomic rename *)
6072 let temp = Printf. sprintf " %s.save.%d.tmp" name (U. gettid () ) in
Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ val iter_files : string -> (string -> in_channel -> unit) -> unit
1616val open_out_append_bin : string -> out_channel
1717val open_out_append_text : string -> out_channel
1818
19+ (* * [mkdir_p ?perm path] creates directory [path] and all missing parent
20+ directories. Similar to [mkdir -p]. Raises [Failure] if [path] exists
21+ but is not a directory. Default [perm] is [0o755]. *)
22+ val mkdir_p : ?perm : Unix .file_perm -> string -> unit
23+
1924(* * [save_as filename ?mode f] is similar to
2025 [Control.with_open_file_bin] except that writing is done to a
2126 temporary file that will be renamed to [filename] after [f] has
You can’t perform that action at this time.
0 commit comments