Skip to content

Commit 502617a

Browse files
committed
writable_file: fix mapping permissions
Was outright missing write permissions on windows and private/non-shared on linux.
1 parent 977ad2a commit 502617a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

include/decodeless/detail/mappedfile_linux.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ template <bool Writable>
182182
class MappedFile {
183183
public:
184184
using data_type = std::conditional_t<Writable, void*, const void*>;
185-
MappedFile(const fs::path& path, int mapFlags = MAP_PRIVATE)
185+
MappedFile(const fs::path& path)
186186
: m_file(path, Writable ? O_RDWR : O_RDONLY)
187-
, m_mapped(nullptr, m_file.size(), mapFlags, m_file, 0) {}
187+
, m_mapped(nullptr, m_file.size(), Writable ? MAP_SHARED : MAP_PRIVATE, m_file, 0) {}
188188

189189
data_type data() const { return m_mapped.address(); }
190190
size_t size() const { return m_mapped.size(); }

include/decodeless/detail/mappedfile_windows.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ class MappedFile {
174174
FILE_SHARE_READ | (Writable ? FILE_SHARE_WRITE : 0), nullptr, OPEN_EXISTING,
175175
FILE_ATTRIBUTE_NORMAL, nullptr)
176176
, m_size(m_file.size())
177-
, m_mapping(m_file, nullptr, PAGE_READONLY, m_size, nullptr)
178-
, m_rawView(m_mapping, FILE_MAP_READ) {}
177+
, m_mapping(m_file, nullptr, Writable ? PAGE_READWRITE : PAGE_READONLY, m_size, nullptr)
178+
, m_rawView(m_mapping, FILE_MAP_READ | (Writable ? FILE_MAP_WRITE : 0)) {}
179179
data_type data() const { return m_rawView.address(); }
180180
size_t size() const { return m_size; }
181181

test/src/mappedfile.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@ class MappedFileFixture : public ::testing::Test {
2626

2727
TEST_F(MappedFileFixture, ReadOnly) {
2828
file mapped(m_tmpFile);
29-
EXPECT_EQ(*reinterpret_cast<const int*>(mapped.data()), 42);
29+
EXPECT_EQ(*static_cast<const int*>(mapped.data()), 42);
30+
}
31+
32+
TEST_F(MappedFileFixture, Writable) {
33+
{
34+
writable_file mapped(m_tmpFile);
35+
ASSERT_GE(mapped.size(), sizeof(int));
36+
*static_cast<int*>(mapped.data()) = 123;
37+
}
38+
{
39+
std::ifstream ifile(m_tmpFile, std::ios::binary);
40+
int contents;
41+
ifile.read(reinterpret_cast<char*>(&contents), sizeof(contents));
42+
EXPECT_EQ(contents, 123);
43+
}
3044
}
3145

3246
#ifdef _WIN32

0 commit comments

Comments
 (0)