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 *);
39 static char *infodir = (char *)0;
40 static char *infofile = (char *)0;
41 static char *newinfofile;
44 static FILE *open_txinfofile(char *, char *, char *);
45 static int close_txinfofile(FILE *);
46 static int read_txinfofile(FILE *, info_t *);
47 static int write_txinfofile(FILE *, info_t *);
48 static int delete_txinfofile(char *, char *);
60 assert(infofile == (char *)0);
62 writing = (*mode == 'w');
64 myhost = sanitise_filename(host);
65 mydisk = sanitise_filename(disk);
67 infofile = vstralloc(infodir,
76 /* create the directory structure if in write mode */
78 if (mkpdir(infofile, 0755, (uid_t)-1, (gid_t)-1) == -1) {
84 newinfofile = stralloc2(infofile, ".new");
87 infof = fopen(newinfofile, mode);
89 amflock(fileno(infof), "info");
92 infof = fopen(infofile, mode);
93 /* no need to lock readers */
96 if(infof == (FILE *)0) {
111 assert(infofile != (char *)0);
114 rc = rename(newinfofile, infofile);
116 amfunlock(fileno(infof), "info");
122 rc = rc || fclose(infof);
129 /* XXX - code assumes AVG_COUNT == 3 */
144 /* get version: command: lines */
146 while ((line = agets(infof)) != NULL) {
151 if (line == NULL) return -1;
152 rc = sscanf(line, _("version: %d"), &version);
154 if(rc != 1) return -2;
156 while ((line = agets(infof)) != NULL) {
161 if (line == NULL) return -1;
162 rc = sscanf(line, _("command: %u"), &info->command);
164 if(rc != 1) return -2;
166 /* get rate: and comp: lines for full dumps */
170 while ((line = agets(infof)) != NULL) {
175 if (line == NULL) return -1;
176 rc = sscanf(line, "full-rate: %lf %lf %lf",
177 &pp->rate[0], &pp->rate[1], &pp->rate[2]);
179 if(rc > 3) return -2;
181 while ((line = agets(infof)) != NULL) {
186 if (line == NULL) return -1;
187 rc = sscanf(line, "full-comp: %lf %lf %lf",
188 &pp->comp[0], &pp->comp[1], &pp->comp[2]);
190 if(rc > 3) return -2;
192 /* get rate: and comp: lines for incr dumps */
196 while ((line = agets(infof)) != NULL) {
201 if (line == NULL) return -1;
202 rc = sscanf(line, "incr-rate: %lf %lf %lf",
203 &pp->rate[0], &pp->rate[1], &pp->rate[2]);
205 if(rc > 3) return -2;
207 while ((line = agets(infof)) != NULL) {
212 if (line == NULL) return -1;
213 rc = sscanf(line, "incr-comp: %lf %lf %lf",
214 &pp->comp[0], &pp->comp[1], &pp->comp[2]);
216 if(rc > 3) return -2;
218 /* get stats for dump levels */
220 for(rc = -2; (line = agets(infof)) != NULL; free(line)) {
221 stats_t onestat; /* one stat record */
228 if(line[0] == '/' && line[1] == '/') {
231 return 0; /* normal end of record */
233 else if (strncmp_const(line,"last_level:") == 0) {
236 else if (strncmp_const(line,"history:") == 0) {
239 memset(&onestat, 0, SIZEOF(onestat));
244 if(strncmp_const_skip(line, "stats:", s, ch) != 0) {
248 skip_whitespace(s, ch);
249 if(ch == '\0' || sscanf((s - 1), "%d", &level) != 1) {
254 skip_whitespace(s, ch);
255 if(ch == '\0' || sscanf((s - 1), "%lld", &off_t_tmp) != 1) {
258 onestat.size = (off_t)off_t_tmp;
261 skip_whitespace(s, ch);
262 if(ch == '\0' || sscanf((s - 1), "%lld", &off_t_tmp) != 1) {
265 onestat.csize = (off_t)off_t_tmp;
268 skip_whitespace(s, ch);
269 if(ch == '\0' || sscanf((s - 1), "%jd", &time_t_tmp) != 1) {
272 onestat.secs = (time_t)time_t_tmp;
275 skip_whitespace(s, ch);
276 if(ch == '\0' || sscanf((s - 1), "%jd", &time_t_tmp) != 1) {
279 onestat.date = (time_t)time_t_tmp;
282 skip_whitespace(s, ch);
284 if(sscanf((s - 1), "%lld", &off_t_tmp) != 1) {
287 onestat.filenum = (off_t)off_t_tmp;
290 skip_whitespace(s, ch);
294 strncpy(onestat.label, s-1, SIZEOF(onestat.label)-1);
295 onestat.label[SIZEOF(onestat.label)-1] = '\0';
298 if(level < 0 || level > DUMP_LEVELS-1)
301 info->inf[level] = onestat;
304 if(line == NULL) return -1;
306 rc = sscanf(line, "last_level: %d %d",
307 &info->last_level, &info->consecutive_runs);
310 if(rc > 2) return -2;
314 for(i=0;i<=NB_HISTORY;i++) {
315 info->history[i].level = -2;
318 for(rc = -2; (line = agets(infof)) != NULL; free(line)) {
319 history_t onehistory; /* one history record */
325 if(line[0] == '/' && line[1] == '/') {
326 info->history[nb_history].level = -2;
329 return 0; /* normal end of record */
332 memset(&onehistory, 0, SIZEOF(onehistory));
337 if(strncmp_const_skip(line, "history:", s, ch) != 0) {
342 skip_whitespace(s, ch);
343 if(ch == '\0' || sscanf((s - 1), "%d", &onehistory.level) != 1) {
349 skip_whitespace(s, ch);
350 if(ch == '\0' || sscanf((s - 1), "%lld", &off_t_tmp) != 1) {
354 onehistory.size = (off_t)off_t_tmp;
357 skip_whitespace(s, ch);
358 if(ch == '\0' || sscanf((s - 1), "%lld", &off_t_tmp) != 1) {
362 onehistory.csize = (off_t)off_t_tmp;
365 skip_whitespace(s, ch);
366 if(ch == '\0' || sscanf((s - 1), "%jd", &time_t_tmp) != 1) {
370 onehistory.date = (time_t)time_t_tmp;
373 onehistory.secs = (unsigned long)-1;
374 skip_whitespace(s, ch);
376 if(sscanf((s - 1), "%jd", &time_t_tmp) != 1) {
380 onehistory.secs = (time_t)time_t_tmp;
384 info->history[nb_history++] = onehistory;
388 while ((line = agets(infof)) != NULL) {
393 if (line == NULL) return -1;
409 g_fprintf(infof, _("version: %d\n"), 0);
411 g_fprintf(infof, _("command: %u\n"), info->command);
415 g_fprintf(infof, "full-rate:");
416 for(i=0; i<AVG_COUNT; i++)
417 if(pp->rate[i] >= 0.0)
418 g_fprintf(infof, " %lf", pp->rate[i]);
419 g_fprintf(infof, "\n");
421 g_fprintf(infof, "full-comp:");
422 for(i=0; i<AVG_COUNT; i++)
423 if(pp->comp[i] >= 0.0)
424 g_fprintf(infof, " %lf", pp->comp[i]);
425 g_fprintf(infof, "\n");
429 g_fprintf(infof, "incr-rate:");
430 for(i=0; i<AVG_COUNT; i++)
431 if(pp->rate[i] >= 0.0)
432 g_fprintf(infof, " %lf", pp->rate[i]);
433 g_fprintf(infof, "\n");
435 g_fprintf(infof, "incr-comp:");
436 for(i=0; i<AVG_COUNT; i++)
437 if(pp->comp[i] >= 0.0)
438 g_fprintf(infof, " %lf", pp->comp[i]);
439 g_fprintf(infof, "\n");
441 for(level=0; level<DUMP_LEVELS; level++) {
442 sp = &info->inf[level];
444 if(sp->date < (time_t)0 && sp->label[0] == '\0') continue;
446 g_fprintf(infof, "stats: %d %lld %lld %jd %lld",
447 level, (long long)sp->size, (long long)sp->csize,
448 (intmax_t)sp->secs, (long long)sp->date);
449 if(sp->label[0] != '\0')
450 g_fprintf(infof, " %lld %s", (long long)sp->filenum, sp->label);
451 g_fprintf(infof, "\n");
454 g_fprintf(infof, _("last_level: %d %d\n"), info->last_level, info->consecutive_runs);
456 for(i=0;info->history[i].level > -1;i++) {
457 g_fprintf(infof, _("history: %d %lld %lld %jd %jd\n"),
458 info->history[i].level,
459 (long long)info->history[i].size,
460 (long long)info->history[i].csize,
461 (intmax_t)info->history[i].date,
462 (intmax_t)info->history[i].secs);
464 g_fprintf(infof, "//\n");
474 char *fn = NULL, *fn_new = NULL;
479 myhost = sanitise_filename(host);
480 mydisk = sanitise_filename(disk);
481 fn = vstralloc(infodir,
486 fn_new = stralloc2(fn, ".new");
494 rc = rmpdir(fn, infodir);
504 assert(infodir == (char *)0);
506 infodir = stralloc(filename);
508 return 0; /* success! */
514 assert(infodir != (char *)0);
519 /* Convert a dump level to a GMT based time stamp */
525 static char stamp[20]; /* YYYY:MM:DD:hh:mm:ss */
532 for(l = 0; l < lev; l++) {
533 this = info->inf[l].date;
534 if (this > last) last = this;
538 g_snprintf(stamp, SIZEOF(stamp), "%d:%d:%d:%d:%d:%d",
539 t->tm_year+1900, t->tm_mon+1, t->tm_mday,
540 t->tm_hour, t->tm_min, t->tm_sec);
550 double * a, /* array of items to average */
551 double d) /* default value */
553 double sum; /* running total */
554 int n; /* number of items in sum */
561 for(i = 0; i < AVG_COUNT; i++) {
579 memset(info, '\0', SIZEOF(info_t));
581 for(i = 0; i < AVG_COUNT; i++) {
582 info->full.comp[i] = info->incr.comp[i] = -1.0;
583 info->full.rate[i] = info->incr.rate[i] = -1.0;
586 for(i = 0; i < DUMP_LEVELS; i++) {
587 info->inf[i].date = (time_t)-1;
590 info->last_level = -1;
591 info->consecutive_runs = -1;
593 for(i=0;i<=NB_HISTORY;i++) {
594 info->history[i].level = -2;
595 info->history[i].size = (off_t)0;
596 info->history[i].csize = (off_t)0;
597 info->history[i].date = 0UL;
610 (void) zero_info(info);
615 infof = open_txinfofile(hostname, diskname, "r");
618 rc = -1; /* record not found */
621 rc = read_txinfofile(infof, info);
623 close_txinfofile(infof);
640 infof = open_txinfofile(hostname, diskname, "w");
642 if(infof == NULL) return -1;
644 rc = write_txinfofile(infof, info);
646 rc = rc || close_txinfofile(infof);
657 return delete_txinfofile(hostname, diskname);
663 void dump_rec(info_t *info);
672 g_printf(_("command word: %d\n"), info->command);
673 g_printf(_("full dump rate (K/s) %5.1lf, %5.1lf, %5.1lf\n"),
674 info->full.rate[0],info->full.rate[1],info->full.rate[2]);
675 g_printf(_("full comp rate %5.1lf, %5.1lf, %5.1lf\n"),
676 info->full.comp[0]*100,info->full.comp[1]*100,info->full.comp[2]*100);
677 g_printf(_("incr dump rate (K/s) %5.1lf, %5.1lf, %5.1lf\n"),
678 info->incr.rate[0],info->incr.rate[1],info->incr.rate[2]);
679 g_printf(_("incr comp rate %5.1lf, %5.1lf, %5.1lf\n"),
680 info->incr.comp[0]*100,info->incr.comp[1]*100,info->incr.comp[2]*100);
681 for(i = 0; i < DUMP_LEVELS; i++) {
683 if( sp->size != -1) {
685 g_printf(_("lev %d date %ld tape %s filenum %lld size %ld csize %ld secs %ld\n"),
686 i, (long)sp->date, sp->label, sp->filenum,
687 sp->size, sp->csize, sp->secs);
691 g_printf(_("last_level: %d %d\n"), info->last_level, info->consecutive_runs);
694 void dump_db( char *host, char *disk);
704 if((rc = get_info(host, disk, &info)) == 0) {
707 g_printf(_("cannot fetch information for %s:%s rc=%d\n"), host, disk, rc);
719 * Configure program for internationalization:
720 * 1) Only set the message locale for now.
721 * 2) Set textdomain for all amanda related programs to "amanda"
722 * We don't want to be forced to support dozens of message catalogs.
724 setlocale(LC_MESSAGES, "C");
725 textdomain("amanda");
729 set_pname("infofile");
731 dbopen(DBG_SUBDIR_SERVER);
733 for(i = 1; i < argc; ++i) {
735 g_fprintf(stderr,_("usage: %s host disk [host disk ...]\n"),argv[0]);
738 open_infofile("curinfo");
739 dump_db(argv[i], argv[i+1]);