Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_ |
| 18 | #define ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <string> |
| 22 | #include "base/unix_file/random_access_file.h" |
| 23 | #include "base/macros.h" |
| 24 | |
| 25 | namespace unix_file { |
| 26 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 27 | // If true, check whether Flush and Close are called before destruction. |
| 28 | static constexpr bool kCheckSafeUsage = true; |
| 29 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | // A RandomAccessFile implementation backed by a file descriptor. |
| 31 | // |
| 32 | // Not thread safe. |
| 33 | class FdFile : public RandomAccessFile { |
| 34 | public: |
| 35 | FdFile(); |
| 36 | // Creates an FdFile using the given file descriptor. Takes ownership of the |
| 37 | // file descriptor. (Use DisableAutoClose to retain ownership.) |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 38 | FdFile(int fd, bool checkUsage); |
| 39 | FdFile(int fd, const std::string& path, bool checkUsage); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 40 | FdFile(int fd, const std::string& path, bool checkUsage, bool read_only_mode); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 41 | |
| 42 | // Destroys an FdFile, closing the file descriptor if Close hasn't already |
| 43 | // been called. (If you care about the return value of Close, call it |
| 44 | // yourself; this is meant to handle failure cases and read-only accesses. |
| 45 | // Note though that calling Close and checking its return value is still no |
| 46 | // guarantee that data actually made it to stable storage.) |
| 47 | virtual ~FdFile(); |
| 48 | |
| 49 | // Opens file 'file_path' using 'flags' and 'mode'. |
| 50 | bool Open(const std::string& file_path, int flags); |
| 51 | bool Open(const std::string& file_path, int flags, mode_t mode); |
| 52 | |
| 53 | // RandomAccessFile API. |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 54 | int Close() OVERRIDE WARN_UNUSED; |
| 55 | int64_t Read(char* buf, int64_t byte_count, int64_t offset) const OVERRIDE WARN_UNUSED; |
| 56 | int SetLength(int64_t new_length) OVERRIDE WARN_UNUSED; |
| 57 | int64_t GetLength() const OVERRIDE; |
| 58 | int64_t Write(const char* buf, int64_t byte_count, int64_t offset) OVERRIDE WARN_UNUSED; |
| 59 | int Flush() OVERRIDE WARN_UNUSED; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 60 | |
| 61 | // Short for SetLength(0); Flush(); Close(); |
| 62 | void Erase(); |
| 63 | |
| 64 | // Try to Flush(), then try to Close(); If either fails, call Erase(). |
| 65 | int FlushCloseOrErase() WARN_UNUSED; |
| 66 | |
| 67 | // Try to Flush and Close(). Attempts both, but returns the first error. |
| 68 | int FlushClose() WARN_UNUSED; |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 69 | |
| 70 | // Bonus API. |
| 71 | int Fd() const; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 72 | bool ReadOnlyMode() const; |
| 73 | bool CheckUsage() const; |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 74 | bool IsOpened() const; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 75 | const std::string& GetPath() const { |
| 76 | return file_path_; |
| 77 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 78 | void DisableAutoClose(); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 79 | bool ReadFully(void* buffer, size_t byte_count) WARN_UNUSED; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 80 | bool PreadFully(void* buffer, size_t byte_count, size_t offset) WARN_UNUSED; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 81 | bool WriteFully(const void* buffer, size_t byte_count) WARN_UNUSED; |
Mathieu Chartier | 6f6b134 | 2016-03-09 11:14:50 -0800 | [diff] [blame] | 82 | bool PwriteFully(const void* buffer, size_t byte_count, size_t offset) WARN_UNUSED; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 83 | |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 84 | // Copy data from another file. |
| 85 | bool Copy(FdFile* input_file, int64_t offset, int64_t size); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 86 | // Clears the file content and resets the file offset to 0. |
| 87 | // Returns true upon success, false otherwise. |
| 88 | bool ClearContent(); |
| 89 | // Resets the file offset to the beginning of the file. |
| 90 | bool ResetOffset(); |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 91 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 92 | // This enum is public so that we can define the << operator over it. |
| 93 | enum class GuardState { |
| 94 | kBase, // Base, file has not been flushed or closed. |
| 95 | kFlushed, // File has been flushed, but not closed. |
| 96 | kClosed, // File has been flushed and closed. |
| 97 | kNoCheck // Do not check for the current file instance. |
| 98 | }; |
| 99 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 100 | // WARNING: Only use this when you know what you're doing! |
| 101 | void MarkUnchecked(); |
| 102 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 103 | protected: |
| 104 | // If the guard state indicates checking (!=kNoCheck), go to the target state "target". Print the |
| 105 | // given warning if the current state is or exceeds warn_threshold. |
| 106 | void moveTo(GuardState target, GuardState warn_threshold, const char* warning); |
| 107 | |
| 108 | // If the guard state indicates checking (<kNoCheck), and is below the target state "target", go |
| 109 | // to "target." If the current state is higher (excluding kNoCheck) than the trg state, print the |
| 110 | // warning. |
| 111 | void moveUp(GuardState target, const char* warning); |
| 112 | |
| 113 | // Forcefully sets the state to the given one. This can overwrite kNoCheck. |
| 114 | void resetGuard(GuardState new_state) { |
| 115 | if (kCheckSafeUsage) { |
| 116 | guard_state_ = new_state; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | GuardState guard_state_; |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 121 | |
| 122 | private: |
Mathieu Chartier | 6f6b134 | 2016-03-09 11:14:50 -0800 | [diff] [blame] | 123 | template <bool kUseOffset> |
| 124 | bool WriteFullyGeneric(const void* buffer, size_t byte_count, size_t offset); |
| 125 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 126 | int fd_; |
| 127 | std::string file_path_; |
| 128 | bool auto_close_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 129 | bool read_only_mode_; |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 130 | |
| 131 | DISALLOW_COPY_AND_ASSIGN(FdFile); |
| 132 | }; |
| 133 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 134 | std::ostream& operator<<(std::ostream& os, const FdFile::GuardState& kind); |
| 135 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 136 | } // namespace unix_file |
| 137 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 138 | #endif // ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_ |