From 18572520334b54c3632672934738a08bd1b3d481 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 5 Jun 2018 14:18:59 +0300 Subject: [PATCH] MXS-1749 Delete old process datadir on startup if it still exists When starting MaxScale it deletes all of the old process datadirs that might have not been cleaned up due to unexpected exit of the program. --- server/core/gateway.cc | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/server/core/gateway.cc b/server/core/gateway.cc index 8d273d0f8..dc5e19a19 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -626,14 +626,20 @@ int ntfw_cb(const char* filename, int fileflags, struct FTW* pfwt) { - int rc = remove(filename); + int rc = 0; + int datadir_len = strlen(get_datadir()); + std::string filename_string(filename + datadir_len); - if (rc != 0) + if (strncmp(filename_string.c_str(), "/data", 5) == 0) { - int eno = errno; - errno = 0; - MXS_ERROR("Failed to remove the data directory %s of MaxScale due to %d, %s.", - datadir, eno, mxs_strerror(eno)); + rc = remove(filename); + if (rc != 0) + { + int eno = errno; + errno = 0; + MXS_ERROR("Failed to remove the data directory %s of MaxScale due to %d, %s.", + filename_string.c_str(), eno, mxs_strerror(eno)); + } } return rc; } @@ -657,6 +663,13 @@ void cleanup_process_datadir() } } +void cleanup_old_process_datadirs() +{ + int depth = 1; + int flags = FTW_CHDIR | FTW_DEPTH | FTW_MOUNT; + nftw(get_datadir(), ntfw_cb, depth, flags); +} + static void write_footer(void) { file_write_footer(stdout); @@ -1983,7 +1996,7 @@ int main(int argc, char **argv) #ifdef SS_DEBUG MXS_NOTICE("Commit: %s", MAXSCALE_COMMIT); #endif - + cleanup_old_process_datadirs(); if (!cnf->config_check) { /*