blob: 6d5b59cbeb10909b5d9f73311e81cfe7c8befddf [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080018#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 Hughes76160052012-12-12 16:31:20 -080023
24namespace unix_file {
25
26FdFile::FdFile() : fd_(-1), auto_close_(true) {
27}
28
29FdFile::FdFile(int fd) : fd_(fd), auto_close_(true) {
30}
31
32FdFile::FdFile(int fd, const std::string& path) : fd_(fd), file_path_(path), auto_close_(true) {
Brian Carlstromb45ccbf2013-03-13 15:14:24 -070033 CHECK_NE(0U, path.size());
Elliott Hughes76160052012-12-12 16:31:20 -080034}
35
36FdFile::~FdFile() {
37 if (auto_close_ && fd_ != -1) {
38 Close();
39 }
40}
41
42void FdFile::DisableAutoClose() {
43 auto_close_ = false;
44}
45
46bool FdFile::Open(const std::string& path, int flags) {
47 return Open(path, flags, 0640);
48}
49
50bool 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
60int 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
71int FdFile::Flush() {
Ian Rogersc5f17732014-06-05 20:48:42 -070072#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -080073 int rc = TEMP_FAILURE_RETRY(fdatasync(fd_));
Ian Rogersc5f17732014-06-05 20:48:42 -070074#else
75 int rc = TEMP_FAILURE_RETRY(fsync(fd_));
76#endif
Elliott Hughes76160052012-12-12 16:31:20 -080077 return (rc == -1) ? -errno : rc;
78}
79
80int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
Ian Rogersc5f17732014-06-05 20:48:42 -070081#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -080082 int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset));
Ian Rogersc5f17732014-06-05 20:48:42 -070083#else
84 int rc = TEMP_FAILURE_RETRY(pread(fd_, buf, byte_count, offset));
85#endif
Elliott Hughes76160052012-12-12 16:31:20 -080086 return (rc == -1) ? -errno : rc;
87}
88
89int FdFile::SetLength(int64_t new_length) {
Ian Rogersc5f17732014-06-05 20:48:42 -070090#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -080091 int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length));
Ian Rogersc5f17732014-06-05 20:48:42 -070092#else
93 int rc = TEMP_FAILURE_RETRY(ftruncate(fd_, new_length));
94#endif
Elliott Hughes76160052012-12-12 16:31:20 -080095 return (rc == -1) ? -errno : rc;
96}
97
98int64_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
104int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700105#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -0800106 int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset));
Ian Rogersc5f17732014-06-05 20:48:42 -0700107#else
108 int rc = TEMP_FAILURE_RETRY(pwrite(fd_, buf, byte_count, offset));
109#endif
Elliott Hughes76160052012-12-12 16:31:20 -0800110 return (rc == -1) ? -errno : rc;
111}
112
113int FdFile::Fd() const {
114 return fd_;
115}
116
117bool FdFile::IsOpened() const {
118 return fd_ >= 0;
119}
120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121bool FdFile::ReadFully(void* buffer, size_t byte_count) {
Elliott Hughes76160052012-12-12 16:31:20 -0800122 char* ptr = static_cast<char*>(buffer);
123 while (byte_count > 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
125 if (bytes_read == -1) {
Elliott Hughes76160052012-12-12 16:31:20 -0800126 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 Rogersef7d42f2014-01-06 12:55:46 -0800134bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
Elliott Hughes76160052012-12-12 16:31:20 -0800135 const char* ptr = static_cast<const char*>(buffer);
136 while (byte_count > 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
138 if (bytes_written == -1) {
Elliott Hughes76160052012-12-12 16:31:20 -0800139 return false;
140 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 byte_count -= bytes_written; // Reduce the number of remaining bytes.
142 ptr += bytes_written; // Move the buffer forward.
Elliott Hughes76160052012-12-12 16:31:20 -0800143 }
144 return true;
145}
146
147} // namespace unix_file