Cleanup: minor changes to temp directory API
- Pass null instead of an empty string to BKE_tempdir_init because the string isn't meant to be used. - Never pass null to BLI_temp_directory_path_copy_if_valid (the caller must check). - Additional comments for which checks are performed & why from discussion about #95411.
This commit is contained in:
parent
b0daa792d3
commit
1b514659ca
|
@ -62,7 +62,7 @@ class AssetLibraryServiceTest : public testing::Test {
|
||||||
* The returned path ends in a slash. */
|
* The returned path ends in a slash. */
|
||||||
CatalogFilePath use_temp_path()
|
CatalogFilePath use_temp_path()
|
||||||
{
|
{
|
||||||
BKE_tempdir_init("");
|
BKE_tempdir_init(nullptr);
|
||||||
const CatalogFilePath tempdir = BKE_tempdir_session();
|
const CatalogFilePath tempdir = BKE_tempdir_session();
|
||||||
temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
|
temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
|
||||||
return temp_library_path_;
|
return temp_library_path_;
|
||||||
|
|
|
@ -79,7 +79,7 @@ class AssetLibraryTestBase : public testing::Test {
|
||||||
* The returned path ends in a slash. */
|
* The returned path ends in a slash. */
|
||||||
std::string use_temp_path()
|
std::string use_temp_path()
|
||||||
{
|
{
|
||||||
BKE_tempdir_init("");
|
BKE_tempdir_init(nullptr);
|
||||||
const std::string tempdir = BKE_tempdir_session();
|
const std::string tempdir = BKE_tempdir_session();
|
||||||
temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
|
temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
|
||||||
return temp_library_path_;
|
return temp_library_path_;
|
||||||
|
|
|
@ -1118,7 +1118,7 @@ void BKE_appdir_app_templates(ListBase *templates)
|
||||||
*/
|
*/
|
||||||
static void where_is_temp(char *tempdir, const size_t tempdir_maxncpy, const char *userdir)
|
static void where_is_temp(char *tempdir, const size_t tempdir_maxncpy, const char *userdir)
|
||||||
{
|
{
|
||||||
if (BLI_temp_directory_path_copy_if_valid(tempdir, tempdir_maxncpy, userdir)) {
|
if (userdir && BLI_temp_directory_path_copy_if_valid(tempdir, tempdir_maxncpy, userdir)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BLI_temp_directory_path_get(tempdir, tempdir_maxncpy);
|
BLI_temp_directory_path_get(tempdir, tempdir_maxncpy);
|
||||||
|
@ -1166,7 +1166,7 @@ static void tempdir_session_create(char *tempdir_session,
|
||||||
|
|
||||||
void BKE_tempdir_init(const char *userdir)
|
void BKE_tempdir_init(const char *userdir)
|
||||||
{
|
{
|
||||||
/* Sets #g_app.temp_dirname_base to \a userdir if specified and is a valid directory,
|
/* Sets #g_app.temp_dirname_base to `userdir` if specified and is a valid directory,
|
||||||
* otherwise chooses a suitable OS-specific temporary directory.
|
* otherwise chooses a suitable OS-specific temporary directory.
|
||||||
* Sets #g_app.temp_dirname_session to a #mkdtemp
|
* Sets #g_app.temp_dirname_session to a #mkdtemp
|
||||||
* generated sub-dir of #g_app.temp_dirname_base. */
|
* generated sub-dir of #g_app.temp_dirname_base. */
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "BLI_compiler_attrs.h"
|
||||||
#include "BLI_sys_types.h"
|
#include "BLI_sys_types.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -15,21 +16,23 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets `temp_directory` from `dirpath` when it's a valid directory.
|
* Sets `tempdir` from `dirpath` when it's a valid directory.
|
||||||
* Simple sanitize operations are performed and a trailing slash is ensured.
|
* Simple sanitize operations are performed and a trailing slash is ensured.
|
||||||
*/
|
*/
|
||||||
bool BLI_temp_directory_path_copy_if_valid(char *temp_directory,
|
bool BLI_temp_directory_path_copy_if_valid(char *tempdir,
|
||||||
const size_t buffer_size,
|
const size_t tempdir_maxncpy,
|
||||||
const char *dirpath);
|
const char *dirpath) ATTR_NONNULL(1, 3);
|
||||||
|
|
||||||
/* Get the path to a directory suitable for temporary files.
|
/**
|
||||||
|
* Get the path to a directory suitable for temporary files.
|
||||||
*
|
*
|
||||||
* The return path is guaranteed to exist and to be a directory, as well as to contain a trailing
|
* The return path is guaranteed to exist and to be a directory, as well as to contain a trailing
|
||||||
* directory separator.
|
* directory separator.
|
||||||
*
|
*
|
||||||
* At maximum the buffer_size number of characters is written to the temp_directory. The directory
|
* At maximum the `tempdir_maxncpy` number of characters is written to the `tempdir`.
|
||||||
* path is always null-terminated. */
|
* The directory path is always null-terminated.
|
||||||
void BLI_temp_directory_path_get(char *temp_directory, const size_t buffer_size);
|
*/
|
||||||
|
void BLI_temp_directory_path_get(char *tempdir, const size_t tempdir_maxncpy) ATTR_NONNULL(1);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,19 @@
|
||||||
#include "BLI_path_util.h"
|
#include "BLI_path_util.h"
|
||||||
#include "BLI_string.h"
|
#include "BLI_string.h"
|
||||||
|
|
||||||
bool BLI_temp_directory_path_copy_if_valid(char *temp_directory,
|
bool BLI_temp_directory_path_copy_if_valid(char *tempdir,
|
||||||
const size_t buffer_size,
|
const size_t tempdir_maxncpy,
|
||||||
const char *dirpath)
|
const char *dirpath)
|
||||||
{
|
{
|
||||||
if (dirpath == NULL) {
|
/* NOTE(@ideasman42): it is *not* the purpose of this function to check that
|
||||||
return false;
|
* `dirpath` is writable under all circumstances.
|
||||||
}
|
* Only check `dirpath` doesn't resolve to an empty string & points to a directory.
|
||||||
|
*
|
||||||
|
* While other checks could be added to avoid problems writing temporary files:
|
||||||
|
* (read-only, permission failure, out-of-I-nodes, disk-full... etc)
|
||||||
|
* it's out of scope for this function as these characteristics can change at run-time.
|
||||||
|
* In general temporary file IO should handle failure properly with sufficient user feedback,
|
||||||
|
* without attempting to *solve* the problem by anticipating file-system issues ahead of time. */
|
||||||
|
|
||||||
/* Disallow paths starting with two forward slashes. While they are valid paths,
|
/* Disallow paths starting with two forward slashes. While they are valid paths,
|
||||||
* Blender interprets them as relative in situations relative paths aren't supported,
|
* Blender interprets them as relative in situations relative paths aren't supported,
|
||||||
|
@ -29,16 +35,16 @@ bool BLI_temp_directory_path_copy_if_valid(char *temp_directory,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BLI_strncpy(temp_directory, dirpath, buffer_size);
|
BLI_strncpy(tempdir, dirpath, tempdir_maxncpy);
|
||||||
|
|
||||||
/* Add a trailing slash if needed. */
|
/* Add a trailing slash if needed. */
|
||||||
BLI_path_slash_ensure(temp_directory, buffer_size);
|
BLI_path_slash_ensure(tempdir, tempdir_maxncpy);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BLI_temp_directory_path_get(char *temp_directory, const size_t buffer_size)
|
void BLI_temp_directory_path_get(char *tempdir, const size_t tempdir_maxncpy)
|
||||||
{
|
{
|
||||||
temp_directory[0] = '\0';
|
tempdir[0] = '\0';
|
||||||
|
|
||||||
const char *env_vars[] = {
|
const char *env_vars[] = {
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -52,16 +58,18 @@ void BLI_temp_directory_path_get(char *temp_directory, const size_t buffer_size)
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
|
for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
|
||||||
if (BLI_temp_directory_path_copy_if_valid(
|
const char *tempdir_test = BLI_getenv(env_vars[i]);
|
||||||
temp_directory, buffer_size, BLI_getenv(env_vars[i])))
|
if (tempdir_test == NULL) {
|
||||||
{
|
continue;
|
||||||
|
}
|
||||||
|
if (BLI_temp_directory_path_copy_if_valid(tempdir, tempdir_maxncpy, tempdir_test)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temp_directory[0] == '\0') {
|
if (tempdir[0] == '\0') {
|
||||||
BLI_strncpy(temp_directory, "/tmp/", buffer_size);
|
BLI_strncpy(tempdir, "/tmp/", tempdir_maxncpy);
|
||||||
}
|
}
|
||||||
|
|
||||||
BLI_dir_create_recursive(temp_directory);
|
BLI_dir_create_recursive(tempdir);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ class PLYExportTest : public BlendfileLoadingBaseTest {
|
||||||
{
|
{
|
||||||
BlendfileLoadingBaseTest::SetUp();
|
BlendfileLoadingBaseTest::SetUp();
|
||||||
|
|
||||||
BKE_tempdir_init("");
|
BKE_tempdir_init(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() override
|
void TearDown() override
|
||||||
|
|
|
@ -60,7 +60,7 @@ class STLExportTest : public BlendfileLoadingBaseTest {
|
||||||
void SetUp() override
|
void SetUp() override
|
||||||
{
|
{
|
||||||
BlendfileLoadingBaseTest::SetUp();
|
BlendfileLoadingBaseTest::SetUp();
|
||||||
BKE_tempdir_init("");
|
BKE_tempdir_init(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() override
|
void TearDown() override
|
||||||
|
|
|
@ -143,7 +143,7 @@ class ObjExporterWriterTest : public testing::Test {
|
||||||
protected:
|
protected:
|
||||||
void SetUp() override
|
void SetUp() override
|
||||||
{
|
{
|
||||||
BKE_tempdir_init("");
|
BKE_tempdir_init(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() override
|
void TearDown() override
|
||||||
|
|
Loading…
Reference in New Issue