Imported Upstream version 2.4.5p1
[debian/amanda] / client-src / calcsize.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /* 
27  * $Id: calcsize.c,v 1.24.2.3.6.1.2.5 2005/09/20 21:31:52 jrjackson Exp $
28  *
29  * traverse directory tree to get backup size estimates
30  */
31 #include "amanda.h"
32 #include "statfs.h"
33 #include "sl.h"
34
35 #define ROUND(n,x)      ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
36
37 /*
38 static unsigned long round_function(n, x)
39 unsigned long n, x;
40 {
41   unsigned long remainder = x % n;
42   if (remainder)
43     x += n-remainder;
44   return x;
45 }
46 */
47
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)))
49
50 #define FILETYPES       (S_IFREG|S_IFLNK|S_IFDIR)
51
52 typedef struct name_s {
53     struct name_s *next;
54     char *str;
55 } Name;
56
57 Name *name_stack;
58
59 #define MAXDUMPS 10
60
61 struct {
62     int max_inode;
63     int total_dirs;
64     int total_files;
65     long total_size;
66     long total_size_name;
67 } dumpstats[MAXDUMPS];
68
69 time_t dumpdate[MAXDUMPS];
70 int  dumplevel[MAXDUMPS];
71 int ndumps;
72
73 void (*add_file_name) P((int, char *));
74 void (*add_file) P((int, struct stat *));
75 long (*final_size) P((int, char *));
76
77
78 int main P((int, char **));
79 void traverse_dirs P((char *, char *));
80
81
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 *));
85
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 *));
89
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 *));
93
94 sl_t *calc_load_file P((char *filename));
95 int calc_check_exclude P((char *filename));
96
97 int use_gtar_excl = 0;
98 sl_t *include_sl=NULL, *exclude_sl=NULL;
99
100 int main(argc, argv)
101 int argc;
102 char **argv;
103 {
104 #ifdef TEST
105 /* standalone test to ckeck wether the calculated file size is ok */
106     struct stat finfo;
107     int i;
108     unsigned long dump_total=0, gtar_total=0;
109     char *d;
110     int l, w;
111
112     safe_fd(-1, 0);
113
114     set_pname("calcsize");
115
116     if (argc < 2) {
117         fprintf(stderr,"Usage: %s file[s]\n",argv[0]);
118         return 1;
119     }
120     for(i=1; i<argc; i++) {
121         if(lstat(argv[i], &finfo) == -1) {
122             fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
123             continue;
124         }
125         printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
126         printf(": blocks=%lu\n", (unsigned long)ST_BLOCKS(finfo));
127         dump_total += (ST_BLOCKS(finfo) + 1)/2 + 1;
128         gtar_total += ROUND(4,(ST_BLOCKS(finfo) + 1));
129     }
130     printf("           gtar           dump\n");
131     printf("total      %-9lu         %-9lu\n",gtar_total,dump_total);
132     return 0;
133 #else
134     int i;
135     char *dirname=NULL, *amname=NULL, *filename=NULL;
136     unsigned long malloc_hist_1, malloc_size_1;
137     unsigned long malloc_hist_2, malloc_size_2;
138
139     safe_fd(-1, 0);
140     safe_cd();
141
142     set_pname("calcsize");
143
144     malloc_size_1 = malloc_inuse(&malloc_hist_1);
145
146 #if 0
147     erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
148 #endif
149
150     argc--, argv++;     /* skip program name */
151
152     /* need at least program, amname, and directory name */
153
154     if(argc < 3) {
155         error("Usage: %s [DUMP|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*",
156               get_pname());
157         return 1;
158     }
159
160     /* parse backup program name */
161
162     if(strcmp(*argv, "DUMP") == 0) {
163 #if !defined(DUMP) && !defined(XFSDUMP)
164         error("dump not available on this system");
165         return 1;
166 #else
167         add_file_name = add_file_name_dump;
168         add_file = add_file_dump;
169         final_size = final_size_dump;
170 #endif
171     }
172     else if(strcmp(*argv, "GNUTAR") == 0) {
173 #ifndef GNUTAR
174         error("gnutar not available on this system");
175         return 1;
176 #else
177         add_file_name = add_file_name_gnutar;
178         add_file = add_file_gnutar;
179         final_size = final_size_gnutar;
180         use_gtar_excl++;
181 #endif
182     }
183     else {
184         add_file_name = add_file_name_unknown;
185         add_file = add_file_unknown;
186         final_size = final_size_unknown;
187     }
188     argc--, argv++;
189
190     /* the amanda name can be different from the directory name */
191
192     if (argc > 0) {
193         amname = *argv;
194         argc--, argv++;
195     } else
196         error("missing <name>");
197
198     /* the toplevel directory name to search from */
199     if (argc > 0) {
200         dirname = *argv;
201         argc--, argv++;
202     } else
203         error("missing <dir>");
204
205     if ((argc > 1) && strcmp(*argv,"-X") == 0) {
206         argv++;
207
208         if (!use_gtar_excl) {
209           error("exclusion specification not supported");
210           return 1;
211         }
212         
213         filename = stralloc(*argv);
214         if (access(filename, R_OK) != 0) {
215             fprintf(stderr,"Cannot open exclude file %s\n",filename);
216             use_gtar_excl = 0;
217         } else {
218           exclude_sl = calc_load_file(filename);
219         }
220         amfree(filename);
221         argc -= 2;
222         argv++;
223     } else
224         use_gtar_excl = 0;
225
226     if ((argc > 1) && strcmp(*argv,"-I") == 0) {
227         argv++;
228         
229         filename = stralloc(*argv);
230         if (access(filename, R_OK) != 0) {
231             fprintf(stderr,"Cannot open include file %s\n",filename);
232             use_gtar_excl = 0;
233         } else {
234           include_sl = calc_load_file(filename);
235         }
236         amfree(filename);
237         argc -= 2;
238         argv++;
239     }
240
241     /* the dump levels to calculate sizes for */
242
243     ndumps = 0;
244     while(argc >= 2) {
245         if(ndumps < MAXDUMPS) {
246             dumplevel[ndumps] = atoi(argv[0]);
247             dumpdate [ndumps] = (time_t) atol(argv[1]);
248             ndumps++;
249             argc -= 2, argv += 2;
250         }
251     }
252
253     if(argc)
254         error("leftover arg \"%s\", expected <level> and <date>", *argv);
255
256     if(is_empty_sl(include_sl)) {
257         traverse_dirs(dirname,".");
258     }
259     else {
260         sle_t *an_include = include_sl->first;
261         while(an_include != NULL) {
262 /*
263             char *adirname = stralloc2(dirname, an_include->name+1);
264             traverse_dirs(adirname);
265             amfree(adirname);
266 */
267             traverse_dirs(dirname, an_include->name);
268             an_include = an_include->next;
269         }
270     }
271     for(i = 0; i < ndumps; i++) {
272
273         amflock(1, "size");
274
275         lseek(1, (off_t)0, SEEK_END);
276
277         fprintf(stderr, "%s %d SIZE %ld\n",
278                amname, dumplevel[i], final_size(i, dirname));
279         fflush(stdout);
280
281         amfunlock(1, "size");
282     }
283
284     malloc_size_2 = malloc_inuse(&malloc_hist_2);
285
286     if(malloc_size_1 != malloc_size_2) {
287         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
288     }
289
290     return 0;
291 #endif
292 }
293
294 /*
295  * =========================================================================
296  */
297
298 #ifndef HAVE_BASENAME
299 char *basename(file)
300 char *file;
301 {
302     char *cp;
303
304     if ( (cp = strrchr(file,'/')) )
305         return cp+1;
306     return file;
307 }
308 #endif
309
310 void push_name P((char *str));
311 char *pop_name P((void));
312
313 void traverse_dirs(parent_dir, include)
314 char *parent_dir;
315 char *include;
316 {
317     DIR *d;
318     struct dirent *f;
319     struct stat finfo;
320     char *dirname, *newname = NULL;
321     char *newbase = NULL;
322     dev_t parent_dev = 0;
323     int i;
324     int l;
325     int parent_len;
326     int has_exclude = !is_empty_sl(exclude_sl) && use_gtar_excl;
327
328     char *aparent = vstralloc(parent_dir, "/", include, NULL);
329
330     if(parent_dir && stat(parent_dir, &finfo) != -1)
331         parent_dev = finfo.st_dev;
332
333     parent_len = strlen(parent_dir);
334
335     push_name(aparent);
336
337     for(dirname = pop_name(); dirname; free(dirname), dirname = pop_name()) {
338         if(has_exclude && calc_check_exclude(dirname+parent_len+1)) {
339             continue;
340         }
341         if((d = opendir(dirname)) == NULL) {
342             perror(dirname);
343             continue;
344         }
345
346         l = strlen(dirname);
347         if(l > 0 && dirname[l - 1] != '/') {
348             newbase = newstralloc2(newbase, dirname, "/");
349         } else {
350             newbase = newstralloc(newbase, dirname);
351         }
352
353         while((f = readdir(d)) != NULL) {
354             int is_symlink = 0;
355             int is_dir;
356             int is_file;
357             if(is_dot_or_dotdot(f->d_name)) {
358                 continue;
359             }
360
361             newname = newstralloc2(newname, newbase, f->d_name);
362             if(lstat(newname, &finfo) == -1) {
363                 fprintf(stderr, "%s/%s: %s\n",
364                         dirname, f->d_name, strerror(errno));
365                 continue;
366             }
367
368             if(finfo.st_dev != parent_dev) {
369                 continue;
370             }
371
372 #ifdef S_IFLNK
373             is_symlink = ((finfo.st_mode & S_IFMT) == S_IFLNK);
374 #endif
375             is_dir = ((finfo.st_mode & S_IFMT) == S_IFDIR);
376             is_file = ((finfo.st_mode & S_IFMT) == S_IFREG);
377
378             if (!(is_file || is_dir || is_symlink)) {
379                 continue;
380             }
381
382             {
383                 int is_excluded = -1;
384                 for(i = 0; i < ndumps; i++) {
385                     add_file_name(i, newname);
386                     if(is_file && finfo.st_ctime >= dumpdate[i]) {
387
388                         if(has_exclude) {
389                             if(is_excluded == -1)
390                                 is_excluded =
391                                        calc_check_exclude(newname+parent_len+1);
392                             if(is_excluded == 1) {
393                                 i = ndumps;
394                                 continue;
395                             }
396                         }
397                         add_file(i, &finfo);
398                     }
399                 }
400                 if(is_dir) {
401                     if(has_exclude && calc_check_exclude(newname+parent_len+1))
402                         continue;
403                     push_name(newname);
404                 }
405             }
406         }
407
408 #ifdef CLOSEDIR_VOID
409         closedir(d);
410 #else
411         if(closedir(d) == -1)
412             perror(dirname);
413 #endif
414     }
415     amfree(newbase);
416     amfree(newname);
417     amfree(aparent);
418 }
419
420 void push_name(str)
421 char *str;
422 {
423     Name *newp;
424
425     newp = alloc(sizeof(*newp));
426     newp->str = stralloc(str);
427
428     newp->next = name_stack;
429     name_stack = newp;
430 }
431
432 char *pop_name()
433 {
434     Name *newp = name_stack;
435     char *str;
436
437     if(!newp) return NULL;
438
439     name_stack = newp->next;
440     str = newp->str;
441     amfree(newp);
442     return str;
443 }
444
445
446 /*
447  * =========================================================================
448  * Backup size calculations for DUMP program
449  *
450  * Given the system-dependent nature of dump, it's impossible to pin this
451  * down accurately.  Luckily, that's not necessary.
452  *
453  * Dump rounds each file up to TP_BSIZE bytes, which is 1k in the BSD dump,
454  * others are unknown.  In addition, dump stores three bitmaps at the
455  * beginning of the dump: a used inode map, a dumped dir map, and a dumped
456  * inode map.  These are sized by the number of inodes in the filesystem.
457  *
458  * We don't take into account the complexities of BSD dump's indirect block
459  * requirements for files with holes, nor the dumping of directories that
460  * are not themselves modified.
461  */
462 void add_file_name_dump(level, name)
463 int level;
464 char *name;
465 {
466     return;
467 }
468
469 void add_file_dump(level, sp)
470 int level;
471 struct stat *sp;
472 {
473     /* keep the size in kbytes, rounded up, plus a 1k header block */
474     if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
475         dumpstats[level].total_size += (ST_BLOCKS(*sp) + 1)/2 + 1;
476 }
477
478 long final_size_dump(level, topdir)
479 int level;
480 char *topdir;
481 {
482     generic_fs_stats_t stats;
483     int mapsize;
484     char *s;
485
486     /* calculate the map sizes */
487
488     s = stralloc2(topdir, "/.");
489     if(get_fs_stats(s, &stats) == -1) {
490         error("statfs %s: %s", s, strerror(errno));
491     }
492     amfree(s);
493
494     mapsize = (stats.files + 7) / 8;    /* in bytes */
495     mapsize = (mapsize + 1023) / 1024;  /* in kbytes */
496
497     /* the dump contains three maps plus the files */
498
499     return 3*mapsize + dumpstats[level].total_size;
500 }
501
502 /*
503  * =========================================================================
504  * Backup size calculations for GNUTAR program
505  *
506  * Gnutar's basic blocksize is 512 bytes.  Each file is rounded up to that
507  * size, plus one header block.  Gnutar stores directories' file lists in
508  * incremental dumps - we'll pick up size of the modified dirs here.  These
509  * will be larger than a simple filelist of their contents, but that's ok.
510  *
511  * As with DUMP, we only need a reasonable estimate, not an exact figure.
512  */
513 void add_file_name_gnutar(level, name)
514 int level;
515 char *name;
516 {
517 /*    dumpstats[level].total_size_name += strlen(name) + 64;*/
518       dumpstats[level].total_size += 1;
519 }
520
521 void add_file_gnutar(level, sp)
522 int level;
523 struct stat *sp;
524 {
525     /* the header takes one additional block */
526     dumpstats[level].total_size += ST_BLOCKS(*sp);
527 }
528
529 long final_size_gnutar(level, topdir)
530 int level;
531 char *topdir;
532 {
533     /* divide by two to get kbytes, rounded up */
534     /* + 4 blocks for security */
535     return (dumpstats[level].total_size + 5 + (dumpstats[level].total_size_name/512)) / 2;
536 }
537
538 /*
539  * =========================================================================
540  * Backup size calculations for unknown backup programs.
541  *
542  * Here we'll just add up the file sizes and output that.
543  */
544
545 void add_file_name_unknown(level, name)
546 int level;
547 char *name;
548 {
549     return;
550 }
551
552 void add_file_unknown(level, sp)
553 int level;
554 struct stat *sp;
555 {
556     /* just add up the block counts */
557     if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
558         dumpstats[level].total_size += ST_BLOCKS(*sp);
559 }
560
561 long final_size_unknown(level, topdir)
562 int level;
563 char *topdir;
564 {
565     /* divide by two to get kbytes, rounded up */
566     return (dumpstats[level].total_size + 1) / 2;
567 }
568
569 /*
570  * =========================================================================
571  */
572 sl_t *calc_load_file(filename)
573 char *filename;
574 {
575     char pattern[1025];
576
577     sl_t *sl_list = new_sl();
578
579     FILE *file = fopen(filename, "r");
580
581     while(fgets(pattern, 1025, file)) {
582         if(strlen(pattern)>0 && pattern[strlen(pattern)-1] == '\n')
583             pattern[strlen(pattern)-1] = '\0';
584         sl_list = append_sl(sl_list, pattern);
585     }  
586     fclose(file);
587
588     return sl_list;
589 }
590
591 int calc_check_exclude(filename)
592 char *filename;
593 {
594     sle_t *an_exclude;
595     if(is_empty_sl(exclude_sl)) return 0;
596
597     an_exclude=exclude_sl->first;
598     while(an_exclude != NULL) {
599         if(match_tar(an_exclude->name, filename)) {
600             return 1;
601         }
602         an_exclude=an_exclude->next;
603     }
604     return 0;
605 }