Skip to content
Snippets Groups Projects
Commit ab5201ff authored by Nikolai Kosmatov's avatar Nikolai Kosmatov Committed by Andre Maroneze
Browse files

[extlib] add recursive mkdir function (fixes #425)

parent 0fd19e48
No related branches found
No related tags found
No related merge requests found
......@@ -325,7 +325,7 @@ struct
let mk_dir d =
try
Unix.mkdir d 0o755;
Extlib.mkdir ~parents:true d 0o755;
L.warning "creating %s directory `%s'" O.option_name d;
d
with Unix.Unix_error _ ->
......
......@@ -343,6 +343,18 @@ let try_finally ~finally f x =
hence interrupting the process, might not work: child processes still need to
run some daemons, such as [flush_all] which is registered by default. *)
let rec mkdir ?(parents=false) name perm =
try Unix.mkdir name perm
with
| Unix.Unix_error (Unix.ENOENT,_,_) when parents ->
let parent_name = Filename.dirname name in
if name <> parent_name then
begin
mkdir ~parents parent_name perm;
Unix.mkdir name perm
end
| e -> raise e
let pid = Unix.getpid ()
let safe_at_exit f =
at_exit
......
......@@ -331,6 +331,17 @@ val try_finally: finally:(unit -> unit) -> ('a -> 'b) -> 'a -> 'b
(** System commands *)
(* ************************************************************************* *)
val mkdir : ?parents:bool -> string -> Unix.file_perm -> unit
(** [mkdir ?parents name perm] creates directory [name] with permission
[perm]. If [parents] is true, recursively create parent directories
if needed. [parents] defaults to false.
Note that this function may create some of the parent directories
and then fail to create the children, e.g. if [perm] does not allow
user execution of the created directory. This will leave the filesystem
in a modified state before raising an exception.
@raise [Unix.Unix_error] if cannot create [name] or its parents.
@since Frama-C+dev *)
val safe_at_exit : (unit -> unit) -> unit
(** Register function to call with [Pervasives.at_exit], but only
for non-child process (fork). The order of execution is preserved
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment