blob: 77f4d020a904dbdac11e6a7deff24f1a5c482dd6 [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) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070045 PLOG(::art::WARNING) << "Failed to stat file '" << GetPath() << "'";
Elliott Hughes76160052012-12-12 16:31:20 -080046 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) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070053 PLOG(::art::WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
54 << file_size_ << " bytes to memory";
Elliott Hughes76160052012-12-12 16:31:20 -080055 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) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070070 PLOG(::art::ERROR) << "Failed to truncate file '" << GetPath() << "' to size " << file_size;
Elliott Hughes76160052012-12-12 16:31:20 -080071 return false;
72 }
73 file_size_ = file_size;
74 do {
75 mapped_file_ =
76 mmap(NULL, file_size_, PROT_READ | PROT_WRITE, MAP_SHARED, Fd(), 0);
77 } while (mapped_file_ == MAP_FAILED && errno == EINTR);
78 if (mapped_file_ == MAP_FAILED) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070079 PLOG(::art::WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
Elliott Hughes76160052012-12-12 16:31:20 -080080 << file_size_ << " bytes to memory";
81 return false;
82 }
83 map_mode_ = kMapReadWrite;
84 return true;
85}
86
87bool MappedFile::Unmap() {
88 CHECK(IsMapped());
89 int result = TEMP_FAILURE_RETRY(munmap(mapped_file_, file_size_));
90 if (result == -1) {
Ian Rogersc7dd2952014-10-21 23:31:19 -070091 PLOG(::art::WARNING) << "Failed unmap file '" << GetPath() << "' of size " << file_size_;
Elliott Hughes76160052012-12-12 16:31:20 -080092 return false;
93 } else {
94 mapped_file_ = NULL;
95 file_size_ = -1;
96 return true;
97 }
98}
99
100int64_t MappedFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
101 if (IsMapped()) {
102 if (offset < 0) {
103 errno = EINVAL;
104 return -errno;
105 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 int64_t read_size = std::max(static_cast<int64_t>(0),
107 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800108 if (read_size > 0) {
109 memcpy(buf, data() + offset, read_size);
110 }
111 return read_size;
112 } else {
113 return FdFile::Read(buf, byte_count, offset);
114 }
115}
116
117int MappedFile::SetLength(int64_t new_length) {
118 CHECK(!IsMapped());
119 return FdFile::SetLength(new_length);
120}
121
122int64_t MappedFile::GetLength() const {
123 if (IsMapped()) {
124 return file_size_;
125 } else {
126 return FdFile::GetLength();
127 }
128}
129
130int MappedFile::Flush() {
131 int rc = IsMapped() ? TEMP_FAILURE_RETRY(msync(mapped_file_, file_size_, 0)) : FdFile::Flush();
132 return rc == -1 ? -errno : 0;
133}
134
135int64_t MappedFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
136 if (IsMapped()) {
137 CHECK_EQ(kMapReadWrite, map_mode_);
138 if (offset < 0) {
139 errno = EINVAL;
140 return -errno;
141 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 int64_t write_size = std::max(static_cast<int64_t>(0),
143 std::min(byte_count, file_size_ - offset));
Elliott Hughes76160052012-12-12 16:31:20 -0800144 if (write_size > 0) {
145 memcpy(data() + offset, buf, write_size);
146 }
147 return write_size;
148 } else {
149 return FdFile::Write(buf, byte_count, offset);
150 }
151}
152
153int64_t MappedFile::size() const {
154 return GetLength();
155}
156
157bool MappedFile::IsMapped() const {
158 return mapped_file_ != NULL && mapped_file_ != MAP_FAILED;
159}
160
161char* MappedFile::data() const {
162 CHECK(IsMapped());
163 return static_cast<char*>(mapped_file_);
164}
165
166} // namespace unix_file