2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 module UTF8 = CamomileLibraryDefault.Camomile.UTF8
35 type t = Filetree_type.t
37 (* Temporary directory for shared use by any function in this file.
38 * It is cleaned up when the program exits.
40 let tmpdir = tmpdir ()
42 let rec create ~packing () =
43 let view = GTree.view ~packing () in
44 (*view#set_rules_hint true;*)
45 (*view#selection#set_mode `MULTIPLE; -- add this later *)
47 (* Hash of index numbers -> hdata. We do this because it's more
48 * efficient for the GC compared to storing OCaml objects directly in
51 let hash = Hashtbl.create 1023 in
53 (* The columns stored in each row. The hidden [index_col] column is
54 * an index into the hash table that records everything else about
55 * this row (see hdata above). The other display columns, eg.
56 * [name_col] contain Pango markup and thus have to be escaped.
58 let cols = new GTree.column_list in
60 let index_col = cols#add Gobject.Data.int in
62 let mode_col = cols#add Gobject.Data.string in
63 let name_col = cols#add Gobject.Data.string in
64 let size_col = cols#add Gobject.Data.string in
65 let date_col = cols#add Gobject.Data.string in
67 (* Create the model. *)
68 let model = GTree.tree_store cols in
70 (* Create the view. *)
71 view#set_model (Some (model :> GTree.model));
73 let renderer = GTree.cell_renderer_text [], ["markup", mode_col] in
74 let mode_view = GTree.view_column ~title:"Permissions" ~renderer () in
75 mode_view#set_resizable true;
76 ignore (view#append_column mode_view);
78 let renderer = GTree.cell_renderer_text [], ["markup", name_col] in
79 let name_view = GTree.view_column ~title:"Filename" ~renderer () in
80 name_view#set_resizable true;
81 name_view#set_sizing `AUTOSIZE;
82 ignore (view#append_column name_view);
84 let renderer = GTree.cell_renderer_text [`XALIGN 1.], ["markup", size_col] in
85 let size_view = GTree.view_column ~title:"Size" ~renderer () in
86 size_view#set_resizable true;
87 ignore (view#append_column size_view);
89 let renderer = GTree.cell_renderer_text [`XALIGN 1.], ["markup", date_col] in
90 let date_view = GTree.view_column ~title:"Date" ~renderer () in
91 date_view#set_resizable true;
92 ignore (view#append_column date_view);
95 view = view; model = model; hash = hash;
96 index_col = index_col;
97 mode_col = mode_col; name_col = name_col; size_col = size_col;
101 (* Open a context menu when a button is pressed. *)
102 ignore (view#event#connect#button_press ~callback:(button_press t));
106 (* Handle mouse button press on the selected row. This opens the
107 * pop-up context menu.
108 * http://scentric.net/tutorial/sec-selections-context-menus.html
110 and button_press ({ model = model; view = view } as t) ev =
111 let button = GdkEvent.Button.button ev in
112 let x = int_of_float (GdkEvent.Button.x ev) in
113 let y = int_of_float (GdkEvent.Button.y ev) in
114 let time = GdkEvent.Button.time ev in
116 (* Right button for opening the context menu. *)
119 (* If no row is selected, select the row under the mouse. *)
121 let sel = view#selection in
122 if sel#count_selected_rows < 1 then (
123 match view#get_path_at_pos ~x ~y with
125 | Some (path, _, _, _) ->
127 sel#select_path path;
130 sel#get_selected_rows (* actually returns paths *) in
132 (* Select the row under the mouse. *)
134 let sel = view#selection in
135 match view#get_path_at_pos ~x ~y with
137 | Some (path, _, _, _) ->
139 sel#select_path path;
142 (* Get the hdata for all the paths. Filter out rows that it doesn't
143 * make sense to select.
148 let row = model#get_iter path in
149 let hdata = get_hdata t row in
151 | { content=(Loading | ErrorMessage _ | Info _) } -> None
152 | { content=(Top _ | Directory _ | File _ |
153 TopWinReg _ | RegKey _ | RegValue _ ) } ->
157 (* Based on number of selected rows and what is selected, construct
160 (match make_context_menu t paths with
161 | Some menu -> menu#popup ~button ~time
165 (* Return true so no other handler will run. *)
168 (* We didn't handle this, defer to other handlers. *)
171 and make_context_menu t paths =
172 let menu = GMenu.menu () in
173 let factory = new GMenu.factory menu in
175 let rec add_file_items path =
176 let item = factory#add_item "View ..." in
177 (match Config.opener with
179 ignore (item#connect#activate ~callback:(view_file t path opener));
181 item#misc#set_sensitive false
183 let item = factory#add_item "File information" in
184 ignore (item#connect#activate ~callback:(file_information t path));
185 let item = factory#add_item "MD5 checksum" in
186 ignore (item#connect#activate ~callback:(checksum_file t path "md5"));
187 let item = factory#add_item "SHA1 checksum" in
188 ignore (item#connect#activate ~callback:(checksum_file t path "sha1"));
189 ignore (factory#add_separator ());
190 let item = factory#add_item "Download ..." in
191 ignore (item#connect#activate ~callback:(download_file t path));
193 and add_directory_items path =
194 let item = factory#add_item "Directory information" in
195 item#misc#set_sensitive false;
196 let item = factory#add_item "Calculate disk usage" in
197 ignore (item#connect#activate ~callback:(disk_usage t path));
198 ignore (factory#add_separator ());
199 let item = factory#add_item "Download ..." in
200 item#misc#set_sensitive false;
201 let item = factory#add_item "Download as .tar ..." in
202 ignore (item#connect#activate
203 ~callback:(download_dir_tarball t Tar path));
204 let item = factory#add_item "Download as .tar.gz ..." in
205 ignore (item#connect#activate
206 ~callback:(download_dir_tarball t TGZ path));
207 let item = factory#add_item "Download as .tar.xz ..." in
208 ignore (item#connect#activate
209 ~callback:(download_dir_tarball t TXZ path));
210 let item = factory#add_item "Download list of filenames ..." in
211 ignore (item#connect#activate ~callback:(download_dir_find0 t path));
213 and add_top_os_items path =
214 let item = factory#add_item "Operating system information" in
215 ignore (item#connect#activate ~callback:(display_inspection_data t path));
216 ignore (factory#add_separator ());
217 add_top_volume_items path
219 and add_top_volume_items path =
220 let item = factory#add_item "Filesystem used & free" in
221 item#misc#set_sensitive false;
222 let item = factory#add_item "Block device information" in
223 item#misc#set_sensitive false;
224 ignore (factory#add_separator ());
225 add_directory_items path
227 and add_topwinreg_items path =
228 let item = factory#add_item "Download hive file ..." in
229 item#misc#set_sensitive false;
230 ignore (factory#add_separator ());
231 add_regkey_items path
233 and add_regkey_items path =
234 let item = factory#add_item "Download as .reg file ..." in
235 (match Config.hivexregedit with
236 | Some hivexregedit ->
237 ignore (item#connect#activate
238 ~callback:(download_as_reg t path hivexregedit));
240 item#misc#set_sensitive false
243 and add_regvalue_items path =
244 let item = factory#add_item "Copy value to clipboard" in
245 ignore (item#connect#activate ~callback:(copy_regvalue t path));
253 (* single selection *)
254 | [path, { content=Top (OS os)} ] -> (* top level operating system *)
255 add_top_os_items path; true
257 | [path, { content=Top (Volume dev) }] -> (* top level volume *)
258 add_top_volume_items path; true
260 | [path, { content=Directory _ }] -> (* directory *)
261 add_directory_items path; true
263 | [path, { content=File _ }] -> (* file *)
264 add_file_items path; true
266 | [path, { content=TopWinReg _ }] -> (* top level registry node *)
267 add_topwinreg_items path; true
269 | [path, { content=RegKey _ }] -> (* registry node *)
270 add_regkey_items path; true
272 | [path, { content=RegValue _ }] -> (* registry key/value pair *)
273 add_regvalue_items path; true
275 | [_, { content=(Loading|ErrorMessage _|Info _) }] -> false
278 (* At the moment multiple selection is disabled. When/if we
279 * enable it we should do something intelligent here. XXX
282 if has_menu then Some menu else None
284 let clear { model = model; hash = hash } =
288 let rec add ({ model = model } as t) name data =
291 (* Populate the top level of the filetree. If there are operating
292 * systems from inspection, these have their own top level entries
293 * followed by only unreferenced filesystems. If we didn't get
294 * anything from inspection, then at the top level we just show
297 let other_filesystems =
298 DeviceSet.of_list (List.map fst data.insp_all_filesystems) in
299 let other_filesystems =
300 List.fold_left (fun set { insp_filesystems = fses } ->
301 DeviceSet.subtract set (DeviceSet.of_array fses))
302 other_filesystems data.insp_oses in
304 (* Add top level operating systems. *)
305 List.iter (add_top_level_os t name) data.insp_oses;
307 (* Add top level left-over filesystems. *)
308 DeviceSet.iter (add_top_level_vol t name) other_filesystems;
310 (* If it's Windows and registry files exist, create a node for
315 (match os.insp_winreg_SAM with
317 add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SAM" filename
320 (match os.insp_winreg_SECURITY with
322 add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SECURITY"
326 (match os.insp_winreg_SOFTWARE with
328 add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SOFTWARE"
332 (match os.insp_winreg_SYSTEM with
334 add_top_level_winreg t name os "HKEY_LOCAL_MACHINE\\SYSTEM"
338 (match os.insp_winreg_DEFAULT with
340 add_top_level_winreg t name os "HKEY_USERS\\.DEFAULT" filename
345 (* Expand the first top level node. *)
346 match model#get_iter_first with
349 t.view#expand_row (model#get_path row)
351 (* Add a top level operating system node. *)
352 and add_top_level_os ({ model = model } as t) name os =
354 sprintf "<b>%s</b>\n<small>%s</small>\n<small>%s</small>"
355 (markup_escape name) (markup_escape os.insp_hostname)
356 (markup_escape os.insp_product_name) in
358 let row = model#append () in
359 make_node t row (Top (OS os)) None;
360 model#set ~row ~column:t.name_col markup
362 (* Add a top level volume (left over filesystem) node. *)
363 and add_top_level_vol ({ model = model } as t) name dev =
365 sprintf "<b>%s</b>\n<small>from %s</small>"
366 (markup_escape dev) (markup_escape name) in
368 let row = model#append () in
369 make_node t row (Top (Volume dev)) None;
370 model#set ~row ~column:t.name_col markup
372 (* Add a top level Windows Registry node. *)
373 and add_top_level_winreg ({ model = model } as t) name os rootkey
375 let cachefile = tmpdir // string_of_int (unique ()) ^ ".hive" in
378 sprintf "<b>%s</b>\n<small>from %s</small>"
379 (markup_escape rootkey) (markup_escape name) in
381 let row = model#append () in
383 (TopWinReg (OS os, rootkey, remotefile, cachefile)) None;
384 model#set ~row ~column:t.name_col markup
386 (* Generic function to make an openable node to the tree. *)
387 and make_node ({ model = model } as t) row content hiveh =
389 { state=NodeNotStarted; content=content; visited=false; hiveh=hiveh } in
390 store_hdata t row hdata;
392 (* Create a placeholder "loading ..." row underneath this node so
393 * the user has something to expand.
395 let placeholder = model#append ~parent:row () in
396 let hdata = { state=IsLeaf; content=Loading; visited=false; hiveh=None } in
397 store_hdata t placeholder hdata;
398 model#set ~row:placeholder ~column:t.name_col "<i>Loading ...</i>";
399 ignore (t.view#connect#row_expanded ~callback:(expand_row t))
401 and make_leaf ({ model = model } as t) row content hiveh =
402 let hdata = { state=IsLeaf; content=content; visited=false; hiveh=hiveh } in
403 store_hdata t row hdata
405 (* This is called when the user expands a row. *)
406 and expand_row ({ model = model } as t) row _ =
407 match get_hdata t row with
408 | { state=NodeNotStarted; content=Top src } as hdata ->
409 (* User has opened a top level node that was not previously opened. *)
411 (* Mark this row as loading, so we don't try to open it again. *)
412 hdata.state <- NodeLoading;
414 (* Get a stable path for this row. *)
415 let path = model#get_path row in
417 Slave.read_directory ~fail:(when_read_directory_fail t path)
418 src "/" (when_read_directory t path)
420 | { state=NodeNotStarted; content=Directory direntry } as hdata ->
421 (* User has opened a filesystem directory not previously opened. *)
423 (* Mark this row as loading. *)
424 hdata.state <- NodeLoading;
426 (* Get a stable path for this row. *)
427 let path = model#get_path row in
429 let src, pathname = get_pathname t row in
431 Slave.read_directory ~fail:(when_read_directory_fail t path)
432 src pathname (when_read_directory t path)
434 | { state=NodeNotStarted;
435 content=TopWinReg (src, rootkey, remotefile, cachefile) } as hdata ->
436 (* User has opened a Windows Registry top level node
437 * not previously opened.
440 (* Mark this row as loading. *)
441 hdata.state <- NodeLoading;
443 (* Get a stable path for this row. *)
444 let path = model#get_path row in
446 (* Since the user has opened this top level registry node for the
447 * first time, we now need to download the hive.
449 cache_registry_file ~fail:(when_downloaded_registry_fail t path)
450 t path src remotefile cachefile (when_downloaded_registry t path)
452 | { state=NodeNotStarted; content=RegKey node } as hdata ->
453 (* User has opened a Windows Registry key node not previously opened. *)
455 (* Mark this row as loading. *)
456 hdata.state <- NodeLoading;
458 expand_hive_node t row node
460 (* Ignore when a user opens a node which is loading or has been loaded. *)
461 | { state=(NodeLoading|IsNode) } -> ()
463 (* In some circumstances these can be nodes, eg. if we have added Info
464 * nodes below them. Just ignore them if opened.
466 | { content=(File _ | RegValue _) } | { state=IsLeaf } -> ()
468 (* Node should not exist in the tree. *)
469 | { state=NodeNotStarted; content=(Loading | ErrorMessage _ | Info _) } ->
472 (* This is the callback when the slave has read the directory for us. *)
473 and when_read_directory ({ model = model } as t) path entries =
474 debug "when_read_directory";
476 let row = model#get_iter path in
478 (* Sort the entries by lexicographic ordering. *)
479 let cmp { dent_name = n1 } { dent_name = n2 } =
482 let entries = List.sort ~cmp entries in
484 (* Add the entries. *)
487 let { dent_name = name; dent_stat = stat; dent_link = link } =
489 let row = model#append ~parent:row () in
490 if is_directory stat.G.mode then
491 make_node t row (Directory direntry) None
493 make_leaf t row (File direntry) None;
494 model#set ~row ~column:t.name_col (markup_of_name direntry);
495 model#set ~row ~column:t.mode_col (markup_of_mode stat.G.mode);
496 model#set ~row ~column:t.size_col (markup_of_size stat.G.size);
497 model#set ~row ~column:t.date_col (markup_of_date stat.G.mtime);
500 (* Remove the placeholder "Loading" entry. NB. Must be done AFTER
501 * adding the other entries, or else Gtk will unexpand the row.
504 let row = find_child_node_by_content t row Loading in
505 ignore (model#remove row)
506 with Invalid_argument _ | Not_found -> ()
509 (* The original directory entry has now been loaded, so
512 let hdata = get_hdata t row in
513 hdata.state <- IsNode;
516 (* This is called instead of when_read_directory when the read directory
517 * (or mount etc) failed. Convert the "Loading" entry into the
520 and when_read_directory_fail ({ model = model } as t) path exn =
521 debug "when_read_directory_fail: %s" (Printexc.to_string exn);
525 let row = model#get_iter path in
526 let row = model#iter_children ~nth:0 (Some row) in
529 { state=IsLeaf; content=ErrorMessage msg; visited=false; hiveh=None } in
530 store_hdata t row hdata;
532 model#set ~row ~column:t.name_col (markup_escape msg)
535 (* unexpected exception: re-raise it *)
538 (* Called when the top level registry node has been opened and the
539 * hive file was downloaded to the cache file successfully.
541 and when_downloaded_registry ({ model = model } as t) path () =
542 debug "when_downloaded_registry";
543 let row = model#get_iter path in
544 let hdata = get_hdata t row in
545 let h = Option.get hdata.hiveh in
547 (* Continue as if expanding any other hive node. *)
548 let root = Hivex.root h in
549 expand_hive_node t row root
551 (* Called instead of {!when_downloaded_registry} if the download failed. *)
552 and when_downloaded_registry_fail ({ model = model } as t) path exn =
553 debug "when_downloaded_registry_fail: %s" (Printexc.to_string exn);
557 | Hivex.Error (_, _, msg) ->
558 let row = model#get_iter path in
559 let row = model#iter_children ~nth:0 (Some row) in
562 { state=IsLeaf; content=ErrorMessage msg; visited=false; hiveh=None } in
563 store_hdata t row hdata;
565 model#set ~row ~column:t.name_col (markup_escape msg)
568 (* unexpected exception: re-raise it *)
571 (* Expand a hive node. *)
572 and expand_hive_node ({ model = model } as t) row node =
573 debug "expand_hive_node";
574 let hdata = get_hdata t row in
575 let h = Option.get hdata.hiveh in
577 (* Read the hive entries (values, subkeys) at this node and add them
580 let values = Hivex.node_values h node in
581 let cmp v1 v2 = UTF8.compare (Hivex.value_key h v1) (Hivex.value_key h v2) in
582 Array.sort cmp values;
585 let row = model#append ~parent:row () in
586 make_leaf t row (RegValue value) (Some h);
587 model#set ~row ~column:t.name_col (markup_of_regvalue h value);
588 model#set ~row ~column:t.size_col (markup_of_regvaluesize h value);
589 model#set ~row ~column:t.date_col (markup_of_regvaluetype h value);
592 let children = Hivex.node_children h node in
593 let cmp n1 n2 = UTF8.compare (Hivex.node_name h n1) (Hivex.node_name h n2) in
594 Array.sort cmp children;
597 let row = model#append ~parent:row () in
598 make_node t row (RegKey node) (Some h);
599 model#set ~row ~column:t.name_col (markup_of_regkey h node);
602 (* Remove the placeholder "Loading" entry. NB. Must be done AFTER
603 * adding the other entries, or else Gtk will unexpand the row.
606 let row = find_child_node_by_content t row Loading in
607 ignore (model#remove row)
608 with Invalid_argument _ | Not_found -> ()
611 (* The original entry has now been loaded, so update its state. *)
612 hdata.state <- IsNode;