blob: 780e37a4d0b75858b8f59add071a7a5831660ea6 [file] [log] [blame]
Elliott Hughes76160052012-12-12 16:31:20 -08001/*
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 Gampe4303ba92014-11-06 01:00:46 -080018
Elliott Hughes76160052012-12-12 16:31:20 -080019#include <errno.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
Elliott Hughes76160052012-12-12 16:31:20 -080023
Andreas Gampe4303ba92014-11-06 01:00:46 -080024#include "base/logging.h"
25
Elliott Hughes76160052012-12-12 16:31:20 -080026namespace unix_file {
27
Andreas Gampe4303ba92014-11-06 01:00:46 -080028FdFile::FdFile() : guard_state_(GuardState::kClosed), fd_(-1), auto_close_(true) {
Elliott Hughes76160052012-12-12 16:31:20 -080029}
30
Andreas Gampe4303ba92014-11-06 01:00:46 -080031FdFile::FdFile(int fd, bool check_usage)
32 : guard_state_(check_usage ? GuardState::kBase : GuardState::kNoCheck),
33 fd_(fd), auto_close_(true) {
Elliott Hughes76160052012-12-12 16:31:20 -080034}
35
Andreas Gampe4303ba92014-11-06 01:00:46 -080036FdFile::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 Carlstromb45ccbf2013-03-13 15:14:24 -070039 CHECK_NE(0U, path.size());
Elliott Hughes76160052012-12-12 16:31:20 -080040}
41
42FdFile::~FdFile() {
Andreas Gampe4303ba92014-11-06 01:00:46 -080043 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 Hughes76160052012-12-12 16:31:20 -080052 if (auto_close_ && fd_ != -1) {
Andreas Gampe4303ba92014-11-06 01:00:46 -080053 if (Close() != 0) {
54 PLOG(::art::WARNING) << "Failed to close file " << file_path_;
55 }
56 }
57}
58
59void 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
70void 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 Hughes76160052012-12-12 16:31:20 -080079 }
80}
81
82void FdFile::DisableAutoClose() {
83 auto_close_ = false;
84}
85
86bool FdFile::Open(const std::string& path, int flags) {
87 return Open(path, flags, 0640);
88}
89
90bool 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 Gampe4303ba92014-11-06 01:00:46 -080097 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 Hughes76160052012-12-12 16:31:20 -0800106 return true;
107}
108
109int FdFile::Close() {
110 int result = TEMP_FAILURE_RETRY(close(fd_));
Andreas Gampe4303ba92014-11-06 01:00:46 -0800111
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 Hughes76160052012-12-12 16:31:20 -0800119 if (result == -1) {
120 return -errno;
121 } else {
122 fd_ = -1;
123 file_path_ = "";
124 return 0;
125 }
126}
127
128int FdFile::Flush() {
Ian Rogersc5f17732014-06-05 20:48:42 -0700129#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -0800130 int rc = TEMP_FAILURE_RETRY(fdatasync(fd_));
Ian Rogersc5f17732014-06-05 20:48:42 -0700131#else
132 int rc = TEMP_FAILURE_RETRY(fsync(fd_));
133#endif
Andreas Gampe4303ba92014-11-06 01:00:46 -0800134 moveUp(GuardState::kFlushed, "Flushing closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800135 return (rc == -1) ? -errno : rc;
136}
137
138int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
Ian Rogersc5f17732014-06-05 20:48:42 -0700139#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -0800140 int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset));
Ian Rogersc5f17732014-06-05 20:48:42 -0700141#else
142 int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset));
143#endif
Elliott Hughes76160052012-12-12 16:31:20 -0800144 return (rc == -1) ? -errno : rc;
145}
146
147int FdFile::SetLength(int64_t new_length) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700148#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -0800149 int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length));
Ian Rogersc5f17732014-06-05 20:48:42 -0700150#else
151 int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length));
152#endif
Andreas Gampe4303ba92014-11-06 01:00:46 -0800153 moveTo(GuardState::kBase, GuardState::kClosed, "Truncating closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800154 return (rc == -1) ? -errno : rc;
155}
156
157int64_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
163int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700164#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -0800165 int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset));
Ian Rogersc5f17732014-06-05 20:48:42 -0700166#else
167 int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset));
168#endif
Andreas Gampe4303ba92014-11-06 01:00:46 -0800169 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800170 return (rc == -1) ? -errno : rc;
171}
172
173int FdFile::Fd() const {
174 return fd_;
175}
176
177bool FdFile::IsOpened() const {
178 return fd_ >= 0;
179}
180
Igor Murashkin37743352014-11-13 14:38:00 -0800181static 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
186template <ssize_t (*read_func)(int, void*, size_t, off_t)>
187static bool ReadFullyGeneric(int fd, void* buffer, size_t byte_count, size_t offset) {
Elliott Hughes76160052012-12-12 16:31:20 -0800188 char* ptr = static_cast<char*>(buffer);
189 while (byte_count > 0) {
Igor Murashkin37743352014-11-13 14:38:00 -0800190 ssize_t bytes_read = TEMP_FAILURE_RETRY(read_func(fd, ptr, byte_count, offset));
Andreas Gampe825201e2014-06-20 10:45:30 -0700191 if (bytes_read <= 0) {
192 // 0: end of file
193 // -1: error
Elliott Hughes76160052012-12-12 16:31:20 -0800194 return false;
195 }
196 byte_count -= bytes_read; // Reduce the number of remaining bytes.
197 ptr += bytes_read; // Move the buffer forward.
Igor Murashkin37743352014-11-13 14:38:00 -0800198 offset += static_cast<size_t>(bytes_read); // Move the offset forward.
Elliott Hughes76160052012-12-12 16:31:20 -0800199 }
200 return true;
201}
202
Igor Murashkin37743352014-11-13 14:38:00 -0800203bool FdFile::ReadFully(void* buffer, size_t byte_count) {
204 return ReadFullyGeneric<ReadIgnoreOffset>(fd_, buffer, byte_count, 0);
205}
206
207bool FdFile::PreadFully(void* buffer, size_t byte_count, size_t offset) {
208 return ReadFullyGeneric<pread>(fd_, buffer, byte_count, offset);
209}
210
Ian Rogersef7d42f2014-01-06 12:55:46 -0800211bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
Elliott Hughes76160052012-12-12 16:31:20 -0800212 const char* ptr = static_cast<const char*>(buffer);
Andreas Gampe4303ba92014-11-06 01:00:46 -0800213 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800214 while (byte_count > 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
216 if (bytes_written == -1) {
Elliott Hughes76160052012-12-12 16:31:20 -0800217 return false;
218 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219 byte_count -= bytes_written; // Reduce the number of remaining bytes.
220 ptr += bytes_written; // Move the buffer forward.
Elliott Hughes76160052012-12-12 16:31:20 -0800221 }
222 return true;
223}
224
Andreas Gampe4303ba92014-11-06 01:00:46 -0800225void FdFile::Erase() {
226 TEMP_FAILURE_RETRY(SetLength(0));
227 TEMP_FAILURE_RETRY(Flush());
228 TEMP_FAILURE_RETRY(Close());
229}
230
231int 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
247int 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
Elliott Hughes76160052012-12-12 16:31:20 -0800259} // namespace unix_file