blob: 6e5e7a1582fbc8161494c85b0abe91f998a6cbb0 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800181bool FdFile::ReadFully(void* buffer, size_t byte_count) {
Elliott Hughes76160052012-12-12 16:31:20 -0800182 char* ptr = static_cast<char*>(buffer);
183 while (byte_count > 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
Andreas Gampe825201e2014-06-20 10:45:30 -0700185 if (bytes_read <= 0) {
186 // 0: end of file
187 // -1: error
Elliott Hughes76160052012-12-12 16:31:20 -0800188 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 Rogersef7d42f2014-01-06 12:55:46 -0800196bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
Elliott Hughes76160052012-12-12 16:31:20 -0800197 const char* ptr = static_cast<const char*>(buffer);
Andreas Gampe4303ba92014-11-06 01:00:46 -0800198 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800199 while (byte_count > 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800200 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
201 if (bytes_written == -1) {
Elliott Hughes76160052012-12-12 16:31:20 -0800202 return false;
203 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800204 byte_count -= bytes_written; // Reduce the number of remaining bytes.
205 ptr += bytes_written; // Move the buffer forward.
Elliott Hughes76160052012-12-12 16:31:20 -0800206 }
207 return true;
208}
209
Andreas Gampe4303ba92014-11-06 01:00:46 -0800210void FdFile::Erase() {
211 TEMP_FAILURE_RETRY(SetLength(0));
212 TEMP_FAILURE_RETRY(Flush());
213 TEMP_FAILURE_RETRY(Close());
214}
215
216int 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
232int 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 Hughes76160052012-12-12 16:31:20 -0800244} // namespace unix_file