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.3 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;
60 /*@keep@*/ DIR_ITEM * this)
69 free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
79 while (item != NULL) {
89 /* add item to list if path not already on list */
100 dbprintf(_("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%lld\" \"%s\"\n"),
101 date, level, tape, (long long)fileno, path);
103 next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
104 memset(next, 0, sizeof(DIR_ITEM));
106 next->date = stralloc(date);
108 next->tape = stralloc(tape);
109 next->fileno = fileno;
110 next->path = stralloc(path);
112 next->next = dir_list;
120 list_disk_history(void)
122 if (converse("DHST") == -1)
128 suck_dir_list_from_server(void)
136 off_t fileno = (off_t)-1;
137 char *tape, *tape_undo, tape_undo_ch = '\0';
139 char *disk_path_slash = NULL;
140 char *disk_path_slash_dot = NULL;
144 if (disk_path == NULL) {
145 g_printf(_("Directory must be set before getting listing\n"));
147 } else if(strcmp(disk_path, "/") == 0) {
148 disk_path_slash = stralloc(disk_path);
150 disk_path_slash = stralloc2(disk_path, "/");
155 cmd = stralloc2("OLSD ", disk_path);
156 if (send_command(cmd) == -1) {
158 amfree(disk_path_slash);
163 if ((i = get_reply_line()) == -1) {
164 amfree(disk_path_slash);
167 if (i == 0) /* assume something wrong! */
169 amfree(disk_path_slash);
174 disk_path_slash_dot = stralloc2(disk_path_slash, ".");
178 /* skip the last line -- duplicate of the preamble */
179 while ((i = get_reply_line()) != 0)
182 amfree(disk_path_slash_dot);
183 amfree(disk_path_slash);
188 if(tape_undo) *tape_undo = tape_undo_ch;
190 cmd = stralloc(l); /* save for the error report */
192 continue; /* throw the rest of the lines away */
201 if (strncmp_const_skip(s, "201-", s, ch) != 0) {
202 err = "bad reply: not 201-";
207 skip_whitespace(s, ch);
209 err = _("bad reply: missing date field");
213 skip_non_whitespace(s, ch);
216 skip_whitespace(s, ch);
217 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
218 err = _("bad reply: cannot parse level field");
223 skip_whitespace(s, ch);
225 err = _("bad reply: missing tape field");
229 skip_non_whitespace(s, ch);
231 tape_undo_ch = *tape_undo;
234 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
235 long long fileno_ = (long long)0;
236 skip_whitespace(s, ch);
237 if(ch == '\0' || sscanf(s - 1, "%lld", &fileno_) != 1) {
238 err = _("bad reply: cannot parse fileno field");
241 fileno = (off_t)fileno_;
248 skip_whitespace(s, ch);
250 err = _("bad reply: missing directory field");
254 dir = unquote_string(qdir);
256 /* add a '.' if it a the entry for the current directory */
257 if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
259 dir = stralloc(disk_path_slash_dot);
261 add_dir_list_item(date, level, tape, fileno, dir);
264 amfree(disk_path_slash_dot);
265 amfree(disk_path_slash);
266 if(!server_happy()) {
290 if (disk_path == NULL) {
291 g_printf(_("Must select a disk before listing files; use the setdisk command.\n"));
295 if ((pager = getenv("PAGER")) == NULL)
300 * Set up the pager command so if the pager is terminated, we do
301 * not get a SIGPIPE back.
303 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
304 if ((fp = popen(pager_command, "w")) == NULL)
306 g_printf(_("Warning - can't pipe through %s\n"), pager);
309 amfree(pager_command);
310 i = strlen(disk_path);
312 i++; /* so disk_path != "/" */
313 for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
314 quoted = quote_string(item->path + i);
315 g_fprintf(fp, "%s %s\n", item->date, quoted);