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