static FILE * open_txinfofile( char * host, char * disk, char * mode) { FILE *infof; char *myhost; char *mydisk; assert(infofile == (char *)0); writing = (*mode == 'w'); myhost = sanitise_filename(host); mydisk = sanitise_filename(disk); infofile = g_strjoin(NULL, infodir, "/", myhost, "/", mydisk, "/info", NULL); amfree(myhost); amfree(mydisk); /* create the directory structure if in write mode */ if (writing) { if (mkpdir(infofile, 0755, (uid_t)-1, (gid_t)-1) == -1) { amfree(infofile); return NULL; } } newinfofile = g_strconcat(infofile, ".new", NULL); if(writing) { infof = fopen(newinfofile, mode); if(infof != NULL) amflock(fileno(infof), "info"); } else { infof = fopen(infofile, mode); /* no need to lock readers */ } if(infof == (FILE *)0) { amfree(infofile); amfree(newinfofile); return NULL; } return infof; }
static void open_log(void) { logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600); if(logfd == -1) { error(_("could not open log file %s: %s"), logfile, strerror(errno)); /*NOTREACHED*/ } if(amflock(logfd, "log") == -1) { error(_("could not lock log file %s: %s"), logfile, strerror(errno)); /*NOTREACHED*/ } }
static void open_log(void) { char *conf_logdir; conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); logfile = g_strjoin(NULL, conf_logdir, "/log", NULL); amfree(conf_logdir); logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600); if(logfd == -1) { error(_("could not open log file %s: %s"), logfile, strerror(errno)); /*NOTREACHED*/ } if(amflock(logfd, "log") == -1) { error(_("could not lock log file %s: %s"), logfile, strerror(errno)); /*NOTREACHED*/ } }
int main( int argc, char ** argv) { #ifdef TEST /* standalone test to ckeck wether the calculated file size is ok */ struct stat finfo; int i; off_t dump_total = (off_t)0; off_t gtar_total = (off_t)0; char *d; int l, w; /* * Configure program for internationalization: * 1) Only set the message locale for now. * 2) Set textdomain for all amanda related programs to "amanda" * We don't want to be forced to support dozens of message catalogs. */ setlocale(LC_MESSAGES, "C"); textdomain("amanda"); safe_fd(-1, 0); set_pname("calcsize"); dbopen(NULL); config_init(CONFIG_INIT_CLIENT, NULL); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); if (argc < 2) { g_fprintf(stderr,_("Usage: %s file[s]\n"),argv[0]); return 1; } for(i=1; i<argc; i++) { if(lstat(argv[i], &finfo) == -1) { g_fprintf(stderr, "%s: %s\n", argv[i], strerror(errno)); continue; } g_printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size); g_printf(": blocks=%llu\n", ST_BLOCKS(finfo)); dump_total += (ST_BLOCKS(finfo) + (off_t)1) / (off_t)2 + (off_t)1; gtar_total += ROUND(4,(ST_BLOCKS(finfo) + (off_t)1)); } g_printf(" gtar dump\n"); g_printf("total %-9lu %-9lu\n",gtar_total,dump_total); return 0; #else int i; char *dirname=NULL; char *amname=NULL, *qamname=NULL; char *filename=NULL, *qfilename = NULL; if (argc > 1 && argv[1] && g_str_equal(argv[1], "--version")) { printf("calcsize-%s\n", VERSION); return (0); } safe_fd(-1, 0); safe_cd(); set_pname("calcsize"); dbopen(DBG_SUBDIR_CLIENT); config_init(CONFIG_INIT_CLIENT, NULL); dbprintf(_("version %s\n"), VERSION); /* drop root privileges; we'll regain them for the required operations */ #ifdef WANT_SETUID_CLIENT check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY); if (!set_root_privs(0)) { error(_("calcsize must be run setuid root")); } #else check_running_as(RUNNING_AS_CLIENT_LOGIN); #endif argc--, argv++; /* skip program name */ /* need at least program, amname, and directory name */ if(argc < 4) { error(_("Usage: %s config [DUMP|STAR|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*"), get_pname()); /*NOTREACHED*/ } dbprintf(_("config: %s\n"), *argv); if (!g_str_equal(*argv, "NOCONFIG")) { dbrename(*argv, DBG_SUBDIR_CLIENT); } argc--; argv++; /* parse backup program name */ if(g_str_equal(*argv, "DUMP")) { #if !defined(DUMP) && !defined(XFSDUMP) error("dump not available on this system"); /*NOTREACHED*/ #else add_file_name = add_file_name_dump; add_file = add_file_dump; final_size = final_size_dump; #endif } else if(g_str_equal(*argv, "GNUTAR")) { #ifndef GNUTAR error("gnutar not available on this system"); /*NOTREACHED*/ #else add_file_name = add_file_name_gnutar; add_file = add_file_gnutar; final_size = final_size_gnutar; use_gtar_excl++; #endif } else { add_file_name = add_file_name_unknown; add_file = add_file_unknown; final_size = final_size_unknown; } argc--, argv++; /* the amanda name can be different from the directory name */ if (argc > 0) { amname = *argv; qamname = quote_string(amname); argc--, argv++; } else { error("missing <name>"); /*NOTREACHED*/ } /* the toplevel directory name to search from */ if (argc > 0) { dirname = *argv; argc--, argv++; } else { error("missing <dir>"); /*NOTREACHED*/ } if ((argc > 1) && g_str_equal(*argv, "-X")) { argv++; if (!(use_gtar_excl || use_star_excl)) { error("exclusion specification not supported"); /*NOTREACHED*/ } filename = g_strdup(*argv); qfilename = quote_string(filename); if (access(filename, R_OK) != 0) { g_fprintf(stderr,"Cannot open exclude file %s\n", qfilename); use_gtar_excl = use_star_excl = 0; } else { exclude_sl = calc_load_file(filename); if (!exclude_sl) { g_fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename, strerror(errno)); use_gtar_excl = use_star_excl = 0; } } amfree(qfilename); amfree(filename); argc -= 2; argv++; } else { use_gtar_excl = use_star_excl = 0; } if ((argc > 1) && g_str_equal(*argv, "-I")) { argv++; filename = g_strdup(*argv); qfilename = quote_string(filename); if (access(filename, R_OK) != 0) { g_fprintf(stderr,"Cannot open include file %s\n", qfilename); use_gtar_excl = use_star_excl = 0; } else { include_sl = calc_load_file(filename); if (!include_sl) { g_fprintf(stderr,"Cannot open include file %s: %s\n", qfilename, strerror(errno)); use_gtar_excl = use_star_excl = 0; } } amfree(qfilename); amfree(filename); argc -= 2; argv++; } /* the dump levels to calculate sizes for */ ndumps = 0; while(argc >= 2) { if(ndumps < MAXDUMPS) { dumplevel[ndumps] = atoi(argv[0]); dumpdate [ndumps] = (time_t) atol(argv[1]); ndumps++; argc -= 2, argv += 2; } } if(argc) { error("leftover arg \"%s\", expected <level> and <date>", *argv); /*NOTREACHED*/ } if(is_empty_sl(include_sl)) { traverse_dirs(dirname,"."); } else { sle_t *an_include = include_sl->first; while(an_include != NULL) { /* char *adirname = stralloc2(dirname, an_include->name+1); traverse_dirs(adirname); amfree(adirname); */ traverse_dirs(dirname, an_include->name); an_include = an_include->next; } } for(i = 0; i < ndumps; i++) { amflock(1, "size"); dbprintf("calcsize: %s %d SIZE %lld\n", qamname, dumplevel[i], (long long)final_size(i, dirname)); g_fprintf(stderr, "%s %d SIZE %lld\n", qamname, dumplevel[i], (long long)final_size(i, dirname)); fflush(stderr); amfunlock(1, "size"); } amfree(qamname); return 0; #endif }
int start_amandates( char *amandates_file, int open_readwrite) { int rc, level = 0; long ldate = 0L; char *line; char *name; char *s; int ch; char *qname; if (amandates_file == NULL) { errno = 0; return 0; } /* clean up from previous invocation */ if(amdf != NULL) finish_amandates(); if(amandates_list != NULL) free_amandates(); amfree(g_amandates_file); /* initialize state */ updated = 0; readonly = !open_readwrite; amdf = NULL; amandates_list = NULL; g_amandates_file = g_strdup(amandates_file); /* open the file */ // if (access(amandates_file,F_OK)) // /* not yet existing */ // if ( (rc = open(amandates_file,(O_CREAT|O_RDWR),0644)) != -1 ) // /* open/create successfull */ // aclose(rc); if (open_readwrite) amdf = fopen(amandates_file, "r+"); else amdf = fopen(amandates_file, "r"); /* create it if we need to */ if (amdf == NULL) { if (errno == ENOENT) { amdf = fopen(amandates_file, "w+"); } } if (amdf == NULL) { // errno is set return 0; } if(open_readwrite) rc = amflock(fileno(amdf), amandates_file); else rc = amroflock(fileno(amdf), amandates_file); if(rc == -1) { error(_("could not lock %s: %s"), amandates_file, strerror(errno)); /*NOTREACHED*/ } for(; (line = agets(amdf)) != NULL; free(line)) { if (line[0] == '\0') continue; s = line; ch = *s++; skip_whitespace(s, ch); if(ch == '\0') { continue; /* no name field */ } qname = s - 1; skip_quoted_string(s, ch); s[-1] = '\0'; /* terminate the name */ name = unquote_string(qname); skip_whitespace(s, ch); if(ch == '\0' || sscanf(s - 1, "%d %ld", &level, &ldate) != 2) { amfree(name); continue; /* no more fields */ } if(level < 0 || level >= DUMP_LEVELS) { amfree(name); continue; } enter_record(name, level, (time_t) ldate); amfree(name); } if(ferror(amdf)) { error(_("reading %s: %s"), amandates_file, strerror(errno)); /*NOTREACHED*/ } updated = 0; /* reset updated flag */ return 1; }