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
40 #define ROUND(n,x) ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
48 unsigned long remainder = x % n;
55 #define ST_BLOCKS(s) \
56 (((((off_t)(s).st_blocks * (off_t)512) <= (s).st_size)) ? \
57 ((off_t)(s).st_blocks + (off_t)1) : \
58 ((s).st_size / (off_t)512 + \
59 (off_t)((((s).st_size % (off_t)512) != (off_t)0) ? \
60 (off_t)1 : (off_t)0)))
62 #define FILETYPES (S_IFREG|S_IFLNK|S_IFDIR)
64 typedef struct name_s {
78 off_t total_size_name;
79 } dumpstats[MAXDUMPS];
81 time_t dumpdate[MAXDUMPS];
82 int dumplevel[MAXDUMPS];
85 void (*add_file_name)(int, char *);
86 void (*add_file)(int, struct stat *);
87 off_t (*final_size)(int, char *);
90 int main(int, char **);
91 void traverse_dirs(char *, char *);
94 void add_file_name_dump(int, char *);
95 void add_file_dump(int, struct stat *);
96 off_t final_size_dump(int, char *);
98 void add_file_name_star(int, char *);
99 void add_file_star(int, struct stat *);
100 off_t final_size_star(int, char *);
102 void add_file_name_gnutar(int, char *);
103 void add_file_gnutar(int, struct stat *);
104 off_t final_size_gnutar(int, char *);
106 void add_file_name_unknown(int, char *);
107 void add_file_unknown(int, struct stat *);
108 off_t final_size_unknown(int, char *);
110 sl_t *calc_load_file(char *filename);
111 int calc_check_exclude(char *filename);
113 int use_star_excl = 0;
114 int use_gtar_excl = 0;
115 sl_t *include_sl=NULL, *exclude_sl=NULL;
123 /* standalone test to ckeck wether the calculated file size is ok */
126 off_t dump_total = (off_t)0;
127 off_t gtar_total = (off_t)0;
132 * Configure program for internationalization:
133 * 1) Only set the message locale for now.
134 * 2) Set textdomain for all amanda related programs to "amanda"
135 * We don't want to be forced to support dozens of message catalogs.
137 setlocale(LC_MESSAGES, "C");
138 textdomain("amanda");
142 set_pname("calcsize");
146 /* Don't die when child closes pipe */
147 signal(SIGPIPE, SIG_IGN);
150 g_fprintf(stderr,_("Usage: %s file[s]\n"),argv[0]);
153 for(i=1; i<argc; i++) {
154 if(lstat(argv[i], &finfo) == -1) {
155 g_fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
158 g_printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
159 g_printf(": blocks=%llu\n", ST_BLOCKS(finfo));
160 dump_total += (ST_BLOCKS(finfo) + (off_t)1) / (off_t)2 + (off_t)1;
161 gtar_total += ROUND(4,(ST_BLOCKS(finfo) + (off_t)1));
163 g_printf(" gtar dump\n");
164 g_printf("total %-9lu %-9lu\n",gtar_total,dump_total);
169 char *amname=NULL, *qamname=NULL;
170 char *filename=NULL, *qfilename = NULL;
172 /* drop root privileges; we'll regain them for the required operations */
173 #ifdef WANT_SETUID_CLIENT
174 if (!set_root_privs(0)) {
175 error(_("calcsize must be run setuid root"));
182 set_pname("calcsize");
184 dbopen(DBG_SUBDIR_CLIENT);
185 dbprintf(_("version %s\n"), version());
188 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
191 argc--, argv++; /* skip program name */
193 /* need at least program, amname, and directory name */
196 error(_("Usage: %s config [DUMP|STAR|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*"),
201 dbprintf(_("config: %s\n"), *argv);
202 if (strcmp(*argv, "NOCONFIG") != 0) {
203 dbrename(*argv, DBG_SUBDIR_CLIENT);
208 check_running_as(RUNNING_AS_CLIENT_LOGIN);
210 /* parse backup program name */
212 if(strcmp(*argv, "DUMP") == 0) {
213 #if !defined(DUMP) && !defined(XFSDUMP)
214 error("dump not available on this system");
217 add_file_name = add_file_name_dump;
218 add_file = add_file_dump;
219 final_size = final_size_dump;
222 else if(strcmp(*argv, "GNUTAR") == 0) {
224 error("gnutar not available on this system");
227 add_file_name = add_file_name_gnutar;
228 add_file = add_file_gnutar;
229 final_size = final_size_gnutar;
234 add_file_name = add_file_name_unknown;
235 add_file = add_file_unknown;
236 final_size = final_size_unknown;
240 /* the amanda name can be different from the directory name */
244 qamname = quote_string(amname);
247 error("missing <name>");
251 /* the toplevel directory name to search from */
256 error("missing <dir>");
260 if ((argc > 1) && strcmp(*argv,"-X") == 0) {
263 if (!(use_gtar_excl || use_star_excl)) {
264 error("exclusion specification not supported");
268 filename = stralloc(*argv);
269 qfilename = quote_string(filename);
270 if (access(filename, R_OK) != 0) {
271 g_fprintf(stderr,"Cannot open exclude file %s\n", qfilename);
272 use_gtar_excl = use_star_excl = 0;
274 exclude_sl = calc_load_file(filename);
276 g_fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename,
278 use_gtar_excl = use_star_excl = 0;
286 use_gtar_excl = use_star_excl = 0;
289 if ((argc > 1) && strcmp(*argv,"-I") == 0) {
292 filename = stralloc(*argv);
293 qfilename = quote_string(filename);
294 if (access(filename, R_OK) != 0) {
295 g_fprintf(stderr,"Cannot open include file %s\n", qfilename);
296 use_gtar_excl = use_star_excl = 0;
298 include_sl = calc_load_file(filename);
300 g_fprintf(stderr,"Cannot open include file %s: %s\n", qfilename,
302 use_gtar_excl = use_star_excl = 0;
311 /* the dump levels to calculate sizes for */
315 if(ndumps < MAXDUMPS) {
316 dumplevel[ndumps] = atoi(argv[0]);
317 dumpdate [ndumps] = (time_t) atol(argv[1]);
319 argc -= 2, argv += 2;
324 error("leftover arg \"%s\", expected <level> and <date>", *argv);
328 if(is_empty_sl(include_sl)) {
329 traverse_dirs(dirname,".");
332 sle_t *an_include = include_sl->first;
333 while(an_include != NULL) {
335 char *adirname = stralloc2(dirname, an_include->name+1);
336 traverse_dirs(adirname);
339 traverse_dirs(dirname, an_include->name);
340 an_include = an_include->next;
343 for(i = 0; i < ndumps; i++) {
347 dbprintf("calcsize: %s %d SIZE %lld\n",
348 qamname, dumplevel[i],
349 (long long)final_size(i, dirname));
350 g_fprintf(stderr, "%s %d SIZE %lld\n",
351 qamname, dumplevel[i],
352 (long long)final_size(i, dirname));
355 amfunlock(1, "size");
364 * =========================================================================
367 #if !defined(HAVE_BASENAME) && defined(BUILTIN_EXCLUDE_SUPPORT)
375 if ( (cp = strrchr(file,'/')) )
381 void push_name(char *str);
382 char *pop_name(void);
392 char *dirname, *newname = NULL;
393 char *newbase = NULL;
394 dev_t parent_dev = (dev_t)0;
401 if(parent_dir == NULL || include == NULL)
404 has_exclude = !is_empty_sl(exclude_sl) && (use_gtar_excl || use_star_excl);
405 aparent = vstralloc(parent_dir, "/", include, NULL);
407 /* We (may) need root privs for the *stat() calls here. */
409 if(stat(parent_dir, &finfo) != -1)
410 parent_dev = finfo.st_dev;
412 parent_len = strlen(parent_dir);
416 for(; (dirname = pop_name()) != NULL; free(dirname)) {
417 if(has_exclude && calc_check_exclude(dirname+parent_len+1)) {
420 if((d = opendir(dirname)) == NULL) {
426 if(l > 0 && dirname[l - 1] != '/') {
427 newbase = newstralloc2(newbase, dirname, "/");
429 newbase = newstralloc(newbase, dirname);
432 while((f = readdir(d)) != NULL) {
436 if(is_dot_or_dotdot(f->d_name)) {
440 newname = newstralloc2(newname, newbase, f->d_name);
441 if(lstat(newname, &finfo) == -1) {
442 g_fprintf(stderr, "%s/%s: %s\n",
443 dirname, f->d_name, strerror(errno));
447 if(finfo.st_dev != parent_dev)
451 is_symlink = ((finfo.st_mode & S_IFMT) == S_IFLNK);
453 is_dir = ((finfo.st_mode & S_IFMT) == S_IFDIR);
454 is_file = ((finfo.st_mode & S_IFMT) == S_IFREG);
456 if (!(is_file || is_dir || is_symlink)) {
461 int is_excluded = -1;
462 for(i = 0; i < ndumps; i++) {
463 add_file_name(i, newname);
464 if(is_file && (time_t)finfo.st_ctime >= dumpdate[i]) {
467 if(is_excluded == -1)
469 calc_check_exclude(newname+parent_len+1);
470 if(is_excluded == 1) {
479 if(has_exclude && calc_check_exclude(newname+parent_len+1))
489 if(closedir(d) == -1)
494 /* drop root privs -- we're done with the permission-sensitive calls */
508 newp = alloc(SIZEOF(*newp));
509 newp->str = stralloc(str);
511 newp->next = name_stack;
518 Name *newp = name_stack;
521 if(!newp) return NULL;
523 name_stack = newp->next;
531 * =========================================================================
532 * Backup size calculations for DUMP program
534 * Given the system-dependent nature of dump, it's impossible to pin this
535 * down accurately. Luckily, that's not necessary.
537 * Dump rounds each file up to TP_BSIZE bytes, which is 1k in the BSD dump,
538 * others are unknown. In addition, dump stores three bitmaps at the
539 * beginning of the dump: a used inode map, a dumped dir map, and a dumped
540 * inode map. These are sized by the number of inodes in the filesystem.
542 * We don't take into account the complexities of BSD dump's indirect block
543 * requirements for files with holes, nor the dumping of directories that
544 * are not themselves modified.
551 (void)level; /* Quiet unused parameter warning */
552 (void)name; /* Quiet unused parameter warning */
562 /* keep the size in kbytes, rounded up, plus a 1k header block */
563 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
564 dumpstats[level].total_size +=
565 (ST_BLOCKS(*sp) + (off_t)1) / (off_t)2 + (off_t)1;
573 struct fs_usage fsusage;
577 /* calculate the map sizes */
579 s = stralloc2(topdir, "/.");
580 if(get_fs_usage(s, NULL, &fsusage) == -1) {
581 error("statfs %s: %s", s, strerror(errno));
586 mapsize = (fsusage.fsu_files + (off_t)7) / (off_t)8; /* in bytes */
587 mapsize = (mapsize + (off_t)1023) / (off_t)1024; /* in kbytes */
589 /* the dump contains three maps plus the files */
591 return (mapsize * (off_t)3) + dumpstats[level].total_size;
595 * =========================================================================
596 * Backup size calculations for GNUTAR program
598 * Gnutar's basic blocksize is 512 bytes. Each file is rounded up to that
599 * size, plus one header block. Gnutar stores directories' file lists in
600 * incremental dumps - we'll pick up size of the modified dirs here. These
601 * will be larger than a simple filelist of their contents, but that's ok.
603 * As with DUMP, we only need a reasonable estimate, not an exact figure.
606 add_file_name_gnutar(
610 (void)name; /* Quiet unused parameter warning */
612 /* dumpstats[level].total_size_name += strlen(name) + 64;*/
613 dumpstats[level].total_size += (off_t)1;
621 /* the header takes one additional block */
622 dumpstats[level].total_size += ST_BLOCKS(*sp);
630 (void)topdir; /* Quiet unused parameter warning */
632 /* divide by two to get kbytes, rounded up */
633 /* + 4 blocks for security */
634 return (dumpstats[level].total_size + (off_t)5 +
635 (dumpstats[level].total_size_name/(off_t)512)) / (off_t)2;
639 * =========================================================================
640 * Backup size calculations for unknown backup programs.
642 * Here we'll just add up the file sizes and output that.
646 add_file_name_unknown(
650 (void)level; /* Quiet unused parameter warning */
651 (void)name; /* Quiet unused parameter warning */
661 /* just add up the block counts */
662 if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
663 dumpstats[level].total_size += ST_BLOCKS(*sp);
671 (void)topdir; /* Quiet unused parameter warning */
673 /* divide by two to get kbytes, rounded up */
674 return (dumpstats[level].total_size + (off_t)1) / (off_t)2;
678 * =========================================================================
688 FILE *file = fopen(filename, "r");
696 while(fgets(pattern, 1025, file)) {
697 if(strlen(pattern)>0 && pattern[strlen(pattern)-1] == '\n')
698 pattern[strlen(pattern)-1] = '\0';
699 sl_list = append_sl(sl_list, pattern);
711 if(is_empty_sl(exclude_sl)) return 0;
713 an_exclude=exclude_sl->first;
714 while(an_exclude != NULL) {
715 if(match_tar(an_exclude->name, filename)) {
718 an_exclude=an_exclude->next;