-
Notifications
You must be signed in to change notification settings - Fork 12
/
Package.cpp
457 lines (384 loc) · 13.1 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
#include "Package.hpp"
#include "Utils.hpp"
#include "ZipUtil.hpp"
#include "constants.h"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#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->manifest = NULL;
}
Package::~Package()
{
delete this->contents;
}
std::string Package::toString()
{
return "[" + this->pkg_name + "] (" + this->version + ") \"" + this->title + "\" - " + this->short_desc;
}
bool Package::downloadZip(const char* tmp_path, float* progress)
{
if (libget_status_callback != NULL)
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);
return downloadFileToDisk(*(this->repoUrl) + "/zips/" + this->pkg_name + ".zip", tmp_path + this->pkg_name + ".zip");
}
bool Package::install(const char* pkg_path, const char* tmp_path)
{
// assumes that download was called first
if (libget_status_callback != NULL)
libget_status_callback(STATUS_INSTALLING, 1, 1);
#ifdef NETWORK_MOCK
// for network mocking, copy over a /mock.zip to the expected download path
cp(ROOT_PATH "mock.zip", (tmp_path + this->pkg_name + ".zip").c_str());
#endif
// 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.valid) {
// go through its paths, add them our existing set
for (int i = 0; i < manifest->entries.size(); i++)
{
ManifestOp op = this->manifest->entries[i].operation;
if (op == MUPDATE || op == MEXTRACT)
existing_package_paths.insert(manifest->entries[i].path);
}
}
//! Open the Zip file
UnZip* HomebrewZip = new UnZip((tmp_path + this->pkg_name + ".zip").c_str());
//! First extract the Manifest
HomebrewZip->ExtractFile(ManifestPathInternal.c_str(), ManifestPath.c_str());
//! Then extract the info.json file (to know what version we have installed and stuff)
std::string jsonPathInternal = "info.json";
std::string jsonPath = pkg_path + this->pkg_name + "/" + jsonPathInternal;
HomebrewZip->ExtractFile(jsonPathInternal.c_str(), jsonPath.c_str());
this->manifest = new Manifest(ManifestPath, ROOT_PATH);
if (!manifest->valid && manifest->fakeManifestPossible)
{
#ifndef NETWORK_MOCK
printf("--> Manifest invalid/doesn't exist but recoverable, generating pseudo-manifest\n");
this->manifest = new Manifest(HomebrewZip->PathDump(), ROOT_PATH);
std::ofstream pseudomanifest (ManifestPath);
for (size_t i = 0; i <= manifest->entries.size() - 1; i++)
{
pseudomanifest << manifest->entries[i].raw << std::endl;
}
pseudomanifest.close();
#endif
}
std::unordered_set<std::string> incoming_package_paths;
if (manifest->valid)
{
// get all file info from within the zip, for every path
auto infoMap = HomebrewZip->GetPathToFilePosMapping();
for (int i = 0; i < manifest->entries.size(); i++)
{
if (networking_callback != NULL)
networking_callback(0, manifest->entries.size(), i+1, 0, 0);
std::string Path = manifest->entries[i].zip_path;
std::string ExtractPath = manifest->entries[i].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())
{
printf("--> ERROR: Could not find [%s] path in zip file\n", pathCStr);
continue;
}
auto filePos = mapResult->second;
int resp = 0;
switch (manifest->entries[i].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, NULL, &filePos);
break;
case MUPDATE:
info("%s : UPDATE\n", pathCStr);
resp = HomebrewZip->Extract(ePathCStr, NULL, &filePos);
break;
case MGET:
info("%s : GET\n", pathCStr);
struct stat sbuff;
if (stat(ExtractPath.c_str(), &sbuff) != 0) //! File doesn't exist, extract
resp = HomebrewZip->Extract(ePathCStr, NULL, &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 (!manifest->fakeManifestPossible){
printf("--> Invalid/No manifest file found (or error writing manifest download)! Refusing to extract.\n");
return false;
}
}
//! Close the Zip file
delete HomebrewZip;
//! Delete the Zip file
std::remove((tmp_path + this->pkg_name + ".zip").c_str());
return true;
}
bool Package::remove(const char* pkg_path)
{
if (libget_status_callback != NULL)
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 = 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) this->manifest = new Manifest(ManifestPath, ROOT_PATH); // Load and parse manifest if not yet done
if(this->manifest->valid)
{
for (int i = 0; i < this->manifest->entries.size(); i++)
{
if (networking_callback != NULL)
networking_callback(0, manifest->entries.size(), i+1, 0, 0);
std::string DeletePath = manifest->entries[i].path;
// the current directory
std::string cur_dir = dir_name(DeletePath);
uniq_folders.insert(cur_dir);
ManifestOp op = this->manifest->entries[i].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 != "")
{
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());
std::remove((std::string(pkg_path) + this->pkg_name + "/info.json").c_str());
delete this->manifest;
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 char* 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 = new Manifest(ManifestPath, ROOT_PATH);
}
// TODO: 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 version;
if (ok && doc.HasMember("version"))
{
const rapidjson::Value& info_doc = doc["version"];
version = info_doc.GetString();
}
else
version = "0.0.0";
if (version != 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 == "appstore")
{
// 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()
{
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()
{
return *(this->repoUrl) + "/packages/" + this->pkg_name + "/icon.png";
}
std::string Package::getBannerUrl()
{
return *(this->repoUrl) + "/packages/" + this->pkg_name + "/screen.png";
}
std::string Package::getManifestUrl()
{
return *(this->repoUrl) + "/packages/" + this->pkg_name + "/manifest.install";
}