Daily check-in.
[guestfs-browser.git] / slave.ml
index 94fce75..17f00b7 100644 (file)
--- a/slave.ml
+++ b/slave.ml
@@ -32,8 +32,11 @@ type 'a callback = 'a -> unit
 type command =
   | Exit_thread
   | Connect of string option * domain list callback
+  | Download_dir_find0 of source * string * string * unit callback
+  | Download_dir_tarball of source * string * download_dir_tarball_format * string * unit callback
+  | Download_file of source * string * string * unit callback
   | Open_domain of string * inspection_data callback
-  | Open_images of string list * inspection_data callback
+  | Open_images of (string * string option) list * inspection_data callback
   | Read_directory of source * string * direntry list callback
 
 and domain = {
@@ -71,27 +74,58 @@ and direntry = {
   dent_link : string;
 }
 
+and download_dir_tarball_format = Tar | TGZ | TXZ
+
 let rec string_of_command = function
   | Exit_thread -> "Exit_thread"
   | Connect (Some name, _) -> sprintf "Connect %s" name
   | Connect (None, _) -> "Connect NULL"
+  | Download_dir_find0 (src, remotedir, localfile, _) ->
+      sprintf "Download_dir_find0 (%s, %s, %s)"
+        (string_of_source src) remotedir localfile
+  | Download_dir_tarball (src, remotedir, format, localfile, _) ->
+      sprintf "Download_dir_tarball (%s, %s, %s, %s)"
+        (string_of_source src) remotedir
+        (string_of_download_dir_tarball_format format) localfile
+  | Download_file (src, remotefile, localfile, _) ->
+      sprintf "Download_file (%s, %s, %s)"
+        (string_of_source src) remotefile localfile
   | Open_domain (name, _) -> sprintf "Open_domain %s" name
   | Open_images (images, _) ->
-      sprintf "Open_images [%s]" (String.concat "; " images)
-  | Read_directory (OS { insp_root = root }, dir, _) ->
-      sprintf "Read_directory (OS %s, %s)" root dir
-  | Read_directory (Volume dev, dir, _) ->
-      sprintf "Read_directory (Volume %s, %s)" dev dir
+      sprintf "Open_images %s" (string_of_images images)
+  | Read_directory (src, dir, _) ->
+      sprintf "Read_directory (%s, %s)" (string_of_source src) dir
+
+and string_of_images images =
+  "[" ^
+    String.concat "; "
+    (List.map (function
+               | fn, None -> fn
+               | fn, Some format -> sprintf "%s (%s)" fn format)
+       images) ^ "]"
+
+and string_of_source = function
+  | OS { insp_root = root } ->
+      sprintf "OS %s" root
+  | Volume dev ->
+      sprintf "Volume %s" dev
+
+and string_of_download_dir_tarball_format = function
+  | Tar -> "Tar"
+  | TGZ -> "TGZ"
+  | TXZ -> "TXZ"
 
 let no_callback _ = ()
 
 let failure_hook = ref (fun _ -> ())
 let busy_hook = ref (fun _ -> ())
 let idle_hook = ref (fun _ -> ())
+let progress_hook = ref (fun _ -> ())
 
 let set_failure_hook cb = failure_hook := cb
 let set_busy_hook cb = busy_hook := cb
 let set_idle_hook cb = idle_hook := cb
+let set_progress_hook cb = progress_hook := cb
 
 (* Execute a function, while holding a mutex.  If the function
  * fails, ensure we release the mutex before rethrowing the
@@ -112,11 +146,11 @@ let q_lock = M.create ()
 let q_cond = Cond.create ()
 
 (* Send a command message to the slave thread. *)
-let send_to_slave cmd =
+let send_to_slave ?fail cmd =
   debug "sending message %s to slave thread ..." (string_of_command cmd);
   with_lock q_lock (
     fun () ->
-      Q.push cmd q;
+      Q.push (fail, cmd) q;
       Cond.signal q_cond
   )
 
@@ -128,10 +162,18 @@ let discard_command_queue () =
       q_discard := true
   )
 
-let connect uri cb = send_to_slave (Connect (uri, cb))
-let open_domain name cb = send_to_slave (Open_domain (name, cb))
-let open_images images cb = send_to_slave (Open_images (images, cb))
-let read_directory src path cb = send_to_slave (Read_directory (src, path, cb))
+let connect ?fail uri cb = send_to_slave ?fail (Connect (uri, cb))
+let download_dir_find0 ?fail src remotedir localfile cb =
+  send_to_slave ?fail (Download_dir_find0 (src, remotedir, localfile, cb))
+let download_dir_tarball ?fail src remotedir format localfile cb =
+  send_to_slave ?fail
+    (Download_dir_tarball (src, remotedir, format, localfile, cb))
+let download_file ?fail src remotefile localfile cb =
+  send_to_slave ?fail (Download_file (src, remotefile, localfile, cb))
+let open_domain ?fail name cb = send_to_slave ?fail (Open_domain (name, cb))
+let open_images ?fail images cb = send_to_slave ?fail (Open_images (images, cb))
+let read_directory ?fail src path cb =
+  send_to_slave ?fail (Read_directory (src, path, cb))
 
 (*----- Slave thread starts here -----*)
 
@@ -177,7 +219,7 @@ let rec loop () =
   debug "top of slave loop";
 
   (* Get the next command. *)
-  let cmd =
+  let fail, cmd =
     with_lock q_lock (
       fun () ->
         while Q.is_empty q do Cond.wait q_cond q_lock done;
@@ -191,11 +233,12 @@ let rec loop () =
      GtkThread.async !busy_hook ();
      execute_command cmd
    with exn ->
-     (* If a command or the callback fails, clear the command queue
-      * and run the failure hook in the main thread.
+     (* If the user provided an override ?fail parameter to the
+      * original call, call that, else call the global hook.
       *)
-     discard_command_queue ();
-     GtkThread.async !failure_hook exn
+     match fail with
+     | Some cb -> GtkThread.async cb exn
+     | None -> GtkThread.async !failure_hook exn
   );
 
   (* If there are no more commands in the queue, run the idle hook. *)
@@ -226,6 +269,35 @@ and execute_command = function
       let doms = List.sort ~cmp doms in
       callback_if_not_discarded cb doms
 
+  | Download_dir_find0 (src, remotedir, localfile, cb) ->
+      let g = get_g () in
+      with_mount_ro g src (
+        fun () ->
+          g#find0 remotedir localfile
+      );
+      callback_if_not_discarded cb ()
+
+  | Download_dir_tarball (src, remotedir, format, localfile, cb) ->
+      let g = get_g () in
+      let f = match format with
+        | Tar -> g#tar_out
+        | TGZ -> g#tgz_out
+        | TXZ -> g#txz_out
+      in
+      with_mount_ro g src (
+        fun () ->
+          f remotedir localfile
+      );
+      callback_if_not_discarded cb ()
+
+  | Download_file (src, remotefile, localfile, cb) ->
+      let g = get_g () in
+      with_mount_ro g src (
+        fun () ->
+          g#download remotefile localfile
+      );
+      callback_if_not_discarded cb ()
+
   | Open_domain (name, cb) ->
       let conn = get_conn () in
       let dom = D.lookup_by_name conn name in
@@ -305,18 +377,34 @@ and get_disk_images_from_xml xml =
     | _ :: rest -> source_of attr_name rest
   in
 
+  (* Look for <driver type=attr_val/> and return attr_val. *)
+  let rec format_of = function
+    | [] -> None
+    | Xml.Element ("driver", attrs, _) :: rest ->
+        (try Some (List.assoc "type" attrs)
+         with Not_found -> format_of rest)
+    | _ :: rest -> format_of rest
+  in
+
   (* Look for <disk> nodes and return the sources (block devices) of those. *)
   let blkdevs =
     List.filter_map (
       function
-      | Xml.Element ("disk", attrs, children) ->
-          (try
-             let typ = List.assoc "type" attrs in
-             if typ = "file" then source_of "file" children
-             else if typ = "block" then source_of "dev" children
-             else None
-           with
-             Not_found -> None)
+      | Xml.Element ("disk", attrs, disks) ->
+          let filename =
+            try
+              let typ = List.assoc "type" attrs in
+              if typ = "file" then source_of "file" disks
+              else if typ = "block" then source_of "dev" disks
+              else None
+            with
+              Not_found -> None in
+          (match filename with
+           | None -> None
+           | Some filename ->
+               let format = format_of disks in
+               Some (filename, format)
+          );
       | _ -> None
     ) devices in
   blkdevs
@@ -325,13 +413,15 @@ and get_disk_images_from_xml xml =
  * libguestfs handle, adds the disks, and launches the appliance.
  *)
 and open_disk_images images cb =
-  debug "opening disk image [%s]" (String.concat "; " images);
+  debug "opening disk image %s" (string_of_images images);
 
   close_g ();
   let g' = new Guestfs.guestfs () in
   g := Some g';
   let g = g' in
 
+  g#set_trace (trace ());
+
   (* Uncomment the next line to pass the verbose flag from the command
    * line through to libguestfs.  This is not generally necessary since
    * we are not so interested in debugging libguestfs problems at this
@@ -340,7 +430,21 @@ and open_disk_images images cb =
    *)
   (* g#set_verbose (verbose ());*)
 
-  List.iter g#add_drive_ro images;
+  (* Attach progress bar callback. *)
+  g#set_progress_callback (
+    fun proc_nr serial position total ->
+      debug "progress callback proc_nr=%d serial=%d posn=%Ld total=%Ld"
+        proc_nr serial position total;
+      GtkThread.async !progress_hook (position, total)
+  );
+
+  List.iter (
+    function
+    | filename, None ->
+        g#add_drive_opts ~readonly:true filename
+    | filename, Some format ->
+        g#add_drive_opts ~readonly:true ~format filename
+  ) images;
 
   g#launch ();