Imported Upstream version 2.6.1
[debian/amanda] / server-src / amtrmlog.c
index 065547dcd4ffedf82eb8ba1c1713c9945d8fbef4..530fcf5b8af5aeab92f7db1d8a7fbcdce369c83c 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: amtrmlog.c,v 1.1.2.3.4.1.2.2 2003/01/01 23:28:53 martinea Exp $
+ * $Id: amtrmlog.c,v 1.17 2006/07/25 18:27:57 martinea Exp $
  *
  * trims number of index files to only those still in system.  Well
  * actually, it keeps a few extra, plus goes back to the last level 0
 
 #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"
 
-int main(argc, argv)
-int argc;
-char **argv;
+int amtrmidx_debug = 0;
+
+int main(int argc, char **argv);
+
+int
+main(
+    int                argc,
+    char **    argv)
 {
-    disklist_t *diskl;
+    disklist_t diskl;
     int no_keep;                       /* files per system to keep */
-    int fd;
     char **output_find_log;
     DIR *dir;
     struct dirent *adir;
@@ -60,126 +61,139 @@ char **argv;
     char *logname = NULL;
     struct stat stat_log;
     struct stat stat_old;
-    char *conffile;
     char *conf_diskfile;
     char *conf_tapelist;
     char *conf_logdir;
-    int amtrmidx_debug = 0;
-
-    for(fd = 3; fd < FD_SETSIZE; fd++) {
-       /*
-        * Make sure nobody spoofs us with a lot of extra open files
-        * that would cause an open we do to get a very high file
-        * descriptor, which in turn might be used as an index into
-        * an array (e.g. an fd_set).
-        */
-       close(fd);
-    }
-
+    int dumpcycle;
+    config_overwrites_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();
 
     set_pname("amtrmlog");
 
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
+
+    cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
+
     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
        amtrmidx_debug = 1;
        argc--;
        argv++;
     }
 
-    if (argc != 2) {
-       fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
+    if (argc < 2) {
+       g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
        return 1;
     }
 
-    dbopen();
-    dbprintf(("%s: version %s\n", argv[0], version()));
+    dbopen(DBG_SUBDIR_SERVER);
+    dbprintf(_("%s: version %s\n"), argv[0], version());
 
-    config_name = argv[1];
+    config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]);
+    apply_config_overwrites(cfg_ovr);
 
-    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);
-    }
-    amfree(conffile);
-    conf_diskfile = getconf_str(CNF_DISKFILE);
-    if (*conf_diskfile == '/') {
-       conf_diskfile = stralloc(conf_diskfile);
-    } else {
-       conf_diskfile = stralloc2(config_dir, conf_diskfile);
-    }
-    if((diskl = read_diskfile(conf_diskfile)) == NULL) {
-       error("could not load disklist %s", conf_diskfile);
-    }
+    conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
+    read_diskfile(conf_diskfile, &diskl);
     amfree(conf_diskfile);
-    conf_tapelist = getconf_str(CNF_TAPELIST);
-    if (*conf_tapelist == '/') {
-       conf_tapelist = stralloc(conf_tapelist);
-    } else {
-       conf_tapelist = stralloc2(config_dir, conf_tapelist);
+
+    if (config_errors(NULL) >= CFGERR_WARNINGS) {
+       config_print_errors();
+       if (config_errors(NULL) >= CFGERR_ERRORS) {
+           g_critical(_("errors processing config file"));
+       }
     }
+
+    check_running_as(RUNNING_AS_DUMPUSER);
+
+    dbrename(get_config_name(), DBG_SUBDIR_SERVER);
+
+    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);
 
     today = time((time_t *)NULL);
-    date_keep = today - (getconf_int(CNF_DUMPCYCLE)*86400);
+    dumpcycle = getconf_int(CNF_DUMPCYCLE);
+    if(dumpcycle > 5000)
+       dumpcycle = 5000;
+    date_keep = today - (dumpcycle * 86400);
 
-    output_find_log = find_log(NULL,0,NULL);
+    output_find_log = find_log();
 
     /* determine how many log to keep */
     no_keep = getconf_int(CNF_TAPECYCLE) * 2;
-    dbprintf(("Keeping %d log file%s\n", no_keep, (no_keep == 1) ? "" : "s"));
+    dbprintf(plural(_("Keeping %d log file\n"),
+                   _("Keeping %d log files\n"), no_keep),
+            no_keep);
 
-    conf_logdir = getconf_str(CNF_LOGDIR);
-    if (*conf_logdir == '/') {
-       conf_logdir = stralloc(conf_logdir);
-    } else {
-       conf_logdir = stralloc2(config_dir, conf_logdir);
-    }
+    conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
     olddir = vstralloc(conf_logdir, "/oldlog", NULL);
-    if (mkpdir(olddir, 02700, (uid_t)-1, (gid_t)-1) != 0) {
-       error("could not create parents of %s: %s", olddir, strerror(errno));
+    if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) {
+       error(_("could not create parents of %s: %s"), olddir, strerror(errno));
+       /*NOTREACHED*/
     }
-    if (mkdir(olddir, 02700) != 0 && errno != EEXIST) {
-       error("could not create %s: %s", olddir, strerror(errno));
+    if (mkdir(olddir, 0700) != 0 && errno != EEXIST) {
+       error(_("could not create %s: %s"), olddir, strerror(errno));
+       /*NOTREACHED*/
     }
 
     if (stat(olddir,&stat_old) == -1) {
-       error("can't stat oldlog directory \"%s\": %s", olddir, strerror(errno));
+       error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno));
+       /*NOTREACHED*/
     }
 
     if (!S_ISDIR(stat_old.st_mode)) {
-       error("Oldlog directory \"%s\" is not a directory", olddir);
+       error(_("Oldlog directory \"%s\" is not a directory"), olddir);
+       /*NOTREACHED*/
     }
+
     if ((dir = opendir(conf_logdir)) == NULL) {
-       error("could not open log directory \"%s\": %s", conf_logdir, strerror(errno));
+       error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno));
+       /*NOTREACHED*/
     }
-    while ((adir = readdir(dir)) != NULL) {
+    while ((adir=readdir(dir)) != NULL) {
        if(strncmp(adir->d_name,"log.",4)==0) {
-           useful = 0;
-           for (name = output_find_log; *name != NULL; name++) {
-               if(strncmp(adir->d_name, *name, 12) == 0) {
-                   useful = 1;
+           useful=0;
+           for (name=output_find_log;*name !=NULL; name++) {
+               if((strlen(adir->d_name) >= 13 &&
+                   strlen(*name) >= 13 &&
+                   adir->d_name[12] == '.' && (*name)[12] == '.' &&
+                   strncmp(adir->d_name,*name,12)==0) ||
+                  strncmp(adir->d_name,*name,18)==0) {
+                   useful=1;
+                   break;
                }
            }
-           logname = newvstralloc(logname,
-                                  conf_logdir, "/", adir->d_name, NULL);
-           if(stat(logname, &stat_log) == 0) {
-               if(stat_log.st_mtime > date_keep) {
+           logname=newvstralloc(logname,
+                                conf_logdir, "/" ,adir->d_name, NULL);
+           if(stat(logname,&stat_log)==0) {
+               if((time_t)stat_log.st_mtime > date_keep) {
                    useful = 1;
                }
            }
-           if(! useful) {
+           if(useful == 0) {
                oldfile = newvstralloc(oldfile,
                                       conf_logdir, "/", adir->d_name, NULL);
                newfile = newvstralloc(newfile,
                                       olddir, "/", adir->d_name, NULL);
-               if(rename(oldfile, newfile) != 0) {
-                   error("could not rename \"%s\" to \"%s\": %s",
+               if (rename(oldfile,newfile) != 0) {
+                   error(_("could not rename \"%s\" to \"%s\": %s"),
                          oldfile, newfile, strerror(errno));
-               }
+                   /*NOTREACHED*/
+               }
            }
        }
     }
@@ -192,8 +206,9 @@ char **argv;
     amfree(oldfile);
     amfree(newfile);
     amfree(olddir);
-    amfree(config_dir);
     amfree(conf_logdir);
+    clear_tapelist();
+    free_disklist(&diskl);
 
     dbclose();