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