add bug closure to changelog
[debian/amanda] / client-src / calcsize.c
index 7d8ad04f0fa276b1ebf9ffd16c57a714f99453b5..9b23647e0276cace1e487fbf6ce72461d643f4f6 100644 (file)
  * argv[1] is the config name or NOCONFIG
  */
 #include "amanda.h"
-#include "statfs.h"
-#include "version.h"
-#include "sl.h"
+#include "match.h"
+#include "conffile.h"
+#include "fsusage.h"
+#include "am_sl.h"
 #include "util.h"
 
 #define ROUND(n,x)     ((x) + (n) - 1 - (((x) + (n) - 1) % (n)))
@@ -107,12 +108,12 @@ 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);
+am_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;
+am_sl_t *include_sl=NULL, *exclude_sl=NULL;
 
 int
 main(
@@ -128,39 +129,52 @@ main(
     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);
+    config_init(CONFIG_INIT_CLIENT, NULL);
 
     /* Don't die when child closes pipe */
     signal(SIGPIPE, SIG_IGN);
 
     if (argc < 2) {
-       fprintf(stderr,"Usage: %s file[s]\n",argv[0]);
+       g_fprintf(stderr,_("Usage: %s file[s]\n"),argv[0]);
        return 1;
     }
     for(i=1; i<argc; i++) {
        if(lstat(argv[i], &finfo) == -1) {
-           fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
+           g_fprintf(stderr, "%s: %s\n", argv[i], strerror(errno));
            continue;
        }
-       printf("%s: st_size=%lu", argv[i],(unsigned long)finfo.st_size);
-       printf(": blocks=%llu\n", ST_BLOCKS(finfo));
+       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));
     }
-    printf("           gtar           dump\n");
-    printf("total      %-9lu         %-9lu\n",gtar_total,dump_total);
+    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;
-    unsigned long malloc_hist_1, malloc_size_1;
-    unsigned long malloc_hist_2, malloc_size_2;
+
+    if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
+       printf("calcsize-%s\n", VERSION);
+       return (0);
+    }
 
     safe_fd(-1, 0);
     safe_cd();
@@ -168,12 +182,17 @@ main(
     set_pname("calcsize");
 
     dbopen(DBG_SUBDIR_CLIENT);
-    dbprintf(("%s: version %s\n", debug_prefix_time(NULL), version()));
-
-    malloc_size_1 = malloc_inuse(&malloc_hist_1);
-
-#if 0
-    erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
+    config_init(CONFIG_INIT_CLIENT, 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 */
@@ -181,12 +200,12 @@ main(
     /* need at least program, amname, and directory name */
 
     if(argc < 4) {
-       error("Usage: %s config [DUMP|GNUTAR] name dir [-X exclude-file] [-I include-file] [level date]*",
+       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));
+    dbprintf(_("config: %s\n"), *argv);
     if (strcmp(*argv, "NOCONFIG") != 0) {
        dbrename(*argv, DBG_SUBDIR_CLIENT);
     }
@@ -254,12 +273,12 @@ main(
        filename = stralloc(*argv);
        qfilename = quote_string(filename);
        if (access(filename, R_OK) != 0) {
-           fprintf(stderr,"Cannot open exclude file %s\n", qfilename);
+           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) {
-               fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename,
+               g_fprintf(stderr,"Cannot open exclude file %s: %s\n", qfilename,
                        strerror(errno));
                use_gtar_excl = use_star_excl = 0;
            }
@@ -278,12 +297,12 @@ main(
        filename = stralloc(*argv);
        qfilename = quote_string(filename);
        if (access(filename, R_OK) != 0) {
-           fprintf(stderr,"Cannot open include file %s\n", qfilename);
+           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) {
-               fprintf(stderr,"Cannot open include file %s: %s\n", qfilename,
+               g_fprintf(stderr,"Cannot open include file %s: %s\n", qfilename,
                        strerror(errno));
                use_gtar_excl = use_star_excl = 0;
            }
@@ -330,22 +349,17 @@ main(
 
        amflock(1, "size");
 
-       dbprintf(("calcsize: %s %d SIZE " OFF_T_FMT "\n",
+       dbprintf("calcsize: %s %d SIZE %lld\n",
               qamname, dumplevel[i],
-              (OFF_T_FMT_TYPE)final_size(i, dirname)));
-       fprintf(stderr, "%s %d SIZE " OFF_T_FMT "\n",
+              (long long)final_size(i, dirname));
+       g_fprintf(stderr, "%s %d SIZE %lld\n",
               qamname, dumplevel[i],
-              (OFF_T_FMT_TYPE)final_size(i, dirname));
+              (long long)final_size(i, dirname));
        fflush(stderr);
 
        amfunlock(1, "size");
     }
-
-    malloc_size_2 = malloc_inuse(&malloc_hist_2);
-
-    if(malloc_size_1 != malloc_size_2) {
-       malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
-    }
+    amfree(qamname);
 
     return 0;
 #endif
@@ -395,6 +409,8 @@ traverse_dirs(
     has_exclude = !is_empty_sl(exclude_sl) && (use_gtar_excl || use_star_excl);
     aparent = vstralloc(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;
 
@@ -428,7 +444,7 @@ traverse_dirs(
 
            newname = newstralloc2(newname, newbase, f->d_name);
            if(lstat(newname, &finfo) == -1) {
-               fprintf(stderr, "%s/%s: %s\n",
+               g_fprintf(stderr, "%s/%s: %s\n",
                        dirname, f->d_name, strerror(errno));
                continue;
            }
@@ -479,6 +495,10 @@ traverse_dirs(
            perror(dirname);
 #endif
     }
+
+    /* drop root privs -- we're done with the permission-sensitive calls */
+    set_root_privs(0);
+
     amfree(newbase);
     amfree(newname);
     amfree(aparent);
@@ -555,20 +575,20 @@ final_size_dump(
     int                level,
     char *     topdir)
 {
-    generic_fs_stats_t stats;
+    struct fs_usage fsusage;
     off_t mapsize;
     char *s;
 
     /* calculate the map sizes */
 
     s = stralloc2(topdir, "/.");
-    if(get_fs_stats(s, &stats) == -1) {
+    if(get_fs_usage(s, NULL, &fsusage) == -1) {
        error("statfs %s: %s", s, strerror(errno));
        /*NOTREACHED*/
     }
     amfree(s);
 
-    mapsize = (stats.files + (off_t)7) / (off_t)8;    /* in bytes */
+    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 */
@@ -662,13 +682,13 @@ final_size_unknown(
 /*
  * =========================================================================
  */
-sl_t *
+am_sl_t *
 calc_load_file(
     char *     filename)
 {
     char pattern[1025];
 
-    sl_t *sl_list;
+    am_sl_t *sl_list;
 
     FILE *file = fopen(filename, "r");