Use ErrnoNumberAsString from libbase
Calling strerror_r is tricky, as glibc provides the GNU version
if _GNU_SOURCE is set, and musl libc only provides the posix version.
Handle the complexity once in libbase and reuse it here.
Bug: 190084016
Test: m USE_HOST_MUSL=true host-native
Change-Id: Id7b600d0cdd5804c75612c4d253637a281d3f0ff
diff --git a/common/utils.cc b/common/utils.cc
index 002e6a0..aa6c6b3 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -38,6 +38,7 @@
#include <utility>
#include <vector>
+#include <android-base/strings.h>
#include <base/callback.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
@@ -489,12 +490,6 @@
return partition_name;
}
-string ErrnoNumberAsString(int err) {
- char buf[100];
- buf[0] = '\0';
- return strerror_r(err, buf, sizeof(buf));
-}
-
bool FileExists(const char* path) {
struct stat stbuf;
return 0 == lstat(path, &stbuf);
diff --git a/common/utils.h b/common/utils.h
index 46e41f0..a33efb2 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -30,6 +30,7 @@
#include <string>
#include <vector>
+#include <android-base/strings.h>
#include <base/files/file_path.h>
#include <base/posix/eintr_wrapper.h>
#include <base/strings/string_number_conversions.h>
@@ -141,8 +142,6 @@
bool SendFile(int out_fd, int in_fd, size_t count);
-std::string ErrnoNumberAsString(int err);
-
// Returns true if the file exists for sure. Returns false if it doesn't exist,
// or an error occurs.
bool FileExists(const char* path);
@@ -528,15 +527,14 @@
} // namespace chromeos_update_engine
-#define TEST_AND_RETURN_FALSE_ERRNO(_x) \
- do { \
- bool _success = static_cast<bool>(_x); \
- if (!_success) { \
- std::string _msg = \
- chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
- LOG(ERROR) << #_x " failed: " << _msg; \
- return false; \
- } \
+#define TEST_AND_RETURN_FALSE_ERRNO(_x) \
+ do { \
+ bool _success = static_cast<bool>(_x); \
+ if (!_success) { \
+ std::string _msg = android::base::ErrnoNumberAsString(errno); \
+ LOG(ERROR) << #_x " failed: " << _msg; \
+ return false; \
+ } \
} while (0)
#define TEST_AND_RETURN_FALSE(_x) \
@@ -548,15 +546,14 @@
} \
} while (0)
-#define TEST_AND_RETURN_ERRNO(_x) \
- do { \
- bool _success = static_cast<bool>(_x); \
- if (!_success) { \
- std::string _msg = \
- chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
- LOG(ERROR) << #_x " failed: " << _msg; \
- return; \
- } \
+#define TEST_AND_RETURN_ERRNO(_x) \
+ do { \
+ bool _success = static_cast<bool>(_x); \
+ if (!_success) { \
+ std::string _msg = android::base::ErrnoNumberAsString(errno); \
+ LOG(ERROR) << #_x " failed: " << _msg; \
+ return; \
+ } \
} while (0)
#define TEST_AND_RETURN(_x) \
diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc
index 20c6b84..739f298 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -77,10 +77,6 @@
EXPECT_EQ(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20), in_data);
}
-TEST(UtilsTest, ErrnoNumberAsStringTest) {
- EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
-}
-
TEST(UtilsTest, IsSymlinkTest) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());