Changeset: a96d4c2dabd5 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=a96d4c2dabd5
Modified Files:
        gdk/gdk_logger.c
        gdk/gdk_storage.c
        gdk/gdk_utils.c
        gdk/gdk_utils.h
Branch: transaction-replication
Log Message:

Remove code duplication when splitting parent and last directory from path 
string
- Add a new function that does that GDKextractParentAndLastDirFromPath
- GDKfileopen now simply calls GDKfilepath_long
- logger_set_logdir_path now uses GDKextractParentAndLastDirFromPath
While there:
- Fix ptrdiff_t, but just removing the incorrest use and keeping only one in 
GDKextractParentAndLastDirFromPath
- Add a lot more docs what actually happens in these functions.


diffs (193 lines):

diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1397,38 +1397,30 @@ logger_find_persistent_catalog(logger *l
  * Returns the role of the dbfarm containing the logdir.
  */
 static int
-logger_set_logdir_path(char *filename, char *fn, char *logdir, int shared)
-{
+logger_set_logdir_path(char *filename, char *fn, char *logdir, int shared) {
        int role = PERSISTENT; /* default role is persistent, i.e. the default 
dbfarm */
-       char *last_dir_full;
-       int last_dirsep_index = 0;
-       char *logdir_name;
-       char *logdir_parent_path;
 
        if (MT_path_absolute(logdir)) {
-               last_dir_full = strrchr(logdir, DIR_SEP);
-               last_dirsep_index = last_dir_full - logdir;
+               char logdir_parent_path[BUFSIZ] = "";
+               char logdir_name[BUFSIZ] = "";
+               /* split the logdir string into absolute parent dir path and 
(relative) log dir name */
+               if (GDKextractParentAndLastDirFromPath(logdir, 
logdir_parent_path, logdir_name)) {
+                       /* set the new relative logdir locaiton including the 
logger function name subdir */
+                       snprintf(filename, BUFSIZ, "%s%c%s%c", logdir_name, 
DIR_SEP, fn, DIR_SEP);
 
-               /* split the logdir string into absolute parent dir path and 
(relative) log dir name */
-               logdir_name = (char*)malloc(strlen(last_dir_full));
-               strncpy(logdir_name, last_dir_full + 1, strlen(logdir));
-               logdir_name[strlen(last_dir_full) - 1] = (char)0;
-
-               snprintf(filename, BUFSIZ, "%s%c%s%c", logdir_name, DIR_SEP, 
fn, DIR_SEP);
-
-               logdir_parent_path = (char*)malloc(last_dirsep_index + 1);
-               strncpy(logdir_parent_path, logdir, last_dirsep_index);
-               logdir_parent_path[last_dirsep_index] = (char)0;
-
-               /* add a new dbfarm for the logger directory using the parent 
dir path,
-                * assuming it is set, s.t. the logs are stored in a location 
other than the default dbfarm,
-                * or at least it appears so to (multi)dbfarm aware functions */
-               if (!shared) {
-                       role = LOG_DIR;
+                       /* add a new dbfarm for the logger directory using the 
parent dir path,
+                        * assuming it is set, s.t. the logs are stored in a 
location other than the default dbfarm,
+                        * or at least it appears so to (multi)dbfarm aware 
functions */
+                       if (!shared) {
+                               role = LOG_DIR;
+                       } else {
+                               role = SHARED_LOG_DIR;
+                       }
+                       BBPaddfarm(logdir_parent_path, 1 << role);
                } else {
-                       role = SHARED_LOG_DIR;
+                       logger_fatal("logger_set_logdir_path: logdir path is 
not correct (%s)."
+                                       "Make sure you specify a valid absolute 
or relative path.\n", logdir, 0, 0);
                }
-               BBPaddfarm(logdir_parent_path, 1 << role);
        } else {
                /* just concat the logdir and fn with appropriate separators */
                snprintf(filename, BUFSIZ, "%s%c%s%c", logdir, DIR_SEP, fn, 
DIR_SEP);
diff --git a/gdk/gdk_storage.c b/gdk/gdk_storage.c
--- a/gdk/gdk_storage.c
+++ b/gdk/gdk_storage.c
@@ -99,34 +99,15 @@ GDKfilepath(int farmid, const char *dir,
 /* Same as GDKfilepath, but tries to extract a filename from multilevel dir 
paths. */
 char *
 GDKfilepath_long(int farmid, const char *dir, const char *ext) {
-       char *last_dir_with_sep;
-       ptrdiff_t last_dirsep_index;
-       char *last_dir;
-       char *last_dir_parent;
+       char last_dir_parent[BUFSIZ] = "";
+       char last_dir[BUFSIZ] = "";
 
-       assert(dir == NULL || *dir != DIR_SEP);
-
-       last_dir_with_sep = strrchr(dir, DIR_SEP);
-       if (last_dir_with_sep == NULL) {
-               /* it wasn't a path, can't work with that */
-               return NULL;
+       if (GDKextractParentAndLastDirFromPath(dir, last_dir_parent, last_dir)) 
{
+               return GDKfilepath(farmid, last_dir_parent, last_dir, ext);
        }
-       last_dirsep_index = last_dir_with_sep - dir;
-
-       /* split the dir string into absolute parent dir path and (relative) 
log dir name */
-       last_dir = (char*)malloc(strlen(last_dir_with_sep));
-       strncpy(last_dir, last_dir_with_sep + 1, strlen(dir));
-       last_dir[strlen(last_dir_with_sep) - 1] = (char)0;
-
-       last_dir_parent = (char*)malloc(last_dirsep_index + 1);
-       strncpy(last_dir_parent, dir, last_dirsep_index);
-       last_dir_parent[last_dirsep_index] = (char)0;
-
-       return GDKfilepath(farmid, last_dir_parent, last_dir, ext);
+       return NULL;
 }
 
-
-
 int
 GDKcreatedir(const char *dir)
 {
@@ -251,39 +232,18 @@ FILE *
 GDKfileopen(int farmid, const char * dir, const char *name, const char 
*extension, const char *mode) {
        char *path;
 
-       /* it is possible for nme and extension to be null, if provided in the 
dir path */
-       if ((dir == NULL) || (*dir == 0)) {
-               return NULL;
-       }
        /* if name is null, try to get one from dir (in case it was a path) */
        if ((name == NULL) || (*name == 0)) {
-               char *last_dir_with_sep;
-               ptrdiff_t last_dirsep_index;
-               char *last_dir;
-               char *last_dir_parent;
-
-               last_dir_with_sep = strrchr(dir, DIR_SEP);
-               if (last_dir_with_sep == NULL) {
-                       /* it wasn't a path, can't work with that */
-                       return NULL;
-               }
-               last_dirsep_index = last_dir_with_sep - dir;
-
-               /* split the dir string into absolute parent dir path and 
(relative) log dir name */
-               last_dir = (char*)malloc(strlen(last_dir_with_sep));
-               strncpy(last_dir, last_dir_with_sep + 1, strlen(dir));
-               last_dir[strlen(last_dir_with_sep) - 1] = (char)0;
-
-               last_dir_parent = (char*)malloc(last_dirsep_index + 1);
-               strncpy(last_dir_parent, dir, last_dirsep_index);
-               last_dir_parent[last_dirsep_index] = (char)0;
-
-               path = GDKfilepath(farmid, last_dir_parent, last_dir, 
extension);
+               path = GDKfilepath_long(farmid, dir, extension);
        } else {
                path = GDKfilepath(farmid, dir, name, extension);
        }
 
-       return fopen(path, mode);
+       if (path != NULL) {
+        IODEBUG THRprintf(GDKstdout, "#GDKfileopen(%s)\n", path);
+               return fopen(path, mode);
+       }
+       return NULL;
 }
 
 /*
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1874,3 +1874,30 @@ GDKversion(void)
        return (_gdk_version_string);
 }
 
+/**
+ * Extracts the last directory from a path string, if possible.
+ * Stores the parent directory (path) in last_dir_parent and
+ * the last directory (name) without a leading separators in last_dir.
+ * Returns 1 for success, 0 on failure.
+ */
+int
+GDKextractParentAndLastDirFromPath(const char *path, char *last_dir_parent, 
char *last_dir) {
+       char *last_dir_with_sep;
+       ptrdiff_t last_dirsep_index;
+
+       if (path == NULL || *path == 0) {
+               return 0;
+       }
+
+       last_dir_with_sep = strrchr(path, DIR_SEP);
+       if (last_dir_with_sep == NULL) {
+               /* it wasn't a path, can't work with that */
+               return 0;
+       }
+       last_dirsep_index = last_dir_with_sep - path;
+       /* split the dir string into absolute parent dir path and (relative) 
log dir name */
+       strncpy(last_dir, last_dir_with_sep + 1, strlen(path));
+       strncpy(last_dir_parent, path, last_dirsep_index);
+
+       return 1;
+}
diff --git a/gdk/gdk_utils.h b/gdk/gdk_utils.h
--- a/gdk/gdk_utils.h
+++ b/gdk/gdk_utils.h
@@ -103,4 +103,6 @@ gdk_export int GDKexiting(void);
 
 gdk_export const char *GDKversion(void);
 
+gdk_export int GDKextractParentAndLastDirFromPath(const char *path, char 
*last_dir_parent, char *last_dir);
+
 #endif /* _GDK_UTILS_H_ */
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to