summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/file.cpp26
-rw-r--r--base/file_test.cpp26
-rw-r--r--base/include/android-base/file.h4
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