Author: nkinder
Update of /cvs/dirsec/adminserver/lib/libdsa In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv17962/lib/libdsa
Modified Files: dsalib_conf.c dsalib_location.c dsalib_util.c Log Message: Resolves: 258341 Summary: Fix rundir and instancedir location functions in dsalib.
Index: dsalib_conf.c =================================================================== RCS file: /cvs/dirsec/adminserver/lib/libdsa/dsalib_conf.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- dsalib_conf.c 24 Jul 2007 20:10:18 -0000 1.4 +++ dsalib_conf.c 31 Aug 2007 17:01:38 -0000 1.5 @@ -104,6 +104,7 @@ {"nsslapd-localuser"}, {"nsslapd-bakdir"}, {"nsslapd-tmpdir"}, +{"nsslapd-instancedir"}, {0} };
Index: dsalib_location.c =================================================================== RCS file: /cvs/dirsec/adminserver/lib/libdsa/dsalib_location.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- dsalib_location.c 24 Jul 2007 20:10:18 -0000 1.4 +++ dsalib_location.c 31 Aug 2007 17:01:38 -0000 1.5 @@ -41,11 +41,11 @@ char *ds_name; static char instance_dir[PATH_MAX];
- if ( (ds_name = ds_get_server_name()) == NULL ) + if ( (ds_name = ds_get_server_name()) == NULL ) { return(NULL); - - PR_snprintf(instance_dir, sizeof(instance_dir), "%s/%s", DSLIBDIR, ds_name); - return(instance_dir); + } else { + return ds_get_config_value(DS_INSTDIR); + } }
/* @@ -90,12 +90,43 @@ DS_EXPORT_SYMBOL char * ds_get_run_dir() { - char *rundir; + char *rundir_env = NULL; + static char rundir[PATH_MAX]; + char *inst_dir = NULL; + char *start_script = NULL; + char *p = NULL; + char line[BIG_LINE]; + FILE *fp = NULL;
- if (rundir = getenv("DS_RUN_DIR")) { - return (rundir); + if (rundir_env = getenv("DS_RUN_DIR")) { + return (rundir_env); } else { - return (PIDDIR);; + /* Find the run dir from the start script */ + inst_dir = ds_get_instance_dir(); + start_script = PR_smprintf("%s%cstart-slapd", inst_dir, FILE_PATHSEP); + fp = fopen(start_script, "r"); + if (fp) { + while(fgets(line, BIG_LINE, fp)) { + /* Find line starting with PIDFILE */ + if (strncmp(line, "PIDFILE", 7) == 0) { + /* Chop off the pidfile name to get the run dir */ + if (p = strrchr(line, '/')) { + *p = '\0'; + PR_snprintf(rundir, sizeof(rundir), "%s", line + 8); + } + break; + } + } + fclose(fp); + } + + PR_smprintf_free(start_script); + + if (rundir[0] != '\0') { + return (rundir); + } else { + return NULL; + } } }
Index: dsalib_util.c =================================================================== RCS file: /cvs/dirsec/adminserver/lib/libdsa/dsalib_util.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- dsalib_util.c 24 Jul 2007 20:10:18 -0000 1.2 +++ dsalib_util.c 31 Aug 2007 17:01:38 -0000 1.3 @@ -52,8 +52,6 @@
#define LOGFILEENVVAR "DEBUG_LOGFILE" /* used for logfp */
-static int internal_rm_rf(const char *path, DS_RM_RF_ERR_FUNC ds_rm_rf_err_func, void *arg); - /* return a FILE * opened in append mode to the log file caller must use fclose to close it */ @@ -840,100 +838,6 @@ return 0; }
-static void -rm_db_dirs(char *fullpath, DS_RM_RF_ERR_FUNC ds_rm_rf_err_func, void *arg) -{ - FILE *fp = fopen(fullpath, "r"); - char buf[2][MAXPATHLEN]; - char *bufp, *nextbufp; - char *retp; - int readit = 0; - - if (fp == NULL) - { - ds_rm_rf_err_func(fullpath, "opening the config file", arg); - return; - } - - bufp = buf[0]; *bufp = '\0'; - nextbufp = buf[1]; *nextbufp = '\0'; - - while (readit || (retp = fgets(bufp, MAXPATHLEN, fp)) != NULL) - { - int len = strlen(bufp); - int type = -1; - char *p, *q; - - if (strstr(bufp, "nsslapd-directory")) - type = DB_DIRECTORY; - else if (strstr(bufp, "nsslapd-db-home-directory")) - type = DB_HOME_DIRECTORY; - else if (strstr(bufp, "nsslapd-db-logdirectory")) - type = DB_LOGDIRECTORY; - else if (strstr(bufp, "nsslapd-changelogdir")) - type = DB_CHANGELOGDIRECTORY; - else - { - readit = 0; - continue; - } - - p = bufp + len; - - while ((retp = fgets(nextbufp, MAXPATHLEN, fp)) != NULL) - { - int thislen; - if (*nextbufp == ' ') - { - thislen = strlen(nextbufp); - len += thislen; - if (len < MAXPATHLEN) - { - strncpy(p, nextbufp, thislen); - p += thislen; - } - /* else too long as a path. ignore it */ - } - else - break; - } - if (retp == NULL) /* done */ - break; - - p = strchr(bufp, ':'); - if (p == NULL) - { - char *tmpp = bufp; - bufp = nextbufp; - nextbufp = tmpp; - readit = 1; - continue; - } - - while (*(++p) == ' ') ; - - q = p + strlen(p) - 1; - while (*q == ' ' || *q == '\t' || *q == '\n') - q--; - *(q+1) = '\0'; - - switch (type) - { - case DB_DIRECTORY: - case DB_LOGDIRECTORY: - case DB_CHANGELOGDIRECTORY: - if (is_fullpath(p)) - internal_rm_rf(p, ds_rm_rf_err_func, NULL); - break; - case DB_HOME_DIRECTORY: - internal_rm_rf(p, ds_rm_rf_err_func, NULL); - break; - } - } - - fclose(fp); -} - static char * get_dir_from_startslapd(char *loc, char *keyword) { @@ -971,134 +875,6 @@ return dir; }
-/* this function will recursively remove a directory hierarchy from the file - system, like "rm -rf" - In order to handle errors, the user supplies a callback function. When an - error occurs, the callback function is called with the file or directory name - and the system errno. The callback function should return TRUE if it wants - to continue or FALSE if it wants the remove aborted. - The error callback should use PR_GetError and/or PR_GetOSError to - determine the cause of the failure -*/ -/* you could locate db dirs non standard location - we should remove them, as well. -*/ -static int -internal_rm_rf(const char *path, DS_RM_RF_ERR_FUNC ds_rm_rf_err_func, void *arg) -{ - struct PRFileInfo prfi; - int retval = 0; - - if (PR_GetFileInfo(path, &prfi) != PR_SUCCESS) { - if (!ds_rm_rf_err_func(path, "reading directory", arg)) { - return 1; - } - } - - if (prfi.type == PR_FILE_DIRECTORY) - { - PRDir *dir; - PRDirEntry *dirent; - - if (!(dir = PR_OpenDir(path))) { - if (!ds_rm_rf_err_func(path, "opening directory", arg)) { - return 1; - } - return 0; - } - - while ((dirent = PR_ReadDir(dir, PR_SKIP_BOTH))) { - char *fullpath = PR_smprintf("%s%c%s", path, FILE_PATHSEP, dirent->name); - if (PR_GetFileInfo(fullpath, &prfi) != PR_SUCCESS) { - if (!ds_rm_rf_err_func(fullpath, "reading file", arg)) { - PR_smprintf_free(fullpath); - PR_CloseDir(dir); - return 1; - } /* else just continue */ - } else if (prfi.type == PR_FILE_DIRECTORY) { - retval = internal_rm_rf(fullpath, ds_rm_rf_err_func, arg); - if (retval) { /* non zero return means stop */ - PR_smprintf_free(fullpath); - break; - } - } else { - /* FHS changes the directory structure. - * Config dir is no longer in the instance dir. - * The info should be found in start-slapd, - * therefore get the path from the file here. - */ - if (0 == strcmp(dirent->name, "start-slapd")) { - char *config_dir = ds_get_config_dir(); - char *run_dir = ds_get_run_dir(); - if (NULL == config_dir || '\0' == *config_dir) { - config_dir = get_dir_from_startslapd(fullpath, DS_CONFIG_DIR); - } - if (NULL == run_dir || '\0' == *run_dir) { - char *ptr = NULL; - run_dir = get_dir_from_startslapd(fullpath, PIDFILE); - ptr = strrchr(run_dir, FILE_PATHSEP); - if (NULL != ptr) { - *ptr = '\0'; /* equiv to dirname */ - } - } - if (NULL != run_dir) { - internal_rm_rf(run_dir, ds_rm_rf_err_func, NULL); - free(run_dir); - } - if (NULL != config_dir) { - char *lock_dir = get_dir_from_config(config_dir, DS_CONFIG_LOCKDIR); - char *err_log = get_dir_from_config(config_dir, DS_CONFIG_ERRLOG); - - if (NULL != lock_dir) { - internal_rm_rf(lock_dir, ds_rm_rf_err_func, NULL); - free(lock_dir); - } - if (NULL != err_log) { - char *ptr = strrchr(err_log, FILE_PATHSEP); - if (NULL != ptr) { - *ptr = '\0'; /* equiv to 'dirname' */ - internal_rm_rf(err_log, ds_rm_rf_err_func, NULL); - } - free(err_log); - } - /* removing db dirs */ - rm_db_dirs(config_dir, ds_rm_rf_err_func, arg); - - /* removing config dir */ - internal_rm_rf(config_dir, ds_rm_rf_err_func, NULL); - } - } - /* - * When the file is the config file, - * check if db dir is in the instance dir or not. - * If db dir exists in the instance dir, it's an old structure. - * Let's clean the old db here, as well. - */ - if (0 == strcmp(dirent->name, DS_CONFIG_FILE)) { - rm_db_dirs(fullpath, ds_rm_rf_err_func, arg); - } - - if (PR_Delete(fullpath) != PR_SUCCESS) { - if (!ds_rm_rf_err_func(fullpath, "deleting file", arg)) { - PR_smprintf_free(fullpath); - PR_CloseDir(dir); - return 1; - } - } - } - PR_smprintf_free(fullpath); - } - PR_CloseDir(dir); - if (PR_RmDir(path) != PR_SUCCESS) { - if (!ds_rm_rf_err_func(path, "removing directory", arg)) { - retval = 1; - } - } - } - - return retval; -} - static int default_err_func(const char *path, const char *op, void *arg) { @@ -1119,38 +895,6 @@ return 1; /* just continue */ }
-/* dir: instance dir, e.g., "$NETSITE_ROOT/slapd-<id>" */ -DS_EXPORT_SYMBOL int -ds_rm_rf(const char *dir, DS_RM_RF_ERR_FUNC ds_rm_rf_err_func, void *arg) -{ - struct PRFileInfo prfi; - - if (!dir) { - ds_send_error("Could not remove NULL directory name", 1); - return 1; - } - - if (!ds_rm_rf_err_func) { - ds_rm_rf_err_func = default_err_func; - } - - if (PR_GetFileInfo(dir, &prfi) != PR_SUCCESS) { - if (ds_rm_rf_err_func(dir, "reading directory", arg)) { - return 0; - } else { - return 1; - } - } - if (prfi.type != PR_FILE_DIRECTORY) { - char *msg = PR_smprintf("Cannot remove directory %s because it is not a directory", dir); - ds_send_error(msg, 0); - PR_smprintf_free(msg); - return 1; - } - - return internal_rm_rf(dir, ds_rm_rf_err_func, arg); -} - DS_EXPORT_SYMBOL int ds_remove_reg_key(void *base, const char *format, ...) {
389-commits@lists.fedoraproject.org