2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of U.M. not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. U.M. makes no representations about the
14 * suitability of this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Authors: the Amanda Development Team. Its members are listed in a
25 * file named AUTHORS, in the root directory of this distribution.
28 * $Id: display_commands.c,v 1.22 2006/07/05 19:42:17 martinea Exp $
30 * implements the directory-display related commands in amrecover
34 #include "amrecover.h"
37 gboolean translate_mode = TRUE;
39 DIR_ITEM *get_dir_list(void);
40 DIR_ITEM *get_next_dir_item(DIR_ITEM *this);
42 void clear_dir_list(void);
43 void free_dir_item(DIR_ITEM *item);
44 static int add_dir_list_item(char *date,
49 void list_disk_history(void);
50 void suck_dir_list_from_server(void);
51 void list_directory(void);
53 static DIR_ITEM *dir_list = NULL;
74 free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
84 while (item != NULL) {
95 /* add item to list if path not already on list */
106 dbprintf(_("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%lld\" \"%s\"\n"),
107 date, level, tape, (long long)fileno, path);
109 next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
110 memset(next, 0, sizeof(DIR_ITEM));
112 next->date = stralloc(date);
114 next->tape = stralloc(tape);
115 next->fileno = fileno;
116 next->path = stralloc(path);
117 next->tpath = translate_octal(g_strdup(path));
119 next->next = dir_list;
127 list_disk_history(void)
129 if (converse("DHST") == -1)
135 suck_dir_list_from_server(void)
143 off_t fileno = (off_t)-1;
144 char *tape, *tape_undo, tape_undo_ch = '\0';
146 char *disk_path_slash = NULL;
147 char *disk_path_slash_dot = NULL;
152 if (disk_path == NULL) {
153 g_printf(_("Directory must be set before getting listing\n"));
155 } else if(strcmp(disk_path, "/") == 0) {
156 disk_path_slash = stralloc(disk_path);
158 disk_path_slash = stralloc2(disk_path, "/");
163 qdisk_path = quote_string(disk_path);
164 cmd = stralloc2("OLSD ", qdisk_path);
166 if (send_command(cmd) == -1) {
168 amfree(disk_path_slash);
173 if ((i = get_reply_line()) == -1) {
174 amfree(disk_path_slash);
177 if (i == 0) /* assume something wrong! */
179 amfree(disk_path_slash);
184 disk_path_slash_dot = stralloc2(disk_path_slash, ".");
188 /* skip the last line -- duplicate of the preamble */
189 while ((i = get_reply_line()) != 0)
192 amfree(disk_path_slash_dot);
193 amfree(disk_path_slash);
198 if(tape_undo) *tape_undo = tape_undo_ch;
200 cmd = stralloc(l); /* save for the error report */
202 continue; /* throw the rest of the lines away */
211 if (strncmp_const_skip(l, "201-", s, ch) != 0) {
212 err = _("bad reply: not 201-");
216 skip_whitespace(s, ch);
218 err = _("bad reply: missing date field");
222 skip_non_whitespace(s, ch);
225 skip_whitespace(s, ch);
226 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
227 err = _("bad reply: cannot parse level field");
232 skip_whitespace(s, ch);
234 err = _("bad reply: missing tape field");
238 skip_quoted_string(s, ch);
240 tape_undo_ch = *tape_undo;
242 tape = unquote_string(tape);
244 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
245 long long fileno_ = (long long)0;
246 skip_whitespace(s, ch);
247 if(ch == '\0' || sscanf(s - 1, "%lld", &fileno_) != 1) {
248 err = _("bad reply: cannot parse fileno field");
251 fileno = (off_t)fileno_;
258 skip_whitespace(s, ch);
260 err = _("bad reply: missing directory field");
264 dir = unquote_string(qdir);
266 /* add a '.' if it a the entry for the current directory */
267 if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
269 dir = stralloc(disk_path_slash_dot);
271 add_dir_list_item(date, level, tape, fileno, dir);
275 amfree(disk_path_slash_dot);
276 amfree(disk_path_slash);
277 if(!server_happy()) {
301 if (disk_path == NULL) {
302 g_printf(_("Must select a disk before listing files; use the setdisk command.\n"));
306 if ((pager = getenv("PAGER")) == NULL)
311 * Set up the pager command so if the pager is terminated, we do
312 * not get a SIGPIPE back.
314 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
315 if ((fp = popen(pager_command, "w")) == NULL)
317 g_printf(_("Warning - can't pipe through %s\n"), pager);
320 amfree(pager_command);
321 i = strlen(disk_tpath);
323 i++; /* so disk_tpath != "/" */
324 for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
325 quoted = quote_string(item->tpath + i);
326 g_fprintf(fp, "%s %s\n", item->date, quoted);