Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,9 @@ class mapped_file_region {
LLVM_ABI size_t size() const;
LLVM_ABI char *data() const;

/// Write changes to disk and synchronize. Equivalent to POSIX msync.
LLVM_ABI std::error_code sync() const;

/// Get a const view of the data. Modifying this memory has undefined
/// behavior.
LLVM_ABI const char *const_data() const;
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,12 @@ void mapped_file_region::unmapImpl() {
::munmap(Mapping, Size);
}

std::error_code mapped_file_region::sync() const {
if (int Res = ::msync(Mapping, Size, MS_SYNC))
return std::error_code(Res, std::generic_category());
return std::error_code();
}

void mapped_file_region::dontNeedImpl() {
assert(Mode == mapped_file_region::readonly);
if (!Mapping)
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,12 @@ void mapped_file_region::unmapImpl() {

void mapped_file_region::dontNeedImpl() {}

std::error_code mapped_file_region::sync() const {
if (::FlushViewOfFile(Mapping, 0))
return std::error_code();
return mapWindowsError(::GetLastError());
}

int mapped_file_region::alignment() {
SYSTEM_INFO SysInfo;
::GetSystemInfo(&SysInfo);
Expand Down
26 changes: 26 additions & 0 deletions llvm/unittests/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,32 @@ TEST_F(FileSystemTest, FileMapping) {
ASSERT_NO_ERROR(fs::remove(TempPath));
}

TEST_F(FileSystemTest, FileMappingSync) {
// Create a temp file.
auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
ASSERT_TRUE((bool)TempFileOrError);
fs::TempFile File = std::move(*TempFileOrError);
StringRef Content("hello there");
ASSERT_NO_ERROR(
fs::resize_file_before_mapping_readwrite(File.FD, Content.size()));
{
// Map in the file and write some content.
std::error_code EC;
fs::mapped_file_region MFR(fs::convertFDToNativeFile(File.FD),
fs::mapped_file_region::readwrite,
Content.size(), 0, EC);
ASSERT_NO_ERROR(EC);
std::copy(Content.begin(), Content.end(), MFR.data());

// Synchronize and check the contents before unmapping.
MFR.sync();
auto Buffer = MemoryBuffer::getFile(File.TmpName);
ASSERT_TRUE((bool)Buffer);
ASSERT_EQ(Content, Buffer->get()->getBuffer());
}
ASSERT_FALSE((bool)File.discard());
}

TEST(Support, NormalizePath) {
// Input, Expected Win, Expected Posix
using TestTuple = std::tuple<const char *, const char *, const char *>;
Expand Down
Loading