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

Maros Zatko mzatko at redhat.com
Wed Mar 18 16:20:07 UTC 2015


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;
 
       (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
-- 
1.9.3




More information about the Libguestfs mailing list