Cleanup: remove basepath argument from BLI_path_normalize{_dir}

Keep these operations separate to simplify path handling logic & docs.
Many callers passed NULL and there were times paths were passed in which
didn't make any sense (where the paths had already been made absolute).
This commit is contained in:
Campbell Barton
2023-04-24 11:31:31 +10:00
parent 20eb682967
commit 10fc2d6d96
21 changed files with 63 additions and 62 deletions

View File

@@ -23,7 +23,7 @@ std::string normalize_directory_path(StringRef directory)
directory.data(),
/* + 1 for null terminator. */
std::min(directory.size() + 1, int64_t(sizeof(dir_normalized))));
BLI_path_normalize_dir(nullptr, dir_normalized, sizeof(dir_normalized));
BLI_path_normalize_dir(dir_normalized, sizeof(dir_normalized));
return std::string(dir_normalized);
}
@@ -34,7 +34,7 @@ std::string normalize_path(StringRefNull path, int64_t max_len)
char *buf = BLI_strdupn(path.c_str(), len);
BLI_path_slash_native(buf);
BLI_path_normalize(nullptr, buf);
BLI_path_normalize(buf);
std::string normalized_path = buf;
MEM_freeN(buf);

View File

@@ -386,7 +386,7 @@ static bool get_path_local_ex(char *targetpath,
char osx_resourses[FILE_MAX + 4 + 9];
BLI_path_join(osx_resourses, sizeof(osx_resourses), g_app.program_dirname, "..", "Resources");
/* Remove the '/../' added above. */
BLI_path_normalize(NULL, osx_resourses);
BLI_path_normalize(osx_resourses);
path_base = osx_resourses;
#endif
return test_path(targetpath,
@@ -871,7 +871,7 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name)
BLI_path_program_search(fullname, maxlen, name);
}
/* Remove "/./" and "/../" so string comparisons can be used on the path. */
BLI_path_normalize(NULL, fullname);
BLI_path_normalize(fullname);
# if defined(DEBUG)
if (!STREQ(name, fullname)) {
@@ -890,7 +890,7 @@ void BKE_appdir_program_path_init(const char *argv0)
* which must point to the Python module for data-files to be detected. */
STRNCPY(g_app.program_filepath, argv0);
BLI_path_abs_from_cwd(g_app.program_filepath, sizeof(g_app.program_filepath));
BLI_path_normalize(NULL, g_app.program_filepath);
BLI_path_normalize(g_app.program_filepath);
if (g_app.program_dirname[0] == '\0') {
/* First time initializing, the file binary path isn't valid from a Python module.

View File

@@ -441,7 +441,7 @@ static bool relative_rebase_foreach_path_cb(BPathForeachPathData *bpath_data,
return false;
}
BLI_path_normalize(NULL, filepath);
BLI_path_normalize(filepath);
/* This may fail, if so it's fine to leave absolute since the path is still valid. */
BLI_path_rel(filepath, data->basedir_dst);

View File

@@ -137,7 +137,7 @@ static bool lib_id_library_local_paths_callback(BPathForeachPathData *bpath_data
/* Path was relative and is now absolute. Remap.
* Important BLI_path_normalize runs before the path is made relative
* because it won't work for paths that start with "//../" */
BLI_path_normalize(NULL, filepath);
BLI_path_normalize(filepath);
BLI_path_rel(filepath, base_new);
BLI_strncpy(r_path_dst, filepath, FILE_MAX);
return true;

View File

@@ -323,19 +323,18 @@ void BLI_path_sequence_encode(
/**
* Remove redundant characters from \a path and optionally make absolute.
*
* \param relabase: The path this is relative to, or ignored when NULL.
* \param path: Can be any input, and this function converts it to a regular full path.
* Also removes garbage from directory paths, like `/../` or double slashes etc.
*
* \note \a path isn't protected for max string names.
*/
void BLI_path_normalize(const char *relabase, char *path) ATTR_NONNULL(2);
void BLI_path_normalize(char *path) ATTR_NONNULL(1);
/**
* Cleanup file-path ensuring a trailing slash.
*
* \note Same as #BLI_path_normalize but adds a trailing slash.
*/
void BLI_path_normalize_dir(const char *relabase, char *dir, size_t dir_maxlen) ATTR_NONNULL(2);
void BLI_path_normalize_dir(char *dir, size_t dir_maxlen) ATTR_NONNULL(1);
/**
* Make given name safe to be used in paths.

View File

@@ -112,20 +112,17 @@ void BLI_path_sequence_encode(
BLI_sprintf(string, "%s%.*d%s", head, numlen, MAX2(0, pic), tail);
}
void BLI_path_normalize(const char *relabase, char *path)
void BLI_path_normalize(char *path)
{
const char *path_orig = path;
int path_len;
ptrdiff_t a;
char *start, *eind;
if (relabase) {
BLI_path_abs(path, relabase);
}
path_len = strlen(path);
if ((relabase == NULL) && (path[0] == '/' && path[1] == '/')) {
if (path[0] == '/' && path[1] == '/') {
path = path + 2; /* Leave the initial `//` untouched. */
path_len -= 2;
@@ -277,14 +274,14 @@ void BLI_path_normalize(const char *relabase, char *path)
BLI_assert(strlen(path) == path_len);
}
void BLI_path_normalize_dir(const char *relabase, char *dir, size_t dir_maxlen)
void BLI_path_normalize_dir(char *dir, size_t dir_maxlen)
{
/* Would just create an unexpected "/" path, just early exit entirely. */
if (dir[0] == '\0') {
return;
}
BLI_path_normalize(relabase, dir);
BLI_path_normalize(dir);
BLI_path_slash_ensure(dir, dir_maxlen);
}
@@ -576,8 +573,8 @@ void BLI_path_rel(char *file, const char *relfile)
BLI_str_replace_char(file + BLI_path_unc_prefix_len(file), '\\', '/');
/* Remove `/./` which confuse the following slash counting. */
BLI_path_normalize(NULL, file);
BLI_path_normalize(NULL, temp);
BLI_path_normalize(file);
BLI_path_normalize(temp);
/* The last slash in the file indicates where the path part ends. */
lslash = BLI_path_slash_rfind(temp);
@@ -978,7 +975,7 @@ bool BLI_path_abs(char *path, const char *basepath)
BLI_strncpy(base, basepath, sizeof(base));
/* File component is ignored, so don't bother with the trailing slash. */
BLI_path_normalize(NULL, base);
BLI_path_normalize(base);
lslash = BLI_path_slash_rfind(base);
BLI_str_replace_char(base + BLI_path_unc_prefix_len(base), '\\', '/');
@@ -1010,7 +1007,7 @@ bool BLI_path_abs(char *path, const char *basepath)
#endif
/* Ensure this is after correcting for path switch. */
BLI_path_normalize(NULL, path);
BLI_path_normalize(path);
return wasrelative;
}
@@ -1710,8 +1707,8 @@ bool BLI_path_contains(const char *container_path, const char *containee_path)
BLI_path_slash_native(container_native);
BLI_path_slash_native(containee_native);
BLI_path_normalize(NULL, container_native);
BLI_path_normalize(NULL, containee_native);
BLI_path_normalize(container_native);
BLI_path_normalize(containee_native);
#ifdef WIN32
BLI_str_tolower_ascii(container_native, PATH_MAX);
@@ -1814,8 +1811,8 @@ int BLI_path_cmp_normalized(const char *p1, const char *p2)
BLI_path_slash_native(norm_p1);
BLI_path_slash_native(norm_p2);
BLI_path_normalize(NULL, norm_p1);
BLI_path_normalize(NULL, norm_p2);
BLI_path_normalize(norm_p1);
BLI_path_normalize(norm_p2);
return BLI_path_cmp(norm_p1, norm_p2);
}

View File

@@ -42,27 +42,20 @@ static char *str_replace_char_strdup(const char *str, char src, char dst)
/** \name Tests for: #BLI_path_normalize
* \{ */
#define NORMALIZE_WITH_BASEDIR(input, input_base, output_expect) \
#define NORMALIZE(input, output_expect) \
{ \
char path[FILE_MAX] = input; \
const char *input_base_test = input_base; \
if (SEP == '\\') { \
str_replace_char_with_relative_exception(path, '/', '\\'); \
input_base_test = str_replace_char_strdup(input_base_test, '/', '\\'); \
} \
BLI_path_normalize(input_base_test, path); \
BLI_path_normalize(path); \
if (SEP == '\\') { \
BLI_str_replace_char(path, '\\', '/'); \
if (input_base_test) { \
free((void *)input_base_test); \
} \
} \
EXPECT_STREQ(path, output_expect); \
} \
((void)0)
#define NORMALIZE(input, output_expect) NORMALIZE_WITH_BASEDIR(input, nullptr, output_expect)
/* #BLI_path_normalize: do nothing. */
TEST(path_util, Normalize_Nop)
{
@@ -109,7 +102,6 @@ TEST(path_util, Normalize_Parent)
{
NORMALIZE("/a/b/c/../../../", "/");
NORMALIZE("/a/../a/b/../b/c/../c/", "/a/b/c/");
NORMALIZE_WITH_BASEDIR("//../", "/a/b/c/", "/a/b/");
}
/* #BLI_path_normalize: with too many "/../", match Python's behavior. */
TEST(path_util, Normalize_UnbalancedAbsolute)
@@ -154,7 +146,6 @@ TEST(path_util, Normalize_UnbalancedRelativeTrailing)
NORMALIZE("../a/../../..", "../../..");
}
#undef NORMALIZE_WITH_BASEDIR
#undef NORMALIZE
/** \} */

View File

@@ -516,7 +516,8 @@ static Main *blo_find_main(FileData *fd, const char *filepath, const char *relab
char name1[FILE_MAX];
BLI_strncpy(name1, filepath, sizeof(name1));
BLI_path_normalize(relabase, name1);
BLI_path_abs(name1, relabase);
BLI_path_normalize(name1);
// printf("blo_find_main: relabase %s\n", relabase);
// printf("blo_find_main: original in %s\n", filepath);
@@ -2695,7 +2696,8 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
/* Make sure we have full path in lib->filepath_abs */
BLI_strncpy(lib->filepath_abs, lib->filepath, sizeof(lib->filepath));
BLI_path_normalize(fd->relabase, lib->filepath_abs);
BLI_path_abs(lib->filepath_abs, fd->relabase);
BLI_path_normalize(lib->filepath_abs);
// printf("direct_link_library: filepath %s\n", lib->filepath);
// printf("direct_link_library: filepath_abs %s\n", lib->filepath_abs);

View File

@@ -1474,13 +1474,13 @@ bool BLO_write_file(Main *mainvar,
/* Normalize the paths in case there is some subtle difference (so they can be compared). */
if (relbase_valid) {
BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
BLI_path_normalize(nullptr, dir_src);
BLI_path_normalize(dir_src);
}
else {
dir_src[0] = '\0';
}
BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
BLI_path_normalize(nullptr, dir_dst);
BLI_path_normalize(dir_dst);
/* Only for relative, not relative-all, as this means making existing paths relative. */
if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {

View File

@@ -198,10 +198,12 @@ static FileSelect file_select_do(bContext *C, int selected_idx, bool do_diropen)
}
else if (file->redirection_path) {
BLI_strncpy(params->dir, file->redirection_path, sizeof(params->dir));
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
}
else {
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
BLI_path_append_dir(params->dir, sizeof(params->dir), file->relpath);
}
@@ -1094,7 +1096,8 @@ static int bookmark_select_exec(bContext *C, wmOperator *op)
RNA_property_string_get(op->ptr, prop, entry);
BLI_strncpy(params->dir, entry, sizeof(params->dir));
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
ED_file_change_dir(C);
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_FILE_LIST, NULL);
@@ -2057,7 +2060,8 @@ static bool file_execute(bContext *C, SpaceFile *sfile)
BLI_path_parent_dir(params->dir);
}
else {
BLI_path_normalize(BKE_main_blendfile_path(bmain), params->dir);
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize(params->dir);
BLI_path_append_dir(params->dir, sizeof(params->dir), file->relpath);
}
ED_file_change_dir(C);
@@ -2221,7 +2225,8 @@ static int file_parent_exec(bContext *C, wmOperator *UNUSED(unused))
if (params) {
if (BLI_path_parent_dir(params->dir)) {
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
ED_file_change_dir(C);
if (params->recursion_level > 1) {
/* Disable 'dirtree' recursion when going up in tree. */
@@ -2804,7 +2809,8 @@ void file_directory_enter_handle(bContext *C, void *UNUSED(arg_unused), void *UN
}
}
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
if (filelist_is_dir(sfile->files, params->dir)) {
if (!STREQ(params->dir, old_dir)) { /* Avoids flickering when nothing's changed. */
@@ -2891,7 +2897,8 @@ void file_filename_enter_handle(bContext *C, void *UNUSED(arg_unused), void *arg
/* if directory, open it and empty filename field */
if (filelist_is_dir(sfile->files, filepath)) {
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
BLI_path_abs(filepath, BKE_main_blendfile_path(bmain));
BLI_path_normalize_dir(filepath, sizeof(filepath));
BLI_strncpy(params->dir, filepath, sizeof(params->dir));
params->file[0] = '\0';
ED_file_change_dir(C);

View File

@@ -1984,7 +1984,8 @@ void filelist_setdir(FileList *filelist, char *r_dir)
const bool allow_invalid = filelist->asset_library_ref != nullptr;
BLI_assert(strlen(r_dir) < FILE_MAX_LIBEXTRA);
BLI_path_normalize_dir(BKE_main_blendfile_path_from_global(), r_dir, FILE_MAX_LIBEXTRA);
BLI_path_abs(r_dir, BKE_main_blendfile_path_from_global());
BLI_path_normalize_dir(r_dir, FILE_MAX_LIBEXTRA);
const bool is_valid_path = filelist->check_dir_fn(filelist, r_dir, !allow_invalid);
BLI_assert(is_valid_path || allow_invalid);
UNUSED_VARS_NDEBUG(is_valid_path);
@@ -3586,7 +3587,8 @@ static void filelist_readjob_recursive_dir_add_items(const bool do_lib,
BLI_strncpy(dir, filelist->filelist.root, sizeof(dir));
BLI_strncpy(filter_glob, filelist->filter_data.filter_glob, sizeof(filter_glob));
BLI_path_normalize_dir(job_params->main_filepath, dir, sizeof(dir));
BLI_path_abs(dir, job_params->main_filepath);
BLI_path_normalize_dir(dir, sizeof(dir));
td_dir->dir = BLI_strdup(dir);
/* Init the file indexer. */
@@ -3617,7 +3619,8 @@ static void filelist_readjob_recursive_dir_add_items(const bool do_lib,
* Note that in the end, this means we 'cache' valid relative subdir once here,
* this is actually better. */
BLI_strncpy(rel_subdir, subdir, sizeof(rel_subdir));
BLI_path_normalize_dir(root, rel_subdir, sizeof(rel_subdir));
BLI_path_abs(rel_subdir, root);
BLI_path_normalize_dir(rel_subdir, sizeof(rel_subdir));
BLI_path_rel(rel_subdir, root);
/* Update the current relative base path within the filelist root. */
@@ -3668,7 +3671,8 @@ static void filelist_readjob_recursive_dir_add_items(const bool do_lib,
/* We have a directory we want to list, add it to todo list!
* Using #BLI_path_join works but isn't needed as `root` has a trailing slash. */
BLI_string_join(dir, sizeof(dir), root, entry->relpath);
BLI_path_normalize_dir(job_params->main_filepath, dir, sizeof(dir));
BLI_path_abs(dir, job_params->main_filepath);
BLI_path_normalize_dir(dir, sizeof(dir));
td_dir = static_cast<TodoDir *>(BLI_stack_push_r(todo_dirs));
td_dir->level = recursion_level + 1;
td_dir->dir = BLI_strdup(dir);

View File

@@ -203,8 +203,8 @@ static FileSelectParams *fileselect_ensure_updated_file_params(SpaceFile *sfile)
}
if (params->dir[0]) {
BLI_path_normalize_dir(blendfile_path, params->dir, sizeof(params->dir));
BLI_path_abs(params->dir, blendfile_path);
BLI_path_normalize_dir(params->dir, sizeof(params->dir));
}
params->flag = 0;

View File

@@ -331,7 +331,7 @@ int Controller::LoadMesh(Render *re, ViewLayer *view_layer, Depsgraph *depsgraph
soc string basename((const char *)qfi.fileName().toAscii().data());
char cleaned[FILE_MAX];
BLI_strncpy(cleaned, iFileName, FILE_MAX);
BLI_path_normalize(NULL, cleaned);
BLI_path_normalize(cleaned);
string basename = string(cleaned);
#endif

View File

@@ -32,7 +32,7 @@ void getPathName(const string &path, const string &base, vector<string> &pathnam
dir = path.substr(pos, sep - pos);
BLI_strncpy(cleaned, dir.c_str(), FILE_MAX);
BLI_path_normalize(nullptr, cleaned);
BLI_path_normalize(cleaned);
res = string(cleaned);
if (!base.empty()) {

View File

@@ -93,7 +93,7 @@ void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
/* make absolute source path */
BLI_strncpy(source_path, image->filepath, sizeof(source_path));
BLI_path_abs(source_path, ID_BLEND_PATH_FROM_GLOBAL(&image->id));
BLI_path_normalize(nullptr, source_path);
BLI_path_normalize(source_path);
if (use_copies) {

View File

@@ -16,7 +16,7 @@ std::string path_reference(StringRefNull filepath,
char filepath_abs[PATH_MAX];
BLI_strncpy(filepath_abs, filepath.c_str(), PATH_MAX);
BLI_path_abs(filepath_abs, base_src.c_str());
BLI_path_normalize(nullptr, filepath_abs);
BLI_path_normalize(filepath_abs);
/* Figure out final mode to be used. */
if (mode == PATH_REFERENCE_MATCH) {

View File

@@ -82,7 +82,7 @@ static std::string copy_asset_to_directory(const char *src_path,
char dest_file_path[FILE_MAX];
BLI_path_join(dest_file_path, sizeof(dest_file_path), dest_dir_path, base_name.c_str());
BLI_path_normalize(NULL, dest_file_path);
BLI_path_normalize(dest_file_path);
if (name_collision_mode == USD_TEX_NAME_COLLISION_USE_EXISTING && BLI_is_file(dest_file_path)) {
return dest_file_path;
@@ -278,7 +278,8 @@ std::string import_asset(const char *src,
}
}
BLI_path_normalize(basepath, dest_dir_path);
BLI_path_abs(dest_dir_path, basepath);
BLI_path_normalize(dest_dir_path);
if (!BLI_dir_create_recursive(dest_dir_path)) {
WM_reportf(

View File

@@ -444,7 +444,7 @@ static void get_absolute_path(Image *ima, char *r_path)
/* Make absolute source path. */
BLI_strncpy(r_path, ima->filepath, FILE_MAX);
BLI_path_abs(r_path, ID_BLEND_PATH_FROM_GLOBAL(&ima->id));
BLI_path_normalize(nullptr, r_path);
BLI_path_normalize(r_path);
}
static pxr::TfToken get_node_tex_image_color_space(bNode *node)

View File

@@ -666,7 +666,7 @@ void MTLWriter::write_materials(const char *blen_filepath,
char blen_filedir[PATH_MAX];
BLI_split_dir_part(blen_filepath, blen_filedir, PATH_MAX);
BLI_path_slash_native(blen_filedir);
BLI_path_normalize(nullptr, blen_filedir);
BLI_path_normalize(blen_filedir);
std::sort(mtlmaterials_.begin(),
mtlmaterials_.end(),

View File

@@ -292,7 +292,7 @@ void export_frame(Depsgraph *depsgraph, const OBJExportParams &export_params, co
BLI_strncpy(dest_dir, export_params.file_base_for_tests, PATH_MAX);
}
BLI_path_slash_native(dest_dir);
BLI_path_normalize(nullptr, dest_dir);
BLI_path_normalize(dest_dir);
mtl_writer->write_materials(export_params.blen_filepath,
export_params.path_mode,
dest_dir,

View File

@@ -2060,7 +2060,7 @@ static int arg_handle_load_file(int UNUSED(argc), const char **argv, void *data)
BLI_strncpy(filepath, argv[0], sizeof(filepath));
BLI_path_slash_native(filepath);
BLI_path_abs_from_cwd(filepath, sizeof(filepath));
BLI_path_normalize(NULL, filepath);
BLI_path_normalize(filepath);
/* load the file */
BKE_reports_init(&reports, RPT_PRINT);