[llvm] [SystemZ][z/OS] Query if a file is text and set the text flag accordingly (PR #109664)
Abhina Sree via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 24 08:43:45 PDT 2024
https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/109664
>From d417ccc42ebfe97d1ee91b74c8bf0651bd2e8379 Mon Sep 17 00:00:00 2001
From: Abhina Sreeskantharajan <Abhina.Sreeskantharajan at ibm.com>
Date: Mon, 23 Sep 2024 09:39:45 -0400
Subject: [PATCH 1/2] Query if a file is text and set the text flag accordingly
---
llvm/include/llvm/Support/AutoConvert.h | 6 ++++++
llvm/lib/Support/AutoConvert.cpp | 26 +++++++++++++++++++++++++
llvm/lib/Support/VirtualFileSystem.cpp | 9 ++++++++-
3 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/AutoConvert.h b/llvm/include/llvm/Support/AutoConvert.h
index 6f45c4683f7775..8ef6d3e8cf973e 100644
--- a/llvm/include/llvm/Support/AutoConvert.h
+++ b/llvm/include/llvm/Support/AutoConvert.h
@@ -52,6 +52,12 @@ std::error_code restorezOSStdHandleAutoConversion(int FD);
/// \brief Set the tag information for a file descriptor.
std::error_code setzOSFileTag(int FD, int CCSID, bool Text);
+// Get the the tag ccsid for a file name or a file descriptor.
+ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
+
+// Query the file tag to determine if the file is a text file.
+ErrorOr<bool> iszOSTextFile(const char *Filename, const int FD = -1);
+
} // namespace llvm
#endif // __cplusplus
diff --git a/llvm/lib/Support/AutoConvert.cpp b/llvm/lib/Support/AutoConvert.cpp
index 66570735f8fc88..06130876c67f05 100644
--- a/llvm/lib/Support/AutoConvert.cpp
+++ b/llvm/lib/Support/AutoConvert.cpp
@@ -116,4 +116,30 @@ std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
return std::error_code();
}
+ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
+ // If we have a file descriptor, use it to find out file tagging. Otherwise we
+ // need to use stat() with the file path.
+ if (FD != -1) {
+ struct f_cnvrt Query = {
+ QUERYCVT, // cvtcmd
+ 0, // pccsid
+ 0, // fccsid
+ };
+ if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
+ return std::error_code(errno, std::generic_category());
+ return Query.fccsid;
+ }
+ struct stat Attr;
+ if (stat(FileName, &Attr) == -1)
+ return std::error_code(errno, std::generic_category());
+ return Attr.st_tag.ft_ccsid;
+}
+
+ErrorOr<bool> llvm::iszOSTextFile(const char *Filename, const int FD) {
+ ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(Filename, FD);
+ if (std::error_code EC = Ccsid.getError())
+ return EC;
+ return *Ccsid != FT_BINARY;
+}
+
#endif // __MVS__
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 928c0b5a24ed65..95a5aeefa61336 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -325,8 +325,15 @@ ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
ErrorOr<std::unique_ptr<File>>
RealFileSystem::openFileForRead(const Twine &Name) {
SmallString<256> RealName, Storage;
+ auto OpenFlags = sys::fs::OF_None;
+#ifdef __MVS__
+ // If the file is tagged with a text ccsid, it may require autoconversion.
+ llvm::ErrorOr<bool> IsFileText = llvm::iszOSTextFile(Name.str().c_str());
+ if (IsFileText && *IsFileText)
+ OpenFlags |= sys::fs::OF_Text;
+#endif
Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
- adjustPath(Name, Storage), sys::fs::OF_None, &RealName);
+ adjustPath(Name, Storage), OpenFlags, &RealName);
if (!FDOrErr)
return errorToErrorCode(FDOrErr.takeError());
return std::unique_ptr<File>(
>From 451e240d4ef50da120bb33c049dfe71d7ff9c70d Mon Sep 17 00:00:00 2001
From: Abhina Sreeskantharajan <Abhina.Sreeskantharajan at ibm.com>
Date: Tue, 24 Sep 2024 11:43:33 -0400
Subject: [PATCH 2/2] address comments
---
llvm/include/llvm/Support/AutoConvert.h | 5 +----
llvm/lib/Support/AutoConvert.cpp | 6 +++---
llvm/lib/Support/VirtualFileSystem.cpp | 3 +--
3 files changed, 5 insertions(+), 9 deletions(-)
diff --git a/llvm/include/llvm/Support/AutoConvert.h b/llvm/include/llvm/Support/AutoConvert.h
index 8ef6d3e8cf973e..a1577713f1b65d 100644
--- a/llvm/include/llvm/Support/AutoConvert.h
+++ b/llvm/include/llvm/Support/AutoConvert.h
@@ -52,11 +52,8 @@ std::error_code restorezOSStdHandleAutoConversion(int FD);
/// \brief Set the tag information for a file descriptor.
std::error_code setzOSFileTag(int FD, int CCSID, bool Text);
-// Get the the tag ccsid for a file name or a file descriptor.
-ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
-
// Query the file tag to determine if the file is a text file.
-ErrorOr<bool> iszOSTextFile(const char *Filename, const int FD = -1);
+bool iszOSTextFile(const char *Filename, const int FD = -1);
} // namespace llvm
#endif // __cplusplus
diff --git a/llvm/lib/Support/AutoConvert.cpp b/llvm/lib/Support/AutoConvert.cpp
index 06130876c67f05..47cf839bfa39ce 100644
--- a/llvm/lib/Support/AutoConvert.cpp
+++ b/llvm/lib/Support/AutoConvert.cpp
@@ -116,7 +116,7 @@ std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
return std::error_code();
}
-ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
+ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD) {
// If we have a file descriptor, use it to find out file tagging. Otherwise we
// need to use stat() with the file path.
if (FD != -1) {
@@ -135,10 +135,10 @@ ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
return Attr.st_tag.ft_ccsid;
}
-ErrorOr<bool> llvm::iszOSTextFile(const char *Filename, const int FD) {
+bool llvm::iszOSTextFile(const char *Filename, const int FD) {
ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(Filename, FD);
if (std::error_code EC = Ccsid.getError())
- return EC;
+ return false;
return *Ccsid != FT_BINARY;
}
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 95a5aeefa61336..d9df9c951d4bbe 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -328,8 +328,7 @@ RealFileSystem::openFileForRead(const Twine &Name) {
auto OpenFlags = sys::fs::OF_None;
#ifdef __MVS__
// If the file is tagged with a text ccsid, it may require autoconversion.
- llvm::ErrorOr<bool> IsFileText = llvm::iszOSTextFile(Name.str().c_str());
- if (IsFileText && *IsFileText)
+ if (llvm::iszOSTextFile(Name.str().c_str()))
OpenFlags |= sys::fs::OF_Text;
#endif
Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
More information about the llvm-commits
mailing list