[Libguestfs] [PATCH 1/2] mllib: add and use read_first_line_from_file

Richard W.M. Jones rjones at redhat.com
Fri Jul 17 14:57:53 UTC 2015


On Fri, Jul 17, 2015 at 03:31:18PM +0200, Pino Toscano wrote:
> Move the read_password_from_file helper in Password to mllib with a more
> generic name, and use it in place of the former.
> 
> Also, use it in v2v instead of reading the whole file contaning a
> password: given that the documentation says that the whole content is
> used, there will not be newlines in the password file, so the behaviour
> will be preserved. The oly difference is that newline is no more an
> acceptable character for passwords, but that is a really unlikely
> (if not impossible at all) situation.
> ---
>  customize/password.ml  | 8 +-------
>  mllib/common_utils.ml  | 6 ++++++
>  mllib/common_utils.mli | 4 ++++
>  v2v/cmdline.ml         | 2 +-
>  4 files changed, 12 insertions(+), 8 deletions(-)
> 
> diff --git a/customize/password.ml b/customize/password.ml
> index d91c4b5..111240e 100644
> --- a/customize/password.ml
> +++ b/customize/password.ml
> @@ -60,7 +60,7 @@ and parse_selector_list orig_arg = function
>      let pw = parse_selector_list orig_arg rest in
>      { pw with pw_locked = true }
>    | [ "file"; filename ] ->
> -    { pw_password = Password (read_password_from_file filename);
> +    { pw_password = Password (read_first_line_from_file filename);
>        pw_locked = false }
>    | "password" :: password ->
>      { pw_password = Password (String.concat ":" password); pw_locked = false }
> @@ -71,12 +71,6 @@ and parse_selector_list orig_arg = function
>    | _ ->
>      error (f_"invalid password selector '%s'; see the man page") orig_arg
>  
> -and read_password_from_file filename =
> -  let chan = open_in filename in
> -  let password = input_line chan in
> -  close_in chan;
> -  password
> -
>  (* Permissible characters in a salt. *)
>  let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./"
>  
> diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
> index 083c5d5..f9e8996 100644
> --- a/mllib/common_utils.ml
> +++ b/mllib/common_utils.ml
> @@ -745,3 +745,9 @@ let last_part_of str sep =
>      let i = String.rindex str sep in
>      Some (String.sub str (i+1) (String.length str - (i+1)))
>    with Not_found -> None
> +
> +let read_first_line_from_file filename =
> +  let chan = open_in filename in
> +  let line = input_line chan in
> +  close_in chan;
> +  line
> diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
> index 550f37f..16834f7 100644
> --- a/mllib/common_utils.mli
> +++ b/mllib/common_utils.mli
> @@ -181,3 +181,7 @@ val guest_arch_compatible : string -> bool
>  
>  val last_part_of : string -> char -> string option
>  (** Return the last part of a string, after the specified separator. *)
> +
> +val read_first_line_from_file : string -> string
> +(** Read only the first line (i.e. until the first newline character)
> +    of a file. *)
> diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
> index 705051f..eaf57dc 100644
> --- a/v2v/cmdline.ml
> +++ b/v2v/cmdline.ml
> @@ -259,7 +259,7 @@ read the man page virt-v2v(1).
>      match password_file with
>      | None -> None
>      | Some filename ->
> -      let password = read_whole_file filename in
> +      let password = read_first_line_from_file filename in
>        Some password in
>  
>    (* Parsing of the argument(s) depends on the input mode. *)
> -- 
> 2.1.0

ACK.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list