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 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: calcsize.c,v 1.37 2006/03/29 15:45:08 martinea Exp $
29 * traverse directory tree to get backup size estimates
35 #define ROUND(n,x) ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
38 static unsigned long round_function(n, x)
41 unsigned long remainder = x % n;
48 #define ST_BLOCKS(s) ((((s).st_blocks * 512) <= (s).st_size) ? (s).st_blocks+1 : ((s).st_size / 512 + (((s).st_size % 512) ? 1 : 0)))
50 #define FILETYPES (S_IFREG|S_IFLNK|S_IFDIR)
52 typedef struct name_s {
67 } dumpstats[MAXDUMPS];
69 time_t dumpdate[MAXDUMPS];
70 int dumplevel[MAXDUMPS];
73 void (*add_file_name) P((int, char *));
74 void (*add_file) P((int, struct stat *));
75 long (*final_size) P((int, char *));
78 int main P((int, char **));
79 void traverse_dirs P((char *, char *));
82 void add_file_name_dump P((int, char *));
83 void add_file_dump P((int, struct stat *));
84 long final_size_dump P((int, char *));
86 void add_file_name_gnutar P((int, char *));
87 void add_file_gnutar P((int, struct stat *));
88 long final_size_gnutar P((int, char *));
90 void add_file_name_unknown P((int, char *));
91 void add_file_unknown P((int, struct stat *));
92 long final_size_unknown P((int, char *));
94 sl_t *calc_load_file P((char *filename));
95 int calc_check_exclude P((char *filename));
97 int use_gtar_excl = 0;
98 sl_t *include_sl=NULL, *exclude_sl=NULL;
105 /* standalone test to ckeck wether the calculated file size is ok */
108 unsigned long dump_total=0, gtar_total=0;
114 set_pname("calcsize");
116 /* Don't die when child closes pipe */
117 signal(SIGPIPE, SIG_IGN);
120 fprintf(stderr,"Usage: %s file[s]\n",argv[0]);
123 for(i=1; i<argc; i++) {
124 if(lstat(argv[i], &finfo) == -1) {
125 fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
128 printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
129 printf(": blocks=%lu\n", (unsigned long)ST_BLOCKS(finfo));
130 dump_total += (ST_BLOCKS(finfo) + 1)/2 + 1;
131 gtar_total += ROUND(4,(ST_BLOCKS(finfo) + 1));
133 printf(" gtar dump\n");
134 printf("total %-9lu %-9lu\n",gtar_total,dump_total);
138 char *dirname=NULL, *amname=NULL, *filename=NULL;
139 unsigned long malloc_hist_1, malloc_size_1;
140 unsigned long malloc_hist_2, malloc_size_2;
145 set_pname("calcsize");
147 malloc_size_1 = malloc_inuse(&malloc_hist_1);
150 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
153 argc--, argv++; /* skip program name */
155 /* need at least program, amname, and directory name */
158 error("Usage: %s [DUMP|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*",
163 /* parse backup program name */
165 if(strcmp(*argv, "DUMP") == 0) {
166 #if !defined(DUMP) && !defined(XFSDUMP)
167 error("dump not available on this system");
170 add_file_name = add_file_name_dump;
171 add_file = add_file_dump;
172 final_size = final_size_dump;
175 else if(strcmp(*argv, "GNUTAR") == 0) {
177 error("gnutar not available on this system");
180 add_file_name = add_file_name_gnutar;
181 add_file = add_file_gnutar;
182 final_size = final_size_gnutar;
187 add_file_name = add_file_name_unknown;
188 add_file = add_file_unknown;
189 final_size = final_size_unknown;
193 /* the amanda name can be different from the directory name */
199 error("missing <name>");
201 /* the toplevel directory name to search from */
206 error("missing <dir>");
208 if ((argc > 1) && strcmp(*argv,"-X") == 0) {
211 if (!use_gtar_excl) {
212 error("exclusion specification not supported");
216 filename = stralloc(*argv);
217 if (access(filename, R_OK) != 0) {
218 fprintf(stderr,"Cannot open exclude file %s\n",filename);
221 exclude_sl = calc_load_file(filename);
229 if ((argc > 1) && strcmp(*argv,"-I") == 0) {
232 filename = stralloc(*argv);
233 if (access(filename, R_OK) != 0) {
234 fprintf(stderr,"Cannot open include file %s\n",filename);
237 include_sl = calc_load_file(filename);
244 /* the dump levels to calculate sizes for */
248 if(ndumps < MAXDUMPS) {
249 dumplevel[ndumps] = atoi(argv[0]);
250 dumpdate [ndumps] = (time_t) atol(argv[1]);
252 argc -= 2, argv += 2;
257 error("leftover arg \"%s\", expected <level> and <date>", *argv);
259 if(is_empty_sl(include_sl)) {
260 traverse_dirs(dirname,".");
263 sle_t *an_include = include_sl->first;
264 while(an_include != NULL) {
266 char *adirname = stralloc2(dirname, an_include->name+1);
267 traverse_dirs(adirname);
270 traverse_dirs(dirname, an_include->name);
271 an_include = an_include->next;
274 for(i = 0; i < ndumps; i++) {
278 lseek(1, (off_t)0, SEEK_END);
280 fprintf(stderr, "%s %d SIZE %ld\n",
281 amname, dumplevel[i], final_size(i, dirname));
284 amfunlock(1, "size");
287 malloc_size_2 = malloc_inuse(&malloc_hist_2);
289 if(malloc_size_1 != malloc_size_2) {
290 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
298 * =========================================================================
301 #if !defined(HAVE_BASENAME) && defined(BUILTIN_EXCLUDE_SUPPORT)
302 static char *basename P((char *));
304 static char *basename(file)
309 if ( (cp = strrchr(file,'/')) )
315 void push_name P((char *str));
316 char *pop_name P((void));
318 void traverse_dirs(parent_dir, include)
325 char *dirname, *newname = NULL;
326 char *newbase = NULL;
327 dev_t parent_dev = 0;
331 int has_exclude = !is_empty_sl(exclude_sl) && use_gtar_excl;
334 if(parent_dir == NULL || include == NULL) return;
336 aparent = vstralloc(parent_dir, "/", include, NULL);
338 if(stat(parent_dir, &finfo) != -1)
339 parent_dev = finfo.st_dev;
341 parent_len = strlen(parent_dir);
345 for(dirname = pop_name(); dirname; free(dirname), dirname = pop_name()) {
346 if(has_exclude && calc_check_exclude(dirname+parent_len+1)) {
349 if((d = opendir(dirname)) == NULL) {
355 if(l > 0 && dirname[l - 1] != '/') {
356 newbase = newstralloc2(newbase, dirname, "/");
358 newbase = newstralloc(newbase, dirname);
361 while((f = readdir(d)) != NULL) {
365 if(is_dot_or_dotdot(f->d_name)) {
369 newname = newstralloc2(newname, newbase, f->d_name);
370 if(lstat(newname, &finfo) == -1) {
371 fprintf(stderr, "%s/%s: %s\n",
372 dirname, f->d_name, strerror(errno));
376 if(finfo.st_dev != parent_dev)
380 is_symlink = ((finfo.st_mode & S_IFMT) == S_IFLNK);
382 is_dir = ((finfo.st_mode & S_IFMT) == S_IFDIR);
383 is_file = ((finfo.st_mode & S_IFMT) == S_IFREG);
385 if (!(is_file || is_dir || is_symlink)) {
390 int is_excluded = -1;
391 for(i = 0; i < ndumps; i++) {
392 add_file_name(i, newname);
393 if(is_file && finfo.st_ctime >= dumpdate[i]) {
396 if(is_excluded == -1)
398 calc_check_exclude(newname+parent_len+1);
399 if(is_excluded == 1) {
408 if(has_exclude && calc_check_exclude(newname+parent_len+1))
418 if(closedir(d) == -1)
432 newp = alloc(sizeof(*newp));
433 newp->str = stralloc(str);
435 newp->next = name_stack;
441 Name *newp = name_stack;
444 if(!newp) return NULL;
446 name_stack = newp->next;
454 * =========================================================================
455 * Backup size calculations for DUMP program
457 * Given the system-dependent nature of dump, it's impossible to pin this
458 * down accurately. Luckily, that's not necessary.
460 * Dump rounds each file up to TP_BSIZE bytes, which is 1k in the BSD dump,
461 * others are unknown. In addition, dump stores three bitmaps at the
462 * beginning of the dump: a used inode map, a dumped dir map, and a dumped
463 * inode map. These are sized by the number of inodes in the filesystem.
465 * We don't take into account the complexities of BSD dump's indirect block
466 * requirements for files with holes, nor the dumping of directories that
467 * are not themselves modified.
469 void add_file_name_dump(level, name)
476 void add_file_dump(level, sp)
480 /* keep the size in kbytes, rounded up, plus a 1k header block */
481 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
482 dumpstats[level].total_size += (ST_BLOCKS(*sp) + 1)/2 + 1;
485 long final_size_dump(level, topdir)
489 generic_fs_stats_t stats;
493 /* calculate the map sizes */
495 s = stralloc2(topdir, "/.");
496 if(get_fs_stats(s, &stats) == -1) {
497 error("statfs %s: %s", s, strerror(errno));
501 mapsize = (stats.files + 7) / 8; /* in bytes */
502 mapsize = (mapsize + 1023) / 1024; /* in kbytes */
504 /* the dump contains three maps plus the files */
506 return 3*mapsize + dumpstats[level].total_size;
510 * =========================================================================
511 * Backup size calculations for GNUTAR program
513 * Gnutar's basic blocksize is 512 bytes. Each file is rounded up to that
514 * size, plus one header block. Gnutar stores directories' file lists in
515 * incremental dumps - we'll pick up size of the modified dirs here. These
516 * will be larger than a simple filelist of their contents, but that's ok.
518 * As with DUMP, we only need a reasonable estimate, not an exact figure.
520 void add_file_name_gnutar(level, name)
524 /* dumpstats[level].total_size_name += strlen(name) + 64;*/
525 dumpstats[level].total_size += 1;
528 void add_file_gnutar(level, sp)
532 /* the header takes one additional block */
533 dumpstats[level].total_size += ST_BLOCKS(*sp);
536 long final_size_gnutar(level, topdir)
540 /* divide by two to get kbytes, rounded up */
541 /* + 4 blocks for security */
542 return (dumpstats[level].total_size + 5 + (dumpstats[level].total_size_name/512)) / 2;
546 * =========================================================================
547 * Backup size calculations for unknown backup programs.
549 * Here we'll just add up the file sizes and output that.
552 void add_file_name_unknown(level, name)
559 void add_file_unknown(level, sp)
563 /* just add up the block counts */
564 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
565 dumpstats[level].total_size += ST_BLOCKS(*sp);
568 long final_size_unknown(level, topdir)
572 /* divide by two to get kbytes, rounded up */
573 return (dumpstats[level].total_size + 1) / 2;
577 * =========================================================================
579 sl_t *calc_load_file(filename)
584 sl_t *sl_list = new_sl();
586 FILE *file = fopen(filename, "r");
588 while(fgets(pattern, 1025, file)) {
589 if(strlen(pattern)>0 && pattern[strlen(pattern)-1] == '\n')
590 pattern[strlen(pattern)-1] = '\0';
591 sl_list = append_sl(sl_list, pattern);
598 int calc_check_exclude(filename)
602 if(is_empty_sl(exclude_sl)) return 0;
604 an_exclude=exclude_sl->first;
605 while(an_exclude != NULL) {
606 if(match_tar(an_exclude->name, filename)) {
609 an_exclude=an_exclude->next;