]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/dird.c
Simplify safer delete code using a single regex
[bacula/bacula] / bacula / src / dird / dird.c
index b2c3741a52c06a9b480ffae648cfdd140e875b57..07ad49db54a82db95f89dfed9f1e37da8c68e715 100644 (file)
@@ -1150,18 +1150,14 @@ static void cleanup_old_files()
    int len = strlen(director->working_directory);
    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
    POOLMEM *basename = get_pool_memory(PM_MESSAGE);
-   regex_t preg1, preg2, pexc1;
+   regex_t preg1;
    char prbuf[500];
    const int nmatch = 30;
    regmatch_t pmatch[nmatch];
    berrno be;
 
-   /* Includes */
-   const char *pat1 = ".*\\.restore\\..*\\.bsr$";
-   const char *pat2 = ".*\\.mail$";
-
-   /* Excludes */
-   const char *exc1 = ".*\\ ";
+   /* Exclude spaces and look for .mail or .restore.xx.bsr files */
+   const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail)$";
 
    /* Setup working directory prefix */
    pm_strcpy(basename, director->working_directory);
@@ -1173,23 +1169,8 @@ static void cleanup_old_files()
    rc = regcomp(&preg1, pat1, REG_EXTENDED);
    if (rc != 0) {
       regerror(rc, &preg1, prbuf, sizeof(prbuf));
-      Dmsg2(500,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
+      Pmsg2(000,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
            pat1, prbuf);
-      goto get_out4;
-   }
-   rc = regcomp(&preg2, pat2, REG_EXTENDED);
-   if (rc != 0) {
-      regerror(rc, &preg2, prbuf, sizeof(prbuf));
-      Pmsg2(100,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
-           pat2, prbuf);
-      goto get_out3;
-   }
-
-   rc = regcomp(&pexc1, exc1, REG_EXTENDED);
-   if (rc != 0) {
-      regerror(rc, &pexc1, prbuf, sizeof(prbuf));
-      Pmsg2(100,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
-           exc1, prbuf);
       goto get_out2;
    }
 
@@ -1200,7 +1181,7 @@ static void cleanup_old_files()
       
    if (!(dp = opendir(director->working_directory))) {
       berrno be;
-      Pmsg2(100, "Failed to open working dir %s for cleanup: ERR=%s\n", 
+      Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n", 
             director->working_directory, be.bstrerror());
       goto get_out1;
       return;
@@ -1217,15 +1198,9 @@ static void cleanup_old_files()
          Dmsg1(500, "Skipped: %s\n", result->d_name);
          continue;    
       }
-      rc = regexec(&pexc1, result->d_name, nmatch, pmatch,  0);
-      if (rc == 0) {
-         Dmsg1(500, "Excluded: %s\n", result->d_name);
-         continue;
-      }
 
       /* Unlink files that match regexes */
-      if (regexec(&preg1, result->d_name, nmatch, pmatch,  0) == 0 ||
-          regexec(&preg2, result->d_name, nmatch, pmatch,  0) == 0) {
+      if (regexec(&preg1, result->d_name, nmatch, pmatch,  0) == 0) {
          pm_strcpy(cleanup, basename);
          pm_strcat(cleanup, result->d_name);
          Dmsg1(100, "Unlink: %s\n", cleanup);
@@ -1237,12 +1212,8 @@ static void cleanup_old_files()
    closedir(dp);
 /* Be careful to free up the correct resources */
 get_out1:
-   regfree(&pexc1);
-get_out2:
-   regfree(&preg2);
-get_out3:
    regfree(&preg1);
-get_out4:
+get_out2:
    free_pool_memory(cleanup);
    free_pool_memory(basename);
 }