Loading base/file.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -20,8 +20,10 @@ #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string> #include <vector> #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. #include "android-base/logging.h" Loading Loading @@ -171,5 +173,29 @@ bool RemoveFileIfExists(const std::string& path, std::string* err) { return true; } #if !defined(_WIN32) bool Readlink(const std::string& path, std::string* result) { result->clear(); // Most Linux file systems (ext2 and ext4, say) limit symbolic links to // 4095 bytes. Since we'll copy out into the string anyway, it doesn't // waste memory to just start there. We add 1 so that we can recognize // whether it actually fit (rather than being truncated to 4095). std::vector<char> buf(4095 + 1); while (true) { ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); // Unrecoverable error? if (size == -1) return false; // It fit! (If size == buf.size(), it may have been truncated.) if (static_cast<size_t>(size) < buf.size()) { result->assign(&buf[0], size); return true; } // Double our buffer and try again. buf.resize(buf.size() * 2); } } #endif } // namespace base } // namespace android base/file_test.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -110,3 +110,29 @@ TEST(file, RemoveFileIfExist) { ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err)); ASSERT_EQ("is not a regular or symbol link file", err); } TEST(file, Readlink) { #if !defined(_WIN32) // Linux doesn't allow empty symbolic links. std::string min("x"); // ext2 and ext4 both have PAGE_SIZE limits. std::string max(static_cast<size_t>(4096 - 1), 'x'); TemporaryDir td; std::string min_path{std::string(td.path) + "/" + "min"}; std::string max_path{std::string(td.path) + "/" + "max"}; ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str())); ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str())); std::string result; result = "wrong"; ASSERT_TRUE(android::base::Readlink(min_path, &result)); ASSERT_EQ(min, result); result = "wrong"; ASSERT_TRUE(android::base::Readlink(max_path, &result)); ASSERT_EQ(max, result); #endif } base/include/android-base/file.h +4 −0 Original line number Diff line number Diff line Loading @@ -43,6 +43,10 @@ bool WriteFully(int fd, const void* data, size_t byte_count); bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); #if !defined(_WIN32) bool Readlink(const std::string& path, std::string* result); #endif } // namespace base } // namespace android Loading Loading
base/file.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -20,8 +20,10 @@ #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string> #include <vector> #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. #include "android-base/logging.h" Loading Loading @@ -171,5 +173,29 @@ bool RemoveFileIfExists(const std::string& path, std::string* err) { return true; } #if !defined(_WIN32) bool Readlink(const std::string& path, std::string* result) { result->clear(); // Most Linux file systems (ext2 and ext4, say) limit symbolic links to // 4095 bytes. Since we'll copy out into the string anyway, it doesn't // waste memory to just start there. We add 1 so that we can recognize // whether it actually fit (rather than being truncated to 4095). std::vector<char> buf(4095 + 1); while (true) { ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); // Unrecoverable error? if (size == -1) return false; // It fit! (If size == buf.size(), it may have been truncated.) if (static_cast<size_t>(size) < buf.size()) { result->assign(&buf[0], size); return true; } // Double our buffer and try again. buf.resize(buf.size() * 2); } } #endif } // namespace base } // namespace android
base/file_test.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -110,3 +110,29 @@ TEST(file, RemoveFileIfExist) { ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err)); ASSERT_EQ("is not a regular or symbol link file", err); } TEST(file, Readlink) { #if !defined(_WIN32) // Linux doesn't allow empty symbolic links. std::string min("x"); // ext2 and ext4 both have PAGE_SIZE limits. std::string max(static_cast<size_t>(4096 - 1), 'x'); TemporaryDir td; std::string min_path{std::string(td.path) + "/" + "min"}; std::string max_path{std::string(td.path) + "/" + "max"}; ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str())); ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str())); std::string result; result = "wrong"; ASSERT_TRUE(android::base::Readlink(min_path, &result)); ASSERT_EQ(min, result); result = "wrong"; ASSERT_TRUE(android::base::Readlink(max_path, &result)); ASSERT_EQ(max, result); #endif }
base/include/android-base/file.h +4 −0 Original line number Diff line number Diff line Loading @@ -43,6 +43,10 @@ bool WriteFully(int fd, const void* data, size_t byte_count); bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); #if !defined(_WIN32) bool Readlink(const std::string& path, std::string* result); #endif } // namespace base } // namespace android Loading