📄 calcsize.c
字号:
/* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1991-1998 University of Maryland at College Park * 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 "fsusage.h"#include "version.h"#include "sl.h"#include "util.h"#define ROUND(n,x) ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))/*static off_tround_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 10struct { 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 *);sl_t *calc_load_file(char *filename);int calc_check_exclude(char *filename);int use_star_excl = 0;int use_gtar_excl = 0;sl_t *include_sl=NULL, *exclude_sl=NULL;intmain( 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; /* * 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); /* 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; /* drop root privileges; we'll regain them for the required operations */#ifdef WANT_SETUID_CLIENT if (!set_root_privs(0)) { error(_("calcsize must be run setuid root")); }#endif safe_fd(-1, 0); safe_cd(); set_pname("calcsize"); dbopen(DBG_SUBDIR_CLIENT); dbprintf(_("version %s\n"), version());#if 0 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);#endif argc--, argv++; /* skip program name */ /* need at least program, amname, and directory name */ if(argc < 4) { error(_("Usage: %s config [DUMP|STAR|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*"), get_pname()); /*NOTREACHED*/ } dbprintf(_("config: %s\n"), *argv); if (strcmp(*argv, "NOCONFIG") != 0) { dbrename(*argv, DBG_SUBDIR_CLIENT); } argc--; argv++; check_running_as(RUNNING_AS_CLIENT_LOGIN); /* parse backup program name */ if(strcmp(*argv, "DUMP") == 0) {#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(strcmp(*argv, "GNUTAR") == 0) {#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) && strcmp(*argv,"-X") == 0) { argv++; if (!(use_gtar_excl || use_star_excl)) { error("exclusion specification not supported"); /*NOTREACHED*/ } filename = stralloc(*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) && strcmp(*argv,"-I") == 0) { argv++; filename = stralloc(*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(1, "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(1, "size"); } amfree(qamname); return 0;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -