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.44 2006/07/25 18:27:56 martinea Exp $
29 * traverse directory tree to get backup size estimates
31 * argv[0] is the calcsize program name
32 * argv[1] is the config name or NOCONFIG
41 #define ROUND(n,x) ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
49 unsigned long remainder = x % n;
56 #define ST_BLOCKS(s) \
57 (((((off_t)(s).st_blocks * (off_t)512) <= (s).st_size)) ? \
58 ((off_t)(s).st_blocks + (off_t)1) : \
59 ((s).st_size / (off_t)512 + \
60 (off_t)((((s).st_size % (off_t)512) != (off_t)0) ? \
61 (off_t)1 : (off_t)0)))
63 #define FILETYPES (S_IFREG|S_IFLNK|S_IFDIR)
65 typedef struct name_s {
79 off_t total_size_name;
80 } dumpstats[MAXDUMPS];
82 time_t dumpdate[MAXDUMPS];
83 int dumplevel[MAXDUMPS];
86 void (*add_file_name)(int, char *);
87 void (*add_file)(int, struct stat *);
88 off_t (*final_size)(int, char *);
91 int main(int, char **);
92 void traverse_dirs(char *, char *);
95 void add_file_name_dump(int, char *);
96 void add_file_dump(int, struct stat *);
97 off_t final_size_dump(int, char *);
99 void add_file_name_star(int, char *);
100 void add_file_star(int, struct stat *);
101 off_t final_size_star(int, char *);
103 void add_file_name_gnutar(int, char *);
104 void add_file_gnutar(int, struct stat *);
105 off_t final_size_gnutar(int, char *);
107 void add_file_name_unknown(int, char *);
108 void add_file_unknown(int, struct stat *);
109 off_t final_size_unknown(int, char *);
111 am_sl_t *calc_load_file(char *filename);
112 int calc_check_exclude(char *filename);
114 int use_star_excl = 0;
115 int use_gtar_excl = 0;
116 am_sl_t *include_sl=NULL, *exclude_sl=NULL;
124 /* standalone test to ckeck wether the calculated file size is ok */
127 off_t dump_total = (off_t)0;
128 off_t gtar_total = (off_t)0;
133 * Configure program for internationalization:
134 * 1) Only set the message locale for now.
135 * 2) Set textdomain for all amanda related programs to "amanda"
136 * We don't want to be forced to support dozens of message catalogs.
138 setlocale(LC_MESSAGES, "C");
139 textdomain("amanda");
143 set_pname("calcsize");
146 config_init(CONFIG_INIT_CLIENT, NULL);
148 /* Don't die when child closes pipe */
149 signal(SIGPIPE, SIG_IGN);
152 g_fprintf(stderr,_("Usage: %s file[s]\n"),argv[0]);
155 for(i=1; i<argc; i++) {
156 if(lstat(argv[i], &finfo) == -1) {
157 g_fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
160 g_printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
161 g_printf(": blocks=%llu\n", ST_BLOCKS(finfo));
162 dump_total += (ST_BLOCKS(finfo) + (off_t)1) / (off_t)2 + (off_t)1;
163 gtar_total += ROUND(4,(ST_BLOCKS(finfo) + (off_t)1));
165 g_printf(" gtar dump\n");
166 g_printf("total %-9lu %-9lu\n",gtar_total,dump_total);
171 char *amname=NULL, *qamname=NULL;
172 char *filename=NULL, *qfilename = NULL;
174 if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
175 printf("calcsize-%s\n", VERSION);
182 set_pname("calcsize");
184 dbopen(DBG_SUBDIR_CLIENT);
185 config_init(CONFIG_INIT_CLIENT, NULL);
186 dbprintf(_("version %s\n"), VERSION);
188 /* drop root privileges; we'll regain them for the required operations */
189 #ifdef WANT_SETUID_CLIENT
190 check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
191 if (!set_root_privs(0)) {
192 error(_("calcsize must be run setuid root"));
195 check_running_as(RUNNING_AS_CLIENT_LOGIN);
198 argc--, argv++; /* skip program name */
200 /* need at least program, amname, and directory name */
203 error(_("Usage: %s config [DUMP|STAR|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*"),
208 dbprintf(_("config: %s\n"), *argv);
209 if (strcmp(*argv, "NOCONFIG") != 0) {
210 dbrename(*argv, DBG_SUBDIR_CLIENT);
215 /* parse backup program name */
217 if(strcmp(*argv, "DUMP") == 0) {
218 #if !defined(DUMP) && !defined(XFSDUMP)
219 error("dump not available on this system");
222 add_file_name = add_file_name_dump;
223 add_file = add_file_dump;
224 final_size = final_size_dump;
227 else if(strcmp(*argv, "GNUTAR") == 0) {
229 error("gnutar not available on this system");
232 add_file_name = add_file_name_gnutar;
233 add_file = add_file_gnutar;
234 final_size = final_size_gnutar;
239 add_file_name = add_file_name_unknown;
240 add_file = add_file_unknown;
241 final_size = final_size_unknown;
245 /* the amanda name can be different from the directory name */
249 qamname = quote_string(amname);
252 error("missing <name>");
256 /* the toplevel directory name to search from */
261 error("missing <dir>");
265 if ((argc > 1) && strcmp(*argv,"-X") == 0) {
268 if (!(use_gtar_excl || use_star_excl)) {
269 error("exclusion specification not supported");
273 filename = stralloc(*argv);
274 qfilename = quote_string(filename);
275 if (access(filename, R_OK) != 0) {
276 g_fprintf(stderr,"Cannot open exclude file %s\n", qfilename);
277 use_gtar_excl = use_star_excl = 0;
279 exclude_sl = calc_load_file(filename);
281 g_fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename,
283 use_gtar_excl = use_star_excl = 0;
291 use_gtar_excl = use_star_excl = 0;
294 if ((argc > 1) && strcmp(*argv,"-I") == 0) {
297 filename = stralloc(*argv);
298 qfilename = quote_string(filename);
299 if (access(filename, R_OK) != 0) {
300 g_fprintf(stderr,"Cannot open include file %s\n", qfilename);
301 use_gtar_excl = use_star_excl = 0;
303 include_sl = calc_load_file(filename);
305 g_fprintf(stderr,"Cannot open include file %s: %s\n", qfilename,
307 use_gtar_excl = use_star_excl = 0;
316 /* the dump levels to calculate sizes for */
320 if(ndumps < MAXDUMPS) {
321 dumplevel[ndumps] = atoi(argv[0]);
322 dumpdate [ndumps] = (time_t) atol(argv[1]);
324 argc -= 2, argv += 2;
329 error("leftover arg \"%s\", expected <level> and <date>", *argv);
333 if(is_empty_sl(include_sl)) {
334 traverse_dirs(dirname,".");
337 sle_t *an_include = include_sl->first;
338 while(an_include != NULL) {
340 char *adirname = stralloc2(dirname, an_include->name+1);
341 traverse_dirs(adirname);
344 traverse_dirs(dirname, an_include->name);
345 an_include = an_include->next;
348 for(i = 0; i < ndumps; i++) {
352 dbprintf("calcsize: %s %d SIZE %lld\n",
353 qamname, dumplevel[i],
354 (long long)final_size(i, dirname));
355 g_fprintf(stderr, "%s %d SIZE %lld\n",
356 qamname, dumplevel[i],
357 (long long)final_size(i, dirname));
360 amfunlock(1, "size");
369 * =========================================================================
372 #if !defined(HAVE_BASENAME) && defined(BUILTIN_EXCLUDE_SUPPORT)
380 if ( (cp = strrchr(file,'/')) )
386 void push_name(char *str);
387 char *pop_name(void);
397 char *dirname, *newname = NULL;
398 char *newbase = NULL;
399 dev_t parent_dev = (dev_t)0;
406 if(parent_dir == NULL || include == NULL)
409 has_exclude = !is_empty_sl(exclude_sl) && (use_gtar_excl || use_star_excl);
410 aparent = vstralloc(parent_dir, "/", include, NULL);
412 /* We (may) need root privs for the *stat() calls here. */
414 if(stat(parent_dir, &finfo) != -1)
415 parent_dev = finfo.st_dev;
417 parent_len = strlen(parent_dir);
421 for(; (dirname = pop_name()) != NULL; free(dirname)) {
422 if(has_exclude && calc_check_exclude(dirname+parent_len+1)) {
425 if((d = opendir(dirname)) == NULL) {
431 if(l > 0 && dirname[l - 1] != '/') {
432 newbase = newstralloc2(newbase, dirname, "/");
434 newbase = newstralloc(newbase, dirname);
437 while((f = readdir(d)) != NULL) {
441 if(is_dot_or_dotdot(f->d_name)) {
445 newname = newstralloc2(newname, newbase, f->d_name);
446 if(lstat(newname, &finfo) == -1) {
447 g_fprintf(stderr, "%s/%s: %s\n",
448 dirname, f->d_name, strerror(errno));
452 if(finfo.st_dev != parent_dev)
456 is_symlink = ((finfo.st_mode & S_IFMT) == S_IFLNK);
458 is_dir = ((finfo.st_mode & S_IFMT) == S_IFDIR);
459 is_file = ((finfo.st_mode & S_IFMT) == S_IFREG);
461 if (!(is_file || is_dir || is_symlink)) {
466 int is_excluded = -1;
467 for(i = 0; i < ndumps; i++) {
468 add_file_name(i, newname);
469 if(is_file && (time_t)finfo.st_ctime >= dumpdate[i]) {
472 if(is_excluded == -1)
474 calc_check_exclude(newname+parent_len+1);
475 if(is_excluded == 1) {
484 if(has_exclude && calc_check_exclude(newname+parent_len+1))
494 if(closedir(d) == -1)
499 /* drop root privs -- we're done with the permission-sensitive calls */
513 newp = alloc(SIZEOF(*newp));
514 newp->str = stralloc(str);
516 newp->next = name_stack;
523 Name *newp = name_stack;
526 if(!newp) return NULL;
528 name_stack = newp->next;
536 * =========================================================================
537 * Backup size calculations for DUMP program
539 * Given the system-dependent nature of dump, it's impossible to pin this
540 * down accurately. Luckily, that's not necessary.
542 * Dump rounds each file up to TP_BSIZE bytes, which is 1k in the BSD dump,
543 * others are unknown. In addition, dump stores three bitmaps at the
544 * beginning of the dump: a used inode map, a dumped dir map, and a dumped
545 * inode map. These are sized by the number of inodes in the filesystem.
547 * We don't take into account the complexities of BSD dump's indirect block
548 * requirements for files with holes, nor the dumping of directories that
549 * are not themselves modified.
556 (void)level; /* Quiet unused parameter warning */
557 (void)name; /* Quiet unused parameter warning */
567 /* keep the size in kbytes, rounded up, plus a 1k header block */
568 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
569 dumpstats[level].total_size +=
570 (ST_BLOCKS(*sp) + (off_t)1) / (off_t)2 + (off_t)1;
578 struct fs_usage fsusage;
582 /* calculate the map sizes */
584 s = stralloc2(topdir, "/.");
585 if(get_fs_usage(s, NULL, &fsusage) == -1) {
586 error("statfs %s: %s", s, strerror(errno));
591 mapsize = (fsusage.fsu_files + (off_t)7) / (off_t)8; /* in bytes */
592 mapsize = (mapsize + (off_t)1023) / (off_t)1024; /* in kbytes */
594 /* the dump contains three maps plus the files */
596 return (mapsize * (off_t)3) + dumpstats[level].total_size;
600 * =========================================================================
601 * Backup size calculations for GNUTAR program
603 * Gnutar's basic blocksize is 512 bytes. Each file is rounded up to that
604 * size, plus one header block. Gnutar stores directories' file lists in
605 * incremental dumps - we'll pick up size of the modified dirs here. These
606 * will be larger than a simple filelist of their contents, but that's ok.
608 * As with DUMP, we only need a reasonable estimate, not an exact figure.
611 add_file_name_gnutar(
615 (void)name; /* Quiet unused parameter warning */
617 /* dumpstats[level].total_size_name += strlen(name) + 64;*/
618 dumpstats[level].total_size += (off_t)1;
626 /* the header takes one additional block */
627 dumpstats[level].total_size += ST_BLOCKS(*sp);
635 (void)topdir; /* Quiet unused parameter warning */
637 /* divide by two to get kbytes, rounded up */
638 /* + 4 blocks for security */
639 return (dumpstats[level].total_size + (off_t)5 +
640 (dumpstats[level].total_size_name/(off_t)512)) / (off_t)2;
644 * =========================================================================
645 * Backup size calculations for unknown backup programs.
647 * Here we'll just add up the file sizes and output that.
651 add_file_name_unknown(
655 (void)level; /* Quiet unused parameter warning */
656 (void)name; /* Quiet unused parameter warning */
666 /* just add up the block counts */
667 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
668 dumpstats[level].total_size += ST_BLOCKS(*sp);
676 (void)topdir; /* Quiet unused parameter warning */
678 /* divide by two to get kbytes, rounded up */
679 return (dumpstats[level].total_size + (off_t)1) / (off_t)2;
683 * =========================================================================
693 FILE *file = fopen(filename, "r");
701 while(fgets(pattern, 1025, file)) {
702 if(strlen(pattern)>0 && pattern[strlen(pattern)-1] == '\n')
703 pattern[strlen(pattern)-1] = '\0';
704 sl_list = append_sl(sl_list, pattern);
716 if(is_empty_sl(exclude_sl)) return 0;
718 an_exclude=exclude_sl->first;
719 while(an_exclude != NULL) {
720 if(match_tar(an_exclude->name, filename)) {
723 an_exclude=an_exclude->next;