[llvm] [SystemZ][z/OS] Preserve filetag when rewriting files (PR #181733)

Abhina Sree via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 11:49:43 PST 2026


https://github.com/abhina-sree created https://github.com/llvm/llvm-project/pull/181733

This patch preserves the file tags on z/OS

>From 76fb40f94f8b0f7d1b83e7a7e12b79059a92e5a1 Mon Sep 17 00:00:00 2001
From: Abhina Sreeskantharajan <Abhina.Sreeskantharajan at ibm.com>
Date: Mon, 16 Feb 2026 10:47:14 -0500
Subject: [PATCH] copy file tag attributes on z/OS

---
 llvm/include/llvm/Support/AutoConvert.h | 10 ++++++++++
 llvm/lib/Support/AutoConvert.cpp        | 10 ++++++++++
 llvm/lib/Support/raw_ostream.cpp        |  8 ++++++++
 3 files changed, 28 insertions(+)

diff --git a/llvm/include/llvm/Support/AutoConvert.h b/llvm/include/llvm/Support/AutoConvert.h
index 15f1ec8af6c57..70b617178cb05 100644
--- a/llvm/include/llvm/Support/AutoConvert.h
+++ b/llvm/include/llvm/Support/AutoConvert.h
@@ -55,6 +55,16 @@ ErrorOr<__ccsid_t> getzOSFileTag(const Twine &FileName, const int FD = -1);
  */
 ErrorOr<bool> needzOSConversion(const Twine &FileName, const int FD = -1);
 
+/** Copy the tag attributes from \a source to \a destination.
+ *
+ * @param Source The name of the source file.
+ * @param Destination The file descriptor of the destination file.
+ * @returns errc::success if the tag attributes were copied successfully,
+ *          otherwise returns a specific error_code.
+ */
+std::error_code copyFileTagAttributes(const std::string &Source,
+                                      const int Destination);
+
 #endif /* __MVS__*/
 
 inline std::error_code disableAutoConversion(int FD) {
diff --git a/llvm/lib/Support/AutoConvert.cpp b/llvm/lib/Support/AutoConvert.cpp
index 741bb7bd2c5b0..03517815be3a3 100644
--- a/llvm/lib/Support/AutoConvert.cpp
+++ b/llvm/lib/Support/AutoConvert.cpp
@@ -132,4 +132,14 @@ ErrorOr<bool> llvm::needzOSConversion(const Twine &FileName, const int FD) {
   }
 }
 
+std::error_code llvm::copyFileTagAttributes(const std::string &Source,
+                                            const int Destination) {
+  struct stat SourceAttributes;
+  if (stat(Source.c_str(), &SourceAttributes) == -1)
+    return std::error_code(errno, std::generic_category());
+
+  return setzOSFileTag(Destination, SourceAttributes.st_tag.ft_ccsid,
+                       SourceAttributes.st_tag.ft_txtflag);
+}
+
 #endif //__MVS__
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index fe07aed335f7e..2b856383e50fc 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -1025,6 +1025,14 @@ Error llvm::writeToOutput(StringRef OutputFileName,
 
   raw_fd_ostream Out(Temp->FD, false);
 
+#if defined(__MVS__)
+  if (auto EC = llvm::copyFileTagAttributes(OutputFileName.str(), Temp->FD)) {
+    if (EC != std::errc::no_such_file_or_directory)
+      llvm::errs() << "Failed to preserve file tag attributes for "
+                   << OutputFileName << "\n";
+  }
+#endif
+
   if (Error E = Write(Out)) {
     if (Error DiscardError = Temp->discard())
       return joinErrors(std::move(E), std::move(DiscardError));



More information about the llvm-commits mailing list