-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPackage.cpp
614 lines (520 loc) · 17.7 KB
/
Package.cpp
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
#include "./repos/Repo.hpp"
#include "Utils.hpp"
#include "ZipUtil.hpp"
#include "./repos/constants.h"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sys/stat.h>
#include <unistd.h>
#include <unordered_set>
#include <vector>
#define u8 uint8_t
#if defined(__WIIU__)
// include xml files for legacy hb app store support
#include "tinyxml.h"
#endif
Package::Package(int state)
{
this->pkg_name = "?";
this->title = "???";
this->author = "Unknown";
this->version = "0.0.0";
this->short_desc = "N/A";
this->long_desc = "N/A";
this->license = "";
this->changelog = "";
this->url = "";
this->updated = "";
this->updated_timestamp = 0;
this->download_size = 0;
this->extracted_size = 0;
this->downloads = 0;
this->category = "_all";
this->binary = "none";
this->status = state;
this->screens = 0;
}
Package::~Package() = default;
std::string Package::toString() const
{
return "[" + this->pkg_name + "] (" + this->version + ") \"" + this->title + "\" - " + this->short_desc;
}
bool Package::downloadZip(std::string_view tmp_path, float*) const
{
if (libget_status_callback != nullptr)
libget_status_callback(STATUS_DOWNLOADING, 1, 1);
// fetch zip file to tmp directory using curl
printf("--> Downloading %s to %s\n", this->pkg_name.c_str(), tmp_path.data());
auto zipUrl = this->mRepo->getZipUrl(*this);
if (zipUrl.empty()) {
printf("--> ERROR: No download url found for %s\n", this->pkg_name.c_str());
return false;
}
return downloadFileToDisk(zipUrl, std::string(tmp_path) + this->pkg_name + getUrlFileExt());
}
std::string Package::getUrlFileExt() const {
std::string urlEnding = ".zip"; // assume zip
auto zipUrl = this->mRepo->getZipUrl(*this);
if (!zipUrl.ends_with(".zip")) { // only do something if it's not a .zip already
auto lastDotPos = zipUrl.find_last_of(".");
if (lastDotPos != std::string::npos) {
// slash found, grab the file ending
std::string ending = zipUrl.substr(lastDotPos);
urlEnding = ending;
// printf("--> Found file ending: %s\n", urlEnding.c_str());
}
}
return urlEnding;
}
bool Package::install(const std::string& pkg_path, const std::string& tmp_path)
{
printf("Going to install %s\n", this->pkg_name.c_str());
// assumes that download was called first
if (libget_status_callback != nullptr)
libget_status_callback(STATUS_ANALYZING, 1, 1);
if (networking_callback != nullptr)
networking_callback(nullptr, 10, 10, 0, 0);
std::string downloadedFilePath = tmp_path + this->pkg_name + getUrlFileExt();
#ifdef NETWORK_MOCK
// for network mocking, copy over a /mock.zip to the expected download path
cp(ROOT_PATH "mock.zip", (downloadedFilePath).c_str());
#endif
// check file size to see if it's 0 bytes
struct stat sbuff = {};
stat((downloadedFilePath).c_str(), &sbuff);
if (sbuff.st_size == 0)
{
printf("--> ERROR: Downloaded file is empty\n");
return false;
}
printf("--> Downloaded file size: %ld\n", sbuff.st_size);
// our internal path of where the manifest will be
std::string ManifestPathInternal = "manifest.install";
std::string ManifestPath = pkg_path + this->pkg_name + "/" + ManifestPathInternal;
// before we uninstall, open up the current manifest, and get all the files in it
// (later we will remove any that aren't in the new manifest)
Manifest existingManifest(ManifestPath, ROOT_PATH);
std::unordered_set<std::string> existing_package_paths;
if (existingManifest.isValid() && manifest.isValid())
{
// go through its paths, add them our existing set
for (const auto& entry : manifest.getEntries())
{
ManifestOp op = entry.operation;
if (op == MUPDATE || op == MEXTRACT)
{
existing_package_paths.insert(entry.path);
}
}
}
printf("--> Existing manifest valid: %d\n", existingManifest.isValid());
//! Open the Zip file
UnZip HomebrewZip(downloadedFilePath);
printf("--> Opened Zip file\n");
// location of the info.json within the zip
std::string jsonPathInternal = "info.json";
std::string jsonPath = pkg_path + this->pkg_name + "/" + jsonPathInternal;
printf("--> Extracting to %s\n", pkg_path.c_str());
//! Check if it's a valid Zip file
if (HomebrewZip.IsValid())
{
printf("--> Valid Zip file\n");
//! First extract the Manifest
HomebrewZip.ExtractFile(ManifestPathInternal, ManifestPath);
//! Then extract the info.json file (to know what version we have installed and stuff)
HomebrewZip.ExtractFile(jsonPathInternal, jsonPath);
} else {
printf("--> NOTICE: Downloaded file is not a valid zip, just copying to SD root\n");
// TODO: support other file types, copying to their destinations
}
this->manifest = Manifest(ManifestPath, ROOT_PATH);
printf("--> Manifest valid: %d\n", manifest.isValid());
if (!manifest.isValid() && manifest.isFakeManifestPossible())
{
printf("--> ERROR: Manifest invalid/doesn't exist but recoverable\n");
#ifndef NETWORK_MOCK
printf("--> Manifest invalid/doesn't exist but recoverable, generating pseudo-manifest\n");
if (HomebrewZip.IsValid())
{
printf("HB zip is valid\n");
// generate a pseudo-manifest from the zip file
this->manifest = Manifest(HomebrewZip.PathDump(), ROOT_PATH);
} else {
printf("Not a zip but going for it\n");
// we're not a zip, so just put our one file path in the manifest
// since we don't know where to put the files, put them in a default app location
// #if defined(__WIIU__)
// platformDefault = "wiiu/apps/";
// #elif defined(WII)
// platformDefault = "apps/";
// #elif defined(SWITCH)
// platformDefault = "switch/";
// #elif defined(_3DS)
// platformDefault = "3ds/";
// #endif
std::string platformDefault = "";
std::string ext = getUrlFileExt();
if (ext == ".3dsx") {
platformDefault = "3ds/";
} else if (ext == ".cia") {
// TODO: where should these go? they need a post install step
} else if (ext == ".nro") {
platformDefault = "switch/";
} else if (ext == ".rpx" || ext == ".wuhb") {
platformDefault = "wiiu/apps/";
} else {
// assume wii format
platformDefault = "apps/";
}
printf("Continuing with %s\n", platformDefault.c_str());
std::string destFilePath = platformDefault + this->pkg_name + ext;
std::vector<std::string> entries = { destFilePath };
this->manifest = Manifest(entries, ROOT_PATH);
}
// write the pseudo-manifest to the internal package .get directory
mkpath((pkg_path + this->pkg_name).c_str());
std::ofstream pseudomanifest(ManifestPath);
printf("--> Writing pseudo-manifest to %s\n", ManifestPath.c_str());
for (const auto& entry : manifest.getEntries()) {
pseudomanifest << entry.raw << std::endl;
}
pseudomanifest.close();
// write a pseudo-info.json here too
// TODO: load other attributes from the package, besides version
printf("--> Writing pseudo-info.json to %s\n", jsonPath.c_str());
std::ofstream pseudojson(jsonPath);
pseudojson << "{\"version\":\"" << this->version << "\"}" << std::endl;
pseudojson.close();
#endif
}
std::unordered_set<std::string> incoming_package_paths;
if (manifest.isValid() && HomebrewZip.IsValid())
{
// get all file info from within the zip, for every path
auto infoMap = HomebrewZip.GetPathToFilePosMapping();
if (libget_status_callback != nullptr)
libget_status_callback(STATUS_INSTALLING, 1, 1);
int i = 0;
const auto& entries = manifest.getEntries();
for (const auto& entry : entries)
{
if (networking_callback != nullptr)
networking_callback(nullptr, entries.size(), i + 1, 0, 0);
i++;
std::string Path = entry.zip_path;
std::string ExtractPath = entry.path;
auto pathCStr = Path.c_str();
auto ePathCStr = ExtractPath.c_str();
// track this specific file for later, when we remove files that we don't have entries for
incoming_package_paths.insert(ExtractPath);
// lookup this path from our map, to get its file info
auto mapResult = infoMap.find(Path);
if (mapResult == infoMap.end())
{
// auto onlyZipPaths = HomebrewZip.PathDump();
// for (auto zipPath : onlyZipPaths)
// {
// printf("zip path: %s\n", zipPath.c_str());
// }
printf("--> ERROR: Could not find [%s] path in zip file\n", pathCStr);
continue;
}
auto filePos = mapResult->second;
int resp = 0;
switch (entry.operation)
{
case MEXTRACT:
//! Simply Extract, with no checks or anything, won't be deleted upon removal
info("%s : EXTRACT\n", pathCStr);
resp = HomebrewZip.Extract(ePathCStr, filePos);
break;
case MUPDATE:
info("%s : UPDATE\n", pathCStr);
resp = HomebrewZip.Extract(ePathCStr, filePos);
break;
case MGET:
{
info("%s : GET\n", pathCStr);
struct stat sbuff = {};
if (stat(ePathCStr, &sbuff) != 0) //! File doesn't exist, extract
resp = HomebrewZip.Extract(ePathCStr, filePos);
else
info("File already exists, skipping...");
break;
}
default:
info("%s : NOP\n", ePathCStr);
break;
}
if (resp < 0)
{
printf("--> Some issue happened while extracting! Error: %d\n", resp);
return false;
}
}
// done installing new files, go through the remaining files that we didn't just visit
// and remove them (files that WERE in our old manifest, and AREN'T in the new one we got)
for (auto& path : existing_package_paths)
{
// only continue if it's not in our incoming package path set
if (incoming_package_paths.find(path) == incoming_package_paths.end())
{
std::remove(path.c_str());
// printf("REMOVING: %s\n", path.c_str());
}
}
}
else
{
//! Extract the whole zip
// printf("No manifest found: extracting the Zip\n");
// HomebrewZip.ExtractAll("sdroot/");
// TODO: generate a manifest here, it's needed for deletion
if (!HomebrewZip.IsValid())
{
// our zip was no good, so just copy over the file to the destination
// it's the first file in the pseudo-manifest
// make a folder for the base name of the target file
auto containingDir = dir_name(manifest.getEntries().front().path);
mkpath(containingDir.c_str());
auto firstEntry = manifest.getEntries().front();
rename(downloadedFilePath.c_str(), firstEntry.path.c_str());
printf("--> Moved %s to %s\n", downloadedFilePath.c_str(), firstEntry.path.c_str());
}
else if (!manifest.isFakeManifestPossible())
{
printf("--> Invalid/No manifest file found (or error writing manifest download)! Refusing to extract.\n");
return false;
}
}
//! Delete the Zip file
std::remove((tmp_path + this->pkg_name + ".zip").c_str());
return true;
}
bool Package::remove(std::string_view pkg_path)
{
if (libget_status_callback != nullptr)
libget_status_callback(STATUS_REMOVING, 1, 1);
// perform an uninstall of the current package, parsing the cached metadata
std::string ManifestPathInternal = "manifest.install";
std::string ManifestPath = std::string(pkg_path) + this->pkg_name + "/" + ManifestPathInternal;
info("HomebrewManager::Delete\n");
std::unordered_set<std::string> uniq_folders;
//! Parse the manifest
info("Parsing the Manifest\n");
if (!manifest.isValid())
{
this->manifest = Manifest(ManifestPath, ROOT_PATH); // Load and parse manifest if not yet done
}
if (this->manifest.isValid())
{
int i = 0;
const auto& entries = manifest.getEntries();
for (const auto& entry : entries)
{
if (networking_callback != nullptr)
networking_callback(nullptr, entries.size(), i + 1, 0, 0);
i++;
const std::string& DeletePath = entry.path;
// the current directory
std::string cur_dir = dir_name(DeletePath);
uniq_folders.insert(cur_dir);
ManifestOp op = entry.operation;
if (op != NOP && op != MEXTRACT) // get, upgrade, and local
{
info("Removing %s\n", DeletePath.c_str());
std::remove(DeletePath.c_str());
}
}
}
else
{
printf("--> ERROR: Manifest missing or invalid at %s\n", ManifestPath.c_str());
return false;
}
// sort unique folders from longest to shortest
std::vector<std::string> folders;
for (auto& folder : uniq_folders)
{
folders.push_back(folder);
}
std::sort(folders.begin(), folders.end(), compareLen);
std::vector<std::string> intermediate_folders;
// rmdir (only works if folders are empty!) out all uniq dirs...
std::string fsroot(ROOT_PATH);
for (auto& folder : folders)
{
auto parent = dir_name(folder);
while (!parent.empty())
{
std::cout << "processing... " << parent << "\n";
if ((uniq_folders.find(parent) == uniq_folders.end()) && (parent.length() > fsroot.length()))
{
std::cout << "adding " << parent << "\n";
// folder not already seen, track it
uniq_folders.insert(parent);
intermediate_folders.push_back(parent);
}
parent = dir_name(parent);
}
}
// have to re-add these outside of the loop because we can't
// modify the vector while iterating through it
for (auto& folder : intermediate_folders)
folders.push_back(folder);
// re-sort it
std::sort(folders.begin(), folders.end(), compareLen);
for (auto& folder : folders)
{
rmdir(folder.c_str());
}
printf("--> Removing manifest...\n");
std::remove(ManifestPath.c_str());
auto full_pkg_path = std::string(pkg_path) + this->pkg_name;
std::remove((full_pkg_path + "/info.json").c_str());
std::remove((full_pkg_path + "/icon.png").c_str()); // clean up icon if present
rmdir((std::string(pkg_path) + this->pkg_name).c_str());
// package removed, clean up empty directories
// TODO: potentially prompt user to remove some known config files for a given package
// see: https://github.com/vgmoose/get/issues/1
// remove_empty_dirs(ROOT_PATH, 0);
printf("--> Homebrew removed\n");
return true;
}
void Package::updateStatus(const std::string& pkg_path)
{
// check if the manifest for this package exists
std::string ManifestPathInternal = "manifest.install";
std::string ManifestPath = pkg_path + this->pkg_name + "/" + ManifestPathInternal;
struct stat sbuff = {};
if (stat(ManifestPath.c_str(), &sbuff) == 0)
{
// manifest exists, we are at least installed
this->status = INSTALLED;
this->manifest = Manifest(ManifestPath, ROOT_PATH);
}
// check for info.json, parse version out of it
// and compare against the package's to know whether
// it's an update or not
std::string jsonPathInternal = "info.json";
std::string jsonPath = pkg_path + this->pkg_name + "/" + jsonPathInternal;
if (INSTALLED && stat(jsonPath.c_str(), &sbuff) == 0)
{
// pull out the version number and check if it's
// different than the one on the repo
std::ifstream ifs(jsonPath.c_str());
rapidjson::IStreamWrapper isw(ifs);
if (!ifs.good())
{
printf("--> Could not locate %s", jsonPath.c_str());
this->status = UPDATE; // issue opening info.json, assume update
return;
}
rapidjson::Document doc;
rapidjson::ParseResult ok = doc.ParseStream(isw);
std::string tmpVersion;
if (ok && doc.HasMember("version"))
{
const rapidjson::Value& info_doc = doc["version"];
tmpVersion = info_doc.GetString();
}
else
tmpVersion = "0.0.0";
if (tmpVersion != this->version)
this->status = UPDATE;
// we're eithe ran update or an install at this point
return;
}
else if (this->status == INSTALLED)
{
this->status = UPDATE; // manifest, but no info, always update
return;
}
// if we're down here, and it's not a local package
// already, it's probably a get package (package was
// available, but the manifest wasn't installed)
if (this->status != LOCAL)
this->status = GET;
// check for any homebrew that may have been previously installed
// TODO: see https://github.com/vgmoose/hb-appstore/issues/20
this->status = this->isPreviouslyInstalled();
}
int Package::isPreviouslyInstalled()
{
// TODO: check for and scan Switch NRO files
#if defined(__WIIU__)
// we're on a Wii U, so let's check for any HBL meta.xml files that match this package's name,
// and if it exists check the version based on that
// TODO: check for and scan WUHB files
TiXmlDocument xmlDoc((std::string(ROOT_PATH) + "wiiu/apps/" + this->pkg_name + "/meta.xml").c_str());
bool xmlExists = xmlDoc.LoadFile();
if (xmlExists)
{
TiXmlElement* appNode = xmlDoc.FirstChildElement("app");
if (appNode)
{
TiXmlElement* node = appNode->FirstChildElement("version");
if (node && node->FirstChild() && node->FirstChild()->Value())
{
// version exists, we should compare the value to the one on the server (this package)
if (this->version != node->FirstChild()->Value())
return UPDATE;
else
return LOCAL;
}
}
}
#endif
// since we are appstore and know that what version we're supposed to be, mark us local or updated if needed
// TODO: make version check here dynamic, and also support other NROs or hint files
// notice: this means that even if appstore isn't installed but is running, it will show as an update
if (this->pkg_name == APP_SHORTNAME)
{
// it's app store, but wasn't detected as installed
if (this->version == APP_VERSION)
return LOCAL;
else
return UPDATE;
}
return this->status;
}
const char* Package::statusString() const
{
switch (this->status)
{
case LOCAL:
return "LOCAL";
case INSTALLED:
return "INSTALLED";
case UPDATE:
return "UPDATE";
case GET:
return "GET";
}
return "UNKNOWN";
}
std::string Package::getIconUrl() const
{
// ask the parent repo for the icon url TODO: some fallback?
if (this->mRepo == nullptr)
{
printf("--> ERROR: Parent repo not set for package %s\n", this->pkg_name.c_str());
return "";
}
return this->mRepo->getIconUrl(*this);
}
std::string Package::getBannerUrl() const
{
return this->mRepo->getUrl() + "/packages/" + this->pkg_name + "/screen.png";
}
std::string Package::getScreenShotUrl(int count) const
{
return this->mRepo->getUrl() + "/packages/" + this->pkg_name + "/screen" + std::to_string(count) + ".png";
}
std::string Package::getManifestUrl() const
{
return this->mRepo->getUrl() + "/packages/" + this->pkg_name + "/manifest.install";
}