Stop using C++ streams.
libvintf used to use std::ifstream to read files but incorrectly
assumed that a non-zero value of errno implied failure. Because the
implementation retries reads that fail with EAGAIN, this isn't true:
a successful operation could still result in errno being set to EAGAIN.
Use ReadFileToString from libbase which gives a clear boolean
success/failure signal.
Test: stress test
Bug: 156701275
Change-Id: Ieedba32f61a76ffcf96f7c06d89df70b5f94813b
diff --git a/FileSystem.cpp b/FileSystem.cpp
index 6b1f73a..3c3a591 100644
--- a/FileSystem.cpp
+++ b/FileSystem.cpp
@@ -19,9 +19,7 @@
#include <dirent.h>
-#include <fstream>
-#include <iostream>
-#include <sstream>
+#include <android-base/file.h>
namespace android {
namespace vintf {
@@ -29,22 +27,13 @@
status_t FileSystemImpl::fetch(const std::string& path, std::string* fetched,
std::string* error) const {
- std::ifstream in;
-
- errno = 0;
- in.open(path);
- if (!in || errno != 0) {
+ if (!android::base::ReadFileToString(path, fetched, true /* follow_symlinks */)) {
if (error) {
- *error = "Cannot open " + path + ": " + strerror(errno);
+ *error = "Cannot read " + path + ": " + strerror(errno);
}
- return -errno;
+ return errno == 0 ? UNKNOWN_ERROR : -errno;
}
-
- std::stringstream ss;
- ss << in.rdbuf();
- *fetched = ss.str();
-
- return -errno;
+ return OK;
}
status_t FileSystemImpl::listFiles(const std::string& path, std::vector<std::string>* out,