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" |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 13 | #include "update_engine/action_processor.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 14 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | |
| 16 | // FileWriter is a class that is used to (synchronously, for now) write to |
| 17 | // a file. This file is a thin wrapper around open/write/close system calls, |
| 18 | // but provides and interface that can be customized by subclasses that wish |
| 19 | // to filter the data. |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class FileWriter { |
| 24 | public: |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 25 | FileWriter() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | virtual ~FileWriter() {} |
| 27 | |
| 28 | // Wrapper around open. Returns 0 on success or -errno on error. |
| 29 | virtual int Open(const char* path, int flags, mode_t mode) = 0; |
| 30 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 31 | // Wrapper around write. Returns true if all requested bytes |
| 32 | // were written, or false on any error, reguardless of progress. |
| 33 | virtual bool Write(const void* bytes, size_t count) = 0; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 35 | // Same as the Write method above but returns a detailed |error| code |
| 36 | // in addition if the returned value is false. By default this method |
| 37 | // returns kActionExitDownloadWriteError as the error code, but subclasses |
| 38 | // can override if they wish to return more specific error codes. |
| 39 | virtual bool Write(const void* bytes, |
| 40 | size_t count, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 41 | ErrorCode* error) { |
| 42 | *error = kErrorCodeDownloadWriteError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 43 | return Write(bytes, count); |
| 44 | } |
| 45 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | // Wrapper around close. Returns 0 on success or -errno on error. |
| 47 | virtual int Close() = 0; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | DISALLOW_COPY_AND_ASSIGN(FileWriter); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // Direct file writer is probably the simplest FileWriter implementation. |
| 54 | // It calls the system calls directly. |
| 55 | |
| 56 | class DirectFileWriter : public FileWriter { |
| 57 | public: |
| 58 | DirectFileWriter() : fd_(-1) {} |
| 59 | virtual ~DirectFileWriter() {} |
| 60 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 61 | virtual int Open(const char* path, int flags, mode_t mode); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 62 | virtual bool Write(const void* bytes, size_t count); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 63 | virtual int Close(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 65 | int fd() const { return fd_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | int fd_; |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 69 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(DirectFileWriter); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 73 | class ScopedFileWriterCloser { |
| 74 | public: |
| 75 | explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} |
| 76 | ~ScopedFileWriterCloser() { |
| 77 | int err = writer_->Close(); |
| 78 | if (err) |
| 79 | LOG(ERROR) << "FileWriter::Close failed: " |
| 80 | << utils::ErrnoNumberAsString(-err); |
| 81 | } |
| 82 | private: |
| 83 | FileWriter* writer_; |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 84 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 85 | DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | } // namespace chromeos_update_engine |
| 89 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 90 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |