Imported Upstream version 2.5.2p1
[debian/amanda] / recover-src / display_commands.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: display_commands.c,v 1.22 2006/07/05 19:42:17 martinea Exp $
28  *
29  * implements the directory-display related commands in amrecover
30  */
31
32 #include "amanda.h"
33 #include "amrecover.h"
34 #include "util.h"
35
36 DIR_ITEM *get_dir_list(void);
37 DIR_ITEM *get_next_dir_item(DIR_ITEM *this);
38
39 void clear_dir_list(void);
40 void free_dir_item(DIR_ITEM *item);
41 static int add_dir_list_item(char *date,
42                                 int level,
43                                 char *tape,
44                                 off_t fileno,
45                                 char *path);
46 void list_disk_history(void);
47 void suck_dir_list_from_server(void);
48 void list_directory(void);
49
50 static DIR_ITEM *dir_list = NULL;
51
52 DIR_ITEM *
53 get_dir_list(void)
54 {
55     return dir_list;
56 }
57
58 DIR_ITEM *
59 get_next_dir_item(
60     DIR_ITEM *  this)
61 {
62     /*@ignore@*/
63     return this->next;
64     /*@end@*/
65 }
66
67
68 void
69 clear_dir_list(void)
70 {
71     free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
72     dir_list = NULL;
73 }
74
75 void
76 free_dir_item(
77     DIR_ITEM *  item)
78 {
79     DIR_ITEM *next;
80
81     while (item != NULL) {
82         next = item->next;
83         amfree(item->date);
84         amfree(item->tape);
85         amfree(item->path);
86         amfree(item);
87         item = next;
88     }
89 }
90
91 /* add item to list if path not already on list */
92 static int
93 add_dir_list_item(
94     char *      date,
95     int         level,
96     char *      tape,
97     off_t       fileno,
98     char *      path)
99 {
100     DIR_ITEM *next;
101
102     dbprintf(("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \""
103               OFF_T_FMT "\" \"%s\"\n",
104               date, level, tape, (OFF_T_FMT_TYPE)fileno, path));
105
106     next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
107     memset(next, 0, sizeof(DIR_ITEM));
108
109     next->date = stralloc(date);
110     next->level = level;
111     next->tape = stralloc(tape);
112     next->fileno = fileno;
113     next->path = stralloc(path);
114
115     next->next = dir_list;
116     dir_list = next;
117
118     return 0;
119 }
120
121
122 void
123 list_disk_history(void)
124 {
125     if (converse("DHST") == -1)
126         exit(1);
127 }
128
129
130 void
131 suck_dir_list_from_server(void)
132 {
133     char *cmd = NULL;
134     char *err = NULL;
135     int i;
136     char *l = NULL;
137     char *date;
138     int level = 0;
139     off_t fileno = (off_t)-1;
140     char *tape, *tape_undo, tape_undo_ch = '\0';
141     char *dir, *qdir;
142     char *disk_path_slash = NULL;
143     char *disk_path_slash_dot = NULL;
144     char *s;
145     int ch;
146     char *qdisk_path;
147
148     if (disk_path == NULL) {
149         printf("Directory must be set before getting listing\n");
150         return;
151     } else if(strcmp(disk_path, "/") == 0) {
152         disk_path_slash = stralloc(disk_path);
153     } else {
154         disk_path_slash = stralloc2(disk_path, "/");
155     }
156
157     clear_dir_list();
158
159     qdisk_path = quote_string(disk_path);
160     cmd = stralloc2("OLSD ", qdisk_path);
161     amfree(qdisk_path);
162     if (send_command(cmd) == -1) {
163         amfree(cmd);
164         amfree(disk_path_slash);
165         exit(1);
166     }
167     amfree(cmd);
168     /* skip preamble */
169     if ((i = get_reply_line()) == -1) {
170         amfree(disk_path_slash);
171         exit(1);
172     }
173     if (i == 0)                         /* assume something wrong! */
174     {
175         amfree(disk_path_slash);
176         l = reply_line();
177         printf("%s\n", l);
178         return;
179     }
180     disk_path_slash_dot = stralloc2(disk_path_slash, ".");
181     amfree(cmd);
182     amfree(err);
183     tape_undo = NULL;
184     /* skip the last line -- duplicate of the preamble */
185     while ((i = get_reply_line()) != 0)
186     {
187         if (i == -1) {
188             amfree(disk_path_slash_dot);
189             amfree(disk_path_slash);
190             exit(1);
191         }
192         if(err) {
193             if(cmd == NULL) {
194                 if(tape_undo) *tape_undo = tape_undo_ch;
195                 tape_undo = NULL;
196                 cmd = stralloc(l);      /* save for the error report */
197             }
198             continue;                   /* throw the rest of the lines away */
199         }
200         l = reply_line();
201         if (!server_happy())
202         {
203             printf("%s\n", l);
204             continue;
205         }
206         s = l;
207         if (strncmp_const_skip(l, "201-", s, ch) != 0) {
208             err = "bad reply: not 201-";
209             continue;
210         }
211         ch = *s++;
212         skip_whitespace(s, ch);
213         if(ch == '\0') {
214             err = "bad reply: missing date field";
215             continue;
216         }
217         date = s - 1;
218         skip_non_whitespace(s, ch);
219         *(s - 1) = '\0';
220
221         skip_whitespace(s, ch);
222         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
223             err = "bad reply: cannot parse level field";
224             continue;
225         }
226         skip_integer(s, ch);
227
228         skip_whitespace(s, ch);
229         if(ch == '\0') {
230             err = "bad reply: missing tape field";
231             continue;
232         }
233         tape = s - 1;
234         skip_non_whitespace(s, ch);
235         tape_undo = s - 1;
236         tape_undo_ch = *tape_undo;
237         *tape_undo = '\0';
238
239         if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
240             OFF_T_FMT_TYPE fileno_ = (OFF_T_FMT_TYPE)0;
241             skip_whitespace(s, ch);
242             if(ch == '\0' || sscanf(s - 1, OFF_T_FMT, &fileno_) != 1) {
243                 err = "bad reply: cannot parse fileno field";
244                 continue;
245             }
246             fileno = (off_t)fileno_;
247             skip_integer(s, ch);
248         }
249         else {
250             fileno = (off_t)-1;
251         }
252
253         skip_whitespace(s, ch);
254         if(ch == '\0') {
255             err = "bad reply: missing directory field";
256             continue;
257         }
258         qdir = s - 1;
259         dir = unquote_string(qdir);
260
261         /* add a '.' if it a the entry for the current directory */
262         if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
263             amfree(dir);
264             dir = stralloc(disk_path_slash_dot);
265         }
266         add_dir_list_item(date, level, tape, fileno, dir);
267         amfree(dir);
268     }
269     amfree(disk_path_slash_dot);
270     amfree(disk_path_slash);
271     if(!server_happy()) {
272         puts(reply_line());
273     } else if(err) {
274         if(*err) {
275             puts(err);
276         }
277         if (cmd)
278             puts(cmd);
279         clear_dir_list();
280     }
281     amfree(cmd);
282 }
283
284
285 void
286 list_directory(void)
287 {
288     size_t i;
289     DIR_ITEM *item;
290     FILE *fp;
291     char *pager;
292     char *pager_command;
293     char *quoted;
294
295     if (disk_path == NULL) {
296         printf("Must select a disk before listing files; use the setdisk command.\n");
297         return;
298     }
299
300     if ((pager = getenv("PAGER")) == NULL)
301     {
302         pager = "more";
303     }
304     /*
305      * Set up the pager command so if the pager is terminated, we do
306      * not get a SIGPIPE back.
307      */
308     pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
309     if ((fp = popen(pager_command, "w")) == NULL)
310     {
311         printf("Warning - can't pipe through %s\n", pager);
312         fp = stdout;
313     }
314     amfree(pager_command);
315     i = strlen(disk_path);
316     if (i != 1)
317         i++;                            /* so disk_path != "/" */
318     for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
319         quoted = quote_string(item->path + i);
320         fprintf(fp, "%s %s\n", item->date, quoted);
321         amfree(quoted);
322     }
323     apclose(fp);
324 }