fix for [#15020] File browser: going back from the root of all directories introduces...
authorCampbell Barton <ideasman42@gmail.com>
Mon, 22 Sep 2008 15:37:32 +0000 (15:37 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 22 Sep 2008 15:37:32 +0000 (15:37 +0000)
bug was introduced when fixing BLI_cleanup_dir not to write to negative character indicies.
added a BLI_parent_dir(char *path)

source/blender/blenlib/BLI_blenlib.h
source/blender/blenlib/intern/util.c
source/blender/src/editimasel.c
source/blender/src/filelist.c
source/blender/src/filesel.c

index 6a50e2de0e5b2076f1c6f1fe742476ac76b5b3e4..4bbda9709d71a5336926fe28740e8d3b44ea820a 100644 (file)
@@ -137,6 +137,9 @@ void BLI_dlist_reinit(struct DynamicList *dlist);
 void BLI_cleanup_file(const char *relabase, char *dir);
 void BLI_cleanup_dir(const char *relabase, char *dir); /* same as above but adds a trailing slash */
 
+/* go back one directory */
+int BLI_parent_dir(char *path);
+
        /**
         * Blender's path code replacement function.
         * Bases @a path strings leading with "//" by the
index 9dfe9557c1b80c4d5840295a7452760f2b473ac2..1630842be05f2286eff6d4fe852a1e2f30006fa6 100644 (file)
@@ -1058,6 +1058,21 @@ void BLI_makestringcode(const char *relfile, char *file)
        }
 }
 
+int BLI_parent_dir(char *path)
+{
+       char tmp[FILE_MAXDIR+FILE_MAXFILE+4];
+       BLI_strncpy(tmp, path, sizeof(tmp));
+       BLI_add_slash(tmp);
+       strcat(tmp, "../");
+       BLI_cleanup_dir(NULL, tmp);
+       
+       if (!BLI_testextensie(tmp, "../")) {
+               BLI_strncpy(path, tmp, sizeof(tmp));    
+               return 1;
+       } else {
+               return 0;
+       }
+}
 
 int BLI_convertstringframe(char *path, int frame)
 {
index 97ddc2e0f1df1f94be238ffe7bc3917ea05b09b5..8335af21247c374690cf70969d2d5f7851219060 100644 (file)
@@ -959,10 +959,15 @@ void winqreadimaselspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                                {
                                                        error("Path too long, cannot enter this directory");
                                                } else {
-                                                       strcat(simasel->dir, file->relname);
-                                                       strcat(simasel->dir,"/");
-                                                       simasel->file[0] = '\0';
-                                                       BLI_cleanup_dir(G.sce, simasel->dir);
+                                                       if (strcmp(file->relname, "..")==0) {
+                                                               /* avoids /../../ */
+                                                               BLI_parent_dir(simasel->dir);
+                                                       } else {
+                                                               strcat(simasel->dir, file->relname);
+                                                               strcat(simasel->dir,"/");
+                                                               simasel->file[0] = '\0';
+                                                               BLI_cleanup_dir(G.sce, simasel->dir);
+                                                       }
                                                        BIF_filelist_setdir(simasel->files, simasel->dir);
                                                        BIF_filelist_free(simasel->files);
                                                        simasel->active_file = -1;
index 589afb7f75f4669bfe32c2693870a664f6177f92..c2c0f00885f80ebf00d8c449a0c3a5335d693fe4 100644 (file)
@@ -629,35 +629,7 @@ int BIF_filelist_empty(struct FileList* filelist)
 
 void BIF_filelist_parent(struct FileList* filelist)
 {
-#ifdef WIN32
-       char c = '\\';
-#else
-       char c = '/';
-#endif
-       char *dir = filelist->dir;
-       size_t len = strlen(dir);
-       
-       while( (len > 0) && (dir[len-1] == c)  )
-       {
-               --len;
-               dir[len] = '\0';
-       }
-       while ( (len > 0) && (dir[len-1] != c) )
-       {
-               --len;
-               dir[len] = '\0';
-       }
-       if (len == 0)
-       {
-               dir[0] = c; dir[1] = '\0';
-       }
-#ifdef WIN32
-       strcat(filelist->dir, "\\");
-#else
-       strcat(filelist->dir, "/");
-#endif
-       
-       BLI_cleanup_dir(G.sce, filelist->dir);
+       BLI_parent_dir(filelist->dir);
        BLI_make_exist(filelist->dir);
        BIF_filelist_readdir(filelist);
 }
index cdb001d919346a74fa19a21a563ce436994246cf..22f7e577ad9b8709240be2e1b0dcbac25f86f67e 100644 (file)
@@ -555,54 +555,18 @@ static void split_sfile(SpaceFile *sfile, char *s1)
 
 void parent(SpaceFile *sfile)
 {
-       short a;
-       char *dir;
+       char path[sizeof(sfile->dir)+4];
        
        /* if databrowse: no parent */
        if(sfile->type==FILE_MAIN && filesel_has_func(sfile)) return;
-
-       dir= sfile->dir;
        
+       BLI_parent_dir(sfile->dir);
+       
+       if (sfile->type!=FILE_MAIN && (sfile->dir[0]=='\0'))
 #ifdef WIN32
-       if( (a = strlen(dir)) ) {                               /* remove all '/' at the end */
-               while(dir[a-1] == '\\') {
-                       a--;
-                       dir[a] = 0;
-                       if (a<=0) break;
-               }
-       }
-       if( (a = strlen(dir)) ) {                               /* then remove all until '/' */
-               while(dir[a-1] != '\\') {
-                       a--;
-                       dir[a] = 0;
-                       if (a<=0) break;
-               }
-       }
-       if( (a = strlen(dir)) ) {
-               if (dir[a-1] != '\\') strcat(dir,"\\");
-       }
-       else if(sfile->type!=FILE_MAIN) { 
-               get_default_root(dir);
-       }
+               get_default_root(sfile->dir);
 #else
-       if( (a = strlen(dir)) ) {                               /* remove all '/' at the end */
-               while(dir[a-1] == '/') {
-                       a--;
-                       dir[a] = 0;
-                       if (a<=0) break;
-               }
-       }
-       if( (a = strlen(dir)) ) {                               /* then remove until '/' */
-               while(dir[a-1] != '/') {
-                       a--;
-                       dir[a] = 0;
-                       if (a<=0) break;
-               }
-       }
-       if ( (a = strlen(dir)) ) {
-               if (dir[a-1] != '/') strcat(dir,"/");
-       }
-       else if(sfile->type!=FILE_MAIN) strcpy(dir,"/");
+               strcpy(sfile->dir,"/");
 #endif
        
        /* to be sure */
@@ -1914,9 +1878,14 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                                {
                                                        error("Path too long, cannot enter this directory");
                                                } else {
-                                                       strcat(sfile->dir, sfile->filelist[act].relname);
-                                                       strcat(sfile->dir,"/");
-                                                       BLI_cleanup_dir(G.sce, sfile->dir);
+                                                       if (strcmp(sfile->filelist[act].relname, "..")==0) {
+                                                               /* avoids /../../ */
+                                                               BLI_parent_dir(sfile->dir);
+                                                       } else {
+                                                               strcat(sfile->dir, sfile->filelist[act].relname);
+                                                               strcat(sfile->dir,"/");
+                                                               BLI_cleanup_dir(G.sce, sfile->dir);
+                                                       }
                                                        freefilelist(sfile);                                            
                                                        sfile->ofs= 0;
                                                        do_draw= 1;