Version 0.1.2.
[guestfs-browser.git] / filetree.ml
index f252f2a..a007d97 100644 (file)
@@ -25,12 +25,18 @@ open Utils
 open DeviceSet
 
 open Filetree_type
+open Filetree_markup
 open Filetree_ops
 
 module G = Guestfs
 
 type t = Filetree_type.t
 
+(* Temporary directory for shared use by all instances of this widget,
+ * cleaned up when the program exits.
+ *)
+let tmpdir = tmpdir ()
+
 let rec create ~packing () =
   let view = GTree.view ~packing () in
   (*view#set_rules_hint true;*)
@@ -70,6 +76,7 @@ let rec create ~packing () =
   let renderer = GTree.cell_renderer_text [], ["markup", name_col] in
   let name_view = GTree.view_column ~title:"Filename" ~renderer () in
   name_view#set_resizable true;
+  name_view#set_sizing `AUTOSIZE;
   ignore (view#append_column name_view);
 
   let renderer = GTree.cell_renderer_text [`XALIGN 1.], ["markup", size_col] in
@@ -140,7 +147,9 @@ and button_press ({ model = model; view = view } as t) ev =
           let hdata = get_hdata t row in
           match hdata with
           | { content=(Loading | ErrorMessage _ | Info _) } -> None
-          | { content=(Top _ | Directory _ | File _) } -> Some (path, hdata)
+          | { content=(Top _ | Directory _ | File _ |
+                           TopWinReg _ | RegKey _ | RegValue _ ) } ->
+              Some (path, hdata)
       ) paths in
 
     (* Based on number of selected rows and what is selected, construct
@@ -234,75 +243,11 @@ and make_context_menu t paths =
 
   menu
 
-(* Mark up mode. *)
-let markup_of_mode mode =
-  let c =
-    if is_socket mode then 's'
-    else if is_symlink mode then 'l'
-    else if is_regular_file mode then '-'
-    else if is_block mode then 'b'
-    else if is_directory mode then 'd'
-    else if is_char mode then 'c'
-    else if is_fifo mode then 'p' else '?' in
-  let ru = if is_ru mode then 'r' else '-' in
-  let wu = if is_wu mode then 'w' else '-' in
-  let xu = if is_xu mode then 'x' else '-' in
-  let rg = if is_rg mode then 'r' else '-' in
-  let wg = if is_wg mode then 'w' else '-' in
-  let xg = if is_xg mode then 'x' else '-' in
-  let ro = if is_ro mode then 'r' else '-' in
-  let wo = if is_wo mode then 'w' else '-' in
-  let xo = if is_xo mode then 'x' else '-' in
-  let str = sprintf "%c%c%c%c%c%c%c%c%c%c" c ru wu xu rg wg xg ro wo xo in
-
-  let suid = is_suid mode in
-  let sgid = is_sgid mode in
-  let svtx = is_svtx mode in
-  if suid then str.[3] <- 's';
-  if sgid then str.[6] <- 's';
-  if svtx then str.[9] <- 't';
-
-  "<span color=\"#222222\" size=\"small\">" ^ str ^ "</span>"
-
-(* Mark up dates. *)
-let markup_of_date t =
-  (* Guestfs gives us int64's, we want float which is OCaml's
-   * equivalent of time_t.
-   *)
-  let t = Int64.to_float t in
-
-  let show_full_date () =
-    let tm = localtime t in
-    sprintf "<span color=\"#222222\" size=\"small\">%04d-%02d-%02d %02d:%02d:%02d</span>"
-      (tm.tm_year + 1900) (tm.tm_mon + 1) tm.tm_mday
-      tm.tm_hour tm.tm_min tm.tm_sec
-  in
-
-  (* How long ago? *)
-  let now = time () in
-  let ago = now -. t in
-  if ago < 0. then (* future *)
-    show_full_date ()
-  else if ago < 60. then
-    "<small>now</small>"
-  else if ago < 60. *. 60. then
-    sprintf "<small>%.0f minutes ago</small>" (ago /. 60.)
-  else if ago < 60. *. 60. *. 24. then
-    sprintf "<small>%.0f hours ago</small>" (ago /. 60. /. 60.)
-  else if ago < 60. *. 60. *. 24. *. 28. then
-    sprintf "<small>%.0f days ago</small>" (ago /. 60. /. 60. /. 24.)
-  else
-    show_full_date ()
-
-(* Mark up file sizes. *)
-let markup_of_size bytes =
-  sprintf "<small>%s</small>" (human_size bytes)
-
 let clear { model = model; hash = hash } =
   model#clear ();
   Hashtbl.clear hash
 
-let rec add ({ model = model; hash = hash } as t) name data =
+let rec add ({ model = model } as t) name data =
   clear t;
 
   (* Populate the top level of the filetree.  If there are operating
@@ -324,51 +269,103 @@ let rec add ({ model = model; hash = hash } as t) name data =
   (* Add top level left-over filesystems. *)
   DeviceSet.iter (add_top_level_vol t name) other_filesystems;
 
+  (* If it's Windows and registry files exist, create a node for
+   * each file.
+   *)
+  List.iter (
+    fun os ->
+      (match os.Slave.insp_winreg_SAM with
+       | Some filename ->
+           add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SAM" filename
+       | None -> ()
+      );
+      (match os.Slave.insp_winreg_SECURITY with
+       | Some filename ->
+           add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SECURITY"
+             filename
+       | None -> ()
+      );
+      (match os.Slave.insp_winreg_SOFTWARE with
+       | Some filename ->
+           add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SOFTWARE"
+             filename
+       | None -> ()
+      );
+      (match os.Slave.insp_winreg_SYSTEM with
+       | Some filename ->
+           add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SYSTEM"
+             filename
+       | None -> ()
+      );
+      (match os.Slave.insp_winreg_DEFAULT with
+       | Some filename ->
+           add_top_level_winreg t name os "HKEY_USERS\\.DEFAULT" filename
+       | None -> ()
+      );
+  ) data.Slave.insp_oses;
+
   (* Expand the first top level node. *)
   match model#get_iter_first with
   | None -> ()
   | Some row ->
       t.view#expand_row (model#get_path row)
 
-and add_top_level_os ({ model = model; hash = hash } as t) name os =
+(* Add a top level operating system node. *)
+and add_top_level_os ({ model = model } as t) name os =
   let markup =
     sprintf "<b>%s</b>\n<small>%s</small>\n<small>%s</small>"
       (markup_escape name) (markup_escape os.Slave.insp_hostname)
       (markup_escape os.Slave.insp_product_name) in
 
   let row = model#append () in
-  make_node t row (Top (Slave.OS os));
+  make_node t row (Top (Slave.OS os)) None;
   model#set ~row ~column:t.name_col markup
 
-and add_top_level_vol ({ model = model; hash = hash } as t) name dev =
+(* Add a top level volume (left over filesystem) node. *)
+and add_top_level_vol ({ model = model } as t) name dev =
   let markup =
     sprintf "<b>%s</b>\n<small>from %s</small>"
       (markup_escape dev) (markup_escape name) in
 
   let row = model#append () in
-  make_node t row (Top (Slave.Volume dev));
+  make_node t row (Top (Slave.Volume dev)) None;
+  model#set ~row ~column:t.name_col markup
+
+(* Add a top level Windows Registry node. *)
+and add_top_level_winreg ({ model = model } as t) name os rootkey
+    remotefile =
+  let cachefile = tmpdir // string_of_int (unique ()) ^ ".hive" in
+
+  let markup =
+    sprintf "<b>%s</b>\n<small>from %s</small>"
+      (markup_escape rootkey) (markup_escape name) in
+
+  let row = model#append () in
+  make_node t row
+    (TopWinReg (Slave.OS os, rootkey, remotefile, cachefile)) None;
   model#set ~row ~column:t.name_col markup
 
 (* Generic function to make an openable node to the tree. *)
-and make_node ({ model = model; hash = hash } as t) row content =
-  let hdata = { state=NodeNotStarted; content=content; visited=false } in
+and make_node ({ model = model } as t) row content hiveh =
+  let hdata =
+    { state=NodeNotStarted; content=content; visited=false; hiveh=hiveh } in
   store_hdata t row hdata;
 
   (* Create a placeholder "loading ..." row underneath this node so
    * the user has something to expand.
    *)
   let placeholder = model#append ~parent:row () in
-  let hdata = { state=IsLeaf; content=Loading; visited=false } in
+  let hdata = { state=IsLeaf; content=Loading; visited=false; hiveh=None } in
   store_hdata t placeholder hdata;
   model#set ~row:placeholder ~column:t.name_col "<i>Loading ...</i>";
   ignore (t.view#connect#row_expanded ~callback:(expand_row t))
 
-and make_leaf ({ model = model; hash = hash } as t) row content =
-  let hdata = { state=IsLeaf; content=content; visited=false } in
+and make_leaf ({ model = model } as t) row content hiveh =
+  let hdata = { state=IsLeaf; content=content; visited=false; hiveh=hiveh } in
   store_hdata t row hdata
 
 (* This is called when the user expands a row. *)
-and expand_row ({ model = model; hash = hash } as t) row _ =
+and expand_row ({ model = model } as t) row _ =
   match get_hdata t row with
   | { state=NodeNotStarted; content=Top src } as hdata ->
       (* User has opened a top level node that was not previously opened. *)
@@ -396,10 +393,37 @@ and expand_row ({ model = model; hash = hash } as t) row _ =
       Slave.read_directory ~fail:(when_read_directory_fail t path)
         src pathname (when_read_directory t path)
 
+  | { state=NodeNotStarted;
+      content=TopWinReg (src, rootkey, remotefile, cachefile) } as hdata ->
+      (* User has opened a Windows Registry top level node
+       * not previously opened.
+       *)
+
+      (* Mark this row as loading. *)
+      hdata.state <- NodeLoading;
+
+      (* Get a stable path for this row. *)
+      let path = model#get_path row in
+
+      (* Since the user has opened this top level registry node for the
+       * first time, we now need to download the hive.
+       *)
+      Slave.download_file ~fail:(when_downloaded_registry_fail t path)
+        src remotefile cachefile (when_downloaded_registry t path)
+
+  | { state=NodeNotStarted; content=RegKey node } as hdata ->
+      (* User has opened a Windows Registry key node not previously opened. *)
+
+      (* Mark this row as loading. *)
+      hdata.state <- NodeLoading;
+
+      expand_hive_node t row node
+
+  (* Ignore when a user opens a node which is loading or has been loaded. *)
   | { state=(NodeLoading|IsNode) } -> ()
 
   (* These are not nodes so it should never be possible to open them. *)
-  | { content=File _ } | { state=IsLeaf } -> assert false
+  | { content=(File _ | RegValue _) } | { state=IsLeaf } -> assert false
 
   (* Node should not exist in the tree. *)
   | { state=NodeNotStarted; content=(Loading | ErrorMessage _ | Info _) } ->
@@ -418,9 +442,9 @@ and when_read_directory ({ model = model } as t) path entries =
         direntry in
       let row = model#append ~parent:row () in
       if is_directory stat.G.mode then
-        make_node t row (Directory direntry)
+        make_node t row (Directory direntry) None
       else
-        make_leaf t row (File direntry);
+        make_leaf t row (File direntry) None;
       model#set ~row ~column:t.name_col (markup_of_name direntry);
       model#set ~row ~column:t.mode_col (markup_of_mode stat.G.mode);
       model#set ~row ~column:t.size_col (markup_of_size stat.G.size);
@@ -455,7 +479,8 @@ and when_read_directory_fail ({ model = model } as t) path exn =
       let row = model#get_iter path in
       let row = model#iter_children ~nth:0 (Some row) in
 
-      let hdata = { state=IsLeaf; content=ErrorMessage msg; visited=false } in
+      let hdata =
+        { state=IsLeaf; content=ErrorMessage msg; visited=false; hiveh=None } in
       store_hdata t row hdata;
 
       model#set ~row ~column:t.name_col (markup_escape msg)
@@ -463,3 +488,91 @@ and when_read_directory_fail ({ model = model } as t) path exn =
   | exn ->
       (* unexpected exception: re-raise it *)
       raise exn
+
+(* Called when the top level registry node has been opened and the
+ * hive file was downloaded to the cache file successfully.
+ *)
+and when_downloaded_registry ({ model = model } as t) path () =
+  debug "when_downloaded_registry";
+  let row = model#get_iter path in
+
+  let hdata = get_hdata t row in
+  match hdata.content with
+  | TopWinReg (src, rootkey, remotefile, cachefile) ->
+      (try
+         (* Open the hive and save the hive handle in the row hdata. *)
+         let flags = if verbose () then [ Hivex.OPEN_VERBOSE ] else [] in
+         let h = Hivex.open_file cachefile flags in
+         hdata.hiveh <- Some h;
+
+         (* Continue as if expanding any other hive node. *)
+         let root = Hivex.root h in
+         expand_hive_node t row root
+       with
+         Hivex.Error _ as exn -> when_downloaded_registry_fail t path exn
+      )
+  | _ -> assert false
+
+(* Called instead of {!when_downloaded_registry} if the download failed. *)
+and when_downloaded_registry_fail ({ model = model } as t) path exn =
+  debug "when_downloaded_registry_fail: %s" (Printexc.to_string exn);
+
+  match exn with
+  | G.Error msg
+  | Hivex.Error (_, _, msg) ->
+      let row = model#get_iter path in
+      let row = model#iter_children ~nth:0 (Some row) in
+
+      let hdata =
+        { state=IsLeaf; content=ErrorMessage msg; visited=false; hiveh=None } in
+      store_hdata t row hdata;
+
+      model#set ~row ~column:t.name_col (markup_escape msg)
+
+  | exn ->
+      (* unexpected exception: re-raise it *)
+      raise exn
+
+(* Expand a hive node. *)
+and expand_hive_node ({ model = model } as t) row node =
+  debug "expand_hive_node";
+  let hdata = get_hdata t row in
+  let h = Option.get hdata.hiveh in
+
+  (* Read the hive entries (values, subkeys) at this node and add them
+   * to the tree.
+   *)
+  let values = Hivex.node_values h node in
+  let cmp v1 v2 = compare (Hivex.value_key h v1) (Hivex.value_key h v2) in
+  Array.sort cmp values;
+  Array.iter (
+    fun value ->
+      let row = model#append ~parent:row () in
+      make_leaf t row (RegValue value) (Some h);
+      model#set ~row ~column:t.name_col (markup_of_regvalue h value);
+      model#set ~row ~column:t.size_col (markup_of_regvaluesize h value);
+      model#set ~row ~column:t.date_col (markup_of_regvaluetype h value);
+  ) values;
+
+  let children = Hivex.node_children h node in
+  let cmp n1 n2 = compare (Hivex.node_name h n1) (Hivex.node_name h n2) in
+  Array.sort cmp children;
+  Array.iter (
+    fun node ->
+      let row = model#append ~parent:row () in
+      make_node t row (RegKey node) (Some h);
+      model#set ~row ~column:t.name_col (markup_of_regkey h node);
+  ) children;
+
+  (* Remove the placeholder "Loading" entry.  NB. Must be done AFTER
+   * adding the other entries, or else Gtk will unexpand the row.
+   *)
+  (try
+     let row = find_child_node_by_content t row Loading in
+     ignore (model#remove row)
+   with Invalid_argument _ | Not_found -> ()
+  );
+
+  (* The original entry has now been loaded, so update its state. *)
+  hdata.state <- IsNode;
+  set_visited t row