diff --git a/source/blender/blenkernel/intern/asset_library_service.cc b/source/blender/blenkernel/intern/asset_library_service.cc index 14604def0d3..53ce2826022 100644 --- a/source/blender/blenkernel/intern/asset_library_service.cc +++ b/source/blender/blenkernel/intern/asset_library_service.cc @@ -24,6 +24,7 @@ #include "BKE_blender.h" #include "BKE_callbacks.h" +#include "BLI_path_util.h" #include "BLI_string_ref.hh" #include "MEM_guardedalloc.h" @@ -54,14 +55,27 @@ void AssetLibraryService::destroy() instance_.reset(); } +namespace { +std::string normalize_directory_path(StringRefNull directory) +{ + + char dir_normalized[PATH_MAX]; + STRNCPY(dir_normalized, directory.c_str()); + BLI_path_normalize_dir(NULL, dir_normalized); + return std::string(dir_normalized); +} +} // namespace + AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_level_directory) { BLI_assert_msg(!top_level_directory.is_empty(), "top level directory must be given for on-disk asset library"); - AssetLibraryPtr *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(top_level_directory); + std::string top_dir_trailing_slash = normalize_directory_path(top_level_directory); + + AssetLibraryPtr *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(top_dir_trailing_slash); if (lib_uptr_ptr != nullptr) { - CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_level_directory.c_str()); + CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_dir_trailing_slash.c_str()); return lib_uptr_ptr->get(); } @@ -69,10 +83,10 @@ AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_l AssetLibrary *lib = lib_uptr.get(); lib->on_save_handler_register(); - lib->load(top_level_directory); + lib->load(top_dir_trailing_slash); - on_disk_libraries_.add_new(top_level_directory, std::move(lib_uptr)); - CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_level_directory.c_str()); + on_disk_libraries_.add_new(top_dir_trailing_slash, std::move(lib_uptr)); + CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_dir_trailing_slash.c_str()); return lib; } diff --git a/source/blender/blenkernel/intern/asset_library_service_test.cc b/source/blender/blenkernel/intern/asset_library_service_test.cc index b4bb1dc0895..ed299028c41 100644 --- a/source/blender/blenkernel/intern/asset_library_service_test.cc +++ b/source/blender/blenkernel/intern/asset_library_service_test.cc @@ -19,6 +19,8 @@ #include "asset_library_service.hh" +#include "BLI_path_util.h" + #include "CLG_log.h" #include "testing/testing.h" @@ -84,6 +86,30 @@ TEST_F(AssetLibraryServiceTest, library_pointers) * cannot be reliably tested by just pointer comparison, though. */ } +TEST_F(AssetLibraryServiceTest, library_path_trailing_slashes) +{ + AssetLibraryService *service = AssetLibraryService::get(); + + char asset_lib_no_slash[PATH_MAX]; + char asset_lib_with_slash[PATH_MAX]; + STRNCPY(asset_lib_no_slash, asset_library_root_.c_str()); + STRNCPY(asset_lib_with_slash, asset_library_root_.c_str()); + + /* Ensure #asset_lib_no_slash has no trailing slash, regardless of what was passed on the CLI to + * the unit test. */ + while (strlen(asset_lib_no_slash) && + ELEM(asset_lib_no_slash[strlen(asset_lib_no_slash) - 1], SEP, ALTSEP)) { + asset_lib_no_slash[strlen(asset_lib_no_slash) - 1] = '\0'; + } + + BLI_path_slash_ensure(asset_lib_with_slash); + + AssetLibrary *const lib_no_slash = service->get_asset_library_on_disk(asset_lib_no_slash); + + EXPECT_EQ(lib_no_slash, service->get_asset_library_on_disk(asset_lib_with_slash)) + << "With or without trailing slash shouldn't matter."; +} + TEST_F(AssetLibraryServiceTest, catalogs_loaded) { AssetLibraryService *const service = AssetLibraryService::get();