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/logging.h"
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>
23
24 namespace unix_file {
25
FdFile()26 FdFile::FdFile() : fd_(-1), auto_close_(true) {
27 }
28
FdFile(int fd)29 FdFile::FdFile(int fd) : fd_(fd), auto_close_(true) {
30 }
31
FdFile(int fd,const std::string & path)32 FdFile::FdFile(int fd, const std::string& path) : fd_(fd), file_path_(path), auto_close_(true) {
33 CHECK_NE(0U, path.size());
34 }
35
~FdFile()36 FdFile::~FdFile() {
37 if (auto_close_ && fd_ != -1) {
38 Close();
39 }
40 }
41
DisableAutoClose()42 void FdFile::DisableAutoClose() {
43 auto_close_ = false;
44 }
45
Open(const std::string & path,int flags)46 bool FdFile::Open(const std::string& path, int flags) {
47 return Open(path, flags, 0640);
48 }
49
Open(const std::string & path,int flags,mode_t mode)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
Close()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
Flush()71 int FdFile::Flush() {
72 #ifdef __linux__
73 int rc = TEMP_FAILURE_RETRY(fdatasync(fd_));
74 #else
75 int rc = TEMP_FAILURE_RETRY(fsync(fd_));
76 #endif
77 return (rc == -1) ? -errno : rc;
78 }
79
Read(char * buf,int64_t byte_count,int64_t offset) const80 int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
81 #ifdef __linux__
82 int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset));
83 #else
84 int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset));
85 #endif
86 return (rc == -1) ? -errno : rc;
87 }
88
SetLength(int64_t new_length)89 int FdFile::SetLength(int64_t new_length) {
90 #ifdef __linux__
91 int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length));
92 #else
93 int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length));
94 #endif
95 return (rc == -1) ? -errno : rc;
96 }
97
GetLength() const98 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
Write(const char * buf,int64_t byte_count,int64_t offset)104 int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
105 #ifdef __linux__
106 int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset));
107 #else
108 int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset));
109 #endif
110 return (rc == -1) ? -errno : rc;
111 }
112
Fd() const113 int FdFile::Fd() const {
114 return fd_;
115 }
116
IsOpened() const117 bool FdFile::IsOpened() const {
118 return fd_ >= 0;
119 }
120
ReadFully(void * buffer,size_t byte_count)121 bool FdFile::ReadFully(void* buffer, size_t byte_count) {
122 char* ptr = static_cast<char*>(buffer);
123 while (byte_count > 0) {
124 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
125 if (bytes_read <= 0) {
126 // 0: end of file
127 // -1: error
128 return false;
129 }
130 byte_count -= bytes_read; // Reduce the number of remaining bytes.
131 ptr += bytes_read; // Move the buffer forward.
132 }
133 return true;
134 }
135
WriteFully(const void * buffer,size_t byte_count)136 bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
137 const char* ptr = static_cast<const char*>(buffer);
138 while (byte_count > 0) {
139 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
140 if (bytes_written == -1) {
141 return false;
142 }
143 byte_count -= bytes_written; // Reduce the number of remaining bytes.
144 ptr += bytes_written; // Move the buffer forward.
145 }
146 return true;
147 }
148
149 } // namespace unix_file
150