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.19 2006/03/09 20:06:11 johnfranks Exp $
29 * implements the directory-display related commands in amrecover
33 #include "amrecover.h"
35 static DIR_ITEM *dir_list = NULL;
37 DIR_ITEM *get_dir_list P((void))
42 DIR_ITEM *get_next_dir_item(this)
49 void clear_dir_list P((void))
51 free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
55 void free_dir_item P((DIR_ITEM *item)) {
58 while (item != NULL) {
68 /* add item to list if path not already on list */
69 static int add_dir_list_item(date, level, tape, fileno, path)
78 dbprintf(("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%d\" \"%s\"\n",
79 date, level, tape, fileno, path));
81 next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
82 memset(next, 0, sizeof(DIR_ITEM));
84 next->date = stralloc(date);
86 next->tape = stralloc(tape);
87 next->fileno = fileno;
88 next->path = stralloc(path);
90 next->next = dir_list;
97 void list_disk_history P((void))
99 if (converse("DHST") == -1)
104 void suck_dir_list_from_server P((void))
110 char *date, *date_undo, date_undo_ch = '\0';
112 char *tape, *tape_undo, tape_undo_ch = '\0';
114 char *disk_path_slash = NULL;
115 char *disk_path_slash_dot = NULL;
119 if (disk_path == NULL) {
120 printf("Directory must be set before getting listing\n");
122 } else if(strcmp(disk_path, "/") == 0) {
123 disk_path_slash = stralloc(disk_path);
125 disk_path_slash = stralloc2(disk_path, "/");
130 cmd = stralloc2("OLSD ", disk_path);
131 if (send_command(cmd) == -1) {
133 amfree(disk_path_slash);
138 if ((i = get_reply_line()) == -1) {
139 amfree(disk_path_slash);
142 if (i == 0) /* assume something wrong! */
144 amfree(disk_path_slash);
149 disk_path_slash_dot = stralloc2(disk_path_slash, ".");
152 date_undo = tape_undo = NULL;
153 /* skip the last line -- duplicate of the preamble */
154 while ((i = get_reply_line()) != 0)
157 amfree(disk_path_slash_dot);
158 amfree(disk_path_slash);
163 if(tape_undo) *tape_undo = tape_undo_ch;
164 date_undo = tape_undo = NULL;
165 cmd = stralloc(l); /* save for the error report */
167 continue; /* throw the rest of the lines away */
176 if (strncmp(l, sc, sizeof(sc)-1) != 0) {
177 err = "bad reply: not 201-";
180 s = l + sizeof(sc)-1;
183 skip_whitespace(s, ch);
185 err = "bad reply: missing date field";
189 skip_non_whitespace(s, ch);
191 date_undo_ch = *date_undo;
194 skip_whitespace(s, ch);
195 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
196 err = "bad reply: cannot parse level field";
201 skip_whitespace(s, ch);
203 err = "bad reply: missing tape field";
207 skip_non_whitespace(s, ch);
209 tape_undo_ch = *tape_undo;
212 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
213 skip_whitespace(s, ch);
214 if(ch == '\0' || sscanf(s - 1, "%d", &fileno) != 1) {
215 err = "bad reply: cannot parse fileno field";
224 skip_whitespace(s, ch);
226 err = "bad reply: missing directory field";
231 /* add a '.' if it a the entry for the current directory */
232 if(strcmp(disk_path,dir)==0 || strcmp(disk_path_slash,dir)==0) {
233 dir = disk_path_slash_dot;
235 add_dir_list_item(date, level, tape, fileno, dir);
237 amfree(disk_path_slash_dot);
238 amfree(disk_path_slash);
239 if(!server_happy()) {
252 void list_directory P((void))
260 if (disk_path == NULL) {
261 printf("Must select a disk before listing files\n");
265 if ((pager = getenv("PAGER")) == NULL)
270 * Set up the pager command so if the pager is terminated, we do
271 * not get a SIGPIPE back.
273 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
274 if ((fp = popen(pager_command, "w")) == NULL)
276 printf("Warning - can't pipe through %s\n", pager);
279 amfree(pager_command);
280 i = strlen(disk_path);
282 i++; /* so disk_path != "/" */
283 for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item))
284 fprintf(fp, "%s %s\n", item->date, item->path+i);