Remove FileWriter from common/utils.h am: 335516c673 am: febf550281 am: f5111d3f18
am: 3ed3cd91a9
Change-Id: I05d83054c0116799fadd2c62b80468cc797ddf8b
diff --git a/common/utils.cc b/common/utils.cc
index 1e04b61..1cb37e5 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -57,7 +57,6 @@
#include "update_engine/common/prefs_interface.h"
#include "update_engine/common/subprocess.h"
#include "update_engine/payload_consumer/file_descriptor.h"
-#include "update_engine/payload_consumer/file_writer.h"
#include "update_engine/payload_consumer/payload_constants.h"
using base::Time;
@@ -189,14 +188,11 @@
return "";
}
-bool WriteFile(const char* path, const void* data, int data_len) {
- DirectFileWriter writer;
- TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
- O_WRONLY | O_CREAT | O_TRUNC,
- 0600));
- ScopedFileWriterCloser closer(&writer);
- TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len));
- return true;
+bool WriteFile(const char* path, const void* data, size_t data_len) {
+ int fd = HANDLE_EINTR(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600));
+ TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
+ ScopedFdCloser fd_closer(&fd);
+ return WriteAll(fd, data, data_len);
}
bool ReadAll(
diff --git a/common/utils.h b/common/utils.h
index 3cffcdd..d551b7b 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -62,7 +62,7 @@
// Writes the data passed to path. The file at path will be overwritten if it
// exists. Returns true on success, false otherwise.
-bool WriteFile(const char* path, const void* data, int data_len);
+bool WriteFile(const char* path, const void* data, size_t data_len);
// Calls write() or pwrite() repeatedly until all count bytes at buf are
// written to fd or an error occurs. Returns true on success.
diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc
index 634de01..7852910 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -52,6 +52,21 @@
EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
}
+TEST(UtilsTest, WriteFileOpenFailure) {
+ EXPECT_FALSE(utils::WriteFile("/this/doesn't/exist", "hello", 5));
+}
+
+TEST(UtilsTest, WriteFileReadFile) {
+ base::FilePath file;
+ EXPECT_TRUE(base::CreateTemporaryFile(&file));
+ ScopedPathUnlinker unlinker(file.value());
+ EXPECT_TRUE(utils::WriteFile(file.value().c_str(), "hello", 5));
+
+ brillo::Blob readback;
+ EXPECT_TRUE(utils::ReadFile(file.value().c_str(), &readback));
+ EXPECT_EQ("hello", string(readback.begin(), readback.end()));
+}
+
TEST(UtilsTest, ReadFileFailure) {
brillo::Blob empty;
EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));