1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
/*
* Amanda, The Advanced Maryland Automatic Network Disk Archiver
* Copyright (c) 1991-1998 University of Maryland at College Park
* Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of U.M. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. U.M. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: the Amanda Development Team. Its members are listed in a
* file named AUTHORS, in the root directory of this distribution.
*/
/*
* $Id: calcsize.c,v 1.44 2006/07/25 18:27:56 martinea Exp $
*
* traverse directory tree to get backup size estimates
*
* argv[0] is the calcsize program name
* argv[1] is the config name or NOCONFIG
*/
#include "amanda.h"
#include "match.h"
#include "conffile.h"
#include "fsusage.h"
#include "am_sl.h"
#include "amutil.h"
#define ROUND(n,x) ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
/*
static off_t
round_function(n, x)
off_t n,
off_t x)
{
unsigned long remainder = x % n;
if (remainder)
x += n - remainder;
return x;
}
*/
#define ST_BLOCKS(s) \
(((((off_t)(s).st_blocks * (off_t)512) <= (s).st_size)) ? \
((off_t)(s).st_blocks + (off_t)1) : \
((s).st_size / (off_t)512 + \
(off_t)((((s).st_size % (off_t)512) != (off_t)0) ? \
(off_t)1 : (off_t)0)))
#define FILETYPES (S_IFREG|S_IFLNK|S_IFDIR)
typedef struct name_s {
struct name_s *next;
char *str;
} Name;
Name *name_stack;
#define MAXDUMPS 10
struct {
int max_inode;
int total_dirs;
int total_files;
off_t total_size;
off_t total_size_name;
} dumpstats[MAXDUMPS];
time_t dumpdate[MAXDUMPS];
int dumplevel[MAXDUMPS];
int ndumps;
void (*add_file_name)(int, char *);
void (*add_file)(int, struct stat *);
off_t (*final_size)(int, char *);
int main(int, char **);
void traverse_dirs(char *, char *);
void add_file_name_dump(int, char *);
void add_file_dump(int, struct stat *);
off_t final_size_dump(int, char *);
void add_file_name_star(int, char *);
void add_file_star(int, struct stat *);
off_t final_size_star(int, char *);
void add_file_name_gnutar(int, char *);
void add_file_gnutar(int, struct stat *);
off_t final_size_gnutar(int, char *);
void add_file_name_unknown(int, char *);
void add_file_unknown(int, struct stat *);
off_t final_size_unknown(int, char *);
am_sl_t *calc_load_file(char *filename);
int calc_check_exclude(char *filename);
int use_star_excl = 0;
int use_gtar_excl = 0;
am_sl_t *include_sl=NULL, *exclude_sl=NULL;
int
main(
int argc,
char ** argv)
{
#ifdef TEST
/* standalone test to ckeck wether the calculated file size is ok */
struct stat finfo;
int i;
off_t dump_total = (off_t)0;
off_t gtar_total = (off_t)0;
char *d;
int l, w;
glib_init();
/*
* Configure program for internationalization:
* 1) Only set the message locale for now.
* 2) Set textdomain for all amanda related programs to "amanda"
* We don't want to be forced to support dozens of message catalogs.
*/
setlocale(LC_MESSAGES, "C");
textdomain("amanda");
safe_fd(-1, 0);
set_pname("calcsize");
dbopen(NULL);
config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL);
/* Don't die when child closes pipe */
signal(SIGPIPE, SIG_IGN);
if (argc < 2) {
g_fprintf(stderr,_("Usage: %s file[s]\n"),argv[0]);
return 1;
}
for(i=1; i<argc; i++) {
if(lstat(argv[i], &finfo) == -1) {
g_fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
continue;
}
g_printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
g_printf(": blocks=%llu\n", ST_BLOCKS(finfo));
dump_total += (ST_BLOCKS(finfo) + (off_t)1) / (off_t)2 + (off_t)1;
gtar_total += ROUND(4,(ST_BLOCKS(finfo) + (off_t)1));
}
g_printf(" gtar dump\n");
g_printf("total %-9lu %-9lu\n",gtar_total,dump_total);
return 0;
#else
int i;
char *dirname=NULL;
char *amname=NULL, *qamname=NULL;
char *filename=NULL, *qfilename = NULL;
if (argc > 1 && argv[1] && g_str_equal(argv[1], "--version")) {
printf("calcsize-%s\n", VERSION);
return (0);
}
safe_fd(-1, 0);
safe_cd();
set_pname("calcsize");
dbopen(DBG_SUBDIR_CLIENT);
config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL);
dbprintf(_("version %s\n"), VERSION);
/* drop root privileges; we'll regain them for the required operations */
#ifdef WANT_SETUID_CLIENT
check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
if (!set_root_privs(0)) {
error(_("calcsize must be run setuid root"));
}
#else
check_running_as(RUNNING_AS_CLIENT_LOGIN);
#endif
argc--, argv++; /* skip program name */
/* need at least program, amname, and directory name */
if(argc < 4) {
error(_("Usage: %s config [BSDTAR|DUMP|STAR|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*"),
get_pname());
/*NOTREACHED*/
}
dbprintf(_("config: %s\n"), *argv);
if (!g_str_equal(*argv, "NOCONFIG")) {
dbrename(*argv, DBG_SUBDIR_CLIENT);
}
argc--;
argv++;
/* parse backup program name */
if(g_str_equal(*argv, "DUMP")) {
#if !defined(DUMP) && !defined(XFSDUMP)
error("dump not available on this system");
/*NOTREACHED*/
#else
add_file_name = add_file_name_dump;
add_file = add_file_dump;
final_size = final_size_dump;
#endif
}
else if(g_str_equal(*argv, "BSDTAR")) {
add_file_name = add_file_name_gnutar;
add_file = add_file_gnutar;
final_size = final_size_gnutar;
use_gtar_excl++;
}
else if(g_str_equal(*argv, "GNUTAR")) {
#ifndef GNUTAR
error("gnutar not available on this system");
/*NOTREACHED*/
#else
add_file_name = add_file_name_gnutar;
add_file = add_file_gnutar;
final_size = final_size_gnutar;
use_gtar_excl++;
#endif
}
else {
add_file_name = add_file_name_unknown;
add_file = add_file_unknown;
final_size = final_size_unknown;
}
argc--, argv++;
/* the amanda name can be different from the directory name */
if (argc > 0) {
amname = *argv;
qamname = quote_string(amname);
argc--, argv++;
} else {
error("missing <name>");
/*NOTREACHED*/
}
/* the toplevel directory name to search from */
if (argc > 0) {
dirname = *argv;
argc--, argv++;
} else {
error("missing <dir>");
/*NOTREACHED*/
}
if ((argc > 1) && g_str_equal(*argv, "-X")) {
argv++;
if (!(use_gtar_excl || use_star_excl)) {
error("exclusion specification not supported");
/*NOTREACHED*/
}
filename = g_strdup(*argv);
qfilename = quote_string(filename);
if (access(filename, R_OK) != 0) {
g_fprintf(stderr,"Cannot open exclude file %s\n", qfilename);
use_gtar_excl = use_star_excl = 0;
} else {
exclude_sl = calc_load_file(filename);
if (!exclude_sl) {
g_fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename,
strerror(errno));
use_gtar_excl = use_star_excl = 0;
}
}
amfree(qfilename);
amfree(filename);
argc -= 2;
argv++;
} else {
use_gtar_excl = use_star_excl = 0;
}
if ((argc > 1) && g_str_equal(*argv, "-I")) {
argv++;
filename = g_strdup(*argv);
qfilename = quote_string(filename);
if (access(filename, R_OK) != 0) {
g_fprintf(stderr,"Cannot open include file %s\n", qfilename);
use_gtar_excl = use_star_excl = 0;
} else {
include_sl = calc_load_file(filename);
if (!include_sl) {
g_fprintf(stderr,"Cannot open include file %s: %s\n", qfilename,
strerror(errno));
use_gtar_excl = use_star_excl = 0;
}
}
amfree(qfilename);
amfree(filename);
argc -= 2;
argv++;
}
/* the dump levels to calculate sizes for */
ndumps = 0;
while(argc >= 2) {
if(ndumps < MAXDUMPS) {
dumplevel[ndumps] = atoi(argv[0]);
dumpdate [ndumps] = (time_t) atol(argv[1]);
ndumps++;
argc -= 2, argv += 2;
}
}
if(argc) {
error("leftover arg \"%s\", expected <level> and <date>", *argv);
/*NOTREACHED*/
}
if(is_empty_sl(include_sl)) {
traverse_dirs(dirname,".");
}
else {
sle_t *an_include = include_sl->first;
while(an_include != NULL) {
/*
char *adirname = stralloc2(dirname, an_include->name+1);
traverse_dirs(adirname);
amfree(adirname);
*/
traverse_dirs(dirname, an_include->name);
an_include = an_include->next;
}
}
for(i = 0; i < ndumps; i++) {
amflock(2, "size");
dbprintf("calcsize: %s %d SIZE %lld\n",
qamname, dumplevel[i],
(long long)final_size(i, dirname));
g_fprintf(stderr, "%s %d SIZE %lld\n",
qamname, dumplevel[i],
(long long)final_size(i, dirname));
fflush(stderr);
amfunlock(2, "size");
}
amfree(qamname);
return 0;
#endif
}
/*
* =========================================================================
*/
#if !defined(HAVE_BASENAME) && defined(BUILTIN_EXCLUDE_SUPPORT)
static char *
basename(
char * file)
{
char *cp;
if ( (cp = strrchr(file,'/')) )
return cp+1;
return file;
}
#endif
void push_name(char *str);
char *pop_name(void);
void
traverse_dirs(
char * parent_dir,
char * include)
{
DIR *d;
struct dirent *f;
struct stat finfo;
char *dirname, *newname = NULL;
char *newbase = NULL;
dev_t parent_dev = (dev_t)0;
int i;
size_t l;
size_t parent_len;
int has_exclude;
char *aparent;
if(parent_dir == NULL || include == NULL)
return;
has_exclude = !is_empty_sl(exclude_sl) && (use_gtar_excl || use_star_excl);
aparent = g_strjoin(NULL, parent_dir, "/", include, NULL);
/* We (may) need root privs for the *stat() calls here. */
set_root_privs(1);
if(stat(parent_dir, &finfo) != -1)
parent_dev = finfo.st_dev;
parent_len = strlen(parent_dir);
push_name(aparent);
for(; (dirname = pop_name()) != NULL; free(dirname)) {
if(has_exclude && calc_check_exclude(dirname+parent_len+1)) {
continue;
}
if((d = opendir(dirname)) == NULL) {
continue;
}
l = strlen(dirname);
if(l > 0 && dirname[l - 1] != '/') {
g_free(newbase);
newbase = g_strconcat(dirname, "/", NULL);
} else {
g_free(newbase);
newbase = g_strdup(dirname);
}
while((f = readdir(d)) != NULL) {
int is_symlink = 0;
int is_dir;
int is_file;
if(is_dot_or_dotdot(f->d_name)) {
continue;
}
g_free(newname);
newname = g_strconcat(newbase, f->d_name, NULL);
if(lstat(newname, &finfo) == -1) {
g_fprintf(stderr, "%s/%s: %s\n",
dirname, f->d_name, strerror(errno));
continue;
}
if(finfo.st_dev != parent_dev)
continue;
#ifdef S_IFLNK
is_symlink = ((finfo.st_mode & S_IFMT) == S_IFLNK);
#endif
is_dir = ((finfo.st_mode & S_IFMT) == S_IFDIR);
is_file = ((finfo.st_mode & S_IFMT) == S_IFREG);
if (!(is_file || is_dir || is_symlink)) {
continue;
}
{
int is_excluded = -1;
for(i = 0; i < ndumps; i++) {
add_file_name(i, newname);
if(is_file && (time_t)finfo.st_ctime >= dumpdate[i]) {
if(has_exclude) {
if(is_excluded == -1)
is_excluded =
calc_check_exclude(newname+parent_len+1);
if(is_excluded == 1) {
i = ndumps;
continue;
}
}
add_file(i, &finfo);
}
}
if(is_dir) {
if(has_exclude && calc_check_exclude(newname+parent_len+1))
continue;
push_name(newname);
}
}
}
#ifdef CLOSEDIR_VOID
closedir(d);
#else
if(closedir(d) == -1)
perror(dirname);
#endif
}
/* drop root privs -- we're done with the permission-sensitive calls */
set_root_privs(0);
amfree(newbase);
amfree(newname);
amfree(aparent);
}
void
push_name(
char * str)
{
Name *newp;
newp = g_malloc(sizeof(*newp));
newp->str = g_strdup(str);
newp->next = name_stack;
name_stack = newp;
}
char *
pop_name(void)
{
Name *newp = name_stack;
char *str;
if(!newp) return NULL;
name_stack = newp->next;
str = newp->str;
amfree(newp);
return str;
}
/*
* =========================================================================
* Backup size calculations for DUMP program
*
* Given the system-dependent nature of dump, it's impossible to pin this
* down accurately. Luckily, that's not necessary.
*
* Dump rounds each file up to TP_BSIZE bytes, which is 1k in the BSD dump,
* others are unknown. In addition, dump stores three bitmaps at the
* beginning of the dump: a used inode map, a dumped dir map, and a dumped
* inode map. These are sized by the number of inodes in the filesystem.
*
* We don't take into account the complexities of BSD dump's indirect block
* requirements for files with holes, nor the dumping of directories that
* are not themselves modified.
*/
void
add_file_name_dump(
int level,
char * name)
{
(void)level; /* Quiet unused parameter warning */
(void)name; /* Quiet unused parameter warning */
return;
}
void
add_file_dump(
int level,
struct stat * sp)
{
/* keep the size in kbytes, rounded up, plus a 1k header block */
if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
dumpstats[level].total_size +=
(ST_BLOCKS(*sp) + (off_t)1) / (off_t)2 + (off_t)1;
}
off_t
final_size_dump(
int level,
char * topdir)
{
struct fs_usage fsusage;
off_t mapsize;
char *s;
int rc;
int saved_errno;
/* calculate the map sizes */
s = g_strconcat(topdir, "/.", NULL);
set_root_privs(1);
rc = get_fs_usage(s, NULL, &fsusage);
saved_errno = errno;
set_root_privs(0);
if (rc == -1) {
error("statfs %s: %s", s, strerror(saved_errno));
/*NOTREACHED*/
}
amfree(s);
mapsize = (fsusage.fsu_files + (off_t)7) / (off_t)8; /* in bytes */
mapsize = (mapsize + (off_t)1023) / (off_t)1024; /* in kbytes */
/* the dump contains three maps plus the files */
return (mapsize * (off_t)3) + dumpstats[level].total_size;
}
/*
* =========================================================================
* Backup size calculations for GNUTAR program
*
* Gnutar's basic blocksize is 512 bytes. Each file is rounded up to that
* size, plus one header block. Gnutar stores directories' file lists in
* incremental dumps - we'll pick up size of the modified dirs here. These
* will be larger than a simple filelist of their contents, but that's ok.
*
* As with DUMP, we only need a reasonable estimate, not an exact figure.
*/
void
add_file_name_gnutar(
int level,
char * name)
{
(void)name; /* Quiet unused parameter warning */
/* dumpstats[level].total_size_name += strlen(name) + 64;*/
dumpstats[level].total_size += (off_t)1;
}
void
add_file_gnutar(
int level,
struct stat * sp)
{
/* the header takes one additional block */
dumpstats[level].total_size += ST_BLOCKS(*sp);
}
off_t
final_size_gnutar(
int level,
char * topdir)
{
(void)topdir; /* Quiet unused parameter warning */
/* divide by two to get kbytes, rounded up */
/* + 4 blocks for security */
return (dumpstats[level].total_size + (off_t)5 +
(dumpstats[level].total_size_name/(off_t)512)) / (off_t)2;
}
/*
* =========================================================================
* Backup size calculations for unknown backup programs.
*
* Here we'll just add up the file sizes and output that.
*/
void
add_file_name_unknown(
int level,
char * name)
{
(void)level; /* Quiet unused parameter warning */
(void)name; /* Quiet unused parameter warning */
return;
}
void
add_file_unknown(
int level,
struct stat * sp)
{
/* just add up the block counts */
if((sp->st_mode & S_IFMT) == S_IFREG || (sp->st_mode & S_IFMT) == S_IFDIR)
dumpstats[level].total_size += ST_BLOCKS(*sp);
}
off_t
final_size_unknown(
int level,
char * topdir)
{
(void)topdir; /* Quiet unused parameter warning */
/* divide by two to get kbytes, rounded up */
return (dumpstats[level].total_size + (off_t)1) / (off_t)2;
}
/*
* =========================================================================
*/
am_sl_t *
calc_load_file(
char * filename)
{
char pattern[1025];
am_sl_t *sl_list;
FILE *file = fopen(filename, "r");
if (!file) {
return NULL;
}
sl_list = new_sl();
while(fgets(pattern, 1025, file)) {
if(strlen(pattern)>0 && pattern[strlen(pattern)-1] == '\n')
pattern[strlen(pattern)-1] = '\0';
sl_list = append_sl(sl_list, pattern);
}
fclose(file);
return sl_list;
}
int
calc_check_exclude(
char * filename)
{
sle_t *an_exclude;
if(is_empty_sl(exclude_sl)) return 0;
an_exclude=exclude_sl->first;
while(an_exclude != NULL) {
if(match_tar(an_exclude->name, filename)) {
return 1;
}
an_exclude=an_exclude->next;
}
return 0;
}
|