Get rid of PATH_MAX in Ghost System X11
authorSergey Sharybin <sergey.vfx@gmail.com>
Tue, 20 Aug 2013 08:33:04 +0000 (08:33 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Tue, 20 Aug 2013 08:33:04 +0000 (08:33 +0000)
The reason of this is because PATH_MAX is not guaranteed
to be defined on all platforms and Hurd doesn't define it.

So either we need to support arbitrary long file path or
we need to define own maximum path length.

The rule here would be:

- If it's not big trouble to support arbitrary long paths
  (i.e. in ghost by using std::string instead of char*)
  then arbitrary long path shall be implemented.

- For other cases to use PATH_MAX please include BLI_fileops.h
  which takes care of making sure PATH_MAX is defined.

Additional change: get rid of own changes made yesterday
which were supposed to make storage.c work fine in cases
PATH_MAX is not define, but on the second though it lead
to unneeded complication of the code.

Thanks Campbell for review!

intern/ghost/intern/GHOST_SystemPathsX11.cpp
source/blender/blenlib/BLI_fileops.h
source/blender/blenlib/BLI_winstuff.h
source/blender/blenlib/intern/storage.c

index 35bebd5..50eb68d 100644 (file)
@@ -42,7 +42,9 @@
 #include <cstdlib>   /* for exit */
 
 #include <pwd.h>     /* for get home without use getenv() */
-#include <limits.h>  /* for PATH_MAX */
+#include <string>
+
+using std::string;
 
 #ifdef PREFIX
 static const char *static_path = PREFIX "/share";
@@ -62,9 +64,8 @@ const GHOST_TUns8 *GHOST_SystemPathsX11::getSystemDir(int, const char *versionst
 {
        /* no prefix assumes a portable build which only uses bundled scripts */
        if (static_path) {
-               static char system_path[PATH_MAX];
-               snprintf(system_path, sizeof(system_path), "%s/blender/%s", static_path, versionstr);
-               return (GHOST_TUns8 *)system_path;
+               static string system_path = string(static_path) + "/blender/" + versionstr;
+               return (GHOST_TUns8 *)system_path.c_str();
        }
 
        return NULL;
@@ -72,36 +73,41 @@ const GHOST_TUns8 *GHOST_SystemPathsX11::getSystemDir(int, const char *versionst
 
 const GHOST_TUns8 *GHOST_SystemPathsX11::getUserDir(int version, const char *versionstr) const
 {
-       static char user_path[PATH_MAX];
+       static string user_path = "";
 
        /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
         * operator works we give a different path depending on the requested version */
        if (version < 264) {
-               const char *home = getenv("HOME");
-
-               if (home) {
-                       snprintf(user_path, sizeof(user_path), "%s/.blender/%s", home, versionstr);
-                       return (GHOST_TUns8 *)user_path;
+               if (user_path.empty()) {
+                       const char *home = getenv("HOME");
+
+                       if (home) {
+                               user_path = string(home) + "/.blender/" + versionstr;
+                       }
+                       else {
+                               return NULL;
+                       }
                }
-
-               return NULL;
+               return (GHOST_TUns8 *)user_path.c_str();
        }
        else {
-               const char *home = getenv("XDG_CONFIG_HOME");
+               if (user_path.empty()) {
+                       const char *home = getenv("XDG_CONFIG_HOME");
 
-               if (home) {
-                       snprintf(user_path, sizeof(user_path), "%s/blender/%s", home, versionstr);
-               }
-               else {
-                       home = getenv("HOME");
+                       if (home) {
+                               user_path = string(home) + "/blender/" + versionstr;
+                       }
+                       else {
+                               home = getenv("HOME");
 
-                       if (home == NULL)
-                               home = getpwuid(getuid())->pw_dir;
+                               if (home == NULL)
+                                       home = getpwuid(getuid())->pw_dir;
 
-                       snprintf(user_path, sizeof(user_path), "%s/.config/blender/%s", home, versionstr);
+                               user_path = string(home) + "/.config/blender/" + versionstr;
+                       }
                }
 
-               return (const GHOST_TUns8 *)user_path;
+               return (const GHOST_TUns8 *)user_path.c_str();
        }
 }
 
index 0a6d95d..ca82086 100644 (file)
@@ -43,6 +43,12 @@ extern "C" {
 /* for size_t (needed on windows) */
 #include <stddef.h>
 
+#include <limits.h>  /* for PATH_MAX */
+
+#ifndef PATH_MAX
+#  define PATH_MAX 4096
+#endif
+
 struct gzFile;
 
 /* Common */
index 20a4c3c..e75ec7e 100644 (file)
@@ -100,7 +100,6 @@ extern "C" {
 // not accepted by access() on windows
 //#  define    X_OK    1
 #  define      F_OK    0
-#  define      PATH_MAX 4096
 #endif
 
 #ifndef FREE_WINDOWS
index c1645c0..e40527b 100644 (file)
@@ -70,7 +70,6 @@
 #ifdef WIN32
 #  include <io.h>
 #  include <direct.h>
-#  include <limits.h>  /* PATH_MAX */
 #  include "BLI_winstuff.h"
 #  include "utfconv.h"
 #else
@@ -255,18 +254,11 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
                                struct dirlink * dlink = (struct dirlink *) dirbase.first;
                                struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles];
                                while (dlink) {
-#ifdef PATH_MAX
-                                       char static_fullname[PATH_MAX];
-                                       char *fullname = static_fullname;
-                                       size_t fullname_size = PATH_MAX;
-#else
-                                       size_t fullname_size = strlen(dirname) + strlen(dlink->name) + 2;
-                                       char *fullname = MEM_mallocN(fullname_size, "bli_builddir fullname");
-#endif
+                                       char fullname[PATH_MAX];
                                        memset(file, 0, sizeof(struct direntry));
                                        file->relname = dlink->name;
                                        file->path = BLI_strdupcat(dirname, dlink->name);
-                                       BLI_join_dirfile(fullname, fullname_size, dirname, dlink->name);
+                                       BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name);
 // use 64 bit file size, only needed for WIN32 and WIN64. 
 // Excluding other than current MSVC compiler until able to test
 #ifdef WIN32
@@ -288,9 +280,6 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
                                        dir_ctx->nrfiles++;
                                        file++;
                                        dlink = dlink->next;
-#ifndef PATH_MAX
-                                       MEM_freeN(fullname);
-#endif
                                }
                        }
                        else {