summaryrefslogtreecommitdiffstats
path: root/api/logic/mojang/PackageManifest.cpp
blob: 0b42056815c7dcceb3eb1b6c31bafdb866d8493b (plain)
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
#include "PackageManifest.h"
#include <Json.h>
#include <QDir>
#include <QDirIterator>
#include <QCryptographicHash>
#include <QDebug>

namespace mojang_files {

const Hash hash_of_empty_string = "da39a3ee5e6b4b0d3255bfef95601890afd80709";

int Path::compare(const Path& rhs) const
{
    auto left_cursor = begin();
    auto left_end = end();
    auto right_cursor = rhs.begin();
    auto right_end = rhs.end();

    while (left_cursor != left_end && right_cursor != right_end)
    {
        if(*left_cursor < *right_cursor)
        {
            return -1;
        }
        else if(*left_cursor > *right_cursor)
        {
            return 1;
        }
        left_cursor++;
        right_cursor++;
    }

    if(left_cursor == left_end)
    {
        if(right_cursor == right_end)
        {
            return 0;
        }
        return -1;
    }
    return 1;
}

void Package::addFile(const Path& path, const File& file) {
    addFolder(path.parent_path());
    files[path] = file;
}

void Package::addFolder(Path folder) {
    if(!folder.has_parent_path()) {
        return;
    }
    do {
        folders.insert(folder);
        folder = folder.parent_path();
    } while(folder.has_parent_path());
}

void Package::addLink(const Path& path, const Path& target) {
    addFolder(path.parent_path());
    symlinks[path] = target;
}

void Package::addSource(const FileSource& source) {
    sources[source.hash] = source;
}


namespace {
void fromJson(QJsonDocument & doc, Package & out) {
    std::set<Path> seen_paths;
    if (!doc.isObject())
    {
        throw JSONValidationError("file manifest is not an object");
    }
    QJsonObject root = doc.object();

    auto filesObj = Json::ensureObject(root, "files");
    auto iter = filesObj.begin();
    while (iter != filesObj.end())
    {
        Path objectPath = Path(iter.key());
        auto value = iter.value();
        iter++;
        if(seen_paths.count(objectPath)) {
            throw JSONValidationError("duplicate path inside manifest, the manifest is invalid");
        }
        if (!value.isObject())
        {
            throw JSONValidationError("file entry inside manifest is not an an object");
        }
        seen_paths.insert(objectPath);

        auto fileObject = value.toObject();
        auto type = Json::requireString(fileObject, "type");
        if(type == "directory") {
            out.addFolder(objectPath);
            continue;
        }
        else if(type == "file") {
            FileSource bestSource;
            File file;
            file.executable = Json::ensureBoolean(fileObject, QString("executable"), false);
            auto downloads = Json::requireObject(fileObject, "downloads");
            for(auto iter2 = downloads.begin(); iter2 != downloads.end(); iter2++) {
                FileSource source;

                auto downloadObject = Json::requireObject(iter2.value());
                source.hash = Json::requireString(downloadObject, "sha1");
                source.size = Json::requireInteger(downloadObject, "size");
                source.url = Json::requireString(downloadObject, "url");

                auto compression = iter2.key();
                if(compression == "raw") {
                    file.hash = source.hash;
                    file.size = source.size;
                    source.compression = Compression::Raw;
                }
                else if (compression == "lzma") {
                    source.compression = Compression::Lzma;
                }
                else {
                    continue;
                }
                bestSource.upgrade(source);
            }
            if(bestSource.isBad()) {
                throw JSONValidationError("No valid compression method for file " + iter.key());
            }
            out.addFile(objectPath, file);
            out.addSource(bestSource);
        }
        else if(type == "link") {
            auto target = Json::requireString(fileObject, "target");
            out.symlinks[objectPath] = target;
            out.addLink(objectPath, target);
        }
        else {
            throw JSONValidationError("Invalid item type in manifest: " + type);
        }
    }
    // make sure the containing folder exists
    out.folders.insert(Path());
}
}

Package Package::fromManifestContents(const QByteArray& contents)
{
    Package out;
    try
    {
        auto doc = Json::requireDocument(contents, "Manifest");
        fromJson(doc, out);
        return out;
    }
    catch (const Exception &e)
    {
        qDebug() << QString("Unable to parse manifest: %1").arg(e.cause());
        out.valid = false;
        return out;
    }
}

Package Package::fromManifestFile(const QString & filename) {
    Package out;
    try
    {
        auto doc = Json::requireDocument(filename, filename);
        fromJson(doc, out);
        return out;
    }
    catch (const Exception &e)
    {
        qDebug() << QString("Unable to parse manifest file %1: %2").arg(filename, e.cause());
        out.valid = false;
        return out;
    }
}

// FIXME: Qt filesystem abstraction is bad, but ... let's hope it doesn't break too much?
// FIXME: The error handling is just DEFICIENT
Package Package::fromInspectedFolder(const QString& folderPath)
{
    QDir root(folderPath);

    Package out;
    QDirIterator iterator(folderPath, QDir::NoDotAndDotDot | QDir::AllEntries | QDir::System | QDir::Hidden, QDirIterator::Subdirectories);
    while(iterator.hasNext()) {
        iterator.next();

        auto fileInfo = iterator.fileInfo();
        auto relPath = root.relativeFilePath(fileInfo.filePath());
        if(fileInfo.isSymLink()) {
            out.addLink(relPath, fileInfo.symLinkTarget());
        }
        else if(fileInfo.isDir()) {
            out.addFolder(relPath);
        }
        else if(fileInfo.isFile()) {
            File f;
            f.executable = fileInfo.isExecutable();
            f.size = fileInfo.size();
            // FIXME: async / optimize the hashing
            QFile input(fileInfo.absoluteFilePath());
            if(!input.open(QIODevice::ReadOnly)) {
                qCritical() << "Folder inspection: Failed to open file:" << fileInfo.absoluteFilePath();
                out.valid = false;
                break;
            }
            f.hash = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Sha1).toHex().constData();
            out.addFile(relPath, f);
        }
        else {
            // Something else... oh my
            qCritical() << "Folder inspection: Unknown filesystem object:" << fileInfo.absoluteFilePath();
            out.valid = false;
            break;
        }
    }
    out.folders.insert(Path("."));
    out.valid = true;
    return out;
}

namespace {
struct shallow_first_sort
{
    bool operator()(const Path &lhs, const Path &rhs) const
    {
        auto lhs_depth = lhs.length();
        auto rhs_depth = rhs.length();
        if(lhs_depth < rhs_depth)
        {
            return true;
        }
        else if(lhs_depth == rhs_depth)
        {
            if(lhs < rhs)
            {
                return true;
            }
        }
        return false;
    }
};

struct deep_first_sort
{
    bool operator()(const Path &lhs, const Path &rhs) const
    {
        auto lhs_depth = lhs.length();
        auto rhs_depth = rhs.length();
        if(lhs_depth > rhs_depth)
        {
            return true;
        }
        else if(lhs_depth == rhs_depth)
        {
            if(lhs < rhs)
            {
                return true;
            }
        }
        return false;
    }
};
}

UpdateOperations UpdateOperations::resolve(const Package& from, const Package& to)
{
    UpdateOperations out;

    if(!from.valid || !to.valid) {
        out.valid = false;
        return out;
    }

    // Files
    for(auto iter = from.files.begin(); iter != from.files.end(); iter++) {
        const auto &current_hash = iter->second.hash;
        const auto &current_executable = iter->second.executable;
        const auto &path = iter->first;

        auto iter2 = to.files.find(path);
        if(iter2 == to.files.end()) {
            // removed
            out.deletes.push_back(path);
            continue;
        }
        auto new_hash = iter2->second.hash;
        auto new_executable = iter2->second.executable;
        if (current_hash != new_hash) {
            out.deletes.push_back(path);
            out.downloads.emplace(
                std::pair<Path, FileDownload>{
                    path,
                    FileDownload(to.sources.at(iter2->second.hash), iter2->second.executable)
                }
            );
        }
        else if (current_executable != new_executable) {
            out.executable_fixes[path] = new_executable;
        }
    }
    for(auto iter = to.files.begin(); iter != to.files.end(); iter++) {
        auto path = iter->first;
        if(!from.files.count(path)) {
            out.downloads.emplace(
                std::pair<Path, FileDownload>{
                    path,
                    FileDownload(to.sources.at(iter->second.hash), iter->second.executable)
                }
            );
        }
    }

    // Folders
    std::set<Path, deep_first_sort> remove_folders;
    std::set<Path, shallow_first_sort> make_folders;
    for(auto from_path: from.folders) {
        auto iter = to.folders.find(from_path);
        if(iter == to.folders.end()) {
            remove_folders.insert(from_path);
        }
    }
    for(auto & rmdir: remove_folders) {
        out.rmdirs.push_back(rmdir);
    }
    for(auto to_path: to.folders) {
        auto iter = from.folders.find(to_path);
        if(iter == from.folders.end()) {
            make_folders.insert(to_path);
        }
    }
    for(auto & mkdir: make_folders) {
        out.mkdirs.push_back(mkdir);
    }

    // Symlinks
    for(auto iter = from.symlinks.begin(); iter != from.symlinks.end(); iter++) {
        const auto &current_target = iter->second;
        const auto &path = iter->first;

        auto iter2 = to.symlinks.find(path);
        if(iter2 == to.symlinks.end()) {
            // removed
            out.deletes.push_back(path);
            continue;
        }
        const auto &new_target = iter2->second;
        if (current_target != new_target) {
            out.deletes.push_back(path);
            out.mklinks[path] = iter2->second;
        }
    }
    for(auto iter = to.symlinks.begin(); iter != to.symlinks.end(); iter++) {
        auto path = iter->first;
        if(!from.symlinks.count(path)) {
            out.mklinks[path] = iter->second;
        }
    }
    out.valid = true;
    return out;
}

}