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/aosp/update_attempter_android_integration_test.cc b/aosp/update_attempter_android_integration_test.cc
index 3ce06eb..909aa3c 100644
--- a/aosp/update_attempter_android_integration_test.cc
+++ b/aosp/update_attempter_android_integration_test.cc
@@ -23,6 +23,7 @@
#include <sys/sendfile.h>
#include <unistd.h>
+#include <android-base/strings.h>
#include <brillo/data_encoding.h>
#include <brillo/message_loops/fake_message_loop.h>
#include <bsdiff/bsdiff.h>
@@ -153,7 +154,7 @@
&source_part));
int out_fd = open(source_part.c_str(), O_RDWR);
ScopedFdCloser closer{&out_fd};
- ASSERT_GE(out_fd, 0) << utils::ErrnoNumberAsString(errno);
+ ASSERT_GE(out_fd, 0) << android::base::ErrnoNumberAsString(errno);
ASSERT_TRUE(utils::SendFile(out_fd, old_part_.fd(), kFakePartitionSize));
}
@@ -218,12 +219,12 @@
ASSERT_TRUE(utils::ReadExtents(
old_part_.path(), op.src_extents(), &old_data, kBlockSize))
<< "Failed to read source data: "
- << utils::ErrnoNumberAsString(errno);
+ << android::base::ErrnoNumberAsString(errno);
brillo::Blob new_data;
ASSERT_TRUE(utils::ReadExtents(
new_part_.path(), op.dst_extents(), &new_data, kBlockSize))
<< "Failed to read target data: "
- << utils::ErrnoNumberAsString(errno);
+ << android::base::ErrnoNumberAsString(errno);
ScopedTempFile patch_file{"bspatch.XXXXXX", true};
ASSERT_EQ(bsdiff::bsdiff(old_data.data(),
old_data.size(),
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());
diff --git a/payload_consumer/file_writer.h b/payload_consumer/file_writer.h
index cdc9fa0..ec391a3 100644
--- a/payload_consumer/file_writer.h
+++ b/payload_consumer/file_writer.h
@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include <android-base/strings.h>
#include <base/logging.h>
#include "update_engine/common/error_code.h"
@@ -88,7 +89,7 @@
int err = writer_->Close();
if (err)
LOG(ERROR) << "FileWriter::Close failed: "
- << utils::ErrnoNumberAsString(-err);
+ << android::base::ErrnoNumberAsString(-err);
}
private: