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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 17 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 18 | #include "base/unix_file/fd_file.h" |
| 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 | |
| 24 | namespace unix_file { |
| 25 | |
| 26 | FdFile::FdFile() : fd_(-1), auto_close_(true) { |
| 27 | } |
| 28 | |
| 29 | FdFile::FdFile(int fd) : fd_(fd), auto_close_(true) { |
| 30 | } |
| 31 | |
| 32 | FdFile::FdFile(int fd, const std::string& path) : fd_(fd), file_path_(path), auto_close_(true) { |
Brian Carlstrom | b45ccbf | 2013-03-13 15:14:24 -0700 | [diff] [blame] | 33 | CHECK_NE(0U, path.size()); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | FdFile::~FdFile() { |
| 37 | if (auto_close_ && fd_ != -1) { |
| 38 | Close(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void FdFile::DisableAutoClose() { |
| 43 | auto_close_ = false; |
| 44 | } |
| 45 | |
| 46 | bool FdFile::Open(const std::string& path, int flags) { |
| 47 | return Open(path, flags, 0640); |
| 48 | } |
| 49 | |
| 50 | bool FdFile::Open(const std::string& path, int flags, mode_t mode) { |
| 51 | CHECK_EQ(fd_, -1) << path; |
| 52 | fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)); |
| 53 | if (fd_ == -1) { |
| 54 | return false; |
| 55 | } |
| 56 | file_path_ = path; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | int FdFile::Close() { |
| 61 | int result = TEMP_FAILURE_RETRY(close(fd_)); |
| 62 | if (result == -1) { |
| 63 | return -errno; |
| 64 | } else { |
| 65 | fd_ = -1; |
| 66 | file_path_ = ""; |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int FdFile::Flush() { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 72 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 73 | int rc = TEMP_FAILURE_RETRY(fdatasync(fd_)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 74 | #else |
| 75 | int rc = TEMP_FAILURE_RETRY(fsync(fd_)); |
| 76 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 77 | return (rc == -1) ? -errno : rc; |
| 78 | } |
| 79 | |
| 80 | 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^] | 81 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 82 | int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 83 | #else |
| 84 | int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset)); |
| 85 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 86 | return (rc == -1) ? -errno : rc; |
| 87 | } |
| 88 | |
| 89 | int FdFile::SetLength(int64_t new_length) { |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 90 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 91 | int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 92 | #else |
| 93 | int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length)); |
| 94 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 95 | return (rc == -1) ? -errno : rc; |
| 96 | } |
| 97 | |
| 98 | int64_t FdFile::GetLength() const { |
| 99 | struct stat s; |
| 100 | int rc = TEMP_FAILURE_RETRY(fstat(fd_, &s)); |
| 101 | return (rc == -1) ? -errno : s.st_size; |
| 102 | } |
| 103 | |
| 104 | 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^] | 105 | #ifdef __linux__ |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 106 | int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset)); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame^] | 107 | #else |
| 108 | int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset)); |
| 109 | #endif |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 110 | return (rc == -1) ? -errno : rc; |
| 111 | } |
| 112 | |
| 113 | int FdFile::Fd() const { |
| 114 | return fd_; |
| 115 | } |
| 116 | |
| 117 | bool FdFile::IsOpened() const { |
| 118 | return fd_ >= 0; |
| 119 | } |
| 120 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 121 | bool FdFile::ReadFully(void* buffer, size_t byte_count) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 122 | char* ptr = static_cast<char*>(buffer); |
| 123 | while (byte_count > 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 124 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count)); |
| 125 | if (bytes_read == -1) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | byte_count -= bytes_read; // Reduce the number of remaining bytes. |
| 129 | ptr += bytes_read; // Move the buffer forward. |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 134 | bool FdFile::WriteFully(const void* buffer, size_t byte_count) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 135 | const char* ptr = static_cast<const char*>(buffer); |
| 136 | while (byte_count > 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 137 | ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count)); |
| 138 | if (bytes_written == -1) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 139 | return false; |
| 140 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 141 | byte_count -= bytes_written; // Reduce the number of remaining bytes. |
| 142 | ptr += bytes_written; // Move the buffer forward. |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | } // namespace unix_file |