[Libguestfs] [PATCH 12/27] daemon: Reimplement ‘file_architecture’ API in OCaml.

Pino Toscano ptoscano at redhat.com
Wed Jul 19 13:20:19 UTC 2017


On Friday, 14 July 2017 15:39:20 CEST Richard W.M. Jones wrote:
> +(* Convert output from 'file' command on ELF files to the canonical
> + * architecture string.  Caller must free the result.

No more need to free anything, I guess?

> +and cpio_arch magic orig_path path =
> +  let sysroot = Sysroot.sysroot () in
> +
> +  let zcat =
> +    if String.find magic "gzip" >= 0 then "zcat"
> +    else if String.find magic "bzip2" >= 0 then "bzcat"
> +    else if String.find magic "XZ compressed" >= 0 then "xzcat"
> +    else "cat" in
> +
> +  let tmpdir = sprintf "/tmp/%s" (String.random8 ()) in
> +  mkdir tmpdir 0o700;

This could use Mkdtemp.temp_dir (from mlutils, Unix_utils).  Even if
there is not much advantage, at least it makes slightly easier to read
the code, and to discover where temporary directories are created.

> +  (* Construct a command to extract named binaries from the initrd file. *)
> +  let cmd =
> +    sprintf "cd %s && %s %s | cpio --quiet -id %s"
> +            tmpdir zcat (quote (sysroot // path))
> +            (String.concat " " (List.map quote initrd_binaries)) in
> +  if verbose () then eprintf "%s\n%!" cmd;
> +  if Sys.command cmd <> 0 then
> +    failwith "cpio command failed";
> +
> +  (* See if any of the binaries were present in the output. *)
> +  let rec loop = function
> +    | bin :: bins ->
> +       let bin_path = tmpdir // bin in
> +       if is_regular_file bin_path then (
> +         let out = command "file" ["-zb"; bin_path] in
> +         file_architecture_of_magic out orig_path bin_path
> +       )
> +       else
> +         loop bins
> +    | [] ->
> +       failwithf "could not determine architecture of cpio archive: %s" path
> +  in
> +  loop initrd_binaries

This must cleanup tmpdir, otherwise things will pile up and fill the
appliance.

> diff --git a/docs/C_SOURCE_FILES b/docs/C_SOURCE_FILES
> index 6149bba43..2dbd5a14b 100644
> --- a/docs/C_SOURCE_FILES
> +++ b/docs/C_SOURCE_FILES
> @@ -72,6 +72,7 @@ daemon/blkdiscard.c
>  daemon/blkid.c
>  daemon/blockdev.c
>  daemon/btrfs.c
> +daemon/caml-stubs.c
>  daemon/cap.c
>  daemon/checksum.c
>  daemon/cleanups.c
> @@ -82,6 +83,7 @@ daemon/compress.c
>  daemon/copy.c
>  daemon/cpio.c
>  daemon/cpmv.c
> +daemon/daemon-c.c
>  daemon/daemon.h
>  daemon/dd.c
>  daemon/debug-bmap.c
> @@ -173,6 +175,7 @@ daemon/stubs.h
>  daemon/swap.c
>  daemon/sync.c
>  daemon/syslinux.c
> +daemon/sysroot-c.c
>  daemon/tar.c
>  daemon/truncate.c
>  daemon/tsk.c
> @@ -296,7 +299,6 @@ lib/errors.c

The three hunks above should be part of patch #2.

-- 
Pino Toscano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20170719/3f50e250/attachment.sig>


More information about the Libguestfs mailing list