-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpmxdb.c
1178 lines (1089 loc) · 32 KB
/
rpmxdb.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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <endian.h>
#include "rpmxdb.h"
#define RPMRC_OK 0
#define RPMRC_NOTFOUND 1
#define RPMRC_FAIL 2
typedef struct rpmxdb_s {
rpmpkgdb pkgdb; /* master database */
char *filename;
int fd;
int flags;
int mode;
int rdonly;
unsigned int pagesize;
unsigned int generation;
unsigned int slotnpages;
unsigned int usergeneration;
unsigned char *mapped;
unsigned int mappedlen;
struct xdb_slot {
unsigned int slotno;
unsigned int blobtag;
unsigned int subtag;
unsigned char *mapped;
int mapflags;
unsigned int startpage;
unsigned int pagecnt;
void (*mapcallback)(rpmxdb xdb, void *data, void *newaddr, size_t newsize);
void *mapcallbackdata;
unsigned int next;
unsigned int prev;
} *slots;
unsigned int nslots;
unsigned int firstfree;
unsigned int usedblobpages;
unsigned int systempagesize;
int dofsync;
} *rpmxdb;
static inline void h2le(unsigned int x, unsigned char *p)
{
p[0] = x;
p[1] = x >> 8;
p[2] = x >> 16;
p[3] = x >> 24;
}
/* aligned versions */
static inline unsigned int le2ha(unsigned char *p)
{
unsigned int x = *(unsigned int *)p;
return le32toh(x);
}
static inline void h2lea(unsigned int x, unsigned char *p)
{
*(unsigned int *)p = htole32(x);
}
#define XDB_MAGIC ('R' | 'p' << 8 | 'm' << 16 | 'X' << 24)
#define XDB_VERSION 0
#define XDB_OFFSET_MAGIC 0
#define XDB_OFFSET_VERSION 4
#define XDB_OFFSET_GENERATION 8
#define XDB_OFFSET_SLOTNPAGES 12
#define XDB_OFFSET_PAGESIZE 16
#define XDB_OFFSET_USERGENERATION 20
/* must be multiple of SLOT_SIZE */
#define XDB_HEADER_SIZE 32
#define SLOT_MAGIC ('S' | 'l' << 8 | 'o' << 16)
#define SLOT_SIZE 16
#define SLOT_START (XDB_HEADER_SIZE / SLOT_SIZE)
static void rpmxdbUnmap(rpmxdb xdb)
{
munmap(xdb->mapped, xdb->mappedlen);
xdb->mapped = 0;
xdb->mappedlen = 0;
}
/* slot mapping functions */
static int mapslot(rpmxdb xdb, struct xdb_slot *slot)
{
void *mapped;
size_t off, size, shift;
if (slot->mapped)
return RPMRC_FAIL;
size = slot->pagecnt * xdb->pagesize;
off = slot->startpage * xdb->pagesize;
shift = 0;
if (xdb->pagesize != xdb->systempagesize) {
shift = off & (xdb->systempagesize - 1);
off -= shift;
size += shift;
size = (size + xdb->systempagesize - 1) & (xdb->systempagesize - 1);
}
mapped = mmap(0, size, slot->mapflags, MAP_SHARED, xdb->fd, off);
if (mapped == MAP_FAILED)
return RPMRC_FAIL;
slot->mapped = (unsigned char *)mapped + shift;
return RPMRC_OK;
}
static void unmapslot(rpmxdb xdb, struct xdb_slot *slot)
{
size_t size;
unsigned char *mapped = slot->mapped;
if (!mapped)
return;
size = slot->pagecnt * xdb->pagesize;
if (xdb->pagesize != xdb->systempagesize) {
size_t off = slot->startpage * xdb->pagesize;
size_t shift = off & (xdb->systempagesize - 1);
mapped -= shift;
size += shift;
size = (size + xdb->systempagesize - 1) & (xdb->systempagesize - 1);
}
munmap(mapped, size);
slot->mapped = 0;
}
static int remapslot(rpmxdb xdb, struct xdb_slot *slot, unsigned int newpagecnt)
{
void *mapped;
size_t off, oldsize, newsize, shift;
oldsize = slot->pagecnt * xdb->pagesize;
newsize = newpagecnt * xdb->pagesize;
off = slot->startpage * xdb->pagesize;
shift = 0;
if (xdb->pagesize != xdb->systempagesize) {
off = slot->startpage * xdb->pagesize;
shift = off & (xdb->systempagesize - 1);
off -= shift;
oldsize += shift;
oldsize = (oldsize + xdb->systempagesize - 1) & (xdb->systempagesize - 1);
newsize += shift;
newsize = (newsize + xdb->systempagesize - 1) & (xdb->systempagesize - 1);
}
if (slot->mapped)
mapped = mremap(slot->mapped - shift, oldsize, newsize, MREMAP_MAYMOVE);
else
mapped = mmap(0, newsize, slot->mapflags, MAP_SHARED, xdb->fd, off);
if (mapped == MAP_FAILED)
return RPMRC_FAIL;
slot->mapped = (unsigned char *)mapped + shift;
slot->pagecnt = newpagecnt;
return RPMRC_OK;
}
static int usedslots_cmp(const void *a, const void *b)
{
struct xdb_slot *sa = *(struct xdb_slot **)a;
struct xdb_slot *sb = *(struct xdb_slot **)b;
if (sa->startpage == sb->startpage) {
return sa->pagecnt > sb->pagecnt ? 1 : sa->pagecnt < sb->pagecnt ? -1 : 0;
}
return sa->startpage > sb->startpage ? 1 : -1;
}
static int rpmxdbReadHeader(rpmxdb xdb)
{
struct xdb_slot *slot;
unsigned int header[XDB_HEADER_SIZE / sizeof(unsigned int)];
unsigned int slotnpages, pagesize, generation, usergeneration;
unsigned int page, *lastfreep;
unsigned char *pageptr;
struct xdb_slot *slots, **usedslots, *lastslot;
unsigned int nslots;
unsigned int usedblobpages;
int i, nused, slotno;
struct stat stb;
size_t mapsize;
if (xdb->mapped) {
if (le2ha(xdb->mapped + XDB_OFFSET_GENERATION) == xdb->generation) {
return RPMRC_OK;
}
rpmxdbUnmap(xdb);
}
if (fstat(xdb->fd, &stb)) {
return RPMRC_FAIL;
}
if (pread(xdb->fd, header, sizeof(header), 0) != sizeof(header)) {
return RPMRC_FAIL;
}
if (le2ha((unsigned char *)header + XDB_OFFSET_MAGIC) != XDB_MAGIC)
return RPMRC_FAIL;
generation = le2ha((unsigned char *)header + XDB_OFFSET_GENERATION);
slotnpages = le2ha((unsigned char *)header + XDB_OFFSET_SLOTNPAGES);
pagesize = le2ha((unsigned char *)header + XDB_OFFSET_PAGESIZE);
usergeneration = le2ha((unsigned char *)header + XDB_OFFSET_USERGENERATION);
if (!slotnpages || !pagesize || stb.st_size % pagesize != 0)
return RPMRC_FAIL;
xdb->pagesize = pagesize;
/* round up */
mapsize = slotnpages * pagesize;
mapsize = (mapsize + xdb->systempagesize - 1) & ~(xdb->systempagesize - 1);
xdb->mapped = mmap(0, mapsize, xdb->rdonly ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, xdb->fd, 0);
if ((void *)xdb->mapped == MAP_FAILED) {
xdb->mapped = 0;
return RPMRC_FAIL;
}
xdb->mappedlen = mapsize;
/* read in all slots */
xdb->firstfree = 0;
nslots = slotnpages * (pagesize / SLOT_SIZE) - SLOT_START + 1;
slots = calloc(nslots + 1, sizeof(struct xdb_slot));
if (!slots) {
rpmxdbUnmap(xdb);
return RPMRC_FAIL;
}
usedslots = calloc(nslots + 1, sizeof(int));
if (!usedslots) {
rpmxdbUnmap(xdb);
free(slots);
return RPMRC_FAIL;
}
nused = 0;
slotno = 1;
slot = slots + 1;
usedblobpages = 0;
lastfreep = &xdb->firstfree;
for (page = 0, pageptr = xdb->mapped; page < slotnpages; page++, pageptr += pagesize) {
unsigned int o;
for (o = page ? 0 : SLOT_START * SLOT_SIZE; o < pagesize; o += SLOT_SIZE, slotno++, slot++) {
unsigned char *pp = pageptr + o;
slot->slotno = slotno;
slot->subtag = le2ha(pp);
if ((slot->subtag & 0x00ffffff) != SLOT_MAGIC) {
free(slots);
free(usedslots);
rpmxdbUnmap(xdb);
return RPMRC_FAIL;
}
slot->subtag = (slot->subtag >> 24) & 255;
slot->blobtag = le2ha(pp + 4);
slot->startpage = le2ha(pp + 8);
slot->pagecnt = le2ha(pp + 12);
if (slot->pagecnt == 0 && slot->startpage) /* empty but used slot? */
slot->startpage = slotnpages;
if (!slot->startpage) {
*lastfreep = slotno;
lastfreep = &slot->next;
} else {
usedslots[nused++] = slot;
usedblobpages += slot->pagecnt;
}
}
}
if (nused > 1) {
qsort(usedslots, nused, sizeof(*usedslots), usedslots_cmp);
}
/* now chain em */
slots[0].pagecnt = slotnpages;
lastslot = slots;
for (i = 0; i < nused; i++, lastslot = slot) {
slot = usedslots[i];
if (lastslot->startpage + lastslot->pagecnt > slot->startpage) {
free(slots);
free(usedslots);
rpmxdbUnmap(xdb);
return RPMRC_FAIL;
}
lastslot->next = slot->slotno;
slot->prev = lastslot->slotno;
}
lastslot->next = nslots;
slots[nslots].slotno = nslots;
slots[nslots].prev = lastslot->slotno;
slots[nslots].startpage = stb.st_size / pagesize;
free(usedslots);
/* now sync with the old slot data */
if (xdb->slots) {
for (i = 1, slot = xdb->slots + i; i < xdb->nslots; i++, slot++) {
if (slot->startpage && (slot->mapped || slot->mapcallback)) {
struct xdb_slot *nslot;
if (i >= nslots || !slots[i].startpage || slots[i].blobtag != slot->blobtag || slots[i].subtag != slot->subtag) {
/* slot is gone */
if (slot->mapped) {
unmapslot(xdb, slot);
slot->mapcallback(xdb, slot->mapcallbackdata, 0, 0);
}
continue;
}
nslot = slots + i;
if (slot->mapcallback) {
nslot->mapflags = slot->mapflags;
nslot->mapcallback = slot->mapcallback;
nslot->mapcallbackdata = slot->mapcallbackdata;
}
if (slot->startpage != nslot->startpage || slot->pagecnt != nslot->pagecnt) {
/* slot moved or was resized */
if (slot->mapped)
unmapslot(xdb, slot);
if (nslot->mapcallback) {
if (nslot->pagecnt) {
mapslot(xdb, nslot);
nslot->mapcallback(xdb, nslot->mapcallbackdata, nslot->mapped, nslot->mapped ? nslot->pagecnt * xdb->pagesize : 0);
} else {
nslot->mapcallback(xdb, nslot->mapcallbackdata, 0, 0);
}
}
}
}
}
free(xdb->slots);
}
xdb->slots = slots;
xdb->nslots = nslots;
xdb->generation = generation;
xdb->slotnpages = slotnpages;
xdb->usergeneration = usergeneration;
xdb->usedblobpages = usedblobpages;
return RPMRC_OK;
}
static int rpmxdbWriteHeader(rpmxdb xdb)
{
if (!xdb->mapped)
return RPMRC_FAIL;
h2lea(XDB_MAGIC, xdb->mapped + XDB_OFFSET_MAGIC);
h2lea(XDB_VERSION, xdb->mapped + XDB_OFFSET_VERSION);
h2lea(xdb->generation, xdb->mapped + XDB_OFFSET_GENERATION);
h2lea(xdb->slotnpages, xdb->mapped + XDB_OFFSET_SLOTNPAGES);
h2lea(xdb->pagesize, xdb->mapped + XDB_OFFSET_PAGESIZE);
return RPMRC_OK;
}
static void rpmxdbUpdateSlot(rpmxdb xdb, struct xdb_slot *slot)
{
unsigned char *pp = xdb->mapped + (SLOT_START - 1 + slot->slotno) * SLOT_SIZE;
h2lea(SLOT_MAGIC | (slot->subtag << 24), pp);
h2lea(slot->blobtag, pp + 4);
if (slot->pagecnt || !slot->startpage)
h2lea(slot->startpage, pp + 8);
else
h2lea(1, pp + 8); /* "empty but used" blobs always start at 1 */
h2lea(slot->pagecnt, pp + 12);
xdb->generation++;
h2lea(xdb->generation, xdb->mapped + XDB_OFFSET_GENERATION);
}
static int rpmxdbWriteEmptyPages(rpmxdb xdb, unsigned int pageno, unsigned int count)
{
unsigned char *page;
if (!count)
return RPMRC_OK;
page = malloc(xdb->pagesize);
if (!page)
return RPMRC_FAIL;
memset(page, 0, xdb->pagesize);
for (; count; count--, pageno++) {
if (pwrite(xdb->fd, page, xdb->pagesize, pageno * xdb->pagesize) != xdb->pagesize) {
free(page);
return RPMRC_FAIL;
}
}
free(page);
return RPMRC_OK;
}
static int rpmxdbWriteEmptySlotpage(rpmxdb xdb, int pageno)
{
unsigned char *page;
int i, spp;
page = malloc(xdb->pagesize);
if (!page)
return RPMRC_FAIL;
memset(page, 0, xdb->pagesize);
spp = xdb->pagesize / SLOT_SIZE; /* slots per page */
for (i = pageno ? 0 : SLOT_START; i < spp; i++)
h2le(SLOT_MAGIC, page + i * SLOT_SIZE);
if (!pageno) {
/* only used when called from InitInternal */
if (xdb->mapped) {
free(page);
return RPMRC_FAIL;
}
xdb->mapped = page;
rpmxdbWriteHeader(xdb);
xdb->mapped = 0;
}
if (pwrite(xdb->fd, page, xdb->pagesize, pageno * xdb->pagesize) != xdb->pagesize) {
free(page);
return RPMRC_FAIL;
}
free(page);
return RPMRC_OK;
}
static int rpmxdbInitInternal(rpmxdb xdb)
{
struct stat stb;
if (fstat(xdb->fd, &stb)) {
return RPMRC_FAIL;
}
if (stb.st_size == 0) {
xdb->slotnpages = 1;
xdb->generation++;
xdb->pagesize = sysconf(_SC_PAGE_SIZE);
if (rpmxdbWriteEmptySlotpage(xdb, 0)) {
return RPMRC_FAIL;
}
}
return RPMRC_OK;
}
/* we use the master pdb for locking */
static int rpmxdbLockOnly(rpmxdb xdb, int excl)
{
if (excl && xdb->rdonly)
return RPMRC_FAIL;
return rpmpkgLock(xdb->pkgdb, excl);
}
/* this is the same as rpmxdbLockReadHeader. It does the
* ReadHeader to sync the mappings if xdb moved some blobs.
*/
int rpmxdbLock(rpmxdb xdb, int excl)
{
if (rpmxdbLockOnly(xdb, excl))
return RPMRC_FAIL;
if (rpmxdbReadHeader(xdb)) {
rpmxdbUnlock(xdb, excl);
return RPMRC_FAIL;
}
return RPMRC_OK;
}
int rpmxdbUnlock(rpmxdb xdb, int excl)
{
return rpmpkgUnlock(xdb->pkgdb, excl);
}
static int rpmxdbLockReadHeader(rpmxdb xdb, int excl)
{
if (rpmxdbLockOnly(xdb, excl))
return RPMRC_FAIL;
if (rpmxdbReadHeader(xdb)) {
rpmxdbUnlock(xdb, excl);
return RPMRC_FAIL;
}
return RPMRC_OK;
}
static int rpmxdbInit(rpmxdb xdb)
{
int rc;
if (rpmxdbLockOnly(xdb, 1))
return RPMRC_FAIL;
rc = rpmxdbInitInternal(xdb);
rpmxdbUnlock(xdb, 1);
return rc;
}
int rpmxdbOpen(rpmxdb *xdbp, rpmpkgdb pkgdb, const char *filename, int flags, int mode)
{
struct stat stb;
rpmxdb xdb;
*xdbp = 0;
xdb = calloc(1, sizeof(*xdb));
xdb->pkgdb = pkgdb;
xdb->filename = strdup(filename);
xdb->systempagesize = sysconf(_SC_PAGE_SIZE);
if (!xdb->filename) {
free(xdb);
return RPMRC_FAIL;
}
if ((flags & (O_RDONLY|O_RDWR)) == O_RDONLY)
xdb->rdonly = 1;
if ((xdb->fd = open(filename, flags, mode)) == -1) {
free(xdb->filename);
free(xdb);
return RPMRC_FAIL;
}
if (fstat(xdb->fd, &stb)) {
close(xdb->fd);
free(xdb->filename);
free(xdb);
return RPMRC_FAIL;
}
if (stb.st_size == 0) {
if (rpmxdbInit(xdb)) {
close(xdb->fd);
free(xdb->filename);
free(xdb);
return RPMRC_FAIL;
}
}
xdb->flags = flags;
xdb->mode = mode;
xdb->dofsync = 1;
*xdbp = xdb;
return RPMRC_OK;
}
void rpmxdbClose(rpmxdb xdb)
{
struct xdb_slot *slot;
int i;
for (i = 1, slot = xdb->slots + 1; i < xdb->nslots; i++, slot++) {
if (slot->mapped) {
unmapslot(xdb, slot);
slot->mapcallback(xdb, slot->mapcallbackdata, 0, 0);
}
}
if (xdb->slots)
free(xdb->slots);
if (xdb->fd >= 0)
close(xdb->fd);
if (xdb->filename)
free(xdb->filename);
free(xdb);
}
/* moves the blob to a given new location (possibly resizeing) */
static int moveblobto(rpmxdb xdb, struct xdb_slot *oldslot, struct xdb_slot *afterslot, unsigned int newpagecnt)
{
struct xdb_slot *nextslot;
unsigned int newstartpage, oldpagecnt;
unsigned int tocopy;
int didmap;
newstartpage = afterslot->startpage + afterslot->pagecnt;
nextslot = xdb->slots + afterslot->next;
/* make sure there's enough room */
if (newpagecnt > nextslot->startpage - newstartpage)
return RPMRC_FAIL;
#if 0
printf("moveblobto %d %d %d %d, afterslot %d\n", oldslot->startpage, oldslot->pagecnt, newstartpage, newpagecnt, afterslot->slotno);
#endif
/* map old content */
didmap = 0;
oldpagecnt = oldslot->pagecnt;
if (!oldslot->mapped && oldpagecnt) {
if (mapslot(xdb, oldslot))
return RPMRC_FAIL;
didmap = 1;
}
/* copy content */
tocopy = newpagecnt > oldpagecnt ? oldpagecnt : newpagecnt;
if (tocopy && pwrite(xdb->fd, oldslot->mapped, tocopy * xdb->pagesize, newstartpage * xdb->pagesize) != tocopy * xdb->pagesize) {
if (didmap)
unmapslot(xdb, oldslot);
return RPMRC_FAIL;
}
/* zero out new pages */
if (newpagecnt > oldpagecnt) {
if (rpmxdbWriteEmptyPages(xdb, newstartpage + oldpagecnt, newpagecnt - oldpagecnt)) {
if (didmap)
unmapslot(xdb, oldslot);
return RPMRC_FAIL;
}
}
if (oldslot->mapped)
unmapslot(xdb, oldslot);
/* set new offset and position */
oldslot->startpage = newstartpage;
oldslot->pagecnt = newpagecnt;
rpmxdbUpdateSlot(xdb, oldslot);
xdb->usedblobpages -= oldpagecnt;
xdb->usedblobpages += newpagecnt;
if (afterslot != oldslot && nextslot != oldslot) {
/* remove from old chain */
xdb->slots[oldslot->prev].next = oldslot->next;
xdb->slots[oldslot->next].prev = oldslot->prev;
/* chain into new position, between lastslot and nextslot */
oldslot->prev = afterslot->slotno;
afterslot->next = oldslot->slotno;
oldslot->next = nextslot->slotno;
nextslot->prev = oldslot->slotno;
}
/* map again (if needed) */
if (oldslot->mapcallback) {
if (newpagecnt) {
if (mapslot(xdb, oldslot))
oldslot->mapped = 0; /* XXX: HELP, what can we do here? */
}
oldslot->mapcallback(xdb, oldslot->mapcallbackdata, oldslot->mapped, oldslot->mapped ? oldslot->pagecnt * xdb->pagesize : 0);
}
return RPMRC_OK;
}
/* moves the blob to a new location (possibly resizeing) */
static int moveblob(rpmxdb xdb, struct xdb_slot *oldslot, unsigned int newpagecnt)
{
struct xdb_slot *slot, *lastslot;
unsigned int nslots;
unsigned int freecnt;
int i;
nslots = xdb->nslots;
freecnt = 0;
lastslot = xdb->slots;
for (i = xdb->slots[0].next; ; lastslot = slot, i = slot->next) {
slot = xdb->slots + i;
freecnt = slot->startpage - (lastslot->startpage + lastslot->pagecnt);
if (freecnt >= newpagecnt)
break;
if (i == nslots)
break;
}
if (i == nslots && newpagecnt > freecnt) {
/* need to grow the file */
if (rpmxdbWriteEmptyPages(xdb, slot->startpage, newpagecnt - freecnt)) {
return RPMRC_FAIL;
}
slot->startpage += newpagecnt - freecnt;
}
return moveblobto(xdb, oldslot, lastslot, newpagecnt);
}
/* move the two blobs at the end of our file to the free area after the provided slot */
static int moveblobstofront(rpmxdb xdb, struct xdb_slot *afterslot)
{
struct xdb_slot *slot1, *slot2;
unsigned int freestart = afterslot->startpage + afterslot->pagecnt;
unsigned int freecount = xdb->slots[afterslot->next].startpage - freestart;
slot1 = xdb->slots + xdb->slots[xdb->nslots].prev;
if (slot1 == xdb->slots)
slot1 = slot2 = 0;
else {
slot2 = xdb->slots + slot1->prev;
if (slot2 == xdb->slots)
slot2 = 0;
}
if (slot1->pagecnt < slot2->pagecnt) {
struct xdb_slot *tmp = slot1;
slot1 = slot2;
slot2 = tmp;
}
if (slot1 && slot1->pagecnt && slot1->pagecnt <= freecount && slot1->startpage > freestart) {
if (moveblobto(xdb, slot1, afterslot, slot1->pagecnt))
return RPMRC_FAIL;
freestart += slot1->pagecnt;
freecount -= slot1->pagecnt;
afterslot = slot1;
}
if (slot2 && slot2->pagecnt && slot2->pagecnt <= freecount && slot2->startpage > freestart) {
if (moveblobto(xdb, slot2, afterslot, slot2->pagecnt))
return RPMRC_FAIL;
}
return RPMRC_OK;
}
/* add a single page containing empty slots */
static int addslotpage(rpmxdb xdb)
{
unsigned char *newaddr;
struct xdb_slot *slot;
int i, spp, nslots;
size_t newmappedlen;
if (xdb->firstfree)
return RPMRC_FAIL;
/* move first blob if needed */
nslots = xdb->nslots;
for (i = xdb->slots[0].next; i != nslots; i = slot->next) {
slot = xdb->slots + i;
if (slot->pagecnt)
break;
}
if (i != nslots && slot->pagecnt && slot->startpage == xdb->slotnpages) {
/* the blob at this slot is in the way. move it. */
if (moveblob(xdb, slot, slot->pagecnt))
return RPMRC_FAIL;
}
spp = xdb->pagesize / SLOT_SIZE; /* slots per page */
slot = realloc(xdb->slots, (nslots + 1 + spp) * sizeof(*slot));
if (!slot)
return RPMRC_FAIL;
xdb->slots = slot;
if (rpmxdbWriteEmptySlotpage(xdb, xdb->slotnpages)) {
return RPMRC_FAIL;
}
/* remap slots */
newmappedlen = xdb->slotnpages * xdb->pagesize + xdb->pagesize;
newmappedlen = (newmappedlen + xdb->systempagesize - 1) & ~(xdb->systempagesize - 1);
newaddr = mremap(xdb->mapped, xdb->mappedlen, newmappedlen, MREMAP_MAYMOVE);
if (newaddr == MAP_FAILED)
return RPMRC_FAIL;
xdb->mapped = newaddr;
xdb->mappedlen = newmappedlen;
/* update the header */
xdb->slotnpages++;
xdb->generation++;
rpmxdbWriteHeader(xdb);
/* fixup empty but used slots */
for (i = xdb->slots[0].next; i != nslots; i = slot->next) {
slot = xdb->slots + i;
if (slot->startpage >= xdb->slotnpages)
break;
slot->startpage = xdb->slotnpages;
if (slot->pagecnt)
abort();
}
/* move tail element to the new end */
slot = xdb->slots + nslots + spp;
*slot = xdb->slots[nslots];
slot->slotno = nslots + spp;
xdb->slots[slot->prev].next = slot->slotno;
xdb->nslots += spp;
/* add new free slots to the firstfree chain */
memset(xdb->slots + nslots, 0, sizeof(*slot) * spp);
for (i = 0; i < spp - 1; i++) {
xdb->slots[nslots + i].slotno = nslots + i;
xdb->slots[nslots + i].next = i + 1;
}
xdb->slots[nslots + i].slotno = nslots + i;
xdb->firstfree = nslots;
return RPMRC_OK;
}
static int createblob(rpmxdb xdb, unsigned int *idp, unsigned int blobtag, unsigned int subtag)
{
struct xdb_slot *slot;
unsigned int id;
if (subtag > 255)
return RPMRC_FAIL;
if (!xdb->firstfree) {
if (addslotpage(xdb))
return RPMRC_FAIL;
}
id = xdb->firstfree;
slot = xdb->slots + xdb->firstfree;
xdb->firstfree = slot->next;
slot->mapped = 0;
slot->blobtag = blobtag;
slot->subtag = subtag;
slot->startpage = xdb->slotnpages;
slot->pagecnt = 0;
rpmxdbUpdateSlot(xdb, slot);
/* enqueue */
slot->prev = 0;
slot->next = xdb->slots[0].next;
xdb->slots[slot->next].prev = id;
xdb->slots[0].next = id;
#if 0
printf("createblob #%d %d/%d\n", id, blobtag, subtag);
#endif
if (slot->slotno != id)
abort();
if (slot->mapped)
abort();
*idp = id;
return RPMRC_OK;
}
int rpmxdbLookupBlob(rpmxdb xdb, unsigned int *idp, unsigned int blobtag, unsigned int subtag, int flags)
{
struct xdb_slot *slot;
unsigned int i, nslots;
if (rpmxdbLockReadHeader(xdb, flags ? 1 : 0))
return RPMRC_FAIL;
nslots = xdb->nslots;
slot = 0;
for (i = xdb->slots[0].next; i != nslots; i = slot->next) {
slot = xdb->slots + i;
if (slot->blobtag == blobtag && slot->subtag == subtag)
break;
}
if (i == nslots)
i = 0;
if (i && (flags & O_TRUNC) != 0) {
if (rpmxdbResizeBlob(xdb, i, 0)) {
rpmxdbUnlock(xdb, flags ? 1 : 0);
return RPMRC_FAIL;
}
}
if (!i && (flags & O_CREAT) != 0) {
if (createblob(xdb, &i, blobtag, subtag)) {
rpmxdbUnlock(xdb, flags ? 1 : 0);
return RPMRC_FAIL;
}
}
*idp = i;
rpmxdbUnlock(xdb, flags ? 1 : 0);
return i ? RPMRC_OK : RPMRC_NOTFOUND;
}
int rpmxdbDelBlob(rpmxdb xdb, unsigned int id)
{
struct xdb_slot *slot;
if (!id)
return RPMRC_FAIL;
if (rpmxdbLockReadHeader(xdb, 1))
return RPMRC_FAIL;
if (id >= xdb->nslots) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
slot = xdb->slots + id;
if (!slot->startpage) {
rpmxdbUnlock(xdb, 1);
return RPMRC_OK;
}
if (slot->mapped) {
unmapslot(xdb, slot);
slot->mapcallback(xdb, slot->mapcallbackdata, 0, 0);
}
/* remove from old chain */
xdb->slots[slot->prev].next = slot->next;
xdb->slots[slot->next].prev = slot->prev;
xdb->usedblobpages -= slot->pagecnt;
if (xdb->usedblobpages * 2 < xdb->slots[xdb->nslots].startpage && (slot->startpage + slot->pagecnt) * 2 < xdb->slots[xdb->nslots].startpage) {
/* freed in first half of pages, move last two blobs if we can */
moveblobstofront(xdb, xdb->slots + slot->prev);
}
/* zero slot */
memset(slot, 0, sizeof(*slot));
slot->slotno = id;
rpmxdbUpdateSlot(xdb, slot);
/* enqueue into free chain */
slot->next = xdb->firstfree;
xdb->firstfree = slot->slotno;
/* check if we should truncate the file */
slot = xdb->slots + xdb->slots[xdb->nslots].prev;
if (slot->startpage + slot->pagecnt < xdb->slots[xdb->nslots].startpage / 4 * 3) {
unsigned int newend = slot->startpage + slot->pagecnt;
if (!ftruncate(xdb->fd, newend * xdb->pagesize))
xdb->slots[xdb->nslots].startpage = newend;
}
rpmxdbUnlock(xdb, 1);
return RPMRC_OK;
}
int rpmxdbResizeBlob(rpmxdb xdb, unsigned int id, size_t newsize)
{
struct xdb_slot *slot;
unsigned int oldpagecnt, newpagecnt;
if (!id)
return RPMRC_FAIL;
if (rpmxdbLockReadHeader(xdb, 1))
return RPMRC_FAIL;
if (id >= xdb->nslots) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
slot = xdb->slots + id;
if (!slot->startpage) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
oldpagecnt = slot->pagecnt;
newpagecnt = (newsize + xdb->pagesize - 1) / xdb->pagesize;
if (oldpagecnt && newpagecnt && newpagecnt <= oldpagecnt) {
/* reducing size. zero to end of page */
unsigned int pg = newsize & (xdb->pagesize - 1);
if (pg) {
if (slot->mapped) {
memset(slot->mapped + pg, 0, xdb->pagesize - pg);
} else {
char *empty = calloc(1, xdb->pagesize - pg);
if (!empty) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
if (pwrite(xdb->fd, empty, xdb->pagesize - pg, (slot->startpage + newpagecnt - 1) * xdb->pagesize + pg ) != xdb->pagesize - pg) {
free(empty);
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
free(empty);
}
}
}
if (newpagecnt == oldpagecnt) {
/* no size change */
rpmxdbUnlock(xdb, 1);
return RPMRC_OK;
}
if (!newpagecnt) {
/* special case: zero size blob, no longer mapped */
if (slot->mapped)
unmapslot(xdb, slot);
slot->pagecnt = 0;
slot->startpage = xdb->slotnpages;
/* remove from old chain */
xdb->slots[slot->prev].next = slot->next;
xdb->slots[slot->next].prev = slot->prev;
/* enqueue into head */
slot->prev = 0;
slot->next = xdb->slots[0].next;
xdb->slots[slot->next].prev = slot->slotno;
xdb->slots[0].next = slot->slotno;
rpmxdbUpdateSlot(xdb, slot);
xdb->usedblobpages -= oldpagecnt;
if (slot->mapcallback)
slot->mapcallback(xdb, slot->mapcallbackdata, 0, 0);
} else if (newpagecnt <= xdb->slots[slot->next].startpage - slot->startpage) {
/* can do it inplace */
if (newpagecnt > oldpagecnt) {
/* zero new pages */
if (rpmxdbWriteEmptyPages(xdb, slot->startpage + oldpagecnt, newpagecnt - oldpagecnt)) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
}
if (slot->mapcallback) {
if (remapslot(xdb, slot, newpagecnt)) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
} else {
if (slot->mapped)
unmapslot(xdb, slot);
slot->pagecnt = newpagecnt;
}
rpmxdbUpdateSlot(xdb, slot);
xdb->usedblobpages -= oldpagecnt;
xdb->usedblobpages += newpagecnt;
if (slot->mapcallback)
slot->mapcallback(xdb, slot->mapcallbackdata, slot->mapped, slot->pagecnt * xdb->pagesize);
} else {
/* need to relocate to a new page area */
if (moveblob(xdb, slot, newpagecnt)) {
rpmxdbUnlock(xdb, 1);
return RPMRC_FAIL;
}
}
rpmxdbUnlock(xdb, 1);
return RPMRC_OK;
}
int rpmxdbMapBlob(rpmxdb xdb, unsigned int id, int flags, void (*mapcallback)(rpmxdb xdb, void *data, void *newaddr, size_t newsize), void *mapcallbackdata)
{
struct xdb_slot *slot;
if (!id || !mapcallback)
return RPMRC_FAIL;
if ((flags & (O_RDONLY|O_RDWR)) == O_RDWR && xdb->rdonly)
return RPMRC_FAIL;
if (rpmxdbLockReadHeader(xdb, 0))
return RPMRC_FAIL;
if (id >= xdb->nslots) {
rpmxdbUnlock(xdb, 0);
return RPMRC_FAIL;
}
slot = xdb->slots + id;
if (!slot->startpage || slot->mapped) {
rpmxdbUnlock(xdb, 0);
return RPMRC_FAIL;
}
slot->mapflags = (flags & (O_RDONLY|O_RDWR)) == O_RDWR ? PROT_READ | PROT_WRITE : PROT_READ;
if (slot->pagecnt) {
if (mapslot(xdb, slot)) {
slot->mapflags = 0;
rpmxdbUnlock(xdb, 0);