diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 56d82a34067..8f89fab0724 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -49,10 +49,12 @@ bool BLI_make_existing_file(const char *name); * - Won't change \a string. * - Won't create any directories. * - Doesn't use CWD, or deal with relative paths. - * - Only fill's in \a dir and \a file when they are non NULL. */ -void BLI_path_split_dir_file( - const char *string, char *dir, size_t dirlen, char *file, size_t filelen); +void BLI_path_split_dir_file(const char *string, + char *dir, + size_t dirlen, + char *file, + size_t filelen) ATTR_NONNULL(1, 2, 4); /** * Copies the parent directory part of string into `dir`, max length `dirlen`. */ diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index dc033594fb3..bb760437c26 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -246,7 +246,7 @@ bool BLI_file_is_writable(const char *filepath) else { /* file doesn't exist -- check I can create it in parent directory */ char parent[FILE_MAX]; - BLI_path_split_dir_file(filepath, parent, sizeof(parent), NULL, 0); + BLI_path_split_dir_part(filepath, parent, sizeof(parent)); #ifdef WIN32 /* windows does not have X_OK */ writable = BLI_access(parent, W_OK) == 0; diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index b9e8bbbe00c..25ecd6f911d 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1479,6 +1479,13 @@ bool BLI_path_filename_ensure(char *filepath, size_t maxlen, const char *filenam return false; } +static size_t path_split_dir_file_offset(const char *string) +{ + const char *lslash_str = BLI_path_slash_rfind(string); + const size_t lslash = lslash_str ? (size_t)(lslash_str - string) + 1 : 0; + return lslash; +} + void BLI_path_split_dir_file( const char *string, char *dir, const size_t dirlen, char *file, const size_t filelen) { @@ -1486,32 +1493,37 @@ void BLI_path_split_dir_file( memset(dir, 0xff, sizeof(*dir) * dirlen); memset(file, 0xff, sizeof(*file) * filelen); #endif - const char *lslash_str = BLI_path_slash_rfind(string); - const size_t lslash = lslash_str ? (size_t)(lslash_str - string) + 1 : 0; - - if (dir) { - if (lslash) { - /* +1 to include the slash and the last char. */ - BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1)); - } - else { - dir[0] = '\0'; - } + const size_t lslash = path_split_dir_file_offset(string); + if (lslash) { /* +1 to include the slash and the last char. */ + BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1)); } - - if (file) { - BLI_strncpy(file, string + lslash, filelen); + else { + dir[0] = '\0'; } + BLI_strncpy(file, string + lslash, filelen); } void BLI_path_split_dir_part(const char *string, char *dir, const size_t dirlen) { - BLI_path_split_dir_file(string, dir, dirlen, NULL, 0); +#ifdef DEBUG_STRSIZE + memset(dir, 0xff, sizeof(*dir) * dirlen); +#endif + const size_t lslash = path_split_dir_file_offset(string); + if (lslash) { /* +1 to include the slash and the last char. */ + BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1)); + } + else { + dir[0] = '\0'; + } } void BLI_path_split_file_part(const char *string, char *file, const size_t filelen) { - BLI_path_split_dir_file(string, NULL, 0, file, filelen); +#ifdef DEBUG_STRSIZE + memset(file, 0xff, sizeof(*file) * filelen); +#endif + const size_t lslash = path_split_dir_file_offset(string); + BLI_strncpy(file, string + lslash, filelen); } const char *BLI_path_extension_or_end(const char *filepath) diff --git a/source/blender/sequencer/intern/disk_cache.c b/source/blender/sequencer/intern/disk_cache.c index 279bd28b8a8..88ad44be134 100644 --- a/source/blender/sequencer/intern/disk_cache.c +++ b/source/blender/sequencer/intern/disk_cache.c @@ -176,7 +176,7 @@ static void seq_disk_cache_get_files(SeqDiskCache *disk_cache, char *path) } char file[FILE_MAX]; - BLI_path_split_dir_file(fl->path, NULL, 0, file, sizeof(file)); + BLI_path_split_file_part(fl->path, file, sizeof(file)); bool is_dir = BLI_is_dir(fl->path); if (is_dir && !FILENAME_IS_CURRPAR(file)) {