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\" \""
101 OFF_T_FMT "\" \"%s\"\n",
102 date, level, tape, (OFF_T_FMT_TYPE)fileno, path));
104 next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
105 memset(next, 0, sizeof(DIR_ITEM));
107 next->date = stralloc(date);
109 next->tape = stralloc(tape);
110 next->fileno = fileno;
111 next->path = stralloc(path);
113 next->next = dir_list;
121 list_disk_history(void)
123 if (converse("DHST") == -1)
129 suck_dir_list_from_server(void)
137 off_t fileno = (off_t)-1;
138 char *tape, *tape_undo, tape_undo_ch = '\0';
140 char *disk_path_slash = NULL;
141 char *disk_path_slash_dot = NULL;
145 if (disk_path == NULL) {
146 printf("Directory must be set before getting listing\n");
148 } else if(strcmp(disk_path, "/") == 0) {
149 disk_path_slash = stralloc(disk_path);
151 disk_path_slash = stralloc2(disk_path, "/");
156 cmd = stralloc2("OLSD ", disk_path);
157 if (send_command(cmd) == -1) {
159 amfree(disk_path_slash);
164 if ((i = get_reply_line()) == -1) {
165 amfree(disk_path_slash);
168 if (i == 0) /* assume something wrong! */
170 amfree(disk_path_slash);
175 disk_path_slash_dot = stralloc2(disk_path_slash, ".");
179 /* skip the last line -- duplicate of the preamble */
180 while ((i = get_reply_line()) != 0)
183 amfree(disk_path_slash_dot);
184 amfree(disk_path_slash);
189 if(tape_undo) *tape_undo = tape_undo_ch;
191 cmd = stralloc(l); /* save for the error report */
193 continue; /* throw the rest of the lines away */
202 if (strncmp_const_skip(s, "201-", s, ch) != 0) {
203 err = "bad reply: not 201-";
208 skip_whitespace(s, ch);
210 err = "bad reply: missing date field";
214 skip_non_whitespace(s, ch);
217 skip_whitespace(s, ch);
218 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
219 err = "bad reply: cannot parse level field";
224 skip_whitespace(s, ch);
226 err = "bad reply: missing tape field";
230 skip_non_whitespace(s, ch);
232 tape_undo_ch = *tape_undo;
235 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
236 OFF_T_FMT_TYPE fileno_ = (OFF_T_FMT_TYPE)0;
237 skip_whitespace(s, ch);
238 if(ch == '\0' || sscanf(s - 1, OFF_T_FMT, &fileno_) != 1) {
239 err = "bad reply: cannot parse fileno field";
242 fileno = (off_t)fileno_;
249 skip_whitespace(s, ch);
251 err = "bad reply: missing directory field";
255 dir = unquote_string(qdir);
257 /* add a '.' if it a the entry for the current directory */
258 if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
260 dir = stralloc(disk_path_slash_dot);
262 add_dir_list_item(date, level, tape, fileno, dir);
265 amfree(disk_path_slash_dot);
266 amfree(disk_path_slash);
267 if(!server_happy()) {
291 if (disk_path == NULL) {
292 printf("Must select a disk before listing files; use the setdisk command.\n");
296 if ((pager = getenv("PAGER")) == NULL)
301 * Set up the pager command so if the pager is terminated, we do
302 * not get a SIGPIPE back.
304 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
305 if ((fp = popen(pager_command, "w")) == NULL)
307 printf("Warning - can't pipe through %s\n", pager);
310 amfree(pager_command);
311 i = strlen(disk_path);
313 i++; /* so disk_path != "/" */
314 for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
315 quoted = quote_string(item->path + i);
316 fprintf(fp, "%s %s\n", item->date, quoted);