summaryrefslogtreecommitdiffstats
path: root/runtime/base/unix_file/mapped_file.cc
blob: 63927b121651d573c5b301342fd22dbe9dec8c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "base/logging.h"
#include "base/unix_file/mapped_file.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <string>

namespace unix_file {

MappedFile::~MappedFile() {
}

int MappedFile::Close() {
  if (IsMapped()) {
    Unmap();
  }
  return FdFile::Close();
}

bool MappedFile::MapReadOnly() {
  CHECK(IsOpened());
  CHECK(!IsMapped());
  struct stat st;
  int result = TEMP_FAILURE_RETRY(fstat(Fd(), &st));
  if (result == -1) {
    PLOG(WARNING) << "Failed to stat file '" << GetPath() << "'";
    return false;
  }
  file_size_ = st.st_size;
  do {
    mapped_file_ = mmap(NULL, file_size_, PROT_READ, MAP_PRIVATE, Fd(), 0);
  } while (mapped_file_ == MAP_FAILED && errno == EINTR);
  if (mapped_file_ == MAP_FAILED) {
    PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
                  << file_size_ << " bytes to memory";
    return false;
  }
  map_mode_ = kMapReadOnly;
  return true;
}

bool MappedFile::MapReadWrite(int64_t file_size) {
  CHECK(IsOpened());
  CHECK(!IsMapped());
#ifdef __linux__
  int result = TEMP_FAILURE_RETRY(ftruncate64(Fd(), file_size));
#else
  int result = TEMP_FAILURE_RETRY(ftruncate(Fd(), file_size));
#endif
  if (result == -1) {
    PLOG(ERROR) << "Failed to truncate file '" << GetPath()
                << "' to size " << file_size;
    return false;
  }
  file_size_ = file_size;
  do {
    mapped_file_ =
        mmap(NULL, file_size_, PROT_READ | PROT_WRITE, MAP_SHARED, Fd(), 0);
  } while (mapped_file_ == MAP_FAILED && errno == EINTR);
  if (mapped_file_ == MAP_FAILED) {
    PLOG(WARNING) << "Failed to mmap file '" << GetPath() << "' of size "
                  << file_size_ << " bytes to memory";
    return false;
  }
  map_mode_ = kMapReadWrite;
  return true;
}

bool MappedFile::Unmap() {
  CHECK(IsMapped());
  int result = TEMP_FAILURE_RETRY(munmap(mapped_file_, file_size_));
  if (result == -1) {
    PLOG(WARNING) << "Failed unmap file '" << GetPath() << "' of size "
                  << file_size_;
    return false;
  } else {
    mapped_file_ = NULL;
    file_size_ = -1;
    return true;
  }
}

int64_t MappedFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
  if (IsMapped()) {
    if (offset < 0) {
      errno = EINVAL;
      return -errno;
    }
    int64_t read_size = std::max(static_cast<int64_t>(0),
                                 std::min(byte_count, file_size_ - offset));
    if (read_size > 0) {
      memcpy(buf, data() + offset, read_size);
    }
    return read_size;
  } else {
    return FdFile::Read(buf, byte_count, offset);
  }
}

int MappedFile::SetLength(int64_t new_length) {
  CHECK(!IsMapped());
  return FdFile::SetLength(new_length);
}

int64_t MappedFile::GetLength() const {
  if (IsMapped()) {
    return file_size_;
  } else {
    return FdFile::GetLength();
  }
}

int MappedFile::Flush() {
  int rc = IsMapped() ? TEMP_FAILURE_RETRY(msync(mapped_file_, file_size_, 0)) : FdFile::Flush();
  return rc == -1 ? -errno : 0;
}

int64_t MappedFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
  if (IsMapped()) {
    CHECK_EQ(kMapReadWrite, map_mode_);
    if (offset < 0) {
      errno = EINVAL;
      return -errno;
    }
    int64_t write_size = std::max(static_cast<int64_t>(0),
                                  std::min(byte_count, file_size_ - offset));
    if (write_size > 0) {
      memcpy(data() + offset, buf, write_size);
    }
    return write_size;
  } else {
    return FdFile::Write(buf, byte_count, offset);
  }
}

int64_t MappedFile::size() const {
  return GetLength();
}

bool MappedFile::IsMapped() const {
  return mapped_file_ != NULL && mapped_file_ != MAP_FAILED;
}

char* MappedFile::data() const {
  CHECK(IsMapped());
  return static_cast<char*>(mapped_file_);
}

}  // namespace unix_file