From 624c11d69fce647a38a8ab392f97a82ae0e2a0d4 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 4 Nov 2022 17:01:03 +1100 Subject: 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. --- source/blender/blenlib/intern/path_util.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'source') 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; } -- cgit v1.2.3