aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
diff options
context:
space:
mode:
authorAlexey Lapshin <a.v.lapshin@mail.ru>2022-04-13 23:40:27 +0300
committerAlexey Lapshin <a.v.lapshin@mail.ru>2022-04-22 20:06:01 +0300
commit79c1991010bda4eb2950e164ba0df4bde4fafa76 (patch)
tree75d648e168048379e9dbb6168076b83b5069114a /llvm/tools/llvm-objcopy/llvm-objcopy.cpp
parent225b91e6cbba31ff1ce787a152a67977d08fdcab (diff)
downloadllvm-79c1991010bda4eb2950e164ba0df4bde4fafa76.zip
llvm-79c1991010bda4eb2950e164ba0df4bde4fafa76.tar.gz
llvm-79c1991010bda4eb2950e164ba0df4bde4fafa76.tar.bz2
[llvm-objcopy][NFC] refactor restoreStatOnFile out of llvm-objcopy.
Functionality of restoreStatOnFile may be reused. Move it into FileUtilities.cpp. Create helper class FilePermissionsApplier to store and apply permissions. Differential Revision: https://reviews.llvm.org/D123821
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/llvm-objcopy.cpp69
1 files changed, 10 insertions, 59 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 0071266..2aaeea6 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -41,6 +41,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Memory.h"
@@ -131,66 +132,16 @@ static Error executeObjcopyOnRawBinary(ConfigManager &ConfigMgr,
llvm_unreachable("unsupported output format");
}
-static Error restoreStatOnFile(StringRef Filename,
- const sys::fs::file_status &Stat,
- const ConfigManager &ConfigMgr) {
- int FD;
- const CommonConfig &Config = ConfigMgr.getCommonConfig();
-
- // Writing to stdout should not be treated as an error here, just
- // do not set access/modification times or permissions.
- if (Filename == "-")
- return Error::success();
-
- if (auto EC =
- sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
- return createFileError(Filename, EC);
-
- if (Config.PreserveDates)
- if (auto EC = sys::fs::setLastAccessAndModificationTime(
- FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
- return createFileError(Filename, EC);
-
- sys::fs::file_status OStat;
- if (std::error_code EC = sys::fs::status(FD, OStat))
- return createFileError(Filename, EC);
- if (OStat.type() == sys::fs::file_type::regular_file) {
-#ifndef _WIN32
- // Keep ownership if llvm-objcopy is called under root.
- if (Config.InputFilename == Config.OutputFilename && OStat.getUser() == 0)
- sys::fs::changeFileOwnership(FD, Stat.getUser(), Stat.getGroup());
-#endif
-
- sys::fs::perms Perm = Stat.permissions();
- if (Config.InputFilename != Config.OutputFilename)
- Perm = static_cast<sys::fs::perms>(Perm & ~sys::fs::getUmask() & ~06000);
-#ifdef _WIN32
- if (auto EC = sys::fs::setPermissions(Filename, Perm))
-#else
- if (auto EC = sys::fs::setPermissions(FD, Perm))
-#endif
- return createFileError(Filename, EC);
- }
-
- if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
- return createFileError(Filename, EC);
-
- return Error::success();
-}
-
/// The function executeObjcopy does the higher level dispatch based on the type
/// of input (raw binary, archive or single object file) and takes care of the
/// format-agnostic modifications, i.e. preserving dates.
static Error executeObjcopy(ConfigManager &ConfigMgr) {
CommonConfig &Config = ConfigMgr.Common;
- sys::fs::file_status Stat;
- if (Config.InputFilename != "-") {
- if (auto EC = sys::fs::status(Config.InputFilename, Stat))
- return createFileError(Config.InputFilename, EC);
- } else {
- Stat.permissions(static_cast<sys::fs::perms>(0777));
- }
+ Expected<FilePermissionsApplier> PermsCarrier =
+ FilePermissionsApplier::create(Config.InputFilename);
+ if (!PermsCarrier)
+ return PermsCarrier.takeError();
std::function<Error(raw_ostream & OutFile)> ObjcopyFunc;
@@ -259,14 +210,14 @@ static Error executeObjcopy(ConfigManager &ConfigMgr) {
}
}
- if (Error E = restoreStatOnFile(Config.OutputFilename, Stat, ConfigMgr))
+ if (Error E =
+ PermsCarrier->apply(Config.OutputFilename, Config.PreserveDates))
return E;
- if (!Config.SplitDWO.empty()) {
- Stat.permissions(static_cast<sys::fs::perms>(0666));
- if (Error E = restoreStatOnFile(Config.SplitDWO, Stat, ConfigMgr))
+ if (!Config.SplitDWO.empty())
+ if (Error E = PermsCarrier->apply(Config.SplitDWO, Config.PreserveDates,
+ static_cast<sys::fs::perms>(0666)))
return E;
- }
return Error::success();
}