summaryrefslogtreecommitdiffstats
path: root/base/file.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2017-04-05 12:09:17 -0700
committerChristopher Ferris <cferris@google.com>2017-04-05 12:30:12 -0700
commit48d08c2c9d3c82c752012f7e16558a5afdf22748 (patch)
treee695a39912b6df70a37328a867da25fb86ba2487 /base/file.cpp
parent568dc01fdf973c2d731c9dafe7c5b73ecb6627aa (diff)
downloadsystem_core-48d08c2c9d3c82c752012f7e16558a5afdf22748.tar.gz
system_core-48d08c2c9d3c82c752012f7e16558a5afdf22748.tar.bz2
system_core-48d08c2c9d3c82c752012f7e16558a5afdf22748.zip
Convert opens to use unique_fd.
Fixes a potential leak of fds in WriteStringToFile. Test: I wrote a temporary test that failed the fchmod on host in Test: WriteStringToFile. I verified this fails with the old code after running Test: out of fds and passes with the new code. Change-Id: I168160841e35dd480d59a69bb4aa8176899fbb32
Diffstat (limited to 'base/file.cpp')
-rw-r--r--base/file.cpp19
1 files changed, 7 insertions, 12 deletions
diff --git a/base/file.cpp b/base/file.cpp
index d4e58942c..7fbebc538 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -28,8 +28,9 @@
#include <string>
#include <vector>
-#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
#include "android-base/logging.h"
+#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
+#include "android-base/unique_fd.h"
#include "android-base/utf8.h"
#include "utils/Compat.h"
@@ -69,13 +70,11 @@ bool ReadFileToString(const std::string& path, std::string* content, bool follow
content->clear();
int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
- int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags));
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags)));
if (fd == -1) {
return false;
}
- bool result = ReadFdToString(fd, content);
- close(fd);
- return result;
+ return ReadFdToString(fd, content);
}
bool WriteStringToFd(const std::string& content, int fd) {
@@ -106,7 +105,7 @@ 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, mode));
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
if (fd == -1) {
PLOG(ERROR) << "android::WriteStringToFile open failed";
return false;
@@ -126,7 +125,6 @@ bool WriteStringToFile(const std::string& content, const std::string& path,
PLOG(ERROR) << "android::WriteStringToFile write failed";
return CleanUpAfterFailedWrite(path);
}
- close(fd);
return true;
}
#endif
@@ -135,14 +133,11 @@ 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));
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, DEFFILEMODE)));
if (fd == -1) {
return false;
}
-
- bool result = WriteStringToFd(content, fd);
- close(fd);
- return result || CleanUpAfterFailedWrite(path);
+ return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path);
}
bool ReadFully(int fd, void* data, size_t byte_count) {