2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: display_commands.c,v 1.22 2006/07/05 19:42:17 martinea Exp $
29 * implements the directory-display related commands in amrecover
33 #include "amrecover.h"
36 DIR_ITEM *get_dir_list(void);
37 DIR_ITEM *get_next_dir_item(DIR_ITEM *this);
39 void clear_dir_list(void);
40 void free_dir_item(DIR_ITEM *item);
41 static int add_dir_list_item(char *date,
46 void list_disk_history(void);
47 void suck_dir_list_from_server(void);
48 void list_directory(void);
50 static DIR_ITEM *dir_list = NULL;
71 free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
81 while (item != NULL) {
91 /* add item to list if path not already on list */
102 dbprintf(_("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%lld\" \"%s\"\n"),
103 date, level, tape, (long long)fileno, path);
105 next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
106 memset(next, 0, sizeof(DIR_ITEM));
108 next->date = stralloc(date);
110 next->tape = stralloc(tape);
111 next->fileno = fileno;
112 next->path = stralloc(path);
114 next->next = dir_list;
122 list_disk_history(void)
124 if (converse("DHST") == -1)
130 suck_dir_list_from_server(void)
138 off_t fileno = (off_t)-1;
139 char *tape, *tape_undo, tape_undo_ch = '\0';
141 char *disk_path_slash = NULL;
142 char *disk_path_slash_dot = NULL;
147 if (disk_path == NULL) {
148 g_printf(_("Directory must be set before getting listing\n"));
150 } else if(strcmp(disk_path, "/") == 0) {
151 disk_path_slash = stralloc(disk_path);
153 disk_path_slash = stralloc2(disk_path, "/");
158 qdisk_path = quote_string(disk_path);
159 cmd = stralloc2("OLSD ", qdisk_path);
161 if (send_command(cmd) == -1) {
163 amfree(disk_path_slash);
168 if ((i = get_reply_line()) == -1) {
169 amfree(disk_path_slash);
172 if (i == 0) /* assume something wrong! */
174 amfree(disk_path_slash);
179 disk_path_slash_dot = stralloc2(disk_path_slash, ".");
183 /* skip the last line -- duplicate of the preamble */
184 while ((i = get_reply_line()) != 0)
187 amfree(disk_path_slash_dot);
188 amfree(disk_path_slash);
193 if(tape_undo) *tape_undo = tape_undo_ch;
195 cmd = stralloc(l); /* save for the error report */
197 continue; /* throw the rest of the lines away */
206 if (strncmp_const_skip(l, "201-", s, ch) != 0) {
207 err = _("bad reply: not 201-");
211 skip_whitespace(s, ch);
213 err = _("bad reply: missing date field");
217 skip_non_whitespace(s, ch);
220 skip_whitespace(s, ch);
221 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
222 err = _("bad reply: cannot parse level field");
227 skip_whitespace(s, ch);
229 err = _("bad reply: missing tape field");
233 skip_non_whitespace(s, ch);
235 tape_undo_ch = *tape_undo;
238 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
239 long long fileno_ = (long long)0;
240 skip_whitespace(s, ch);
241 if(ch == '\0' || sscanf(s - 1, "%lld", &fileno_) != 1) {
242 err = _("bad reply: cannot parse fileno field");
245 fileno = (off_t)fileno_;
252 skip_whitespace(s, ch);
254 err = _("bad reply: missing directory field");
258 dir = unquote_string(qdir);
260 /* add a '.' if it a the entry for the current directory */
261 if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
263 dir = stralloc(disk_path_slash_dot);
265 add_dir_list_item(date, level, tape, fileno, dir);
268 amfree(disk_path_slash_dot);
269 amfree(disk_path_slash);
270 if(!server_happy()) {
294 if (disk_path == NULL) {
295 g_printf(_("Must select a disk before listing files; use the setdisk command.\n"));
299 if ((pager = getenv("PAGER")) == NULL)
304 * Set up the pager command so if the pager is terminated, we do
305 * not get a SIGPIPE back.
307 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
308 if ((fp = popen(pager_command, "w")) == NULL)
310 g_printf(_("Warning - can't pipe through %s\n"), pager);
313 amfree(pager_command);
314 i = strlen(disk_path);
316 i++; /* so disk_path != "/" */
317 for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
318 quoted = quote_string(item->path + i);
319 g_fprintf(fp, "%s %s\n", item->date, quoted);