(* 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 Printf open Utils open Filetree_type (* Get the basename of a file, using path conventions which are valid * for libguestfs. So [Filename.basename] won't necessarily work * because it will use host path conventions. *) let basename pathname = let len = String.length pathname in try let i = String.rindex pathname '/' in let r = String.sub pathname (i+1) (len-i-1) in if r = "" then "root" else r with Not_found -> pathname (* Download a single file. *) let rec download_file ({ model = model } as t) path () = let row = model#get_iter path in let src, pathname = get_pathname t row in debug "download_file %s: showing dialog" pathname; (* Put up the dialog. *) let title = "Download file" in let dlg = GWindow.file_chooser_dialog ~action:`SAVE ~title ~modal:true () in dlg#add_button_stock `CANCEL `CANCEL; dlg#add_select_button_stock `SAVE `SAVE; dlg#set_current_name (basename pathname); match dlg#run () with | `DELETE_EVENT | `CANCEL -> dlg#destroy () | `SAVE -> match dlg#filename with | None -> () | Some localfile -> 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) and when_downloaded_file t _ localfile () = update_status t (sprintf "Finished downloading %s" localfile) (* Download a directory as a tarball. *) let rec download_dir_tarball ({ model = model } as t) format path () = let row = model#get_iter path in let src, pathname = get_pathname t row in debug "download_dir_tarball %s: showing dialog" pathname; (* Put up the dialog. *) let title = "Download directory to tar file" in let dlg = GWindow.file_chooser_dialog ~action:`SAVE ~title ~modal:true () in dlg#add_button_stock `CANCEL `CANCEL; dlg#add_select_button_stock `SAVE `SAVE; let extension = match format with | Slave.Tar -> ".tar" | Slave.TGZ -> ".tar.gz" | Slave.TXZ -> ".tar.xz" in dlg#set_current_name (basename pathname ^ extension); match dlg#run () with | `DELETE_EVENT | `CANCEL -> dlg#destroy () | `SAVE -> match dlg#filename with | None -> () | Some localfile -> 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) and when_downloaded_dir_tarball t _ localfile () = update_status t (sprintf "Finished downloading %s" localfile) let rec download_dir_find0 ({ model = model } as t) path () = let row = model#get_iter path in let src, pathname = get_pathname t row in debug "download_dir_find0 %s: showing dialog" pathname; (* Put up the dialog. *) let title = "Download list of filenames" in let dlg = GWindow.file_chooser_dialog ~action:`SAVE ~title ~modal:true () in dlg#add_button_stock `CANCEL `CANCEL; dlg#add_select_button_stock `SAVE `SAVE; dlg#set_current_name (basename pathname ^ ".filenames.txt"); (* Notify that the list of strings is \0 separated. *) let hbox = let hbox = GPack.hbox () in ignore (GMisc.image ~stock:`INFO ~packing:hbox#pack ()); let label = GMisc.label ~text:"The list of filenames is saved to a file with zero byte separators, to allow the full range of characters to be used in the names themselves." ~packing:hbox#pack () in label#set_line_wrap true; hbox in dlg#set_extra_widget (hbox :> GObj.widget); match dlg#run () with | `DELETE_EVENT | `CANCEL -> dlg#destroy () | `SAVE -> match dlg#filename with | None -> () | Some localfile -> 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) and when_downloaded_dir_find0 t _ localfile () = update_status t (sprintf "Finished downloading %s" localfile)