Commit: 8a17918555d9ba8c7f420deda35b77ffa8d3850e
Author: Bastien Montagne
Date:   Tue Jul 14 18:48:51 2015 +0200
Branches: master
https://developer.blender.org/rB8a17918555d9ba8c7f420deda35b77ffa8d3850e

Fix T45424: Blender able to create folders with invalid characters at the end 
of the name.

In fact, filebrowser was not making any checks for invalid file/dir names here!

Added checks in the three places that should be protected:
* Renaming.
* Creating dirs.
* Typing in filename field.

===================================================================

M       source/blender/editors/space_file/file_draw.c
M       source/blender/editors/space_file/file_ops.c

===================================================================

diff --git a/source/blender/editors/space_file/file_draw.c 
b/source/blender/editors/space_file/file_draw.c
index be602e7..7863833 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -428,6 +428,7 @@ static void renamebutton_cb(bContext *C, void 
*UNUSED(arg1), char *oldname)
 
        BLI_make_file_string(G.main->name, orgname, sfile->params->dir, 
oldname);
        BLI_strncpy(filename, sfile->params->renameedit, sizeof(filename));
+       BLI_filename_make_safe(filename);
        BLI_make_file_string(G.main->name, newname, sfile->params->dir, 
filename);
 
        if (!STREQ(orgname, newname)) {
diff --git a/source/blender/editors/space_file/file_ops.c 
b/source/blender/editors/space_file/file_ops.c
index bfe8be9..dddda43 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -1598,7 +1598,7 @@ int file_directory_new_exec(bContext *C, wmOperator *op)
 {
        char name[FILE_MAXFILE];
        char path[FILE_MAX];
-       int generate_name = 1;
+       bool generate_name = true;
        PropertyRNA *prop;
 
        wmWindowManager *wm = CTX_wm_manager(C);
@@ -1613,7 +1613,9 @@ int file_directory_new_exec(bContext *C, wmOperator *op)
 
        if ((prop = RNA_struct_find_property(op->ptr, "directory"))) {
                RNA_property_string_get(op->ptr, prop, path);
-               if (path[0] != '\0') generate_name = 0;
+               if (path[0] != '\0') {
+                       generate_name = false;
+               }
        }
 
        if (generate_name) {
@@ -1623,10 +1625,23 @@ int file_directory_new_exec(bContext *C, wmOperator *op)
                        return OPERATOR_CANCELLED;
                }
        }
+       else { /* We assume we are able to generate a valid name! */
+               char org_path[FILE_MAX];
+
+               BLI_strncpy(org_path, path, sizeof(org_path));
+               if (BLI_path_make_safe(path)) {
+                       BKE_reportf(op->reports, RPT_WARNING, "'%s' given path 
is OS-invalid, creating '%s' path instead",
+                                   org_path, path);
+               }
+       }
 
        /* create the file */
-       BLI_dir_create_recursive(path);
+       if (!BLI_dir_create_recursive(path)) {
+               BKE_report(op->reports, RPT_ERROR, "Could not create new 
folder");
+               return OPERATOR_CANCELLED;
+       }
 
+       /* Should no more be needed, now that BLI_dir_create_recursive returns 
a success state - but kept just in case. */
        if (!BLI_exists(path)) {
                BKE_report(op->reports, RPT_ERROR, "Could not create new 
folder");
                return OPERATOR_CANCELLED;
@@ -1674,6 +1689,7 @@ void FILE_OT_directory_new(struct wmOperatorType *ot)
 }
 
 
+/* TODO This should go to BLI_path_utils. */
 static void file_expand_directory(bContext *C)
 {
        SpaceFile *sfile = CTX_wm_space_file(C);
@@ -1714,6 +1730,7 @@ static void file_expand_directory(bContext *C)
        }
 }
 
+/* TODO check we still need this, it's annoying to have OS-specific code 
here... :/ */
 #if defined(WIN32)
 static bool can_create_dir(const char *dir)
 {
@@ -1796,6 +1813,8 @@ void file_filename_enter_handle(bContext *C, void 
*UNUSED(arg_unused), void *arg
                matched_file[0] = '\0';
                filepath[0] = '\0';
 
+               BLI_filename_make_safe(sfile->params->file);
+
                file_expand_directory(C);
 
                matches = file_select_match(sfile, sfile->params->file, 
matched_file);
@@ -1971,7 +1990,7 @@ static int file_rename_exec(bContext *C, wmOperator 
*UNUSED(op))
        if (sfile->params) {
                int idx = sfile->params->highlight_file;
                int numfiles = filelist_numfiles(sfile->files);
-               if ( (0 <= idx) && (idx < numfiles) ) {
+               if ((0 <= idx) && (idx < numfiles)) {
                        struct direntry *file = filelist_file(sfile->files, 
idx);
                        filelist_select_file(sfile->files, idx, FILE_SEL_ADD, 
FILE_SEL_EDITING, CHECK_ALL);
                        BLI_strncpy(sfile->params->renameedit, file->relname, 
FILE_MAXFILE);

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to