#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::sys;
#define ASSERT_NO_ERROR(x) \
do { \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
errs() << #x ": did not return errc::success.\n" \
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
} \
} while (false)
namespace {
std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
const StringRef &content) {
int FD = 0;
if (std::error_code ec = fs::openFileForWrite(FilePath, FD))
return ec;
const bool ShouldClose = true;
raw_fd_ostream OS(FD, ShouldClose);
OS << content;
return std::error_code();
}
class ScopedFD {
int FD;
ScopedFD(const ScopedFD &) = delete;
ScopedFD &operator=(const ScopedFD &) = delete;
public:
explicit ScopedFD(int Descriptor) : FD(Descriptor) {}
~ScopedFD() { Process::SafelyCloseFileDescriptor(FD); }
};
bool FDHasContent(int FD, StringRef Content) {
auto Buffer =
MemoryBuffer::getOpenFile(sys::fs::convertFDToNativeFile(FD), "", -1);
assert(Buffer);
return Buffer.get()->getBuffer() == Content;
}
bool FileHasContent(StringRef File, StringRef Content) {
int FD = 0;
auto EC = fs::openFileForRead(File, FD);
(void)EC;
assert(!EC);
ScopedFD EventuallyCloseIt(FD);
return FDHasContent(FD, Content);
}
TEST(rename, FileOpenedForReadingCanBeReplaced) {
SmallString<128> TestDirectory;
ASSERT_NO_ERROR(fs::createUniqueDirectory(
"FileOpenedForReadingCanBeReplaced-test", TestDirectory));
SmallString<128> SourceFileName(TestDirectory);
path::append(SourceFileName, "source");
SmallString<128> TargetFileName(TestDirectory);
path::append(TargetFileName, "target");
ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
{
int ReadFD = 0;
ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, ReadFD));
ScopedFD EventuallyCloseIt(ReadFD);
EXPECT_TRUE(!fs::rename(SourceFileName, TargetFileName));
EXPECT_TRUE(FDHasContent(ReadFD, "!!target!!"));
EXPECT_FALSE(fs::exists(SourceFileName));
}
EXPECT_TRUE(FileHasContent(TargetFileName, "!!source!!"));
EXPECT_TRUE(!fs::rename(TargetFileName, SourceFileName));
EXPECT_FALSE(fs::exists(TargetFileName));
ASSERT_TRUE(fs::exists(SourceFileName));
ASSERT_NO_ERROR(fs::remove(SourceFileName));
ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
}
TEST(rename, ExistingTemp) {
SmallString<128> TestDirectory;
ASSERT_NO_ERROR(
fs::createUniqueDirectory("ExistingTemp-test", TestDirectory));
SmallString<128> SourceFileName(TestDirectory);
path::append(SourceFileName, "source");
SmallString<128> TargetFileName(TestDirectory);
path::append(TargetFileName, "target");
SmallString<128> TargetTmp0FileName(TestDirectory);
path::append(TargetTmp0FileName, "target.tmp0");
SmallString<128> TargetTmp1FileName(TestDirectory);
path::append(TargetTmp1FileName, "target.tmp1");
ASSERT_NO_ERROR(CreateFileWithContent(SourceFileName, "!!source!!"));
ASSERT_NO_ERROR(CreateFileWithContent(TargetFileName, "!!target!!"));
ASSERT_NO_ERROR(CreateFileWithContent(TargetTmp0FileName, "!!target.tmp0!!"));
{
int TargetFD;
std::error_code EC;
ASSERT_NO_ERROR(fs::openFileForRead(TargetFileName, TargetFD));
ScopedFD X(TargetFD);
sys::fs::mapped_file_region MFR(sys::fs::convertFDToNativeFile(TargetFD),
sys::fs::mapped_file_region::readonly, 10,
0, EC);
ASSERT_FALSE(EC);
ASSERT_NO_ERROR(fs::rename(SourceFileName, TargetFileName));
#ifdef _WIN32
int Tmp1FD;
EXPECT_EQ(errc::permission_denied,
fs::openFileForRead(TargetTmp1FileName, Tmp1FD));
#endif
}
EXPECT_TRUE(FileHasContent(TargetTmp0FileName, "!!target.tmp0!!"));
ASSERT_NO_ERROR(fs::remove(TargetFileName));
ASSERT_NO_ERROR(fs::remove(TargetTmp0FileName));
ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
}
}