Imported Upstream version 3.2.0
[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\" \"%lld\" \"%s\"\n"),
103               date, level, tape, (long long)fileno, path);
104
105     next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
106     memset(next, 0, sizeof(DIR_ITEM));
107
108     next->date = stralloc(date);
109     next->level = level;
110     next->tape = stralloc(tape);
111     next->fileno = fileno;
112     next->path = stralloc(path);
113
114     next->next = dir_list;
115     dir_list = next;
116
117     return 0;
118 }
119
120
121 void
122 list_disk_history(void)
123 {
124     if (converse("DHST") == -1)
125         exit(1);
126 }
127
128
129 void
130 suck_dir_list_from_server(void)
131 {
132     char *cmd = NULL;
133     char *err = NULL;
134     int i;
135     char *l = NULL;
136     char *date;
137     int level = 0;
138     off_t fileno = (off_t)-1;
139     char *tape, *tape_undo, tape_undo_ch = '\0';
140     char *dir, *qdir;
141     char *disk_path_slash = NULL;
142     char *disk_path_slash_dot = NULL;
143     char *s;
144     int ch;
145     char *qdisk_path;
146
147     if (disk_path == NULL) {
148         g_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     qdisk_path = quote_string(disk_path);
159     cmd = stralloc2("OLSD ", qdisk_path);
160     amfree(qdisk_path);
161     if (send_command(cmd) == -1) {
162         amfree(cmd);
163         amfree(disk_path_slash);
164         exit(1);
165     }
166     amfree(cmd);
167     /* skip preamble */
168     if ((i = get_reply_line()) == -1) {
169         amfree(disk_path_slash);
170         exit(1);
171     }
172     if (i == 0)                         /* assume something wrong! */
173     {
174         amfree(disk_path_slash);
175         l = reply_line();
176         g_printf("%s\n", l);
177         return;
178     }
179     disk_path_slash_dot = stralloc2(disk_path_slash, ".");
180     amfree(cmd);
181     amfree(err);
182     tape_undo = NULL;
183     /* skip the last line -- duplicate of the preamble */
184     while ((i = get_reply_line()) != 0)
185     {
186         if (i == -1) {
187             amfree(disk_path_slash_dot);
188             amfree(disk_path_slash);
189             exit(1);
190         }
191         if(err) {
192             if(cmd == NULL) {
193                 if(tape_undo) *tape_undo = tape_undo_ch;
194                 tape_undo = NULL;
195                 cmd = stralloc(l);      /* save for the error report */
196             }
197             continue;                   /* throw the rest of the lines away */
198         }
199         l = reply_line();
200         if (!server_happy())
201         {
202             g_printf("%s\n", l);
203             continue;
204         }
205         s = l;
206         if (strncmp_const_skip(l, "201-", s, ch) != 0) {
207             err = _("bad reply: not 201-");
208             continue;
209         }
210         ch = *s++;
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_quoted_string(s, ch);
234         tape_undo = s - 1;
235         tape_undo_ch = *tape_undo;
236         *tape_undo = '\0';
237         tape = unquote_string(tape);
238
239         if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
240             long long fileno_ = (long long)0;
241             skip_whitespace(s, ch);
242             if(ch == '\0' || sscanf(s - 1, "%lld", &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(tape);
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         g_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         g_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         g_fprintf(fp, "%s %s\n", item->date, quoted);
322         amfree(quoted);
323     }
324     apclose(fp);
325 }