Loading base/file.cpp +40 −15 Original line number Diff line number Diff line Loading @@ -49,29 +49,41 @@ #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) { auto path = name_template; if (_mktemp_s(path, size_in_chars) != 0) { return -1; } std::wstring path_wide; CHECK(android::base::UTF8ToWide(path, &path_wide)) << "path can't be converted to wchar: " << path; // 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); return _wopen(path_wide.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR); } char* mkdtemp(char* template_name) { if (_mktemp(template_name) == nullptr) { static char* mkdtemp(char* name_template, size_t size_in_chars) { auto path = name_template; if (_mktemp_s(path, size_in_chars) != 0) { return nullptr; } if (_mkdir(template_name) == -1) { std::wstring path_wide; CHECK(android::base::UTF8ToWide(path, &path_wide)) << "path can't be converted to wchar: " << path; if (_wmkdir(path_wide.c_str()) != 0) { return nullptr; } return template_name; return path; } #endif namespace { std::string GetSystemTempDir() { #if defined(__ANDROID__) const auto* tmpdir = getenv("TMPDIR"); Loading @@ -83,15 +95,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"); Loading Loading @@ -127,7 +144,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() { Loading Loading @@ -167,7 +188,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 { Loading base/file_test.cpp +88 −7 Original line number Diff line number Diff line Loading @@ -16,18 +16,25 @@ #include "android-base/file.h" #include "android-base/utf8.h" #include <gtest/gtest.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <wchar.h> #include <string> #if !defined(_WIN32) #include <pwd.h> #else #include <processenv.h> #endif #include "android-base/logging.h" // and must be after windows.h for ERROR TEST(file, ReadFileToString_ENOENT) { std::string s("hello"); errno = 0; Loading @@ -38,7 +45,7 @@ TEST(file, ReadFileToString_ENOENT) { TEST(file, ReadFileToString_WriteStringToFile) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) << strerror(errno); std::string s; Loading Loading @@ -70,7 +77,7 @@ TEST(file, ReadFileToString_WriteStringToFile_symlink) { #if !defined(_WIN32) TEST(file, WriteStringToFile2) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, getuid(), getgid())) << strerror(errno); Loading @@ -86,9 +93,83 @@ TEST(file, WriteStringToFile2) { } #endif #if defined(_WIN32) TEST(file, NonUnicodeCharsWindows) { constexpr auto kMaxEnvVariableValueSize = 32767; std::wstring old_tmp; old_tmp.resize(kMaxEnvVariableValueSize); old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size())); std::wstring new_tmp = old_tmp; if (new_tmp.back() != L'\\') { new_tmp.push_back(L'\\'); } { auto path(new_tmp + L"锦绣成都\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } { auto path(new_tmp + L"директория с длинным именем\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } { auto path(new_tmp + L"äüöß weiß\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } SetEnvironmentVariableW(L"TMP", old_tmp.c_str()); } TEST(file, RootDirectoryWindows) { constexpr auto kMaxEnvVariableValueSize = 32767; std::wstring old_tmp; old_tmp.resize(kMaxEnvVariableValueSize); old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size())); SetEnvironmentVariableW(L"TMP", L"C:"); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; SetEnvironmentVariableW(L"TMP", old_tmp.c_str()); } #endif TEST(file, WriteStringToFd) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); Loading @@ -100,7 +181,7 @@ TEST(file, WriteStringToFd) { TEST(file, WriteFully) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); Loading @@ -119,7 +200,7 @@ TEST(file, WriteFully) { TEST(file, RemoveFileIfExists) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; close(tf.fd); tf.fd = -1; std::string err; Loading Loading @@ -253,7 +334,7 @@ TEST(file, Dirname) { TEST(file, ReadFileToString_capacity) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; // For a huge file, the overhead should still be small. std::string s; Loading @@ -280,7 +361,7 @@ TEST(file, ReadFileToString_capacity) { TEST(file, ReadFileToString_capacity_0) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; // Because /proc reports its files as zero-length, we don't actually trust // any file that claims to be zero-length. Rather than add increasingly Loading Loading
base/file.cpp +40 −15 Original line number Diff line number Diff line Loading @@ -49,29 +49,41 @@ #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) { auto path = name_template; if (_mktemp_s(path, size_in_chars) != 0) { return -1; } std::wstring path_wide; CHECK(android::base::UTF8ToWide(path, &path_wide)) << "path can't be converted to wchar: " << path; // 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); return _wopen(path_wide.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR); } char* mkdtemp(char* template_name) { if (_mktemp(template_name) == nullptr) { static char* mkdtemp(char* name_template, size_t size_in_chars) { auto path = name_template; if (_mktemp_s(path, size_in_chars) != 0) { return nullptr; } if (_mkdir(template_name) == -1) { std::wstring path_wide; CHECK(android::base::UTF8ToWide(path, &path_wide)) << "path can't be converted to wchar: " << path; if (_wmkdir(path_wide.c_str()) != 0) { return nullptr; } return template_name; return path; } #endif namespace { std::string GetSystemTempDir() { #if defined(__ANDROID__) const auto* tmpdir = getenv("TMPDIR"); Loading @@ -83,15 +95,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"); Loading Loading @@ -127,7 +144,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() { Loading Loading @@ -167,7 +188,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 { Loading
base/file_test.cpp +88 −7 Original line number Diff line number Diff line Loading @@ -16,18 +16,25 @@ #include "android-base/file.h" #include "android-base/utf8.h" #include <gtest/gtest.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <wchar.h> #include <string> #if !defined(_WIN32) #include <pwd.h> #else #include <processenv.h> #endif #include "android-base/logging.h" // and must be after windows.h for ERROR TEST(file, ReadFileToString_ENOENT) { std::string s("hello"); errno = 0; Loading @@ -38,7 +45,7 @@ TEST(file, ReadFileToString_ENOENT) { TEST(file, ReadFileToString_WriteStringToFile) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) << strerror(errno); std::string s; Loading Loading @@ -70,7 +77,7 @@ TEST(file, ReadFileToString_WriteStringToFile_symlink) { #if !defined(_WIN32) TEST(file, WriteStringToFile2) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, getuid(), getgid())) << strerror(errno); Loading @@ -86,9 +93,83 @@ TEST(file, WriteStringToFile2) { } #endif #if defined(_WIN32) TEST(file, NonUnicodeCharsWindows) { constexpr auto kMaxEnvVariableValueSize = 32767; std::wstring old_tmp; old_tmp.resize(kMaxEnvVariableValueSize); old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size())); std::wstring new_tmp = old_tmp; if (new_tmp.back() != L'\\') { new_tmp.push_back(L'\\'); } { auto path(new_tmp + L"锦绣成都\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } { auto path(new_tmp + L"директория с длинным именем\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } { auto path(new_tmp + L"äüöß weiß\\"); _wmkdir(path.c_str()); ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str())); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); std::string s; ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); EXPECT_EQ("abc", s); } SetEnvironmentVariableW(L"TMP", old_tmp.c_str()); } TEST(file, RootDirectoryWindows) { constexpr auto kMaxEnvVariableValueSize = 32767; std::wstring old_tmp; old_tmp.resize(kMaxEnvVariableValueSize); old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size())); SetEnvironmentVariableW(L"TMP", L"C:"); TemporaryFile tf; ASSERT_NE(tf.fd, -1) << tf.path; SetEnvironmentVariableW(L"TMP", old_tmp.c_str()); } #endif TEST(file, WriteStringToFd) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); Loading @@ -100,7 +181,7 @@ TEST(file, WriteStringToFd) { TEST(file, WriteFully) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); Loading @@ -119,7 +200,7 @@ TEST(file, WriteFully) { TEST(file, RemoveFileIfExists) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; close(tf.fd); tf.fd = -1; std::string err; Loading Loading @@ -253,7 +334,7 @@ TEST(file, Dirname) { TEST(file, ReadFileToString_capacity) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; // For a huge file, the overhead should still be small. std::string s; Loading @@ -280,7 +361,7 @@ TEST(file, ReadFileToString_capacity) { TEST(file, ReadFileToString_capacity_0) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_NE(tf.fd, -1) << tf.path; // Because /proc reports its files as zero-length, we don't actually trust // any file that claims to be zero-length. Rather than add increasingly Loading