[PATCH] D98511: [llvm-objcopy][NFC] Move ownership keeping code into the restoreStatOnFile().

Alexey Lapshin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 12 07:43:19 PST 2021


avl created this revision.
avl added reviewers: rupprecht, jhenderson, MaskRay, dblaikie, jcai19.
Herald added subscribers: abrachet, yaxunl.
Herald added a reviewer: alexshap.
avl requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The D93881 <https://reviews.llvm.org/D93881> added functionality which preserve ownership for output file
if llvm-objcopy is called under /root. That code was added into the place
where output file is created. The llvm-objcopy already has a function which
sets/restores rights/permissions for the output file.
That is the restoreStatOnFile() function. This patch moves code
(preserving ownershipping) into the restoreStatOnFile() function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98511

Files:
  llvm/tools/llvm-objcopy/llvm-objcopy.cpp
  llvm/tools/llvm-objcopy/llvm-objcopy.h


Index: llvm/tools/llvm-objcopy/llvm-objcopy.h
===================================================================
--- llvm/tools/llvm-objcopy/llvm-objcopy.h
+++ llvm/tools/llvm-objcopy/llvm-objcopy.h
@@ -31,13 +31,9 @@
 /// \p OutputFileName: std::outs for the "-", raw_null_ostream for
 /// the "/dev/null", temporary file in the same directory as the final output
 /// file for other names. The final output file is atomically replaced with
-/// the temporary file after \p Write handler is finished. \p KeepOwnership
-/// used to setting specified \p UserID and \p GroupID for the resulting file
-/// if writeToFile is called under /root.
+/// the temporary file after \p Write handler is finished.
 Error writeToFile(StringRef OutputFileName,
-                  std::function<Error(raw_ostream &)> Write,
-                  bool KeepOwnership = false, unsigned UserID = 0,
-                  unsigned GroupID = 0);
+                  std::function<Error(raw_ostream &)> Write);
 
 } // end namespace objcopy
 } // end namespace llvm
Index: llvm/tools/llvm-objcopy/llvm-objcopy.cpp
===================================================================
--- llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -58,8 +58,7 @@
 namespace objcopy {
 
 Error writeToFile(StringRef OutputFileName,
-                  std::function<Error(raw_ostream &)> Write, bool KeepOwnership,
-                  unsigned UserID, unsigned GroupID) {
+                  std::function<Error(raw_ostream &)> Write) {
   if (OutputFileName == "-")
     return Write(outs());
 
@@ -74,15 +73,6 @@
   if (!Temp)
     return createFileError(OutputFileName, Temp.takeError());
 
-#ifndef _WIN32
-  // Try to preserve file ownership if requested.
-  if (KeepOwnership) {
-    sys::fs::file_status Stat;
-    if (!sys::fs::status(Temp->FD, Stat) && Stat.getUser() == 0)
-      sys::fs::changeFileOwnership(Temp->FD, UserID, GroupID);
-  }
-#endif
-
   raw_fd_ostream Out(Temp->FD, false);
 
   if (Error E = Write(Out)) {
@@ -306,6 +296,13 @@
     if (auto EC = sys::fs::setPermissions(FD, Perm))
 #endif
       return createFileError(Filename, EC);
+
+#ifndef _WIN32
+    // Keep ownership if llvm-objcopy is called under /root.
+    if (Config.InputFilename == Config.OutputFilename)
+      if (OStat.getUser() == 0)
+        sys::fs::changeFileOwnership(FD, Stat.getUser(), Stat.getGroup());
+#endif
   }
 
   if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
@@ -360,14 +357,10 @@
         return E;
     } else {
       if (Error E = writeToFile(
-              Config.OutputFilename,
-              [&](raw_ostream &OutFile) -> Error {
+              Config.OutputFilename, [&](raw_ostream &OutFile) -> Error {
                 return executeObjcopyOnBinary(
                     Config, *BinaryOrErr.get().getBinary(), OutFile);
-              },
-              Config.InputFilename != "-" &&
-                  Config.InputFilename == Config.OutputFilename,
-              Stat.getUser(), Stat.getGroup()))
+              }))
         return E;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98511.330246.patch
Type: text/x-patch
Size: 3081 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210312/16551fa4/attachment.bin>


More information about the llvm-commits mailing list