Blender V4.3
tempfile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_tempfile.h"
6
7#include "BLI_fileops.h"
8#include "BLI_path_utils.hh"
9#include "BLI_string.h"
10
12 const size_t tempdir_maxncpy,
13 const char *dirpath)
14{
15 /* NOTE(@ideasman42): it is *not* the purpose of this function to check that
16 * `dirpath` is writable under all circumstances.
17 * Only check `dirpath` doesn't resolve to an empty string & points to a directory.
18 *
19 * While other checks could be added to avoid problems writing temporary files:
20 * (read-only, permission failure, out-of-I-nodes, disk-full... etc)
21 * it's out of scope for this function as these characteristics can change at run-time.
22 * In general temporary file IO should handle failure properly with sufficient user feedback,
23 * without attempting to *solve* the problem by anticipating file-system issues ahead of time. */
24
25 /* Disallow paths starting with two forward slashes. While they are valid paths,
26 * Blender interprets them as relative in situations relative paths aren't supported,
27 * see #95411. */
28 while (UNLIKELY(dirpath[0] == '/' && dirpath[1] == '/')) {
29 dirpath++;
30 }
31 if (dirpath[0] == '\0') {
32 return false;
33 }
34 if (!BLI_is_dir(dirpath)) {
35 return false;
36 }
37
38 BLI_strncpy(tempdir, dirpath, tempdir_maxncpy);
39
40 /* Add a trailing slash if needed. */
41 BLI_path_slash_ensure(tempdir, tempdir_maxncpy);
42
43 /* There's nothing preventing an environment variable (even preferences) from being CWD relative.
44 * This causes:
45 * - Asserts in code-paths which expect absolute paths (blend-file IO).
46 * - The temporary directory to change if the CWD changes.
47 * Avoid issues by ensuring the temporary directory is *never* CWD relative. */
48 BLI_path_abs_from_cwd(tempdir, tempdir_maxncpy);
49
50 return true;
51}
52
53void BLI_temp_directory_path_get(char *tempdir, const size_t tempdir_maxncpy)
54{
55 tempdir[0] = '\0';
56
57 const char *env_vars[] = {
58#ifdef WIN32
59 "TEMP",
60#else
61 /* Non standard (could be removed). */
62 "TMP",
63 /* Posix standard. */
64 "TMPDIR",
65#endif
66 };
67
68 for (int i = 0; i < ARRAY_SIZE(env_vars); i++) {
69 const char *tempdir_test = BLI_getenv(env_vars[i]);
70 if (tempdir_test == nullptr) {
71 continue;
72 }
73 if (BLI_temp_directory_path_copy_if_valid(tempdir, tempdir_maxncpy, tempdir_test)) {
74 break;
75 }
76 }
77
78 if (tempdir[0] == '\0') {
79 BLI_strncpy(tempdir, "/tmp/", tempdir_maxncpy);
80 }
81}
File and directory operations.
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:433
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
bool BLI_path_abs_from_cwd(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
void BLI_temp_directory_path_get(char *tempdir, const size_t tempdir_maxncpy)
Definition tempfile.cc:53
bool BLI_temp_directory_path_copy_if_valid(char *tempdir, const size_t tempdir_maxncpy, const char *dirpath)
Definition tempfile.cc:11