Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 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 | // |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_DESCRIPTOR_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_DESCRIPTOR_H_ |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <sys/types.h> |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 22 | #include <memory> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 23 | |
David Anderson | a4b7ba6 | 2023-05-10 21:41:37 -0700 | [diff] [blame] | 24 | #include <android-base/macros.h> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 25 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 26 | // Abstraction for managing opening, reading, writing and closing of file |
| 27 | // descriptors. This includes an abstract class and one standard implementation |
| 28 | // based on POSIX system calls. |
| 29 | // |
| 30 | // TODO(garnold) this class is modeled after (and augments the functionality of) |
| 31 | // the FileWriter class; ultimately, the latter should be replaced by the former |
| 32 | // throughout the codebase. A few deviations from the original FileWriter: |
| 33 | // |
| 34 | // * Providing two flavors of Open() |
| 35 | // |
| 36 | // * A FileDescriptor is reusable and can be used to read/write multiple files |
| 37 | // as long as open/close preconditions are respected. |
| 38 | // |
| 39 | // * Write() returns the number of bytes written: this appears to be more useful |
| 40 | // for clients, who may wish to retry or otherwise do something useful with |
| 41 | // the remaining data that was not written. |
| 42 | |
| 43 | namespace chromeos_update_engine { |
| 44 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 45 | class FileDescriptor; |
| 46 | using FileDescriptorPtr = std::shared_ptr<FileDescriptor>; |
| 47 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 48 | // An abstract class defining the file descriptor API. |
| 49 | class FileDescriptor { |
| 50 | public: |
| 51 | FileDescriptor() {} |
| 52 | virtual ~FileDescriptor() {} |
| 53 | |
| 54 | // Opens a file descriptor. The descriptor must be in the closed state prior |
| 55 | // to this call. Returns true on success, false otherwise. Specific |
| 56 | // implementations may set errno accordingly. |
| 57 | virtual bool Open(const char* path, int flags, mode_t mode) = 0; |
| 58 | virtual bool Open(const char* path, int flags) = 0; |
| 59 | |
| 60 | // Reads from a file descriptor up to a given count. The descriptor must be |
| 61 | // open prior to this call. Returns the number of bytes read, or -1 on error. |
| 62 | // Specific implementations may set errno accordingly. |
| 63 | virtual ssize_t Read(void* buf, size_t count) = 0; |
| 64 | |
| 65 | // Writes to a file descriptor. The descriptor must be open prior to this |
| 66 | // call. Returns the number of bytes written, or -1 if an error occurred and |
| 67 | // no bytes were written. Specific implementations may set errno accordingly. |
| 68 | virtual ssize_t Write(const void* buf, size_t count) = 0; |
| 69 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 70 | // Seeks to an offset. Returns the resulting offset location as measured in |
| 71 | // bytes from the beginning. On error, return -1. Specific implementations |
| 72 | // may set errno accordingly. |
| 73 | virtual off64_t Seek(off64_t offset, int whence) = 0; |
| 74 | |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 75 | // Return the size of the block device in bytes, or 0 if the device is not a |
| 76 | // block device or an error occurred. |
| 77 | virtual uint64_t BlockDevSize() = 0; |
| 78 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 79 | // Runs a ioctl() on the file descriptor if supported. Returns whether |
| 80 | // the operation is supported. The |request| can be one of BLKDISCARD, |
| 81 | // BLKZEROOUT and BLKSECDISCARD to discard, write zeros or securely discard |
| 82 | // the blocks. These ioctls accept a range of bytes (|start| and |length|) |
| 83 | // over which they perform the operation. The return value from the ioctl is |
| 84 | // stored in |result|. |
| 85 | virtual bool BlkIoctl(int request, |
| 86 | uint64_t start, |
| 87 | uint64_t length, |
| 88 | int* result) = 0; |
| 89 | |
Amin Hassani | 5192fe5 | 2017-08-28 10:28:46 -0700 | [diff] [blame] | 90 | // Flushes any cached data. The descriptor must be opened prior to this |
| 91 | // call. Returns false if it fails to write data. Implementations may set |
| 92 | // errno accrodingly. |
| 93 | virtual bool Flush() = 0; |
| 94 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 95 | // Closes a file descriptor. The descriptor must be open prior to this call. |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 96 | // Returns true on success, false otherwise. Specific implementations may set |
| 97 | // errno accordingly. |
| 98 | virtual bool Close() = 0; |
| 99 | |
| 100 | // Indicates whether or not an implementation sets meaningful errno. |
| 101 | virtual bool IsSettingErrno() = 0; |
| 102 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 103 | // Indicates whether the descriptor is currently open. |
| 104 | virtual bool IsOpen() = 0; |
| 105 | |
Kelvin Zhang | 32a1604 | 2022-10-03 11:08:40 -0700 | [diff] [blame] | 106 | // Return the wrapped underlying file descriptor. Some classes might not |
| 107 | // support this. |
| 108 | // Using read/write syscall to read from the returned file descriptor should |
| 109 | // have same effect as calling Read()/Write() method of this FileDescriptor |
| 110 | // instance. |
| 111 | virtual int Fd() { return -1; } |
| 112 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 113 | private: |
| 114 | DISALLOW_COPY_AND_ASSIGN(FileDescriptor); |
| 115 | }; |
| 116 | |
| 117 | // A simple EINTR-immune wrapper implementation around standard system calls. |
Kelvin Zhang | 32a1604 | 2022-10-03 11:08:40 -0700 | [diff] [blame] | 118 | class EintrSafeFileDescriptor final : public FileDescriptor { |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 119 | public: |
| 120 | EintrSafeFileDescriptor() : fd_(-1) {} |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 121 | ~EintrSafeFileDescriptor(); |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 122 | |
| 123 | // Interface methods. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 124 | bool Open(const char* path, int flags, mode_t mode) override; |
| 125 | bool Open(const char* path, int flags) override; |
| 126 | ssize_t Read(void* buf, size_t count) override; |
| 127 | ssize_t Write(const void* buf, size_t count) override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 128 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 129 | uint64_t BlockDevSize() override; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 130 | bool BlkIoctl(int request, |
| 131 | uint64_t start, |
| 132 | uint64_t length, |
| 133 | int* result) override; |
Amin Hassani | 5192fe5 | 2017-08-28 10:28:46 -0700 | [diff] [blame] | 134 | bool Flush() override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 135 | bool Close() override; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 136 | bool IsSettingErrno() override { return true; } |
| 137 | bool IsOpen() override { return (fd_ >= 0); } |
Kelvin Zhang | 32a1604 | 2022-10-03 11:08:40 -0700 | [diff] [blame] | 138 | int Fd() override { return fd_; } |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 139 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 140 | protected: |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 141 | int fd_; |
| 142 | }; |
| 143 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 144 | } // namespace chromeos_update_engine |
| 145 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 146 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_DESCRIPTOR_H_ |