diff options
author | Steven Wu <stevenwu@apple.com> | 2025-08-14 17:05:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-14 17:05:33 -0700 |
commit | 7e46f5db21d40dd386aced6586e5e1fa6892092f (patch) | |
tree | ed69e630f44ecfe85fda4c81cae613869756363c /llvm/unittests/Support/Path.cpp | |
parent | 769a9058c8d04fc920994f6a5bbb03c8a4fbcd05 (diff) | |
download | llvm-7e46f5db21d40dd386aced6586e5e1fa6892092f.zip llvm-7e46f5db21d40dd386aced6586e5e1fa6892092f.tar.gz llvm-7e46f5db21d40dd386aced6586e5e1fa6892092f.tar.bz2 |
[Support] Add mapped_file_region::sync(), equivalent to msync (#153632)
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 355aa6b..888729b 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -1471,6 +1471,43 @@ TEST_F(FileSystemTest, FileMapping) { ASSERT_NO_ERROR(fs::remove(TempPath)); } +TEST_F(FileSystemTest, FileMappingSync) { + // Create a temp file. + SmallString<0> TempPath(TestDirectory); + sys::path::append(TempPath, "test-%%%%"); + auto TempFileOrError = fs::TempFile::create(TempPath); + ASSERT_TRUE((bool)TempFileOrError); + fs::TempFile File = std::move(*TempFileOrError); + StringRef Content("hello there"); + std::string FileName = File.TmpName; + 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); + + // Keep the file so it can be read. + ASSERT_FALSE((bool)File.keep()); + + // Write content through mapped memory. + ASSERT_NO_ERROR(EC); + std::copy(Content.begin(), Content.end(), MFR.data()); + + // Synchronize to file system. + ASSERT_FALSE((bool)MFR.sync()); + + // Check the file content using file IO APIs. + auto Buffer = MemoryBuffer::getFile(FileName); + ASSERT_TRUE((bool)Buffer); + ASSERT_EQ(Content, Buffer->get()->getBuffer()); + } + // Manually remove the test file. + ASSERT_FALSE((bool)fs::remove(FileName)); +} + TEST(Support, NormalizePath) { // Input, Expected Win, Expected Posix using TestTuple = std::tuple<const char *, const char *, const char *>; |