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 <avakulenko@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Nam Nguyen <namnguyen@chromium.org>
Tested-by: Nam Nguyen <namnguyen@chromium.org>
diff --git a/extent_writer.h b/extent_writer.h
index 883507a..0ea39fc 100644
--- a/extent_writer.h
+++ b/extent_writer.h
@@ -9,6 +9,7 @@
 
 #include <base/logging.h>
 
+#include "update_engine/file_descriptor.h"
 #include "update_engine/update_metadata.pb.h"
 #include "update_engine/utils.h"
 
@@ -25,7 +26,7 @@
   }
 
   // Returns true on success.
-  virtual bool Init(int fd,
+  virtual bool Init(FileDescriptorPtr fd,
                     const std::vector<Extent>& extents,
                     uint32_t block_size) = 0;
 
@@ -49,13 +50,15 @@
 class DirectExtentWriter : public ExtentWriter {
  public:
   DirectExtentWriter()
-      : fd_(-1),
+      : fd_(nullptr),
         block_size_(0),
         extent_bytes_written_(0),
         next_extent_index_(0) {}
   ~DirectExtentWriter() {}
 
-  bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) {
+  bool Init(FileDescriptorPtr fd,
+            const std::vector<Extent>& extents,
+            uint32_t block_size) {
     fd_ = fd;
     block_size_ = block_size;
     extents_ = extents;
@@ -67,7 +70,7 @@
   }
 
  private:
-  int fd_;
+  FileDescriptorPtr fd_;
 
   size_t block_size_;
   // Bytes written into next_extent_index_ thus far
@@ -90,7 +93,9 @@
         bytes_written_mod_block_size_(0) {}
   ~ZeroPadExtentWriter() {}
 
-  bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) {
+  bool Init(FileDescriptorPtr fd,
+            const std::vector<Extent>& extents,
+            uint32_t block_size) {
     block_size_ = block_size;
     return underlying_extent_writer_->Init(fd, extents, block_size);
   }