Index: util/file/file_writer.h |
diff --git a/util/file/file_writer.h b/util/file/file_writer.h |
index 7b9524fc537caa22cbf057e66181993093d99029..62fddf5f37180ef39c78c3c6ecfeda726d6d30aa 100644 |
--- a/util/file/file_writer.h |
+++ b/util/file/file_writer.h |
@@ -75,6 +75,50 @@ class FileWriterInterface { |
~FileWriterInterface() {} |
}; |
+//! \brief A file writer backed by a FileHandle. |
+//! |
+//! FileWriter requires users to provide a FilePath to open, but this class |
+//! accepts an already-open FileHandle instead. Like FileWriter, this class may |
+//! write to a filesystem-based file, but unlike FileWriter, this class is not |
+//! responsible for creating or closing the file. Users of this class must |
+//! ensure that the file handle is closed appropriately elsewhere. Objects of |
+//! this class may be used to write to file handles not associated with |
+//! filesystem-based files, although special attention should be paid to the |
+//! Seek() method, which may not function on file handles that do not refer to |
+//! disk-based files. |
+//! |
+//! This class is expected to be used when other code is responsible for |
+//! creating files and already provides file handles. |
+class WeakFileHandleFileWriter : public FileWriterInterface { |
+ public: |
+ explicit WeakFileHandleFileWriter(FileHandle file_handle); |
+ ~WeakFileHandleFileWriter(); |
+ |
+ // FileWriterInterface: |
+ bool Write(const void* data, size_t size) override; |
+ bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; |
+ |
+ //! \copydoc FileWriterInterface::Seek() |
+ //! |
+ //! \note This method is only guaranteed to function on file handles referring |
+ //! to disk-based files. |
+ FileOffset Seek(FileOffset offset, int whence) override; |
+ |
+ private: |
+ void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } |
+ |
+ FileHandle file_handle_; // weak |
+ |
+ // FileWriter uses this class as its internal implementation, and it needs to |
+ // be able to call set_file_handle(). FileWriter cannot initialize an |
+ // WeakFileHandleFileWriter with a correct file descriptor at the time of |
+ // construction because no file descriptor will be available until |
+ // FileWriter::Open() is called. |
+ friend class FileWriter; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter); |
+}; |
+ |
//! \brief A file writer implementation that wraps traditional system file |
//! operations on files accessed through the filesystem. |
class FileWriter : public FileWriterInterface { |
@@ -122,6 +166,7 @@ class FileWriter : public FileWriterInterface { |
private: |
ScopedFileHandle file_; |
+ WeakFileHandleFileWriter weak_file_handle_file_writer_; |
DISALLOW_COPY_AND_ASSIGN(FileWriter); |
}; |