update_engine: Add MtdFileDescriptor and UbiFileDescriptor

We send a pure file descriptor to ExtentWriter. This CL changes that to
use FileDescriptor. At the same time, the CL adds two other
FileDescriptor classes MtdFileDescriptor and UbiFileDescriptor to work
with raw NAND and UBI devices. Both of these classes support either read
only or sequential write, but not both at the same time. Seek operation
is possible in read only mode.

These classes are conditionally included if USE_mtd is not '0'.

BUG=chromium:426742
TEST=unittest
TEST=USE=mtd emerge update_engine, make sure there is MtdFileDescriptor
     in /usr/sbin/update_engine
TEST=emerge --unmerge android_mtdutils; USE=-mtd emerge update_engine
     make sure there is no UbiFileDescriptor in that same file

Change-Id: If3ba43677d93dc4f3cea037f19866c8b546b2cae
Reviewed-on: https://chromium-review.googlesource.com/229004
Reviewed-by: Alex Vakulenko <[email protected]>
Reviewed-by: Alex Deymo <[email protected]>
Commit-Queue: Nam Nguyen <[email protected]>
Tested-by: Nam Nguyen <[email protected]>
diff --git a/file_descriptor.h b/file_descriptor.h
index d86d4ca..78647de 100644
--- a/file_descriptor.h
+++ b/file_descriptor.h
@@ -6,12 +6,11 @@
 #define UPDATE_ENGINE_FILE_DESCRIPTOR_H_
 
 #include <errno.h>
+#include <memory>
 #include <sys/types.h>
 
 #include <base/logging.h>
 
-#include "update_engine/utils.h"
-
 // Abstraction for managing opening, reading, writing and closing of file
 // descriptors. This includes an abstract class and one standard implementation
 // based on POSIX system calls.
@@ -37,6 +36,9 @@
 
 namespace chromeos_update_engine {
 
+class FileDescriptor;
+using FileDescriptorPtr = std::shared_ptr<FileDescriptor>;
+
 // An abstract class defining the file descriptor API.
 class FileDescriptor {
  public:
@@ -59,6 +61,11 @@
   // no bytes were written. Specific implementations may set errno accordingly.
   virtual ssize_t Write(const void* buf, size_t count) = 0;
 
+  // Seeks to an offset. Returns the resulting offset location as measured in
+  // bytes from the beginning. On error, return -1. Specific implementations
+  // may set errno accordingly.
+  virtual off64_t Seek(off64_t offset, int whence) = 0;
+
   // Closes a file descriptor. The descriptor must be open prior to this call.
   // Returns true on success, false otherwise. Specific implementations may set
   // errno accordingly.
@@ -88,6 +95,7 @@
   bool Open(const char* path, int flags) override;
   ssize_t Read(void* buf, size_t count) override;
   ssize_t Write(const void* buf, size_t count) override;
+  off64_t Seek(off64_t offset, int whence) override;
   bool Close() override;
   void Reset() override;
   bool IsSettingErrno() override {
@@ -97,7 +105,7 @@
     return (fd_ >= 0);
   }
 
- private:
+ protected:
   int fd_;
 };