base: add parameter that controls O_NOFOLLOW in file functions.
Bug: http://b/31491920
Change-Id: I19cb06941d87c0180ccab8bb2d85e57338811624
Test: m
diff --git a/base/file.cpp b/base/file.cpp
index 721ab2f..6284b04 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -55,10 +55,11 @@
return (n == 0) ? true : false;
}
-bool ReadFileToString(const std::string& path, std::string* content) {
+bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) {
content->clear();
- int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_BINARY));
+ int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
+ int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags));
if (fd == -1) {
return false;
}
@@ -91,8 +92,10 @@
#if !defined(_WIN32)
bool WriteStringToFile(const std::string& content, const std::string& path,
- mode_t mode, uid_t owner, gid_t group) {
- int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY;
+ mode_t mode, uid_t owner, gid_t group,
+ bool follow_symlinks) {
+ int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
+ (follow_symlinks ? 0 : O_NOFOLLOW);
int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode));
if (fd == -1) {
PLOG(ERROR) << "android::WriteStringToFile open failed";
@@ -118,8 +121,10 @@
}
#endif
-bool WriteStringToFile(const std::string& content, const std::string& path) {
- int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY;
+bool WriteStringToFile(const std::string& content, const std::string& path,
+ bool follow_symlinks) {
+ int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
+ (follow_symlinks ? 0 : O_NOFOLLOW);
int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, DEFFILEMODE));
if (fd == -1) {
return false;