ReplaceFileTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===- llvm/unittest/Support/ReplaceFileTest.cpp - unit tests -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/Errc.h"
  9. #include "llvm/Support/ErrorHandling.h"
  10. #include "llvm/Support/FileSystem.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/Process.h"
  14. #include "gtest/gtest.h"
  15. using namespace llvm;
  16. using namespace llvm::sys;
  17. #define ASSERT_NO_ERROR(x) \
  18. do { \
  19. if (std::error_code ASSERT_NO_ERROR_ec = x) { \
  20. errs() << #x ": did not return errc::success.\n" \
  21. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  22. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  23. } \
  24. } while (false)
  25. namespace {
  26. std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
  27. const StringRef &content) {
  28. int FD = 0;
  29. if (std::error_code ec = fs::openFileForWrite(FilePath, FD))
  30. return ec;
  31. const bool ShouldClose = true;
  32. raw_fd_ostream OS(FD, ShouldClose);
  33. OS << content;
  34. return std::error_code();
  35. }
  36. class ScopedFD {
  37. int FD;
  38. ScopedFD(const ScopedFD &) = delete;
  39. ScopedFD &operator=(const ScopedFD &) = delete;
  40. public:
  41. explicit ScopedFD(int Descriptor) : FD(Descriptor) {}
  42. ~ScopedFD() { Process::SafelyCloseFileDescriptor(FD); }
  43. };
  44. bool FDHasContent(int FD, StringRef Content) {
  45. auto Buffer =
  46. MemoryBuffer::getOpenFile(sys::fs::convertFDToNativeFile(FD), "", -1);
  47. assert(Buffer);
  48. return Buffer.get()->getBuffer() == Content;
  49. }
  50. bool FileHasContent(StringRef File, StringRef Content) {
  51. int FD = 0;
  52. auto EC = fs::openFileForRead(File, FD);
  53. (void)EC;
  54. assert(!EC);
  55. ScopedFD EventuallyCloseIt(FD);
  56. return FDHasContent(FD, Content);
  57. }
  58. TEST(rename, FileOpenedForReadingCanBeReplaced) {
  59. // Create unique temporary directory for this test.
  60. SmallString<128> TestDirectory;
  61. ASSERT_NO_ERROR(fs::createUniqueDirectory(
  62. "FileOpenedForReadingCanBeReplaced-test", TestDirectory));
  63. // Add a couple of files to the test directory.
  64. SmallString<128> SourceFileName(TestDirectory);
  65. path::append(SourceFileName, "source");
  66. SmallString<128> TargetFileName(TestDirectory);
  67. path::append(TargetFileName, "target");
  68. ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
  69. ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
  70. {
  71. // Open the target file for reading.
  72. int ReadFD = 0;
  73. ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
  74. ScopedFD EventuallyCloseIt(ReadFD);
  75. // Confirm we can replace the file while it is open.
  76. EXPECT_TRUE(!fs::rename(SourceFileName, TargetFileName));
  77. // We should still be able to read the old data through the existing
  78. // descriptor.
  79. EXPECT_TRUE(FDHasContent(ReadFD, "!!target!!"));
  80. // The source file should no longer exist
  81. EXPECT_FALSE(fs::exists(SourceFileName));
  82. }
  83. // If we obtain a new descriptor for the target file, we should find that it
  84. // contains the content that was in the source file.
  85. EXPECT_TRUE(FileHasContent(TargetFileName, "!!source!!"));
  86. // Rename the target file back to the source file name to confirm that rename
  87. // still works if the destination does not already exist.
  88. EXPECT_TRUE(!fs::rename(TargetFileName, SourceFileName));
  89. EXPECT_FALSE(fs::exists(TargetFileName));
  90. ASSERT_TRUE(fs::exists(SourceFileName));
  91. // Clean up.
  92. ASSERT_NO_ERROR(fs::remove(SourceFileName));
  93. ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
  94. }
  95. TEST(rename, ExistingTemp) {
  96. // Test that existing .tmpN files don't get deleted by the Windows
  97. // sys::fs::rename implementation.
  98. SmallString<128> TestDirectory;
  99. ASSERT_NO_ERROR(
  100. fs::createUniqueDirectory("ExistingTemp-test", TestDirectory));
  101. SmallString<128> SourceFileName(TestDirectory);
  102. path::append(SourceFileName, "source");
  103. SmallString<128> TargetFileName(TestDirectory);
  104. path::append(TargetFileName, "target");
  105. SmallString<128> TargetTmp0FileName(TestDirectory);
  106. path::append(TargetTmp0FileName, "target.tmp0");
  107. SmallString<128> TargetTmp1FileName(TestDirectory);
  108. path::append(TargetTmp1FileName, "target.tmp1");
  109. ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
  110. ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
  111. ASSERT_NO_ERROR(CreateFileWithContent(TargetTmp0FileName, "!!target.tmp0!!"));
  112. {
  113. // Use mapped_file_region to make sure that the destination file is mmap'ed.
  114. // This will cause SetInformationByHandle to fail when renaming to the
  115. // destination, and we will follow the code path that tries to give target
  116. // a temporary name.
  117. int TargetFD;
  118. std::error_code EC;
  119. ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, TargetFD));
  120. ScopedFD X(TargetFD);
  121. sys::fs::mapped_file_region MFR(sys::fs::convertFDToNativeFile(TargetFD),
  122. sys::fs::mapped_file_region::readonly, 10,
  123. 0, EC);
  124. ASSERT_FALSE(EC);
  125. ASSERT_NO_ERROR(fs::rename(SourceFileName, TargetFileName));
  126. #ifdef _WIN32
  127. // Make sure that target was temporarily renamed to target.tmp1 on Windows.
  128. // This is signified by a permission denied error as opposed to no such file
  129. // or directory when trying to open it.
  130. int Tmp1FD;
  131. EXPECT_EQ(errc::permission_denied,
  132. fs::openFileForRead(TargetTmp1FileName, Tmp1FD));
  133. #endif
  134. }
  135. EXPECT_TRUE(FileHasContent(TargetTmp0FileName, "!!target.tmp0!!"));
  136. ASSERT_NO_ERROR(fs::remove(TargetFileName));
  137. ASSERT_NO_ERROR(fs::remove(TargetTmp0FileName));
  138. ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
  139. }
  140. } // anonymous namespace