1 //===- llvm/unittest/Support/ReplaceFileTest.cpp - unit tests -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Errc.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Process.h"
16 #include "gtest/gtest.h"
19 using namespace llvm::sys;
21 #define ASSERT_NO_ERROR(x) \
23 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
24 errs() << #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"; \
31 std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
32 const StringRef &content) {
34 if (std::error_code ec = fs::openFileForWrite(FilePath, FD, fs::F_None))
37 const bool ShouldClose = true;
38 raw_fd_ostream OS(FD, ShouldClose);
41 return std::error_code();
47 ScopedFD(const ScopedFD &) = delete;
48 ScopedFD &operator=(const ScopedFD &) = delete;
51 explicit ScopedFD(int Descriptor) : FD(Descriptor) {}
52 ~ScopedFD() { Process::SafelyCloseFileDescriptor(FD); }
55 bool FDHasContent(int FD, StringRef Content) {
56 auto Buffer = MemoryBuffer::getOpenFile(FD, "", -1);
58 return Buffer.get()->getBuffer() == Content;
61 bool FileHasContent(StringRef File, StringRef Content) {
63 auto EC = fs::openFileForRead(File, FD);
66 ScopedFD EventuallyCloseIt(FD);
67 return FDHasContent(FD, Content);
70 TEST(rename, FileOpenedForReadingCanBeReplaced) {
71 // Create unique temporary directory for this test.
72 SmallString<128> TestDirectory;
73 ASSERT_NO_ERROR(fs::createUniqueDirectory(
74 "FileOpenedForReadingCanBeReplaced-test", TestDirectory));
76 // Add a couple of files to the test directory.
77 SmallString<128> SourceFileName(TestDirectory);
78 path::append(SourceFileName, "source");
80 SmallString<128> TargetFileName(TestDirectory);
81 path::append(TargetFileName, "target");
83 ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
84 ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
87 // Open the target file for reading.
89 ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
90 ScopedFD EventuallyCloseIt(ReadFD);
92 // Confirm we can replace the file while it is open.
93 EXPECT_TRUE(!fs::rename(SourceFileName, TargetFileName));
95 // We should still be able to read the old data through the existing
97 EXPECT_TRUE(FDHasContent(ReadFD, "!!target!!"));
99 // The source file should no longer exist
100 EXPECT_FALSE(fs::exists(SourceFileName));
103 // If we obtain a new descriptor for the target file, we should find that it
104 // contains the content that was in the source file.
105 EXPECT_TRUE(FileHasContent(TargetFileName, "!!source!!"));
107 // Rename the target file back to the source file name to confirm that rename
108 // still works if the destination does not already exist.
109 EXPECT_TRUE(!fs::rename(TargetFileName, SourceFileName));
110 EXPECT_FALSE(fs::exists(TargetFileName));
111 ASSERT_TRUE(fs::exists(SourceFileName));
114 ASSERT_NO_ERROR(fs::remove(SourceFileName));
115 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
118 TEST(rename, ExistingTemp) {
119 // Test that existing .tmpN files don't get deleted by the Windows
120 // sys::fs::rename implementation.
121 SmallString<128> TestDirectory;
123 fs::createUniqueDirectory("ExistingTemp-test", TestDirectory));
125 SmallString<128> SourceFileName(TestDirectory);
126 path::append(SourceFileName, "source");
128 SmallString<128> TargetFileName(TestDirectory);
129 path::append(TargetFileName, "target");
131 SmallString<128> TargetTmp0FileName(TestDirectory);
132 path::append(TargetTmp0FileName, "target.tmp0");
134 SmallString<128> TargetTmp1FileName(TestDirectory);
135 path::append(TargetTmp1FileName, "target.tmp1");
137 ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
138 ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
139 ASSERT_NO_ERROR(CreateFileWithContent(TargetTmp0FileName, "!!target.tmp0!!"));
142 // Use mapped_file_region to make sure that the destination file is mmap'ed.
143 // This will cause SetInformationByHandle to fail when renaming to the
144 // destination, and we will follow the code path that tries to give target
148 ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, TargetFD));
149 ScopedFD X(TargetFD);
150 sys::fs::mapped_file_region MFR(
151 TargetFD, sys::fs::mapped_file_region::readonly, 10, 0, EC);
154 ASSERT_NO_ERROR(fs::rename(SourceFileName, TargetFileName));
157 // Make sure that target was temporarily renamed to target.tmp1 on Windows.
158 // This is signified by a permission denied error as opposed to no such file
159 // or directory when trying to open it.
161 EXPECT_EQ(errc::permission_denied,
162 fs::openFileForRead(TargetTmp1FileName, Tmp1FD));
166 EXPECT_TRUE(FileHasContent(TargetTmp0FileName, "!!target.tmp0!!"));
168 ASSERT_NO_ERROR(fs::remove(TargetFileName));
169 ASSERT_NO_ERROR(fs::remove(TargetTmp0FileName));
170 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
173 } // anonymous namespace