[Bf-blender-cvs] [6f3a9031f7b] master: Cleanup: BKE_appdir left paths set even when not found

Campbell Barton noreply at git.blender.org
Sun Oct 4 00:35:11 CEST 2020


Commit: 6f3a9031f7b93e7c687edde646beed9f02d920d4
Author: Campbell Barton
Date:   Sun Oct 4 01:19:24 2020 +1000
Branches: master
https://developer.blender.org/rB6f3a9031f7b93e7c687edde646beed9f02d920d4

Cleanup: BKE_appdir left paths set even when not found

Internally appdir functions would test if a path exists,
returning false if it doesn't, leaving the string set instead
of clearing it.

This is error prone as invalid paths could be used accidentally.

Since BKE_appdir_folder_id_user_notest & BKE_appdir_folder_id_version
depend on this, add a 'check_is_dir' argument so the path can be used
even when the directory can't be found.

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

M	source/blender/blenkernel/BKE_appdir.h
M	source/blender/blenkernel/intern/appdir.c

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

diff --git a/source/blender/blenkernel/BKE_appdir.h b/source/blender/blenkernel/BKE_appdir.h
index 09d74c16bc8..8e63631b9d5 100644
--- a/source/blender/blenkernel/BKE_appdir.h
+++ b/source/blender/blenkernel/BKE_appdir.h
@@ -28,14 +28,16 @@ struct ListBase;
 /* note on naming: typical _get() suffix is omitted here,
  * since its the main purpose of the API. */
 const char *BKE_appdir_folder_default(void);
-const char *BKE_appdir_folder_id_ex(const int folder_id,
-                                    const char *subfolder,
-                                    char *path,
-                                    size_t path_len);
+bool BKE_appdir_folder_id_ex(const int folder_id,
+                             const char *subfolder,
+                             char *path,
+                             size_t path_len);
 const char *BKE_appdir_folder_id(const int folder_id, const char *subfolder);
 const char *BKE_appdir_folder_id_create(const int folder_id, const char *subfolder);
 const char *BKE_appdir_folder_id_user_notest(const int folder_id, const char *subfolder);
-const char *BKE_appdir_folder_id_version(const int folder_id, const int ver, const bool do_check);
+const char *BKE_appdir_folder_id_version(const int folder_id,
+                                         const int ver,
+                                         const bool check_is_dir);
 
 bool BKE_appdir_app_is_portable_install(void);
 bool BKE_appdir_app_template_any(void);
diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
index d568bda1988..fd5c39b8054 100644
--- a/source/blender/blenkernel/intern/appdir.c
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -183,15 +183,17 @@ bool BKE_appdir_font_folder_default(
  * \param path_base: Path base, never NULL.
  * \param folder_name: First sub-directory (optional).
  * \param subfolder_name: Second sub-directory (optional).
+ * \param check_is_dir: When false, return true even if the path doesn't exist.
  *
  * \note The names for optional paths only follow other usage in this function,
- * as far as this function is concerned the names don't matter.
+ * the names don't matter for this function.
  *
  * \note If it's useful we could take an arbitrary number of paths.
  * For now usage is limited and we don't need this.
  */
 static bool test_path(char *targetpath,
                       size_t targetpath_len,
+                      const bool check_is_dir,
                       const char *path_base,
                       const char *folder_name,
                       const char *subfolder_name)
@@ -199,6 +201,12 @@ static bool test_path(char *targetpath,
   /* Only the last argument should be NULL. */
   BLI_assert(!(folder_name == NULL && (subfolder_name != NULL)));
   BLI_path_join(targetpath, targetpath_len, path_base, folder_name, subfolder_name, NULL);
+  if (check_is_dir == false) {
+#ifdef PATH_DEBUG
+    printf("\t%s using without test: %s\n", __func__, targetpath);
+#endif
+    return true;
+  }
 
   if (BLI_is_dir(targetpath)) {
 #ifdef PATH_DEBUG
@@ -210,7 +218,10 @@ static bool test_path(char *targetpath,
 #ifdef PATH_DEBUG
   printf("\t%s missing: %s\n", __func__, targetpath);
 #endif
-  // targetpath[0] = '\0';
+
+  /* Path not found, don't accidentally use it,
+   * otherwise call this function with `check_is_dir` set to false. */
+  targetpath[0] = '\0';
   return false;
 }
 
@@ -218,13 +229,20 @@ static bool test_path(char *targetpath,
  * Puts the value of the specified environment variable into *path if it exists
  * and points at a directory. Returns true if this was done.
  */
-static bool test_env_path(char *path, const char *envvar)
+static bool test_env_path(char *path, const char *envvar, const bool check_is_dir)
 {
   const char *env = envvar ? BLI_getenv(envvar) : NULL;
   if (!env) {
     return false;
   }
 
+  if (check_is_dir == false) {
+#ifdef PATH_DEBUG
+    printf("\t%s using without test: %s\n", __func__, targetpath);
+#endif
+    return true;
+  }
+
   if (BLI_is_dir(env)) {
     BLI_strncpy(path, env, FILE_MAX);
 #ifdef PATH_DEBUG
@@ -233,10 +251,13 @@ static bool test_env_path(char *path, const char *envvar)
     return true;
   }
 
-  path[0] = '\0';
 #ifdef PATH_DEBUG
   printf("\t%s env %s missing: %s\n", __func__, envvar, env);
 #endif
+
+  /* Path not found, don't accidentally use it,
+   * otherwise call this function with `check_is_dir` set to false. */
+  path[0] = '\0';
   return false;
 }
 
@@ -247,14 +268,17 @@ static bool test_env_path(char *path, const char *envvar)
  * \param targetpath: String to return path.
  * \param folder_name: Optional folder name within version-specific directory.
  * \param subfolder_name: Optional sub-folder name within folder_name.
+ *
  * \param ver: To construct name of version-specific directory within #bprogdir.
+ * \param check_is_dir: When false, return true even if the path doesn't exist.
  * \return true if such a directory exists.
  */
-static bool get_path_local(char *targetpath,
-                           size_t targetpath_len,
-                           const char *folder_name,
-                           const char *subfolder_name,
-                           const int ver)
+static bool get_path_local_ex(char *targetpath,
+                              size_t targetpath_len,
+                              const char *folder_name,
+                              const char *subfolder_name,
+                              const int ver,
+                              const bool check_is_dir)
 {
   char relfolder[FILE_MAX];
 
@@ -281,7 +305,22 @@ static bool get_path_local(char *targetpath,
   BLI_path_normalize(NULL, osx_resourses);
   path_base = osx_resourses;
 #endif
-  return test_path(targetpath, targetpath_len, path_base, blender_version_decimal(ver), relfolder);
+  return test_path(targetpath,
+                   targetpath_len,
+                   check_is_dir,
+                   path_base,
+                   blender_version_decimal(ver),
+                   relfolder);
+}
+static bool get_path_local(char *targetpath,
+                           size_t targetpath_len,
+                           const char *folder_name,
+                           const char *subfolder_name)
+{
+  const int ver = BLENDER_VERSION;
+  const bool check_is_dir = true;
+  return get_path_local_ex(
+      targetpath, targetpath_len, folder_name, subfolder_name, ver, check_is_dir);
 }
 
 /**
@@ -291,10 +330,8 @@ static bool get_path_local(char *targetpath,
 bool BKE_appdir_app_is_portable_install(void)
 {
   /* Detect portable install by the existence of `config` folder. */
-  const int ver = BLENDER_VERSION;
   char path[FILE_MAX];
-
-  return get_path_local(path, sizeof(path), "config", NULL, ver);
+  return get_path_local(path, sizeof(path), "config", NULL);
 }
 
 /**
@@ -303,43 +340,30 @@ bool BKE_appdir_app_is_portable_install(void)
  * \param targetpath: String to return path.
  * \param subfolder_name: optional name of sub-folder within folder.
  * \param envvar: name of environment variable to check folder_name.
+ * \param check_is_dir: When false, return true even if the path doesn't exist.
  * \return true if it was able to construct such a path and the path exists.
  */
-static bool get_path_environment(char *targetpath,
-                                 size_t targetpath_len,
-                                 const char *subfolder_name,
-                                 const char *envvar)
+static bool get_path_environment_ex(char *targetpath,
+                                    size_t targetpath_len,
+                                    const char *subfolder_name,
+                                    const char *envvar,
+                                    const bool check_is_dir)
 {
   char user_path[FILE_MAX];
 
-  if (test_env_path(user_path, envvar)) {
+  if (test_env_path(user_path, envvar, check_is_dir)) {
     /* Note that `subfolder_name` may be NULL, in this case we use `user_path` as-is. */
-    return test_path(targetpath, targetpath_len, user_path, subfolder_name, NULL);
+    return test_path(targetpath, targetpath_len, check_is_dir, user_path, subfolder_name, NULL);
   }
   return false;
 }
-
-/**
- * Returns the path of a folder from environment variables.
- *
- * \param targetpath: String to return path.
- * \param subfolder_name: optional name of sub-folder within folder.
- * \param envvar: name of environment variable to check folder_name.
- * \return true if it was able to construct such a path.
- */
-static bool get_path_environment_notest(char *targetpath,
-                                        size_t targetpath_len,
-                                        const char *subfolder_name,
-                                        const char *envvar)
+static bool get_path_environment(char *targetpath,
+                                 size_t targetpath_len,
+                                 const char *subfolder_name,
+                                 const char *envvar)
 {
-  char user_path[FILE_MAX];
-
-  if (test_env_path(user_path, envvar)) {
-    /* Note that `subfolder_name` may be NULL, in this case we use `user_path` as-is. */
-    BLI_path_join(targetpath, targetpath_len, user_path, subfolder_name, NULL);
-    return true;
-  }
-  return false;
+  const bool check_is_dir = true;
+  return get_path_environment_ex(targetpath, targetpath_len, subfolder_name, envvar, check_is_dir);
 }
 
 /**
@@ -349,20 +373,23 @@ static bool get_path_environment_notest(char *targetpath,
  * \param folder_name: default name of folder within user area.
  * \param subfolder_name: optional name of sub-folder within folder.
  * \param ver: Blender version, used to construct a sub-directory name.
+ * \param check_is_dir: When false, return true even if the path doesn't exist.
  * \return true if it was able to construct such a path.
  */
-static bool get_path_user(char *targetpath,
-                          size_t targetpath_len,
-                          const char *folder_name,
-                          const char *subfolder_name,
-                          const int ver)
+static bool get_path_user_ex(char *targetpath,
+                             size_t targetpath_len,
+                   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list