+ path::native(*i, temp_store);
+ }
+}
+
+class FileSystemTest : public testing::Test {
+protected:
+ /// Unique temporary directory in which all created filesystem entities must
+ /// be placed. It is recursively removed at the end of each test.
+ SmallString<128> TestDirectory;
+
+ virtual void SetUp() {
+ ASSERT_NO_ERROR(
+ fs::createUniqueDirectory("file-system-test", TestDirectory));
+ // We don't care about this specific file.
+ errs() << "Test Directory: " << TestDirectory << '\n';
+ errs().flush();
+ }
+
+ virtual void TearDown() {
+ uint32_t removed;
+ ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
+ }
+};
+
+TEST_F(FileSystemTest, Unique) {
+ // Create a temp file.
+ int FileDescriptor;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(
+ fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+
+ // The same file should return an identical unique id.
+ uint64_t F1, F2;
+ ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
+ ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
+ ASSERT_EQ(F1, F2);
+
+ // Different files should return different unique ids.
+ int FileDescriptor2;
+ SmallString<64> TempPath2;
+ ASSERT_NO_ERROR(
+ fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
+
+ uint64_t D;
+ ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
+ ASSERT_NE(D, F1);
+ ::close(FileDescriptor2);
+
+ ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
+
+ // Two paths representing the same file on disk should still provide the
+ // same unique id. We can test this by making a hard link.
+ ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
+ uint64_t D2;
+ ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
+ ASSERT_EQ(D2, F1);
+
+ ::close(FileDescriptor);
+}
+
+TEST_F(FileSystemTest, TempFiles) {
+ // Create a temp file.
+ int FileDescriptor;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(
+ fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+
+ // Make sure it exists.
+ bool TempFileExists;
+ ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
+ EXPECT_TRUE(TempFileExists);
+
+ // Create another temp tile.
+ int FD2;
+ SmallString<64> TempPath2;
+ ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
+ ASSERT_NE(TempPath.str(), TempPath2.str());
+
+ fs::file_status A, B;
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
+ EXPECT_FALSE(fs::equivalent(A, B));
+
+ // Try to copy the first to the second.
+ EXPECT_EQ(
+ fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
+
+ ::close(FD2);
+ // Try again with the proper options.
+ ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
+ fs::copy_option::overwrite_if_exists));
+ // Remove Temp2.
+ ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
+ EXPECT_TRUE(TempFileExists);
+
+ // Make sure Temp2 doesn't exist.
+ ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
+ EXPECT_FALSE(TempFileExists);
+
+ // Create a hard link to Temp1.
+ ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
+ bool equal;
+ ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
+ EXPECT_TRUE(equal);
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
+ EXPECT_TRUE(fs::equivalent(A, B));
+
+ // Remove Temp1.
+ ::close(FileDescriptor);
+ ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
+ EXPECT_TRUE(TempFileExists);
+
+ // Remove the hard link.
+ ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
+ EXPECT_TRUE(TempFileExists);
+
+ // Make sure Temp1 doesn't exist.
+ ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
+ EXPECT_FALSE(TempFileExists);
+
+#ifdef LLVM_ON_WIN32
+ // Path name > 260 chars should get an error.
+ const char *Path270 =
+ "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
+ "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
+ "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
+ "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
+ "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
+ EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath),
+ windows_error::path_not_found);
+#endif
+}