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" 18 19 #include <errno.h> 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <unistd.h> 23 24 #include "base/logging.h" 25 26 namespace unix_file { 27 28 FdFile::FdFile() : guard_state_(GuardState::kClosed), fd_(-1), auto_close_(true) { 29 } 30 31 FdFile::FdFile(int fd, bool check_usage) 32 : guard_state_(check_usage ? GuardState::kBase : GuardState::kNoCheck), 33 fd_(fd), auto_close_(true) { 34 } 35 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) { 39 CHECK_NE(0U, path.size()); 40 } 41 42 FdFile::~FdFile() { 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 } 52 if (auto_close_ && fd_ != -1) { 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 } 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; 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 } 106 return true; 107 } 108 109 int FdFile::Close() { 110 int result = close(fd_); 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 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() { 129 #ifdef __linux__ 130 int rc = TEMP_FAILURE_RETRY(fdatasync(fd_)); 131 #else 132 int rc = TEMP_FAILURE_RETRY(fsync(fd_)); 133 #endif 134 moveUp(GuardState::kFlushed, "Flushing closed file."); 135 return (rc == -1) ? -errno : rc; 136 } 137 138 int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const { 139 #ifdef __linux__ 140 int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset)); 141 #else 142 int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset)); 143 #endif 144 return (rc == -1) ? -errno : rc; 145 } 146 147 int FdFile::SetLength(int64_t new_length) { 148 #ifdef __linux__ 149 int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length)); 150 #else 151 int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length)); 152 #endif 153 moveTo(GuardState::kBase, GuardState::kClosed, "Truncating closed file."); 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) { 164 #ifdef __linux__ 165 int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset)); 166 #else 167 int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset)); 168 #endif 169 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file."); 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 181 static ssize_t ReadIgnoreOffset(int fd, void *buf, size_t count, off_t offset) { 182 DCHECK_EQ(offset, 0); 183 return read(fd, buf, count); 184 } 185 186 template <ssize_t (*read_func)(int, void*, size_t, off_t)> 187 static bool ReadFullyGeneric(int fd, void* buffer, size_t byte_count, size_t offset) { 188 char* ptr = static_cast<char*>(buffer); 189 while (byte_count > 0) { 190 ssize_t bytes_read = TEMP_FAILURE_RETRY(read_func(fd, ptr, byte_count, offset)); 191 if (bytes_read <= 0) { 192 // 0: end of file 193 // -1: error 194 return false; 195 } 196 byte_count -= bytes_read; // Reduce the number of remaining bytes. 197 ptr += bytes_read; // Move the buffer forward. 198 offset += static_cast<size_t>(bytes_read); // Move the offset forward. 199 } 200 return true; 201 } 202 203 bool FdFile::ReadFully(void* buffer, size_t byte_count) { 204 return ReadFullyGeneric<ReadIgnoreOffset>(fd_, buffer, byte_count, 0); 205 } 206 207 bool FdFile::PreadFully(void* buffer, size_t byte_count, size_t offset) { 208 return ReadFullyGeneric<pread>(fd_, buffer, byte_count, offset); 209 } 210 211 bool FdFile::WriteFully(const void* buffer, size_t byte_count) { 212 const char* ptr = static_cast<const char*>(buffer); 213 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file."); 214 while (byte_count > 0) { 215 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count)); 216 if (bytes_written == -1) { 217 return false; 218 } 219 byte_count -= bytes_written; // Reduce the number of remaining bytes. 220 ptr += bytes_written; // Move the buffer forward. 221 } 222 return true; 223 } 224 225 void FdFile::Erase() { 226 TEMP_FAILURE_RETRY(SetLength(0)); 227 TEMP_FAILURE_RETRY(Flush()); 228 TEMP_FAILURE_RETRY(Close()); 229 } 230 231 int FdFile::FlushCloseOrErase() { 232 int flush_result = TEMP_FAILURE_RETRY(Flush()); 233 if (flush_result != 0) { 234 LOG(::art::ERROR) << "CloseOrErase failed while flushing a file."; 235 Erase(); 236 return flush_result; 237 } 238 int close_result = TEMP_FAILURE_RETRY(Close()); 239 if (close_result != 0) { 240 LOG(::art::ERROR) << "CloseOrErase failed while closing a file."; 241 Erase(); 242 return close_result; 243 } 244 return 0; 245 } 246 247 int FdFile::FlushClose() { 248 int flush_result = TEMP_FAILURE_RETRY(Flush()); 249 if (flush_result != 0) { 250 LOG(::art::ERROR) << "FlushClose failed while flushing a file."; 251 } 252 int close_result = TEMP_FAILURE_RETRY(Close()); 253 if (close_result != 0) { 254 LOG(::art::ERROR) << "FlushClose failed while closing a file."; 255 } 256 return (flush_result != 0) ? flush_result : close_result; 257 } 258 259 void FdFile::MarkUnchecked() { 260 guard_state_ = GuardState::kNoCheck; 261 } 262 263 } // namespace unix_file 264