Version 0.1.2.
[guestfs-browser.git] / filetree_ops.ml
index c273f30..5946a81 100644 (file)
@@ -19,8 +19,8 @@
 open Printf
 
 open Utils
-
 open Filetree_type
+open Filetree_markup
 
 (* Get the basename of a file, using path conventions which are valid
  * for libguestfs.  So [Filename.basename] won't necessarily work
@@ -58,13 +58,12 @@ let rec download_file ({ model = model } as t) path () =
           dlg#destroy ();
 
           (* Download the file. *)
-          update_status t
-            (sprintf "Downloading %s to %s ..." pathname localfile);
           Slave.download_file src pathname localfile
-            (when_downloaded_file t pathname localfile)
+            (when_downloaded_file t path)
 
-and when_downloaded_file t _ localfile () =
-  update_status t (sprintf "Finished downloading %s" localfile)
+and when_downloaded_file ({ model = model } as t) path () =
+  let row = model#get_iter path in
+  set_visited t row
 
 (* Download a directory as a tarball. *)
 let rec download_dir_tarball ({ model = model } as t) format path () =
@@ -95,13 +94,12 @@ let rec download_dir_tarball ({ model = model } as t) format path () =
           dlg#destroy ();
 
           (* Download the directory. *)
-          update_status t
-            (sprintf "Downloading %s to %s ..." pathname localfile);
           Slave.download_dir_tarball src pathname format localfile
-            (when_downloaded_dir_tarball t pathname localfile)
+            (when_downloaded_dir_tarball t path)
 
-and when_downloaded_dir_tarball t _ localfile () =
-  update_status t (sprintf "Finished downloading %s" localfile)
+and when_downloaded_dir_tarball ({ model = model } as t) path () =
+  let row = model#get_iter path in
+  set_visited t row
 
 let rec download_dir_find0 ({ model = model } as t) path () =
   let row = model#get_iter path in
@@ -136,10 +134,95 @@ let rec download_dir_find0 ({ model = model } as t) path () =
           dlg#destroy ();
 
           (* Download the directory. *)
-          update_status t
-            (sprintf "Downloading filenames in %s to %s ..." pathname localfile);
           Slave.download_dir_find0 src pathname localfile
-            (when_downloaded_dir_find0 t pathname localfile)
+            (when_downloaded_dir_find0 t path)
+
+and when_downloaded_dir_find0 ({ model = model } as t) path () =
+  let row = model#get_iter path in
+  set_visited t row
+
+let has_child_node_equals t row content =
+  try ignore (find_child_node_by_content t row content); true
+  with Not_found -> false
+
+(* Calculate disk space used by a directory. *)
+let rec disk_usage ({ model = model } as t) path () =
+  t.view#expand_row path;
+
+  let row = model#get_iter path in
+  let src, pathname = get_pathname t row in
+  debug "disk_usage %s" pathname;
+
+  (* See if this node already has an Info "disk_usage" child node.  If
+   * so they don't recreate it.
+   *)
+  let content = Info "disk_usage" in
+  if not (has_child_node_equals t row content) then (
+    (* Create the child node first. *)
+    let row = model#insert ~parent:row 0 in
+    let hdata = { state=IsLeaf; content=content; visited=false; hiveh=None } in
+    store_hdata t row hdata;
+    model#set ~row ~column:t.name_col "<i>Calculating disk usage ...</i>";
+
+    Slave.disk_usage src pathname (when_disk_usage t path pathname)
+  )
+
+and when_disk_usage ({ model = model } as t) path pathname kbytes =
+  let row = model#get_iter path in
+
+  (* Find the Info "disk_usage" child node add above, and replace the
+   * text in it with the final size.
+   *)
+  try
+    let content = Info "disk_usage" in
+    let row = find_child_node_by_content t row content in
+    let msg =
+      sprintf "<b>%s</b>\n<small>Disk usage of %s (%Ld KB)</small>"
+        (human_size_1k kbytes) pathname kbytes in
+    model#set ~row ~column:t.name_col msg
+  with
+    Not_found -> ()
 
-and when_downloaded_dir_find0 t _ localfile () =
-  update_status t (sprintf "Finished downloading %s" localfile)
+(* Display operating system inspection information. *)
+let display_inspection_data ({ model = model } as t) path () =
+  t.view#expand_row path;
+
+  let row = model#get_iter path in
+  let src, _ = get_pathname t row in
+  debug "display_inspection_data";
+
+  (* Should be an OS source, if not ignore. *)
+  match src with
+  | Slave.Volume _ -> ()
+  | Slave.OS os ->
+      (* See if this node already has an Info "inspection_data" child
+       * node.  If so they don't recreate it.
+       *)
+      let content = Info "inspection_data" in
+      if not (has_child_node_equals t row content) then (
+        let row = model#insert ~parent:row 0 in
+        let hdata =
+          { state=IsLeaf; content=content; visited=false; hiveh=None } in
+        store_hdata t row hdata;
+
+        (* XXX UGHLEE *)
+        let data =
+          sprintf "Type: <b>%s</b>\nDistro: <b>%s</b>\nVersion: <b>%d.%d</b>\nArch.: <b>%s</b>\nPackaging: <b>%s</b>/<b>%s</b>\n%sMountpoints:\n%s"
+            os.Slave.insp_type os.Slave.insp_distro
+            os.Slave.insp_major_version os.Slave.insp_minor_version
+            os.Slave.insp_arch
+            os.Slave.insp_package_management os.Slave.insp_package_format
+            (match os.Slave.insp_windows_systemroot with
+             | None -> ""
+             | Some path ->
+                 sprintf "%%systemroot%%: <b>%s</b>\n" (markup_escape path))
+            (String.concat "\n"
+               (List.map (
+                  fun (mp, dev) ->
+                    sprintf "<b>%s</b> on <b>%s</b>"
+                      (markup_escape dev) (markup_escape mp))
+                  os.Slave.insp_mountpoints)
+            ) in
+
+        model#set ~row ~column:t.name_col data
+      )