Imported Upstream version 2.5.1p3
[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.2.1 2006/12/22 15:10:26 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 #define sc "201-"
207         if (strncmp(l, sc, sizeof(sc)-1) != 0) {
208             err = "bad reply: not 201-";
209             continue;
210         }
211         s = l + sizeof(sc)-1;
212         ch = *s++;
213 #undef sc
214         skip_whitespace(s, ch);
215         if(ch == '\0') {
216             err = "bad reply: missing date field";
217             continue;
218         }
219         date = s - 1;
220         skip_non_whitespace(s, ch);
221         *(s - 1) = '\0';
222
223         skip_whitespace(s, ch);
224         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
225             err = "bad reply: cannot parse level field";
226             continue;
227         }
228         skip_integer(s, ch);
229
230         skip_whitespace(s, ch);
231         if(ch == '\0') {
232             err = "bad reply: missing tape field";
233             continue;
234         }
235         tape = s - 1;
236         skip_non_whitespace(s, ch);
237         tape_undo = s - 1;
238         tape_undo_ch = *tape_undo;
239         *tape_undo = '\0';
240
241         if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
242             skip_whitespace(s, ch);
243             if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
244                                     (OFF_T_FMT_TYPE *)&fileno) != 1) {
245                 err = "bad reply: cannot parse fileno field";
246                 continue;
247             }
248             skip_integer(s, ch);
249         }
250         else {
251             fileno = (off_t)-1;
252         }
253
254         skip_whitespace(s, ch);
255         if(ch == '\0') {
256             err = "bad reply: missing directory field";
257             continue;
258         }
259         qdir = s - 1;
260         dir = unquote_string(qdir);
261
262         /* add a '.' if it a the entry for the current directory */
263         if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
264             amfree(dir);
265             dir = stralloc(disk_path_slash_dot);
266         }
267         add_dir_list_item(date, level, tape, fileno, dir);
268         amfree(dir);
269     }
270     amfree(disk_path_slash_dot);
271     amfree(disk_path_slash);
272     if(!server_happy()) {
273         puts(reply_line());
274     } else if(err) {
275         if(*err) {
276             puts(err);
277         }
278         if (cmd)
279             puts(cmd);
280         clear_dir_list();
281     }
282     amfree(cmd);
283 }
284
285
286 void
287 list_directory(void)
288 {
289     size_t i;
290     DIR_ITEM *item;
291     FILE *fp;
292     char *pager;
293     char *pager_command;
294     char *quoted;
295
296     if (disk_path == NULL) {
297         printf("Must select a disk before listing files; use the setdisk command.\n");
298         return;
299     }
300
301     if ((pager = getenv("PAGER")) == NULL)
302     {
303         pager = "more";
304     }
305     /*
306      * Set up the pager command so if the pager is terminated, we do
307      * not get a SIGPIPE back.
308      */
309     pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
310     if ((fp = popen(pager_command, "w")) == NULL)
311     {
312         printf("Warning - can't pipe through %s\n", pager);
313         fp = stdout;
314     }
315     amfree(pager_command);
316     i = strlen(disk_path);
317     if (i != 1)
318         i++;                            /* so disk_path != "/" */
319     for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
320         quoted = quote_string(item->path + i);
321         fprintf(fp, "%s %s\n", item->date, quoted);
322         amfree(quoted);
323     }
324     apclose(fp);
325 }