-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathappdir.cpp
More file actions
868 lines (660 loc) · 39.2 KB
/
appdir.cpp
File metadata and controls
868 lines (660 loc) · 39.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// system headers
#include <set>
#include <string>
#include <vector>
// library headers
#include <boost/filesystem.hpp>
#include <CImg.h>
#include <fnmatch.h>
#include <subprocess.hpp>
// local headers
#include "linuxdeploy/core/appdir.h"
#include "linuxdeploy/core/elf.h"
#include "linuxdeploy/core/log.h"
#include "linuxdeploy/util/util.h"
#include "excludelist.h"
using namespace linuxdeploy::core;
using namespace linuxdeploy::core::log;
using namespace cimg_library;
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
std::map<bf::path, bf::path> copyOperations;
std::set<bf::path> stripOperations;
std::map<bf::path, std::string> setElfRPathOperations;
// 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;
public:
PrivateData() : copyOperations(), stripOperations(), setElfRPathOperations(), visitedFiles(), appDirPath(), appName() {};
public:
// actually copy file
// mimics cp command behavior
bool copyFile(const bf::path& from, bf::path to, bool overwrite = false) {
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;
return true;
}
bf::copy_file(from, to, bf::copy_option::overwrite_if_exists);
} catch (const bf::filesystem_error& e) {
ldLog() << LD_ERROR << "Failed to copy file" << from << "to" << to << LD_NO_SPACE << ":" << e.what() << std::endl;
return false;
}
return true;
}
// create symlink
bool symlinkFile(const bf::path& target, const bf::path& symlink, const bool useRelativePath = true) {
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;
}
subprocess::Popen proc({"ln", "-f", "-s", "--relative", target.c_str(), symlink.c_str()},
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE)
);
auto outputs = proc.communicate();
if (proc.retcode() != 0) {
ldLog() << LD_ERROR << "ln subprocess failed:" << std::endl
<< outputs.first.buf << std::endl << outputs.second.buf << std::endl;
return false;
}
return true;
}
bool hasBeenVisitedAlready(const bf::path& path) {
return visitedFiles.find(path) != visitedFiles.end();
}
// 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;
if (!copyFile(from, to))
success = false;
copyOperations.erase(copyOperations.begin());
}
if (!success)
return false;
if (getenv("NO_STRIP") != nullptr) {
ldLog() << LD_WARNING << "$NO_STRIP environment variable detected, not stripping binaries" << std::endl;
stripOperations.clear();
} else {
while (!stripOperations.empty()) {
const auto& filePath = *(stripOperations.begin());
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;
std::map<std::string, std::string> env;
env.insert(std::make_pair(std::string("LC_ALL"), std::string("C")));
subprocess::Popen proc(
{getStripPath().c_str(), filePath.c_str()},
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE),
subprocess::environment(env)
);
std::string err = proc.communicate().second.buf.data();
if (proc.retcode() != 0 &&
!util::stringContains(err, "Not enough room for program headers")) {
ldLog() << LD_ERROR << "Strip call failed:" << err << std::endl;
success = false;
}
}
stripOperations.erase(stripOperations.begin());
}
}
if (!success)
return false;
while (!setElfRPathOperations.empty()) {
const auto& currentEntry = *(setElfRPathOperations.begin());
const auto& filePath = currentEntry.first;
const auto& rpath = currentEntry.second;
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;
}
// search for copyright file related to given file
// this function utilizes distribution tools like dpkg-query to determine the paths of copyright
// files
std::vector<bf::path> searchForCopyrightFiles(const bf::path& from) {
auto check_command = [](const std::string& command) {
auto p = subprocess::Popen(
command,
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE)
);
return p.wait();
};
// cannot deploy copyright files for files in AppDir
if (!util::stringStartsWith(bf::absolute(from).string(), bf::absolute(appDirPath).string())) {
if (check_command("which dpkg-query") == 0) {
ldLog() << LD_DEBUG << "Using dpkg-query to search for copyright files" << std::endl;
auto call = [](const std::initializer_list<const char*>& args) {
auto proc = subprocess::Popen(
args,
subprocess::output(subprocess::PIPE),
subprocess::error(subprocess::PIPE)
);
auto output = proc.communicate();
return std::make_pair(proc.retcode(), output.first);
};
auto dpkgQueryPackages = call({"dpkg-query", "-S", from.c_str()});
if (dpkgQueryPackages.first != 0
|| dpkgQueryPackages.second.buf.empty()
|| dpkgQueryPackages.second.buf.front() == '\0') {
ldLog() << LD_WARNING << "Could not find copyright files for file" << from << "using dpkg-query" << std::endl;
return {};
}
auto packageName = util::split(util::splitLines(dpkgQueryPackages.second.buf.data())[0], ':')[0];
if (!packageName.empty()) {
auto copyrightFilePath = bf::path("/usr/share/doc") / packageName / "copyright";
if (bf::is_regular_file(copyrightFilePath)) {
return {copyrightFilePath};
}
} else {
ldLog() << LD_WARNING << "Could not find copyright files for file" << from << "using dpkg-query" << std::endl;
}
}
} else {
ldLog() << LD_DEBUG << "Cannot deploy copyright files for files in AppDir:" << from << std::endl;
}
ldLog() << LD_DEBUG << "Could not find suitable tool for copyright files deployment, skipping" << from << std::endl;
return {};
}
// search for copyright file for file and deploy it to AppDir
bool deployCopyrightFiles(const bf::path& from, const std::string& logPrefix = "") {
ldLog() << logPrefix << LD_NO_SPACE << "Deploying copyright files for file" << from << std::endl;
auto copyrightFiles = searchForCopyrightFiles(from);
if (copyrightFiles.empty())
return false;
for (const auto& file : copyrightFiles) {
std::string targetDir = file.string();
targetDir.erase(0, 1);
deployFile(file, appDirPath / targetDir);
}
return true;
}
// 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
void deployFile(const bf::path& from, bf::path to, bool verbose = false) {
if (verbose)
ldLog() << "Deploying file" << from << "to" << to << std::endl;
// 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();
}
copyOperations[from] = to;
// mark file as visited
visitedFiles.insert(from);
}
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;
for (const auto& dependencyPath : elf::ElfFile(path).traceDynamicDependencies()) {
if (!deployLibrary(dependencyPath, recursionLevel + 1))
return false;
}
return true;
}
static std::string getStripPath() {
// by default, try to use a patchelf next to the linuxdeploy binary
// if that isn't available, fall back to searching for patchelf in the PATH
std::string patchelfPath = "strip";
auto binDirPath = bf::path(util::getOwnExecutablePath());
auto localStripPath = binDirPath.parent_path() / "strip";
if (bf::exists(localStripPath))
patchelfPath = localStripPath.string();
ldLog() << LD_DEBUG << "Using strip:" << patchelfPath << std::endl;
return patchelfPath;
}
bool deployLibrary(const bf::path& path, int recursionLevel = 0, bool forceDeploy = false,const bf::path &destination = bf::path()) {
auto logPrefix = getLogPrefix(recursionLevel);
if (!forceDeploy && hasBeenVisitedAlready(path)) {
ldLog() << LD_DEBUG << logPrefix << LD_NO_SPACE << "File has been visited already:" << path << std::endl;
return true;
}
static auto isInExcludelist = [&logPrefix](const bf::path& fileName) {
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:
ldLog() << LD_ERROR << logPrefix << LD_NO_SPACE << "fnmatch() reported error:" << fnmatchResult << std::endl;
return false;
}
}
return false;
};
if (!forceDeploy && isInExcludelist(path.filename())) {
ldLog() << logPrefix << LD_NO_SPACE << "Skipping deployment of blacklisted library" << path << std::endl;
// mark file as visited
visitedFiles.insert(path);
return true;
}
ldLog() << logPrefix << LD_NO_SPACE << "Deploying shared library" << path;
if (!destination.empty())
ldLog() << " (destination:" << destination << LD_NO_SPACE << ")";
ldLog() << std::endl;
auto destinationPath = destination.empty() ? appDirPath / "usr/lib/" : destination;
// not sure whether this is 100% bullet proof, but it simulates the cp command behavior
if (destinationPath.string().back() == '/' || bf::is_directory(destinationPath)) {
destinationPath /= path.filename();
}
deployFile(path, destinationPath);
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();
}
auto relPath = bf::relative(bf::absolute(appDirPath) / "usr/lib", bf::absolute(rpathDestination));
rpath = "$ORIGIN/" + relPath.string() + ":$ORIGIN";
}
setElfRPathOperations[destinationPath] = rpath;
stripOperations.insert(destinationPath);
if (!deployElfDependencies(path, recursionLevel))
return false;
return true;
}
bool deployExecutable(const bf::path& path, const boost::filesystem::path& destination) {
if (hasBeenVisitedAlready(path)) {
ldLog() << LD_DEBUG << "File has been visited already:" << path << std::endl;
return true;
}
ldLog() << "Deploying executable" << path << std::endl;
// FIXME: make executables executable
auto destinationPath = destination.empty() ? appDirPath / "usr/bin/" : destination;
deployFile(path, destinationPath);
deployCopyrightFiles(path);
std::string rpath = "$ORIGIN/../lib";
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();
}
auto relPath = bf::relative(bf::absolute(appDirPath) / "usr/lib", bf::absolute(rpathDestination));
rpath = "$ORIGIN/" + relPath.string();
}
setElfRPathOperations[destinationPath / path.filename()] = rpath;
stripOperations.insert(destinationPath / path.filename());
if (!deployElfDependencies(path))
return false;
return true;
}
bool deployDesktopFile(const desktopfile::DesktopFile& desktopFile) {
if (hasBeenVisitedAlready(desktopFile.path())) {
ldLog() << LD_DEBUG << "File has been visited already:" << desktopFile.path() << std::endl;
return true;
}
if (!desktopFile.validate()) {
ldLog() << LD_ERROR << "Failed to validate desktop file:" << desktopFile.path() << std::endl;
}
ldLog() << "Deploying desktop file" << desktopFile.path() << std::endl;
deployFile(desktopFile.path(), appDirPath / "usr/share/applications/");
return true;
}
bool deployIcon(const bf::path& path) {
if (hasBeenVisitedAlready(path)) {
ldLog() << LD_DEBUG << "File has been visited already:" << path << std::endl;
return true;
}
ldLog() << "Deploying icon" << path << std::endl;
std::string resolution;
// if file is a vector image, use "scalable" directory
if (util::strLower(path.filename().extension().string()) == ".svg") {
resolution = "scalable";
} else {
try {
CImg<unsigned char> image(path.c_str());
auto xRes = image.width();
auto yRes = image.height();
if (xRes != yRes) {
ldLog() << LD_WARNING << "x and y resolution of icon are not equal:" << path;
}
resolution = std::to_string(xRes) + "x" + std::to_string(yRes);
// otherwise, test resolution against "known good" values, and reject invalid ones
const auto knownResolutions = {8, 16, 20, 22, 24, 32, 48, 64, 72, 96, 128, 192, 256, 512};
// assume invalid
bool invalidXRes = true, invalidYRes = true;
for (const auto res : knownResolutions) {
if (xRes == res)
invalidXRes = false;
if (yRes == res)
invalidYRes = false;
}
if (invalidXRes) {
ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes;
return false;
}
if (invalidYRes) {
ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes;
return false;
}
} catch (const CImgException& e) {
ldLog() << LD_ERROR << "CImg error: " << e.what() << std::endl;
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);
deployCopyrightFiles(path);
return true;
}
};
AppDir::AppDir(const bf::path& path) {
d = new PrivateData();
d->appDirPath = path;
}
AppDir::~AppDir() {
delete d;
}
AppDir::AppDir(const std::string& path) : AppDir(bf::path(path)) {}
bool AppDir::createBasicStructure() {
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) {
return d->deployLibrary(path, 0, false, destination);
}
bool AppDir::forceDeployLibrary(const bf::path& path, const bf::path& destination) {
return d->deployLibrary(path, 0, true, destination);
}
bool AppDir::deployExecutable(const bf::path& path, const boost::filesystem::path& destination) {
return d->deployExecutable(path, destination);
}
bool AppDir::deployDesktopFile(const desktopfile::DesktopFile& desktopFile) {
return d->deployDesktopFile(desktopFile);
}
bool AppDir::deployIcon(const bf::path& path) {
return d->deployIcon(path);
}
bool AppDir::executeDeferredOperations() {
return d->executeDeferredOperations();
}
boost::filesystem::path AppDir::path() {
return d->appDirPath;
}
static std::vector<bf::path> listFilesInDirectory(const bf::path& path, const bool recursive = true) {
std::vector<bf::path> foundPaths;
// directory_iterators throw exceptions if the directory doesn't exist
if (!bf::is_directory(path)) {
ldLog() << LD_DEBUG << "No such directory:" << path << std::endl;
return {};
}
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());
}
}
} else {
for (bf::directory_iterator i(path); i != bf::directory_iterator(); ++i) {
if (bf::is_regular_file(*i)) {
foundPaths.push_back((*i).path());
}
}
}
return foundPaths;
}
std::vector<bf::path> AppDir::deployedIconPaths() {
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;
}
std::vector<bf::path> AppDir::deployedExecutablePaths() {
return listFilesInDirectory(path() / "usr/bin/", false);
}
std::vector<desktopfile::DesktopFile> AppDir::deployedDesktopFiles() {
std::vector<desktopfile::DesktopFile> desktopFiles;
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) {
desktopFiles.push_back(desktopfile::DesktopFile(path));
}
return desktopFiles;
}
bool AppDir::createLinksInAppDirRoot(const desktopfile::DesktopFile& desktopFile, boost::filesystem::path customAppRunPath) {
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
std::string iconName;
if (!desktopFile.getEntry("Desktop Entry", "Icon", iconName)) {
ldLog() << LD_ERROR << "Icon entry missing in desktop file:" << desktopFile.path() << std::endl;
return false;
}
bool iconDeployed = false;
const auto foundIconPaths = deployedIconPaths();
if (foundIconPaths.empty()) {
ldLog() << LD_ERROR << "Could not find icon executable for Icon entry:" << iconName << std::endl;
return false;
}
for (const auto& iconPath : foundIconPaths) {
ldLog() << LD_DEBUG << "Icon found:" << iconPath << std::endl;
const bool matchesFilenameWithExtension = iconPath.filename() == iconName;
if (iconPath.stem() == iconName || matchesFilenameWithExtension) {
if (matchesFilenameWithExtension) {
ldLog() << LD_WARNING << "Icon= entry filename contains extension" << std::endl;
}
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;
}
iconDeployed = true;
break;
}
}
if (!iconDeployed) {
ldLog() << LD_ERROR << "Could not find suitable icon for Icon entry:" << iconName << std::endl;
return false;
}
if (!customAppRunPath.empty()) {
// copy custom AppRun executable
// FIXME: make sure this file is executable
ldLog() << "Deploying custom AppRun:" << customAppRunPath;
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")) {
ldLog() << LD_WARNING << "Custom AppRun detected, skipping deployment of symlink" << std::endl;
} else {
// look for suitable binary to create AppRun symlink
std::string executableName;
if (!desktopFile.getEntry("Desktop Entry", "Exec", executableName)) {
ldLog() << LD_ERROR << "Exec entry missing in desktop file:" << desktopFile.path()
<< std::endl;
return false;
}
executableName = util::split(executableName)[0];
const auto foundExecutablePaths = deployedExecutablePaths();
if (foundExecutablePaths.empty()) {
ldLog() << LD_ERROR << "Could not find suitable executable for Exec entry:" << executableName
<< std::endl;
return false;
}
bool deployedExecutable = false;
for (const auto& executablePath : foundExecutablePaths) {
ldLog() << LD_DEBUG << "Executable found:" << executablePath << std::endl;
if (executablePath.filename() == executableName) {
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;
}
deployedExecutable = true;
break;
}
}
if (!deployedExecutable) {
ldLog() << LD_ERROR << "Could not deploy symlink for executable: could not find suitable executable for Exec entry:" << executableName << std::endl;
return false;
}
}
}
return true;
}
void AppDir::deployFile(const boost::filesystem::path& from, const boost::filesystem::path& to) {
return d->deployFile(from, to, true);
}
void AppDir::setAppName(const std::string& appName) {
d->appName = appName;
}
std::vector<bf::path> AppDir::listExecutables() {
util::magic::Magic magic;
std::vector<bf::path> executables;
for (const auto& file : listFilesInDirectory(path() / "usr" / "bin", false)) {
auto fileType = magic.fileType(bf::absolute(file).string());
ldLog() << LD_DEBUG << "Type of file" << file << LD_NO_SPACE << ":" << fileType << std::endl;
// 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;
}
// if (util::stringStartsWith(fileType, "application/x-executable"))
executables.push_back(file);
}
return executables;
}
std::vector<bf::path> AppDir::listSharedLibraries() {
util::magic::Magic magic;
std::vector<bf::path> sharedLibraries;
for (const auto& file : listFilesInDirectory(path() / "usr" / "lib", true)) {
auto fileType = magic.fileType(bf::absolute(file).string());
ldLog() << LD_DEBUG << "Type of file" << file << LD_NO_SPACE << ":" << fileType << std::endl;
// 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;
}
// if (util::stringStartsWith(fileType, "application/x-sharedlib"))
sharedLibraries.push_back(file);
}
return sharedLibraries;
}
bool AppDir::deployDependenciesForExistingFiles() {
for (const auto& executable : listExecutables()) {
if (!d->deployElfDependencies(executable))
return false;
d->setElfRPathOperations[executable] = "$ORIGIN/../lib";
}
for (const auto& sharedLibrary : listSharedLibraries()) {
if (!d->deployElfDependencies(sharedLibrary))
return false;
d->setElfRPathOperations[sharedLibrary] = "$ORIGIN";
}
return true;
}
}
}
}