| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_WRITER_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_WRITER_H_ | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <fcntl.h> | 
| Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> | 
|  | 22 | #include <sys/types.h> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | #include <unistd.h> | 
| Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 24 |  | 
| Colin Cross | 84fe9da | 2021-12-21 13:19:14 -0800 | [diff] [blame] | 25 | #include <android-base/strings.h> | 
| Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 26 | #include <base/logging.h> | 
|  | 27 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/error_code.h" | 
|  | 29 | #include "update_engine/common/utils.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 |  | 
|  | 31 | // FileWriter is a class that is used to (synchronously, for now) write to | 
|  | 32 | // a file. This file is a thin wrapper around open/write/close system calls, | 
|  | 33 | // but provides and interface that can be customized by subclasses that wish | 
|  | 34 | // to filter the data. | 
|  | 35 |  | 
|  | 36 | namespace chromeos_update_engine { | 
|  | 37 |  | 
|  | 38 | class FileWriter { | 
|  | 39 | public: | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | FileWriter() {} | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 | virtual ~FileWriter() {} | 
|  | 42 |  | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 43 | // Wrapper around write. Returns true if all requested bytes | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 44 | // were written, or false on any error, regardless of progress. | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 45 | virtual bool Write(const void* bytes, size_t count) = 0; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 47 | // Same as the Write method above but returns a detailed |error| code | 
|  | 48 | // in addition if the returned value is false. By default this method | 
|  | 49 | // returns kActionExitDownloadWriteError as the error code, but subclasses | 
|  | 50 | // can override if they wish to return more specific error codes. | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 51 | virtual bool Write(const void* bytes, size_t count, ErrorCode* error) { | 
|  | 52 | *error = ErrorCode::kDownloadWriteError; | 
|  | 53 | return Write(bytes, count); | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | // Wrapper around close. Returns 0 on success or -errno on error. | 
|  | 57 | virtual int Close() = 0; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | private: | 
|  | 60 | DISALLOW_COPY_AND_ASSIGN(FileWriter); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
|  | 63 | // Direct file writer is probably the simplest FileWriter implementation. | 
|  | 64 | // It calls the system calls directly. | 
|  | 65 |  | 
|  | 66 | class DirectFileWriter : public FileWriter { | 
|  | 67 | public: | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 68 | DirectFileWriter() = default; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 70 | // FileWriter overrides. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 71 | bool Write(const void* bytes, size_t count) override; | 
|  | 72 | int Close() override; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 74 | // Wrapper around open. Returns 0 on success or -errno on error. | 
|  | 75 | int Open(const char* path, int flags, mode_t mode); | 
|  | 76 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 77 | int fd() const { return fd_; } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | private: | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 80 | int fd_{-1}; | 
| Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 81 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(DirectFileWriter); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 85 | class ScopedFileWriterCloser { | 
|  | 86 | public: | 
|  | 87 | explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} | 
|  | 88 | ~ScopedFileWriterCloser() { | 
|  | 89 | int err = writer_->Close(); | 
|  | 90 | if (err) | 
|  | 91 | LOG(ERROR) << "FileWriter::Close failed: " | 
| Colin Cross | 84fe9da | 2021-12-21 13:19:14 -0800 | [diff] [blame] | 92 | << android::base::ErrnoNumberAsString(-err); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 93 | } | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 94 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 95 | private: | 
|  | 96 | FileWriter* writer_; | 
| Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 97 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 99 | }; | 
|  | 100 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | }  // namespace chromeos_update_engine | 
|  | 102 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 103 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_WRITER_H_ |