Imported Upstream version 3.2.0
[debian/amanda] / server-src / amtrmidx.c
index e30a1d5968344720e5891068403c6a5529abe56d..6e20da187e4138c5d304e460feaf6e98aae0bc32 100644 (file)
 
 #include "amanda.h"
 #include "arglist.h"
-#ifdef HAVE_NETINET_IN_SYSTM_H
-#include <netinet/in_systm.h>
-#endif
 #include "conffile.h"
 #include "diskfile.h"
 #include "tapefile.h"
 #include "find.h"
-#include "version.h"
 #include "util.h"
 
 static int sort_by_name_reversed(const void *a, const void *b);
@@ -58,20 +54,30 @@ static int sort_by_name_reversed(
 }
 
 
-int main(int argc, char **argv)
+int
+main(
+    int                argc,
+    char **    argv)
 {
     disk_t *diskp;
     disklist_t diskl;
     size_t i;
-    char *conffile;
     char *conf_diskfile;
     char *conf_tapelist;
     char *conf_indexdir;
     find_result_t *output_find;
     time_t tmp_time;
     int amtrmidx_debug = 0;
-    int    new_argc,   my_argc;
-    char **new_argv, **my_argv;
+    config_overrides_t *cfg_ovr = NULL;
+
+    /*
+     * 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);
     safe_cd();
@@ -82,69 +88,49 @@ int main(int argc, char **argv)
     signal(SIGPIPE, SIG_IGN);
 
     dbopen(DBG_SUBDIR_SERVER);
-    dbprintf(("%s: version %s\n", argv[0], version()));
+    dbprintf(_("%s: version %s\n"), argv[0], VERSION);
 
-    parse_conf(argc, argv, &new_argc, &new_argv);
-    my_argc = new_argc;
-    my_argv = new_argv;
+    cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
 
-    if (my_argc > 1 && strcmp(my_argv[1], "-t") == 0) {
+    if (argc > 1 && strcmp(argv[1], "-t") == 0) {
        amtrmidx_debug = 1;
-       my_argc--;
-       my_argv++;
+       argc--;
+       argv++;
     }
 
-    if (my_argc < 2) {
-       fprintf(stderr, "Usage: %s [-t] <config> [-o configoption]*\n", my_argv[0]);
+    if (argc < 2) {
+       g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
        return 1;
     }
 
-    config_name = my_argv[1];
+    set_config_overrides(cfg_ovr);
+    config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, argv[1]);
 
-    config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
-    conffile = stralloc2(config_dir, CONFFILE_NAME);
-    if (read_conffile(conffile)) {
-       error("errors processing config file \"%s\"", conffile);
-       /*NOTREACHED*/
-    }
-    amfree(conffile);
+    conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
+    read_diskfile(conf_diskfile, &diskl);
+    amfree(conf_diskfile);
 
-    dbrename(config_name, DBG_SUBDIR_SERVER);
+    if (config_errors(NULL) >= CFGERR_WARNINGS) {
+       config_print_errors();
+       if (config_errors(NULL) >= CFGERR_ERRORS) {
+           g_critical(_("errors processing config file"));
+       }
+    }
 
-    report_bad_conf_arg();
+    check_running_as(RUNNING_AS_DUMPUSER);
 
-    conf_diskfile = getconf_str(CNF_DISKFILE);
-    if(*conf_diskfile == '/') {
-       conf_diskfile = stralloc(conf_diskfile);
-    } else {
-       conf_diskfile = stralloc2(config_dir, conf_diskfile);
-    }
-    if (read_diskfile(conf_diskfile, &diskl) < 0) {
-       error("could not load disklist \"%s\"", conf_diskfile);
-       /*NOTREACHED*/
-    }
-    amfree(conf_diskfile);
+    dbrename(get_config_name(), DBG_SUBDIR_SERVER);
 
-    conf_tapelist = getconf_str(CNF_TAPELIST);
-    if(*conf_tapelist == '/') {
-       conf_tapelist = stralloc(conf_tapelist);
-    } else {
-       conf_tapelist = stralloc2(config_dir, conf_tapelist);
-    }
+    conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
     if(read_tapelist(conf_tapelist)) {
-       error("could not load tapelist \"%s\"", conf_tapelist);
+       error(_("could not load tapelist \"%s\""), conf_tapelist);
        /*NOTREACHED*/
     }
     amfree(conf_tapelist);
 
-    output_find = find_dump(1, &diskl);
+    output_find = find_dump(&diskl);
 
-    conf_indexdir = getconf_str(CNF_INDEXDIR);
-    if(*conf_indexdir == '/') {
-       conf_indexdir = stralloc(conf_indexdir);
-    } else {
-       conf_indexdir = stralloc2(config_dir, conf_indexdir);
-    }
+    conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
 
     /* now go through the list of disks and find which have indexes */
     time(&tmp_time);
@@ -162,7 +148,8 @@ int main(int argc, char **argv)
            char *host;
            char *disk, *qdisk;
            size_t len_date;
-
+           disk_t *dp;
+           GSList *matching_dp = NULL;
 
            /* get listing of indices, newest first */
            host = sanitise_filename(diskp->host->hostname);
@@ -174,13 +161,27 @@ int main(int argc, char **argv)
                                 NULL);
            qindexdir = quote_string(indexdir);
 
-           dbprintf(("%s %s -> %s\n", diskp->host->hostname,
-                       qdisk, qindexdir));
+           /* find all dles that use the same indexdir */
+           for (dp = diskl.head; dp != NULL; dp = dp->next) {
+               char *dp_host, *dp_disk;
+
+               dp_host = sanitise_filename(dp->host->hostname);
+               dp_disk = sanitise_filename(dp->name);
+               if (strcmp(host, dp_host) == 0 &&
+                   strcmp(disk, dp_disk) == 0) {
+                   matching_dp = g_slist_append(matching_dp, dp);
+               }
+               amfree(dp_host);
+               amfree(dp_disk);
+           }
+
+           dbprintf("%s %s -> %s\n", diskp->host->hostname,
+                       qdisk, qindexdir);
            amfree(host);
            amfree(qdisk);
            amfree(disk);
            if ((d = opendir(indexdir)) == NULL) {
-               dbprintf(("could not open index directory %s\n", qindexdir));
+               dbprintf(_("could not open index directory %s\n"), qindexdir);
                amfree(indexdir);
                amfree(qindexdir);
                continue;
@@ -221,10 +222,10 @@ int main(int argc, char **argv)
                    if(lstat(path, &sbuf) != -1
                        && ((sbuf.st_mode & S_IFMT) == S_IFREG)
                        && ((time_t)sbuf.st_mtime < tmp_time)) {
-                       dbprintf(("rm %s\n", qpath));
+                       dbprintf("rm %s\n", qpath);
                        if(amtrmidx_debug == 0 && unlink(path) == -1) {
-                           dbprintf(("Error removing %s: %s\n",
-                                     qpath, strerror(errno)));
+                           dbprintf(_("Error removing %s: %s\n"),
+                                     qpath, strerror(errno));
                        }
                    }
                    amfree(qpath);
@@ -253,6 +254,8 @@ int main(int argc, char **argv)
                char *datestamp;
                int level;
                size_t len_date;
+               int matching = 0;
+               GSList *mdp;
 
                for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
                     if(! isdigit((int)(names[i][len_date]))) {
@@ -262,16 +265,23 @@ int main(int argc, char **argv)
 
                datestamp = stralloc(names[i]);
                datestamp[len_date] = '\0';
-               level = names[i][len_date+1] - '0';
-               if(!dump_exist(output_find, diskp->host->hostname,
-                               diskp->name, datestamp, level)) {
+               if (sscanf(&names[i][len_date+1], "%d", &level) != 1)
+                   level = 0;
+               for (mdp = matching_dp; mdp != NULL; mdp = mdp->next) {
+                   dp = mdp->data;
+                   if (dump_exist(output_find, dp->host->hostname,
+                                  dp->name, datestamp, level)) {
+                       matching = 1;
+                   }
+               }
+               if (!matching) {
                    char *path, *qpath;
                    path = stralloc2(indexdir, names[i]);
                    qpath = quote_string(path);
-                   dbprintf(("rm %s\n", qpath));
+                   dbprintf("rm %s\n", qpath);
                    if(amtrmidx_debug == 0 && unlink(path) == -1) {
-                       dbprintf(("Error removing %s: %s\n",
-                                 qpath, strerror(errno)));
+                       dbprintf(_("Error removing %s: %s\n"),
+                                 qpath, strerror(errno));
                    }
                    amfree(qpath);
                    amfree(path);
@@ -286,12 +296,9 @@ int main(int argc, char **argv)
     }
 
     amfree(conf_indexdir);
-    amfree(config_dir);
     free_find_result(&output_find);
     clear_tapelist();
     free_disklist(&diskl);
-    free_new_argv(new_argc, new_argv);
-    free_server_config();
 
     dbclose();