From 2671b84ba959a1eae9b6cd317acdcee76d49b763 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 10 Sep 2012 12:01:45 +0200 Subject: [PATCH] Simplify safer delete code using a single regex --- bacula/src/dird/dird.c | 43 +++++++------------------------------- bacula/src/lib/bsys.c | 30 ++++---------------------- bacula/src/lib/message.c | 5 ++++- bacula/src/stored/stored.c | 39 ++++++++++------------------------ 4 files changed, 26 insertions(+), 91 deletions(-) diff --git a/bacula/src/dird/dird.c b/bacula/src/dird/dird.c index b2c3741a52..07ad49db54 100644 --- a/bacula/src/dird/dird.c +++ b/bacula/src/dird/dird.c @@ -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); } diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index ce4964e763..aecad99100 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -46,10 +46,9 @@ static pthread_cond_t timer = PTHREAD_COND_INITIALIZER; /* * This routine is a somewhat safer unlink in that it - * allows you to ensure that there are no spaces in the - * filename to be deleted, and it also allows you to run - * a regex on the filename before excepting it. It also - * requires the file to be in the working directory. + * allows you to run a regex on the filename before + * excepting it. It also requires the file to be in + * the working directory. */ int safer_unlink(const char *pathname, const char *regx) { @@ -60,16 +59,13 @@ int safer_unlink(const char *pathname, const char *regx) regmatch_t pmatch[nmatch]; int rtn; - /* Excludes */ - const char *exc1 = ".*\\ "; - /* Name must start with working directory */ if (strncmp(pathname, working_directory, strlen(working_directory)) != 0) { Pmsg1(000, "Safe_unlink excluded: %s\n", pathname); return EROFS; } - /* Compile regex expressions */ + /* Compile regex expression */ rc = regcomp(&preg1, regx, REG_EXTENDED); if (rc != 0) { regerror(rc, &preg1, prbuf, sizeof(prbuf)); @@ -78,22 +74,6 @@ int safer_unlink(const char *pathname, const char *regx) return ENOENT; } - rc = regcomp(&pexc1, exc1, REG_EXTENDED); - if (rc != 0) { - regerror(rc, &pexc1, prbuf, sizeof(prbuf)); - Pmsg2(000, _("safe_unlink could not compile regex pattern \"%s\" ERR=%s\n"), - exc1, prbuf); - regfree(&preg1); - return ENOENT; - } - - rc = regexec(&pexc1, pathname, nmatch, pmatch, 0); - if (rc == 0) { - Pmsg1(000, "safe_unlink excluded: %s\n", pathname); - rtn = EROFS; - goto get_out; - } - /* Unlink files that match regexes */ if (regexec(&preg1, pathname, nmatch, pmatch, 0) == 0) { Dmsg1(100, "safe_unlink unlinking: %s\n", pathname); @@ -102,8 +82,6 @@ int safer_unlink(const char *pathname, const char *regx) Pmsg2(000, "safe_unlink regex failed: regex=%s file=%s\n", regx, pathname); rtn = EROFS; } - -get_out: regfree(&preg1); regfree(&pexc1); return rtn; diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index e8149722d8..24afdf19e3 100644 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -74,6 +74,9 @@ void create_jcr_key(); /* Static storage */ +/* Exclude spaces but require .mail at end */ +#define MAIL_REGEX "^[^ ]+\\.mail$" + /* Allow only one thread to tweak d->fd at a time */ static pthread_mutex_t fides_mutex = PTHREAD_MUTEX_INITIALIZER; static MSGS *daemon_msgs; /* global messages */ @@ -589,7 +592,7 @@ rem_temp_file: fclose(d->fd); d->fd = NULL; /* Exclude spaces in mail_filename */ - safer_unlink(d->mail_filename, ".*\\.mail$"); + safer_unlink(d->mail_filename, MAIL_REGEX); free_pool_memory(d->mail_filename); d->mail_filename = NULL; Dmsg0(850, "end mail or mail on error\n"); diff --git a/bacula/src/stored/stored.c b/bacula/src/stored/stored.c index f2c65beaf3..b31b8392f2 100644 --- a/bacula/src/stored/stored.c +++ b/bacula/src/stored/stored.c @@ -456,6 +456,9 @@ static int check_resources() return OK; } +/* + * Remove old .spool files written by me from the working directory. + */ static void cleanup_old_files() { DIR* dp; @@ -465,17 +468,14 @@ static void cleanup_old_files() int len = strlen(me->working_directory); POOLMEM *cleanup = get_pool_memory(PM_MESSAGE); POOLMEM *basename = get_pool_memory(PM_MESSAGE); - regex_t preg1, pexc1; + regex_t preg1; char prbuf[500]; const int nmatch = 30; regmatch_t pmatch[nmatch]; berrno be; - /* Includes */ - const char *pat1 = ".*\\.spool$"; - - /* Excludes */ - const char *exc1 = ".*\\ "; + /* Look for .spool files but don't allow spaces */ + const char *pat1 = "^[^ ]+\\.spool$"; /* Setup working directory prefix */ pm_strcpy(basename, me->working_directory); @@ -487,16 +487,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_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; } @@ -507,13 +499,11 @@ static void cleanup_old_files() if (!(dp = opendir(me->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", me->working_directory, be.bstrerror()); goto get_out1; } - //Dmsg1(000, "my_name=%s\n", my_name); - entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000); while (1) { if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) { @@ -525,13 +515,8 @@ 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 */ + /* Unlink files that match regex */ if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) { pm_strcpy(cleanup, basename); pm_strcat(cleanup, result->d_name); @@ -539,14 +524,12 @@ static void cleanup_old_files() unlink(cleanup); } } - free(entry); closedir(dp); + get_out1: - regfree(&pexc1); -get_out2: regfree(&preg1); -get_out3: +get_out2: free_pool_memory(cleanup); free_pool_memory(basename); } -- 2.39.5