From d3ff6e5231f53a95518532ff74a7256aabbf32e5 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Aug 2016 15:53:45 -0700 Subject: [PATCH] Add android::base::Readlink. Bug: http://b/30988271 Change-Id: Ib844d7c9e33465dabf7aee5e24dc3d1d8d799abd --- base/file.cpp | 26 ++++++++++++++++++++++++++ base/file_test.cpp | 26 ++++++++++++++++++++++++++ base/include/android-base/file.h | 4 ++++ 3 files changed, 56 insertions(+) diff --git a/base/file.cpp b/base/file.cpp index 4e7ac82d1..39630815c 100644 --- a/base/file.cpp +++ b/base/file.cpp @@ -20,8 +20,10 @@ #include #include #include +#include #include +#include #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. #include "android-base/logging.h" @@ -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 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) < 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 diff --git a/base/file_test.cpp b/base/file_test.cpp index 17755bfed..ca01ee88a 100644 --- a/base/file_test.cpp +++ b/base/file_test.cpp @@ -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(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 +} diff --git a/base/include/android-base/file.h b/base/include/android-base/file.h index aa18ea796..2726a623d 100644 --- a/base/include/android-base/file.h +++ b/base/include/android-base/file.h @@ -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