90e181674afc385ea96f3629be90ed1b72b12927
[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
147     if (disk_path == NULL) {
148         printf("Directory must be set before getting listing\n");
149         return;
150     } else if(strcmp(disk_path, "/") == 0) {
151         disk_path_slash = stralloc(disk_path);
152     } else {
153         disk_path_slash = stralloc2(disk_path, "/");
154     }
155
156     clear_dir_list();
157
158     cmd = stralloc2("OLSD ", disk_path);
159     if (send_command(cmd) == -1) {
160         amfree(cmd);
161         amfree(disk_path_slash);
162         exit(1);
163     }
164     amfree(cmd);
165     /* skip preamble */
166     if ((i = get_reply_line()) == -1) {
167         amfree(disk_path_slash);
168         exit(1);
169     }
170     if (i == 0)                         /* assume something wrong! */
171     {
172         amfree(disk_path_slash);
173         l = reply_line();
174         printf("%s\n", l);
175         return;
176     }
177     disk_path_slash_dot = stralloc2(disk_path_slash, ".");
178     amfree(cmd);
179     amfree(err);
180     tape_undo = NULL;
181     /* skip the last line -- duplicate of the preamble */
182     while ((i = get_reply_line()) != 0)
183     {
184         if (i == -1) {
185             amfree(disk_path_slash_dot);
186             amfree(disk_path_slash);
187             exit(1);
188         }
189         if(err) {
190             if(cmd == NULL) {
191                 if(tape_undo) *tape_undo = tape_undo_ch;
192                 tape_undo = NULL;
193                 cmd = stralloc(l);      /* save for the error report */
194             }
195             continue;                   /* throw the rest of the lines away */
196         }
197         l = reply_line();
198         if (!server_happy())
199         {
200             printf("%s\n", l);
201             continue;
202         }
203 #define sc "201-"
204         if (strncmp(l, sc, sizeof(sc)-1) != 0) {
205             err = "bad reply: not 201-";
206             continue;
207         }
208         s = l + sizeof(sc)-1;
209         ch = *s++;
210 #undef sc
211         skip_whitespace(s, ch);
212         if(ch == '\0') {
213             err = "bad reply: missing date field";
214             continue;
215         }
216         date = s - 1;
217         skip_non_whitespace(s, ch);
218         *(s - 1) = '\0';
219
220         skip_whitespace(s, ch);
221         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
222             err = "bad reply: cannot parse level field";
223             continue;
224         }
225         skip_integer(s, ch);
226
227         skip_whitespace(s, ch);
228         if(ch == '\0') {
229             err = "bad reply: missing tape field";
230             continue;
231         }
232         tape = s - 1;
233         skip_non_whitespace(s, ch);
234         tape_undo = s - 1;
235         tape_undo_ch = *tape_undo;
236         *tape_undo = '\0';
237
238         if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
239             skip_whitespace(s, ch);
240             if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
241                                     (OFF_T_FMT_TYPE *)&fileno) != 1) {
242                 err = "bad reply: cannot parse fileno field";
243                 continue;
244             }
245             skip_integer(s, ch);
246         }
247         else {
248             fileno = (off_t)-1;
249         }
250
251         skip_whitespace(s, ch);
252         if(ch == '\0') {
253             err = "bad reply: missing directory field";
254             continue;
255         }
256         qdir = s - 1;
257         dir = unquote_string(qdir);
258
259         /* add a '.' if it a the entry for the current directory */
260         if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
261             amfree(dir);
262             dir = stralloc(disk_path_slash_dot);
263         }
264         add_dir_list_item(date, level, tape, fileno, dir);
265         amfree(dir);
266     }
267     amfree(disk_path_slash_dot);
268     amfree(disk_path_slash);
269     if(!server_happy()) {
270         puts(reply_line());
271     } else if(err) {
272         if(*err) {
273             puts(err);
274         }
275         if (cmd)
276             puts(cmd);
277         clear_dir_list();
278     }
279     amfree(cmd);
280 }
281
282
283 void
284 list_directory(void)
285 {
286     size_t i;
287     DIR_ITEM *item;
288     FILE *fp;
289     char *pager;
290     char *pager_command;
291     char *quoted;
292
293     if (disk_path == NULL) {
294         printf("Must select a disk before listing files; use the setdisk command.\n");
295         return;
296     }
297
298     if ((pager = getenv("PAGER")) == NULL)
299     {
300         pager = "more";
301     }
302     /*
303      * Set up the pager command so if the pager is terminated, we do
304      * not get a SIGPIPE back.
305      */
306     pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
307     if ((fp = popen(pager_command, "w")) == NULL)
308     {
309         printf("Warning - can't pipe through %s\n", pager);
310         fp = stdout;
311     }
312     amfree(pager_command);
313     i = strlen(disk_path);
314     if (i != 1)
315         i++;                            /* so disk_path != "/" */
316     for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
317         quoted = quote_string(item->path + i);
318         fprintf(fp, "%s %s\n", item->date, quoted);
319         amfree(quoted);
320     }
321     apclose(fp);
322 }