/
database.c
2925 lines (2514 loc) · 77.9 KB
/
database.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
/* database.c - Alpine Package Keeper (APK)
*
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation. See http://www.gnu.org/ for details.
*/
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <mntent.h>
#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fnmatch.h>
#include <sys/vfs.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/statvfs.h>
#include <linux/magic.h>
#include "apk_defines.h"
#include "apk_package.h"
#include "apk_database.h"
#include "apk_applet.h"
#include "apk_archive.h"
#include "apk_print.h"
static const apk_spn_match_def apk_spn_repo_separators = {
[4] = (1<<0) /* */,
[7] = (1<<2) /*:*/,
};
enum {
APK_DIR_FREE = 0,
APK_DIR_REMOVE
};
int apk_verbosity = 1;
unsigned int apk_flags = 0, apk_force = 0;
static apk_blob_t tmpprefix = { .len=8, .ptr = ".apknew." };
static const char * const apkindex_tar_gz = "APKINDEX.tar.gz";
static const char * const apk_static_cache_dir = "var/cache/apk";
static const char * const apk_world_file = "etc/apk/world";
static const char * const apk_world_file_tmp = "etc/apk/world.new";
static const char * const apk_arch_file = "etc/apk/arch";
static const char * const apk_lock_file = "lib/apk/db/lock";
static const char * const apk_scripts_file = "lib/apk/db/scripts.tar";
static const char * const apk_scripts_file_tmp = "lib/apk/db/scripts.tar.new";
static const char * const apk_triggers_file = "lib/apk/db/triggers";
static const char * const apk_triggers_file_tmp = "lib/apk/db/triggers.new";
const char * const apk_installed_file = "lib/apk/db/installed";
static const char * const apk_installed_file_tmp = "lib/apk/db/installed.new";
static struct apk_db_acl *apk_default_acl_dir, *apk_default_acl_file;
struct install_ctx {
struct apk_database *db;
struct apk_package *pkg;
struct apk_installed_package *ipkg;
int script;
char **script_args;
int script_pending : 1;
struct apk_db_dir_instance *diri;
struct apk_checksum data_csum;
struct apk_sign_ctx sctx;
apk_progress_cb cb;
void *cb_ctx;
size_t installed_size;
size_t current_file_size;
struct hlist_node **diri_node;
struct hlist_node **file_diri_node;
};
static apk_blob_t pkg_name_get_key(apk_hash_item item)
{
return APK_BLOB_STR(((struct apk_name *) item)->name);
}
static void pkg_name_free(struct apk_name *name)
{
free(name->name);
apk_provider_array_free(&name->providers);
apk_name_array_free(&name->rdepends);
apk_name_array_free(&name->rinstall_if);
free(name);
}
static const struct apk_hash_ops pkg_name_hash_ops = {
.node_offset = offsetof(struct apk_name, hash_node),
.get_key = pkg_name_get_key,
.hash_key = apk_blob_hash,
.compare = apk_blob_compare,
.delete_item = (apk_hash_delete_f) pkg_name_free,
};
static apk_blob_t pkg_info_get_key(apk_hash_item item)
{
return APK_BLOB_CSUM(((struct apk_package *) item)->csum);
}
static unsigned long csum_hash(apk_blob_t csum)
{
/* Checksum's highest bits have the most "randomness", use that
* directly as hash */
return *(unsigned long *) csum.ptr;
}
static const struct apk_hash_ops pkg_info_hash_ops = {
.node_offset = offsetof(struct apk_package, hash_node),
.get_key = pkg_info_get_key,
.hash_key = csum_hash,
.compare = apk_blob_compare,
.delete_item = (apk_hash_delete_f) apk_pkg_free,
};
static apk_blob_t apk_db_dir_get_key(apk_hash_item item)
{
struct apk_db_dir *dir = (struct apk_db_dir *) item;
return APK_BLOB_PTR_LEN(dir->name, dir->namelen);
}
static const struct apk_hash_ops dir_hash_ops = {
.node_offset = offsetof(struct apk_db_dir, hash_node),
.get_key = apk_db_dir_get_key,
.hash_key = apk_blob_hash,
.compare = apk_blob_compare,
.delete_item = (apk_hash_delete_f) free,
};
struct apk_db_file_hash_key {
apk_blob_t dirname;
apk_blob_t filename;
};
static unsigned long apk_db_file_hash_key(apk_blob_t _key)
{
struct apk_db_file_hash_key *key = (struct apk_db_file_hash_key *) _key.ptr;
return apk_blob_hash_seed(key->filename, apk_blob_hash(key->dirname));
}
static unsigned long apk_db_file_hash_item(apk_hash_item item)
{
struct apk_db_file *dbf = (struct apk_db_file *) item;
return apk_blob_hash_seed(APK_BLOB_PTR_LEN(dbf->name, dbf->namelen),
dbf->diri->dir->hash);
}
static int apk_db_file_compare_item(apk_hash_item item, apk_blob_t _key)
{
struct apk_db_file *dbf = (struct apk_db_file *) item;
struct apk_db_file_hash_key *key = (struct apk_db_file_hash_key *) _key.ptr;
struct apk_db_dir *dir = dbf->diri->dir;
int r;
r = apk_blob_compare(key->filename,
APK_BLOB_PTR_LEN(dbf->name, dbf->namelen));
if (r != 0)
return r;
r = apk_blob_compare(key->dirname,
APK_BLOB_PTR_LEN(dir->name, dir->namelen));
return r;
}
static const struct apk_hash_ops file_hash_ops = {
.node_offset = offsetof(struct apk_db_file, hash_node),
.hash_key = apk_db_file_hash_key,
.hash_item = apk_db_file_hash_item,
.compare_item = apk_db_file_compare_item,
.delete_item = (apk_hash_delete_f) free,
};
struct apk_name *apk_db_query_name(struct apk_database *db, apk_blob_t name)
{
return (struct apk_name *) apk_hash_get(&db->available.names, name);
}
struct apk_name *apk_db_get_name(struct apk_database *db, apk_blob_t name)
{
struct apk_name *pn;
unsigned long hash = apk_hash_from_key(&db->available.names, name);
pn = (struct apk_name *) apk_hash_get_hashed(&db->available.names, name, hash);
if (pn != NULL)
return pn;
pn = calloc(1, sizeof(struct apk_name));
if (pn == NULL)
return NULL;
pn->name = apk_blob_cstr(name);
apk_provider_array_init(&pn->providers);
apk_name_array_init(&pn->rdepends);
apk_name_array_init(&pn->rinstall_if);
apk_hash_insert_hashed(&db->available.names, pn, hash);
return pn;
}
static struct apk_db_acl *apk_db_acl_atomize(mode_t mode, uid_t uid, gid_t gid, const struct apk_checksum *xattr_csum)
{
struct apk_db_acl acl = { .mode = mode & 07777, .uid = uid, .gid = gid };
apk_blob_t *b;
if (xattr_csum && xattr_csum->type != APK_CHECKSUM_NONE)
acl.xattr_csum = *xattr_csum;
b = apk_blob_atomize_dup(APK_BLOB_STRUCT(acl));
return (struct apk_db_acl *) b->ptr;
}
static void apk_db_dir_prepare(struct apk_database *db, struct apk_db_dir *dir, mode_t newmode)
{
struct stat st;
if (dir->namelen == 0) return;
if (dir->created) return;
if (fstatat(db->root_fd, dir->name, &st, AT_SYMLINK_NOFOLLOW) == 0) {
/* If directory exists and stats match what we expect,
* then we can allow auto updating the permissions */
dir->created = 1;
dir->update_permissions |=
(st.st_mode & 07777) == (dir->mode & 07777) &&
st.st_uid == dir->uid && st.st_gid == dir->gid;
} else if (newmode) {
if (!(apk_flags & APK_SIMULATE))
mkdirat(db->root_fd, dir->name, newmode);
dir->created = 1;
dir->update_permissions = 1;
}
}
void apk_db_dir_unref(struct apk_database *db, struct apk_db_dir *dir, int rmdir_mode)
{
if (--dir->refs > 0) return;
db->installed.stats.dirs--;
apk_protected_path_array_free(&dir->protected_paths);
if (dir->namelen != 0) {
if (rmdir_mode == APK_DIR_REMOVE) {
dir->modified = 1;
if (!(apk_flags & APK_SIMULATE) &&
unlinkat(db->root_fd, dir->name, AT_REMOVEDIR) != 0)
;
}
apk_db_dir_unref(db, dir->parent, rmdir_mode);
dir->parent = NULL;
}
dir->seen = dir->created = dir->update_permissions = 0;
}
struct apk_db_dir *apk_db_dir_ref(struct apk_db_dir *dir)
{
dir->refs++;
return dir;
}
struct apk_db_dir *apk_db_dir_query(struct apk_database *db,
apk_blob_t name)
{
return (struct apk_db_dir *) apk_hash_get(&db->installed.dirs, name);
}
struct apk_db_dir *apk_db_dir_get(struct apk_database *db, apk_blob_t name)
{
struct apk_db_dir *dir;
struct apk_protected_path_array *ppaths;
struct apk_protected_path *ppath;
apk_blob_t bparent;
unsigned long hash = apk_hash_from_key(&db->installed.dirs, name);
char *relative_name;
if (name.len && name.ptr[name.len-1] == '/') name.len--;
dir = (struct apk_db_dir *) apk_hash_get_hashed(&db->installed.dirs, name, hash);
if (dir != NULL && dir->refs) return apk_db_dir_ref(dir);
if (dir == NULL) {
dir = calloc(1, sizeof(*dir) + name.len + 1);
dir->rooted_name[0] = '/';
memcpy(dir->name, name.ptr, name.len);
dir->name[name.len] = 0;
dir->namelen = name.len;
dir->hash = hash;
apk_protected_path_array_init(&dir->protected_paths);
apk_hash_insert_hashed(&db->installed.dirs, dir, hash);
}
db->installed.stats.dirs++;
dir->refs = 1;
dir->uid = (uid_t) -1;
dir->gid = (gid_t) -1;
if (name.len == 0) {
dir->parent = NULL;
dir->has_protected_children = 1;
ppaths = NULL;
} else if (apk_blob_rsplit(name, '/', &bparent, NULL)) {
dir->parent = apk_db_dir_get(db, bparent);
dir->protect_mode = dir->parent->protect_mode;
dir->has_protected_children = (dir->protect_mode != APK_PROTECT_NONE);
ppaths = dir->parent->protected_paths;
} else {
dir->parent = apk_db_dir_get(db, APK_BLOB_NULL);
ppaths = db->protected_paths;
}
if (ppaths == NULL)
return dir;
relative_name = strrchr(dir->rooted_name, '/') + 1;
foreach_array_item(ppath, ppaths) {
char *slash = strchr(ppath->relative_pattern, '/');
if (slash != NULL) {
*slash = 0;
if (fnmatch(ppath->relative_pattern, relative_name, FNM_PATHNAME) != 0) {
*slash = '/';
continue;
}
*slash = '/';
*apk_protected_path_array_add(&dir->protected_paths) = (struct apk_protected_path) {
.relative_pattern = slash + 1,
.protect_mode = ppath->protect_mode,
};
} else {
if (fnmatch(ppath->relative_pattern, relative_name, FNM_PATHNAME) != 0)
continue;
dir->protect_mode = ppath->protect_mode;
}
dir->has_protected_children |= (ppath->protect_mode != APK_PROTECT_NONE);
}
return dir;
}
static struct apk_db_dir_instance *apk_db_diri_new(struct apk_database *db,
struct apk_package *pkg,
apk_blob_t name,
struct hlist_node ***after)
{
struct apk_db_dir_instance *diri;
diri = calloc(1, sizeof(struct apk_db_dir_instance));
if (diri != NULL) {
hlist_add_after(&diri->pkg_dirs_list, *after);
*after = &diri->pkg_dirs_list.next;
diri->dir = apk_db_dir_get(db, name);
diri->pkg = pkg;
diri->acl = apk_default_acl_dir;
}
return diri;
}
static void apk_db_dir_apply_diri_permissions(struct apk_db_dir_instance *diri)
{
struct apk_db_dir *dir = diri->dir;
struct apk_db_acl *acl = diri->acl;
if (acl->uid < dir->uid || (acl->uid == dir->uid && acl->gid < dir->gid)) {
dir->uid = acl->uid;
dir->gid = acl->gid;
dir->mode = acl->mode;
} else if (acl->uid == dir->uid && acl->gid == dir->gid) {
dir->mode &= acl->mode;
}
}
static void apk_db_diri_set(struct apk_db_dir_instance *diri, struct apk_db_acl *acl)
{
diri->acl = acl;
apk_db_dir_apply_diri_permissions(diri);
}
static void apk_db_diri_free(struct apk_database *db,
struct apk_db_dir_instance *diri,
int rmdir_mode)
{
struct apk_db_dir *dir = diri->dir;
if (rmdir_mode == APK_DIR_REMOVE)
apk_db_dir_prepare(db, diri->dir, 0);
apk_db_dir_unref(db, dir, rmdir_mode);
free(diri);
}
struct apk_db_file *apk_db_file_query(struct apk_database *db,
apk_blob_t dir,
apk_blob_t name)
{
struct apk_db_file_hash_key key;
if (dir.len && dir.ptr[dir.len-1] == '/')
dir.len--;
key = (struct apk_db_file_hash_key) {
.dirname = dir,
.filename = name,
};
return (struct apk_db_file *) apk_hash_get(&db->installed.files,
APK_BLOB_BUF(&key));
}
static struct apk_db_file *apk_db_file_new(struct apk_db_dir_instance *diri,
apk_blob_t name,
struct hlist_node ***after)
{
struct apk_db_file *file;
file = malloc(sizeof(*file) + name.len + 1);
if (file == NULL)
return NULL;
memset(file, 0, sizeof(*file));
memcpy(file->name, name.ptr, name.len);
file->name[name.len] = 0;
file->namelen = name.len;
file->diri = diri;
file->acl = apk_default_acl_file;
hlist_add_after(&file->diri_files_list, *after);
*after = &file->diri_files_list.next;
return file;
}
static struct apk_db_file *apk_db_file_get(struct apk_database *db,
struct apk_db_dir_instance *diri,
apk_blob_t name,
struct hlist_node ***after)
{
struct apk_db_file *file;
struct apk_db_file_hash_key key;
struct apk_db_dir *dir = diri->dir;
unsigned long hash;
key = (struct apk_db_file_hash_key) {
.dirname = APK_BLOB_PTR_LEN(dir->name, dir->namelen),
.filename = name,
};
hash = apk_blob_hash_seed(name, dir->hash);
file = (struct apk_db_file *) apk_hash_get_hashed(
&db->installed.files, APK_BLOB_BUF(&key), hash);
if (file != NULL)
return file;
file = apk_db_file_new(diri, name, after);
apk_hash_insert_hashed(&db->installed.files, file, hash);
db->installed.stats.files++;
return file;
}
static void apk_db_pkg_rdepends(struct apk_database *db, struct apk_package *pkg)
{
struct apk_name *rname, **rd;
struct apk_dependency *d;
foreach_array_item(d, pkg->depends) {
rname = d->name;
rname->is_dependency |= !d->conflict;
foreach_array_item(rd, rname->rdepends)
if (*rd == pkg->name)
goto rdeps_done;
*apk_name_array_add(&rname->rdepends) = pkg->name;
rdeps_done: ;
}
foreach_array_item(d, pkg->install_if) {
rname = d->name;
foreach_array_item(rd, rname->rinstall_if)
if (*rd == pkg->name)
goto riif_done;
*apk_name_array_add(&rname->rinstall_if) = pkg->name;
riif_done: ;
}
return;
}
static inline void add_provider(struct apk_name *name, struct apk_provider p)
{
*apk_provider_array_add(&name->providers) = p;
}
struct apk_package *apk_db_pkg_add(struct apk_database *db, struct apk_package *pkg)
{
struct apk_package *idb;
struct apk_dependency *dep;
if (pkg->license == NULL)
pkg->license = apk_blob_atomize(APK_BLOB_NULL);
/* Set as "cached" if installing from specified file, and
* for virtual packages */
if (pkg->filename != NULL || pkg->installed_size == 0)
pkg->repos |= BIT(APK_REPOSITORY_CACHED);
idb = apk_hash_get(&db->available.packages, APK_BLOB_CSUM(pkg->csum));
if (idb == NULL) {
idb = pkg;
apk_hash_insert(&db->available.packages, pkg);
add_provider(pkg->name, APK_PROVIDER_FROM_PACKAGE(pkg));
foreach_array_item(dep, pkg->provides)
add_provider(dep->name, APK_PROVIDER_FROM_PROVIDES(pkg, dep));
if (db->open_complete)
apk_db_pkg_rdepends(db, pkg);
} else {
idb->repos |= pkg->repos;
if (idb->filename == NULL && pkg->filename != NULL) {
idb->filename = pkg->filename;
pkg->filename = NULL;
}
if (idb->ipkg == NULL && pkg->ipkg != NULL) {
idb->ipkg = pkg->ipkg;
idb->ipkg->pkg = idb;
pkg->ipkg = NULL;
}
apk_pkg_free(pkg);
}
return idb;
}
static int apk_pkg_format_cache_pkg(apk_blob_t to, struct apk_package *pkg)
{
/* pkgname-1.0_alpha1.12345678.apk */
apk_blob_push_blob(&to, APK_BLOB_STR(pkg->name->name));
apk_blob_push_blob(&to, APK_BLOB_STR("-"));
apk_blob_push_blob(&to, *pkg->version);
apk_blob_push_blob(&to, APK_BLOB_STR("."));
apk_blob_push_hexdump(&to, APK_BLOB_PTR_LEN((char *) pkg->csum.data,
APK_CACHE_CSUM_BYTES));
apk_blob_push_blob(&to, APK_BLOB_STR(".apk"));
apk_blob_push_blob(&to, APK_BLOB_PTR_LEN("", 1));
if (APK_BLOB_IS_NULL(to))
return -ENOBUFS;
return 0;
}
int apk_repo_format_cache_index(apk_blob_t to, struct apk_repository *repo)
{
/* APKINDEX.12345678.tar.gz */
apk_blob_push_blob(&to, APK_BLOB_STR("APKINDEX."));
apk_blob_push_hexdump(&to, APK_BLOB_PTR_LEN((char *) repo->csum.data, APK_CACHE_CSUM_BYTES));
apk_blob_push_blob(&to, APK_BLOB_STR(".tar.gz"));
apk_blob_push_blob(&to, APK_BLOB_PTR_LEN("", 1));
if (APK_BLOB_IS_NULL(to))
return -ENOBUFS;
return 0;
}
int apk_repo_format_real_url(struct apk_database *db, struct apk_repository *repo,
struct apk_package *pkg, char *buf, size_t len)
{
apk_blob_t arch;
int r;
if (pkg && pkg->arch) arch = *pkg->arch;
else arch = *db->arch;
if (pkg != NULL)
r = snprintf(buf, len, "%s%s" BLOB_FMT "/" PKG_FILE_FMT,
repo->url, repo->url[strlen(repo->url)-1] == '/' ? "" : "/",
BLOB_PRINTF(arch), PKG_FILE_PRINTF(pkg));
else
r = snprintf(buf, len, "%s%s" BLOB_FMT "/%s",
repo->url, repo->url[strlen(repo->url)-1] == '/' ? "" : "/",
BLOB_PRINTF(arch), apkindex_tar_gz);
if (r >= len)
return -ENOBUFS;
return 0;
}
int apk_repo_format_item(struct apk_database *db, struct apk_repository *repo, struct apk_package *pkg,
int *fd, char *buf, size_t len)
{
if (repo->url == db->repos[APK_REPOSITORY_CACHED].url) {
*fd = db->cache_fd;
return apk_pkg_format_cache_pkg(APK_BLOB_PTR_LEN(buf, len), pkg);
} else {
*fd = AT_FDCWD;
return apk_repo_format_real_url(db, repo, pkg, buf, len);
}
}
int apk_cache_download(struct apk_database *db, struct apk_repository *repo,
struct apk_package *pkg, int verify, int autoupdate,
apk_progress_cb cb, void *cb_ctx)
{
struct stat st = {0};
struct apk_istream *is;
struct apk_bstream *bs;
struct apk_sign_ctx sctx;
char url[PATH_MAX];
char tmpcacheitem[128], *cacheitem = &tmpcacheitem[tmpprefix.len];
apk_blob_t b = APK_BLOB_BUF(tmpcacheitem);
int r, fd;
time_t now = time(NULL);
apk_blob_push_blob(&b, tmpprefix);
if (pkg != NULL)
r = apk_pkg_format_cache_pkg(b, pkg);
else
r = apk_repo_format_cache_index(b, repo);
if (r < 0) return r;
r = apk_repo_format_real_url(db, repo, pkg, url, sizeof(url));
if (r < 0) return r;
if (autoupdate && !(apk_force & APK_FORCE_REFRESH)) {
if (fstatat(db->cache_fd, cacheitem, &st, 0) == 0 &&
now - st.st_mtime <= db->cache_max_age)
return -EALREADY;
}
apk_message("fetch %s", url);
if (apk_flags & APK_SIMULATE) return 0;
if (cb) cb(cb_ctx, 0);
if (verify != APK_SIGN_NONE) {
apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL, db->keys_fd);
bs = apk_bstream_from_url_if_modified(url, st.st_mtime);
bs = apk_bstream_tee(bs, db->cache_fd, tmpcacheitem, !autoupdate, cb, cb_ctx);
is = apk_bstream_gunzip_mpart(bs, apk_sign_ctx_mpart_cb, &sctx);
if (!IS_ERR_OR_NULL(is))
r = apk_tar_parse(is, apk_sign_ctx_verify_tar, &sctx, FALSE, &db->id_cache);
else
r = PTR_ERR(is) ?: -EIO;
apk_sign_ctx_free(&sctx);
} else {
is = apk_istream_from_url_if_modified(url, st.st_mtime);
if (!IS_ERR_OR_NULL(is)) {
fd = openat(db->cache_fd, tmpcacheitem, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0) r = -errno;
} else fd = -1, r = PTR_ERR(is) ?: -EIO;
if (fd >= 0) {
struct apk_file_meta meta;
r = apk_istream_splice(is, fd, APK_SPLICE_ALL, cb, cb_ctx);
if (!autoupdate) {
apk_istream_get_meta(is, &meta);
apk_file_meta_to_fd(fd, &meta);
}
close(fd);
}
}
if (!IS_ERR_OR_NULL(is)) apk_istream_close(is);
if (r == -EALREADY) {
if (autoupdate) utimensat(db->cache_fd, cacheitem, NULL, 0);
return r;
}
if (r < 0) {
unlinkat(db->cache_fd, tmpcacheitem, 0);
return r;
}
if (renameat(db->cache_fd, tmpcacheitem, db->cache_fd, cacheitem) < 0)
return -errno;
return 0;
}
static struct apk_db_dir_instance *find_diri(struct apk_installed_package *ipkg,
apk_blob_t dirname,
struct apk_db_dir_instance *curdiri,
struct hlist_node ***tail)
{
struct hlist_node *n;
struct apk_db_dir_instance *diri;
if (curdiri != NULL &&
apk_blob_compare(APK_BLOB_PTR_LEN(curdiri->dir->name,
curdiri->dir->namelen),
dirname) == 0)
return curdiri;
hlist_for_each_entry(diri, n, &ipkg->owned_dirs, pkg_dirs_list) {
if (apk_blob_compare(APK_BLOB_PTR_LEN(diri->dir->name,
diri->dir->namelen), dirname) == 0) {
if (tail != NULL)
*tail = hlist_tail_ptr(&diri->owned_files);
return diri;
}
}
return NULL;
}
int apk_db_read_overlay(struct apk_database *db, struct apk_bstream *bs)
{
struct apk_db_dir_instance *diri = NULL;
struct hlist_node **diri_node = NULL, **file_diri_node = NULL;
struct apk_package *pkg;
struct apk_installed_package *ipkg;
apk_blob_t token = APK_BLOB_STR("\n"), line, bdir, bfile;
if (IS_ERR_OR_NULL(bs)) return -1;
pkg = apk_pkg_new();
if (pkg == NULL)
return -1;
ipkg = apk_pkg_install(db, pkg);
if (ipkg == NULL)
return -1;
diri_node = hlist_tail_ptr(&ipkg->owned_dirs);
while (!APK_BLOB_IS_NULL(line = apk_bstream_read(bs, token))) {
if (!apk_blob_rsplit(line, '/', &bdir, &bfile))
break;
if (bfile.len == 0) {
diri = apk_db_diri_new(db, pkg, bdir, &diri_node);
file_diri_node = &diri->owned_files.first;
diri->dir->created = 1;
} else {
diri = find_diri(ipkg, bdir, diri, &file_diri_node);
if (diri == NULL) {
diri = apk_db_diri_new(db, pkg, bdir, &diri_node);
file_diri_node = &diri->owned_files.first;
}
(void) apk_db_file_get(db, diri, bfile, &file_diri_node);
}
}
return 0;
}
int apk_db_index_read(struct apk_database *db, struct apk_bstream *bs, int repo)
{
struct apk_package *pkg = NULL;
struct apk_installed_package *ipkg = NULL;
struct apk_db_dir_instance *diri = NULL;
struct apk_db_file *file = NULL;
struct apk_db_acl *acl;
struct hlist_node **diri_node = NULL;
struct hlist_node **file_diri_node = NULL;
struct apk_checksum xattr_csum;
apk_blob_t token = APK_BLOB_STR("\n"), l;
mode_t mode;
uid_t uid;
gid_t gid;
int field, r, lineno = 0;
while (!APK_BLOB_IS_NULL(l = apk_bstream_read(bs, token))) {
lineno++;
if (l.len < 2 || l.ptr[1] != ':') {
if (pkg == NULL)
continue;
if (diri) apk_db_dir_apply_diri_permissions(diri);
if (repo >= 0) {
pkg->repos |= BIT(repo);
} else if (repo == -2) {
pkg->cached_non_repository = 1;
} else if (repo == -1 && ipkg == NULL) {
/* Installed package without files */
ipkg = apk_pkg_install(db, pkg);
}
if (apk_db_pkg_add(db, pkg) == NULL) {
apk_error("Installed database load failed");
return -1;
}
pkg = NULL;
ipkg = NULL;
continue;
}
/* Get field */
field = l.ptr[0];
l.ptr += 2;
l.len -= 2;
/* If no package, create new */
if (pkg == NULL) {
pkg = apk_pkg_new();
ipkg = NULL;
diri = NULL;
file_diri_node = NULL;
}
/* Standard index line? */
r = apk_pkg_add_info(db, pkg, field, l);
if (r == 0)
continue;
if (r == 1 && repo == -1 && ipkg == NULL) {
/* Instert to installed database; this needs to
* happen after package name has been read, but
* before first FDB entry. */
ipkg = apk_pkg_install(db, pkg);
diri_node = hlist_tail_ptr(&ipkg->owned_dirs);
}
if (repo != -1 || ipkg == NULL)
continue;
/* Check FDB special entries */
switch (field) {
case 'F':
if (diri) apk_db_dir_apply_diri_permissions(diri);
if (pkg->name == NULL) goto bad_entry;
diri = apk_db_diri_new(db, pkg, l, &diri_node);
file_diri_node = &diri->owned_files.first;
break;
case 'a':
if (file == NULL) goto bad_entry;
case 'M':
if (diri == NULL) goto bad_entry;
uid = apk_blob_pull_uint(&l, 10);
apk_blob_pull_char(&l, ':');
gid = apk_blob_pull_uint(&l, 10);
apk_blob_pull_char(&l, ':');
mode = apk_blob_pull_uint(&l, 8);
if (apk_blob_pull_blob_match(&l, APK_BLOB_STR(":")))
apk_blob_pull_csum(&l, &xattr_csum);
else
xattr_csum.type = APK_CHECKSUM_NONE;
acl = apk_db_acl_atomize(mode, uid, gid, &xattr_csum);
if (field == 'M')
diri->acl = acl;
else
file->acl = acl;
break;
case 'R':
if (diri == NULL) goto bad_entry;
file = apk_db_file_get(db, diri, l, &file_diri_node);
break;
case 'Z':
if (file == NULL) goto bad_entry;
apk_blob_pull_csum(&l, &file->csum);
break;
case 'r':
apk_blob_pull_deps(&l, db, &ipkg->replaces);
break;
case 'q':
ipkg->replaces_priority = apk_blob_pull_uint(&l, 10);
break;
case 's':
ipkg->repository_tag = apk_db_get_tag_id(db, l);
break;
case 'f':
for (r = 0; r < l.len; r++) {
switch (l.ptr[r]) {
case 'f': ipkg->broken_files = 1; break;
case 's': ipkg->broken_script = 1; break;
case 'x': ipkg->broken_xattr = 1; break;
default:
if (!(apk_force & APK_FORCE_OLD_APK))
goto old_apk_tools;
}
}
break;
default:
if (r != 0 && !(apk_force & APK_FORCE_OLD_APK))
goto old_apk_tools;
/* Installed. So mark the package as installable. */
pkg->filename = NULL;
continue;
}
if (APK_BLOB_IS_NULL(l)) goto bad_entry;
}
return 0;
old_apk_tools:
/* Installed db should not have unsupported fields */
apk_error("This apk-tools is too old to handle installed packages");
return -1;
bad_entry:
apk_error("FDB format error (line %d, entry '%c')", lineno, field);
return -1;
}
static void apk_blob_push_db_acl(apk_blob_t *b, char field, struct apk_db_acl *acl)
{
char hdr[2] = { field, ':' };
apk_blob_push_blob(b, APK_BLOB_BUF(hdr));
apk_blob_push_uint(b, acl->uid, 10);
apk_blob_push_blob(b, APK_BLOB_STR(":"));
apk_blob_push_uint(b, acl->gid, 10);
apk_blob_push_blob(b, APK_BLOB_STR(":"));
apk_blob_push_uint(b, acl->mode, 8);
if (acl->xattr_csum.type != APK_CHECKSUM_NONE) {
apk_blob_push_blob(b, APK_BLOB_STR(":"));
apk_blob_push_csum(b, &acl->xattr_csum);
}
apk_blob_push_blob(b, APK_BLOB_STR("\n"));
}
static int apk_db_write_fdb(struct apk_database *db, struct apk_ostream *os)
{
struct apk_installed_package *ipkg;
struct apk_package *pkg;
struct apk_db_dir_instance *diri;
struct apk_db_file *file;
struct hlist_node *c1, *c2;
char buf[1024];
apk_blob_t bbuf = APK_BLOB_BUF(buf);
int r;
list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
pkg = ipkg->pkg;
r = apk_pkg_write_index_entry(pkg, os);
if (r < 0)
return r;
if (ipkg->replaces->num) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("r:"));
apk_blob_push_deps(&bbuf, db, ipkg->replaces);
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
}
if (ipkg->replaces_priority) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("q:"));
apk_blob_push_uint(&bbuf, ipkg->replaces_priority, 10);
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
}
if (ipkg->repository_tag) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("s:"));
apk_blob_push_blob(&bbuf, db->repo_tags[ipkg->repository_tag].plain_name);
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
}
if (ipkg->broken_files || ipkg->broken_script || ipkg->broken_xattr) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("f:"));
if (ipkg->broken_files)
apk_blob_push_blob(&bbuf, APK_BLOB_STR("f"));
if (ipkg->broken_script)
apk_blob_push_blob(&bbuf, APK_BLOB_STR("s"));
if (ipkg->broken_xattr)
apk_blob_push_blob(&bbuf, APK_BLOB_STR("x"));
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
}
hlist_for_each_entry(diri, c1, &ipkg->owned_dirs, pkg_dirs_list) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("F:"));
apk_blob_push_blob(&bbuf, APK_BLOB_PTR_LEN(diri->dir->name, diri->dir->namelen));
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
if (diri->acl != apk_default_acl_dir)
apk_blob_push_db_acl(&bbuf, 'M', diri->acl);
hlist_for_each_entry(file, c2, &diri->owned_files, diri_files_list) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("R:"));
apk_blob_push_blob(&bbuf, APK_BLOB_PTR_LEN(file->name, file->namelen));
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
if (file->acl != apk_default_acl_file)
apk_blob_push_db_acl(&bbuf, 'a', file->acl);
if (file->csum.type != APK_CHECKSUM_NONE) {
apk_blob_push_blob(&bbuf, APK_BLOB_STR("Z:"));
apk_blob_push_csum(&bbuf, &file->csum);
apk_blob_push_blob(&bbuf, APK_BLOB_STR("\n"));
}
if (apk_ostream_write(os, buf, bbuf.ptr - buf) != bbuf.ptr - buf)
return -EIO;
bbuf = APK_BLOB_BUF(buf);
}
if (apk_ostream_write(os, buf, bbuf.ptr - buf) != bbuf.ptr - buf)
return -EIO;
bbuf = APK_BLOB_BUF(buf);
}
apk_ostream_write(os, "\n", 1);
}
return 0;
}
static int apk_db_scriptdb_write(struct apk_database *db, struct apk_ostream *os)
{
struct apk_installed_package *ipkg;
struct apk_package *pkg;