BLI_path api, minor changes to CWD handling
authorCampbell Barton <ideasman42@gmail.com>
Thu, 8 Oct 2015 04:05:58 +0000 (15:05 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 8 Oct 2015 04:05:58 +0000 (15:05 +1100)
- BLI_current_working_dir's return value must be checked, since it may fail.
- BLI_current_working_dir now behaves like getcwd, where a too-small target will return failure.
- avoid buffer overrun with BLI_path_cwd, by taking a maxlen arg.

source/blender/blenkernel/intern/appdir.c
source/blender/blenlib/BLI_fileops.h
source/blender/blenlib/BLI_path_util.h
source/blender/blenlib/intern/path_util.c
source/blender/blenlib/intern/storage.c
source/blender/python/intern/bpy_interface.c
source/creator/creator.c
source/gameengine/GamePlayer/ghost/GPG_ghost.cpp

index ee6710e..639a502 100644 (file)
@@ -548,15 +548,7 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name)
 
                BLI_strncpy(fullname, name, maxlen);
                if (name[0] == '.') {
-                       char wdir[FILE_MAX] = "";
-                       BLI_current_working_dir(wdir, sizeof(wdir));     /* backup cwd to restore after */
-
-                       // not needed but avoids annoying /./ in name
-                       if (name[1] == SEP)
-                               BLI_join_dirfile(fullname, maxlen, wdir, name + 2);
-                       else
-                               BLI_join_dirfile(fullname, maxlen, wdir, name);
-
+                       BLI_path_cwd(fullname, maxlen);
 #ifdef _WIN32
                        BLI_path_program_extensions_add_win32(fullname, maxlen);
 #endif
index 33dae45..53ebc81 100644 (file)
@@ -89,7 +89,7 @@ bool   BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 bool   BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 bool   BLI_dir_create_recursive(const char *dir) ATTR_NONNULL();
 double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
-char  *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_NONNULL();
+char  *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 
 /* Filelist */
 
index a344c9d..1be086c 100644 (file)
@@ -136,7 +136,7 @@ bool BLI_path_frame_range(char *path, int sta, int end, int digits) ATTR_NONNULL
 bool BLI_path_frame_get(char *path, int *r_frame, int *numdigits) ATTR_NONNULL();
 void BLI_path_frame_strip(char *path, bool setsharp, char *ext) ATTR_NONNULL();
 bool BLI_path_frame_check_chars(const char *path) ATTR_NONNULL();
-bool BLI_path_cwd(char *path) ATTR_NONNULL();
+bool BLI_path_cwd(char *path, const size_t maxlen) ATTR_NONNULL();
 void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL();
 
 bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
index 658c55c..473ec1c 100644 (file)
@@ -1202,7 +1202,7 @@ bool BLI_path_abs(char *path, const char *basepath)
  * \note Should only be done with command line paths.
  * this is _not_ something blenders internal paths support like the "//" prefix
  */
-bool BLI_path_cwd(char *path)
+bool BLI_path_cwd(char *path, const size_t maxlen)
 {
        bool wasrelative = true;
        const int filelen = strlen(path);
@@ -1216,24 +1216,15 @@ bool BLI_path_cwd(char *path)
 #endif
        
        if (wasrelative) {
-               char cwd[FILE_MAX] = "";
-               BLI_current_working_dir(cwd, sizeof(cwd)); /* in case the full path to the blend isn't used */
-               
-               if (cwd[0] == '\0') {
-                       printf("Could not get the current working directory - $PWD for an unknown reason.\n");
-               }
-               else {
-                       /* uses the blend path relative to cwd important for loading relative linked files.
-                        *
-                        * cwd should contain c:\ etc on win32 so the relbase can be NULL
-                        * relbase being NULL also prevents // being misunderstood as relative to the current
-                        * blend file which isn't a feature we want to use in this case since were dealing
-                        * with a path from the command line, rather than from inside Blender */
-
+               char cwd[FILE_MAX];
+               /* in case the full path to the blend isn't used */
+               if (BLI_current_working_dir(cwd, sizeof(cwd))) {
                        char origpath[FILE_MAX];
                        BLI_strncpy(origpath, path, FILE_MAX);
-                       
-                       BLI_make_file_string(NULL, path, cwd, origpath); 
+                       BLI_join_dirfile(path, maxlen, cwd, origpath);
+               }
+               else {
+                       printf("Could not get the current working directory - $PWD for an unknown reason.\n");
                }
        }
        
index 7fdf6ec..04d5e35 100644 (file)
@@ -91,8 +91,14 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy)
 {
        const char *pwd = getenv("PWD");
        if (pwd) {
-               BLI_strncpy(dir, pwd, maxncpy);
-               return dir;
+               size_t srclen = BLI_strnlen(pwd, maxncpy);
+               if (srclen != maxncpy) {
+                       memcpy(dir, pwd, srclen);
+                       return dir;
+               }
+               else {
+                       return NULL;
+               }
        }
 
        return getcwd(dir, maxncpy);
index 61477d0..04e49f9 100644 (file)
@@ -828,7 +828,7 @@ static void bpy_module_delay_init(PyObject *bpy_proxy)
        char filename_abs[1024];
 
        BLI_strncpy(filename_abs, filename_rel, sizeof(filename_abs));
-       BLI_path_cwd(filename_abs);
+       BLI_path_cwd(filename_abs, sizeof(filename_abs));
 
        argv[0] = filename_abs;
        argv[1] = NULL;
index 86922c3..517dcb1 100644 (file)
@@ -1246,7 +1246,7 @@ static int run_python_file(int argc, const char **argv, void *data)
                /* Make the path absolute because its needed for relative linked blends to be found */
                char filename[FILE_MAX];
                BLI_strncpy(filename, argv[1], sizeof(filename));
-               BLI_path_cwd(filename);
+               BLI_path_cwd(filename, sizeof(filename));
 
                BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL));
 
@@ -1371,7 +1371,7 @@ static int load_file(int UNUSED(argc), const char **argv, void *data)
        }
 
        BLI_strncpy(filename, argv[0], sizeof(filename));
-       BLI_path_cwd(filename);
+       BLI_path_cwd(filename, sizeof(filename));
 
        if (G.background) {
                Main *bmain;
index 23d5b89..137f4cd 100644 (file)
@@ -855,7 +855,7 @@ int main(int argc, char** argv)
 
                                get_filename(argc_py_clamped, argv, filename);
                                if (filename[0])
-                                       BLI_path_cwd(filename);
+                                       BLI_path_cwd(filename, sizeof(filename));
                                
 
                                // fill the GlobalSettings with the first scene files