blob: 63927b121651d573c5b301342fd22dbe9dec8c47 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080018#include "base/unix_file/mapped_file.h"
19#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
27namespace unix_file {
28
29MappedFile::~MappedFile() {
30}
31
32int MappedFile::Close() {
33 if (IsMapped()) {
34 Unmap();
35 }
36 return FdFile::Close();
37}
38
39bool MappedFile::MapReadOnly() {
40 CHECK(IsOpened());
41 CHECK(!IsMapped());
42 struct stat st;
43 int result = TEMP_FAILURE_RETRY(fstat(Fd(), &st));
44 if (result == -1) {
45 PLOG(WARNING) << "Failed to stat file '" << GetPath() << "'";
46 return false;
47 }
48 file_size_ = st.st_size;
49 do {
50 mapped_file_ = mmap(NULL, file_size_, PROT_READ, MAP_PRIVATE, Fd(), 0);
51 } while (mapped_file_ == MAP_FAILED && errno == EINTR);
52 if (mapped_file_ == MAP_FAILED) {
53 PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
54 << file_size_ << " bytes to memory";
55 return false;
56 }
57 map_mode_ = kMapReadOnly;
58 return true;
59}
60
61bool MappedFile::MapReadWrite(int64_t file_size) {
62 CHECK(IsOpened());
63 CHECK(!IsMapped());
Ian Rogersc5f17732014-06-05 20:48:42 -070064#ifdef __linux__
Elliott Hughes76160052012-12-12 16:31:20 -080065 int result = TEMP_FAILURE_RETRY(ftruncate64(Fd(), file_size));
Ian Rogersc5f17732014-06-05 20:48:42 -070066#else
67 int result = TEMP_FAILURE_RETRY(ftruncate(Fd(), file_size));
68#endif
Elliott Hughes76160052012-12-12 16:31:20 -080069 if (result == -1) {
70 PLOG(ERROR) << "Failed to truncate file '" << GetPath()
71 << "' to size " << file_size;
72 return false;
73 }
74 file_size_ = file_size;
75 do {
76 mapped_file_ =
77 mmap(NULL, file_size_, PROT_READ | PROT_WRITE, MAP_SHARED, Fd(), 0);
78 } while (mapped_file_ == MAP_FAILED && errno == EINTR);
79 if (mapped_file_ == MAP_FAILED) {
80 PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
81 << file_size_ << " bytes to memory";
82 return false;
83 }
84 map_mode_ = kMapReadWrite;
85 return true;
86}
87
88bool MappedFile::Unmap() {
89 CHECK(IsMapped());
90 int result = TEMP_FAILURE_RETRY(munmap(mapped_file_, file_size_));
91 if (result == -1) {
92 PLOG(WARNING) << "Failed unmap file '" << GetPath() << "' of size "
93 << file_size_;
94 return false;
95 } else {
96 mapped_file_ = NULL;
97 file_size_ = -1;
98 return true;
99 }
100}
101
102int64_t MappedFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
103 if (IsMapped()) {
104 if (offset < 0) {
105 errno = EINVAL;
106 return -errno;
107 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 int64_t read_size = std::max(static_cast<int64_t>(0),
109 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800110 if (read_size > 0) {
111 memcpy(buf, data() + offset, read_size);
112 }
113 return read_size;
114 } else {
115 return FdFile::Read(buf, byte_count, offset);
116 }
117}
118
119int MappedFile::SetLength(int64_t new_length) {
120 CHECK(!IsMapped());
121 return FdFile::SetLength(new_length);
122}
123
124int64_t MappedFile::GetLength() const {
125 if (IsMapped()) {
126 return file_size_;
127 } else {
128 return FdFile::GetLength();
129 }
130}
131
132int MappedFile::Flush() {
133 int rc = IsMapped() ? TEMP_FAILURE_RETRY(msync(mapped_file_, file_size_, 0)) : FdFile::Flush();
134 return rc == -1 ? -errno : 0;
135}
136
137int64_t MappedFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
138 if (IsMapped()) {
139 CHECK_EQ(kMapReadWrite, map_mode_);
140 if (offset < 0) {
141 errno = EINVAL;
142 return -errno;
143 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 int64_t write_size = std::max(static_cast<int64_t>(0),
145 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800146 if (write_size > 0) {
147 memcpy(data() + offset, buf, write_size);
148 }
149 return write_size;
150 } else {
151 return FdFile::Write(buf, byte_count, offset);
152 }
153}
154
155int64_t MappedFile::size() const {
156 return GetLength();
157}
158
159bool MappedFile::IsMapped() const {
160 return mapped_file_ != NULL && mapped_file_ != MAP_FAILED;
161}
162
163char* MappedFile::data() const {
164 CHECK(IsMapped());
165 return static_cast<char*>(mapped_file_);
166}
167
168} // namespace unix_file