[Libguestfs] [PATCH 1/2] mllib: add an hook to cleanup directories on exit

Richard W.M. Jones rjones at redhat.com
Thu Feb 20 13:08:40 UTC 2014


On Thu, Feb 20, 2014 at 11:53:16AM +0100, Pino Toscano wrote:
> Much similar to unlink_on_exit, but recursively cleaning directories.
> ---
>  mllib/common_utils.ml | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
> index 3943417..f49ede6 100644
> --- a/mllib/common_utils.ml
> +++ b/mllib/common_utils.ml
> @@ -386,6 +386,35 @@ let unlink_on_exit =
>        registered_handlers := true
>      )
>  
> +(* Remove a temporary directory on exit. *)
> +let rmdir_on_exit =
> +  let dirs = ref [] in
> +  let registered_handlers = ref false in
> +
> +  let rec unlink_dirs () =
> +    let rec recursive_rmdir fn =
> +      if Sys.is_directory fn then (

I suspect this will follow symlinks, so that if the temporary
directory contains a link like:

  /tmp/tmpdir/foo -> /

it will proceed to wipe out other bits of your filesystem.

An altogether easier, safety and faster way to do this is to do it
like supermin does:

https://github.com/libguestfs/supermin/blob/master/src/supermin_utils.ml#L91

Especially line 105 ff. but the rest of the function may be
interesting too in the context of patch 2/2.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW




More information about the Libguestfs mailing list