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