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" |
| 18 | #include <errno.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | #include "logging.h" |
| 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) { |
| 33 | } |
| 34 | |
| 35 | FdFile::~FdFile() { |
| 36 | if (auto_close_ && fd_ != -1) { |
| 37 | Close(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void FdFile::DisableAutoClose() { |
| 42 | auto_close_ = false; |
| 43 | } |
| 44 | |
| 45 | bool FdFile::Open(const std::string& path, int flags) { |
| 46 | return Open(path, flags, 0640); |
| 47 | } |
| 48 | |
| 49 | bool FdFile::Open(const std::string& path, int flags, mode_t mode) { |
| 50 | CHECK_EQ(fd_, -1) << path; |
| 51 | fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)); |
| 52 | if (fd_ == -1) { |
| 53 | return false; |
| 54 | } |
| 55 | file_path_ = path; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | int FdFile::Close() { |
| 60 | int result = TEMP_FAILURE_RETRY(close(fd_)); |
| 61 | if (result == -1) { |
| 62 | return -errno; |
| 63 | } else { |
| 64 | fd_ = -1; |
| 65 | file_path_ = ""; |
| 66 | return 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | int FdFile::Flush() { |
| 71 | int rc = TEMP_FAILURE_RETRY(fdatasync(fd_)); |
| 72 | return (rc == -1) ? -errno : rc; |
| 73 | } |
| 74 | |
| 75 | int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const { |
| 76 | int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset)); |
| 77 | return (rc == -1) ? -errno : rc; |
| 78 | } |
| 79 | |
| 80 | int FdFile::SetLength(int64_t new_length) { |
| 81 | int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length)); |
| 82 | return (rc == -1) ? -errno : rc; |
| 83 | } |
| 84 | |
| 85 | int64_t FdFile::GetLength() const { |
| 86 | struct stat s; |
| 87 | int rc = TEMP_FAILURE_RETRY(fstat(fd_, &s)); |
| 88 | return (rc == -1) ? -errno : s.st_size; |
| 89 | } |
| 90 | |
| 91 | int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) { |
| 92 | int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset)); |
| 93 | return (rc == -1) ? -errno : rc; |
| 94 | } |
| 95 | |
| 96 | int FdFile::Fd() const { |
| 97 | return fd_; |
| 98 | } |
| 99 | |
| 100 | bool FdFile::IsOpened() const { |
| 101 | return fd_ >= 0; |
| 102 | } |
| 103 | |
| 104 | std::string FdFile::GetPath() const { |
| 105 | return file_path_; |
| 106 | } |
| 107 | |
| 108 | bool FdFile::ReadFully(void* buffer, int64_t byte_count) { |
| 109 | char* ptr = static_cast<char*>(buffer); |
| 110 | while (byte_count > 0) { |
| 111 | int bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count)); |
| 112 | if (bytes_read <= 0) { |
| 113 | return false; |
| 114 | } |
| 115 | byte_count -= bytes_read; // Reduce the number of remaining bytes. |
| 116 | ptr += bytes_read; // Move the buffer forward. |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool FdFile::WriteFully(const void* buffer, int64_t byte_count) { |
| 122 | const char* ptr = static_cast<const char*>(buffer); |
| 123 | while (byte_count > 0) { |
| 124 | int bytes_read = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count)); |
| 125 | if (bytes_read < 0) { |
| 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 | |
| 134 | } // namespace unix_file |