Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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_DESCRIPTOR_H_ |
| 6 | #define UPDATE_ENGINE_FILE_DESCRIPTOR_H_ |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 7 | |
| 8 | #include <errno.h> |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame^] | 9 | #include <memory> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 12 | #include <base/logging.h> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 13 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 14 | // Abstraction for managing opening, reading, writing and closing of file |
| 15 | // descriptors. This includes an abstract class and one standard implementation |
| 16 | // based on POSIX system calls. |
| 17 | // |
| 18 | // TODO(garnold) this class is modeled after (and augments the functionality of) |
| 19 | // the FileWriter class; ultimately, the latter should be replaced by the former |
| 20 | // throughout the codebase. A few deviations from the original FileWriter: |
| 21 | // |
| 22 | // * Providing two flavors of Open() |
| 23 | // |
| 24 | // * A FileDescriptor is reusable and can be used to read/write multiple files |
| 25 | // as long as open/close preconditions are respected. |
| 26 | // |
| 27 | // * Write() returns the number of bytes written: this appears to be more useful |
| 28 | // for clients, who may wish to retry or otherwise do something useful with |
| 29 | // the remaining data that was not written. |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 30 | // |
| 31 | // * Provides a Reset() method, which will force to abandon a currently open |
| 32 | // file descriptor and allow opening another file, without necessarily |
| 33 | // properly closing the old one. This may be useful in cases where a "closer" |
| 34 | // class does not care whether Close() was successful, but may need to reuse |
| 35 | // the same file descriptor again. |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame^] | 39 | class FileDescriptor; |
| 40 | using FileDescriptorPtr = std::shared_ptr<FileDescriptor>; |
| 41 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 42 | // An abstract class defining the file descriptor API. |
| 43 | class FileDescriptor { |
| 44 | public: |
| 45 | FileDescriptor() {} |
| 46 | virtual ~FileDescriptor() {} |
| 47 | |
| 48 | // Opens a file descriptor. The descriptor must be in the closed state prior |
| 49 | // to this call. Returns true on success, false otherwise. Specific |
| 50 | // implementations may set errno accordingly. |
| 51 | virtual bool Open(const char* path, int flags, mode_t mode) = 0; |
| 52 | virtual bool Open(const char* path, int flags) = 0; |
| 53 | |
| 54 | // Reads from a file descriptor up to a given count. The descriptor must be |
| 55 | // open prior to this call. Returns the number of bytes read, or -1 on error. |
| 56 | // Specific implementations may set errno accordingly. |
| 57 | virtual ssize_t Read(void* buf, size_t count) = 0; |
| 58 | |
| 59 | // Writes to a file descriptor. The descriptor must be open prior to this |
| 60 | // call. Returns the number of bytes written, or -1 if an error occurred and |
| 61 | // no bytes were written. Specific implementations may set errno accordingly. |
| 62 | virtual ssize_t Write(const void* buf, size_t count) = 0; |
| 63 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame^] | 64 | // Seeks to an offset. Returns the resulting offset location as measured in |
| 65 | // bytes from the beginning. On error, return -1. Specific implementations |
| 66 | // may set errno accordingly. |
| 67 | virtual off64_t Seek(off64_t offset, int whence) = 0; |
| 68 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 69 | // 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] | 70 | // Returns true on success, false otherwise. Specific implementations may set |
| 71 | // errno accordingly. |
| 72 | virtual bool Close() = 0; |
| 73 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 74 | // Resets the file descriptor, abandoning a currently open file and returning |
| 75 | // the descriptor to the closed state. |
| 76 | virtual void Reset() = 0; |
| 77 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 78 | // Indicates whether or not an implementation sets meaningful errno. |
| 79 | virtual bool IsSettingErrno() = 0; |
| 80 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 81 | // Indicates whether the descriptor is currently open. |
| 82 | virtual bool IsOpen() = 0; |
| 83 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 84 | private: |
| 85 | DISALLOW_COPY_AND_ASSIGN(FileDescriptor); |
| 86 | }; |
| 87 | |
| 88 | // A simple EINTR-immune wrapper implementation around standard system calls. |
| 89 | class EintrSafeFileDescriptor : public FileDescriptor { |
| 90 | public: |
| 91 | EintrSafeFileDescriptor() : fd_(-1) {} |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 92 | |
| 93 | // Interface methods. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 94 | bool Open(const char* path, int flags, mode_t mode) override; |
| 95 | bool Open(const char* path, int flags) override; |
| 96 | ssize_t Read(void* buf, size_t count) override; |
| 97 | ssize_t Write(const void* buf, size_t count) override; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame^] | 98 | off64_t Seek(off64_t offset, int whence) override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 99 | bool Close() override; |
| 100 | void Reset() override; |
| 101 | bool IsSettingErrno() override { |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 102 | return true; |
| 103 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 104 | bool IsOpen() override { |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 105 | return (fd_ >= 0); |
| 106 | } |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 107 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame^] | 108 | protected: |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 109 | int fd_; |
| 110 | }; |
| 111 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 112 | // A scoped closer for a FileDescriptor object. The destructor of this class |
| 113 | // invokes the Close() method of the given file descriptor, if it's not in the |
| 114 | // closed state already. Note, however, that if Close() fails, this class will |
| 115 | // force a Reset() invocation, which will abandon the current file descriptor. |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 116 | class ScopedFileDescriptorCloser { |
| 117 | public: |
| 118 | explicit ScopedFileDescriptorCloser(FileDescriptor* descriptor) |
| 119 | : descriptor_(descriptor) {} |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 120 | ~ScopedFileDescriptorCloser(); |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 121 | private: |
| 122 | FileDescriptor* descriptor_; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(ScopedFileDescriptorCloser); |
| 125 | }; |
| 126 | |
| 127 | } // namespace chromeos_update_engine |
| 128 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 129 | #endif // UPDATE_ENGINE_FILE_DESCRIPTOR_H_ |