[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libguestfs] [PATCH 2/2] builder: support for download resume



On Wed, Mar 18, 2015 at 05:20:07PM +0100, Maros Zatko wrote:
> Partially downloaded file is not deleted on exit anymore.
> There is a check for partially downloaded image in cache directory
> based on its name. When found, download_to crafts appropriate
> options to continue its download.
> ---
>  builder/downloader.ml | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/builder/downloader.ml b/builder/downloader.ml
> index 8a23bdc..6e19ee4 100644
> --- a/builder/downloader.ml
> +++ b/builder/downloader.ml
> @@ -65,11 +65,11 @@ let rec download ~prog t ?template ?progress_bar ?(proxy = SystemProxy) uri =
>         * If not, download it.
>         *)
>        if not (Sys.file_exists filename) then
> -        download_to ~prog t ?progress_bar ~proxy uri filename;
> +        download_to ~prog t ?progress_bar ?continue:(Some true) ~proxy uri filename;

To expand on what Pino said:

?foo arguments are passed as options, ie. None or Some ..  The
function has (hidden, generated by the compiler) code which does
effectively:

  let foo = match ?foo with None -> (* default value *) | Some x -> x in

When calling if you write the argument as ~foo:true then the
compiler automatically translates that to 'Some true'.

>        (filename, false)
>  
> -and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
> +and download_to ~prog t ?(progress_bar = false) ?(continue = false) ~proxy uri filename =
>    let parseduri =
>      try URI.parse_uri uri
>      with Invalid_argument "URI.parse_uri" ->
> @@ -82,7 +82,6 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
>     * atomically rename it to the final filename.
>     *)
>    let filename_new = filename ^ "." ^ string_random8 () in
> -  unlink_on_exit filename_new;
>  
>    (match parseduri.URI.protocol with
>    | "file" ->
> @@ -115,11 +114,20 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
>      if bad_status_code status_code then
>        error (f_"failed to download %s: HTTP status code %s") uri status_code;
>  
> +    let cmd = sprintf "ls %s.* 2>/dev/null" filename in
> +    let lines = if continue
> +      then external_command ~prog ?ignore_error:(Some true) cmd
> +      else [] in
> +    let filename_new, continue_download = match List.length lines with
> +      | 0 -> filename_new, ""
> +      | _ -> List.hd lines, " -C -" in
> +
>      (* Now download the file. *)
> -    let cmd = sprintf "%s%s%s -g -o %s %s"
> +    let cmd = sprintf "%s%s%s%s -g -o %s %s"
>        outenv
>        t.curl
>        (if t.verbose then "" else if progress_bar then " -#" else " -s -S")
> +      continue_download
>        (quote filename_new) (quote uri) in
>      if t.verbose then printf "%s\n%!" cmd;
>      let r = Sys.command cmd in

I can't say I'm very happy to have partial downloads lying around in
the cache directory.

Let's see what Pino thinks of the updated patch however.

Rich.

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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]