[llvm] [Support] Report EISDIR when opening a directory (PR #79880)
Hubert Tong via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 15 08:30:56 PDT 2024
================
@@ -1296,6 +1296,39 @@ TEST_F(FileSystemTest, UTF8ToUTF16DirectoryIteration) {
}
#endif
+TEST_F(FileSystemTest, OpenDirectoryAsFileForRead) {
+ ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory)));
+ ASSERT_EQ(fs::create_directory(Twine(TestDirectory), false),
+ errc::file_exists);
+
+ std::string Buf(5, '?');
+ Expected<fs::file_t> FD = fs::openNativeFileForRead(TestDirectory);
+#ifdef _WIN32
+ ASSERT_EQ(errorToErrorCode(BytesRead.takeError()), errc::is_a_directory);
+#else
+ ASSERT_THAT_EXPECTED(FD, Succeeded());
+ auto Close = make_scope_exit([&] { fs::closeFile(*FD); });
+ Expected<size_t> BytesRead =
+ fs::readNativeFile(*FD, MutableArrayRef(&*Buf.begin(), Buf.size()));
+ ASSERT_EQ(errorToErrorCode(BytesRead.takeError()), errc::is_a_directory);
+#endif
+}
+
+TEST_F(FileSystemTest, OpenDirectoryAsFileForWrite) {
+ ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory)));
+ ASSERT_EQ(fs::create_directory(Twine(TestDirectory), false),
+ errc::file_exists);
+
+ int FD;
+ std::error_code EC;
+ EC = fs::openFileForWrite(Twine(TestDirectory), FD);
+ if (!EC)
+ ::close(FD);
+ ASSERT_EQ(EC, errc::is_a_directory);
+
+ ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory)));
----------------
hubert-reinterpretcast wrote:
It is better to use `make_scope_exit` right after the directory is created.
https://github.com/llvm/llvm-project/pull/79880
More information about the llvm-commits
mailing list