1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Path.h" 11 #include "llvm/Support/ErrorHandling.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/MemoryBuffer.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 using namespace llvm::sys; 19 20 #define ASSERT_NO_ERROR(x) \ 21 if (error_code ASSERT_NO_ERROR_ec = x) { \ 22 SmallString<128> MessageStorage; \ 23 raw_svector_ostream Message(MessageStorage); \ 24 Message << #x ": did not return errc::success.\n" \ 25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 28 } else {} 29 30 namespace { 31 32 TEST(is_separator, Works) { 33 EXPECT_TRUE(path::is_separator('/')); 34 EXPECT_FALSE(path::is_separator('\0')); 35 EXPECT_FALSE(path::is_separator('-')); 36 EXPECT_FALSE(path::is_separator(' ')); 37 38 #ifdef LLVM_ON_WIN32 39 EXPECT_TRUE(path::is_separator('\\')); 40 #else 41 EXPECT_FALSE(path::is_separator('\\')); 42 #endif 43 } 44 45 TEST(Support, Path) { 46 SmallVector<StringRef, 40> paths; 47 paths.push_back(""); 48 paths.push_back("."); 49 paths.push_back(".."); 50 paths.push_back("foo"); 51 paths.push_back("/"); 52 paths.push_back("/foo"); 53 paths.push_back("foo/"); 54 paths.push_back("/foo/"); 55 paths.push_back("foo/bar"); 56 paths.push_back("/foo/bar"); 57 paths.push_back("//net"); 58 paths.push_back("//net/foo"); 59 paths.push_back("///foo///"); 60 paths.push_back("///foo///bar"); 61 paths.push_back("/."); 62 paths.push_back("./"); 63 paths.push_back("/.."); 64 paths.push_back("../"); 65 paths.push_back("foo/."); 66 paths.push_back("foo/.."); 67 paths.push_back("foo/./"); 68 paths.push_back("foo/./bar"); 69 paths.push_back("foo/.."); 70 paths.push_back("foo/../"); 71 paths.push_back("foo/../bar"); 72 paths.push_back("c:"); 73 paths.push_back("c:/"); 74 paths.push_back("c:foo"); 75 paths.push_back("c:/foo"); 76 paths.push_back("c:foo/"); 77 paths.push_back("c:/foo/"); 78 paths.push_back("c:/foo/bar"); 79 paths.push_back("prn:"); 80 paths.push_back("c:\\"); 81 paths.push_back("c:foo"); 82 paths.push_back("c:\\foo"); 83 paths.push_back("c:foo\\"); 84 paths.push_back("c:\\foo\\"); 85 paths.push_back("c:\\foo/"); 86 paths.push_back("c:/foo\\bar"); 87 88 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), 89 e = paths.end(); 90 i != e; 91 ++i) { 92 for (sys::path::const_iterator ci = sys::path::begin(*i), 93 ce = sys::path::end(*i); 94 ci != ce; 95 ++ci) { 96 ASSERT_FALSE(ci->empty()); 97 } 98 99 #if 0 // Valgrind is whining about this. 100 outs() << " Reverse Iteration: ["; 101 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), 102 ce = sys::path::rend(*i); 103 ci != ce; 104 ++ci) { 105 outs() << *ci << ','; 106 } 107 outs() << "]\n"; 108 #endif 109 110 path::has_root_path(*i); 111 path::root_path(*i); 112 path::has_root_name(*i); 113 path::root_name(*i); 114 path::has_root_directory(*i); 115 path::root_directory(*i); 116 path::has_parent_path(*i); 117 path::parent_path(*i); 118 path::has_filename(*i); 119 path::filename(*i); 120 path::has_stem(*i); 121 path::stem(*i); 122 path::has_extension(*i); 123 path::extension(*i); 124 path::is_absolute(*i); 125 path::is_relative(*i); 126 127 SmallString<128> temp_store; 128 temp_store = *i; 129 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 130 temp_store = *i; 131 path::remove_filename(temp_store); 132 133 temp_store = *i; 134 path::replace_extension(temp_store, "ext"); 135 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; 136 stem = path::stem(filename); 137 ext = path::extension(filename); 138 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); 139 140 path::native(*i, temp_store); 141 } 142 } 143 144 TEST(Support, RelativePathIterator) { 145 SmallString<64> Path(StringRef("c/d/e/foo.txt")); 146 typedef SmallVector<StringRef, 4> PathComponents; 147 PathComponents ExpectedPathComponents; 148 PathComponents ActualPathComponents; 149 150 StringRef(Path).split(ExpectedPathComponents, "/"); 151 152 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 153 ++I) { 154 ActualPathComponents.push_back(*I); 155 } 156 157 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 158 159 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 160 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 161 } 162 } 163 164 TEST(Support, AbsolutePathIterator) { 165 SmallString<64> Path(StringRef("/c/d/e/foo.txt")); 166 typedef SmallVector<StringRef, 4> PathComponents; 167 PathComponents ExpectedPathComponents; 168 PathComponents ActualPathComponents; 169 170 StringRef(Path).split(ExpectedPathComponents, "/"); 171 172 // The root path will also be a component when iterating 173 ExpectedPathComponents[0] = "/"; 174 175 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 176 ++I) { 177 ActualPathComponents.push_back(*I); 178 } 179 180 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 181 182 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 183 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 184 } 185 } 186 187 #ifdef LLVM_ON_WIN32 188 TEST(Support, AbsolutePathIteratorWin32) { 189 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); 190 typedef SmallVector<StringRef, 4> PathComponents; 191 PathComponents ExpectedPathComponents; 192 PathComponents ActualPathComponents; 193 194 StringRef(Path).split(ExpectedPathComponents, "\\"); 195 196 // The root path (which comes after the drive name) will also be a component 197 // when iterating. 198 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); 199 200 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 201 ++I) { 202 ActualPathComponents.push_back(*I); 203 } 204 205 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 206 207 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 208 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 209 } 210 } 211 #endif // LLVM_ON_WIN32 212 213 TEST(Support, HomeDirectory) { 214 #ifdef LLVM_ON_UNIX 215 // This test only makes sense on Unix if $HOME is set. 216 if (::getenv("HOME")) { 217 #endif 218 SmallString<128> HomeDir; 219 EXPECT_TRUE(path::home_directory(HomeDir)); 220 EXPECT_FALSE(HomeDir.empty()); 221 #ifdef LLVM_ON_UNIX 222 } 223 #endif 224 } 225 226 class FileSystemTest : public testing::Test { 227 protected: 228 /// Unique temporary directory in which all created filesystem entities must 229 /// be placed. It is recursively removed at the end of each test. 230 SmallString<128> TestDirectory; 231 232 virtual void SetUp() { 233 ASSERT_NO_ERROR( 234 fs::createUniqueDirectory("file-system-test", TestDirectory)); 235 // We don't care about this specific file. 236 errs() << "Test Directory: " << TestDirectory << '\n'; 237 errs().flush(); 238 } 239 240 virtual void TearDown() { 241 ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); 242 } 243 }; 244 245 TEST_F(FileSystemTest, Unique) { 246 // Create a temp file. 247 int FileDescriptor; 248 SmallString<64> TempPath; 249 ASSERT_NO_ERROR( 250 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 251 252 // The same file should return an identical unique id. 253 fs::UniqueID F1, F2; 254 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); 255 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); 256 ASSERT_EQ(F1, F2); 257 258 // Different files should return different unique ids. 259 int FileDescriptor2; 260 SmallString<64> TempPath2; 261 ASSERT_NO_ERROR( 262 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); 263 264 fs::UniqueID D; 265 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); 266 ASSERT_NE(D, F1); 267 ::close(FileDescriptor2); 268 269 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 270 271 // Two paths representing the same file on disk should still provide the 272 // same unique id. We can test this by making a hard link. 273 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); 274 fs::UniqueID D2; 275 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); 276 ASSERT_EQ(D2, F1); 277 278 ::close(FileDescriptor); 279 280 SmallString<128> Dir1; 281 ASSERT_NO_ERROR( 282 fs::createUniqueDirectory("dir1", Dir1)); 283 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); 284 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); 285 ASSERT_EQ(F1, F2); 286 287 SmallString<128> Dir2; 288 ASSERT_NO_ERROR( 289 fs::createUniqueDirectory("dir2", Dir2)); 290 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); 291 ASSERT_NE(F1, F2); 292 } 293 294 TEST_F(FileSystemTest, TempFiles) { 295 // Create a temp file. 296 int FileDescriptor; 297 SmallString<64> TempPath; 298 ASSERT_NO_ERROR( 299 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 300 301 // Make sure it exists. 302 bool TempFileExists; 303 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); 304 EXPECT_TRUE(TempFileExists); 305 306 // Create another temp tile. 307 int FD2; 308 SmallString<64> TempPath2; 309 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); 310 ASSERT_TRUE(TempPath2.endswith(".temp")); 311 ASSERT_NE(TempPath.str(), TempPath2.str()); 312 313 fs::file_status A, B; 314 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 315 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 316 EXPECT_FALSE(fs::equivalent(A, B)); 317 318 ::close(FD2); 319 320 // Remove Temp2. 321 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); 322 EXPECT_TRUE(TempFileExists); 323 324 error_code EC = fs::status(TempPath2.c_str(), B); 325 EXPECT_EQ(EC, errc::no_such_file_or_directory); 326 EXPECT_EQ(B.type(), fs::file_type::file_not_found); 327 328 // Make sure Temp2 doesn't exist. 329 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); 330 EXPECT_FALSE(TempFileExists); 331 332 SmallString<64> TempPath3; 333 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); 334 ASSERT_FALSE(TempPath3.endswith(".")); 335 336 // Create a hard link to Temp1. 337 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); 338 bool equal; 339 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 340 EXPECT_TRUE(equal); 341 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 342 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 343 EXPECT_TRUE(fs::equivalent(A, B)); 344 345 // Remove Temp1. 346 ::close(FileDescriptor); 347 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); 348 EXPECT_TRUE(TempFileExists); 349 350 // Remove the hard link. 351 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); 352 EXPECT_TRUE(TempFileExists); 353 354 // Make sure Temp1 doesn't exist. 355 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); 356 EXPECT_FALSE(TempFileExists); 357 358 #ifdef LLVM_ON_WIN32 359 // Path name > 260 chars should get an error. 360 const char *Path270 = 361 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" 362 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 363 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 364 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 365 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 366 EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), 367 windows_error::path_not_found); 368 #endif 369 } 370 371 TEST_F(FileSystemTest, DirectoryIteration) { 372 error_code ec; 373 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) 374 ASSERT_NO_ERROR(ec); 375 376 // Create a known hierarchy to recurse over. 377 bool existed; 378 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) 379 + "/recursive/a0/aa1", existed)); 380 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) 381 + "/recursive/a0/ab1", existed)); 382 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) 383 + "/recursive/dontlookhere/da1", existed)); 384 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) 385 + "/recursive/z0/za1", existed)); 386 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) 387 + "/recursive/pop/p1", existed)); 388 typedef std::vector<std::string> v_t; 389 v_t visited; 390 for (fs::recursive_directory_iterator i(Twine(TestDirectory) 391 + "/recursive", ec), e; i != e; i.increment(ec)){ 392 ASSERT_NO_ERROR(ec); 393 if (path::filename(i->path()) == "p1") { 394 i.pop(); 395 // FIXME: recursive_directory_iterator should be more robust. 396 if (i == e) break; 397 } 398 if (path::filename(i->path()) == "dontlookhere") 399 i.no_push(); 400 visited.push_back(path::filename(i->path())); 401 } 402 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); 403 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); 404 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); 405 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), 406 "dontlookhere"); 407 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); 408 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); 409 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); 410 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); 411 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); 412 413 // Make sure that each path was visited correctly. 414 ASSERT_NE(a0, visited.end()); 415 ASSERT_NE(aa1, visited.end()); 416 ASSERT_NE(ab1, visited.end()); 417 ASSERT_NE(dontlookhere, visited.end()); 418 ASSERT_EQ(da1, visited.end()); // Not visited. 419 ASSERT_NE(z0, visited.end()); 420 ASSERT_NE(za1, visited.end()); 421 ASSERT_NE(pop, visited.end()); 422 ASSERT_EQ(p1, visited.end()); // Not visited. 423 424 // Make sure that parents were visited before children. No other ordering 425 // guarantees can be made across siblings. 426 ASSERT_LT(a0, aa1); 427 ASSERT_LT(a0, ab1); 428 ASSERT_LT(z0, za1); 429 430 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); 431 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); 432 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); 433 ASSERT_NO_ERROR( 434 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); 435 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); 436 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); 437 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); 438 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); 439 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); 440 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); 441 } 442 443 const char archive[] = "!<arch>\x0A"; 444 const char bitcode[] = "\xde\xc0\x17\x0b"; 445 const char coff_object[] = "\x00\x00......"; 446 const char coff_import_library[] = "\x00\x00\xff\xff...."; 447 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 448 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 449 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; 450 const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; 451 const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; 452 const char macho_fixed_virtual_memory_shared_lib[] = 453 "\xfe\xed\xfa\xce..........\x00\x03"; 454 const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; 455 const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; 456 const char macho_dynamically_linked_shared_lib[] = 457 "\xfe\xed\xfa\xce..........\x00\x06"; 458 const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; 459 const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; 460 const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; 461 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; 462 463 TEST_F(FileSystemTest, Magic) { 464 struct type { 465 const char *filename; 466 const char *magic_str; 467 size_t magic_str_len; 468 fs::file_magic magic; 469 } types[] = { 470 #define DEFINE(magic) \ 471 { #magic, magic, sizeof(magic), fs::file_magic::magic } 472 DEFINE(archive), 473 DEFINE(bitcode), 474 DEFINE(coff_object), 475 DEFINE(coff_import_library), 476 DEFINE(elf_relocatable), 477 DEFINE(macho_universal_binary), 478 DEFINE(macho_object), 479 DEFINE(macho_executable), 480 DEFINE(macho_fixed_virtual_memory_shared_lib), 481 DEFINE(macho_core), 482 DEFINE(macho_preload_executable), 483 DEFINE(macho_dynamically_linked_shared_lib), 484 DEFINE(macho_dynamic_linker), 485 DEFINE(macho_bundle), 486 DEFINE(macho_dsym_companion), 487 DEFINE(windows_resource) 488 #undef DEFINE 489 }; 490 491 // Create some files filled with magic. 492 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; 493 ++i) { 494 SmallString<128> file_pathname(TestDirectory); 495 path::append(file_pathname, i->filename); 496 std::string ErrMsg; 497 raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_Binary); 498 ASSERT_FALSE(file.has_error()); 499 StringRef magic(i->magic_str, i->magic_str_len); 500 file << magic; 501 file.close(); 502 bool res = false; 503 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); 504 EXPECT_TRUE(res); 505 EXPECT_EQ(i->magic, fs::identify_magic(magic)); 506 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); 507 } 508 } 509 510 #ifdef LLVM_ON_WIN32 511 TEST_F(FileSystemTest, CarriageReturn) { 512 SmallString<128> FilePathname(TestDirectory); 513 std::string ErrMsg; 514 path::append(FilePathname, "test"); 515 516 { 517 raw_fd_ostream File(FilePathname.c_str(), ErrMsg); 518 EXPECT_EQ(ErrMsg, ""); 519 File << '\n'; 520 } 521 { 522 OwningPtr<MemoryBuffer> Buf; 523 MemoryBuffer::getFile(FilePathname.c_str(), Buf); 524 EXPECT_EQ(Buf->getBuffer(), "\r\n"); 525 } 526 527 { 528 raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Binary); 529 EXPECT_EQ(ErrMsg, ""); 530 File << '\n'; 531 } 532 { 533 OwningPtr<MemoryBuffer> Buf; 534 MemoryBuffer::getFile(FilePathname.c_str(), Buf); 535 EXPECT_EQ(Buf->getBuffer(), "\n"); 536 } 537 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); 538 } 539 #endif 540 541 TEST_F(FileSystemTest, FileMapping) { 542 // Create a temp file. 543 int FileDescriptor; 544 SmallString<64> TempPath; 545 ASSERT_NO_ERROR( 546 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 547 // Map in temp file and add some content 548 error_code EC; 549 StringRef Val("hello there"); 550 { 551 fs::mapped_file_region mfr(FileDescriptor, 552 true, 553 fs::mapped_file_region::readwrite, 554 4096, 555 0, 556 EC); 557 ASSERT_NO_ERROR(EC); 558 std::copy(Val.begin(), Val.end(), mfr.data()); 559 // Explicitly add a 0. 560 mfr.data()[Val.size()] = 0; 561 // Unmap temp file 562 } 563 564 // Map it back in read-only 565 fs::mapped_file_region mfr(Twine(TempPath), 566 fs::mapped_file_region::readonly, 567 0, 568 0, 569 EC); 570 ASSERT_NO_ERROR(EC); 571 572 // Verify content 573 EXPECT_EQ(StringRef(mfr.const_data()), Val); 574 575 // Unmap temp file 576 577 #if LLVM_HAS_RVALUE_REFERENCES 578 fs::mapped_file_region m(Twine(TempPath), 579 fs::mapped_file_region::readonly, 580 0, 581 0, 582 EC); 583 ASSERT_NO_ERROR(EC); 584 const char *Data = m.const_data(); 585 fs::mapped_file_region mfrrv(llvm_move(m)); 586 EXPECT_EQ(mfrrv.const_data(), Data); 587 #endif 588 } 589 } // anonymous namespace 590