-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtmpfile_hack.cpp
More file actions
59 lines (44 loc) · 1.66 KB
/
tmpfile_hack.cpp
File metadata and controls
59 lines (44 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "tmpfile_hack.hpp"
#include <string>
#include <optional>
#include <random>
#include <android/log.h>
#include <cstdlib>
#include <unistd.h>
static constexpr std::string_view s_filename_template = "tmpfile-XXXXXX";
static constexpr std::string_view s_default_directory = "/data/local/tmp";
static std::optional<std::string> s_tmpfile_directory;
std::string tmpfile_hack::get_tmpfile_directory() {
if (s_tmpfile_directory.has_value()) {
return s_tmpfile_directory.value();
}
if (const char *tmpfile_directory = std::getenv("TMPDIR"); tmpfile_directory != nullptr) {
return tmpfile_directory;
}
return std::string(s_default_directory);
}
void tmpfile_hack::set_tmpfile_directory(std::string_view tmpfile_dir) {
s_tmpfile_directory = std::string(tmpfile_dir);
}
extern "C" {
extern FILE *tmpfile() {
std::string tmpfile_path =
tmpfile_hack::get_tmpfile_directory() + "/" + std::string(s_filename_template);
int descriptor = mkstemp(tmpfile_path.data());
if (descriptor == -1) {
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack",
"Failed to create temporary file: %s", tmpfile_path.c_str());
return nullptr;
}
__android_log_print(ANDROID_LOG_VERBOSE, "tmpfile_hack", "Temporary file created: %s",
tmpfile_path.c_str());
FILE *handle = fdopen(descriptor, "w+b");
unlink(tmpfile_path.c_str());
if (handle == nullptr) {
close(descriptor);
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack", "Failed to open temporary file: %s",
tmpfile_path.c_str());
}
return handle;
}
}