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