diff options
author | Elliott Hughes <enh@google.com> | 2016-08-23 15:53:45 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2016-08-31 09:35:44 -0700 |
commit | d3ff6e5231f53a95518532ff74a7256aabbf32e5 (patch) | |
tree | f718a1e42c11432a30f0e6addb514dea87c2ea63 /base | |
parent | e461b37965ff609ea36c1fbb6e4e8df1fb7b7d7f (diff) | |
download | system_core-d3ff6e5231f53a95518532ff74a7256aabbf32e5.tar.gz system_core-d3ff6e5231f53a95518532ff74a7256aabbf32e5.tar.bz2 system_core-d3ff6e5231f53a95518532ff74a7256aabbf32e5.zip |
Add android::base::Readlink.
Bug: http://b/30988271
Change-Id: Ib844d7c9e33465dabf7aee5e24dc3d1d8d799abd
Diffstat (limited to 'base')
-rw-r--r-- | base/file.cpp | 26 | ||||
-rw-r--r-- | base/file_test.cpp | 26 | ||||
-rw-r--r-- | base/include/android-base/file.h | 4 |
3 files changed, 56 insertions, 0 deletions
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 <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" @@ -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 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<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 +} 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 |