326c1be40845d29908bec2f08554a50a6f0e02dd
[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 && dumper->chunker->fd == fd)
76             return (dumper->chunker->name);
77     }
78     g_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], (int)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], (int)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         safe_fd(-1, 0);
118         execve(taper_program, config_options, safe_env());
119         error("exec %s: %s", taper_program, strerror(errno));
120         /*NOTREACHED*/
121
122     default:    /* parent process */
123         aclose(fd[1]);
124         taper = fd[0];
125         taper_ev_read = NULL;
126     }
127 }
128
129 void
130 startup_dump_process(
131     dumper_t *dumper,
132     char *dumper_program)
133 {
134     int    fd[2];
135     char **config_options;
136
137     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
138         error(_("%s pipe: %s"), dumper->name, strerror(errno));
139         /*NOTREACHED*/
140     }
141
142     switch(dumper->pid = fork()) {
143     case -1:
144         error(_("fork %s: %s"), dumper->name, strerror(errno));
145         /*NOTREACHED*/
146
147     case 0:             /* child process */
148         aclose(fd[0]);
149         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
150             error(_("%s dup2: %s"), dumper->name, strerror(errno));
151         config_options = get_config_options(2);
152         config_options[0] = dumper->name ? dumper->name : "dumper",
153         config_options[1] = config_name;
154         safe_fd(-1, 0);
155         execve(dumper_program, config_options, safe_env());
156         error(_("exec %s (%s): %s"), dumper_program,
157               dumper->name, strerror(errno));
158         /*NOTREACHED*/
159
160     default:    /* parent process */
161         aclose(fd[1]);
162         dumper->fd = fd[0];
163         dumper->ev_read = NULL;
164         dumper->busy = dumper->down = 0;
165         dumper->dp = NULL;
166         g_fprintf(stderr,_("driver: started %s pid %u\n"),
167                 dumper->name, (unsigned)dumper->pid);
168         fflush(stderr);
169     }
170 }
171
172 void
173 startup_dump_processes(
174     char *dumper_program,
175     int inparallel,
176     char *timestamp)
177 {
178     int i;
179     dumper_t *dumper;
180     char number[NUM_STR_SIZE];
181
182     for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
183         g_snprintf(number, SIZEOF(number), "%d", i);
184         dumper->name = stralloc2("dumper", number);
185         dumper->chunker = &chktable[i];
186         chktable[i].name = stralloc2("chunker", number);
187         chktable[i].dumper = dumper;
188         chktable[i].fd = -1;
189
190         startup_dump_process(dumper, dumper_program);
191         dumper_cmd(dumper, START, (void *)timestamp);
192     }
193 }
194
195 void
196 startup_chunk_process(
197     chunker_t *chunker,
198     char *chunker_program)
199 {
200     int    fd[2];
201     char **config_options;
202
203     if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
204         error(_("%s pipe: %s"), chunker->name, strerror(errno));
205         /*NOTREACHED*/
206     }
207
208     switch(chunker->pid = fork()) {
209     case -1:
210         error(_("fork %s: %s"), chunker->name, strerror(errno));
211         /*NOTREACHED*/
212
213     case 0:             /* child process */
214         aclose(fd[0]);
215         if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
216             error(_("%s dup2: %s"), chunker->name, strerror(errno));
217             /*NOTREACHED*/
218         }
219         config_options = get_config_options(2);
220         config_options[0] = chunker->name ? chunker->name : "chunker",
221         config_options[1] = config_name;
222         safe_fd(-1, 0);
223         execve(chunker_program, config_options, safe_env());
224         error(_("exec %s (%s): %s"), chunker_program,
225               chunker->name, strerror(errno));
226         /*NOTREACHED*/
227
228     default:    /* parent process */
229         aclose(fd[1]);
230         chunker->down = 0;
231         chunker->fd = fd[0];
232         chunker->ev_read = NULL;
233         g_fprintf(stderr,_("driver: started %s pid %u\n"),
234                 chunker->name, (unsigned)chunker->pid);
235         fflush(stderr);
236     }
237 }
238
239 cmd_t
240 getresult(
241     int fd,
242     int show,
243     int *result_argc,
244     char **result_argv,
245     int max_arg)
246 {
247     int arg;
248     cmd_t t;
249     char *line;
250
251     if((line = areads(fd)) == NULL) {
252         if(errno) {
253             error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
254             /*NOTREACHED*/
255         }
256         *result_argc = 0;                               /* EOF */
257     } else {
258         *result_argc = split(line, result_argv, max_arg, " ");
259     }
260
261     if(show) {
262         g_printf(_("driver: result time %s from %s:"),
263                walltime_str(curclock()),
264                childstr(fd));
265         if(line) {
266             for(arg = 1; arg <= *result_argc; arg++) {
267                 g_printf(" %s", result_argv[arg]);
268             }
269             putchar('\n');
270         } else {
271             g_printf(" (eof)\n");
272         }
273         fflush(stdout);
274     }
275     amfree(line);
276
277 #ifdef DEBUG
278     g_printf("argc = %d\n", *result_argc);
279     for(arg = 0; arg < *result_argc; arg++)
280         g_printf("argv[%d] = \"%s\"\n", arg, result_argv[arg]);
281 #endif
282
283     if(*result_argc < 1) return BOGUS;
284
285     for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
286         if(strcmp(result_argv[1], cmdstr[t]) == 0) return t;
287
288     return BOGUS;
289 }
290
291
292 int
293 taper_cmd(
294     cmd_t cmd,
295     void *ptr,
296     char *destname,
297     int level,
298     char *datestamp)
299 {
300     char *cmdline = NULL;
301     char number[NUM_STR_SIZE];
302     char splitsize[NUM_STR_SIZE];
303     char fallback_splitsize[NUM_STR_SIZE];
304     char *diskbuffer = NULL;
305     disk_t *dp;
306     char *qname;
307     char *qdest;
308
309     switch(cmd) {
310     case START_TAPER:
311         cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
312         break;
313     case FILE_WRITE:
314         dp = (disk_t *) ptr;
315         qname = quote_string(dp->name);
316         qdest = quote_string(destname);
317         g_snprintf(number, SIZEOF(number), "%d", level);
318         g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
319                  (long long)dp->tape_splitsize * 1024);
320         cmdline = vstralloc(cmdstr[cmd],
321                             " ", disk2serial(dp),
322                             " ", qdest,
323                             " ", dp->host->hostname,
324                             " ", qname,
325                             " ", number,
326                             " ", datestamp,
327                             " ", splitsize,
328                             "\n", NULL);
329         amfree(qdest);
330         amfree(qname);
331         break;
332     case PORT_WRITE:
333         dp = (disk_t *) ptr;
334         qname = quote_string(dp->name);
335         g_snprintf(number, SIZEOF(number), "%d", level);
336
337         /*
338           If we haven't been given a place to buffer split dumps to disk,
339           make the argument something besides and empty string so's taper
340           won't get confused
341         */
342         if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
343             diskbuffer = "NULL";
344         } else {
345             diskbuffer = dp->split_diskbuffer;
346         }
347         g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
348                  (long long)dp->tape_splitsize * 1024);
349         g_snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), "%lld",
350                  (long long)dp->fallback_splitsize * 1024);
351         cmdline = vstralloc(cmdstr[cmd],
352                             " ", disk2serial(dp),
353                             " ", dp->host->hostname,
354                             " ", qname,
355                             " ", number,
356                             " ", datestamp,
357                             " ", splitsize,
358                             " ", diskbuffer,
359                             " ", fallback_splitsize,
360                             "\n", NULL);
361         amfree(qname);
362         break;
363     case DONE: /* handle */
364     case FAILED: /* handle */
365         dp = (disk_t *) ptr;
366         cmdline = vstralloc(cmdstr[cmd],
367                             " ", disk2serial(dp),
368                             "\n", NULL);
369         break;
370     case NEW_TAPE:
371     case NO_NEW_TAPE:
372     case QUIT:
373         cmdline = stralloc2(cmdstr[cmd], "\n");
374         break;
375     default:
376         error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
377         /*NOTREACHED*/
378     }
379
380     /*
381      * Note: cmdline already has a '\n'.
382      */
383     g_printf(_("driver: send-cmd time %s to taper: %s"),
384            walltime_str(curclock()), cmdline);
385     fflush(stdout);
386     if ((fullwrite(taper, cmdline, strlen(cmdline))) < 0) {
387         g_printf(_("writing taper command '%s' failed: %s\n"),
388                 cmdline, strerror(errno));
389         fflush(stdout);
390         amfree(cmdline);
391         return 0;
392     }
393     if(cmd == QUIT) aclose(taper);
394     amfree(cmdline);
395     return 1;
396 }
397
398 int
399 dumper_cmd(
400     dumper_t *dumper,
401     cmd_t cmd,
402     disk_t *dp)
403 {
404     char *cmdline = NULL;
405     char number[NUM_STR_SIZE];
406     char numberport[NUM_STR_SIZE];
407     char *o;
408     char *device;
409     char *features;
410     char *qname;
411     char *qdest;
412
413     switch(cmd) {
414     case START:
415         cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
416         break;
417     case PORT_DUMP:
418         if(dp && dp->device) {
419             device = dp->device;
420         }
421         else {
422             device = "NODEVICE";
423         }
424
425         if (dp != NULL) {
426             device = quote_string((dp->device) ? dp->device : "NODEVICE");
427             qname = quote_string(dp->name);
428             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
429             g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
430             features = am_feature_to_string(dp->host->features);
431             o = optionstr(dp, dp->host->features, NULL);
432             if ( o == NULL ) {
433               error(_("problem with option string, check the dumptype definition.\n"));
434             }
435               
436             cmdline = vstralloc(cmdstr[cmd],
437                             " ", disk2serial(dp),
438                             " ", numberport,
439                             " ", dp->host->hostname,
440                             " ", features,
441                             " ", qname,
442                             " ", device,
443                             " ", number,
444                             " ", sched(dp)->dumpdate,
445                             " ", dp->program,
446                             " ", dp->amandad_path,
447                             " ", dp->client_username,
448                             " ", dp->ssh_keys,
449                             " |", o,
450                             "\n", NULL);
451             amfree(features);
452             amfree(o);
453             amfree(qname);
454             amfree(device);
455         } else {
456                 error(_("PORT-DUMP without disk pointer\n"));
457                 /*NOTREACHED*/
458         }
459         break;
460     case QUIT:
461     case ABORT:
462         if( dp ) {
463             qdest = quote_string(sched(dp)->destname);
464             cmdline = vstralloc(cmdstr[cmd],
465                                 " ", qdest,
466                                 "\n", NULL );
467             amfree(qdest);
468         } else {
469             cmdline = stralloc2(cmdstr[cmd], "\n");
470         }
471         break;
472     default:
473         error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
474         /*NOTREACHED*/
475     }
476
477     /*
478      * Note: cmdline already has a '\n'.
479      */
480     if(dumper->down) {
481         g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
482                walltime_str(curclock()), dumper->name, cmdline);
483     } else {
484         g_printf(_("driver: send-cmd time %s to %s: %s"),
485                walltime_str(curclock()), dumper->name, cmdline);
486         fflush(stdout);
487         if (fullwrite(dumper->fd, cmdline, strlen(cmdline)) < 0) {
488             g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
489             fflush(stdout);
490             amfree(cmdline);
491             return 0;
492         }
493         if (cmd == QUIT) aclose(dumper->fd);
494     }
495     amfree(cmdline);
496     return 1;
497 }
498
499 int
500 chunker_cmd(
501     chunker_t *chunker,
502     cmd_t cmd,
503     disk_t *dp)
504 {
505     char *cmdline = NULL;
506     char number[NUM_STR_SIZE];
507     char chunksize[NUM_STR_SIZE];
508     char use[NUM_STR_SIZE];
509     char *o;
510     int activehd=0;
511     assignedhd_t **h=NULL;
512     char *features;
513     char *qname;
514     char *qdest;
515
516     switch(cmd) {
517     case START:
518         cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
519         break;
520     case PORT_WRITE:
521         if(dp && sched(dp) && sched(dp)->holdp) {
522             h = sched(dp)->holdp;
523             activehd = sched(dp)->activehd;
524         }
525
526         if (dp && h) {
527             qname = quote_string(dp->name);
528             qdest = quote_string(sched(dp)->destname);
529             h[activehd]->disk->allocated_dumpers++;
530             g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
531             g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
532                     (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
533             g_snprintf(use, SIZEOF(use), "%lld",
534                     (long long)h[0]->reserved);
535             features = am_feature_to_string(dp->host->features);
536             o = optionstr(dp, dp->host->features, NULL);
537             if ( o == NULL ) {
538               error(_("problem with option string, check the dumptype definition.\n"));
539             }
540             cmdline = vstralloc(cmdstr[cmd],
541                             " ", disk2serial(dp),
542                             " ", qdest,
543                             " ", dp->host->hostname,
544                             " ", features,
545                             " ", qname,
546                             " ", number,
547                             " ", sched(dp)->dumpdate,
548                             " ", chunksize,
549                             " ", dp->program,
550                             " ", use,
551                             " |", o,
552                             "\n", NULL);
553             amfree(features);
554             amfree(o);
555             amfree(qdest);
556             amfree(qname);
557         } else {
558                 error(_("%s command without disk and holding disk.\n"),
559                       cmdstr[cmd]);
560                 /*NOTREACHED*/
561         }
562         break;
563     case CONTINUE:
564         if(dp && sched(dp) && sched(dp)->holdp) {
565             h = sched(dp)->holdp;
566             activehd = sched(dp)->activehd;
567         }
568
569         if(dp && h) {
570             qname = quote_string(dp->name);
571             qdest = quote_string(h[activehd]->destname);
572             h[activehd]->disk->allocated_dumpers++;
573             g_snprintf(chunksize, SIZEOF(chunksize), "%lld", 
574                      (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
575             g_snprintf(use, SIZEOF(use), "%lld", 
576                      (long long)(h[activehd]->reserved - h[activehd]->used));
577             cmdline = vstralloc(cmdstr[cmd],
578                                 " ", disk2serial(dp),
579                                 " ", qdest,
580                                 " ", chunksize,
581                                 " ", use,
582                                 "\n", NULL );
583             amfree(qdest);
584             amfree(qname);
585         } else {
586             cmdline = stralloc2(cmdstr[cmd], "\n");
587         }
588         break;
589     case QUIT:
590     case ABORT:
591         cmdline = stralloc2(cmdstr[cmd], "\n");
592         break;
593     case DONE:
594     case FAILED:
595         if( dp ) {
596             cmdline = vstralloc(cmdstr[cmd],
597                                 " ", disk2serial(dp),
598                                 "\n",  NULL);
599         } else {
600             cmdline = vstralloc(cmdstr[cmd], "\n");
601         }
602         break;
603     default:
604         error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
605         /*NOTREACHED*/
606     }
607
608     /*
609      * Note: cmdline already has a '\n'.
610      */
611     g_printf(_("driver: send-cmd time %s to %s: %s"),
612            walltime_str(curclock()), chunker->name, cmdline);
613     fflush(stdout);
614     if (fullwrite(chunker->fd, cmdline, strlen(cmdline)) < 0) {
615         g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
616         fflush(stdout);
617         amfree(cmdline);
618         return 0;
619     }
620     if (cmd == QUIT) aclose(chunker->fd);
621     amfree(cmdline);
622     return 1;
623 }
624
625 #define MAX_SERIAL MAX_DUMPERS+1        /* one for the taper */
626
627 long generation = 1;
628
629 struct serial_s {
630     long gen;
631     disk_t *dp;
632 } stable[MAX_SERIAL];
633
634 disk_t *
635 serial2disk(
636     char *str)
637 {
638     int rc, s;
639     long gen;
640
641     rc = sscanf(str, "%d-%ld", &s, &gen);
642     if(rc != 2) {
643         error(_("error [serial2disk \"%s\" parse error]"), str);
644         /*NOTREACHED*/
645     } else if (s < 0 || s >= MAX_SERIAL) {
646         error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
647         /*NOTREACHED*/
648     }
649     if(gen != stable[s].gen)
650         g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
651                walltime_str(curclock()), str);
652     return stable[s].dp;
653 }
654
655 void
656 free_serial(
657     char *str)
658 {
659     int rc, s;
660     long gen;
661
662     rc = sscanf(str, _("%d-%ld"), &s, &gen);
663     if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
664         /* nuke self to get core dump for Brett */
665         g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
666                 str, rc, s);
667         fflush(stderr);
668         abort();
669     }
670
671     if(gen != stable[s].gen)
672         g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
673                walltime_str(curclock()),str);
674     stable[s].gen = 0;
675     stable[s].dp = NULL;
676 }
677
678
679 void
680 free_serial_dp(
681     disk_t *dp)
682 {
683     int s;
684
685     for(s = 0; s < MAX_SERIAL; s++) {
686         if(stable[s].dp == dp) {
687             stable[s].gen = 0;
688             stable[s].dp = NULL;
689             return;
690         }
691     }
692
693     g_printf(_("driver: error time %s serial not found\n"),
694            walltime_str(curclock()));
695 }
696
697
698 void
699 check_unfree_serial(void)
700 {
701     int s;
702
703     /* find used serial number */
704     for(s = 0; s < MAX_SERIAL; s++) {
705         if(stable[s].gen != 0 || stable[s].dp != NULL) {
706             g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
707                    walltime_str(curclock()), s, stable[s].gen);
708         }
709     }
710 }
711
712 char *disk2serial(
713     disk_t *dp)
714 {
715     int s;
716     static char str[NUM_STR_SIZE];
717
718     for(s = 0; s < MAX_SERIAL; s++) {
719         if(stable[s].dp == dp) {
720             g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
721             return str;
722         }
723     }
724
725     /* find unused serial number */
726     for(s = 0; s < MAX_SERIAL; s++)
727         if(stable[s].gen == 0 && stable[s].dp == NULL)
728             break;
729     if(s >= MAX_SERIAL) {
730         g_printf(_("driver: error time %s bug: out of serial numbers\n"),
731                walltime_str(curclock()));
732         s = 0;
733     }
734
735     stable[s].gen = generation++;
736     stable[s].dp = dp;
737
738     g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
739     return str;
740 }
741
742 void
743 update_info_dumper(
744      disk_t *dp,
745      off_t origsize,
746      off_t dumpsize,
747      time_t dumptime)
748 {
749     int level, i;
750     info_t info;
751     stats_t *infp;
752     perf_t *perfp;
753     char *conf_infofile;
754
755     level = sched(dp)->level;
756
757     conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
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 }