summaryrefslogtreecommitdiff
path: root/base/file.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'base/file.cpp')
-rw-r--r--base/file.cpp68
1 files changed, 53 insertions, 15 deletions
diff --git a/base/file.cpp b/base/file.cpp
index 3dfcfbb7ec..6321fc6246 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -49,29 +49,54 @@
#include "android-base/unique_fd.h"
#include "android-base/utf8.h"
+namespace {
+
#ifdef _WIN32
-int mkstemp(char* template_name) {
- if (_mktemp(template_name) == nullptr) {
+static int mkstemp(char* name_template, size_t size_in_chars) {
+ std::wstring path;
+ CHECK(android::base::UTF8ToWide(name_template, &path))
+ << "path can't be converted to wchar: " << name_template;
+ if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
return -1;
}
+
// Use open() to match the close() that TemporaryFile's destructor does.
// Use O_BINARY to match base file APIs.
- return open(template_name, O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
+ int fd = _wopen(path.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
+ if (fd < 0) {
+ return -1;
+ }
+
+ std::string path_utf8;
+ CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
+ CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
+ << "utf8 path can't be assigned back to name_template";
+
+ return fd;
}
-char* mkdtemp(char* template_name) {
- if (_mktemp(template_name) == nullptr) {
+static char* mkdtemp(char* name_template, size_t size_in_chars) {
+ std::wstring path;
+ CHECK(android::base::UTF8ToWide(name_template, &path))
+ << "path can't be converted to wchar: " << name_template;
+
+ if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
return nullptr;
}
- if (_mkdir(template_name) == -1) {
+
+ if (_wmkdir(path.c_str()) != 0) {
return nullptr;
}
- return template_name;
+
+ std::string path_utf8;
+ CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
+ CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
+ << "utf8 path can't be assigned back to name_template";
+
+ return name_template;
}
#endif
-namespace {
-
std::string GetSystemTempDir() {
#if defined(__ANDROID__)
const auto* tmpdir = getenv("TMPDIR");
@@ -83,15 +108,20 @@ std::string GetSystemTempDir() {
// so try current directory if /data/local/tmp is not accessible.
return ".";
#elif defined(_WIN32)
- char tmp_dir[MAX_PATH];
- DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); // checks TMP env
- CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError();
- CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result;
+ wchar_t tmp_dir_w[MAX_PATH];
+ DWORD result = GetTempPathW(std::size(tmp_dir_w), tmp_dir_w); // checks TMP env
+ CHECK_NE(result, 0ul) << "GetTempPathW failed, error: " << GetLastError();
+ CHECK_LT(result, std::size(tmp_dir_w)) << "path truncated to: " << result;
// GetTempPath() returns a path with a trailing slash, but init()
// does not expect that, so remove it.
- CHECK_EQ(tmp_dir[result - 1], '\\');
- tmp_dir[result - 1] = '\0';
+ if (tmp_dir_w[result - 1] == L'\\') {
+ tmp_dir_w[result - 1] = L'\0';
+ }
+
+ std::string tmp_dir;
+ CHECK(android::base::WideToUTF8(tmp_dir_w, &tmp_dir)) << "path can't be converted to utf8";
+
return tmp_dir;
#else
const auto* tmpdir = getenv("TMPDIR");
@@ -127,7 +157,11 @@ int TemporaryFile::release() {
void TemporaryFile::init(const std::string& tmp_dir) {
snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
+#if defined(_WIN32)
+ fd = mkstemp(path, sizeof(path));
+#else
fd = mkstemp(path);
+#endif
}
TemporaryDir::TemporaryDir() {
@@ -167,7 +201,11 @@ TemporaryDir::~TemporaryDir() {
bool TemporaryDir::init(const std::string& tmp_dir) {
snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
+#if defined(_WIN32)
+ return (mkdtemp(path, sizeof(path)) != nullptr);
+#else
return (mkdtemp(path) != nullptr);
+#endif
}
namespace android {