88c0cea74846d91006cb2cb67c2e8bf15f72025d
[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.19 2006/03/09 20:06:11 johnfranks Exp $
28  *
29  * implements the directory-display related commands in amrecover
30  */
31
32 #include "amanda.h"
33 #include "amrecover.h"
34
35 static DIR_ITEM *dir_list = NULL;
36
37 DIR_ITEM *get_dir_list P((void))
38 {
39     return dir_list;
40 }
41
42 DIR_ITEM *get_next_dir_item(this)
43 DIR_ITEM *this;
44 {
45     return this->next;
46 }
47
48
49 void clear_dir_list P((void))
50 {
51     free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
52     dir_list = NULL;
53 }
54
55 void free_dir_item P((DIR_ITEM *item)) {
56     DIR_ITEM *next;
57
58     while (item != NULL) {
59         next = item->next;
60         amfree(item->date);
61         amfree(item->tape);
62         amfree(item->path);
63         amfree(item);
64         item = next;
65     }
66 }
67
68 /* add item to list if path not already on list */
69 static int add_dir_list_item(date, level, tape, fileno, path)
70 char *date;
71 int level;
72 char *tape;
73 int fileno;
74 char *path;
75 {
76     DIR_ITEM *next;
77
78     dbprintf(("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%d\" \"%s\"\n",
79               date, level, tape, fileno, path));
80
81     next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
82     memset(next, 0, sizeof(DIR_ITEM));
83
84     next->date = stralloc(date);
85     next->level = level;
86     next->tape = stralloc(tape);
87     next->fileno = fileno;
88     next->path = stralloc(path);
89
90     next->next = dir_list;
91     dir_list = next;
92
93     return 0;
94 }
95
96
97 void list_disk_history P((void))
98 {
99     if (converse("DHST") == -1)
100         exit(1);
101 }
102
103
104 void suck_dir_list_from_server P((void))
105 {
106     char *cmd = NULL;
107     char *err = NULL;
108     int i;
109     char *l = NULL;
110     char *date, *date_undo, date_undo_ch = '\0';
111     int level, fileno;
112     char *tape, *tape_undo, tape_undo_ch = '\0';
113     char *dir;
114     char *disk_path_slash = NULL;
115     char *disk_path_slash_dot = NULL;
116     char *s;
117     int ch;
118
119     if (disk_path == NULL) {
120         printf("Directory must be set before getting listing\n");
121         return;
122     } else if(strcmp(disk_path, "/") == 0) {
123         disk_path_slash = stralloc(disk_path);
124     } else {
125         disk_path_slash = stralloc2(disk_path, "/");
126     }
127
128     clear_dir_list();
129
130     cmd = stralloc2("OLSD ", disk_path);
131     if (send_command(cmd) == -1) {
132         amfree(cmd);
133         amfree(disk_path_slash);
134         exit(1);
135     }
136     amfree(cmd);
137     /* skip preamble */
138     if ((i = get_reply_line()) == -1) {
139         amfree(disk_path_slash);
140         exit(1);
141     }
142     if (i == 0)                         /* assume something wrong! */
143     {
144         amfree(disk_path_slash);
145         l = reply_line();
146         printf("%s\n", l);
147         return;
148     }
149     disk_path_slash_dot = stralloc2(disk_path_slash, ".");
150     amfree(cmd);
151     amfree(err);
152     date_undo = tape_undo = NULL;
153     /* skip the last line -- duplicate of the preamble */
154     while ((i = get_reply_line()) != 0)
155     {
156         if (i == -1) {
157             amfree(disk_path_slash_dot);
158             amfree(disk_path_slash);
159             exit(1);
160         }
161         if(err) {
162             if(cmd == NULL) {
163                 if(tape_undo) *tape_undo = tape_undo_ch;
164                 date_undo = tape_undo = NULL;
165                 cmd = stralloc(l);      /* save for the error report */
166             }
167             continue;                   /* throw the rest of the lines away */
168         }
169         l = reply_line();
170         if (!server_happy())
171         {
172             printf("%s\n", l);
173             continue;
174         }
175 #define sc "201-"
176         if (strncmp(l, sc, sizeof(sc)-1) != 0) {
177             err = "bad reply: not 201-";
178             continue;
179         }
180         s = l + sizeof(sc)-1;
181         ch = *s++;
182 #undef sc
183         skip_whitespace(s, ch);
184         if(ch == '\0') {
185             err = "bad reply: missing date field";
186             continue;
187         }
188         date = s - 1;
189         skip_non_whitespace(s, ch);
190         date_undo = s - 1;
191         date_undo_ch = *date_undo;
192         *date_undo = '\0';
193
194         skip_whitespace(s, ch);
195         if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
196             err = "bad reply: cannot parse level field";
197             continue;
198         }
199         skip_integer(s, ch);
200
201         skip_whitespace(s, ch);
202         if(ch == '\0') {
203             err = "bad reply: missing tape field";
204             continue;
205         }
206         tape = s - 1;
207         skip_non_whitespace(s, ch);
208         tape_undo = s - 1;
209         tape_undo_ch = *tape_undo;
210         *tape_undo = '\0';
211
212         if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
213             skip_whitespace(s, ch);
214             if(ch == '\0' || sscanf(s - 1, "%d", &fileno) != 1) {
215                 err = "bad reply: cannot parse fileno field";
216                 continue;
217             }
218             skip_integer(s, ch);
219         }
220         else {
221             fileno = -1;
222         }
223
224         skip_whitespace(s, ch);
225         if(ch == '\0') {
226             err = "bad reply: missing directory field";
227             continue;
228         }
229         dir = s - 1;
230
231         /* add a '.' if it a the entry for the current directory */
232         if(strcmp(disk_path,dir)==0 || strcmp(disk_path_slash,dir)==0) {
233             dir = disk_path_slash_dot;
234         }
235         add_dir_list_item(date, level, tape, fileno, dir);
236     }
237     amfree(disk_path_slash_dot);
238     amfree(disk_path_slash);
239     if(!server_happy()) {
240         puts(reply_line());
241     } else if(err) {
242         if(*err) {
243             puts(err);
244         }
245         puts(cmd);
246         clear_dir_list();
247     }
248     amfree(cmd);
249 }
250
251
252 void list_directory P((void))
253 {
254     size_t i;
255     DIR_ITEM *item;
256     FILE *fp;
257     char *pager;
258     char *pager_command;
259
260     if (disk_path == NULL) {
261         printf("Must select a disk before listing files\n");
262         return;
263     }
264
265     if ((pager = getenv("PAGER")) == NULL)
266     {
267         pager = "more";
268     }
269     /*
270      * Set up the pager command so if the pager is terminated, we do
271      * not get a SIGPIPE back.
272      */
273     pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
274     if ((fp = popen(pager_command, "w")) == NULL)
275     {
276         printf("Warning - can't pipe through %s\n", pager);
277         fp = stdout;
278     }
279     amfree(pager_command);
280     i = strlen(disk_path);
281     if (i != 1)
282         i++;                            /* so disk_path != "/" */
283     for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item))
284         fprintf(fp, "%s %s\n", item->date, item->path+i);
285     apclose(fp);
286 }