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