#99807: Add support for exporting to USDZ - ultimate fixes #105185
|
@ -145,9 +145,15 @@ eFileAttributes BLI_file_attributes(const char *path);
|
|||
/**
|
||||
* Changes the current working directory to the provided path.
|
||||
*
|
||||
* Usage of this function is strongly discouraged as it is not thread safe. It will likely cause
|
||||
* issues if there is an operation on another thread that does not expect the current working
|
||||
* directory to change. This has been added to support USDZ export, which has a problematic
|
||||
* "feature" described in this task https://developer.blender.org/D15623. It will be removed if it
|
||||
* is possible to resolve that issue upstream in the USD library.
|
||||
*
|
||||
* \return true on success, false otherwise.
|
||||
*/
|
||||
bool BLI_change_working_dir(char *dir);
|
||||
bool BLI_change_working_dir(const char *dir);
|
||||
|
||||
/** \} */
|
||||
|
||||
|
@ -319,6 +325,8 @@ void BLI_file_free_lines(struct LinkNode *lines);
|
|||
* Giving a path without leading `~` is not an error.
|
||||
*/
|
||||
const char *BLI_expand_tilde(const char *path_with_tilde);
|
||||
const char *BLI_apple_getcwd(const size_t max_length);
|
||||
int BLI_apple_chdir(const char *dir);
|
||||
#endif
|
||||
/* This weirdo pops up in two places. */
|
||||
#if !defined(WIN32)
|
||||
|
|
|
@ -54,10 +54,13 @@
|
|||
#include "BLI_linklist.h"
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string.h"
|
||||
#include "BLI_threads.h"
|
||||
#include "BLI_utildefines.h"
|
||||
|
||||
bool BLI_change_working_dir(char *dir)
|
||||
bool BLI_change_working_dir(const char *dir)
|
||||
{
|
||||
BLI_assert(BLI_thread_is_main());
|
||||
|
||||
if (!BLI_is_dir(dir)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -67,8 +70,14 @@ bool BLI_change_working_dir(char *dir)
|
|||
return false;
|
||||
}
|
||||
return _wchdir(wdir) == 0;
|
||||
#elif defined(__APPLE__)
|
||||
return BLI_apple_chdir(dir) == 0;
|
||||
#else
|
||||
return chdir(dir) == 0;
|
||||
int result = chdir(dir);
|
||||
if (result == 0) {
|
||||
BLI_setenv("PWD", dir);
|
||||
}
|
||||
return result == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -82,6 +91,9 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy)
|
|||
}
|
||||
}
|
||||
return NULL;
|
||||
#elif defined(__APPLE__)
|
||||
BLI_strncpy(dir, BLI_apple_getcwd(maxncpy), maxncpy);
|
||||
return dir;
|
||||
#else
|
||||
const char *pwd = BLI_getenv("PWD");
|
||||
if (pwd) {
|
||||
|
|
|
@ -185,3 +185,27 @@ const char *BLI_expand_tilde(const char *path_with_tilde)
|
|||
}
|
||||
return path_expanded;
|
||||
}
|
||||
|
||||
|
||||
const char *BLI_apple_getcwd(const size_t max_length) {
|
||||
static char path_expanded[PATH_MAX];
|
||||
@autoreleasepool {
|
||||
NSString *path = [[NSFileManager defaultManager] currentDirectoryPath];
|
||||
const size_t length = max_length > PATH_MAX ? PATH_MAX : max_length;
|
||||
[path getCString:path_expanded maxLength: length encoding:NSUTF8StringEncoding];
|
||||
return path_expanded;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int BLI_apple_chdir(const char* dir) {
|
||||
@autoreleasepool {
|
||||
NSString* path = [[NSString alloc] initWithUTF8String: dir];
|
||||
if ([[NSFileManager defaultManager] changeCurrentDirectoryPath: path] == YES) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string.h"
|
||||
#include "testing/testing.h"
|
||||
|
||||
#include "BLI_fileops.hh"
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string.h"
|
||||
|
||||
#include "testing/testing.h"
|
||||
#include "BLI_threads.h"
|
||||
|
||||
namespace blender::tests {
|
||||
|
||||
|
@ -41,33 +44,37 @@ TEST(fileops, fstream_open_charptr_filename)
|
|||
|
||||
TEST(fileops, change_working_directory)
|
||||
{
|
||||
BLI_threadapi_init();
|
||||
|
||||
char original_wd[FILE_MAX];
|
||||
BLI_current_working_dir(original_wd, FILE_MAX);
|
||||
|
||||
char temp_wd[FILE_MAX];
|
||||
BLI_path_join(temp_wd, FILE_MAX, original_wd, "test_temp");
|
||||
const std::string test_temp_dir = blender::tests::flags_test_asset_dir() + "/" +
|
||||
"fileops_test_temp_новый";
|
||||
|
||||
if (BLI_exists(temp_wd)) {
|
||||
BLI_delete(temp_wd, true, false);
|
||||
if (BLI_exists(test_temp_dir.c_str())) {
|
||||
BLI_delete(test_temp_dir.c_str(), true, false);
|
||||
}
|
||||
|
||||
bool result = BLI_change_working_dir(temp_wd);
|
||||
bool result = BLI_change_working_dir(test_temp_dir.c_str());
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
BLI_dir_create_recursive(temp_wd);
|
||||
BLI_dir_create_recursive(test_temp_dir.c_str());
|
||||
|
||||
result = BLI_change_working_dir(temp_wd);
|
||||
result = BLI_change_working_dir(test_temp_dir.c_str());
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
char cwd[FILE_MAX];
|
||||
BLI_current_working_dir(cwd, FILE_MAX);
|
||||
|
||||
ASSERT_TRUE(BLI_path_cmp(cwd, temp_wd) == 0);
|
||||
ASSERT_TRUE(BLI_path_cmp_normalized(cwd, test_temp_dir.c_str()) == 0);
|
||||
|
||||
result = BLI_change_working_dir(original_wd);
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
BLI_delete(temp_wd, true, false);
|
||||
BLI_delete(test_temp_dir.c_str(), true, false);
|
||||
|
||||
BLI_threadapi_exit();
|
||||
}
|
||||
|
||||
} // namespace blender::tests
|
||||
|
|
Loading…
Reference in New Issue