forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.c
1156 lines (950 loc) · 27.6 KB
/
storage.c
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017-2022 Pantacor Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
#include <netdb.h>
#include <inttypes.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <errno.h>
#include <linux/limits.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/statfs.h>
#include <mbedtls/sha256.h>
#include <jsmn/jsmnutil.h>
#include "updater.h"
#include "objects.h"
#include "storage.h"
#include "state.h"
#include "bootloader.h"
#include "init.h"
#include "addons.h"
#include "state.h"
#include "jsons.h"
#include "tsh.h"
#include "signature.h"
#include "paths.h"
#include "metadata.h"
#include "parser/parser.h"
#include "utils/json.h"
#include "utils/str.h"
#include "utils/fs.h"
#include "utils/timer.h"
#include "utils/math.h"
#define MODULE_NAME "storage"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
static struct timer threshold_timer;
static int pv_storage_gc_objects(struct pantavisor *pv)
{
int reclaimed = 0;
char path[PATH_MAX];
struct stat st;
struct pv_path *o, *tmp;
struct dl_list objects;
dl_list_init(&objects);
pv_paths_storage_object(path, PATH_MAX, "");
if (pv_storage_get_subdir(path, "", &objects))
goto out;
dl_list_for_each_safe(o, tmp, &objects, struct pv_path, list)
{
if (!strncmp(o->path, "..", strlen("..")) ||
!strncmp(o->path, ".", strlen(".")))
continue;
pv_paths_storage_object(path, PATH_MAX, o->path);
memset(&st, 0, sizeof(struct stat));
if (stat(path, &st) < 0)
continue;
if (st.st_nlink > 1)
continue;
// do not remove objects belonging to an ongoing update
if (pv->update) {
if (pv_objects_id_in_step(pv->update->pending, o->path))
continue;
}
reclaimed += st.st_size;
pv_fs_path_remove(path, false);
pv_log(DEBUG, "removed unused object '%s', reclaimed %lu bytes",
path, st.st_size);
}
out:
pv_storage_free_subdir(&objects);
return reclaimed;
}
void pv_storage_rm_rev(const char *rev)
{
char path[PATH_MAX] = { 0 };
pv_log(DEBUG, "removing revision %s from disk", rev);
pv_paths_storage_trail(path, PATH_MAX, rev);
pv_fs_path_remove(path, true);
pv_paths_pv_log(path, PATH_MAX, rev);
pv_fs_path_remove(path, true);
pv_paths_storage_disks_rev(path, PATH_MAX, rev);
pv_fs_path_remove(path, true);
}
int pv_storage_get_subdir(const char *path, const char *prefix,
struct dl_list *subdirs)
{
int n, len, ret = 0;
char basedir[PATH_MAX];
struct dirent **dirs = NULL;
struct pv_path *subdir;
SNPRINTF_WTRUNC(basedir, sizeof(basedir), "%s%s", path, prefix);
n = scandir(basedir, &dirs, NULL, alphasort);
if (n < 0)
goto out;
while (n--) {
char *tmp = dirs[n]->d_name;
while (*tmp)
tmp++;
if (tmp[0] != '\0')
continue;
subdir = calloc(1, sizeof(struct pv_path));
if (!subdir) {
ret = -1;
free(dirs[n]);
goto out;
}
len = strlen(prefix) + strlen(dirs[n]->d_name) + 1;
subdir->path = calloc(len, sizeof(char));
SNPRINTF_WTRUNC(subdir->path, len, "%s%s", prefix,
dirs[n]->d_name);
free(dirs[n]);
dl_list_init(&subdir->list);
dl_list_add(subdirs, &subdir->list);
}
out:
if (dirs)
free(dirs);
return ret;
}
void pv_storage_free_subdir(struct dl_list *subdirs)
{
struct pv_path *p, *tmp;
dl_list_for_each_safe(p, tmp, subdirs, struct pv_path, list)
{
dl_list_del(&p->list);
free(p->path);
free(p);
}
}
static int pv_storage_get_revisions(struct dl_list *revisions)
{
char path[PATH_MAX];
int ret = -1;
pv_paths_storage_trail(path, PATH_MAX, "");
if (pv_storage_get_subdir(path, "locals/", revisions) ||
pv_storage_get_subdir(path, "", revisions))
goto out;
ret = 0;
out:
return ret;
}
struct pv_storage {
off_t total;
off_t free;
int free_percentage;
off_t reserved;
int reserved_percentage;
off_t real_free;
int real_free_percentage;
int threshold;
};
static struct pv_storage *pv_storage_new()
{
char path[PATH_MAX];
struct statfs buf;
struct pv_storage *this;
pv_paths_storage_file(path, PATH_MAX, PVMOUNTED_FNAME);
if (statfs(path, &buf) < 0)
return NULL;
this = calloc(1, sizeof(struct pv_storage));
if (this) {
this->total = buf.f_bsize * buf.f_blocks;
this->free = buf.f_bsize * buf.f_bfree;
if (this->total)
this->free_percentage =
(this->free * 100) / this->total;
this->reserved_percentage =
pv_config_get_int(PV_STORAGE_GC_RESERVED);
this->reserved =
(this->total * this->reserved_percentage) / 100;
if (this->free > this->reserved)
this->real_free = this->free - this->reserved;
if (this->total)
this->real_free_percentage =
(this->real_free * 100) / this->total;
this->threshold = pv_config_get_int(PV_STORAGE_GC_THRESHOLD);
return this;
}
return NULL;
}
static void pv_storage_print(struct pv_storage *storage)
{
pv_log(DEBUG, "total disk space: %d B", storage->total);
pv_log(DEBUG, "free disk space: %d B (%d%% of total)", storage->free,
storage->free_percentage);
pv_log(DEBUG, "reserved disk space: %d B (%d%% of total)",
storage->reserved, storage->reserved_percentage);
pv_log(INFO, "real free disk space: %d B (%d%% of total)",
storage->real_free, storage->real_free_percentage);
}
off_t pv_storage_get_free()
{
off_t real_free = 0;
struct pv_storage *storage;
storage = pv_storage_new();
if (storage) {
real_free = storage->real_free;
free(storage);
}
return real_free;
}
int pv_storage_gc_run()
{
int reclaimed = 0, len;
struct pv_state *s = 0, *u = 0;
struct dl_list revisions; // pv_path
struct pv_path *r, *tmp;
struct pantavisor *pv = pv_get_instance();
if (pv->state)
s = pv->state;
if (pv->update)
u = pv->update->pending;
dl_list_init(&revisions);
if (pv_storage_get_revisions(&revisions)) {
pv_log(ERROR, "error parsings revs on disk for GC");
return -1;
}
// check all revisions in list
dl_list_for_each_safe(r, tmp, &revisions, struct pv_path, list)
{
len = strlen(r->path) + 1;
// dont reclaim current, locals, update, last booted up revisions or factory if configured
if (!strncmp(r->path, "..", len) ||
!strncmp(r->path, ".", len) ||
!strncmp(r->path, "current", len) ||
!strncmp(r->path, "locals", len) ||
!strncmp(r->path, "locals/..", len) ||
!strncmp(r->path, "locals/.", len) ||
(s && !strncmp(r->path, s->rev, len)) ||
(u && !strncmp(r->path, u->rev, len)) ||
!strncmp(r->path, pv_bootloader_get_done(), len) ||
(pv_config_get_bool(PV_STORAGE_GC_KEEP_FACTORY) &&
!strncmp(r->path, "0", len)))
continue;
// unlink the given revision from local storage
pv_storage_rm_rev(r->path);
}
pv_storage_free_subdir(&revisions);
// get rid of orphaned objects
reclaimed = pv_storage_gc_objects(pv);
if (reclaimed)
pv_log(DEBUG, "total reclaimed: %d bytes", reclaimed);
return reclaimed;
}
off_t pv_storage_gc_run_needed(off_t needed)
{
off_t available = pv_storage_get_free();
if (needed > available) {
pv_log(WARN,
"%d B needed but only %d B available. Freeing up space...",
needed, available);
pv_storage_gc_run();
available = pv_storage_get_free();
if (needed > available)
pv_log(ERROR,
"still %d B needed but only %d B available",
needed, available);
}
return available;
}
void pv_storage_gc_defer_run_threshold()
{
struct pantavisor *pv = pv_get_instance();
int defertime = pv_config_get_int(PV_STORAGE_GC_THRESHOLD_DEFERTIME);
timer_start(&threshold_timer, defertime, 0, RELATIV_TIMER);
if (!pv->loading_objects) {
pv->loading_objects = true;
pv_log(INFO,
"disabled garbage collector threshold. Will be available again in %d seconds",
defertime);
}
}
static char *pv_storage_get_json(struct pv_storage *storage)
{
struct pv_json_ser js;
pv_json_ser_init(&js, 512);
pv_json_ser_object(&js);
{
pv_json_ser_key(&js, "total");
pv_json_ser_number(&js, storage->total);
pv_json_ser_key(&js, "free");
pv_json_ser_number(&js, storage->free);
pv_json_ser_key(&js, "reserved");
pv_json_ser_number(&js, storage->reserved);
pv_json_ser_key(&js, "real_free");
pv_json_ser_number(&js, storage->real_free);
pv_json_ser_object_pop(&js);
}
return pv_json_ser_str(&js);
}
void pv_storage_gc_run_threshold()
{
char *json;
struct pv_storage *storage;
struct pantavisor *pv = pv_get_instance();
struct timer_state tstate;
storage = pv_storage_new();
if (!storage)
return;
json = pv_storage_get_json(storage);
pv_metadata_add_devmeta("storage", json);
free(json);
tstate = timer_current_state(&threshold_timer);
if (pv->loading_objects && tstate.fin) {
pv->loading_objects = false;
pv_log(INFO, "garbage collector enabled again");
}
if (!pv_config_get_int(PV_STORAGE_GC_THRESHOLD) || pv->loading_objects)
goto out;
if (storage && (storage->real_free_percentage < storage->threshold)) {
pv_log(INFO,
"free disk space is %d%%, which is under the %d%% threshold. Freeing up space",
storage->real_free_percentage, storage->threshold);
pv_storage_gc_run();
}
out:
free(storage);
}
int pv_storage_validate_file_checksum(char *path, char *checksum)
{
int fd, ret = -1, bytes;
mbedtls_sha256_context sha256_ctx;
unsigned char buf[4096];
unsigned char cloud_sha[32];
unsigned char local_sha[32];
char *tmp_sha;
char byte[3];
fd = open(path, O_RDONLY);
if (fd < 0)
return ret;
mbedtls_sha256_init(&sha256_ctx);
mbedtls_sha256_starts(&sha256_ctx, 0);
while ((bytes = read(fd, buf, 4096)) > 0)
mbedtls_sha256_update(&sha256_ctx, buf, bytes);
mbedtls_sha256_finish(&sha256_ctx, local_sha);
mbedtls_sha256_free(&sha256_ctx);
// signed to unsigned
tmp_sha = checksum;
for (int i = 0, j = 0; j < 32; i = i + 2, j++) {
strncpy(byte, &tmp_sha[i], 2);
byte[2] = 0;
cloud_sha[j] = strtoul(byte, NULL, 16);
}
if (strncmp((char *)cloud_sha, (char *)local_sha, 32)) {
pv_log(WARN, "sha256 mismatch in %s", path);
goto out;
}
ret = 0;
out:
close(fd);
return ret;
}
static bool pv_storage_validate_objects_object_checksum(char *checksum)
{
char path[PATH_MAX];
pv_paths_storage_object(path, PATH_MAX, checksum);
pv_log(DEBUG, "validating checksum for object %s", path);
return !pv_storage_validate_file_checksum(path, checksum);
}
bool pv_storage_validate_trails_object_checksum(const char *rev,
const char *name,
char *checksum)
{
char path[PATH_MAX];
// validate object in pool to match
if (!pv_storage_validate_objects_object_checksum(checksum)) {
pv_log(ERROR, "object %s with checksum %s failed", name,
checksum);
return false;
}
pv_paths_storage_trail_file(path, PATH_MAX, rev, name);
pv_log(DEBUG, "validating checksum for object in trail %s", path);
return !pv_storage_validate_file_checksum(path, checksum);
}
bool pv_storage_validate_trails_json_value(const char *rev, const char *name,
char *val)
{
char path[PATH_MAX];
char *buf;
pv_paths_storage_trail_file(path, PATH_MAX, rev, name);
buf = pv_fs_file_load(path, 0);
if (!buf) {
pv_log(ERROR, "could not load %s, %s", path, strerror(errno));
return false;
}
pv_log(DEBUG, "validating value for json %s", path);
bool ret = pv_str_matches(val, strlen(val), buf, strlen(buf));
free(buf);
return ret;
}
void pv_storage_set_active(struct pantavisor *pv)
{
char path[PATH_MAX];
// path to current revision - relative and dir for fd
pv_paths_storage_trail(path, PATH_MAX, "current");
unlink(path);
symlink(pv->state->rev, path);
// path to current logs - relative and fd for dir
pv_paths_pv_log(path, PATH_MAX, "current");
unlink(path);
symlink(pv->state->rev, path);
}
int pv_storage_update_factory(const char *rev)
{
int res = -1, fd_c = -1, fd_f = -1;
char dst_path[PATH_MAX], src_path[PATH_MAX];
// first, remove revision 0 that is going to be substituted
pv_storage_rm_rev("0");
// now, create revision 0
pv_paths_storage_trail_pvr_file(dst_path, PATH_MAX, "0", "");
pv_fs_mkdir_p(dst_path, 0755);
// finally, copy revision json to revision 0
pv_paths_storage_trail_pvr_file(src_path, PATH_MAX, rev, JSON_FNAME);
pv_paths_storage_trail_pvr_file(dst_path, PATH_MAX, "0", JSON_FNAME);
pv_log(DEBUG, "copying %s to %s", src_path, dst_path);
if (pv_fs_file_copy(src_path, dst_path, 0644) < 0) {
pv_log(ERROR, "cannot copy %s into %s: %s", src_path, dst_path,
strerror(errno));
goto out;
}
res = 0;
out:
close(fd_c);
close(fd_f);
return res;
}
bool pv_storage_is_revision_local(const char *rev)
{
bool ret = false;
char *first = strchr(rev, '/');
char *last = strrchr(rev, '/');
size_t pre_len = strlen(PREFIX_LOCAL_REV);
size_t len = strlen(rev);
if ((len > pre_len) && (len - pre_len > SIZE_LOCAL_REV)) {
pv_log(WARN, "revision name longer than %d", SIZE_LOCAL_REV);
goto out;
}
if (strncmp(rev, PREFIX_LOCAL_REV, pre_len)) {
pv_log(INFO, "revision name does not start with %s",
PREFIX_LOCAL_REV);
goto out;
}
if (!first || (first != last)) {
pv_log(WARN, "revision name contains more than one '/'");
goto out;
}
ret = true;
out:
return ret;
}
static char *pv_storage_get_file_date(const char *path)
{
struct stat st;
struct tm *nowtm;
char *date = calloc(32, sizeof(char));
stat(path, &st);
nowtm = localtime(&st.st_mtim.tv_sec);
strftime(date, 32, "%Y-%m-%dT%H:%M:%SZ", nowtm);
return date;
}
char *pv_storage_get_revisions_string()
{
int len = 1, line_len;
char path[PATH_MAX];
char *json = calloc(len, sizeof(char)), *progress = NULL, *date = NULL,
*commitmsg = NULL, *esc_commitmsg = NULL;
struct dl_list revisions; // pv_path
struct pv_path *r, *tmp;
dl_list_init(&revisions);
if (pv_storage_get_revisions(&revisions)) {
pv_log(ERROR, "error parsings revs on disk for ctrl");
goto out;
}
// open json
json[0] = '[';
if (dl_list_empty(&revisions)) {
len++;
goto out;
}
// fill up revision list in json
dl_list_for_each_safe(r, tmp, &revisions, struct pv_path, list)
{
// dont list current or locals dir
if (!strncmp(r->path, "..", strlen("..") + 1) ||
!strncmp(r->path, ".", strlen(".") + 1) ||
!strncmp(r->path, "current", strlen("current") + 1) ||
!strncmp(r->path, "locals", strlen("locals") + 1) ||
!strncmp(r->path, "locals/..", strlen("locals/..") + 1) ||
!strncmp(r->path, "locals/.", strlen("locals/.") + 1))
continue;
// get revision progress
pv_paths_storage_trail_pv_file(path, PATH_MAX, r->path,
PROGRESS_FNAME);
progress = pv_fs_file_load(path, 512);
if (!progress || !strlen(progress)) {
progress = calloc(3, sizeof(char));
sprintf(progress, "{}");
}
// get revision date
pv_paths_storage_trail(path, PATH_MAX, r->path);
date = pv_storage_get_file_date(path);
// get revision commit message
pv_paths_storage_trail_pv_file(path, PATH_MAX, r->path,
COMMITMSG_FNAME);
commitmsg = pv_fs_file_load(path, 512);
if (commitmsg)
esc_commitmsg =
pv_json_format(commitmsg, strlen(commitmsg));
if (!commitmsg || !esc_commitmsg) {
esc_commitmsg = calloc(1, sizeof(char));
esc_commitmsg[0] = '\0';
}
if (commitmsg) {
free(commitmsg);
commitmsg = NULL;
}
// add new revision line to json
line_len = strlen(r->path) + strlen(esc_commitmsg) +
strlen(date) + strlen(progress) + 52;
json = realloc(json, len + line_len + 1);
SNPRINTF_WTRUNC(
&json[len], line_len + 1,
"{\"name\":\"%s\", \"date\":\"%s\", \"commitmsg\":\"%s\", \"progress\":%s},",
r->path, date, esc_commitmsg, progress);
len += line_len;
if (progress) {
free(progress);
progress = NULL;
}
if (esc_commitmsg) {
free(esc_commitmsg);
esc_commitmsg = NULL;
}
if (date) {
free(date);
date = NULL;
}
}
out:
len += 1;
json = realloc(json, len);
// close json
json[len - 2] = ']';
json[len - 1] = '\0';
// free temporary revision list
dl_list_for_each_safe(r, tmp, &revisions, struct pv_path, list)
{
free(r->path);
dl_list_del(&r->list);
free(r);
}
if (progress)
free(progress);
if (commitmsg)
free(commitmsg);
return json;
}
void pv_storage_set_rev_done(const char *rev)
{
char path[PATH_MAX];
pv_paths_storage_trail_pv_file(path, PATH_MAX, rev, DONE_FNAME);
pv_log(DEBUG, "saving done file for rev %s in %s", rev, path);
if (pv_fs_file_save(path, "", 0644) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
}
bool pv_storage_is_rev_done(const char *rev)
{
char path[PATH_MAX];
pv_paths_storage_trail_pv_file(path, PATH_MAX, rev, DONE_FNAME);
return pv_fs_path_exist(path);
}
void pv_storage_set_rev_progress(const char *rev, const char *progress)
{
if (!rev)
return;
char path[PATH_MAX];
pv_paths_storage_trail_pv_file(path, PATH_MAX, rev, PROGRESS_FNAME);
pv_log(DEBUG, "saving progress file for rev %s in %s: %s", rev, path,
progress);
if (pv_fs_file_save(path, progress, 0644) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
}
char *pv_storage_get_rev_progress(const char *rev)
{
if (!rev)
return NULL;
char path[PATH_MAX];
pv_paths_storage_trail_pv_file(path, PATH_MAX, rev, PROGRESS_FNAME);
pv_log(DEBUG, "loading progress file for rev %s from %s", rev, path);
return pv_fs_file_load(path, UPDATE_PROGRESS_JSON_SIZE);
}
#define PVR_CONFIGF "{\"ObjectsDir\": \"%s/objects\"}"
void pv_storage_init_trail_pvr()
{
struct pantavisor *pv = pv_get_instance();
char path[PATH_MAX], config[PATH_MAX];
struct stat st;
if (!pv || !pv->state)
return;
// check .pvr/config exists
pv_paths_storage_trail_pvr_file(path, PATH_MAX, pv->state->rev,
CONFIG_FNAME);
if (!stat(path, &st))
return;
// save .pvr/config with that links trail contents and system paths
SNPRINTF_WTRUNC(config, PATH_MAX, PVR_CONFIGF,
pv_config_get_str(PV_STORAGE_MNTPOINT));
if (pv_fs_file_save(path, config, 0644) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
}
int pv_storage_meta_expand_jsons(struct pantavisor *pv, struct pv_state *s)
{
int ret = 0;
struct stat st;
char path[PATH_MAX];
char *file = 0, *dir = 0;
if (!pv || !s)
goto out;
struct pv_json *j, *j_tmp;
dl_list_for_each_safe(j, j_tmp, &s->jsons, struct pv_json, list)
{
pv_paths_storage_trail_file(path, PATH_MAX, s->rev, j->name);
// we skip saving the file if it already exists
if (stat(path, &st) == 0)
continue;
file = strdup(path);
dir = dirname(file);
if (stat(dir, &st))
pv_fs_mkdir_p(dir, 0755);
free(file);
pv_log(DEBUG, "saving json %s", j->name);
if (pv_fs_file_save(path, j->value, 0644) < 0)
pv_log(ERROR, "could not save file %s: %s", path,
strerror(errno));
}
ret = 1;
out:
return ret;
}
int pv_storage_meta_link_boot(struct pantavisor *pv, struct pv_state *s)
{
int i;
char src[PATH_MAX], dst[PATH_MAX], fname[PATH_MAX], prefix[PATH_MAX];
struct pv_addon *a, *tmp;
struct dl_list *addons = NULL;
if (!s)
s = pv->state;
if (pv_config_get_system_init_mode() == IM_APPENGINE)
return 0;
/*
* Toggle directory depth with null prefix
*/
switch (pv_state_spec(s)) {
case SPEC_SYSTEM1:
SNPRINTF_WTRUNC(prefix, sizeof(prefix), "bsp/");
break;
case SPEC_MULTI1:
default:
prefix[0] = '\0';
break;
}
// addons
i = 0;
addons = &s->addons;
dl_list_for_each_safe(a, tmp, addons, struct pv_addon, list)
{
SNPRINTF_WTRUNC(fname, sizeof(fname), "pv-initrd.img.%d", i++);
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev, fname);
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev, prefix,
a->name);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
}
pv_fs_mkdir_p(dst, 0755);
if (s->bsp.img.std.initrd) {
// initrd
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"pv-initrd.img");
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev, prefix,
s->bsp.img.std.initrd);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
// kernel
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"pv-kernel.img");
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev, prefix,
s->bsp.img.std.kernel);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
// fdt
if (s->bsp.img.std.fdt) {
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"pv-fdt.dtb");
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev,
prefix,
s->bsp.img.std.fdt);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
}
} else if (s->bsp.img.ut.fit) {
// pantavisor.fit
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"pantavisor.fit");
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev, prefix,
s->bsp.img.ut.fit);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
} else if (s->bsp.img.rpiab.bootimg) {
// rpiboot.img[.gz]
if (!strcmp(s->bsp.img.rpiab.bootimg +
(strlen(s->bsp.img.rpiab.bootimg) - 3),
".gz")) {
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"rpiboot.img.gz");
} else {
pv_paths_storage_trail_pv_file(dst, PATH_MAX, s->rev,
"rpiboot.img");
}
pv_paths_storage_trail_plat_file(src, PATH_MAX, s->rev, prefix,
s->bsp.img.rpiab.bootimg);
pv_log(DEBUG, "installing hardlink of platform file %s to %s",
src, dst);
pv_fs_path_remove(dst, false);
if (link(src, dst) < 0)
goto err;
} else {
pv_log(ERROR,
"bsp type not supported. no std,fit or rpiab boot assets found for rev=%s",
s->rev);
return -1;
}
pv_log(DEBUG, "linked boot assets for rev=%s", s->rev);
return 0;
err:
pv_log(ERROR, "unable to link '%s' to '%s', errno %d", src, dst, errno);
return -1;
}
char *pv_storage_get_state_json(const char *rev)
{
char path[PATH_MAX];
pv_paths_storage_trail_pvr_file(path, PATH_MAX, rev, JSON_FNAME);
pv_log(DEBUG, "reading state from: '%s'", path);
return pv_fs_file_load(path, 0);
}
bool pv_storage_verify_state_json(const char *rev, char *msg,
unsigned int msg_len)
{
bool ret = false;
char *json = NULL;
struct pv_state *state = NULL;
json = pv_storage_get_state_json(rev);
if (!json) {
SNPRINTF_WTRUNC(msg, msg_len,
"Storage: Cannot read state JSON");
pv_log(ERROR, "Could not read state json");
goto out;
}
sign_state_res_t sres;
sres = pv_signature_verify(json);
if (sres != SIGN_STATE_OK) {
SNPRINTF_WTRUNC(msg, msg_len, "Secureboot: %s",
pv_signature_sign_state_str(sres));
pv_log(ERROR, "Could not verify state json signatures");
goto out;
}
state = pv_parser_get_state(json, rev);
if (!state) {
SNPRINTF_WTRUNC(msg, msg_len,
"Parser: State JSON has bad format");
pv_log(ERROR, "Could not verify state json format");
goto out;
}
ret = true;
out:
if (json)
free(json);
if (state)
pv_state_free(state);
return ret;
}
void pv_storage_save_usermeta(const char *key, const char *value)
{
char path[PATH_MAX];
char *pname, *pkey;