rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <unistd.h> |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 12 | #include "base/logging.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 13 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | |
| 15 | // FileWriter is a class that is used to (synchronously, for now) write to |
| 16 | // a file. This file is a thin wrapper around open/write/close system calls, |
| 17 | // but provides and interface that can be customized by subclasses that wish |
| 18 | // to filter the data. |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class FileWriter { |
| 23 | public: |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 24 | FileWriter() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | virtual ~FileWriter() {} |
| 26 | |
| 27 | // Wrapper around open. Returns 0 on success or -errno on error. |
| 28 | virtual int Open(const char* path, int flags, mode_t mode) = 0; |
| 29 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 30 | // Wrapper around write. Returns true if all requested bytes |
| 31 | // were written, or false on any error, reguardless of progress. |
| 32 | virtual bool Write(const void* bytes, size_t count) = 0; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
| 34 | // Wrapper around close. Returns 0 on success or -errno on error. |
| 35 | virtual int Close() = 0; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | DISALLOW_COPY_AND_ASSIGN(FileWriter); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | // Direct file writer is probably the simplest FileWriter implementation. |
| 42 | // It calls the system calls directly. |
| 43 | |
| 44 | class DirectFileWriter : public FileWriter { |
| 45 | public: |
| 46 | DirectFileWriter() : fd_(-1) {} |
| 47 | virtual ~DirectFileWriter() {} |
| 48 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 49 | virtual int Open(const char* path, int flags, mode_t mode); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 50 | virtual bool Write(const void* bytes, size_t count); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 51 | virtual int Close(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 53 | int fd() const { return fd_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | int fd_; |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 57 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 58 | DISALLOW_COPY_AND_ASSIGN(DirectFileWriter); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 61 | class ScopedFileWriterCloser { |
| 62 | public: |
| 63 | explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} |
| 64 | ~ScopedFileWriterCloser() { |
| 65 | int err = writer_->Close(); |
| 66 | if (err) |
| 67 | LOG(ERROR) << "FileWriter::Close failed: " |
| 68 | << utils::ErrnoNumberAsString(-err); |
| 69 | } |
| 70 | private: |
| 71 | FileWriter* writer_; |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 72 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | } // namespace chromeos_update_engine |
| 77 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 78 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |