aboutsummaryrefslogtreecommitdiffstats
path: root/src/runtime.c
blob: b20353cf5f64438c91f0dd569faec7bcb25311cd (plain) (blame)
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
#include "runtime.h"
#include "config.h"
#include "digest/digest.h"
#include "macros.h"
#include "l2su.h"
#include "endian.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <jansson.h>
#include <stdio.h>
#include <stdint.h>
#include <curl/curl.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
#include <ftw.h>

#define L2_RUNTIME__MANIFEST_EXP_TIME ((time_t)120)

int l2_runtime__read_manifest_info(l2_sha1_digest_t *digest, size_t *sz);
int l2_runtime__write_manifest_info(const l2_sha1_digest_t *digest, size_t sz, time_t now);
int l2_runtime__download_manifest(json_t **manifest);
int l2_runtime__load_local_manifest(const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **manifest);

int l2_runtime_load_manifest(json_t **manifest)
{
  int res;

  l2_sha1_digest_t exp_digest;
  size_t psize;

  if ((res = l2_runtime__read_manifest_info(&exp_digest, &psize)) < 0) {
    return res;
  } else if (res) {
    if ((res = l2_runtime__load_local_manifest(&exp_digest, psize, manifest)) < 0) {
      return res;
    } else if (res) {
      return 0;
    }
  }

  return l2_runtime__download_manifest(manifest);
}

int l2_runtime__read_manifest_info(l2_sha1_digest_t *digest, size_t *sz)
{
  char *path;
  size_t pathlen;
  char dgbytes[L2_SHA1_DIGEST_BYTES];
  uint64_t outsz = 0;
  int retval = -1;

  L2_ASPRINTF(path, pathlen, "%s/runtime/.manifestinfo", l2_state.paths.data);

  FILE *infofp = fopen(path, "rb");
  if (!infofp) {
    if (errno == ENOENT) {
      CMD_DEBUG0("Downloading manifest: manifest info not found!");
      return 0;
    } else {
      CMD_WARN("Failed to check if the runtime manifest should be downloaded: %s", strerror(errno));
      return -1;
    }
  }

  errno = 0; /* fread may not set errno on partial reads... */
  if (fread(dgbytes, L2_SHA1_DIGEST_BYTES, 1, infofp) < 1) {
    CMD_WARN("Failed to read digest from runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  if (fread(&outsz, sizeof(uint64_t), 1, infofp) < 1) {
    CMD_WARN("Failed to read size from runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  uint64_t rdtime;
  if (fread(&rdtime, sizeof(uint64_t), 1, infofp) < 1) {
    CMD_WARN("Failed to read time from runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  rdtime = l2_betoh64(rdtime);

  if (time(NULL) - rdtime > L2_RUNTIME__MANIFEST_EXP_TIME) {
    CMD_DEBUG0("Redownloading runtime manifest (it expired hehe)");
    retval = 0;
    goto cleanup;
  }

  l2_sha1_digest_from_buffer(digest, dgbytes);
  *sz = (size_t)l2_betoh64(outsz);
  
  fclose(infofp);
  infofp = NULL;

  return 1;

cleanup:
  if (infofp) fclose(infofp);
  return retval;
}

int l2_runtime__write_manifest_info(const l2_sha1_digest_t *digest, size_t sz, time_t tm)
{
  char *path;
  size_t pathlen;
  uint64_t outsz = l2_htobe64((uint64_t)sz);

  L2_ASPRINTF(path, pathlen, "%s/runtime/.manifestinfo", l2_state.paths.data);

  FILE *infofp = fopen(path, "wb");
  if (!infofp) {
    CMD_WARN("Failed to write runtime manifest information: %s", strerror(errno));
    return -1;
  }

  char dgbytes[L2_SHA1_DIGEST_BYTES];
  l2_sha1_digest_to_buffer(digest, dgbytes);

  errno = 0;
  if (fwrite(dgbytes, L2_SHA1_DIGEST_BYTES, 1, infofp) < 1) {
    CMD_WARN("Failed to write digest to runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  if (fwrite(&outsz, sizeof(uint64_t), 1, infofp) < 1) {
    CMD_WARN("Failed to write size to runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  uint64_t wrtime = l2_htobe64((uint64_t)tm);
  if (fwrite(&wrtime, sizeof(uint64_t), 1, infofp) < 1) {
    CMD_WARN("Failed to write time to runtime manifest info: %s", strerror(errno));
    goto cleanup;
  }

  fclose(infofp);
  infofp = NULL;
  return 0;

cleanup:
  if (infofp) fclose(infofp);
  return -1;
}

struct l2_runtime__dlinfo {
  l2_sha1_state_t st;
  char *data;
  size_t len;
  size_t cap;

  FILE *ofp;
};

size_t l2_runtime__manifest_dlcb(char *buf, size_t size, size_t nmemb, void *user)
{
  struct l2_runtime__dlinfo *info = user;
  size_t realsz = size * nmemb;
  
  l2_sha1_update(&info->st, buf, realsz);

  if (fwrite(buf, size, nmemb, info->ofp) < nmemb) {
    return CURL_WRITEFUNC_ERROR;
  }

  if (info->len + realsz > info->cap) {
    size_t newcap = 16;
    while (newcap && info->len + realsz > newcap) newcap <<= 1;
    if (!newcap) return CURL_WRITEFUNC_ERROR;

    char *newbuf = realloc(info->data, newcap);
    if (!newbuf) {
      CMD_DEBUG("Failed to download runtime manifest: could not grow buffer from %zu to %zu bytes!", info->cap, newcap);
      return CURL_WRITEFUNC_ERROR;
    }

    info->cap = newcap;
    info->data = newbuf;
  }

  memcpy(info->data + info->len, buf, realsz);
  info->len += realsz;

  return realsz;
}

int l2_runtime__download_manifest(json_t **manifest)
{
  char errbuf[CURL_ERROR_SIZE];
  CURL *pc = curl_easy_init();
  if (!pc) return -1;

  struct l2_runtime__dlinfo dlinfo;

  char *path;
  size_t pathlen;
  L2_ASPRINTF(path, pathlen, "%s/runtime/manifest.json", l2_state.paths.data);

  errno = 0;
  if (l2_launcher_mkdir_parents_ex(path, 1) < 0) {
    CMD_WARN("Failed to create directories for runtime manifest: %s", strerror(errno));
    return -1;
  }

  memset(errbuf, 0, sizeof(errbuf));
  memset(&dlinfo, 0, sizeof(struct l2_runtime__dlinfo));
  l2_sha1_init(&dlinfo.st);

  dlinfo.ofp = fopen(path, "w");
  if (!dlinfo.ofp) {
    CMD_WARN("Failed to open %s for writing: %s", path, strerror(errno));
    return -1;
  }

  curl_easy_setopt(pc, CURLOPT_USERAGENT, L2_USER_AGENT);
  curl_easy_setopt(pc, CURLOPT_ERRORBUFFER, errbuf);
  curl_easy_setopt(pc, CURLOPT_URL, L2_URL_META_RUNTIME_MANIFEST);
  curl_easy_setopt(pc, CURLOPT_WRITEFUNCTION, &l2_runtime__manifest_dlcb);
  curl_easy_setopt(pc, CURLOPT_WRITEDATA, &dlinfo);

  CURLcode code = curl_easy_perform(pc);

  if (code != CURLE_OK) {
    CMD_WARN("Failed to download runtime manifest: CURL error: %s: %s", curl_easy_strerror(code), errbuf);
    goto cleanup;
  }

  long httpres;
  curl_easy_getinfo(pc, CURLINFO_RESPONSE_CODE, &httpres);
  if (httpres / 100 != 2) {
    CMD_WARN("Failed to download runtime manifest: server responded with %ld", httpres);
    goto cleanup;
  }

  curl_easy_cleanup(pc);
  pc = NULL;

  /* we must ultimately trust what we download (sad) */

  json_error_t err;
  json_t *myjson = json_loadb(dlinfo.data, dlinfo.len, JSON_REJECT_DUPLICATES, &err);
  if (!myjson) {
    CMD_WARN("Failed to parse runtime manifest JSON: %s", err.text);
    goto cleanup;
  }

  free(dlinfo.data);
  dlinfo.data = NULL;

  fclose(dlinfo.ofp);
  dlinfo.ofp = NULL;

  l2_sha1_digest_t rddg;
  l2_sha1_finalize(&dlinfo.st, &rddg);
  if (l2_runtime__write_manifest_info(&rddg, dlinfo.len, time(NULL)) < 0) {
    CMD_WARN("Failed to write runtime info: %s", strerror(errno));
  }

  *manifest = myjson;
  return 0;

cleanup:
  if (pc) curl_easy_cleanup(pc);
  
  if (dlinfo.ofp) {
    fclose(dlinfo.ofp);
    if (unlink(path) < 0) {
      CMD_WARN("Failed to delete runtime manifest %s: %s", path, strerror(errno));
    }
  }

  free(dlinfo.data);

  return -1;
}

int l2_runtime__load_local_manifest(const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **manifest)
{
  int res;
  char *manifestpath;
  size_t manifestsz;

  L2_ASPRINTF(manifestpath, manifestsz, "%s/runtime/manifest.json", l2_state.paths.data);

  FILE *fp = fopen(manifestpath, "r");
  if (!fp) {
    if (errno == ENOENT) {
      CMD_DEBUG0("Could not find local manifest!");
      return 0;
    } else {
      CMD_WARN("Failed to open runtime manifest for reading: %s", strerror(errno));
      return -1;
    }
  }

  /* check integrity */
#define L2_RUNTIME__INFO_READBUF_SZ (4096)
  uint8_t buf[L2_RUNTIME__INFO_READBUF_SZ];
  size_t nread;
  size_t nread_total = 0;

  l2_sha1_state_t st;
  l2_sha1_digest_t rddg;
  l2_sha1_init(&st);

  while ((nread = fread(buf, 1, L2_RUNTIME__INFO_READBUF_SZ, fp))) {
    l2_sha1_update(&st, buf, nread);
    nread_total += nread;
  }

#undef L2_RUNTIME__INFO_READBUF_SZ

  l2_sha1_finalize(&st, &rddg);
  
  if (l2_sha1_digest_compare(expect_digest, &rddg)) {
    char expstr[L2_SHA1_HEX_STRLEN + 1], gotstr[L2_SHA1_HEX_STRLEN + 1];
    l2_sha1_digest_to_hex(expect_digest, expstr);
    l2_sha1_digest_to_hex(&rddg, gotstr);
    CMD_DEBUG("Not using saved runtime manifest: SHA1 mismatch (expected: %s, got: %s)", expstr, gotstr);
    res = 0;
    goto unlink_cleanup;
  }

  if (expect_size != nread_total) {
    CMD_DEBUG("Not using saved runtime manifest: size mismatch (expected: %zu bytes, got %zu bytes)", expect_size, nread_total);
    res = 0;
    goto unlink_cleanup;
  }

  rewind(fp);

  json_error_t err;
  json_t *rmanifest = json_loadf(fp, JSON_REJECT_DUPLICATES, &err);
  if (!rmanifest) {
    CMD_WARN("JSON error loading runtime manifest: %s", err.text);
    res = -1;
    goto cleanup;
  }

  *manifest = rmanifest;
  res = 1;
  goto cleanup;

unlink_cleanup:
  if (unlink(manifestpath) < 0) {
    CMD_WARN("Could not delete modified runtime manifest: %s", strerror(errno));
  }

cleanup:
  if (fp) fclose(fp);
  return res;
}

int l2_runtime__load_component_manifest(const char *compname, const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **compmanifest);
int l2_runtime__download_component_manifest(const char *url, const char *compname, const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **compmanifest);
int l2_runtime__read_component_manifest_info(const char *compname, char **version, time_t *reltime);
int l2_runtime__save_component_manifest_info(const char *compname, const char *version, time_t reltime);

int l2_runtime__check_runtime_ftw(const char *fpath, const struct stat *sb, int typeflag, struct l2_ftw_data *ftwbuf);

int l2_runtime__ensure_runtime_files(json_t *jfiles);

int l2_runtime_install_component(json_t *manifest, const char *component)
{
  json_t *jrtcomps;
  if (json_unpack(manifest, "{s:{s:o}}", L2SU_JRE_ARCH, component, &jrtcomps) < 0) {
    return -1;
  }

  if (!json_is_array(jrtcomps)) {
    return -1;
  }

  if (!json_array_size(jrtcomps)) {
    CMD_ERROR("The JRE %s is not supported on your architecture (%s)!", component, L2SU_JRE_ARCH);
    return 0;
  }

  json_t *comp = json_array_get(jrtcomps, 0);
  const char *digest, *url;
  json_int_t manifestsz;

  if (json_unpack(comp, "{s:{s:s, s:I, s:s}}", "manifest", "sha1", &digest, "size", &manifestsz, "url", &url) < 0) {
    CMD_MSG0("bug", "Failed to parse java runtime manifest!");
    return -1;
  }

  l2_sha1_digest_t expect_digest;
  if (l2_sha1_digest_from_hex(&expect_digest, digest) < 0) {
    CMD_WARN("Runtime manifest has invalid SHA1 hash: %s", digest);
    return -1;
  }

  json_t *jcompmanifest = NULL;
  int res;
  if ((res = l2_runtime__load_component_manifest(component, &expect_digest, (size_t)manifestsz, &jcompmanifest)) < 0) {
    return -1;
  } else if (!res) {
    if ((res = l2_runtime__download_component_manifest(url, component, &expect_digest, (size_t)manifestsz, &jcompmanifest)) < 0) {
      return -1;
    }
  }

  char *jrepath;
  size_t jrepathlen;
  L2_ASPRINTF(jrepath, jrepathlen, "%s/runtime/%s/%s", l2_state.paths.data, L2SU_JRE_ARCH, component);

  errno = 0;
  if (l2_launcher_mkdir_parents(jrepath) < 0) {
    CMD_WARN("Failed to create directory %s: %s", jrepath, strerror(errno));
    goto cleanup;
  }

  json_t *jfiles = json_object_get(jcompmanifest, "files");
  if (!json_is_object(jfiles)) {
    CMD_WARN("Runtime manifest %s has an invalid format: no \"files\" object!", component);
    res = -1;
    goto cleanup;
  }

  /* clear the runtime directory of files/folders that should not exist */
  errno = 0;
  if (l2_launcher_ftw(jrepath, 32, &l2_runtime__check_runtime_ftw, jfiles) < 0) {
    CMD_WARN("Failed to check JRE files in %s: %s", jrepath, strerror(errno));
    goto cleanup;
  }

  if (l2_runtime__ensure_runtime_files(jfiles) < 0) {
    goto cleanup;
  }

  json_decref(jcompmanifest);
  return 0;

cleanup:
  if (jcompmanifest) json_decref(jcompmanifest);
  return -1;
}

int l2_runtime__check_runtime_ftw(const char *fpath, const struct stat *sb, int typeflag, struct l2_ftw_data *ftw)
{
  L2_UNUSED(sb);

  json_t *jfiles = ftw->user;
  json_t *myent = json_object_get(jfiles, fpath + ftw->reloffset);
  int expect_type;

  if (json_is_object(myent)) {
    const char *typestr;
    if (json_unpack(myent, "{s:s}", "type", &typestr) < 0) {
      CMD_DEBUG("couldn't unpack %s (no type)", fpath);
      return -1;
    }

    if (!strcmp(typestr, "file")) {
      expect_type = L2_FTW_FILE;
    } else if (!strcmp(typestr, "directory")) {
      expect_type = L2_FTW_DIR;
    } else if (!strcmp(typestr, "link")) {
      expect_type = L2_FTW_SYMLINK;
    } else {
      CMD_WARN("Encountered file %s which has unknown type \"%s\"!", fpath, typestr);
      return -1;
    }
  }

  if (!json_is_object(myent) || (expect_type ^ typeflag) & L2_FTW_TYPEMASK) {
    CMD_DEBUG("File %s should not exist.", fpath);
    if (S_ISDIR(sb->st_mode)) {
      errno = 0;
      if (l2_launcher_rm_tree(fpath, 50) < 0) {
        CMD_WARN("Failed to delete directory %s (which should not exist): %s", fpath, strerror(errno));
        return -1;
      }
    } else {
      if (unlink(fpath) < 0) {
        CMD_WARN("Failed to delete symlink or regular file %s (which should not exist): %s", fpath, strerror(errno));
        return -1;
      }
    }
    return 0;
  }

  /* here, we know the type is correct and myent is an object */
  if ((typeflag & L2_FTW_TYPEMASK) == L2_FTW_FILE) {
    int executable;
    if (json_unpack(myent, "{s:b}", "executable", &executable) < 0) {
      CMD_DEBUG("couldn't unpack %s (no executable)", fpath);
      return -1;
    }

    unsigned desired_mode = (sb->st_mode & 07777) | 0111;
    if ((sb->st_mode & 07777) != desired_mode && chmod(fpath, desired_mode) < 0) {
      CMD_WARN("Failed chmod(%s, %#o): %s", fpath, desired_mode, strerror(errno));
      return -1;
    }
  }

  return 0;
}

int l2_runtime__load_component_manifest(const char *compname, const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **compmanifest)
{
  int retval = -1;
  char *comppath;
  size_t complen;

  L2_ASPRINTF(comppath, complen, "%s/runtime/%s/%s.json", l2_state.paths.data, L2SU_JRE_ARCH, compname);

  FILE *fp = fopen(comppath, "r");
  if (!fp) {
    if (errno == ENOENT) {
      return 0;
    } else {
      CMD_WARN("Failed to load local %s: %s", compname, strerror(errno));
      return -1;
    }
  }

#define L2_RUNTIME__COMP_READBUF_SZ (4096)
  char readbuf[L2_RUNTIME__COMP_READBUF_SZ];
  size_t nread;
  size_t nread_total = 0;

  l2_sha1_state_t st;
  l2_sha1_digest_t rddg;
  l2_sha1_init(&st);

  while ((nread = fread(readbuf, 1, L2_RUNTIME__COMP_READBUF_SZ, fp)) < L2_RUNTIME__COMP_READBUF_SZ) {
    l2_sha1_update(&st, readbuf, nread);
    nread_total += nread;
  }
#undef L2_RUNTIME__COMP_READBUF_SZ

  l2_sha1_finalize(&st, &rddg);

  if (l2_sha1_digest_compare(expect_digest, &rddg)) {
    retval = 0;
    goto cleanup;
  }

  if (nread_total != expect_size) {
    retval = 0;
    goto cleanup;
  }

  rewind(fp);

  json_error_t err;
  json_t *js = json_loadf(fp, JSON_REJECT_DUPLICATES, &err);
  if (!js) {
    CMD_WARN("Failed to parse local %s: %s", comppath, err.text);
    goto cleanup;
  }

  *compmanifest = js;
  retval = 1;

cleanup:
  if (fp) fclose(fp);
  return retval;
}

/* TODO: refactor this I feel like I have written this function 100 times */
int l2_runtime__download_component_manifest(const char *url, const char *compname, const l2_sha1_digest_t *expect_digest, size_t expect_size, json_t **compmanifest)
{
  CURL *pc = NULL;
  char errbuf[CURL_ERROR_SIZE];

  char *comppath;
  size_t complen;
  bool cleanup_delete = false;

  L2_ASPRINTF(comppath, complen, "%s/runtime/%s/%s.json", l2_state.paths.data, L2SU_JRE_ARCH, compname);

  if (l2_launcher_mkdir_parents_ex(comppath, 1) < 0) {
    CMD_WARN("Failed to create directories to download %s manifest", compname);
    return -1;
  }

  struct l2_runtime__dlinfo dlinfo;
  memset(&dlinfo, 0, sizeof(struct l2_runtime__dlinfo));
  memset(errbuf, 0, sizeof(errbuf));

  l2_sha1_init(&dlinfo.st);

  dlinfo.ofp = fopen(comppath, "w");
  if (!dlinfo.ofp) {
    CMD_WARN("Failed to open %s for writing: %s", comppath, strerror(errno));
    goto cleanup;
  }

  pc = curl_easy_init();
  if (!pc) {
    CMD_WARN("Failed to initialize CURL for downloading %s!", compname);
    goto cleanup;
  }
  
  curl_easy_setopt(pc, CURLOPT_USERAGENT, L2_USER_AGENT);
  curl_easy_setopt(pc, CURLOPT_URL, url);
  curl_easy_setopt(pc, CURLOPT_WRITEFUNCTION, &l2_runtime__manifest_dlcb);
  curl_easy_setopt(pc, CURLOPT_WRITEDATA, &dlinfo);
  curl_easy_setopt(pc, CURLOPT_ERRORBUFFER, errbuf);

  CURLcode code;
  if ((code = curl_easy_perform(pc)) != CURLE_OK) {
    CMD_WARN("Failed to download %s manifest: %s: %s", compname, curl_easy_strerror(code), errbuf);
    goto cleanup;
  }

  long httpres = 0;
  curl_easy_getinfo(pc, CURLINFO_RESPONSE_CODE, &httpres);
  if (httpres / 100 != 2) {
    CMD_WARN("Failed to download %s manifest: server responded with %ld", compname, httpres);
    goto cleanup;
  }

  fclose(dlinfo.ofp);
  dlinfo.ofp = NULL;

  curl_easy_cleanup(pc);
  pc = NULL;

  l2_sha1_digest_t rddg;
  l2_sha1_finalize(&dlinfo.st, &rddg);
  if (l2_sha1_digest_compare(&rddg, expect_digest)) {
    char expstr[L2_SHA1_HEX_STRLEN + 1], gotstr[L2_SHA1_HEX_STRLEN + 1];
    l2_sha1_digest_to_hex(expect_digest, expstr);
    l2_sha1_digest_to_hex(&rddg, gotstr);

    CMD_WARN("Failed to download %s manifest: SHA1 mismatch (expected %s, got %s)", compname, expstr, gotstr);
    cleanup_delete = true;
    goto cleanup;
  }

  if (expect_size != dlinfo.len) {
    CMD_WARN("Failed to download %s manifest: size mismatch (expected %zu bytes, got %zu bytes)", compname, expect_size, dlinfo.len);
    cleanup_delete = true;
    goto cleanup;
  }

  json_error_t err;
  json_t *jcomp = json_loadb(dlinfo.data, dlinfo.len, JSON_REJECT_DUPLICATES, &err);
  if (!jcomp) {
    CMD_WARN("Failed to download %s manifest: JSON parse error: %s", compname, err.text);
    goto cleanup;
  }

  free(dlinfo.data);
  dlinfo.data = NULL;
  *compmanifest = jcomp;

  return 0;

cleanup:
  free(dlinfo.data);

  if (dlinfo.ofp) {
    fclose(dlinfo.ofp);
    cleanup_delete = true;
  }

  if (cleanup_delete && unlink(comppath) < 0) {
    CMD_WARN("Failed to delete %s: %s", comppath, strerror(errno));
  }

  if (pc) curl_easy_cleanup(pc);

  return -1;
}

int l2_runtime__read_component_manifest_info(const char *compname, char **version, time_t *reltime)
{
  char *infopath;
  size_t pathlen;

  uint64_t releasetime;
  uint64_t verlen;
  char *vername = NULL;

  L2_ASPRINTF(infopath, pathlen, "%s/runtime/%s/.%s.info", l2_state.paths.data, L2SU_JRE_ARCH, compname);

  FILE *fp = fopen(infopath, "rb");
  if (!fp) {
    if (errno == ENOENT) {
      return 0;
    } else {
      CMD_WARN("Failed to read component manifest info %s: %s", infopath, strerror(errno));
      return -1;
    }
  }

  errno = 0;
  if (fread(&verlen, sizeof(uint64_t), 1, fp) < 1) {
    CMD_WARN("Failed to read version length in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  verlen = l2_betoh64(verlen);
  vername = calloc(verlen + 1, 1);
  if (!vername) {
    CMD_WARN("Failed to allocate %" PRIx64 "-byte buffer: %s", verlen, strerror(errno));
    goto cleanup;
  }

  if (fread(vername, 1, verlen, fp) < verlen) {
    CMD_WARN("Failed to read version in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  vername[verlen] = '\0';

  if (fread(&releasetime, sizeof(uint64_t), 1, fp) < 1) {
    CMD_WARN("Failed to read release time in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  *version = vername;
  *reltime = (time_t)l2_betoh64(releasetime);

  return 0;

cleanup:
  if (fp) fclose(fp);
  if (vername) free(vername);
  return -1;
}

int l2_runtime__save_component_manifest_info(const char *compname, const char *version, time_t reltime)
{
  char *infopath;
  size_t pathlen;
  size_t verstrlen = strlen(version);
  uint64_t wverlen = l2_htobe64((uint64_t)verstrlen);
  uint64_t wreltime = l2_htobe64((uint64_t)reltime);

  L2_ASPRINTF(infopath, pathlen, "%s/runtime/%s/.%s.info", l2_state.paths.data, L2SU_JRE_ARCH, compname);

  FILE *fp = fopen(infopath, "wb");
  if (!fp) {
    CMD_WARN("Failed to write component manifest info %s: %s", infopath, strerror(errno));
    return -1;
  }

  errno = 0;
  if (fwrite(&wverlen, sizeof(uint64_t), 1, fp) < 1) {
    CMD_WARN("Failed to write version length in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  if (fwrite(version, 1, verstrlen, fp) < verstrlen) {
    CMD_WARN("Failed to write version name in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  if (fwrite(&wreltime, sizeof(uint64_t), 1, fp) < 1) {
    CMD_WARN("Failed to write release time in %s: %s", infopath, strerror(errno));
    goto cleanup;
  }

  fclose(fp);
  return 0;

cleanup:
  if (fp) {
    fclose(fp);
    if (unlink(infopath) < 0) {
      CMD_WARN("Failed to remove %s: %s", infopath, strerror(errno));
    }
  }
  return -1;
}