Imported Upstream version 2.5.1
[debian/amanda] / server-src / driverio.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  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: driverio.c,v 1.92 2006/08/24 01:57:16 paddy_s Exp $
29  *
30  * I/O-related functions for driver program
31  */
32 #include "amanda.h"
33 #include "util.h"
34 #include "clock.h"
35 #include "server_util.h"
36 #include "conffile.h"
37 #include "diskfile.h"
38 #include "infofile.h"
39 #include "logfile.h"
40 #include "token.h"
41
42 #define GLOBAL          /* the global variables defined here */
43 #include "driverio.h"
44
45 int nb_chunker = 0;
46
47 static const char *childstr(int);
48
49 void
50 init_driverio(void)
51 {
52     dumper_t *dumper;
53
54     taper = -1;
55
56     for(dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
57         dumper->fd = -1;
58     }
59 }
60
61
62 static const char *
63 childstr(
64     int fd)
65 {
66     static char buf[NUM_STR_SIZE + 32];
67     dumper_t *dumper;
68
69     if (fd == taper)
70         return ("taper");
71
72     for (dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
73         if (dumper->fd == fd)
74             return (dumper->name);
75         if (dumper->chunker->fd == fd)
76             return (dumper->chunker->name);
77     }
78     snprintf(buf, SIZEOF(buf), "unknown child (fd %d)", fd);
79     return (buf);
80 }
81
82
83 void
84 startup_tape_process(
85     char *taper_program)
86 {
87     int fd[2];
88
89     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
90         error("taper pipe: %s", strerror(errno));
91         /*NOTREACHED*/
92     }
93     if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
94         error("taper socketpair 0: descriptor %d out of range (0 .. %d)\n",
95               fd[0], FD_SETSIZE-1);
96         /*NOTREACHED*/
97     }
98     if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
99         error("taper socketpair 1: descriptor %d out of range (0 .. %d)\n",
100               fd[1], FD_SETSIZE-1);
101         /*NOTREACHED*/
102     }
103
104     switch(taper_pid = fork()) {
105     case -1:
106         error("fork taper: %s", strerror(errno));
107         /*NOTREACHED*/
108
109     case 0:     /* child process */
110         aclose(fd[0]);
111         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
112             error("taper dup2: %s", strerror(errno));
113         execle(taper_program, "taper", config_name, (char *)0, safe_env());
114         error("exec %s: %s", taper_program, strerror(errno));
115         /*NOTREACHED*/
116
117     default:    /* parent process */
118         aclose(fd[1]);
119         taper = fd[0];
120         taper_ev_read = NULL;
121     }
122 }
123
124 void
125 startup_dump_process(
126     dumper_t *dumper,
127     char *dumper_program)
128 {
129     int fd[2];
130
131     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
132         error("%s pipe: %s", dumper->name, strerror(errno));
133         /*NOTREACHED*/
134     }
135
136     switch(dumper->pid = fork()) {
137     case -1:
138         error("fork %s: %s", dumper->name, strerror(errno));
139         /*NOTREACHED*/
140
141     case 0:             /* child process */
142         aclose(fd[0]);
143         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
144             error("%s dup2: %s", dumper->name, strerror(errno));
145         execle(dumper_program,
146                dumper->name ? dumper->name : "dumper",
147                config_name,
148                (char *)0,
149                safe_env());
150         error("exec %s (%s): %s", dumper_program,
151               dumper->name, strerror(errno));
152         /*NOTREACHED*/
153
154     default:    /* parent process */
155         aclose(fd[1]);
156         dumper->fd = fd[0];
157         dumper->ev_read = NULL;
158         dumper->busy = dumper->down = 0;
159         dumper->dp = NULL;
160         fprintf(stderr,"driver: started %s pid %u\n",
161                 dumper->name, (unsigned)dumper->pid);
162         fflush(stderr);
163     }
164 }
165
166 void
167 startup_dump_processes(
168     char *dumper_program,
169     int inparallel,
170     char *timestamp)
171 {
172     int i;
173     dumper_t *dumper;
174     char number[NUM_STR_SIZE];
175
176     for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
177         snprintf(number, SIZEOF(number), "%d", i);
178         dumper->name = stralloc2("dumper", number);
179         dumper->chunker = &chktable[i];
180         chktable[i].name = stralloc2("chunker", number);
181         chktable[i].dumper = dumper;
182         chktable[i].fd = -1;
183
184         startup_dump_process(dumper, dumper_program);
185         dumper_cmd(dumper, START, (void *)timestamp);
186     }
187 }
188
189 void
190 startup_chunk_process(
191     chunker_t *chunker,
192     char *chunker_program)
193 {
194     int fd[2];
195
196     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
197         error("%s pipe: %s", chunker->name, strerror(errno));
198         /*NOTREACHED*/
199     }
200
201     switch(chunker->pid = fork()) {
202     case -1:
203         error("fork %s: %s", chunker->name, strerror(errno));
204         /*NOTREACHED*/
205
206     case 0:             /* child process */
207         aclose(fd[0]);
208         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
209             error("%s dup2: %s", chunker->name, strerror(errno));
210             /*NOTREACHED*/
211         }
212         execle(chunker_program,
213                chunker->name ? chunker->name : "chunker",
214                config_name,
215                (char *)0,
216                safe_env());
217         error("exec %s (%s): %s", chunker_program,
218               chunker->name, strerror(errno));
219         /*NOTREACHED*/
220
221     default:    /* parent process */
222         aclose(fd[1]);
223         chunker->down = 0;
224         chunker->fd = fd[0];
225         chunker->ev_read = NULL;
226         fprintf(stderr,"driver: started %s pid %u\n",
227                 chunker->name, (unsigned)chunker->pid);
228         fflush(stderr);
229     }
230 }
231
232 cmd_t
233 getresult(
234     int fd,
235     int show,
236     int *result_argc,
237     char **result_argv,
238     int max_arg)
239 {
240     int arg;
241     cmd_t t;
242     char *line;
243
244     if((line = areads(fd)) == NULL) {
245         if(errno) {
246             error("reading result from %s: %s", childstr(fd), strerror(errno));
247             /*NOTREACHED*/
248         }
249         *result_argc = 0;                               /* EOF */
250     } else {
251         *result_argc = split(line, result_argv, max_arg, " ");
252     }
253
254     if(show) {
255         printf("driver: result time %s from %s:",
256                walltime_str(curclock()),
257                childstr(fd));
258         if(line) {
259             for(arg = 1; arg <= *result_argc; arg++) {
260                 printf(" %s", result_argv[arg]);
261             }
262             putchar('\n');
263         } else {
264             printf(" (eof)\n");
265         }
266         fflush(stdout);
267     }
268     amfree(line);
269
270 #ifdef DEBUG
271     printf("argc = %d\n", *result_argc);
272     for(arg = 0; arg < *result_argc; arg++)
273         printf("argv[%d] = \"%s\"\n", arg, result_argv[arg]);
274 #endif
275
276     if(*result_argc < 1) return BOGUS;
277
278     for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
279         if(strcmp(result_argv[1], cmdstr[t]) == 0) return t;
280
281     return BOGUS;
282 }
283
284
285 int
286 taper_cmd(
287     cmd_t cmd,
288     void *ptr,
289     char *destname,
290     int level,
291     char *datestamp)
292 {
293     char *cmdline = NULL;
294     char number[NUM_STR_SIZE];
295     char splitsize[NUM_STR_SIZE];
296     char fallback_splitsize[NUM_STR_SIZE];
297     char *diskbuffer = NULL;
298     disk_t *dp;
299     char *features;
300     char *qname;
301     char *qdest;
302
303     switch(cmd) {
304     case START_TAPER:
305         cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
306         break;
307     case FILE_WRITE:
308         dp = (disk_t *) ptr;
309         qname = quote_string(dp->name);
310         qdest = quote_string(destname);
311         snprintf(number, SIZEOF(number), "%d", level);
312         snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
313                  (OFF_T_FMT_TYPE)dp->tape_splitsize);
314         features = am_feature_to_string(dp->host->features);
315         cmdline = vstralloc(cmdstr[cmd],
316                             " ", disk2serial(dp),
317                             " ", qdest,
318                             " ", dp->host->hostname,
319                             " ", features,
320                             " ", qname,
321                             " ", number,
322                             " ", datestamp,
323                             " ", splitsize,
324                             "\n", NULL);
325         amfree(features);
326         amfree(qdest);
327         amfree(qname);
328         break;
329     case PORT_WRITE:
330         dp = (disk_t *) ptr;
331         qname = quote_string(dp->name);
332         snprintf(number, SIZEOF(number), "%d", level);
333
334         /*
335           If we haven't been given a place to buffer split dumps to disk,
336           make the argument something besides and empty string so's taper
337           won't get confused
338         */
339         if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
340             diskbuffer = "NULL";
341         } else {
342             diskbuffer = dp->split_diskbuffer;
343         }
344         snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
345                  (OFF_T_FMT_TYPE)dp->tape_splitsize);
346         snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), OFF_T_FMT,
347                  (OFF_T_FMT_TYPE)dp->fallback_splitsize);
348         features = am_feature_to_string(dp->host->features);
349         cmdline = vstralloc(cmdstr[cmd],
350                             " ", disk2serial(dp),
351                             " ", dp->host->hostname,
352                             " ", features,
353                             " ", qname,
354                             " ", number,
355                             " ", datestamp,
356                             " ", splitsize,
357                             " ", diskbuffer,
358                             " ", fallback_splitsize,
359                             "\n", NULL);
360         amfree(features);
361         amfree(qname);
362         break;
363     case QUIT:
364         cmdline = stralloc2(cmdstr[cmd], "\n");
365         break;
366     default:
367         error("Don't know how to send %s command to taper", cmdstr[cmd]);
368         /*NOTREACHED*/
369     }
370
371     /*
372      * Note: cmdline already has a '\n'.
373      */
374     printf("driver: send-cmd time %s to taper: %s",
375            walltime_str(curclock()), cmdline);
376     fflush(stdout);
377     if ((fullwrite(taper, cmdline, strlen(cmdline))) < 0) {
378         printf("writing taper command '%s' failed: %s\n",
379                 cmdline, strerror(errno));
380         fflush(stdout);
381         amfree(cmdline);
382         return 0;
383     }
384     if(cmd == QUIT) aclose(taper);
385     amfree(cmdline);
386     return 1;
387 }
388
389 int
390 dumper_cmd(
391     dumper_t *dumper,
392     cmd_t cmd,
393     disk_t *dp)
394 {
395     char *cmdline = NULL;
396     char number[NUM_STR_SIZE];
397     char numberport[NUM_STR_SIZE];
398     char *o;
399     char *device;
400     char *features;
401     char *qname;
402     char *qdest;
403
404     switch(cmd) {
405     case START:
406         cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
407         break;
408     case PORT_DUMP:
409         if(dp && dp->device) {
410             device = dp->device;
411         }
412         else {
413             device = "NODEVICE";
414         }
415
416         if (dp != NULL) {
417             device = quote_string((dp->device) ? dp->device : "NODEVICE");
418             qname = quote_string(dp->name);
419             snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
420             snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
421             features = am_feature_to_string(dp->host->features);
422             o = optionstr(dp, dp->host->features, NULL);
423             if ( o == NULL ) {
424               error("problem with option string, check the dumptype definition.\n");
425             }
426               
427             cmdline = vstralloc(cmdstr[cmd],
428                             " ", disk2serial(dp),
429                             " ", numberport,
430                             " ", dp->host->hostname,
431                             " ", features,
432                             " ", qname,
433                             " ", device,
434                             " ", number,
435                             " ", sched(dp)->dumpdate,
436                             " ", dp->program,
437                             " ", dp->amandad_path,
438                             " ", dp->client_username,
439                             " ", dp->ssh_keys,
440                             " |", o,
441                             "\n", NULL);
442             amfree(features);
443             amfree(o);
444             amfree(qname);
445             amfree(device);
446         } else {
447                 error("PORT-DUMP without disk pointer\n");
448                 /*NOTREACHED*/
449         }
450         break;
451     case QUIT:
452     case ABORT:
453         if( dp ) {
454             qdest = quote_string(sched(dp)->destname);
455             cmdline = vstralloc(cmdstr[cmd],
456                                 " ", qdest,
457                                 "\n", NULL );
458             amfree(qdest);
459         } else {
460             cmdline = stralloc2(cmdstr[cmd], "\n");
461         }
462         break;
463     default:
464         error("Don't know how to send %s command to dumper", cmdstr[cmd]);
465         /*NOTREACHED*/
466     }
467
468     /*
469      * Note: cmdline already has a '\n'.
470      */
471     if(dumper->down) {
472         printf("driver: send-cmd time %s ignored to down dumper %s: %s",
473                walltime_str(curclock()), dumper->name, cmdline);
474     } else {
475         printf("driver: send-cmd time %s to %s: %s",
476                walltime_str(curclock()), dumper->name, cmdline);
477         fflush(stdout);
478         if (fullwrite(dumper->fd, cmdline, strlen(cmdline)) < 0) {
479             printf("writing %s command: %s\n", dumper->name, strerror(errno));
480             fflush(stdout);
481             amfree(cmdline);
482             return 0;
483         }
484         if (cmd == QUIT) aclose(dumper->fd);
485     }
486     amfree(cmdline);
487     return 1;
488 }
489
490 int
491 chunker_cmd(
492     chunker_t *chunker,
493     cmd_t cmd,
494     disk_t *dp)
495 {
496     char *cmdline = NULL;
497     char number[NUM_STR_SIZE];
498     char chunksize[NUM_STR_SIZE];
499     char use[NUM_STR_SIZE];
500     char *o;
501     int activehd=0;
502     assignedhd_t **h=NULL;
503     char *features;
504     char *qname;
505     char *qdest;
506
507     switch(cmd) {
508     case START:
509         cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
510         break;
511     case PORT_WRITE:
512         if(dp && sched(dp) && sched(dp)->holdp) {
513             h = sched(dp)->holdp;
514             activehd = sched(dp)->activehd;
515         }
516
517         if (dp && h) {
518             qname = quote_string(dp->name);
519             qdest = quote_string(sched(dp)->destname);
520             holdalloc(h[activehd]->disk)->allocated_dumpers++;
521             snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
522             snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT,
523                     (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[0]->disk));
524             snprintf(use, SIZEOF(use), OFF_T_FMT,
525                     (OFF_T_FMT_TYPE)h[0]->reserved);
526             features = am_feature_to_string(dp->host->features);
527             o = optionstr(dp, dp->host->features, NULL);
528             if ( o == NULL ) {
529               error("problem with option string, check the dumptype definition.\n");
530             }
531             cmdline = vstralloc(cmdstr[cmd],
532                             " ", disk2serial(dp),
533                             " ", qdest,
534                             " ", dp->host->hostname,
535                             " ", features,
536                             " ", qname,
537                             " ", number,
538                             " ", sched(dp)->dumpdate,
539                             " ", chunksize,
540                             " ", dp->program,
541                             " ", use,
542                             " |", o,
543                             "\n", NULL);
544             amfree(features);
545             amfree(o);
546             amfree(qdest);
547             amfree(qname);
548         } else {
549                 error("Write command without disk and holding disk.\n",
550                       cmdstr[cmd]);
551                 /*NOTREACHED*/
552         }
553         break;
554     case CONTINUE:
555         if(dp && sched(dp) && sched(dp)->holdp) {
556             h = sched(dp)->holdp;
557             activehd = sched(dp)->activehd;
558         }
559
560         if(dp && h) {
561             qname = quote_string(dp->name);
562             qdest = quote_string(h[activehd]->destname);
563             holdalloc(h[activehd]->disk)->allocated_dumpers++;
564             snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT, 
565                      (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[activehd]->disk));
566             snprintf(use, SIZEOF(use), OFF_T_FMT, 
567                      (OFF_T_FMT_TYPE)(h[activehd]->reserved - h[activehd]->used));
568             cmdline = vstralloc(cmdstr[cmd],
569                                 " ", disk2serial(dp),
570                                 " ", qdest,
571                                 " ", chunksize,
572                                 " ", use,
573                                 "\n", NULL );
574             amfree(qdest);
575             amfree(qname);
576         } else {
577             cmdline = stralloc2(cmdstr[cmd], "\n");
578         }
579         break;
580     case QUIT:
581     case ABORT:
582         cmdline = stralloc2(cmdstr[cmd], "\n");
583         break;
584     case DONE:
585     case FAILED:
586         if( dp ) {
587             cmdline = vstralloc(cmdstr[cmd],
588                                 " ", disk2serial(dp),
589                                 "\n",  NULL);
590         } else {
591             cmdline = vstralloc(cmdstr[cmd], "\n");
592         }
593         break;
594     default:
595         error("Don't know how to send %s command to chunker", cmdstr[cmd]);
596         /*NOTREACHED*/
597     }
598
599     /*
600      * Note: cmdline already has a '\n'.
601      */
602     printf("driver: send-cmd time %s to %s: %s",
603            walltime_str(curclock()), chunker->name, cmdline);
604     fflush(stdout);
605     if (fullwrite(chunker->fd, cmdline, strlen(cmdline)) < 0) {
606         printf("writing %s command: %s\n", chunker->name, strerror(errno));
607         fflush(stdout);
608         amfree(cmdline);
609         return 0;
610     }
611     if (cmd == QUIT) aclose(chunker->fd);
612     amfree(cmdline);
613     return 1;
614 }
615
616 #define MAX_SERIAL MAX_DUMPERS+1        /* one for the taper */
617
618 long generation = 1;
619
620 struct serial_s {
621     long gen;
622     disk_t *dp;
623 } stable[MAX_SERIAL];
624
625 disk_t *
626 serial2disk(
627     char *str)
628 {
629     int rc, s;
630     long gen;
631
632     rc = sscanf(str, "%d-%ld", &s, &gen);
633     if(rc != 2) {
634         error("error [serial2disk \"%s\" parse error]", str);
635         /*NOTREACHED*/
636     } else if (s < 0 || s >= MAX_SERIAL) {
637         error("error [serial out of range 0..%d: %d]", MAX_SERIAL, s);
638         /*NOTREACHED*/
639     }
640     if(gen != stable[s].gen)
641         printf("driver: serial2disk error time %s serial gen mismatch %s\n",
642                walltime_str(curclock()), str);
643     return stable[s].dp;
644 }
645
646 void
647 free_serial(
648     char *str)
649 {
650     int rc, s;
651     long gen;
652
653     rc = sscanf(str, "%d-%ld", &s, &gen);
654     if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
655         /* nuke self to get core dump for Brett */
656         fprintf(stderr, "driver: free_serial: str \"%s\" rc %d s %d\n",
657                 str, rc, s);
658         fflush(stderr);
659         abort();
660     }
661
662     if(gen != stable[s].gen)
663         printf("driver: free_serial error time %s serial gen mismatch %s\n",
664                walltime_str(curclock()),str);
665     stable[s].gen = 0;
666     stable[s].dp = NULL;
667 }
668
669
670 void
671 free_serial_dp(
672     disk_t *dp)
673 {
674     int s;
675
676     for(s = 0; s < MAX_SERIAL; s++) {
677         if(stable[s].dp == dp) {
678             stable[s].gen = 0;
679             stable[s].dp = NULL;
680             return;
681         }
682     }
683
684     printf("driver: error time %s serial not found\n",
685            walltime_str(curclock()));
686 }
687
688
689 void
690 check_unfree_serial(void)
691 {
692     int s;
693
694     /* find used serial number */
695     for(s = 0; s < MAX_SERIAL; s++) {
696         if(stable[s].gen != 0 || stable[s].dp != NULL) {
697             printf("driver: error time %s bug: serial in use: %02d-%05ld\n",
698                    walltime_str(curclock()), s, stable[s].gen);
699         }
700     }
701 }
702
703 char *disk2serial(
704     disk_t *dp)
705 {
706     int s;
707     static char str[NUM_STR_SIZE];
708
709     for(s = 0; s < MAX_SERIAL; s++) {
710         if(stable[s].dp == dp) {
711             snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
712             return str;
713         }
714     }
715
716     /* find unused serial number */
717     for(s = 0; s < MAX_SERIAL; s++)
718         if(stable[s].gen == 0 && stable[s].dp == NULL)
719             break;
720     if(s >= MAX_SERIAL) {
721         printf("driver: error time %s bug: out of serial numbers\n",
722                walltime_str(curclock()));
723         s = 0;
724     }
725
726     stable[s].gen = generation++;
727     stable[s].dp = dp;
728
729     snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
730     return str;
731 }
732
733 void
734 update_info_dumper(
735      disk_t *dp,
736      off_t origsize,
737      off_t dumpsize,
738      time_t dumptime)
739 {
740     int level, i;
741     info_t info;
742     stats_t *infp;
743     perf_t *perfp;
744     char *conf_infofile;
745
746     level = sched(dp)->level;
747
748     conf_infofile = getconf_str(CNF_INFOFILE);
749     if (*conf_infofile == '/') {
750         conf_infofile = stralloc(conf_infofile);
751     } else {
752         conf_infofile = stralloc2(config_dir, conf_infofile);
753     }
754     if (open_infofile(conf_infofile)) {
755         error("could not open info db \"%s\"", conf_infofile);
756         /*NOTREACHED*/
757     }
758     amfree(conf_infofile);
759
760     get_info(dp->host->hostname, dp->name, &info);
761
762     /* Clean up information about this and higher-level dumps.  This
763        assumes that update_info_dumper() is always run before
764        update_info_taper(). */
765     for (i = level; i < DUMP_LEVELS; ++i) {
766       infp = &info.inf[i];
767       infp->size = (off_t)-1;
768       infp->csize = (off_t)-1;
769       infp->secs = (time_t)-1;
770       infp->date = (time_t)-1;
771       infp->label[0] = '\0';
772       infp->filenum = 0;
773     }
774
775     /* now store information about this dump */
776     infp = &info.inf[level];
777     infp->size = origsize;
778     infp->csize = dumpsize;
779     infp->secs = dumptime;
780     infp->date = sched(dp)->timestamp;
781
782     if(level == 0) perfp = &info.full;
783     else perfp = &info.incr;
784
785     /* Update the stats, but only if the new values are meaningful */
786     if(dp->compress != COMP_NONE && origsize > (off_t)0) {
787         newperf(perfp->comp, (double)dumpsize/(double)origsize);
788     }
789     if(dumptime > (time_t)0) {
790         if((off_t)dumptime >= dumpsize)
791             newperf(perfp->rate, 1);
792         else
793             newperf(perfp->rate, (double)dumpsize/(double)dumptime);
794     }
795
796     if(getconf_int(CNF_RESERVE)<100) {
797         info.command = NO_COMMAND;
798     }
799
800     if(level == info.last_level)
801         info.consecutive_runs++;
802     else {
803         info.last_level = level;
804         info.consecutive_runs = 1;
805     }
806
807     if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
808         for(i=NB_HISTORY-1;i>0;i--) {
809             info.history[i] = info.history[i-1];
810         }
811
812         info.history[0].level = level;
813         info.history[0].size  = origsize;
814         info.history[0].csize = dumpsize;
815         info.history[0].date  = sched(dp)->timestamp;
816         info.history[0].secs  = dumptime;
817     }
818
819     if(put_info(dp->host->hostname, dp->name, &info)) {
820         error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
821         /*NOTREACHED*/
822     }
823
824     close_infofile();
825 }
826
827 void
828 update_info_taper(
829     disk_t *dp,
830     char *label,
831     off_t filenum,
832     int level)
833 {
834     info_t info;
835     stats_t *infp;
836     int rc;
837
838     rc = open_infofile(getconf_str(CNF_INFOFILE));
839     if(rc) {
840         error("could not open infofile %s: %s (%d)", getconf_str(CNF_INFOFILE),
841               strerror(errno), rc);
842         /*NOTREACHED*/
843     }
844
845     get_info(dp->host->hostname, dp->name, &info);
846
847     infp = &info.inf[level];
848     /* XXX - should we record these two if no-record? */
849     strncpy(infp->label, label, SIZEOF(infp->label)-1);
850     infp->label[SIZEOF(infp->label)-1] = '\0';
851     infp->filenum = filenum;
852
853     info.command = NO_COMMAND;
854
855     if(put_info(dp->host->hostname, dp->name, &info)) {
856         error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
857         /*NOTREACHED*/
858     }
859     close_infofile();
860 }
861
862 /* Free an array of pointers to assignedhd_t after freeing the
863  * assignedhd_t themselves. The array must be NULL-terminated.
864  */
865 void free_assignedhd(
866     assignedhd_t **ahd)
867 {
868     int i;
869
870     if( !ahd ) { return; }
871
872     for( i = 0; ahd[i]; i++ ) {
873         amfree(ahd[i]->destname);
874         amfree(ahd[i]);
875     }
876     amfree(ahd);
877 }