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