Imported Upstream version 2.5.1
[debian/amanda] / restore-src / restore.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: restore.c,v 1.52 2006/08/23 11:41:54 martinea Exp $
28  *
29  * retrieves files from an amanda tape
30  */
31
32 #include "amanda.h"
33 #include "tapeio.h"
34 #include "util.h"
35 #include "restore.h"
36 #include "find.h"
37 #include "changer.h"
38 #include "logfile.h"
39 #include "fileheader.h"
40 #include "arglist.h"
41 #include <signal.h>
42
43 #define LOAD_STOP    -1
44 #define LOAD_CHANGER -2
45
46 int file_number;
47
48 /* stuff we're stuck having global */
49 static size_t blocksize = (size_t)SSIZE_MAX;
50 static char *cur_tapedev = NULL;
51 static char *searchlabel = NULL;
52 static int backwards;
53 static int exitassemble = 0;
54 static int tapefd;
55
56 char *rst_conf_logdir = NULL;
57 char *rst_conf_logfile = NULL;
58 char *curslot = NULL;
59
60 typedef struct open_output_s {
61     struct open_output_s *next;
62     dumpfile_t *file;
63     int lastpartnum;
64     pid_t comp_enc_pid;
65     int outfd;
66 } open_output_t;
67
68 typedef struct dumplist_s {
69     struct dumplist_s *next;
70     dumpfile_t *file;
71 } dumplist_t;
72
73 typedef struct seentapes_s {
74     struct seentapes_s *next;
75     char *slotstr;
76     char *label;
77     dumplist_t *files;
78 } seentapes_t;
79
80 static open_output_t *open_outputs = NULL;
81 static dumplist_t *alldumps_list = NULL;
82
83 /* local functions */
84
85 static ssize_t get_block(int tapefd, char *buffer, int isafile);
86 static void append_file_to_fd(char *filename, int fd);
87 static int headers_equal(dumpfile_t *file1, dumpfile_t *file2, int ignore_partnums);
88 static int already_have_dump(dumpfile_t *file);
89 static void handle_sigint(int sig);
90 static int scan_init(void *ud, int rc, int ns, int bk, int s);
91 int loadlabel_slot(void *ud, int rc, char *slotstr, char *device);
92 void drain_file(int tapefd, rst_flags_t *flags);
93 char *label_of_current_slot(char *cur_tapedev, FILE *prompt_out,
94                             int *tapefd, dumpfile_t *file, rst_flags_t *flags,
95                             am_feature_t *their_features,
96                             ssize_t *read_result, tapelist_t *desired_tape);
97
98 int load_next_tape(char **cur_tapedev, FILE *prompt_out, int backwards,
99                    rst_flags_t *flags, am_feature_t *their_features,
100                    tapelist_t *desired_tape);
101 int load_manual_tape(char **cur_tapedev, FILE *prompt_out,
102                      rst_flags_t *flags, am_feature_t *their_features,
103                      tapelist_t *desired_tape);
104 void search_a_tape(char *cur_tapedev, FILE *prompt_out, rst_flags_t *flags,
105                    am_feature_t *their_features, tapelist_t *desired_tape,
106                    int isafile, match_list_t *match_list,
107                    seentapes_t *tape_seen, dumpfile_t *file,
108                    dumpfile_t *prev_rst_file, dumpfile_t *tapestart,
109                    int slot_num, ssize_t *read_result);
110
111 /*
112  * We might want to flush any open dumps and unmerged splits before exiting
113  * on SIGINT, so do so.
114  */
115 static void
116 handle_sigint(
117     int         sig)
118 {
119     (void)sig;  /* Quiet unused parameter warning */
120
121     flush_open_outputs(exitassemble, NULL);
122     if(rst_conf_logfile) unlink(rst_conf_logfile);
123     exit(0);
124 }
125
126 int
127 lock_logfile(void)
128 {
129     rst_conf_logdir = getconf_str(CNF_LOGDIR);
130     if (*rst_conf_logdir == '/') {
131         rst_conf_logdir = stralloc(rst_conf_logdir);
132     } else {
133         rst_conf_logdir = stralloc2(config_dir, rst_conf_logdir);
134     }
135     rst_conf_logfile = vstralloc(rst_conf_logdir, "/log", NULL);
136     if (access(rst_conf_logfile, F_OK) == 0) {
137         dbprintf(("%s exists: amdump or amflush is already running, "
138                   "or you must run amcleanup\n", rst_conf_logfile));
139         return 0;
140     }
141     log_add(L_INFO, get_pname());
142     return 1;
143 }
144
145 /*
146  * Return 1 if the two fileheaders match in name, disk, type, split chunk part
147  * number, and datestamp, and 0 if not.  The part number can be optionally
148  * ignored.
149  */
150 int
151 headers_equal(
152     dumpfile_t *file1,
153     dumpfile_t *file2,
154     int         ignore_partnums)
155 {
156     if(!file1 || !file2) return(0);
157     
158     if(file1->dumplevel == file2->dumplevel &&
159            file1->type == file2->type &&
160            !strcmp(file1->datestamp, file2->datestamp) &&
161            !strcmp(file1->name, file2->name) &&
162            !strcmp(file1->disk, file2->disk) &&
163            (ignore_partnums || file1->partnum == file2->partnum)){
164         return(1);
165     }
166     return(0);
167 }
168
169
170 /*
171  * See whether we're already pulled an exact copy of the given file (chunk
172  * number and all).  Returns 0 if not, 1 if so.
173  */
174 int
175 already_have_dump(
176     dumpfile_t *file)
177 {
178     dumplist_t *fileentry = NULL;
179
180     if(!file) return(0);
181     for(fileentry=alldumps_list;fileentry;fileentry=fileentry->next){
182         if(headers_equal(file, fileentry->file, 0)) return(1);
183     }
184     return(0);
185 }
186
187 /*
188  * Open the named file and append its contents to the (hopefully open) file
189  * descriptor supplies.
190  */
191 static void
192 append_file_to_fd(
193     char *      filename,
194     int         fd)
195 {
196     ssize_t bytes_read;
197     ssize_t s;
198     off_t wc = (off_t)0;
199     char *buffer;
200
201     if(blocksize == SIZE_MAX)
202         blocksize = DISK_BLOCK_BYTES;
203     buffer = alloc(blocksize);
204
205     if((tapefd = open(filename, O_RDONLY)) == -1) {
206         error("can't open %s: %s", filename, strerror(errno));
207         /*NOTREACHED*/
208     }
209
210     for (;;) {
211         bytes_read = get_block(tapefd, buffer, 1); /* same as isafile = 1 */
212         if(bytes_read < 0) {
213             error("read error: %s", strerror(errno));
214             /*NOTREACHED*/
215         }
216
217         if (bytes_read == 0)
218                 break;
219
220         s = fullwrite(fd, buffer, (size_t)bytes_read);
221         if (s < bytes_read) {
222             fprintf(stderr,"Error (%s) offset " OFF_T_FMT "+" OFF_T_FMT ", wrote " OFF_T_FMT "\n",
223                     strerror(errno), (OFF_T_FMT_TYPE)wc,
224                     (OFF_T_FMT_TYPE)bytes_read, (OFF_T_FMT_TYPE)s);
225             if (s < 0) {
226                 if((errno == EPIPE) || (errno == ECONNRESET)) {
227                     error("%s: pipe reader has quit in middle of file.",
228                         get_pname());
229                     /*NOTREACHED*/
230                 }
231                 error("restore: write error = %s", strerror(errno));
232                 /*NOTREACHED*/
233             }
234             error("Short write: wrote %d bytes expected %d.", s, bytes_read);
235             /*NOTREACHCED*/
236         }
237         wc += (off_t)bytes_read;
238     }
239
240     amfree(buffer);
241     aclose(tapefd);
242 }
243
244 /*
245  * Tape changer support routines, stolen brazenly from amtape
246  */
247 static int 
248 scan_init(
249      void *     ud,
250      int        rc,
251      int        ns,
252      int        bk,
253      int        s)
254 {
255     (void)ud;   /* Quiet unused parameter warning */
256     (void)ns;   /* Quiet unused parameter warning */
257     (void)s;    /* Quiet unused parameter warning */
258
259     if(rc) {
260         error("could not get changer info: %s", changer_resultstr);
261         /*NOTREACHED*/
262     }
263     backwards = bk;
264
265     return 0;
266 }
267
268 int
269 loadlabel_slot(
270      void *     ud,
271      int        rc,
272      char *     slotstr,
273      char *     device)
274 {
275     char *errstr;
276     char *datestamp = NULL;
277     char *label = NULL;
278
279     (void)ud;   /* Quiet unused parameter warning */
280
281     if(rc > 1) {
282         error("could not load slot %s: %s", slotstr, changer_resultstr);
283         /*NOTREACHED*/
284     } else if(rc == 1) {
285         fprintf(stderr, "%s: slot %s: %s\n",
286                 get_pname(), slotstr, changer_resultstr);
287     } else if((errstr = tape_rdlabel(device, &datestamp, &label)) != NULL) {
288         fprintf(stderr, "%s: slot %s: %s\n", get_pname(), slotstr, errstr);
289     } else {
290         if(strlen(datestamp)>8)
291             fprintf(stderr, "%s: slot %s: date %-14s label %s",
292                     get_pname(), slotstr, datestamp, label);
293         else
294             fprintf(stderr, "%s: slot %s: date %-8s label %s",
295                     get_pname(), slotstr, datestamp, label);
296         if(strcmp(label, FAKE_LABEL) != 0
297            && strcmp(label, searchlabel) != 0)
298             fprintf(stderr, " (wrong tape)\n");
299         else {
300             fprintf(stderr, " (exact label match)\n");
301             if((errstr = tape_rewind(device)) != NULL) {
302                 fprintf(stderr,
303                         "%s: could not rewind %s: %s",
304                         get_pname(), device, errstr);
305                 amfree(errstr);
306             }
307             amfree(cur_tapedev);
308             curslot = newstralloc(curslot, slotstr);
309             amfree(datestamp);
310             amfree(label);
311             if(device)
312                 cur_tapedev = stralloc(device);
313             return 1;
314         }
315     }
316     amfree(datestamp);
317     amfree(label);
318
319     amfree(cur_tapedev);
320     curslot = newstralloc(curslot, slotstr);
321     if(!device) return(1);
322     cur_tapedev = stralloc(device);
323
324     return 0;
325 }
326
327
328 /* non-local functions follow */
329
330
331
332 /*
333  * Check whether we've read all of the preceding parts of a given split dump,
334  * generally used to see if we're done and can close the thing.
335  */
336 int
337 have_all_parts (
338     dumpfile_t *file,
339     int         upto)
340 {
341     int c;
342     int *foundparts = NULL;
343     dumplist_t *fileentry = NULL;
344
345     if(!file || file->partnum < 1) return(0);
346
347     if(upto < 1) upto = file->totalparts;
348
349     foundparts = alloc(SIZEOF(*foundparts) * upto); 
350     for(c = 0 ; c< upto; c++) foundparts[c] = 0;
351     
352     for(fileentry=alldumps_list;fileentry; fileentry=fileentry->next){
353         dumpfile_t *cur_file = fileentry->file;
354         if(headers_equal(file, cur_file, 1)){
355             if(cur_file->partnum > upto){
356                 amfree(foundparts);
357                 return(0);
358             }
359
360             foundparts[cur_file->partnum - 1] = 1;
361         }
362     }
363
364     for(c = 0 ; c< upto; c++){
365         if(!foundparts[c]){
366             amfree(foundparts);
367             return(0);
368         }
369     }
370     
371     amfree(foundparts);
372     return(1);
373 }
374
375 /*
376  * Free up the open filehandles and memory we were using to track in-progress
377  * dumpfiles (generally for split ones we're putting back together).  If
378  * applicable, also find the ones that are continuations of one another and
379  * string them together.  If given an optional file header argument, flush
380  * only that dump and do not flush/free any others.
381  */
382 void
383 flush_open_outputs(
384     int         reassemble,
385     dumpfile_t *only_file)
386 {
387     open_output_t *cur_out = NULL, *prev = NULL;
388     find_result_t *sorted_files = NULL;
389     amwait_t compress_status;
390
391     if(!only_file){
392         fprintf(stderr, "\n");
393     }
394
395     /*
396      * Deal with any split dumps we've been working on, appending pieces
397      * that haven't yet been appended and closing filehandles we've been
398      * holding onto.
399      */
400     if(reassemble){
401         find_result_t *cur_find_res = NULL;
402         int outfd = -1, lastpartnum = -1;
403         dumpfile_t *main_file = NULL;
404         cur_out = open_outputs;
405         
406         /* stick the dumpfile_t's into a list find_result_t's so that we can
407            abuse existing sort functionality */
408         for(cur_out=open_outputs; cur_out; cur_out=cur_out->next){
409             find_result_t *cur_find_res = NULL;
410             dumpfile_t *cur_file = cur_out->file;
411             /* if we requested a particular file, do only that one */
412             if(only_file && !headers_equal(cur_file, only_file, 1)){
413                 continue;
414             }
415             cur_find_res = alloc(SIZEOF(find_result_t));
416             memset(cur_find_res, '\0', SIZEOF(find_result_t));
417             cur_find_res->timestamp = stralloc(cur_file->datestamp);
418             cur_find_res->hostname = stralloc(cur_file->name);
419             cur_find_res->diskname = stralloc(cur_file->disk);
420             cur_find_res->level = cur_file->dumplevel;
421             if(cur_file->partnum < 1) cur_find_res->partnum = stralloc("--");
422             else{
423                 char part_str[NUM_STR_SIZE];
424                 snprintf(part_str, SIZEOF(part_str), "%d", cur_file->partnum);
425                 cur_find_res->partnum = stralloc(part_str);
426             }
427             cur_find_res->user_ptr = (void*)cur_out;
428
429             cur_find_res->next = sorted_files;
430             sorted_files = cur_find_res;
431         }
432         sort_find_result("hkdlp", &sorted_files);
433
434         /* now we have an in-order list of the files we need to concatenate */
435         cur_find_res = sorted_files;
436         for(cur_find_res=sorted_files;
437                 cur_find_res;
438                 cur_find_res=cur_find_res->next){
439             dumpfile_t *cur_file = NULL;
440             cur_out = (open_output_t*)cur_find_res->user_ptr;
441             cur_file = cur_out->file;
442
443             /* if we requested a particular file, do only that one */
444             if(only_file && !headers_equal(cur_file, only_file, 1)){
445                 continue;
446             }
447
448             if(cur_file->type == F_SPLIT_DUMPFILE) {
449                 /* is it a continuation of one we've been writing? */
450                 if(main_file && cur_file->partnum > lastpartnum &&
451                         headers_equal(cur_file, main_file, 1)){
452                     char *cur_filename;
453                     char *main_filename;
454
455                     /* effectively changing filehandles */
456                     aclose(cur_out->outfd);
457                     cur_out->outfd = outfd;
458
459                     cur_filename  = make_filename(cur_file);
460                     main_filename = make_filename(main_file);
461                     fprintf(stderr, "Merging %s with %s\n",
462                             cur_filename, main_filename);
463                     append_file_to_fd(cur_filename, outfd);
464                     if(unlink(cur_filename) < 0){
465                         fprintf(stderr, "Failed to unlink %s: %s\n",
466                                      cur_filename, strerror(errno));
467                     }
468                     amfree(cur_filename);
469                     amfree(main_filename);
470                 }
471                 /* or a new file? */
472                 else {
473                     if(outfd >= 0) aclose(outfd);
474                     amfree(main_file);
475                     main_file = alloc(SIZEOF(dumpfile_t));
476                     memcpy(main_file, cur_file, SIZEOF(dumpfile_t));
477                     outfd = cur_out->outfd;
478                     if(outfd < 0) {
479                         char *cur_filename = make_filename(cur_file);
480                         open(cur_filename, O_RDWR|O_APPEND);
481                         if (outfd < 0) {
482                           error("Couldn't open %s for appending: %s",
483                                 cur_filename, strerror(errno));
484                           /*NOTREACHED*/
485                         }
486                         amfree(cur_filename);
487                     }
488                 }
489                 lastpartnum = cur_file->partnum;
490             }
491             else {
492                 aclose(cur_out->outfd);
493             }
494         }
495         if(outfd >= 0) {
496             aclose(outfd);
497         }
498
499         amfree(main_file);
500         free_find_result(&sorted_files);
501     }
502
503     /*
504      * Now that the split dump closure is done, free up resources we don't
505      * need anymore.
506      */
507     for(cur_out=open_outputs; cur_out; cur_out=cur_out->next){
508         dumpfile_t *cur_file = NULL;
509         amfree(prev);
510         cur_file = cur_out->file;
511         /* if we requested a particular file, do only that one */
512         if(only_file && !headers_equal(cur_file, only_file, 1)){
513             continue;
514         }
515         if(!reassemble) {
516             aclose(cur_out->outfd);
517         }
518
519         if(cur_out->comp_enc_pid > 0){
520             waitpid(cur_out->comp_enc_pid, &compress_status, 0);
521         }
522         amfree(cur_out->file);
523         prev = cur_out;
524     }
525
526     open_outputs = NULL;
527 }
528
529 /*
530  * Turn a fileheader into a string suited for use on the filesystem.
531  */
532 char *
533 make_filename(
534     dumpfile_t *file)
535 {
536     char number[NUM_STR_SIZE];
537     char part[NUM_STR_SIZE];
538     char totalparts[NUM_STR_SIZE];
539     char *sfn = NULL;
540     char *fn = NULL;
541     char *pad = NULL;
542     size_t padlen = 0;
543
544     snprintf(number, SIZEOF(number), "%d", file->dumplevel);
545     snprintf(part, SIZEOF(part), "%d", file->partnum);
546
547     if(file->totalparts < 0) {
548         snprintf(totalparts, SIZEOF(totalparts), "UNKNOWN");
549     }
550     else {
551         snprintf(totalparts, SIZEOF(totalparts), "%d", file->totalparts);
552     }
553     padlen = strlen(totalparts) + 1 - strlen(part);
554     pad = alloc(padlen);
555     memset(pad, '0', padlen);
556     pad[padlen - 1] = '\0';
557
558     snprintf(part, SIZEOF(part), "%s%d", pad, file->partnum);
559
560     sfn = sanitise_filename(file->disk);
561     fn = vstralloc(file->name,
562                    ".",
563                    sfn, 
564                    ".",
565                    file->datestamp,
566                    ".",
567                    number,
568                    NULL);
569     if (file->partnum > 0) {
570         vstrextend(&fn, ".", part, NULL);
571     }
572     amfree(sfn);
573     amfree(pad);
574     return fn;
575 }
576
577
578 /*
579  * XXX Making this thing a lib functiong broke a lot of assumptions everywhere,
580  * but I think I've found them all.  Maybe.  Damn globals all over the place.
581  */
582
583 static ssize_t
584 get_block(
585     int         tapefd,
586     char *      buffer,
587     int         isafile)
588 {
589     if(isafile)
590         return (fullread(tapefd, buffer, blocksize));
591
592     return(tapefd_read(tapefd, buffer, blocksize));
593 }
594
595 /*
596  * Returns 1 if the current dump file matches the hostname and diskname
597  * regular expressions given on the command line, 0 otherwise.  As a 
598  * special case, empty regexs are considered equivalent to ".*": they 
599  * match everything.
600  */
601
602 int
603 disk_match(
604     dumpfile_t *file,
605     char *      datestamp,
606     char *      hostname,
607     char *      diskname,
608     char *      level)
609 {
610     char level_str[NUM_STR_SIZE];
611     snprintf(level_str, SIZEOF(level_str), "%d", file->dumplevel);
612
613     if(file->type != F_DUMPFILE && file->type != F_SPLIT_DUMPFILE) return 0;
614
615     if((*hostname == '\0' || match_host(hostname, file->name)) &&
616        (*diskname == '\0' || match_disk(diskname, file->disk)) &&
617        (*datestamp == '\0' || match_datestamp(datestamp, file->datestamp)) &&
618        (*level == '\0' || match_level(level, level_str)))
619         return 1;
620     else
621         return 0;
622 }
623
624
625 /*
626  * Reads the first block of a tape file.
627  */
628
629 ssize_t
630 read_file_header(
631     dumpfile_t *        file,
632     int                 tapefd,
633     int                 isafile,
634     rst_flags_t *       flags)
635 {
636     ssize_t bytes_read;
637     char *buffer;
638   
639     if(flags->blocksize > 0)
640         blocksize = (size_t)flags->blocksize;
641     else if(blocksize == (size_t)SSIZE_MAX)
642         blocksize = DISK_BLOCK_BYTES;
643     buffer = alloc(blocksize);
644
645     bytes_read = get_block(tapefd, buffer, isafile);
646     if(bytes_read < 0) {
647         fprintf(stderr, "%s: error reading file header: %s\n",
648                 get_pname(), strerror(errno));
649         file->type = F_UNKNOWN;
650     } else if((size_t)bytes_read < blocksize) {
651         if(bytes_read == 0) {
652             fprintf(stderr, "%s: missing file header block\n", get_pname());
653         } else {
654             fprintf(stderr, "%s: short file header block: " OFF_T_FMT " byte%s\n",
655                     get_pname(), (OFF_T_FMT_TYPE)bytes_read, (bytes_read == 1) ? "" : "s");
656         }
657         file->type = F_UNKNOWN;
658     } else {
659         parse_file_header(buffer, file, (size_t)bytes_read);
660     }
661     amfree(buffer);
662     return bytes_read;
663 }
664
665
666 void
667 drain_file(
668     int                 tapefd,
669     rst_flags_t *       flags)
670 {
671     ssize_t bytes_read;
672     char *buffer;
673
674     if(flags->blocksize)
675         blocksize = (size_t)flags->blocksize;
676     else if(blocksize == (size_t)SSIZE_MAX)
677         blocksize = DISK_BLOCK_BYTES;
678     buffer = alloc(blocksize);
679
680     do {
681        bytes_read = get_block(tapefd, buffer, 0);
682        if(bytes_read < 0) {
683            error("drain read error: %s", strerror(errno));
684            /*NOTREACHED*/
685        }
686     } while (bytes_read > 0);
687
688     amfree(buffer);
689 }
690
691 /*
692  * Restore the current file from tape.  Depending on the settings of
693  * the command line flags, the file might need to be compressed or
694  * uncompressed.  If so, a pipe through compress or uncompress is set
695  * up.  The final output usually goes to a file named host.disk.date.lev,
696  * but with the -p flag the output goes to stdout (and presumably is
697  * piped to restore).
698  */
699
700 ssize_t
701 restore(
702     dumpfile_t *        file,
703     char *              filename,
704     int                 tapefd,
705     int                 isafile,
706     rst_flags_t *       flags)
707 {
708     int dest = -1, out;
709     ssize_t s;
710     int file_is_compressed;
711     int is_continuation = 0;
712     int check_for_aborted = 0;
713     char *tmp_filename = NULL, *final_filename = NULL;
714     struct stat statinfo;
715     open_output_t *myout = NULL, *oldout = NULL;
716     dumplist_t *tempdump = NULL, *fileentry = NULL;
717     char *buffer;
718     int need_compress=0, need_uncompress=0, need_decrypt=0;
719     int stage=0;
720     ssize_t bytes_read;
721     struct pipeline {
722         int     pipe[2];
723     } pipes[3];
724
725     memset(pipes, -1, SIZEOF(pipes));
726     if(flags->blocksize)
727         blocksize = (size_t)flags->blocksize;
728     else if(blocksize == (size_t)SSIZE_MAX)
729         blocksize = DISK_BLOCK_BYTES;
730
731     if(already_have_dump(file)){
732         char *filename = make_filename(file);
733         fprintf(stderr, " *** Duplicate file %s, one is probably an aborted write\n", filename);
734         amfree(filename);
735         check_for_aborted = 1;
736     }
737
738     /* store a shorthand record of this dump */
739     tempdump = alloc(SIZEOF(dumplist_t));
740     tempdump->file = alloc(SIZEOF(dumpfile_t));
741     tempdump->next = NULL;
742     memcpy(tempdump->file, file, SIZEOF(dumpfile_t));
743
744     /*
745      * If we're appending chunked files to one another, and if this is a
746      * continuation of a file we just restored, and we've still got the
747      * output handle from that previous restore, we're golden.  Phew.
748      */
749     if(flags->inline_assemble && file->type == F_SPLIT_DUMPFILE){
750         myout = open_outputs;
751         while(myout != NULL){
752             if(myout->file->type == F_SPLIT_DUMPFILE &&
753                     headers_equal(file, myout->file, 1)){
754                 if(file->partnum == myout->lastpartnum + 1){
755                     is_continuation = 1;
756                     break;
757                 }
758             }
759             myout = myout->next;
760         }
761         if(myout != NULL) myout->lastpartnum = file->partnum;
762         else if(file->partnum != 1){
763             fprintf(stderr, "%s:      Chunk out of order, will save to disk and append to output.\n", get_pname());
764             flags->pipe_to_fd = -1;
765             flags->compress = 0;
766             flags->leave_comp = 1;
767         }
768         if(myout == NULL){
769             myout = alloc(SIZEOF(open_output_t));
770             memset(myout, 0, SIZEOF(open_output_t));
771         }
772     }
773     else{
774       myout = alloc(SIZEOF(open_output_t));
775       memset(myout, 0, SIZEOF(open_output_t));
776     }
777
778
779     if(is_continuation && flags->pipe_to_fd == -1){
780         char *filename;
781         filename = make_filename(myout->file);
782         fprintf(stderr, "%s:      appending to %s\n", get_pname(),
783                 filename);
784         amfree(filename);
785     }
786
787     /* adjust compression flag */
788     file_is_compressed = file->compressed;
789     if(!flags->compress && file_is_compressed && !known_compress_type(file)) {
790         fprintf(stderr, 
791                 "%s: unknown compression suffix %s, can't uncompress\n",
792                 get_pname(), file->comp_suffix);
793         flags->compress = 1;
794     }
795
796     /* set up final destination file */
797
798     if(is_continuation && myout != NULL) {
799       out = myout->outfd;
800     } else {
801       if(flags->pipe_to_fd != -1) {
802           dest = flags->pipe_to_fd;     /* standard output */
803       } else {
804           char *filename_ext = NULL;
805   
806           if(flags->compress) {
807               filename_ext = file_is_compressed ? file->comp_suffix
808                                               : COMPRESS_SUFFIX;
809           } else if(flags->raw) {
810               filename_ext = ".RAW";
811           } else {
812               filename_ext = "";
813           }
814           filename_ext = stralloc2(filename, filename_ext);
815           tmp_filename = stralloc(filename_ext); 
816           if(flags->restore_dir != NULL) {
817               char *tmpstr = vstralloc(flags->restore_dir, "/",
818                                        tmp_filename, NULL);
819               amfree(tmp_filename);
820               tmp_filename = tmpstr;
821           } 
822           final_filename = stralloc(tmp_filename); 
823           tmp_filename = newvstralloc(tmp_filename, ".tmp", NULL);
824           if((dest = open(tmp_filename, (O_CREAT | O_RDWR | O_TRUNC),
825                           CREAT_MODE)) < 0) {
826               error("could not create output file %s: %s",
827                     tmp_filename, strerror(errno));
828               /*NOTREACHED*/
829           }
830           amfree(filename_ext);
831       }
832   
833       out = dest;
834     }
835
836     /*
837      * If -r or -h, write the header before compress or uncompress pipe.
838      * Only write DISK_BLOCK_BYTES, regardless of how much was read.
839      * This makes the output look like a holding disk image, and also
840      * makes it easier to remove the header (e.g. in amrecover) since
841      * it has a fixed size.
842      */
843     if(flags->raw || (flags->headers && !is_continuation)) {
844         ssize_t w;
845         dumpfile_t tmp_hdr;
846
847         if(flags->compress && !file_is_compressed) {
848             file->compressed = 1;
849             snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
850                         " %s %s |", UNCOMPRESS_PATH,
851 #ifdef UNCOMPRESS_OPT
852                         UNCOMPRESS_OPT
853 #else
854                         ""
855 #endif
856                         );
857             strncpy(file->comp_suffix,
858                     COMPRESS_SUFFIX,
859                     SIZEOF(file->comp_suffix)-1);
860             file->comp_suffix[SIZEOF(file->comp_suffix)-1] = '\0';
861         }
862
863         memcpy(&tmp_hdr, file, SIZEOF(dumpfile_t));
864
865         /* remove CONT_FILENAME from header */
866         memset(file->cont_filename,'\0',SIZEOF(file->cont_filename));
867         file->blocksize = DISK_BLOCK_BYTES;
868
869         /*
870          * Dumb down split file headers as well, so that older versions of
871          * things like amrecover won't gag on them.
872          */
873         if(file->type == F_SPLIT_DUMPFILE && flags->mask_splits){
874             file->type = F_DUMPFILE;
875         }
876
877         buffer = alloc(DISK_BLOCK_BYTES);
878         build_header(buffer, file, DISK_BLOCK_BYTES);
879
880         if((w = fullwrite(out, buffer, DISK_BLOCK_BYTES)) != DISK_BLOCK_BYTES) {
881             if(w < 0) {
882                 error("write error: %s", strerror(errno));
883                 /*NOTREACHED*/
884             } else {
885                 error("write error: %d instead of %d", w, DISK_BLOCK_BYTES);
886                 /*NOTREACHED*/
887             }
888         }
889         amfree(buffer);
890
891         memcpy(file, &tmp_hdr, SIZEOF(dumpfile_t));
892     }
893  
894     /* find out if compression or uncompression is needed here */
895     if(flags->compress && !file_is_compressed && !is_continuation
896           && !flags->leave_comp
897           && (flags->inline_assemble || file->type != F_SPLIT_DUMPFILE))
898        need_compress=1;
899        
900     if(!flags->raw && !flags->compress && file_is_compressed
901           && !is_continuation && !flags->leave_comp && (flags->inline_assemble
902           || file->type != F_SPLIT_DUMPFILE))
903        need_uncompress=1;   
904
905     if(!flags->raw && file->encrypted)
906        need_decrypt=1;
907    
908     /* Setup pipes for decryption / compression / uncompression  */
909     stage = 0;
910     if (need_decrypt) {
911       if (pipe(&pipes[stage].pipe[0]) < 0) {
912         error("error [pipe[%d]: %s]", stage, strerror(errno));
913         /*NOTREACHED*/
914       }
915       stage++;
916     }
917
918     if (need_compress || need_uncompress) {
919       if (pipe(&pipes[stage].pipe[0]) < 0) {
920         error("error [pipe[%d]: %s]", stage, strerror(errno));
921         /*NOTREACHED*/
922       }
923       stage++;
924     }
925     pipes[stage].pipe[0] = -1; 
926     pipes[stage].pipe[1] = out; 
927
928     stage = 0;
929
930     /* decrypt first if it's encrypted and no -r */
931     if(need_decrypt) {
932       switch(myout->comp_enc_pid = fork()) {
933       case -1:
934         error("could not fork for decrypt: %s", strerror(errno));
935         /*NOTREACHED*/
936
937       default:
938         aclose(pipes[stage].pipe[0]);
939         aclose(pipes[stage+1].pipe[1]);
940         stage++;
941         break;
942
943       case 0:
944         if(dup2(pipes[stage].pipe[0], 0) == -1) {
945             error("error decrypt stdin [dup2 %d %d: %s]", stage,
946                 pipes[stage].pipe[0], strerror(errno));
947                 /*NOTREACHED*/
948         }
949
950         if(dup2(pipes[stage+1].pipe[1], 1) == -1) {
951             error("error decrypt stdout [dup2 %d %d: %s]", stage + 1,
952                 pipes[stage+1].pipe[1], strerror(errno));
953                 /*NOTREACHED*/
954         }
955
956         safe_fd(-1, 0);
957         if (*file->srv_encrypt) {
958           (void) execlp(file->srv_encrypt, file->srv_encrypt,
959                         file->srv_decrypt_opt, (char *)NULL);
960           error("could not exec %s: %s", file->srv_encrypt, strerror(errno));
961           /*NOTREACHED*/
962         }  else if (*file->clnt_encrypt) {
963           (void) execlp(file->clnt_encrypt, file->clnt_encrypt,
964                         file->clnt_decrypt_opt, (char *)NULL);
965           error("could not exec %s: %s", file->clnt_encrypt, strerror(errno));
966           /*NOTREACHED*/
967         }
968       }
969     }
970
971     if (need_compress) {
972         /*
973          * Insert a compress pipe
974          */
975         switch(myout->comp_enc_pid = fork()) {
976         case -1:
977             error("could not fork for %s: %s", COMPRESS_PATH, strerror(errno));
978             /*NOTREACHED*/
979
980         default:
981             aclose(pipes[stage].pipe[0]);
982             aclose(pipes[stage+1].pipe[1]);
983             stage++;
984             break;
985
986         case 0:
987             if(dup2(pipes[stage].pipe[0], 0) == -1) {
988                 error("error compress stdin [dup2 %d %d: %s]", stage,
989                   pipes[stage].pipe[0], strerror(errno));
990                 /*NOTREACHED*/
991             }
992
993             if(dup2(pipes[stage+1].pipe[1], 1) == -1) {
994                 error("error compress stdout [dup2 %d %d: %s]", stage + 1,
995                   pipes[stage+1].pipe[1], strerror(errno));
996                   /*NOTREACHED*/
997             }
998             if (*flags->comp_type == '\0') {
999                 flags->comp_type = NULL;
1000             }
1001
1002             safe_fd(-1, 0);
1003             (void) execlp(COMPRESS_PATH, COMPRESS_PATH, flags->comp_type, (char *)0);
1004             error("could not exec %s: %s", COMPRESS_PATH, strerror(errno));
1005             /*NOTREACHED*/
1006         }
1007     } else if(need_uncompress) {
1008         /*
1009          * If not -r, -c, -l, and file is compressed, and split reassembly 
1010          * options are sane, insert uncompress pipe
1011          */
1012
1013         /* 
1014          * XXX for now we know that for the two compression types we
1015          * understand, .Z and optionally .gz, UNCOMPRESS_PATH will take
1016          * care of both.  Later, we may need to reference a table of
1017          * possible uncompress programs.
1018          */ 
1019         switch(myout->comp_enc_pid = fork()) {
1020         case -1: 
1021             error("could not fork for %s: %s",
1022                   UNCOMPRESS_PATH, strerror(errno));
1023             /*NOTREACHED*/
1024
1025         default:
1026             aclose(pipes[stage].pipe[0]);
1027             aclose(pipes[stage+1].pipe[1]);
1028             stage++;
1029             break;
1030
1031         case 0:
1032             if(dup2(pipes[stage].pipe[0], 0) == -1) {
1033                 error("error uncompress stdin [dup2 %d %d: %s]", stage,
1034                   pipes[stage].pipe[0], strerror(errno));
1035                 /*NOTREACHED*/
1036             }
1037
1038             if(dup2(pipes[stage+1].pipe[1], 1) == -1) {
1039                 error("error uncompress stdout [dup2 %d %d: %s]", stage + 1,
1040                   pipes[stage+1].pipe[1], strerror(errno));
1041                 /*NOTREACHED*/
1042             }
1043
1044             safe_fd(-1, 0);
1045             if (*file->srvcompprog) {
1046               (void) execlp(file->srvcompprog, file->srvcompprog, "-d",
1047                             (char *)NULL);
1048               error("could not exec %s: %s", file->srvcompprog,
1049                     strerror(errno));
1050               /*NOTREACHED*/
1051             } else if (*file->clntcompprog) {
1052               (void) execlp(file->clntcompprog, file->clntcompprog, "-d",
1053                             (char *)NULL);
1054               error("could not exec %s: %s", file->clntcompprog,
1055                     strerror(errno));
1056               /*NOTREACHED*/
1057             } else {
1058               (void) execlp(UNCOMPRESS_PATH, UNCOMPRESS_PATH,
1059 #ifdef UNCOMPRESS_OPT
1060                           UNCOMPRESS_OPT,
1061 #endif
1062                           (char *)NULL);
1063               error("could not exec %s: %s", UNCOMPRESS_PATH, strerror(errno));
1064               /*NOTREACHED*/
1065             }
1066         }
1067     }
1068
1069     /* copy the rest of the file from tape to the output */
1070     if(flags->blocksize > 0)
1071         blocksize = (size_t)flags->blocksize;
1072     else if(blocksize == SIZE_MAX)
1073         blocksize = DISK_BLOCK_BYTES;
1074     buffer = alloc(blocksize);
1075
1076     do {
1077         bytes_read = get_block(tapefd, buffer, isafile);
1078         if(bytes_read < 0) {
1079             error("restore read error: %s", strerror(errno));
1080             /*NOTREACHED*/
1081         }
1082
1083         if(bytes_read > 0) {
1084             if((s = fullwrite(pipes[0].pipe[1], buffer, (size_t)bytes_read)) < 0) {
1085                 if ((errno == EPIPE) || (errno == ECONNRESET)) {
1086                     /*
1087                      * reading program has ended early
1088                      * e.g: bzip2 closes pipe when it
1089                      * trailing garbage after EOF
1090                      */
1091                     break;
1092                 }
1093                 error("restore: write error: %s", strerror(errno));
1094                 /* NOTREACHED */
1095             } else if (s < bytes_read) {
1096                 error("restore: wrote %d of %d bytes: %s",
1097                     s, bytes_read, strerror(errno));
1098                 /* NOTREACHED */
1099             }
1100         }
1101         else if(isafile) {
1102             /*
1103              * See if we need to switch to the next file in a holding restore
1104              */
1105             if(file->cont_filename[0] == '\0') {
1106                 break;                          /* no more files */
1107             }
1108             aclose(tapefd);
1109             if((tapefd = open(file->cont_filename, O_RDONLY)) == -1) {
1110                 char *cont_filename = strrchr(file->cont_filename,'/');
1111                 if(cont_filename) {
1112                     cont_filename++;
1113                     if((tapefd = open(cont_filename,O_RDONLY)) == -1) {
1114                         error("can't open %s: %s", file->cont_filename,
1115                               strerror(errno));
1116                         /*NOTREACHED*/
1117                     }
1118                     else {
1119                         fprintf(stderr, "cannot open %s: %s\n",
1120                                 file->cont_filename, strerror(errno));
1121                         fprintf(stderr, "using %s\n",
1122                                 cont_filename);
1123                     }
1124                 }
1125                 else {
1126                     error("can't open %s: %s", file->cont_filename,
1127                           strerror(errno));
1128                     /*NOTREACHED*/
1129                 }
1130             }
1131             bytes_read = read_file_header(file, tapefd, isafile, flags);
1132             if(file->type != F_DUMPFILE && file->type != F_CONT_DUMPFILE
1133                     && file->type != F_SPLIT_DUMPFILE) {
1134                 fprintf(stderr, "unexpected header type: ");
1135                 print_header(stderr, file);
1136                 exit(2);
1137             }
1138         }
1139     } while (bytes_read > 0);
1140
1141     amfree(buffer);
1142
1143     if(!flags->inline_assemble) {
1144         if(out != dest)
1145             aclose(out);
1146     }
1147     if(!is_continuation){
1148         if(tmp_filename && stat(tmp_filename, &statinfo) < 0){
1149             error("Can't stat the file I just created (%s)!", tmp_filename);
1150             /*NOTREACHED*/
1151         } else {
1152             statinfo.st_size = (off_t)0;
1153         }
1154         if (check_for_aborted && final_filename) {
1155             char *old_dump = final_filename;
1156             struct stat oldstat;
1157             if(stat(old_dump, &oldstat) >= 0){
1158                 if(oldstat.st_size <= statinfo.st_size){
1159                     dumplist_t *prev_fileentry = NULL;
1160                     open_output_t *prev_out = NULL;
1161                     fprintf(stderr, "Newer restore is larger, using that\n");
1162                     /* nuke the old dump's entry in alldump_list */
1163                     for(fileentry=alldumps_list;
1164                             fileentry->next;
1165                             fileentry=fileentry->next){
1166                         if(headers_equal(file, fileentry->file, 0)){
1167                             if(prev_fileentry){
1168                                 prev_fileentry->next = fileentry->next;
1169                             }
1170                             else {
1171                                 alldumps_list = fileentry->next;
1172                             }
1173                             amfree(fileentry);
1174                             break;
1175                         }
1176                         prev_fileentry = fileentry;
1177                     }
1178                     myout = open_outputs;
1179                     while(myout != NULL){
1180                         if(headers_equal(file, myout->file, 0)){
1181                             if(myout->outfd >= 0)
1182                                 aclose(myout->outfd);
1183                             if(prev_out){
1184                                 prev_out->next = myout->next;
1185                             }
1186                             else open_outputs = myout->next;
1187                             amfree(myout);
1188                             break;
1189                         }
1190                         prev_out = myout;
1191                         myout = myout->next;
1192                     }
1193                 }
1194                 else{
1195                     fprintf(stderr, "Older restore is larger, using that\n");
1196                     if (tmp_filename)
1197                         unlink(tmp_filename);
1198                     amfree(tempdump->file);
1199                     amfree(tempdump);
1200                     amfree(tmp_filename);
1201                     amfree(final_filename);
1202                     return (bytes_read);
1203                 }
1204             }
1205         }
1206         if(tmp_filename && final_filename &&
1207                 rename(tmp_filename, final_filename) < 0) {
1208             error("Can't rename %s to %s: %s",
1209                    tmp_filename, final_filename, strerror(errno));
1210             /*NOTREACHED*/
1211         }
1212     }
1213     amfree(tmp_filename);
1214     amfree(final_filename);
1215
1216
1217     /*
1218      * actually insert tracking data for this file into our various
1219      * structures (we waited in case we needed to give up)
1220      */
1221     if(!is_continuation){
1222         oldout = alloc(SIZEOF(open_output_t));
1223         oldout->file = alloc(SIZEOF(dumpfile_t));
1224         memcpy(oldout->file, file, SIZEOF(dumpfile_t));
1225         if(flags->inline_assemble) oldout->outfd = pipes[0].pipe[1];
1226         else oldout->outfd = -1;
1227         oldout->comp_enc_pid = -1;
1228         oldout->lastpartnum = file->partnum;
1229         oldout->next = open_outputs;
1230         open_outputs = oldout;
1231     }
1232     if(alldumps_list){
1233         fileentry = alldumps_list;
1234         while (fileentry->next != NULL)
1235             fileentry=fileentry->next;
1236         fileentry->next = tempdump;
1237     }
1238     else {
1239         alldumps_list = tempdump;
1240     }
1241
1242     return (bytes_read);
1243 }
1244
1245 /* return NULL if the label is not the expected one                     */
1246 /* return the label if it is the expected one, and set *tapefd to a     */
1247 /* file descriptor to the tapedev                                       */
1248 char *
1249 label_of_current_slot(
1250     char         *cur_tapedev,
1251     FILE         *prompt_out,
1252     int          *tapefd,
1253     dumpfile_t   *file,
1254     rst_flags_t  *flags,
1255     am_feature_t *their_features,
1256     ssize_t      *read_result,
1257     tapelist_t   *desired_tape)
1258 {
1259     struct stat stat_tape;
1260     char *label = NULL;
1261     int wrongtape = 0;
1262     char *err;
1263
1264     if (!cur_tapedev) {
1265         send_message(prompt_out, flags, their_features,
1266                      "no tapedev specified");
1267     } else if (tape_stat(cur_tapedev, &stat_tape) !=0 ) {
1268         send_message(prompt_out, flags, their_features, 
1269                      "could not stat '%s': %s",
1270                      cur_tapedev, strerror(errno));
1271         wrongtape = 1;
1272     } else if((err = tape_rewind(cur_tapedev)) != NULL) {
1273         send_message(prompt_out, flags, their_features, 
1274                          "Could not rewind device '%s': %s",
1275                          cur_tapedev, err);
1276         wrongtape = 1;
1277         /* err should not be freed */
1278     } else if((*tapefd = tape_open(cur_tapedev, 0)) < 0){
1279         send_message(prompt_out, flags, their_features,
1280                          "could not open tape device %s: %s",
1281                          cur_tapedev, strerror(errno));
1282         wrongtape = 1;
1283     }
1284
1285     if (!wrongtape) {
1286         *read_result = read_file_header(file, *tapefd, 0, flags);
1287         if (file->type != F_TAPESTART) {
1288             send_message(prompt_out, flags, their_features,
1289                              "Not an amanda tape");
1290             tapefd_close(*tapefd);
1291         } else {
1292             if (flags->check_labels && desired_tape &&
1293                          strcmp(file->name, desired_tape->label) != 0) {
1294                 send_message(prompt_out, flags, their_features,
1295                                  "Label mismatch, got %s and expected %s",
1296                                  file->name, desired_tape->label);
1297                 tapefd_close(*tapefd);
1298             }
1299             else {
1300                 label = stralloc(file->name);
1301             }
1302         }
1303     }
1304     return label;
1305 }
1306
1307 /* return >0            the number of slot move            */
1308 /* return LOAD_STOP     if the search must be stopped      */
1309 /* return LOAD_CHANGER  if the changer search the library  */
1310 int
1311 load_next_tape(
1312     char         **cur_tapedev,
1313     FILE          *prompt_out,
1314     int            backwards,
1315     rst_flags_t   *flags,
1316     am_feature_t  *their_features,
1317     tapelist_t    *desired_tape)
1318 {
1319     int ret = -1;
1320
1321     if (desired_tape) {
1322         send_message(prompt_out, flags, their_features,
1323                      "Looking for tape %s...",
1324                      desired_tape->label);
1325         if (backwards) {
1326             searchlabel = desired_tape->label; 
1327             changer_find(NULL, scan_init, loadlabel_slot,
1328                          desired_tape->label);
1329             ret = LOAD_CHANGER;
1330         } else {
1331             amfree(curslot);
1332             changer_loadslot("next", &curslot,
1333                              cur_tapedev);
1334             ret = 1;
1335         }
1336     } else {
1337         assert(!flags->amidxtaped);
1338         amfree(curslot);
1339         changer_loadslot("next", &curslot, cur_tapedev);
1340         ret = 1;
1341     }
1342     return ret;
1343 }
1344
1345
1346 /* return  0     a new tape is loaded       */
1347 /* return -1     no new tape                */
1348 int
1349 load_manual_tape(
1350     char         **cur_tapedev,
1351     FILE          *prompt_out,
1352     rst_flags_t   *flags,
1353     am_feature_t  *their_features,
1354     tapelist_t    *desired_tape)
1355 {
1356     int ret = 0;
1357     char *input = NULL;
1358
1359     if (flags->amidxtaped) {
1360         if (their_features &&
1361             am_has_feature(their_features,
1362                            fe_amrecover_FEEDME)) {
1363             fprintf(prompt_out, "FEEDME %s\r\n",
1364                     desired_tape->label);
1365             fflush(prompt_out);
1366             input = agets(stdin);/* Strips \n but not \r */
1367             if(!input) {
1368                 error("Connection lost with amrecover");
1369                 /*NOTREACHED*/
1370             } else if (strcmp("OK\r", input) == 0) {
1371             } else if (strncmp("TAPE ", input, 5) == 0) {
1372                 amfree(*cur_tapedev);
1373                 *cur_tapedev = alloc(1025);
1374                 if (sscanf(input, "TAPE %1024s\r", *cur_tapedev) != 1) {
1375                     error("Got bad response from amrecover: %s", input);
1376                     /*NOTREACHED*/
1377                 }
1378             } else {
1379                 send_message(prompt_out, flags, their_features,
1380                              "Got bad response from amrecover: %s", input);
1381                 error("Got bad response from amrecover: %s", input);
1382                 /*NOTREACHED*/
1383             }
1384         } else {
1385             send_message(prompt_out, flags, their_features,
1386                          "Client doesn't support fe_amrecover_FEEDME");
1387             error("Client doesn't support fe_amrecover_FEEDME");
1388             /*NOTREACHED*/
1389         }
1390     }
1391     else {
1392         if (desired_tape) {
1393             fprintf(prompt_out,
1394                     "Insert tape labeled %s in device %s \n"
1395                     "and press enter, ^D to finish reading tapes\n",
1396                     desired_tape->label, *cur_tapedev);
1397         } else {
1398             fprintf(prompt_out,"Insert a tape to search and press "
1399                     "enter, ^D to finish reading tapes\n");
1400         }
1401         fflush(prompt_out);
1402         if((input = agets(stdin)) == NULL)
1403             ret = -1;
1404     }
1405
1406     amfree(input);
1407     return ret;
1408 }
1409
1410
1411 void 
1412 search_a_tape(
1413     char         *cur_tapedev,
1414     FILE         *prompt_out,
1415     rst_flags_t  *flags,
1416     am_feature_t *their_features,
1417     tapelist_t   *desired_tape,
1418     int           isafile,
1419     match_list_t *match_list,
1420     seentapes_t  *tape_seen,
1421     dumpfile_t   *file,
1422     dumpfile_t   *prev_rst_file,
1423     dumpfile_t   *tapestart,
1424     int           slot_num,
1425     ssize_t      *read_result)
1426 {
1427     off_t       filenum;
1428     dumplist_t *fileentry = NULL;
1429     int         tapefile_idx = -1;
1430     int         i;
1431     char       *logline = NULL;
1432     FILE       *logstream = NULL;
1433     off_t       fsf_by;
1434
1435     filenum = (off_t)0;
1436     if(desired_tape && desired_tape->numfiles > 0)
1437         tapefile_idx = 0;
1438
1439     if (desired_tape) {
1440         dbprintf(("search_a_tape: desired_tape=%p label=%s\n",
1441                   desired_tape, desired_tape->label));
1442         dbprintf(("tape:   numfiles = %d\n", desired_tape->numfiles));
1443         for (i=0; i < desired_tape->numfiles; i++) {
1444             dbprintf(("tape:   files[%d] = " OFF_T_FMT "\n",
1445                       i, (OFF_T_FMT_TYPE)desired_tape->files[i]));
1446         }
1447     } else {
1448         dbprintf(("search_a_tape: no desired_tape\n"));
1449     }
1450     dbprintf(("current tapefile_idx = %d\n", tapefile_idx));
1451         
1452     /* if we know where we're going, fastforward there */
1453     if(flags->fsf && !isafile){
1454         /* If we have a tapelist entry, filenums will be store there */
1455         if(tapefile_idx >= 0) {
1456             fsf_by = desired_tape->files[tapefile_idx]; 
1457         } else {
1458             /*
1459              * older semantics assume we're restoring one file, with the fsf
1460              * flag being the filenum on tape for said file
1461              */
1462             fsf_by = (flags->fsf == 0) ? (off_t)0 : (off_t)1;
1463         }
1464         if(fsf_by > (off_t)0){
1465             if(tapefd_rewind(tapefd) < 0) {
1466                 send_message(prompt_out, flags, their_features,
1467                              "Could not rewind device %s: %s",
1468                              cur_tapedev, strerror(errno));
1469                 error("Could not rewind device %s: %s",
1470                       cur_tapedev, strerror(errno));
1471                 /*NOTREACHED*/
1472             }
1473
1474             if(tapefd_fsf(tapefd, fsf_by) < 0) {
1475                 send_message(prompt_out, flags, their_features,
1476                              "Could not fsf device %s by " OFF_T_FMT ": %s",
1477                              cur_tapedev, (OFF_T_FMT_TYPE)fsf_by,
1478                              strerror(errno));
1479                 error("Could not fsf device %s by " OFF_T_FMT ": %s",
1480                       cur_tapedev, (OFF_T_FMT_TYPE)fsf_by,
1481                       strerror(errno));
1482                 /*NOTREACHED*/
1483             }
1484             else {
1485                 filenum = fsf_by;
1486             }
1487             *read_result = read_file_header(file, tapefd, isafile, flags);
1488         }
1489     }
1490
1491     while((file->type == F_TAPESTART || file->type == F_DUMPFILE ||
1492            file->type == F_SPLIT_DUMPFILE) &&
1493           (tapefile_idx < 0 || tapefile_idx < desired_tape->numfiles)) {
1494         int found_match = 0;
1495         match_list_t *me;
1496         dumplist_t *tempdump = NULL;
1497
1498         /* store record of this dump for inventorying purposes */
1499         tempdump = alloc(SIZEOF(dumplist_t));
1500         tempdump->file = alloc(SIZEOF(dumpfile_t));
1501         tempdump->next = NULL;
1502         memcpy(tempdump->file, &file, SIZEOF(dumpfile_t));
1503         if(tape_seen->files){
1504             fileentry = tape_seen->files;
1505             while (fileentry->next != NULL)
1506                    fileentry = fileentry->next;
1507             fileentry->next = tempdump;
1508         }
1509         else {
1510             tape_seen->files = tempdump;
1511         }
1512
1513         /* see if we need to restore the thing */
1514         if(isafile)
1515             found_match = 1;
1516         else if(tapefile_idx >= 0){ /* do it by explicit file #s */
1517             if(filenum == desired_tape->files[tapefile_idx]){
1518                 found_match = 1;
1519                 tapefile_idx++;
1520             }
1521         }
1522         else{ /* search and match headers */
1523             for(me = match_list; me; me = me->next) {
1524                 if(disk_match(file, me->datestamp, me->hostname,
1525                               me->diskname, me->level) != 0){
1526                     found_match = 1;
1527                     break;
1528                 }
1529             }
1530         }
1531
1532         if(found_match){
1533             char *filename = make_filename(file);
1534
1535             fprintf(stderr, "%s: " OFF_T_FMT ": restoring ",
1536                     get_pname(), (OFF_T_FMT_TYPE)filenum);
1537             print_header(stderr, file);
1538             *read_result = restore(file, filename, tapefd, isafile, flags);
1539             filenum++;
1540             amfree(filename);
1541         }
1542
1543         /* advance to the next file, fast-forwarding where reasonable */
1544         if (!isafile) {
1545             if (*read_result == 0) {
1546                 tapefd_close(tapefd);
1547                 if((tapefd = tape_open(cur_tapedev, 0)) < 0) {
1548                     send_message(prompt_out, flags, their_features,
1549                                  "could not open %s: %s",
1550                                  cur_tapedev, strerror(errno));
1551                     error("could not open %s: %s",
1552                           cur_tapedev, strerror(errno));
1553                     /*NOTREACHED*/
1554                 }
1555             /* if the file is not what we're looking for fsf to next one */
1556             }
1557             else if (!found_match) {
1558                 if (tapefd_fsf(tapefd, (off_t)1) < 0) {
1559                     send_message(prompt_out, flags, their_features,
1560                                  "Could not fsf device %s: %s",
1561                                  cur_tapedev, strerror(errno));
1562                     error("Could not fsf device %s: %s",
1563                           cur_tapedev, strerror(errno));
1564                     /*NOTREACHED*/
1565                 }
1566                 filenum ++;
1567             }
1568             else if (flags->fsf && (tapefile_idx >= 0) && 
1569                      (tapefile_idx < desired_tape->numfiles)) {
1570                 fsf_by = desired_tape->files[tapefile_idx] - filenum;
1571                 if (fsf_by > (off_t)0) {
1572                     if(tapefd_fsf(tapefd, fsf_by) < 0) {
1573                         send_message(prompt_out, flags, their_features,
1574                                      "Could not fsf device %s by "
1575                                      OFF_T_FMT ": %s",
1576                                      cur_tapedev, (OFF_T_FMT_TYPE)fsf_by,
1577                                      strerror(errno));
1578                         error("Could not fsf device %s by " OFF_T_FMT ": %s",
1579                               cur_tapedev, (OFF_T_FMT_TYPE)fsf_by,
1580                               strerror(errno));
1581                         /*NOTREACHED*/
1582                     }
1583                     filenum = desired_tape->files[tapefile_idx];
1584                 }
1585             }
1586         } /* !isafile */
1587
1588         memcpy(prev_rst_file, file, SIZEOF(dumpfile_t));
1589               
1590         if(isafile)
1591             break;
1592         *read_result = read_file_header(file, tapefd, isafile, flags);
1593
1594         /* only restore a single dump, if piping to stdout */
1595         if (!headers_equal(prev_rst_file, file, 1) &&
1596             (flags->pipe_to_fd == fileno(stdout)) && found_match) {
1597             break;
1598         }
1599     } /* while we keep seeing headers */
1600
1601     if (!isafile) {
1602         if (file->type == F_EMPTY) {
1603             aclose(tapefd);
1604             if((tapefd = tape_open(cur_tapedev, 0)) < 0) {
1605                 send_message(prompt_out, flags, their_features,
1606                              "could not open %s: %s",
1607                              cur_tapedev, strerror(errno));
1608                 error("could not open %s: %s",
1609                       cur_tapedev, strerror(errno));
1610                 /*NOTREACHED*/
1611             }
1612         } else {
1613             if (tapefd_fsf(tapefd, (off_t)1) < 0) {
1614                 send_message(prompt_out, flags, their_features,
1615                              "could not fsf %s: %s",
1616                              cur_tapedev, strerror(errno));;
1617                 error("could not fsf %s: %s",
1618                       cur_tapedev, strerror(errno));
1619                 /*NOTREACHED*/
1620             }
1621         }
1622     }
1623     tapefd_close(tapefd);
1624
1625     /* spit out our accumulated list of dumps, if we're inventorying */
1626     if (logstream) {
1627         logline = log_genstring(L_START, "taper",
1628                                     "datestamp %s label %s tape %d",
1629                                     tapestart->datestamp, tapestart->name,
1630                                     slot_num);
1631         fprintf(logstream, "%s", logline);
1632         for(fileentry=tape_seen->files; fileentry; fileentry=fileentry->next){
1633             logline = NULL;
1634             switch (fileentry->file->type) {
1635                 case F_DUMPFILE:
1636                     logline = log_genstring(L_SUCCESS, "taper",
1637                                        "%s %s %s %d [faked log entry]",
1638                                        fileentry->file->name,
1639                                        fileentry->file->disk,
1640                                        fileentry->file->datestamp,
1641                                        fileentry->file->dumplevel);
1642                     break;
1643                 case F_SPLIT_DUMPFILE:
1644                     logline = log_genstring(L_CHUNK, "taper", 
1645                                        "%s %s %s %d %d [faked log entry]",
1646                                        fileentry->file->name,
1647                                        fileentry->file->disk,
1648                                        fileentry->file->datestamp,
1649                                        fileentry->file->partnum,
1650                                        fileentry->file->dumplevel);
1651                     break;
1652                 default:
1653                     break;
1654             }
1655             if(logline){
1656                 fprintf(logstream, "%s", logline);
1657                 amfree(logline);
1658                 fflush(logstream);
1659             }
1660         }
1661     }
1662 }
1663
1664 /* 
1665  * Take a pattern of dumps and restore it blind, a la amrestore.  In addition,
1666  * be smart enough to change tapes and continue with minimal operator
1667  * intervention, and write out a record of what was found on tapes in the
1668  * the regular logging format.  Can take a tapelist with a specific set of
1669  * tapes to search (rather than "everything I can find"), which in turn can
1670  * optionally list specific files to restore.
1671  */
1672 void
1673 search_tapes(
1674     FILE *              prompt_out,
1675     int                 use_changer,
1676     tapelist_t *        tapelist,
1677     match_list_t *      match_list,
1678     rst_flags_t *       flags,
1679     am_feature_t *      their_features)
1680 {
1681     int have_changer = 1;
1682     int slot_num = -1;
1683     int slots = -1;
1684     FILE *logstream = NULL;
1685     tapelist_t *desired_tape = NULL;
1686     struct sigaction act, oact;
1687     ssize_t read_result;
1688     int slot;
1689     char *label = NULL;
1690     seentapes_t *seentapes = NULL;
1691     int ret;
1692
1693     dbprintf(("search_tapes(prompt=%p, use_changer=%d, tapelist=%p, "
1694               "match_list=%p, flags=%p, features=%p)\n",
1695               prompt_out, use_changer, tapelist, match_list,
1696               flags, their_features));
1697
1698     if(!prompt_out) prompt_out = stderr;
1699
1700     if(flags->blocksize)
1701         blocksize = (size_t)flags->blocksize;
1702     else if(blocksize == (size_t)SSIZE_MAX)
1703         blocksize = DISK_BLOCK_BYTES;
1704
1705     /* Don't die when child closes pipe */
1706     signal(SIGPIPE, SIG_IGN);
1707
1708     /* catch SIGINT with something that'll flush unmerged splits */
1709     act.sa_handler = handle_sigint;
1710     sigemptyset(&act.sa_mask);
1711     act.sa_flags = 0;
1712     if(sigaction(SIGINT, &act, &oact) != 0){
1713         error("error setting SIGINT handler: %s", strerror(errno));
1714         /*NOTREACHED*/
1715     }
1716     if(flags->delay_assemble || flags->inline_assemble) exitassemble = 1;
1717     else exitassemble = 0;
1718
1719     /* if given a log file, print an inventory of stuff found */
1720     if(flags->inventory_log) {
1721         if(!strcmp(flags->inventory_log, "-")) logstream = stdout;
1722         else if((logstream = fopen(flags->inventory_log, "w+")) == NULL) {
1723             error("Couldn't open log file %s for writing: %s",
1724                   flags->inventory_log, strerror(errno));
1725             /*NOTREACHED*/
1726         }
1727     }
1728
1729     /* Suss what tape device we're using, whether there's a changer, etc. */
1730     if(!use_changer || (have_changer = changer_init()) == 0) {
1731         if(flags->alt_tapedev) cur_tapedev = stralloc(flags->alt_tapedev);
1732         else if(!cur_tapedev) cur_tapedev = getconf_str(CNF_TAPEDEV);
1733         /* XXX oughta complain if no config is loaded */
1734         fprintf(stderr, "%s: Using tapedev %s\n", get_pname(), cur_tapedev);
1735         have_changer = 0;
1736     } else if (have_changer != 1) {
1737         error("changer initialization failed: %s", strerror(errno));
1738         /*NOTREACHED*/
1739     }
1740     else{ /* good, the changer works, see what it can do */
1741         amfree(curslot);
1742         changer_info(&slots, &curslot, &backwards);
1743     }
1744
1745     if(tapelist && !flags->amidxtaped){
1746       slots = num_entries(tapelist);
1747       /*
1748         Spit out a list of expected tapes, so people with manual changers know
1749         what to load
1750       */
1751       fprintf(prompt_out, "The following tapes are needed:");
1752       for(desired_tape = tapelist; desired_tape != NULL;
1753           desired_tape = desired_tape->next){
1754         fprintf(prompt_out, " %s", desired_tape->label);
1755       }
1756       fprintf(prompt_out, "\n");
1757       fflush(prompt_out);
1758       if(flags->wait_tape_prompt){
1759         char *input = NULL;
1760         fprintf(prompt_out,"Press enter when ready\n");
1761         fflush(prompt_out);
1762         input = agets(stdin);
1763         amfree(input);
1764         fprintf(prompt_out, "\n");
1765         fflush(prompt_out);
1766       }
1767     }
1768     desired_tape = tapelist;
1769
1770     if(use_changer && !cur_tapedev) { /* load current slot */
1771         amfree(curslot);
1772         changer_loadslot("current", &curslot, &cur_tapedev);
1773     }
1774
1775     /*
1776      * If we're not given a tapelist, iterate over everything our changer can
1777      * find.  If there's no changer, we'll prompt to be handfed tapes.
1778      *
1779      * If we *are* given a tapelist, restore from those tapes in the order in
1780      * which they're listed.  Unless the changer (if we have one) can't go
1781      * backwards, in which case check every tape we see and restore from it if
1782      * appropriate.
1783      *
1784      * (obnoxious, isn't this?)
1785      */
1786
1787     do { /* all desired tape */
1788         seentapes_t *tape_seen = NULL;
1789         dumpfile_t file, tapestart, prev_rst_file;
1790         int isafile = 0;
1791         read_result = 0;
1792
1793         slot_num = 0;
1794
1795         memset(&file, 0, SIZEOF(file));
1796
1797         if (desired_tape && desired_tape->isafile) {
1798             isafile = 1;
1799             if ((tapefd = open(desired_tape->label, 0)) == -1) {
1800                 send_message(prompt_out, flags, their_features, 
1801                              "could not open %s: %s",
1802                              desired_tape->label, strerror(errno));
1803                 continue;
1804             }
1805             fprintf(stderr, "Reading %s to fd %d\n",
1806                             desired_tape->label, tapefd);
1807
1808             read_result = read_file_header(&file, tapefd, 1, flags);
1809             label = stralloc(desired_tape->label);
1810         } else {
1811             /* check current_slot */
1812             label = label_of_current_slot(cur_tapedev, prompt_out,
1813                                           &tapefd, &file, flags,
1814                                           their_features, &read_result,
1815                                           desired_tape);
1816             while (label==NULL && slot_num < slots &&
1817                    use_changer) {
1818                 /*
1819                  * If we have an incorrect tape loaded, go try to find
1820                  * the right one
1821                  * (or just see what the next available one is).
1822                  */
1823                 slot = load_next_tape(&cur_tapedev, prompt_out,
1824                                       backwards, flags,
1825                                       their_features, desired_tape);
1826                 if(slot == LOAD_STOP) {
1827                     slot_num = slots;
1828                     amfree(label);
1829                 } else {
1830                     if (slot == LOAD_CHANGER)
1831                         slot_num = slots;
1832                     else /* slot > 0 */
1833                         slot_num += slot;
1834
1835                     /* check current_slot */
1836                     label = label_of_current_slot(cur_tapedev, prompt_out,
1837                                                   &tapefd, &file, flags,
1838                                                   their_features, &read_result,
1839                                                   desired_tape);
1840                 }
1841             }
1842
1843             if (label == NULL) {
1844                 ret = load_manual_tape(&cur_tapedev, prompt_out,
1845                                        flags,
1846                                        their_features, desired_tape);
1847                 if (ret == 0) {
1848                     label = label_of_current_slot(cur_tapedev, prompt_out,
1849                                                   &tapefd, &file, flags,
1850                                                   their_features, &read_result,
1851                                                   desired_tape);
1852                 }
1853             }
1854
1855             if (label)
1856                 memcpy(&tapestart, &file, SIZEOF(dumpfile_t));
1857         }
1858         
1859         if (!label)
1860             continue;
1861
1862         /*
1863          * Skip this tape if we did it already.  Note that this would let
1864          * duplicate labels through, so long as they were in the same slot.
1865          * I'm over it, are you?
1866          */
1867         if (!isafile) {
1868             for (tape_seen = seentapes; tape_seen;
1869                  tape_seen = tape_seen->next) {
1870                 if (!strcmp(tape_seen->label, label) &&
1871                     !strcmp(tape_seen->slotstr, curslot)){
1872                     send_message(prompt_out, flags, their_features,
1873                                  "Saw repeat tape %s in slot %s",
1874                                  label, curslot);
1875                     amfree(label);
1876                     break;
1877                 }
1878             }
1879         }
1880
1881         if(!label)
1882             continue;
1883
1884         if(!curslot)
1885             curslot = stralloc("<none>");
1886
1887         if(!isafile){
1888             fprintf(stderr, "Scanning %s (slot %s)\n", label, curslot);
1889             fflush(stderr);
1890         }
1891
1892         tape_seen = alloc(SIZEOF(seentapes_t));
1893         memset(tape_seen, '\0', SIZEOF(seentapes_t));
1894
1895         tape_seen->label = label;
1896         tape_seen->slotstr = stralloc(curslot);
1897         tape_seen->next = seentapes;
1898         tape_seen->files = NULL;
1899         seentapes = tape_seen;
1900
1901         /*
1902          * Start slogging through the tape itself.  If our tapelist (if we
1903          * have one) contains a list of files to restore, obey that instead
1904          * of checking for matching headers on all files.
1905          */
1906
1907         search_a_tape(cur_tapedev, prompt_out, flags, their_features,
1908                       desired_tape, isafile, match_list, tape_seen,
1909                       &file, &prev_rst_file, &tapestart, slot_num,
1910                       &read_result);
1911
1912         fprintf(stderr, "%s: Search of %s complete\n",
1913                         get_pname(), tape_seen->label);
1914         if (desired_tape) desired_tape = desired_tape->next;
1915
1916         /* only restore a single dump, if piping to stdout */
1917         if (!headers_equal(&prev_rst_file, &file, 1) &&
1918             flags->pipe_to_fd == fileno(stdout))
1919                 break;
1920
1921     } while (desired_tape);
1922
1923     while (seentapes != NULL) {
1924         seentapes_t *tape_seen = seentapes;
1925         seentapes = seentapes->next;
1926         while(tape_seen->files != NULL) {
1927             dumplist_t *temp_dump = tape_seen->files;
1928             tape_seen->files = temp_dump->next;
1929             amfree(temp_dump->file);
1930             amfree(temp_dump);
1931         }
1932         amfree(tape_seen->label);
1933         amfree(tape_seen->slotstr);
1934         amfree(tape_seen);
1935         
1936     }
1937
1938     if(logstream && logstream != stderr && logstream != stdout){
1939         fclose(logstream);
1940     }
1941     if(flags->delay_assemble || flags->inline_assemble){
1942         flush_open_outputs(1, NULL);
1943     }
1944     else flush_open_outputs(0, NULL);
1945 }
1946
1947 /*
1948  * Create a new, clean set of restore flags with some sane default values.
1949  */
1950 rst_flags_t *
1951 new_rst_flags(void)
1952 {
1953     rst_flags_t *flags = alloc(SIZEOF(rst_flags_t));
1954
1955     memset(flags, 0, SIZEOF(rst_flags_t));
1956
1957     flags->fsf = 1;
1958     flags->comp_type = COMPRESS_FAST_OPT;
1959     flags->inline_assemble = 1;
1960     flags->pipe_to_fd = -1;
1961     flags->check_labels = 1;
1962
1963     return(flags);
1964 }
1965
1966 /*
1967  * Make sure the set of restore options given is sane.  Print errors for
1968  * things that're odd, and return -1 for fatal errors.
1969  */
1970 int
1971 check_rst_flags(
1972     rst_flags_t *       flags)
1973 {
1974     int ret = 0;        
1975     
1976     if(!flags) return(-1);
1977
1978     if(flags->compress && flags->leave_comp){
1979         fprintf(stderr, "Cannot specify 'compress output' and 'leave compression alone' together\n");
1980         ret = -1;
1981     }
1982
1983     if(flags->restore_dir != NULL){
1984         struct stat statinfo;
1985
1986         if(flags->pipe_to_fd != -1){
1987             fprintf(stderr, "Specifying output directory and piping output are mutually exclusive\n");
1988             ret = -1;
1989         }
1990         if(stat(flags->restore_dir, &statinfo) < 0){
1991             fprintf(stderr, "Cannot stat restore target dir '%s': %s\n",
1992                       flags->restore_dir, strerror(errno));
1993             ret = -1;
1994         }
1995         if((statinfo.st_mode & S_IFMT) != S_IFDIR){
1996             fprintf(stderr, "'%s' is not a directory\n", flags->restore_dir);
1997             ret = -1;
1998         }
1999     }
2000
2001     if((flags->pipe_to_fd != -1 || flags->compress) &&
2002             (flags->delay_assemble || !flags->inline_assemble)){
2003         fprintf(stderr, "Split dumps *must* be automatically reassembled when piping output or compressing/uncompressing\n");
2004         ret = -1;
2005     }
2006
2007     if(flags->delay_assemble && flags->inline_assemble){
2008         fprintf(stderr, "Inline split assembling and delayed assembling are mutually exclusive\n");
2009         ret = -1;
2010     }
2011
2012     return(ret);
2013 }
2014
2015 /*
2016  * Clean up after a rst_flags_t
2017  */
2018 void
2019 free_rst_flags(
2020     rst_flags_t *       flags)
2021 {
2022     if(!flags) return;
2023
2024     amfree(flags->restore_dir);
2025     amfree(flags->alt_tapedev);
2026     amfree(flags->inventory_log);
2027
2028     amfree(flags);
2029 }
2030
2031
2032 /*
2033  * Clean up after a match_list_t
2034  */
2035 void
2036 free_match_list(
2037     match_list_t *      match_list)
2038 {
2039     match_list_t *me;
2040     match_list_t *prev = NULL;
2041   
2042     for(me = match_list; me; me = me->next){
2043         /* XXX freeing these is broken? can't work out why */
2044 /*      amfree(me->hostname);
2045         amfree(me->diskname);
2046         amfree(me->datestamp);
2047         amfree(me->level); */
2048         amfree(prev);
2049         prev = me;
2050     }
2051     amfree(prev);
2052 }
2053
2054
2055 printf_arglist_function3(
2056     void send_message,
2057     FILE *, prompt_out,
2058     rst_flags_t *, flags,
2059     am_feature_t *, their_features,
2060     char *, format)
2061 {
2062     va_list argp;
2063     char linebuf[STR_SIZE];
2064
2065     arglist_start(argp, format);
2066     vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
2067     arglist_end(argp);
2068
2069     fprintf(stderr,"%s\n", linebuf);
2070     if (flags->amidxtaped && their_features &&
2071         am_has_feature(their_features, fe_amrecover_message)) {
2072         fprintf(prompt_out, "MESSAGE %s\r\n", linebuf);
2073         fflush(prompt_out);
2074     }
2075 }
2076