Remove FileWriter from common/utils.h

The FileWriter abstraction is used to write the payload to the
DeltaPerformer. This patch removes its usage from the utils.h and
replaces it with the WriteAll() function already existing in utils.h

Bug: None
Test: Added unittests.

Change-Id: I2d9251565c8978af41ee55f7eae2cede56a72ac2
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));