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