blob: ad433824b18aef333268788f69708f81289b0c0a [file] [log] [blame]
Elliott Hughes76160052012-12-12 16:31:20 -08001/*
2 * Copyright (C) 2008 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/mapped_file.h"
Andreas Gampe9433ec62014-11-06 01:00:46 -080018
Elliott Hughes76160052012-12-12 16:31:20 -080019#include <fcntl.h>
20#include <sys/mman.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24#include <algorithm>
25#include <string>
Elliott Hughes76160052012-12-12 16:31:20 -080026
Andreas Gampe9433ec62014-11-06 01:00:46 -080027#include "base/logging.h"
28
Elliott Hughes76160052012-12-12 16:31:20 -080029namespace unix_file {
30
31MappedFile::~MappedFile() {
32}
33
34int MappedFile::Close() {
35 if (IsMapped()) {
36 Unmap();
37 }
38 return FdFile::Close();
39}
40
41bool MappedFile::MapReadOnly() {
42 CHECK(IsOpened());
43 CHECK(!IsMapped());
Andreas Gampe9433ec62014-11-06 01:00:46 -080044
45 // Mapping readonly means we don't need to enforce Flush and Close.
46 resetGuard(GuardState::kNoCheck);
47
Elliott Hughes76160052012-12-12 16:31:20 -080048 struct stat st;
49 int result = TEMP_FAILURE_RETRY(fstat(Fd(), &st));
50 if (result == -1) {
51 PLOG(WARNING) << "Failed to stat file '" << GetPath() << "'";
52 return false;
53 }
54 file_size_ = st.st_size;
55 do {
56 mapped_file_ = mmap(NULL, file_size_, PROT_READ, MAP_PRIVATE, Fd(), 0);
57 } while (mapped_file_ == MAP_FAILED && errno == EINTR);
58 if (mapped_file_ == MAP_FAILED) {
59 PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
60 << file_size_ << " bytes to memory";
61 return false;
62 }
63 map_mode_ = kMapReadOnly;
64 return true;
65}
66
67bool MappedFile::MapReadWrite(int64_t file_size) {
68 CHECK(IsOpened());
69 CHECK(!IsMapped());
Ian Rogersc5f17732014-06-05 20:48:42 -070070#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -080071 int result = TEMP_FAILURE_RETRY(ftruncate64(Fd(), file_size));
Ian Rogersc5f17732014-06-05 20:48:42 -070072#else
73 int result = TEMP_FAILURE_RETRY(ftruncate(Fd(), file_size));
74#endif
Elliott Hughes76160052012-12-12 16:31:20 -080075 if (result == -1) {
76 PLOG(ERROR) << "Failed to truncate file '" << GetPath()
77 << "' to size " << file_size;
78 return false;
79 }
Andreas Gampe9433ec62014-11-06 01:00:46 -080080
81 // Need to track this now.
82 resetGuard(GuardState::kBase);
83
Elliott Hughes76160052012-12-12 16:31:20 -080084 file_size_ = file_size;
85 do {
86 mapped_file_ =
87 mmap(NULL, file_size_, PROT_READ | PROT_WRITE, MAP_SHARED, Fd(), 0);
88 } while (mapped_file_ == MAP_FAILED && errno == EINTR);
89 if (mapped_file_ == MAP_FAILED) {
90 PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
91 << file_size_ << " bytes to memory";
92 return false;
93 }
94 map_mode_ = kMapReadWrite;
95 return true;
96}
97
98bool MappedFile::Unmap() {
99 CHECK(IsMapped());
100 int result = TEMP_FAILURE_RETRY(munmap(mapped_file_, file_size_));
101 if (result == -1) {
102 PLOG(WARNING) << "Failed unmap file '" << GetPath() << "' of size "
103 << file_size_;
104 return false;
105 } else {
106 mapped_file_ = NULL;
107 file_size_ = -1;
108 return true;
109 }
110}
111
112int64_t MappedFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
113 if (IsMapped()) {
114 if (offset < 0) {
115 errno = EINVAL;
116 return -errno;
117 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 int64_t read_size = std::max(static_cast<int64_t>(0),
119 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800120 if (read_size > 0) {
121 memcpy(buf, data() + offset, read_size);
122 }
123 return read_size;
124 } else {
125 return FdFile::Read(buf, byte_count, offset);
126 }
127}
128
129int MappedFile::SetLength(int64_t new_length) {
130 CHECK(!IsMapped());
131 return FdFile::SetLength(new_length);
132}
133
134int64_t MappedFile::GetLength() const {
135 if (IsMapped()) {
136 return file_size_;
137 } else {
138 return FdFile::GetLength();
139 }
140}
141
142int MappedFile::Flush() {
Andreas Gampe9433ec62014-11-06 01:00:46 -0800143 moveUp(GuardState::kFlushed, "Flushing closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800144 int rc = IsMapped() ? TEMP_FAILURE_RETRY(msync(mapped_file_, file_size_, 0)) : FdFile::Flush();
145 return rc == -1 ? -errno : 0;
146}
147
148int64_t MappedFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
149 if (IsMapped()) {
150 CHECK_EQ(kMapReadWrite, map_mode_);
151 if (offset < 0) {
152 errno = EINVAL;
153 return -errno;
154 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 int64_t write_size = std::max(static_cast<int64_t>(0),
156 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800157 if (write_size > 0) {
158 memcpy(data() + offset, buf, write_size);
Andreas Gampe9433ec62014-11-06 01:00:46 -0800159 moveTo(GuardState::kBase, GuardState::kClosed, "Writing into a closed file.");
Elliott Hughes76160052012-12-12 16:31:20 -0800160 }
161 return write_size;
162 } else {
163 return FdFile::Write(buf, byte_count, offset);
164 }
165}
166
167int64_t MappedFile::size() const {
168 return GetLength();
169}
170
171bool MappedFile::IsMapped() const {
172 return mapped_file_ != NULL && mapped_file_ != MAP_FAILED;
173}
174
175char* MappedFile::data() const {
176 CHECK(IsMapped());
177 return static_cast<char*>(mapped_file_);
178}
179
180} // namespace unix_file