diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.h b/llvm/tools/llvm-objcopy/llvm-objcopy.h --- a/llvm/tools/llvm-objcopy/llvm-objcopy.h +++ b/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 Write, - bool KeepOwnership = false, unsigned UserID = 0, - unsigned GroupID = 0); + std::function Write); } // end namespace objcopy } // end namespace llvm diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -58,8 +58,7 @@ namespace objcopy { Error writeToFile(StringRef OutputFileName, - std::function Write, bool KeepOwnership, - unsigned UserID, unsigned GroupID) { + std::function 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; } }