[Bf-blender-cvs] [624c11d69fc] master: BLI_path: remove use of BLI_path_normalize in BLI_path_parent_dir

Campbell Barton noreply at git.blender.org
Fri Nov 4 07:22:41 CET 2022


Commit: 624c11d69fce647a38a8ab392f97a82ae0e2a0d4
Author: Campbell Barton
Date:   Fri Nov 4 17:01:03 2022 +1100
Branches: master
https://developer.blender.org/rB624c11d69fce647a38a8ab392f97a82ae0e2a0d4

BLI_path: remove use of BLI_path_normalize in BLI_path_parent_dir

Normalize is no longer necessary as BLI_path_name_at_index skips
redundant path components such as "//" and "/./".

This has the advantage that the path length isn't limited to FILE_MAX.

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

M	source/blender/blenlib/intern/path_util.c

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

diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 179a1a305d1..d13f3fe5ced 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -626,17 +626,6 @@ bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char
 
 bool BLI_path_parent_dir(char *path)
 {
-  char tmp[FILE_MAX];
-
-  STRNCPY(tmp, path);
-  /* Does all the work of normalizing the path for us.
-   *
-   * NOTE(@campbellbarton): While it's possible strip text after the second last slash,
-   * this would have to be clever and skip cases like "/./" & multiple slashes.
-   * Since this ends up solving some of the same problems as #BLI_path_normalize,
-   * call this function instead of attempting to handle them separately. */
-  BLI_path_normalize(NULL, tmp);
-
   /* Use #BLI_path_name_at_index instead of checking if the strings ends with `parent_dir`
    * to ensure the logic isn't confused by:
    * - Directory names that happen to end with `..`.
@@ -644,18 +633,19 @@ bool BLI_path_parent_dir(char *path)
    *   which would cause checking for a tailing `/../` fail.
    * Extracting the span of the final directory avoids both these issues. */
   int tail_ofs = 0, tail_len = 0;
-  if (!BLI_path_name_at_index(tmp, -1, &tail_ofs, &tail_len)) {
+  if (!BLI_path_name_at_index(path, -1, &tail_ofs, &tail_len)) {
     return false;
   }
   if (tail_len == 1) {
     /* Last path is ".", as normalize should remove this, it's safe to assume failure.
      * This happens when the input a single period (possibly with slashes before or after). */
-    if (tmp[tail_ofs] == '.') {
+    if (path[tail_ofs] == '.') {
       return false;
     }
   }
 
-  memcpy(path, tmp, tail_ofs);
+  /* Input paths should already be normalized if `..` is part of the path. */
+  BLI_assert(!((tail_len == 2) && (path[tail_ofs] == '.') && (path[tail_ofs + 1] == '.')));
   path[tail_ofs] = '\0';
   return true;
 }



More information about the Bf-blender-cvs mailing list