| // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 
 | // Use of this source code is governed by a BSD-style license that can be | 
 | // found in the LICENSE file. | 
 |  | 
 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ | 
 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ | 
 |  | 
 | #include <sys/types.h> | 
 | #include <sys/stat.h> | 
 | #include <fcntl.h> | 
 | #include <unistd.h> | 
 | #include "base/logging.h" | 
 | #include "update_engine/action_processor.h" | 
 | #include "update_engine/utils.h" | 
 |  | 
 | // FileWriter is a class that is used to (synchronously, for now) write to | 
 | // a file. This file is a thin wrapper around open/write/close system calls, | 
 | // but provides and interface that can be customized by subclasses that wish | 
 | // to filter the data. | 
 |  | 
 | namespace chromeos_update_engine { | 
 |  | 
 | class FileWriter { | 
 |  public: | 
 |   FileWriter() {} | 
 |   virtual ~FileWriter() {} | 
 |  | 
 |   // Wrapper around open. Returns 0 on success or -errno on error. | 
 |   virtual int Open(const char* path, int flags, mode_t mode) = 0; | 
 |  | 
 |   // Wrapper around write. Returns true if all requested bytes | 
 |   // were written, or false on any error, reguardless of progress. | 
 |   virtual bool Write(const void* bytes, size_t count) = 0; | 
 |  | 
 |   // Same as the Write method above but returns a detailed |error| code | 
 |   // in addition if the returned value is false. By default this method | 
 |   // returns kActionExitDownloadWriteError as the error code, but subclasses | 
 |   // can override if they wish to return more specific error codes. | 
 |   virtual bool Write(const void* bytes, | 
 |                      size_t count, | 
 |                      ActionExitCode* error) { | 
 |      *error = kActionCodeDownloadWriteError; | 
 |      return Write(bytes, count); | 
 |   } | 
 |  | 
 |   // Wrapper around close. Returns 0 on success or -errno on error. | 
 |   virtual int Close() = 0; | 
 |  | 
 |  private: | 
 |   DISALLOW_COPY_AND_ASSIGN(FileWriter); | 
 | }; | 
 |  | 
 | // Direct file writer is probably the simplest FileWriter implementation. | 
 | // It calls the system calls directly. | 
 |  | 
 | class DirectFileWriter : public FileWriter { | 
 |  public: | 
 |   DirectFileWriter() : fd_(-1) {} | 
 |   virtual ~DirectFileWriter() {} | 
 |  | 
 |   virtual int Open(const char* path, int flags, mode_t mode); | 
 |   virtual bool Write(const void* bytes, size_t count); | 
 |   virtual int Close(); | 
 |  | 
 |   int fd() const { return fd_; } | 
 |  | 
 |  private: | 
 |   int fd_; | 
 |  | 
 |   DISALLOW_COPY_AND_ASSIGN(DirectFileWriter); | 
 | }; | 
 |  | 
 | class ScopedFileWriterCloser { | 
 |  public: | 
 |   explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} | 
 |   ~ScopedFileWriterCloser() { | 
 |     int err = writer_->Close(); | 
 |     if (err) | 
 |       LOG(ERROR) << "FileWriter::Close failed: " | 
 |                  << utils::ErrnoNumberAsString(-err); | 
 |   } | 
 |  private: | 
 |   FileWriter* writer_; | 
 |  | 
 |   DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser); | 
 | }; | 
 |  | 
 | }  // namespace chromeos_update_engine | 
 |  | 
 | #endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |