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

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 11:50:15 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Abhina Sree (abhina-sree)

<details>
<summary>Changes</summary>

This patch preserves the file tags on z/OS

---
Full diff: https://github.com/llvm/llvm-project/pull/181733.diff


3 Files Affected:

- (modified) llvm/include/llvm/Support/AutoConvert.h (+10) 
- (modified) llvm/lib/Support/AutoConvert.cpp (+10) 
- (modified) llvm/lib/Support/raw_ostream.cpp (+8) 


``````````diff
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));

``````````

</details>


https://github.com/llvm/llvm-project/pull/181733


More information about the llvm-commits mailing list