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 | |
| 17 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 18 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 24 | #include "base/logging.h" |
| 25 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | namespace unix_file { |
| 27 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 28 | FdFile::FdFile() : guard_state_(GuardState::kClosed), fd_(-1), auto_close_(true) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 31 | FdFile::FdFile(int fd, bool check_usage) |
| 32 | : guard_state_(check_usage ? GuardState::kBase : GuardState::kNoCheck), |
| 33 | fd_(fd), auto_close_(true) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 36 | FdFile::FdFile(int fd, const std::string& path, bool check_usage) |
| 37 | : guard_state_(check_usage ? GuardState::kBase : GuardState::kNoCheck), |
| 38 | fd_(fd), file_path_(path), auto_close_(true) { |
Brian Carlstrom | b45ccbf | 2013-03-13 15:14:24 -0700 | [diff] [blame] | 39 | CHECK_NE(0U, path.size()); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | FdFile::~FdFile() { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 43 | if (kCheckSafeUsage && (guard_state_ < GuardState::kNoCheck)) { |
| 44 | if (guard_state_ < GuardState::kFlushed) { |
| 45 | LOG(::art::ERROR) << "File " << file_path_ << " wasn't explicitly flushed before destruction."; |
| 46 | } |
| 47 | if (guard_state_ < GuardState::kClosed) { |
| 48 | LOG(::art::ERROR) << "File " << file_path_ << " wasn't explicitly closed before destruction."; |
| 49 | } |
| 50 | CHECK_GE(guard_state_, GuardState::kClosed); |
| 51 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 52 | if (auto_close_ && fd_ != -1) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 53 | if (Close() != 0) { |
| 54 | PLOG(::art::WARNING) << "Failed to close file " << file_path_; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void FdFile::moveTo(GuardState target, GuardState warn_threshold, const char* warning) { |
| 60 | if (kCheckSafeUsage) { |
| 61 | if (guard_state_ < GuardState::kNoCheck) { |
| 62 | if (warn_threshold < GuardState::kNoCheck && guard_state_ >= warn_threshold) { |
| 63 | LOG(::art::ERROR) << warning; |
| 64 | } |
| 65 | guard_state_ = target; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void FdFile::moveUp(GuardState target, const char* warning) { |
| 71 | if (kCheckSafeUsage) { |
| 72 | if (guard_state_ < GuardState::kNoCheck) { |
| 73 | if (guard_state_ < target) { |
| 74 | guard_state_ = target; |
| 75 | } else if (target < guard_state_) { |
| 76 | LOG(::art::ERROR) << warning; |
| 77 | } |
| 78 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | void FdFile::DisableAutoClose() { |
| 83 | auto_close_ = false; |
| 84 | } |
| 85 | |
| 86 | bool FdFile::Open(const std::string& path, int flags) { |
| 87 | return Open(path, flags, 0640); |
| 88 | } |
| 89 | |
| 90 | bool FdFile::Open(const std::string& path, int flags, mode_t mode) { |
| 91 | CHECK_EQ(fd_, -1) << path; |
| 92 | fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)); |
| 93 | if (fd_ == -1) { |
| 94 | return false; |
| 95 | } |
| 96 | file_path_ = path; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 97 | static_assert(O_RDONLY == 0, "Readonly flag has unexpected value."); |
| 98 | if (kCheckSafeUsage && (flags & (O_RDWR | O_CREAT | O_WRONLY)) != 0) { |
| 99 | // Start in the base state (not flushed, not closed). |
| 100 | guard_state_ = GuardState::kBase; |
| 101 | } else { |
| 102 | // We are not concerned with read-only files. In that case, proper flushing and closing is |
| 103 | // not important. |
| 104 | guard_state_ = GuardState::kNoCheck; |
| 105 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | int FdFile::Close() { |
| 110 | int result = TEMP_FAILURE_RETRY(close(fd_)); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 111 | |
| 112 | // Test here, so the file is closed and not leaked. |
| 113 | if (kCheckSafeUsage) { |
| 114 | CHECK_GE(guard_state_, GuardState::kFlushed) << "File " << file_path_ |
| 115 | << " has not been flushed before closing."; |
| 116 | moveUp(GuardState::kClosed, nullptr); |
| 117 | } |
| 118 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 119 | if (result == -1) { |
| 120 | return -errno; |
| 121 | } else { |
| 122 | fd_ = -1; |
| 123 | file_path_ = ""; |
| 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | int FdFile::Flush() { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 129 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 130 | int rc = TEMP_FAILURE_RETRY(fdatasync(fd_)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 131 | #else |
| 132 | int rc = TEMP_FAILURE_RETRY(fsync(fd_)); |
| 133 | #endif |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 134 | moveUp(GuardState::kFlushed, "Flushing closed file."); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 135 | return (rc == -1) ? -errno : rc; |
| 136 | } |
| 137 | |
| 138 | int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 139 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 140 | int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 141 | #else |
| 142 | int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset)); |
| 143 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 144 | return (rc == -1) ? -errno : rc; |
| 145 | } |
| 146 | |
| 147 | int FdFile::SetLength(int64_t new_length) { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 148 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 149 | int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 150 | #else |
| 151 | int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length)); |
| 152 | #endif |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 153 | moveTo(GuardState::kBase, GuardState::kClosed, "Truncating closed file."); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 154 | return (rc == -1) ? -errno : rc; |
| 155 | } |
| 156 | |
| 157 | int64_t FdFile::GetLength() const { |
| 158 | struct stat s; |
| 159 | int rc = TEMP_FAILURE_RETRY(fstat(fd_, &s)); |
| 160 | return (rc == -1) ? -errno : s.st_size; |
| 161 | } |
| 162 | |
| 163 | int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 164 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 165 | int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 166 | #else |
| 167 | int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset)); |
| 168 | #endif |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 169 | moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file."); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 170 | return (rc == -1) ? -errno : rc; |
| 171 | } |
| 172 | |
| 173 | int FdFile::Fd() const { |
| 174 | return fd_; |
| 175 | } |
| 176 | |
| 177 | bool FdFile::IsOpened() const { |
| 178 | return fd_ >= 0; |
| 179 | } |
| 180 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 181 | bool FdFile::ReadFully(void* buffer, size_t byte_count) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 182 | char* ptr = static_cast<char*>(buffer); |
| 183 | while (byte_count > 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 184 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count)); |
Andreas Gampe | 825201e | 2014-06-20 10:45:30 -0700 | [diff] [blame] | 185 | if (bytes_read <= 0) { |
| 186 | // 0: end of file |
| 187 | // -1: error |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | byte_count -= bytes_read; // Reduce the number of remaining bytes. |
| 191 | ptr += bytes_read; // Move the buffer forward. |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 196 | bool FdFile::WriteFully(const void* buffer, size_t byte_count) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 197 | const char* ptr = static_cast<const char*>(buffer); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 198 | moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file."); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 199 | while (byte_count > 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 200 | ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count)); |
| 201 | if (bytes_written == -1) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 204 | byte_count -= bytes_written; // Reduce the number of remaining bytes. |
| 205 | ptr += bytes_written; // Move the buffer forward. |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 210 | void FdFile::Erase() { |
| 211 | TEMP_FAILURE_RETRY(SetLength(0)); |
| 212 | TEMP_FAILURE_RETRY(Flush()); |
| 213 | TEMP_FAILURE_RETRY(Close()); |
| 214 | } |
| 215 | |
| 216 | int FdFile::FlushCloseOrErase() { |
| 217 | int flush_result = TEMP_FAILURE_RETRY(Flush()); |
| 218 | if (flush_result != 0) { |
| 219 | LOG(::art::ERROR) << "CloseOrErase failed while flushing a file."; |
| 220 | Erase(); |
| 221 | return flush_result; |
| 222 | } |
| 223 | int close_result = TEMP_FAILURE_RETRY(Close()); |
| 224 | if (close_result != 0) { |
| 225 | LOG(::art::ERROR) << "CloseOrErase failed while closing a file."; |
| 226 | Erase(); |
| 227 | return close_result; |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | int FdFile::FlushClose() { |
| 233 | int flush_result = TEMP_FAILURE_RETRY(Flush()); |
| 234 | if (flush_result != 0) { |
| 235 | LOG(::art::ERROR) << "FlushClose failed while flushing a file."; |
| 236 | } |
| 237 | int close_result = TEMP_FAILURE_RETRY(Close()); |
| 238 | if (close_result != 0) { |
| 239 | LOG(::art::ERROR) << "FlushClose failed while closing a file."; |
| 240 | } |
| 241 | return (flush_result != 0) ? flush_result : close_result; |
| 242 | } |
| 243 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 244 | } // namespace unix_file |