(* Guestfs Browser. * Copyright (C) 2010 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) open Utils type t = { view : GTree.view; model : GTree.tree_store; hash : (int, hdata) Hashtbl.t; (* hash from index_col -> hdata *) index_col : int GTree.column; mode_col : string GTree.column; name_col : string GTree.column; size_col : int64 GTree.column; date_col : string GTree.column; link_col : string GTree.column; } and hdata = state_t * content_t (* The type of the hidden column used to implement on-demand loading. * All rows are classified as either nodes or leafs (eg. a "node" might * be a directory, or a top-level operating system, or anything else * which the user could open and look inside). *) and state_t = | IsLeaf (* there are no children *) | NodeNotStarted (* user has not tried to open this *) | NodeLoading (* user tried to open it, still loading *) | IsNode (* we've loaded the children of this directory *) (* The actual content of a row. *) and content_t = | Loading (* special "loading ..." node *) | ErrorMessage of string (* error message node *) | Info of string (* information node (eg. disk usage) *) | Top of Slave.source (* top level OS or volume node *) | Directory of Slave.direntry (* a directory *) | File of Slave.direntry (* a file inc. special files *) (* Store hdata into a row. *) let store_hdata {model = model; hash = hash; index_col = index_col} row hdata = let index = unique () in Hashtbl.add hash index hdata; model#set ~row ~column:index_col index (* Retrieve previously stored hdata from a row. *) let get_hdata { model = model; hash = hash; index_col = index_col } row = let index = model#get ~row ~column:index_col in try Hashtbl.find hash index with Not_found -> assert false (* Iterate over children of node, looking for matching hdata. *) let find_child_node_by_hdata ({ model = model } as t) row hdata = let rec loop row = if hdata = get_hdata t row then row else if model#iter_next row then loop row else raise Not_found in if not (model#iter_has_child row) then raise Not_found; let first_child = model#iter_children (Some row) in loop first_child (* Search up to the top of the tree so we know if this directory * comes from an OS or a volume, and the full path to here. * * The path up the tree will always look something like: * Top * \_ Directory * \_ Directory * \_ Loading <--- you are here *) let rec get_pathname ({ model = model } as t) row = let hdata = get_hdata t row in let parent = model#iter_parent row in match hdata, parent with | (IsLeaf, (Loading|ErrorMessage _|Info _)), Some parent -> get_pathname t parent | (IsLeaf, (Loading|ErrorMessage _|Info _)), None -> assert false | (_, Directory { Slave.dent_name = name }), Some parent | (_, File { Slave.dent_name = name }), Some parent -> let src, parent_name = get_pathname t parent in let path = if parent_name = "/" then "/" ^ name else parent_name ^ "/" ^ name in src, path | (_, Top src), _ -> src, "/" | (_, Directory _), None -> assert false | (_, File _), None -> assert false | (_, Loading), _ -> assert false | (_, ErrorMessage _), _ -> assert false | (_, Info _), _ -> assert false