Files
linuxdeploy/src/core/appdir.cpp
T

907 lines
41 KiB
C++
Raw Normal View History

// system headers
#include <set>
#include <string>
#include <vector>
2018-05-30 19:21:08 +02:00
// library headers
#include <boost/filesystem.hpp>
2018-06-25 17:37:21 +02:00
#include <CImg.h>
2018-05-30 19:21:08 +02:00
#include <fnmatch.h>
#include <subprocess.hpp>
2018-05-30 19:21:08 +02:00
// local headers
#include "linuxdeploy/core/appdir.h"
#include "linuxdeploy/core/elf.h"
#include "linuxdeploy/core/log.h"
2018-12-22 23:09:04 +01:00
#include "linuxdeploy/desktopfile/desktopfileentry.h"
2018-08-03 00:55:48 +02:00
#include "linuxdeploy/util/util.h"
2018-08-30 22:13:01 +02:00
#include "copyright.h"
// auto-generated headers
2018-05-30 19:21:08 +02:00
#include "excludelist.h"
using namespace linuxdeploy::core;
2018-12-22 23:09:04 +01:00
using namespace linuxdeploy::desktopfile;
2018-05-30 19:21:08 +02:00
using namespace linuxdeploy::core::log;
2018-06-25 17:37:21 +02:00
using namespace cimg_library;
2018-05-30 19:21:08 +02:00
namespace bf = boost::filesystem;
namespace linuxdeploy {
namespace core {
namespace appdir {
class AppDir::PrivateData {
public:
bf::path appDirPath;
// store deferred operations
// these can be executed by calling excuteDeferredOperations
2018-05-30 19:21:08 +02:00
std::map<bf::path, bf::path> copyOperations;
std::set<bf::path> stripOperations;
2018-06-01 15:02:05 +02:00
std::map<bf::path, std::string> setElfRPathOperations;
2018-05-30 19:21:08 +02:00
2018-06-01 15:27:08 +02:00
// stores all files that have been visited by the deploy functions, e.g., when they're blacklisted,
// have been added to the deferred operations already, etc.
// lookups in a single container are a lot faster than having to look up in several ones, therefore
// the little amount of additional memory is worth it, considering the improved performance
std::set<bf::path> visitedFiles;
// used to automatically rename resources to improve the UX, e.g. icons
std::string appName;
// platform dependent implementation of copyright files deployment
2018-08-30 22:13:01 +02:00
std::shared_ptr<copyright::ICopyrightFilesManager> copyrightFilesManager;
// decides whether copyright files deployment is performed
bool disableCopyrightFilesDeployment = false;
2018-05-30 19:21:08 +02:00
public:
2018-08-30 22:13:01 +02:00
PrivateData() : copyOperations(), stripOperations(), setElfRPathOperations(), visitedFiles(), appDirPath(), appName() {
copyrightFilesManager = copyright::ICopyrightFilesManager::getInstance();
};
2018-05-30 19:21:08 +02:00
public:
2018-09-03 23:30:53 +02:00
// calculate library directory name for given ELF file, taking system architecture into account
static std::string getLibraryDirName(const bf::path& path) {
const auto systemElfClass = elf::ElfFile::getSystemElfClass();
const auto elfClass = elf::ElfFile(path).getElfClass();
std::string libDirName = "lib";
if (systemElfClass != elfClass) {
if (elfClass == ELFCLASS32)
libDirName += "32";
else
libDirName += "64";
}
return libDirName;
}
2018-05-30 19:21:08 +02:00
// actually copy file
// mimics cp command behavior
2019-08-14 01:08:20 +02:00
static bool copyFile(const bf::path& from, bf::path to, bool overwrite = false) {
2018-05-30 19:21:08 +02:00
ldLog() << "Copying file" << from << "to" << to << std::endl;
try {
if (!to.parent_path().empty() && !bf::is_directory(to.parent_path()) && !bf::create_directories(to.parent_path())) {
ldLog() << LD_ERROR << "Failed to create parent directory" << to.parent_path() << "for path" << to << std::endl;
return false;
}
if (*(to.string().end() - 1) == '/' || bf::is_directory(to))
to /= from.filename();
if (!overwrite && bf::exists(to)) {
ldLog() << LD_DEBUG << "File exists, skipping:" << to << std::endl;
2018-06-11 23:43:21 +02:00
return true;
}
2018-06-11 23:43:21 +02:00
bf::copy_file(from, to, bf::copy_option::overwrite_if_exists);
2018-05-30 19:21:08 +02:00
} catch (const bf::filesystem_error& e) {
2018-06-11 23:52:29 +02:00
ldLog() << LD_ERROR << "Failed to copy file" << from << "to" << to << LD_NO_SPACE << ":" << e.what() << std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
return true;
}
// create symlink
bool symlinkFile(const bf::path& target, bf::path symlink, const bool useRelativePath = true) {
2018-05-30 19:21:08 +02:00
ldLog() << "Creating symlink for file" << target << "in/as" << symlink << std::endl;
/*try {
if (!symlink.parent_path().empty() && !bf::is_directory(symlink.parent_path()) && !bf::create_directories(symlink.parent_path())) {
ldLog() << LD_ERROR << "Failed to create parent directory" << symlink.parent_path() << "for path" << symlink << std::endl;
return false;
}
if (*(symlink.string().end() - 1) == '/' || bf::is_directory(symlink))
symlink /= target.filename();
if (bf::exists(symlink) || bf::symbolic_link_exists(symlink))
bf::remove(symlink);
if (relativeDirectory != "") {
// TODO
}
bf::create_symlink(target, symlink);
} catch (const bf::filesystem_error& e) {
return false;
}*/
if (!useRelativePath) {
ldLog() << LD_ERROR << "Not implemented" << std::endl;
return false;
}
// cannot use ln's --relative option any more since we want to support old distros as well
// (looking at you, CentOS 6!)
auto symlinkBase = symlink;
if (!bf::is_directory(symlinkBase))
symlinkBase = symlinkBase.parent_path();
auto relativeTargetPath = bf::relative(target, symlinkBase);
subprocess::Popen proc({"ln", "-f", "-s", relativeTargetPath.c_str(), symlink.c_str()},
2018-05-30 19:21:08 +02:00
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE)
);
auto outputs = util::subprocess::check_output_error(proc);
2018-05-30 19:21:08 +02:00
if (proc.retcode() != 0) {
ldLog() << LD_ERROR << "ln subprocess failed:" << std::endl
<< outputs.first << std::endl << outputs.second << std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
return true;
}
2018-06-01 15:27:08 +02:00
bool hasBeenVisitedAlready(const bf::path& path) {
return visitedFiles.find(path) != visitedFiles.end();
2018-05-30 19:21:08 +02:00
}
// execute deferred copy operations registered with the deploy* functions
bool executeDeferredOperations() {
bool success = true;
while (!copyOperations.empty()) {
const auto& pair = *(copyOperations.begin());
const auto& from = pair.first;
const auto& to = pair.second;
2018-06-11 23:49:50 +02:00
if (!copyFile(from, to))
2018-05-30 19:21:08 +02:00
success = false;
copyOperations.erase(copyOperations.begin());
}
if (!success)
return false;
2018-05-30 19:21:08 +02:00
2018-07-12 21:08:50 +02:00
if (getenv("NO_STRIP") != nullptr) {
ldLog() << LD_WARNING << "$NO_STRIP environment variable detected, not stripping binaries" << std::endl;
stripOperations.clear();
} else {
2018-08-21 01:18:00 +02:00
const auto stripPath = getStripPath();
2018-07-12 21:08:50 +02:00
while (!stripOperations.empty()) {
const auto& filePath = *(stripOperations.begin());
2018-05-30 19:21:08 +02:00
2018-07-12 21:08:50 +02:00
if (util::stringStartsWith(elf::ElfFile(filePath).getRPath(), "$")) {
ldLog() << LD_WARNING << "Not calling strip on binary" << filePath << LD_NO_SPACE
<< ": rpath starts with $" << std::endl;
} else {
ldLog() << "Calling strip on library" << filePath << std::endl;
2018-07-12 21:08:50 +02:00
std::map<std::string, std::string> env;
env.insert(std::make_pair(std::string("LC_ALL"), std::string("C")));
2018-07-12 21:08:50 +02:00
subprocess::Popen proc(
2018-08-21 01:18:00 +02:00
{stripPath.c_str(), filePath.c_str()},
2018-07-12 21:08:50 +02:00
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE),
subprocess::environment(env)
);
std::string err = util::subprocess::check_output_error(proc).second;
2018-07-12 21:08:50 +02:00
if (proc.retcode() != 0 &&
!util::stringContains(err, "Not enough room for program headers")) {
ldLog() << LD_ERROR << "Strip call failed:" << err << std::endl;
success = false;
}
2018-06-28 14:56:54 +02:00
}
2018-07-12 21:08:50 +02:00
stripOperations.erase(stripOperations.begin());
}
2018-05-30 19:21:08 +02:00
}
if (!success)
return false;
while (!setElfRPathOperations.empty()) {
const auto& currentEntry = *(setElfRPathOperations.begin());
const auto& filePath = currentEntry.first;
const auto& rpath = currentEntry.second;
2019-01-29 01:00:36 +01:00
// no need to set rpath in debug symbols files
// also, patchelf crashes on such symbols
if (isInDebugSymbolsLocation(filePath)) {
ldLog() << LD_WARNING << "Not setting rpath in debug symbols file:" << filePath << std::endl;
} else {
ldLog() << "Setting rpath in ELF file" << filePath << "to" << rpath << std::endl;
if (!elf::ElfFile(filePath).setRPath(rpath)) {
ldLog() << LD_ERROR << "Failed to set rpath in ELF file:" << filePath << std::endl;
success = false;
}
}
setElfRPathOperations.erase(setElfRPathOperations.begin());
}
return true;
2018-05-30 19:21:08 +02:00
}
2018-06-26 03:15:01 +02:00
// search for copyright file for file and deploy it to AppDir
bool deployCopyrightFiles(const bf::path& from, const std::string& logPrefix = "") {
if (disableCopyrightFilesDeployment)
return true;
2018-08-30 22:13:01 +02:00
if (copyrightFilesManager == nullptr)
return false;
auto copyrightFiles = copyrightFilesManager->getCopyrightFilesForPath(from);
2018-06-26 03:15:01 +02:00
if (copyrightFiles.empty())
return false;
2019-01-29 01:00:36 +01:00
ldLog() << logPrefix << LD_NO_SPACE << "Deploying copyright files for file" << from << std::endl;
2018-06-26 03:15:01 +02:00
for (const auto& file : copyrightFiles) {
2018-06-26 04:13:07 +02:00
std::string targetDir = file.string();
targetDir.erase(0, 1);
deployFile(file, appDirPath / targetDir);
2018-06-26 03:15:01 +02:00
}
return true;
}
2018-05-30 19:21:08 +02:00
// register copy operation that will be executed later
// by compiling a list of files to copy instead of just copying everything, one can ensure that
// the files are touched once only
2019-02-06 23:23:48 +01:00
// returns the full path of the deployment destination (useful if to is a directory
bf::path deployFile(const bf::path& from, bf::path to, bool verbose = false) {
2018-05-30 19:21:08 +02:00
// not sure whether this is 100% bullet proof, but it simulates the cp command behavior
if (to.string().back() == '/' || bf::is_directory(to)) {
to /= from.filename();
}
2019-02-06 23:23:48 +01:00
if (verbose)
ldLog() << "Deploying file" << from << "to" << to << std::endl;
2018-05-30 19:21:08 +02:00
copyOperations[from] = to;
2018-06-01 15:27:08 +02:00
// mark file as visited
visitedFiles.insert(from);
2019-02-06 23:23:48 +01:00
return to;
2018-05-30 19:21:08 +02:00
}
2018-06-08 14:49:30 +02:00
std::string getLogPrefix(int recursionLevel) {
std::string logPrefix;
for (int i = 0; i < recursionLevel; i++)
logPrefix += " ";
return logPrefix;
}
bool deployElfDependencies(const bf::path& path, int recursionLevel = 0) {
auto logPrefix = getLogPrefix(recursionLevel);
ldLog() << logPrefix << LD_NO_SPACE << "Deploying dependencies for ELF file" << path << std::endl;
2018-08-11 15:30:06 +02:00
try {
for (const auto &dependencyPath : elf::ElfFile(path).traceDynamicDependencies()) {
if (!deployLibrary(dependencyPath, recursionLevel + 1))
return false;
}
} catch (const elf::DependencyNotFoundError& e) {
ldLog() << LD_ERROR << e.what() << std::endl;
return false;
2018-05-30 19:21:08 +02:00
}
2018-08-11 15:30:06 +02:00
2018-05-30 19:21:08 +02:00
return true;
}
2018-06-28 02:36:46 +02:00
static std::string getStripPath() {
2018-08-21 01:18:00 +02:00
// by default, try to use a strip next to the linuxdeploy binary
// if that isn't available, fall back to searching for strip in the PATH
std::string stripPath = "strip";
2018-06-28 02:36:46 +02:00
2018-08-21 01:18:00 +02:00
auto binDirPath = bf::path(util::getOwnExecutablePath()).parent_path();
auto localStripPath = binDirPath / "strip";
2018-06-28 02:36:46 +02:00
if (bf::exists(localStripPath))
2018-08-21 01:18:00 +02:00
stripPath = localStripPath.string();
2018-06-28 02:36:46 +02:00
2018-08-21 01:18:00 +02:00
ldLog() << LD_DEBUG << "Using strip:" << stripPath << std::endl;
2018-06-28 02:36:46 +02:00
2018-08-21 01:18:00 +02:00
return stripPath;
2018-06-28 02:36:46 +02:00
}
2019-02-06 23:23:48 +01:00
bool deployLibrary(const bf::path& path, int recursionLevel = 0, bool forceDeploy = false, const bf::path& destination = bf::path()) {
2018-06-08 14:49:30 +02:00
auto logPrefix = getLogPrefix(recursionLevel);
2018-07-01 01:06:31 +02:00
if (!forceDeploy && hasBeenVisitedAlready(path)) {
2018-06-08 14:49:30 +02:00
ldLog() << LD_DEBUG << logPrefix << LD_NO_SPACE << "File has been visited already:" << path << std::endl;
2018-05-30 19:21:08 +02:00
return true;
}
2019-02-23 18:01:47 +01:00
if (!bf::exists(path)) {
ldLog() << LD_ERROR << logPrefix << LD_NO_SPACE << "Cannot deploy non-existing library file:" << path << std::endl;
return false;
}
2018-06-08 14:49:30 +02:00
static auto isInExcludelist = [&logPrefix](const bf::path& fileName) {
2018-05-30 19:21:08 +02:00
for (const auto& excludePattern : generatedExcludelist) {
// simple string match is faster than using fnmatch
if (excludePattern == fileName)
return true;
auto fnmatchResult = fnmatch(excludePattern.c_str(), fileName.string().c_str(), FNM_PATHNAME);
switch (fnmatchResult) {
case 0:
return true;
case FNM_NOMATCH:
break;
default:
2018-06-08 14:49:30 +02:00
ldLog() << LD_ERROR << logPrefix << LD_NO_SPACE << "fnmatch() reported error:" << fnmatchResult << std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
}
return false;
};
2018-07-01 02:28:44 +02:00
if (!forceDeploy && isInExcludelist(path.filename())) {
2018-06-08 14:49:30 +02:00
ldLog() << logPrefix << LD_NO_SPACE << "Skipping deployment of blacklisted library" << path << std::endl;
2018-06-01 15:27:08 +02:00
// mark file as visited
visitedFiles.insert(path);
2018-05-30 19:21:08 +02:00
return true;
}
// note for self: make sure to have a trailing slash in libraryDir, otherwise copyFile won't
// create a directory
2018-09-04 14:33:25 +02:00
bf::path libraryDir = appDirPath / "usr" / (getLibraryDirName(path) + "/");
2018-06-08 14:49:30 +02:00
ldLog() << logPrefix << LD_NO_SPACE << "Deploying shared library" << path;
2018-06-08 14:20:59 +02:00
if (!destination.empty())
ldLog() << " (destination:" << destination << LD_NO_SPACE << ")";
ldLog() << std::endl;
2019-02-06 23:23:48 +01:00
auto actualDestination = destination.empty() ? libraryDir : destination;
2018-05-30 19:21:08 +02:00
// not sure whether this is 100% bullet proof, but it simulates the cp command behavior
2019-02-06 23:23:48 +01:00
if (actualDestination.string().back() == '/' || bf::is_directory(actualDestination)) {
actualDestination /= path.filename();
}
2019-02-06 23:23:48 +01:00
// in case destinationPath is a directory, deployFile will give us the deployed file's path
actualDestination = deployFile(path, actualDestination);
deployCopyrightFiles(path, logPrefix);
std::string rpath = "$ORIGIN";
if (!destination.empty()) {
std::string rpathDestination = destination.string();
if (destination.string().back() == '/') {
rpathDestination = destination.string();
while (rpathDestination.back() == '/')
rpathDestination.erase(rpathDestination.end() - 1, rpathDestination.end());
} else {
rpathDestination = destination.parent_path().string();
}
2018-06-08 11:36:18 +02:00
auto relPath = bf::relative(bf::absolute(libraryDir), bf::absolute(rpathDestination));
2018-06-08 11:36:18 +02:00
rpath = "$ORIGIN/" + relPath.string() + ":$ORIGIN";
}
2019-01-29 01:00:36 +01:00
// no need to set rpath in debug symbols files
// also, patchelf crashes on such symbols
2019-02-06 23:23:48 +01:00
if (!isInDebugSymbolsLocation(actualDestination)) {
setElfRPathOperations[actualDestination] = rpath;
2019-01-29 01:00:36 +01:00
}
2019-02-06 23:23:48 +01:00
stripOperations.insert(actualDestination);
2018-05-30 19:21:08 +02:00
2018-06-08 14:49:30 +02:00
if (!deployElfDependencies(path, recursionLevel))
2018-05-30 19:21:08 +02:00
return false;
return true;
}
bool deployExecutable(const bf::path& path, const boost::filesystem::path& destination) {
2018-06-01 15:27:08 +02:00
if (hasBeenVisitedAlready(path)) {
ldLog() << LD_DEBUG << "File has been visited already:" << path << std::endl;
2018-05-30 19:21:08 +02:00
return true;
}
ldLog() << "Deploying executable" << path << std::endl;
// FIXME: make executables executable
auto destinationPath = destination.empty() ? appDirPath / "usr/bin/" : destination;
2018-05-30 19:21:08 +02:00
2018-06-14 21:21:32 +02:00
deployFile(path, destinationPath);
2018-06-26 03:15:01 +02:00
deployCopyrightFiles(path);
2018-09-03 23:30:53 +02:00
std::string rpath = "$ORIGIN/../" + getLibraryDirName(path);
if (!destination.empty()) {
std::string rpathDestination = destination.string();
if (destination.string().back() == '/') {
rpathDestination = destination.string();
while (rpathDestination.back() == '/')
rpathDestination.erase(rpathDestination.end() - 1, rpathDestination.end());
} else {
rpathDestination = destination.parent_path().string();
}
2018-09-03 23:33:26 +02:00
auto relPath = bf::relative(bf::absolute(appDirPath) / "usr" / getLibraryDirName(path), bf::absolute(rpathDestination));
rpath = "$ORIGIN/" + relPath.string();
}
2018-06-14 21:21:32 +02:00
setElfRPathOperations[destinationPath / path.filename()] = rpath;
stripOperations.insert(destinationPath / path.filename());
2018-05-30 19:21:08 +02:00
2018-06-15 15:02:24 +02:00
if (!deployElfDependencies(path))
2018-05-30 19:21:08 +02:00
return false;
return true;
}
2018-11-15 20:53:16 +01:00
bool deployDesktopFile(const DesktopFile& desktopFile) {
2018-06-01 15:27:08 +02:00
if (hasBeenVisitedAlready(desktopFile.path())) {
ldLog() << LD_DEBUG << "File has been visited already:" << desktopFile.path() << std::endl;
2018-05-30 19:21:08 +02:00
return true;
}
2018-06-18 03:12:54 +02:00
if (!desktopFile.validate()) {
2018-06-18 03:13:09 +02:00
ldLog() << LD_ERROR << "Failed to validate desktop file:" << desktopFile.path() << std::endl;
2018-05-30 19:21:08 +02:00
}
ldLog() << "Deploying desktop file" << desktopFile.path() << std::endl;
deployFile(desktopFile.path(), appDirPath / "usr/share/applications/");
return true;
}
bool deployIcon(const bf::path& path) {
2018-06-01 15:27:08 +02:00
if (hasBeenVisitedAlready(path)) {
ldLog() << LD_DEBUG << "File has been visited already:" << path << std::endl;
2018-05-30 19:21:08 +02:00
return true;
}
ldLog() << "Deploying icon" << path << std::endl;
2018-06-16 03:33:55 +02:00
std::string resolution;
2018-05-30 19:21:08 +02:00
// if file is a vector image, use "scalable" directory
2018-06-16 03:50:43 +02:00
if (util::strLower(path.filename().extension().string()) == ".svg") {
2018-05-30 19:21:08 +02:00
resolution = "scalable";
} else {
2018-06-16 03:33:55 +02:00
try {
2018-06-25 17:37:21 +02:00
CImg<unsigned char> image(path.c_str());
2018-06-16 03:33:55 +02:00
2018-06-25 17:37:21 +02:00
auto xRes = image.width();
auto yRes = image.height();
2018-06-16 03:33:55 +02:00
2018-06-25 17:37:21 +02:00
if (xRes != yRes) {
ldLog() << LD_WARNING << "x and y resolution of icon are not equal:" << path;
}
2018-06-16 03:33:55 +02:00
2018-06-25 17:37:21 +02:00
resolution = std::to_string(xRes) + "x" + std::to_string(yRes);
2018-06-16 03:33:55 +02:00
2018-06-25 17:37:21 +02:00
// otherwise, test resolution against "known good" values, and reject invalid ones
2019-05-24 16:19:13 +02:00
const auto knownResolutions = {8, 16, 20, 22, 24, 28, 32, 36, 42, 48, 64, 72, 96, 128, 160, 192, 256, 384, 480, 512};
2018-05-30 19:21:08 +02:00
2018-06-25 17:37:21 +02:00
// assume invalid
bool invalidXRes = true, invalidYRes = true;
2018-05-30 19:21:08 +02:00
2018-06-25 17:37:21 +02:00
for (const auto res : knownResolutions) {
if (xRes == res)
invalidXRes = false;
if (yRes == res)
invalidYRes = false;
}
2018-05-30 19:21:08 +02:00
2019-05-24 23:25:03 +02:00
auto printIconHint = [&knownResolutions]() {
std::stringstream ss;
for (const auto res : knownResolutions) {
ss << res << "x" << res;
if (res != *(knownResolutions.end() - 1))
ss << ", ";
}
ldLog() << LD_ERROR << "Valid resolutions for icons are:" << ss.str() << std::endl;
};
2018-06-25 17:37:21 +02:00
if (invalidXRes) {
2019-05-24 22:39:18 +02:00
ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes << std::endl;
2019-05-24 23:25:03 +02:00
printIconHint();
2018-06-25 17:37:21 +02:00
return false;
}
2018-05-30 19:21:08 +02:00
2018-06-25 17:37:21 +02:00
if (invalidYRes) {
2019-05-24 22:39:18 +02:00
ldLog() << LD_ERROR << "Icon" << path << "has invalid y resolution:" << yRes << std::endl;
2019-05-24 23:25:03 +02:00
printIconHint();
2018-06-25 17:37:21 +02:00
return false;
}
} catch (const CImgException& e) {
ldLog() << LD_ERROR << "CImg error: " << e.what() << std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
}
// rename files like <appname>_*.ext to <appname>.ext
auto filename = path.filename().string();
if (!appName.empty() && util::stringStartsWith(path.string(), appName)) {
auto newFilename = appName + path.extension().string();
if (newFilename != filename) {
ldLog() << LD_WARNING << "Renaming icon" << path << "to" << newFilename << std::endl;
filename = newFilename;
}
}
deployFile(path, appDirPath / "usr/share/icons/hicolor" / resolution / "apps" / filename);
2018-06-26 03:15:01 +02:00
deployCopyrightFiles(path);
2018-05-30 19:21:08 +02:00
return true;
}
2019-01-29 01:00:36 +01:00
2019-07-25 17:43:45 +02:00
static bool isInDebugSymbolsLocation(const bf::path& path) {
2019-01-29 01:00:36 +01:00
// TODO: check if there's more potential locations for debug symbol files
for (const std::string& dbgSymbolsPrefix : {".debug/"}) {
if (path.string().substr(0, dbgSymbolsPrefix.size()) == dbgSymbolsPrefix)
return true;
}
return false;
}
2018-05-30 19:21:08 +02:00
};
AppDir::AppDir(const bf::path& path) {
2019-01-29 00:02:49 +01:00
d = std::make_shared<PrivateData>();
2018-05-30 19:21:08 +02:00
d->appDirPath = path;
}
AppDir::AppDir(const std::string& path) : AppDir(bf::path(path)) {}
2019-07-27 16:37:16 +02:00
bool AppDir::createBasicStructure() const {
2018-05-30 19:21:08 +02:00
std::vector<std::string> dirPaths = {
"usr/bin/",
"usr/lib/",
"usr/share/applications/",
"usr/share/icons/hicolor/",
};
for (const std::string& resolution : {"16x16", "32x32", "64x64", "128x128", "256x256", "scalable"}) {
auto iconPath = "usr/share/icons/hicolor/" + resolution + "/apps/";
dirPaths.push_back(iconPath);
}
for (const auto& dirPath : dirPaths) {
auto fullDirPath = d->appDirPath / dirPath;
ldLog() << "Creating directory" << fullDirPath << std::endl;
// skip directory if it exists
if (bf::is_directory(fullDirPath))
continue;
try {
bf::create_directories(fullDirPath);
} catch (const bf::filesystem_error&) {
ldLog() << LD_ERROR << "Failed to create directory" << fullDirPath;
return false;
}
}
return true;
}
bool AppDir::deployLibrary(const bf::path& path, const bf::path& destination) {
2018-07-01 01:06:31 +02:00
return d->deployLibrary(path, 0, false, destination);
2018-06-30 23:27:02 +02:00
}
bool AppDir::forceDeployLibrary(const bf::path& path, const bf::path& destination) {
2018-07-01 01:06:31 +02:00
return d->deployLibrary(path, 0, true, destination);
2018-05-30 19:21:08 +02:00
}
bool AppDir::deployExecutable(const bf::path& path, const boost::filesystem::path& destination) {
return d->deployExecutable(path, destination);
2018-05-30 19:21:08 +02:00
}
2018-11-15 20:53:16 +01:00
bool AppDir::deployDesktopFile(const DesktopFile& desktopFile) {
2018-05-30 19:21:08 +02:00
return d->deployDesktopFile(desktopFile);
}
bool AppDir::deployIcon(const bf::path& path) {
return d->deployIcon(path);
}
bool AppDir::executeDeferredOperations() {
return d->executeDeferredOperations();
}
2019-07-27 16:37:16 +02:00
boost::filesystem::path AppDir::path() const {
2018-05-30 19:21:08 +02:00
return d->appDirPath;
}
static std::vector<bf::path> listFilesInDirectory(const bf::path& path, const bool recursive = true) {
std::vector<bf::path> foundPaths;
2018-06-16 00:49:18 +02:00
// directory_iterators throw exceptions if the directory doesn't exist
2018-06-23 23:35:33 +02:00
if (!bf::is_directory(path)) {
ldLog() << LD_DEBUG << "No such directory:" << path << std::endl;
2018-06-16 00:49:18 +02:00
return {};
2018-06-23 23:35:33 +02:00
}
2018-06-16 00:49:18 +02:00
2018-05-30 19:21:08 +02:00
if (recursive) {
for (bf::recursive_directory_iterator i(path); i != bf::recursive_directory_iterator(); ++i) {
if (bf::is_regular_file(*i)) {
foundPaths.push_back((*i).path());
2018-05-30 19:21:08 +02:00
}
}
} else {
for (bf::directory_iterator i(path); i != bf::directory_iterator(); ++i) {
if (bf::is_regular_file(*i)) {
foundPaths.push_back((*i).path());
2018-05-30 19:21:08 +02:00
}
}
}
return foundPaths;
}
2019-07-27 16:37:16 +02:00
std::vector<bf::path> AppDir::deployedIconPaths() const {
auto icons = listFilesInDirectory(path() / "usr/share/icons/");
auto pixmaps = listFilesInDirectory(path() / "usr/share/pixmaps/", false);
icons.reserve(pixmaps.size());
std::copy(pixmaps.begin(), pixmaps.end(), std::back_inserter(icons));
return icons;
2018-05-30 19:21:08 +02:00
}
2019-07-27 16:37:16 +02:00
std::vector<bf::path> AppDir::deployedExecutablePaths() const {
2018-06-16 02:56:46 +02:00
return listFilesInDirectory(path() / "usr/bin/", false);
2018-05-30 19:21:08 +02:00
}
2019-07-27 16:37:16 +02:00
std::vector<DesktopFile> AppDir::deployedDesktopFiles() const {
2018-11-15 20:53:16 +01:00
std::vector<DesktopFile> desktopFiles;
2018-05-30 19:21:08 +02:00
auto paths = listFilesInDirectory(path() / "usr/share/applications/", false);
paths.erase(std::remove_if(paths.begin(), paths.end(), [](const bf::path& path) {
return path.extension() != ".desktop";
}), paths.end());
for (const auto& path : paths) {
2018-12-22 23:09:04 +01:00
desktopFiles.emplace_back(path.string());
2018-05-30 19:21:08 +02:00
}
return desktopFiles;
}
bool AppDir::setUpAppDirRoot(const DesktopFile& desktopFile, boost::filesystem::path customAppRunPath) {
2018-05-30 19:21:08 +02:00
ldLog() << "Deploying desktop file to AppDir root:" << desktopFile.path() << std::endl;
// copy desktop file to root directory
if (!d->symlinkFile(desktopFile.path(), path())) {
ldLog() << LD_ERROR << "Failed to create link to desktop file in AppDir root:" << desktopFile.path() << std::endl;
return false;
}
// look for suitable icon
2018-11-15 20:53:16 +01:00
DesktopFileEntry iconEntry;
2018-05-30 19:21:08 +02:00
2018-11-15 20:53:16 +01:00
if (!desktopFile.getEntry("Desktop Entry", "Icon", iconEntry)) {
2018-05-30 19:21:08 +02:00
ldLog() << LD_ERROR << "Icon entry missing in desktop file:" << desktopFile.path() << std::endl;
return false;
}
2018-06-18 04:19:22 +02:00
bool iconDeployed = false;
2018-05-30 19:21:08 +02:00
const auto foundIconPaths = deployedIconPaths();
if (foundIconPaths.empty()) {
2018-11-15 20:53:16 +01:00
ldLog() << LD_ERROR << "Could not find icon executable for Icon entry:" << iconEntry.value() << std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
for (const auto& iconPath : foundIconPaths) {
ldLog() << LD_DEBUG << "Icon found:" << iconPath << std::endl;
2018-11-15 20:53:16 +01:00
const bool matchesFilenameWithExtension = iconPath.filename() == iconEntry.value();
2018-07-31 15:21:38 +02:00
2018-11-15 20:53:16 +01:00
if (iconPath.stem() == iconEntry.value() || matchesFilenameWithExtension) {
2018-07-31 15:21:38 +02:00
if (matchesFilenameWithExtension) {
ldLog() << LD_WARNING << "Icon= entry filename contains extension" << std::endl;
}
2018-05-30 19:21:08 +02:00
ldLog() << "Deploying icon to AppDir root:" << iconPath << std::endl;
if (!d->symlinkFile(iconPath, path())) {
ldLog() << LD_ERROR << "Failed to create symlink for icon in AppDir root:" << iconPath << std::endl;
return false;
}
2018-06-18 04:19:22 +02:00
iconDeployed = true;
break;
2018-05-30 19:21:08 +02:00
}
}
2018-06-18 04:19:22 +02:00
if (!iconDeployed) {
2018-11-15 20:53:16 +01:00
ldLog() << LD_ERROR << "Could not find suitable icon for Icon entry:" << iconEntry.value() << std::endl;
2018-06-18 04:19:22 +02:00
return false;
}
2018-06-01 02:55:37 +02:00
if (!customAppRunPath.empty()) {
// copy custom AppRun executable
// FIXME: make sure this file is executable
ldLog() << "Deploying custom AppRun:" << customAppRunPath;
2018-05-30 19:21:08 +02:00
2018-06-01 02:55:37 +02:00
if (!d->copyFile(customAppRunPath, path() / "AppRun"))
return false;
} else {
// check if there is a custom AppRun already
// in that case, skip deployment of symlink
if (bf::exists(path() / "AppRun")) {
2019-07-25 17:47:34 +02:00
ldLog() << LD_WARNING << "Existing AppRun detected, skipping deployment of symlink" << std::endl;
2018-06-01 02:55:37 +02:00
} else {
// look for suitable binary to create AppRun symlink
2018-11-15 20:53:16 +01:00
DesktopFileEntry executableEntry;
2018-05-30 19:21:08 +02:00
2018-11-15 20:53:16 +01:00
if (!desktopFile.getEntry("Desktop Entry", "Exec", executableEntry)) {
2018-06-01 02:55:37 +02:00
ldLog() << LD_ERROR << "Exec entry missing in desktop file:" << desktopFile.path()
<< std::endl;
2018-05-30 19:21:08 +02:00
return false;
}
2018-06-01 02:55:37 +02:00
2018-11-15 20:53:16 +01:00
auto executableName = util::split(executableEntry.value())[0];
2018-06-20 16:24:27 +02:00
2018-06-01 02:55:37 +02:00
const auto foundExecutablePaths = deployedExecutablePaths();
if (foundExecutablePaths.empty()) {
2018-06-20 16:24:13 +02:00
ldLog() << LD_ERROR << "Could not find suitable executable for Exec entry:" << executableName
2018-06-01 02:55:37 +02:00
<< std::endl;
return false;
}
2018-06-19 20:55:22 +02:00
bool deployedExecutable = false;
2018-06-01 02:55:37 +02:00
for (const auto& executablePath : foundExecutablePaths) {
ldLog() << LD_DEBUG << "Executable found:" << executablePath << std::endl;
2018-06-19 20:55:22 +02:00
if (executablePath.filename() == executableName) {
2018-06-01 02:55:37 +02:00
ldLog() << "Deploying AppRun symlink for executable in AppDir root:" << executablePath
<< std::endl;
if (!d->symlinkFile(executablePath, path() / "AppRun")) {
ldLog() << LD_ERROR
<< "Failed to create AppRun symlink for executable in AppDir root:"
<< executablePath << std::endl;
return false;
}
2018-06-19 20:55:22 +02:00
deployedExecutable = true;
break;
2018-06-01 02:55:37 +02:00
}
}
2018-06-19 20:55:22 +02:00
if (!deployedExecutable) {
ldLog() << LD_ERROR << "Could not deploy symlink for executable: could not find suitable executable for Exec entry:" << executableName << std::endl;
return false;
}
2018-05-30 19:21:08 +02:00
}
}
return true;
}
2019-02-06 23:23:48 +01:00
bf::path AppDir::deployFile(const boost::filesystem::path& from, const boost::filesystem::path& to) {
return d->deployFile(from, to, true);
2018-06-11 23:04:37 +02:00
}
bool AppDir::copyFile(const bf::path& from, const bf::path& to, bool overwrite) const {
return d->copyFile(from, to, overwrite);
}
2019-07-27 16:37:16 +02:00
bool AppDir::createRelativeSymlink(const bf::path& target, const bf::path& symlink) const {
2018-11-19 21:50:29 +01:00
return d->symlinkFile(target, symlink, true);
}
2019-07-27 16:37:16 +02:00
std::vector<bf::path> AppDir::listExecutables() const {
std::vector<bf::path> executables;
for (const auto& file : listFilesInDirectory(path() / "usr" / "bin", false)) {
2018-06-20 21:34:26 +02:00
// make sure it's an ELF file
try {
elf::ElfFile elfFile(file);
} catch (const elf::ElfFileParseError&) {
// FIXME: remove this workaround once the MIME check below works as intended
continue;
}
2018-08-20 23:54:35 +02:00
executables.push_back(file);
}
return executables;
}
2019-07-27 16:37:16 +02:00
std::vector<bf::path> AppDir::listSharedLibraries() const {
std::vector<bf::path> sharedLibraries;
for (const auto& file : listFilesInDirectory(path() / "usr" / "lib", true)) {
2019-01-29 01:00:36 +01:00
// exclude debug symbols
if (d->isInDebugSymbolsLocation(file))
continue;
2018-06-20 21:34:26 +02:00
// make sure it's an ELF file
try {
elf::ElfFile elfFile(file);
} catch (const elf::ElfFileParseError&) {
// FIXME: remove this workaround once the MIME check below works as intended
continue;
}
2018-08-20 23:54:35 +02:00
sharedLibraries.push_back(file);
}
return sharedLibraries;
}
2019-07-27 16:37:16 +02:00
bool AppDir::deployDependenciesForExistingFiles() const {
for (const auto& executable : listExecutables()) {
if (bf::is_symlink(executable))
continue;
if (!d->deployElfDependencies(executable))
return false;
2018-09-03 23:30:53 +02:00
std::string rpath = "$ORIGIN/../" + PrivateData::getLibraryDirName(executable);
2018-09-03 21:07:43 +02:00
d->setElfRPathOperations[executable] = rpath;
}
for (const auto& sharedLibrary : listSharedLibraries()) {
if (bf::is_symlink(sharedLibrary))
continue;
if (!d->deployElfDependencies(sharedLibrary))
return false;
d->setElfRPathOperations[sharedLibrary] = "$ORIGIN";
}
return true;
}
2018-11-19 21:49:56 +01:00
void AppDir::setDisableCopyrightFilesDeployment(bool disable) {
d->disableCopyrightFilesDeployment = disable;
}
2018-05-30 19:21:08 +02:00
}
}
}