2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: infofile.c,v 1.64 2006/07/25 18:18:48 martinea Exp $
30 * manage current info file
37 static void zero_info(info_t *);
40 static char *infodir = (char *)0;
41 static char *infofile = (char *)0;
42 static char *newinfofile;
45 static FILE *open_txinfofile(char *, char *, char *);
46 static int close_txinfofile(FILE *);
47 static int read_txinfofile(FILE *, info_t *);
48 static int write_txinfofile(FILE *, info_t *);
49 static int delete_txinfofile(char *, char *);
52 /*# define HEADER (SIZEOF(info_t)-DUMP_LEVELS*SIZEOF(stats_t))*/
54 static DBM *infodb = NULL;
70 assert(infofile == (char *)0);
72 writing = (*mode == 'w');
74 myhost = sanitise_filename(host);
75 mydisk = sanitise_filename(disk);
77 infofile = vstralloc(infodir,
86 /* create the directory structure if in write mode */
88 if (mkpdir(infofile, 02755, (uid_t)-1, (gid_t)-1) == -1) {
94 newinfofile = stralloc2(infofile, ".new");
97 infof = fopen(newinfofile, mode);
99 amflock(fileno(infof), "info");
102 infof = fopen(infofile, mode);
103 /* no need to lock readers */
106 if(infof == (FILE *)0) {
121 assert(infofile != (char *)0);
124 rc = rename(newinfofile, infofile);
126 amfunlock(fileno(infof), "info");
132 rc = rc || fclose(infof);
139 /* XXX - code assumes AVG_COUNT == 3 */
154 /* get version: command: lines */
156 while ((line = agets(infof)) != NULL) {
161 if (line == NULL) return -1;
162 rc = sscanf(line, "version: %d", &version);
164 if(rc != 1) return -2;
166 while ((line = agets(infof)) != NULL) {
171 if (line == NULL) return -1;
172 rc = sscanf(line, "command: %u", &info->command);
174 if(rc != 1) return -2;
176 /* get rate: and comp: lines for full dumps */
180 while ((line = agets(infof)) != NULL) {
185 if (line == NULL) return -1;
186 rc = sscanf(line, "full-rate: %lf %lf %lf",
187 &pp->rate[0], &pp->rate[1], &pp->rate[2]);
189 if(rc > 3) return -2;
191 while ((line = agets(infof)) != NULL) {
196 if (line == NULL) return -1;
197 rc = sscanf(line, "full-comp: %lf %lf %lf",
198 &pp->comp[0], &pp->comp[1], &pp->comp[2]);
200 if(rc > 3) return -2;
202 /* get rate: and comp: lines for incr dumps */
206 while ((line = agets(infof)) != NULL) {
211 if (line == NULL) return -1;
212 rc = sscanf(line, "incr-rate: %lf %lf %lf",
213 &pp->rate[0], &pp->rate[1], &pp->rate[2]);
215 if(rc > 3) return -2;
217 while ((line = agets(infof)) != NULL) {
222 if (line == NULL) return -1;
223 rc = sscanf(line, "incr-comp: %lf %lf %lf",
224 &pp->comp[0], &pp->comp[1], &pp->comp[2]);
226 if(rc > 3) return -2;
228 /* get stats for dump levels */
230 for(rc = -2; (line = agets(infof)) != NULL; free(line)) {
231 stats_t onestat; /* one stat record */
233 time_t *date_p = &date;
239 if(line[0] == '/' && line[1] == '/') {
242 return 0; /* normal end of record */
244 else if (strncmp_const(line,"last_level:") == 0) {
247 else if (strncmp_const(line,"history:") == 0) {
250 memset(&onestat, 0, SIZEOF(onestat));
255 if(strncmp_const_skip(line, "stats:", s, ch) != 0) {
259 skip_whitespace(s, ch);
260 if(ch == '\0' || sscanf((s - 1), "%d", &level) != 1) {
265 skip_whitespace(s, ch);
266 if(ch == '\0' || sscanf((s - 1), OFF_T_FMT,
267 (OFF_T_FMT_TYPE *)&onestat.size) != 1) {
272 skip_whitespace(s, ch);
273 if(ch == '\0' || sscanf((s - 1), OFF_T_FMT,
274 (OFF_T_FMT_TYPE *)&onestat.csize) != 1) {
279 skip_whitespace(s, ch);
280 secs_p = &onestat.secs;
281 if(ch == '\0' || sscanf((s - 1), TIME_T_FMT,
282 (TIME_T_FMT_TYPE *)secs_p) != 1) {
287 skip_whitespace(s, ch);
288 if(ch == '\0' || sscanf((s - 1), TIME_T_FMT,
289 (TIME_T_FMT_TYPE *)date_p) != 1) {
294 skip_whitespace(s, ch);
296 if(sscanf((s - 1), OFF_T_FMT,
297 (OFF_T_FMT_TYPE *)&onestat.filenum) != 1) {
302 skip_whitespace(s, ch);
306 strncpy(onestat.label, s-1, SIZEOF(onestat.label)-1);
307 onestat.label[SIZEOF(onestat.label)-1] = '\0';
310 onestat.date = date; /* time_t not guarranteed to be long */
312 if(level < 0 || level > DUMP_LEVELS-1)
315 info->inf[level] = onestat;
318 if(line == NULL) return -1;
320 rc = sscanf(line, "last_level: %d %d",
321 &info->last_level, &info->consecutive_runs);
324 if(rc > 2) return -2;
328 for(i=0;i<=NB_HISTORY;i++) {
329 info->history[i].level = -2;
332 for(rc = -2; (line = agets(infof)) != NULL; free(line)) {
333 history_t onehistory; /* one history record */
335 time_t *date_p = &date;
341 if(line[0] == '/' && line[1] == '/') {
342 info->history[nb_history].level = -2;
345 return 0; /* normal end of record */
348 memset(&onehistory, 0, SIZEOF(onehistory));
353 if(strncmp_const_skip(line, "history:", s, ch) != 0) {
358 skip_whitespace(s, ch);
359 if(ch == '\0' || sscanf((s - 1), "%d", &onehistory.level) != 1) {
365 skip_whitespace(s, ch);
366 if(ch == '\0' || sscanf((s - 1), OFF_T_FMT,
367 (OFF_T_FMT_TYPE *)&onehistory.size) != 1) {
373 skip_whitespace(s, ch);
374 if(ch == '\0' || sscanf((s - 1), OFF_T_FMT,
375 (OFF_T_FMT_TYPE *)&onehistory.csize) != 1) {
381 skip_whitespace(s, ch);
382 if(ch == '\0' || sscanf((s - 1), TIME_T_FMT,
383 (TIME_T_FMT_TYPE *)date_p) != 1) {
389 onehistory.date = date; /* time_t not guaranteed to be long */
391 onehistory.secs = (unsigned long)-1;
392 skip_whitespace(s, ch);
393 secs_p = &onehistory.secs;
395 if(sscanf((s - 1), TIME_T_FMT,
396 (TIME_T_FMT_TYPE *)secs_p) != 1) {
403 info->history[nb_history++] = onehistory;
407 while ((line = agets(infof)) != NULL) {
412 if (line == NULL) return -1;
428 fprintf(infof, "version: %d\n", 0);
430 fprintf(infof, "command: %u\n", info->command);
434 fprintf(infof, "full-rate:");
435 for(i=0; i<AVG_COUNT; i++)
436 if(pp->rate[i] >= 0.0)
437 fprintf(infof, " %lf", pp->rate[i]);
438 fprintf(infof, "\n");
440 fprintf(infof, "full-comp:");
441 for(i=0; i<AVG_COUNT; i++)
442 if(pp->comp[i] >= 0.0)
443 fprintf(infof, " %lf", pp->comp[i]);
444 fprintf(infof, "\n");
448 fprintf(infof, "incr-rate:");
449 for(i=0; i<AVG_COUNT; i++)
450 if(pp->rate[i] >= 0.0)
451 fprintf(infof, " %lf", pp->rate[i]);
452 fprintf(infof, "\n");
454 fprintf(infof, "incr-comp:");
455 for(i=0; i<AVG_COUNT; i++)
456 if(pp->comp[i] >= 0.0)
457 fprintf(infof, " %lf", pp->comp[i]);
458 fprintf(infof, "\n");
460 for(level=0; level<DUMP_LEVELS; level++) {
461 sp = &info->inf[level];
463 if(sp->date < (time_t)0 && sp->label[0] == '\0') continue;
465 fprintf(infof, "stats: %d " OFF_T_FMT " " OFF_T_FMT
466 " " TIME_T_FMT " " OFF_T_FMT,
467 level, (OFF_T_FMT_TYPE)sp->size, (OFF_T_FMT_TYPE)sp->csize,
468 (TIME_T_FMT_TYPE)sp->secs, (OFF_T_FMT_TYPE)sp->date);
469 if(sp->label[0] != '\0')
470 fprintf(infof, " " OFF_T_FMT " %s",
471 (OFF_T_FMT_TYPE)sp->filenum, sp->label);
472 fprintf(infof, "\n");
475 fprintf(infof, "last_level: %d %d\n", info->last_level, info->consecutive_runs);
477 for(i=0;info->history[i].level > -1;i++) {
478 fprintf(infof, "history: %d " OFF_T_FMT " " OFF_T_FMT
479 " " TIME_T_FMT " " TIME_T_FMT "\n",
480 info->history[i].level,
481 (OFF_T_FMT_TYPE)info->history[i].size,
482 (OFF_T_FMT_TYPE)info->history[i].csize,
483 (TIME_T_FMT_TYPE)info->history[i].date,
484 (TIME_T_FMT_TYPE)info->history[i].secs);
486 fprintf(infof, "//\n");
496 char *fn = NULL, *fn_new = NULL;
501 myhost = sanitise_filename(host);
502 mydisk = sanitise_filename(disk);
503 fn = vstralloc(infodir,
508 fn_new = stralloc2(fn, ".new");
516 rc = rmpdir(fn, infodir);
524 static char *lockname = NULL;
532 assert(infodir == (char *)0);
534 infodir = stralloc(filename);
536 return 0; /* success! */
538 /* lock the dbm file */
540 lockname = newstralloc2(lockname, filename, ".lck");
541 if((lockfd = open(lockname, O_CREAT|O_RDWR, 0644)) == -1)
544 if(amflock(lockfd, "info") == -1) {
550 if(!(infodb = dbm_open(filename, O_CREAT|O_RDWR, 0644))) {
551 amfunlock(lockfd, "info");
557 return (infodb == NULL); /* return 1 on error */
565 assert(infodir != (char *)0);
571 if(amfunlock(lockfd, "info") == -1) {
572 error("could not unlock infofile: %s", strerror(errno));
583 /* Convert a dump level to a GMT based time stamp */
589 static char stamp[20]; /* YYYY:MM:DD:hh:mm:ss */
596 for(l = 0; l < lev; l++) {
597 this = info->inf[l].date;
598 if (this > last) last = this;
602 snprintf(stamp, SIZEOF(stamp), "%d:%d:%d:%d:%d:%d",
603 t->tm_year+1900, t->tm_mon+1, t->tm_mday,
604 t->tm_hour, t->tm_min, t->tm_sec);
614 double * a, /* array of items to average */
615 double d) /* default value */
617 double sum; /* running total */
618 int n; /* number of items in sum */
625 for(i = 0; i < AVG_COUNT; i++) {
643 memset(info, '\0', SIZEOF(info_t));
645 for(i = 0; i < AVG_COUNT; i++) {
646 info->full.comp[i] = info->incr.comp[i] = -1.0;
647 info->full.rate[i] = info->incr.rate[i] = -1.0;
650 for(i = 0; i < DUMP_LEVELS; i++) {
651 info->inf[i].date = (time_t)-1;
654 info->last_level = -1;
655 info->consecutive_runs = -1;
657 for(i=0;i<=NB_HISTORY;i++) {
658 info->history[i].level = -2;
659 info->history[i].size = (off_t)0;
660 info->history[i].csize = (off_t)0;
661 info->history[i].date = 0UL;
674 (void) zero_info(info);
680 infof = open_txinfofile(hostname, diskname, "r");
683 rc = -1; /* record not found */
686 rc = read_txinfofile(infof, info);
688 close_txinfofile(infof);
695 k.dptr = vstralloc(hostname, ":", diskname, NULL);
696 k.dsize = strlen(k.dptr)+1;
700 d = dbm_fetch(infodb, k);
703 rc = -1; /* record not found */
706 memcpy(info, d.dptr, d.dsize);
724 (void)hostname; /* Quiet unused parameter warning */
725 (void)hostname_size; /* Quiet unused parameter warning */
726 (void)diskname; /* Quiet unused parameter warning */
727 (void)diskname_size; /* Quiet unused parameter warning */
737 k = dbm_firstkey(infodb);
738 if(k.dptr == NULL) return 0;
743 skip_whitespace(s, ch);
744 if(ch == '\0') return 0;
746 while(ch && ch != ':') {
747 if(fp >= hostname+hostname_size-1) {
754 if(fp == NULL) return 0;
757 if(ch != ':') return 0;
759 copy_string(s, ch, diskname, diskname_size, fp);
760 if(fp == NULL) return 0;
775 (void)hostname; /* Quiet unused parameter warning */
776 (void)hostname_size; /* Quiet unused parameter warning */
777 (void)diskname; /* Quiet unused parameter warning */
778 (void)diskname_size; /* Quiet unused parameter warning */
788 k = dbm_nextkey(infodb);
789 if(k.dptr == NULL) return 0;
794 skip_whitespace(s, ch);
795 if(ch == '\0') return 0;
797 while(ch && ch != ':') {
798 if(fp >= hostname+hostname_size-1) {
805 if(fp == NULL) return 0;
808 if(ch != ':') return 0;
810 copy_string(s, ch, diskname, diskname_size, fp);
811 if(fp == NULL) return 0;
828 infof = open_txinfofile(hostname, diskname, "w");
830 if(infof == NULL) return -1;
832 rc = write_txinfofile(infof, info);
834 rc = rc || close_txinfofile(infof);
843 k.dptr = vstralloc(hostname, ":", diskname, NULL);
844 k.dsize = strlen(k.dptr)+1;
846 d.dptr = (char *)info;
847 d.dsize = SIZEOF(info_t);
851 if(dbm_store(infodb, k, d, DBM_REPLACE) != 0) {
868 return delete_txinfofile(hostname, diskname);
875 k.dptr = vstralloc(hostname, ":", diskname, NULL);
876 k.dsize = strlen(key)+1;
878 /* delete key and record */
880 if(dbm_delete(infodb, k) != 0) {
892 void dump_rec(info_t *info);
901 printf("command word: %d\n", info->command);
902 printf("full dump rate (K/s) %5.1lf, %5.1lf, %5.1lf\n",
903 info->full.rate[0],info->full.rate[1],info->full.rate[2]);
904 printf("full comp rate %5.1lf, %5.1lf, %5.1lf\n",
905 info->full.comp[0]*100,info->full.comp[1]*100,info->full.comp[2]*100);
906 printf("incr dump rate (K/s) %5.1lf, %5.1lf, %5.1lf\n",
907 info->incr.rate[0],info->incr.rate[1],info->incr.rate[2]);
908 printf("incr comp rate %5.1lf, %5.1lf, %5.1lf\n",
909 info->incr.comp[0]*100,info->incr.comp[1]*100,info->incr.comp[2]*100);
910 for(i = 0; i < DUMP_LEVELS; i++) {
912 if( sp->size != -1) {
914 printf("lev %d date %ld tape %s filenum " OFF_T_FMT " size %ld csize %ld secs %ld\n",
915 i, (long)sp->date, sp->label, sp->filenum,
916 sp->size, sp->csize, sp->secs);
920 printf("last_level: %d %d\n", info->last_level, info->consecutive_runs);
924 void dump_db( char *host, char *disk);
934 if((rc = get_info(host, disk, &info)) == 0) {
937 printf("cannot fetch information for %s:%s rc=%d\n", host, disk, rc);
950 printf("info database %s:\n--------\n", str);
952 k = dbm_firstkey(infodb);
953 while(k.dptr != NULL) {
955 printf("%3d: KEY %s =\n", rec, k.dptr);
957 d = dbm_fetch(infodb, k);
958 memset(&info, '\0', SIZEOF(info));
959 memcpy(&info, d.dptr, d.dsize);
961 num = (d.dsize-HEADER)/SIZEOF(stats_t);
964 k = dbm_nextkey(infodb);
977 unsigned long malloc_hist_1, malloc_size_1;
978 unsigned long malloc_hist_2, malloc_size_2;
982 set_pname("infofile");
984 dbopen(DBG_SUBDIR_SERVER);
986 malloc_size_1 = malloc_inuse(&malloc_hist_1);
988 for(i = 1; i < argc; ++i) {
991 fprintf(stderr,"usage: %s host disk [host disk ...]\n",argv[0]);
994 open_infofile("curinfo");
995 dump_db(argv[i], argv[i+1]);
998 open_infofile(argv[i]);
1004 malloc_size_2 = malloc_inuse(&malloc_hist_2);
1006 if(malloc_size_1 != malloc_size_2) {
1007 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);