-
Notifications
You must be signed in to change notification settings - Fork 20
/
database.c
2726 lines (2447 loc) · 71.1 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
/* This file is part of "reprepro"
* Copyright (C) 2007,2008,2016 Bernhard R. Link
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <db.h>
#include "globals.h"
#include "error.h"
#include "ignore.h"
#include "strlist.h"
#include "names.h"
#include "database.h"
#include "dirs.h"
#include "filecntl.h"
#include "files.h"
#include "filelist.h"
#include "reference.h"
#include "tracking.h"
#include "dpkgversions.h"
#include "distribution.h"
#include "database_p.h"
#include "chunks.h"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define LIBDB_VERSION_STRING "bdb" TOSTRING(DB_VERSION_MAJOR) "." TOSTRING(DB_VERSION_MINOR) "." TOSTRING(DB_VERSION_PATCH)
#define CLEARDBT(dbt) { memset(&dbt, 0, sizeof(dbt)); }
#define SETDBT(dbt, datastr) {const char *my = datastr; memset(&dbt, 0, sizeof(dbt)); dbt.data = (void *)my; dbt.size = strlen(my) + 1;}
#define SETDBTl(dbt, datastr, datasize) {const char *my = datastr; memset(&dbt, 0, sizeof(dbt)); dbt.data = (void *)my; dbt.size = datasize;}
static bool rdb_initialized, rdb_used, rdb_locked, rdb_verbose;
static int rdb_dircreationdepth;
static bool rdb_nopackages, rdb_readonly;
static /*@null@*/ char *rdb_version, *rdb_lastsupportedversion,
*rdb_dbversion, *rdb_lastsupporteddbversion;
static DB_ENV *rdb_env = NULL;
struct table *rdb_checksums, *rdb_contents;
struct table *rdb_references;
static struct {
bool createnewtables;
} rdb_capabilities;
struct opened_tables {
struct opened_tables *next;
const char *name;
const char *subname;
};
struct opened_tables *opened_tables = NULL;
static void database_free(void) {
if (!rdb_initialized)
return;
free(rdb_version);
rdb_version = NULL;
free(rdb_lastsupportedversion);
rdb_lastsupportedversion = NULL;
free(rdb_dbversion);
rdb_dbversion = NULL;
free(rdb_lastsupporteddbversion);
rdb_lastsupporteddbversion = NULL;
rdb_initialized = false;
}
static inline char *dbfilename(const char *filename) {
return calc_dirconcat(global.dbdir, filename);
}
static retvalue database_openenv(void) {
int dbret;
dbret = db_env_create(&rdb_env, 0);
if (dbret != 0) {
fprintf(stderr, "db_env_create: %s\n", db_strerror(dbret));
return RET_ERROR;
}
// DB_INIT_LOCK is needed to open multiple databases in one file (e.g. for move command)
dbret = rdb_env->open(rdb_env, global.dbdir,
DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOCK, 0664);
if (dbret != 0) {
rdb_env->err(rdb_env, dbret, "environment open: %s", global.dbdir);
return RET_ERROR;
}
return RET_OK;
}
static void database_closeenv(void) {
int dbret;
dbret = rdb_env->close(rdb_env, 0);
if (dbret != 0) {
fprintf(stderr, "Error: DB_ENV->close: %s\n", db_strerror(dbret));
}
rdb_env = NULL;
}
/**********************/
/* lock file handling */
/**********************/
static retvalue database_lock(size_t waitforlock) {
char *lockfile;
int fd;
retvalue r;
size_t tries = 0;
assert (!rdb_locked);
rdb_dircreationdepth = 0;
r = dir_create_needed(global.dbdir, &rdb_dircreationdepth);
if (RET_WAS_ERROR(r))
return r;
lockfile = dbfilename("lockfile");
if (FAILEDTOALLOC(lockfile))
return RET_ERROR_OOM;
fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY,
S_IRUSR|S_IWUSR);
while (fd < 0) {
int e = errno;
if (e == EEXIST) {
if (tries < waitforlock && ! interrupted()) {
unsigned int timetosleep = 10;
if (verbose >= 0)
printf(
"Could not acquire lock: %s already exists!\nWaiting 10 seconds before trying again.\n",
lockfile);
while (timetosleep > 0)
timetosleep = sleep(timetosleep);
tries++;
fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL
|O_NOFOLLOW|O_NOCTTY,
S_IRUSR|S_IWUSR);
continue;
}
fprintf(stderr,
"The lock file '%s' already exists. There might be another instance with the\n"
"same database dir running. To avoid locking overhead, only one process\n"
"can access the database at the same time. Do not delete the lock file unless\n"
"you are sure no other version is still running!\n", lockfile);
} else
fprintf(stderr,
"Error %d creating lock file '%s': %s!\n",
e, lockfile, strerror(e));
free(lockfile);
return RET_ERRNO(e);
}
// TODO: do some more locking of this file to avoid problems
// with the non-atomity of O_EXCL with nfs-filesystems...
if (close(fd) != 0) {
int e = errno;
fprintf(stderr,
"(Late) Error %d creating lock file '%s': %s!\n",
e, lockfile, strerror(e));
(void)unlink(lockfile);
free(lockfile);
return RET_ERRNO(e);
}
free(lockfile);
rdb_locked = true;
r = database_openenv();
if (RET_WAS_ERROR(r)) {
(void)unlink(lockfile);
free(lockfile);
return r;
}
return RET_OK;
}
static void releaselock(void) {
char *lockfile;
assert (rdb_locked);
database_closeenv();
lockfile = dbfilename("lockfile");
if (lockfile == NULL)
return;
if (unlink(lockfile) != 0) {
int e = errno;
fprintf(stderr, "Error %d deleting lock file '%s': %s!\n",
e, lockfile, strerror(e));
(void)unlink(lockfile);
}
free(lockfile);
dir_remove_new(global.dbdir, rdb_dircreationdepth);
rdb_locked = false;
}
static retvalue writeversionfile(void);
retvalue database_close(void) {
retvalue result = RET_OK, r;
if (rdb_references != NULL) {
r = table_close(rdb_references);
RET_UPDATE(result, r);
rdb_references = NULL;
}
if (rdb_checksums != NULL) {
r = table_close(rdb_checksums);
RET_UPDATE(result, r);
rdb_checksums = NULL;
}
if (rdb_contents != NULL) {
r = table_close(rdb_contents);
RET_UPDATE(result, r);
rdb_contents = NULL;
}
r = writeversionfile();
RET_UPDATE(result, r);
if (rdb_locked)
releaselock();
database_free();
return result;
}
static retvalue database_hasdatabasefile(const char *filename, /*@out@*/bool *exists_p) {
char *fullfilename;
fullfilename = dbfilename(filename);
if (FAILEDTOALLOC(fullfilename))
return RET_ERROR_OOM;
*exists_p = isregularfile(fullfilename);
free(fullfilename);
return RET_OK;
}
enum database_type {
dbt_QUERY,
dbt_BTREE, dbt_BTREEDUP, dbt_BTREEPAIRS, dbt_BTREEVERSIONS,
dbt_HASH,
dbt_COUNT /* must be last */
};
static const uint32_t types[dbt_COUNT] = {
DB_UNKNOWN,
DB_BTREE, DB_BTREE, DB_BTREE, DB_BTREE,
DB_HASH
};
static int debianversioncompare(UNUSED(DB *db), const DBT *a, const DBT *b);
#if DB_VERSION_MAJOR >= 6
static int paireddatacompare(UNUSED(DB *db), const DBT *a, const DBT *b, size_t *locp);
#else
static int paireddatacompare(UNUSED(DB *db), const DBT *a, const DBT *b);
#endif
static retvalue database_opentable(const char *filename, /*@null@*/const char *subtable, enum database_type type, uint32_t flags, /*@out@*/DB **result) {
DB *table;
int dbret;
dbret = db_create(&table, rdb_env, 0);
if (dbret != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(dbret));
return RET_DBERR(dbret);
}
if (type == dbt_BTREEPAIRS || type == dbt_BTREEVERSIONS) {
dbret = table->set_flags(table, DB_DUPSORT);
if (dbret != 0) {
table->err(table, dbret, "db_set_flags(DB_DUPSORT):");
(void)table->close(table, 0);
return RET_DBERR(dbret);
}
} else if (type == dbt_BTREEDUP) {
dbret = table->set_flags(table, DB_DUP);
if (dbret != 0) {
table->err(table, dbret, "db_set_flags(DB_DUP):");
(void)table->close(table, 0);
return RET_DBERR(dbret);
}
}
if (type == dbt_BTREEPAIRS) {
dbret = table->set_dup_compare(table, paireddatacompare);
if (dbret != 0) {
table->err(table, dbret, "db_set_dup_compare:");
(void)table->close(table, 0);
return RET_DBERR(dbret);
}
}
if (type == dbt_BTREEVERSIONS) {
dbret = table->set_dup_compare(table, debianversioncompare);
if (dbret != 0) {
table->err(table, dbret, "db_set_dup_compare:");
(void)table->close(table, 0);
return RET_DBERR(dbret);
}
}
#if DB_VERSION_MAJOR == 5 || DB_VERSION_MAJOR == 6
#define DB_OPEN(database, filename, name, type, flags) \
database->open(database, NULL, filename, name, type, flags, 0664)
#else
#if DB_VERSION_MAJOR == 4
#define DB_OPEN(database, filename, name, type, flags) \
database->open(database, NULL, filename, name, type, flags, 0664)
#else
#if DB_VERSION_MAJOR == 3
#define DB_OPEN(database, filename, name, type, flags) \
database->open(database, filename, name, type, flags, 0664)
#else
#error Unexpected DB_VERSION_MAJOR!
#endif
#endif
#endif
dbret = DB_OPEN(table, filename, subtable, types[type], flags);
if (dbret == ENOENT && !ISSET(flags, DB_CREATE)) {
(void)table->close(table, 0);
return RET_NOTHING;
}
if (dbret != 0) {
if (subtable != NULL)
table->err(table, dbret, "db_open(%s:%s)[%d]",
filename, subtable, dbret);
else
table->err(table, dbret, "db_open(%s)[%d]",
filename, dbret);
(void)table->close(table, 0);
return RET_DBERR(dbret);
}
*result = table;
return RET_OK;
}
retvalue database_listsubtables(const char *filename, struct strlist *result) {
DB *table;
DBC *cursor;
DBT key, data;
int dbret;
retvalue ret, r;
struct strlist ids;
r = database_opentable(filename, NULL,
dbt_QUERY, DB_RDONLY, &table);
if (!RET_IS_OK(r))
return r;
cursor = NULL;
if ((dbret = table->cursor(table, NULL, &cursor, 0)) != 0) {
table->err(table, dbret, "cursor(%s):", filename);
(void)table->close(table, 0);
return RET_ERROR;
}
CLEARDBT(key);
CLEARDBT(data);
strlist_init(&ids);
ret = RET_NOTHING;
while ((dbret=cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
char *identifier = strndup(key.data, key.size);
if (FAILEDTOALLOC(identifier)) {
(void)table->close(table, 0);
strlist_done(&ids);
return RET_ERROR_OOM;
}
r = strlist_add(&ids, identifier);
if (RET_WAS_ERROR(r)) {
(void)table->close(table, 0);
strlist_done(&ids);
return r;
}
ret = RET_OK;
CLEARDBT(key);
CLEARDBT(data);
}
if (dbret != 0 && dbret != DB_NOTFOUND) {
table->err(table, dbret, "c_get(%s):", filename);
(void)table->close(table, 0);
strlist_done(&ids);
return RET_DBERR(dbret);
}
if ((dbret = cursor->c_close(cursor)) != 0) {
table->err(table, dbret, "c_close(%s):", filename);
(void)table->close(table, 0);
strlist_done(&ids);
return RET_DBERR(dbret);
}
dbret = table->close(table, 0);
if (dbret != 0) {
table->err(table, dbret, "close(%s):", filename);
strlist_done(&ids);
return RET_DBERR(dbret);
} else {
strlist_move(result, &ids);
return ret;
}
}
retvalue database_dropsubtable(const char *table, const char *subtable) {
char *filename;
DB *db;
int dbret;
filename = dbfilename(table);
if (FAILEDTOALLOC(filename))
return RET_ERROR_OOM;
if ((dbret = db_create(&db, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s %s\n",
filename, db_strerror(dbret));
free(filename);
return RET_DBERR(dbret);
}
dbret = db->remove(db, filename, subtable, 0);
if (dbret == ENOENT) {
free(filename);
return RET_NOTHING;
}
if (dbret != 0) {
fprintf(stderr, "Error removing '%s' from %s!\n",
subtable, filename);
free(filename);
return RET_DBERR(dbret);
}
free(filename);
return RET_OK;
}
static inline bool targetisdefined(const char *identifier, struct distribution *distributions) {
struct distribution *d;
struct target *t;
for (d = distributions ; d != NULL ; d = d->next) {
for (t = d->targets; t != NULL ; t = t->next) {
if (strcmp(t->identifier, identifier) == 0) {
t->existed = true;
return true;
}
}
}
return false;
}
static retvalue warnidentifiers(const struct strlist *identifiers, struct distribution *distributions, bool readonly) {
struct distribution *d;
struct target *t;
const char *identifier;
retvalue r;
int i;
for (i = 0; i < identifiers->count ; i++) {
identifier = identifiers->values[i];
if (targetisdefined(identifier, distributions))
continue;
fprintf(stderr,
"Error: packages database contains unused '%s' database.\n", identifier);
if (ignored[IGN_undefinedtarget] == 0) {
(void)fputs(
"This usually means you removed some component, architecture or even\n"
"a whole distribution from conf/distributions.\n"
"In that case you most likely want to call reprepro clearvanished to get rid\n"
"of the databases belonging to those removed parts.\n"
"(Another reason to get this error is using conf/ and db/ directories\n"
" belonging to different reprepro repositories).\n",
stderr);
}
if (IGNORABLE(undefinedtarget)) {
(void)fputs(
"Ignoring as --ignore=undefinedtarget given.\n",
stderr);
ignored[IGN_undefinedtarget]++;
continue;
}
(void)fputs(
"To ignore use --ignore=undefinedtarget.\n",
stderr);
return RET_ERROR;
}
if (readonly)
return RET_OK;
for (d = distributions ; d != NULL ; d = d->next) {
bool architecture_existed[d->architectures.count];
bool have_old = false;
/* check for new architectures */
memset(architecture_existed, 0, sizeof(architecture_existed));
for (t = d->targets; t != NULL ; t = t->next) {
int o;
if (!t->existed)
continue;
o = atomlist_ofs(&d->architectures,
t->architecture);
assert (o >= 0);
if (o >= 0) {
architecture_existed[o] = true;
/* only warn about new ones if there
* is at least one old one, otherwise
* it's just a new distribution */
have_old = true;
}
}
for (i = 0 ; have_old && i < d->architectures.count ; i++) {
architecture_t a;
if (architecture_existed[i])
continue;
a = d->architectures.atoms[i];
fprintf(stderr,
"New architecture '%s' in '%s'. Perhaps you want to call\n"
"reprepro flood '%s' '%s'\n"
"to populate it with architecture 'all' packages from other architectures.\n",
atoms_architectures[a], d->codename,
d->codename, atoms_architectures[a]);
}
/* create databases, so we know next time what is new */
for (t = d->targets; t != NULL ; t = t->next) {
if (t->existed)
continue;
/* create database now, to test it can be created
* early, and to know when new architectures
* arrive in the future. */
r = target_initpackagesdb(t, READWRITE);
if (RET_WAS_ERROR(r))
return r;
r = target_closepackagesdb(t);
if (RET_WAS_ERROR(r))
return r;
}
}
return RET_OK;
}
static retvalue warnunusedtracking(const struct strlist *codenames, const struct distribution *distributions) {
const char *codename;
const struct distribution *d;
int i;
for (i = 0; i < codenames->count ; i++) {
codename = codenames->values[i];
d = distributions;
while (d != NULL && strcmp(d->codename, codename) != 0)
d = d->next;
if (d != NULL && d->tracking != dt_NONE)
continue;
fprintf(stderr,
"Error: tracking database contains unused '%s' database.\n", codename);
if (ignored[IGN_undefinedtracking] == 0) {
if (d == NULL)
(void)fputs(
"This either means you removed a distribution from the distributions config\n"
"file without calling clearvanished (or at least removealltracks), you\n"
"experienced a bug in retrack in versions < 3.0.0, you found a new bug or your\n"
"config does not belong to this database.\n",
stderr);
else
(void)fputs(
"This either means you removed the Tracking: options from this distribution without\n"
"calling removealltracks for it, or your config does not belong to this database.\n",
stderr);
}
if (IGNORABLE(undefinedtracking)) {
(void)fputs(
"Ignoring as --ignore=undefinedtracking given.\n",
stderr);
ignored[IGN_undefinedtracking]++;
continue;
}
(void)fputs("To ignore use --ignore=undefinedtracking.\n",
stderr);
return RET_ERROR;
}
return RET_OK;
}
static retvalue readline(/*@out@*/char **result, FILE *f, const char *versionfilename) {
char buffer[21];
size_t l;
if (fgets(buffer, 20, f) == NULL) {
int e = errno;
if (e == 0) {
fprintf(stderr,
"Error reading '%s': unexpected empty file\n",
versionfilename);
return RET_ERROR;
} else {
fprintf(stderr, "Error reading '%s': %s(errno is %d)\n",
versionfilename, strerror(e), e);
return RET_ERRNO(e);
}
}
l = strlen(buffer);
while (l > 0 && (buffer[l-1] == '\r' || buffer[l-1] == '\n')) {
buffer[--l] = '\0';
}
if (l == 0) {
fprintf(stderr, "Error reading '%s': unexpected empty line.\n",
versionfilename);
return RET_ERROR;
}
*result = strdup(buffer);
if (FAILEDTOALLOC(*result))
return RET_ERROR_OOM;
return RET_OK;
}
static retvalue readversionfile(bool nopackagesyet) {
char *versionfilename;
FILE *f;
retvalue r;
int c;
versionfilename = dbfilename("version");
if (FAILEDTOALLOC(versionfilename))
return RET_ERROR_OOM;
f = fopen(versionfilename, "r");
if (f == NULL) {
int e = errno;
if (e != ENOENT) {
fprintf(stderr, "Error opening '%s': %s(errno is %d)\n",
versionfilename, strerror(e), e);
free(versionfilename);
return RET_ERRNO(e);
}
free(versionfilename);
if (nopackagesyet) {
/* set to default for new packages.db files: */
rdb_version = strdup(VERSION);
if (FAILEDTOALLOC(rdb_version))
return RET_ERROR_OOM;
rdb_capabilities.createnewtables = true;
} else
rdb_version = NULL;
rdb_lastsupportedversion = NULL;
rdb_dbversion = NULL;
rdb_lastsupporteddbversion = NULL;
return RET_NOTHING;
}
/* first line is the version creating this database */
r = readline(&rdb_version, f, versionfilename);
if (RET_WAS_ERROR(r)) {
(void)fclose(f);
free(versionfilename);
return r;
}
/* second line says which versions of reprepro will be able to cope
* with this database */
r = readline(&rdb_lastsupportedversion, f, versionfilename);
if (RET_WAS_ERROR(r)) {
(void)fclose(f);
free(versionfilename);
return r;
}
/* next line is the version of the underlying database library */
r = readline(&rdb_dbversion, f, versionfilename);
if (RET_WAS_ERROR(r)) {
(void)fclose(f);
free(versionfilename);
return r;
}
/* and then the minimum version of this library needed. */
r = readline(&rdb_lastsupporteddbversion, f, versionfilename);
if (RET_WAS_ERROR(r)) {
(void)fclose(f);
free(versionfilename);
return r;
}
(void)fclose(f);
free(versionfilename);
/* check for enabled capabilities in the version */
r = dpkgversions_cmp(rdb_version, "3", &c);
if (RET_WAS_ERROR(r))
return r;
if (c >= 0)
rdb_capabilities.createnewtables = true;
/* ensure we can understand it */
r = dpkgversions_cmp(VERSION, rdb_lastsupportedversion, &c);
if (RET_WAS_ERROR(r))
return r;
if (c < 0) {
fprintf(stderr,
"According to %s/version this database was created with a future version\n"
"and uses features this version cannot understand. Aborting...\n",
global.dbdir);
return RET_ERROR;
}
/* ensure it's a libdb database: */
if (strncmp(rdb_dbversion, "bdb", 3) != 0) {
fprintf(stderr,
"According to %s/version this database was created with a yet unsupported\n"
"database library. Aborting...\n",
global.dbdir);
return RET_ERROR;
}
if (strncmp(rdb_lastsupporteddbversion, "bdb", 3) != 0) {
fprintf(stderr,
"According to %s/version this database was created with a yet unsupported\n"
"database library. Aborting...\n",
global.dbdir);
return RET_ERROR;
}
r = dpkgversions_cmp(LIBDB_VERSION_STRING,
rdb_lastsupporteddbversion, &c);
if (RET_WAS_ERROR(r))
return r;
if (c < 0) {
fprintf(stderr,
"According to %s/version this database was created with a future version\n"
"%s of libdb. The libdb version this binary is linked against cannot yet\n"
"handle this format. Aborting...\n",
global.dbdir, rdb_dbversion + 3);
return RET_ERROR;
}
return RET_OK;
}
static retvalue writeversionfile(void) {
char *versionfilename, *finalversionfilename;
FILE *f;
int i, e;
versionfilename = dbfilename("version.new");
if (FAILEDTOALLOC(versionfilename))
return RET_ERROR_OOM;
f = fopen(versionfilename, "w");
if (f == NULL) {
e = errno;
fprintf(stderr, "Error creating '%s': %s(errno is %d)\n",
versionfilename, strerror(e), e);
free(versionfilename);
return RET_ERRNO(e);
}
if (rdb_version == NULL)
(void)fputs("0\n", f);
else {
(void)fputs(rdb_version, f);
(void)fputc('\n', f);
}
if (rdb_lastsupportedversion == NULL) {
(void)fputs("3.3.0\n", f);
} else {
int c;
retvalue r;
r = dpkgversions_cmp(rdb_lastsupportedversion,
"3.3.0", &c);
if (!RET_IS_OK(r) || c < 0)
(void)fputs("3.3.0\n", f);
else {
(void)fputs(rdb_lastsupportedversion, f);
(void)fputc('\n', f);
}
}
if (rdb_dbversion == NULL)
fprintf(f, "bdb%d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
DB_VERSION_PATCH);
else {
(void)fputs(rdb_dbversion, f);
(void)fputc('\n', f);
}
if (rdb_lastsupporteddbversion == NULL)
fprintf(f, "bdb%d.%d.0\n", DB_VERSION_MAJOR, DB_VERSION_MINOR);
else {
(void)fputs(rdb_lastsupporteddbversion, f);
(void)fputc('\n', f);
}
e = ferror(f);
if (e != 0) {
fprintf(stderr, "Error writing '%s': %s(errno is %d)\n",
versionfilename, strerror(e), e);
(void)fclose(f);
unlink(versionfilename);
free(versionfilename);
return RET_ERRNO(e);
}
if (fclose(f) != 0) {
e = errno;
fprintf(stderr, "Error writing '%s': %s(errno is %d)\n",
versionfilename, strerror(e), e);
unlink(versionfilename);
free(versionfilename);
return RET_ERRNO(e);
}
finalversionfilename = dbfilename("version");
if (FAILEDTOALLOC(finalversionfilename)) {
unlink(versionfilename);
free(versionfilename);
return RET_ERROR_OOM;
}
i = rename(versionfilename, finalversionfilename);
if (i != 0) {
e = errno;
fprintf(stderr, "Error %d moving '%s' to '%s': %s\n",
e, versionfilename, finalversionfilename,
strerror(e));
(void)unlink(versionfilename);
free(versionfilename);
free(finalversionfilename);
return RET_ERRNO(e);
}
free(finalversionfilename);
free(versionfilename);
return RET_OK;
}
static retvalue createnewdatabase(struct distribution *distributions) {
struct distribution *d;
struct target *t;
retvalue result = RET_NOTHING, r;
for (d = distributions ; d != NULL ; d = d->next) {
for (t = d->targets ; t != NULL ; t = t->next) {
r = target_initpackagesdb(t, READWRITE);
RET_UPDATE(result, r);
if (RET_IS_OK(r)) {
r = target_closepackagesdb(t);
RET_UPDATE(result, r);
}
}
}
r = writeversionfile();
RET_UPDATE(result, r);
return result;
}
/* Initialize a database.
* - if not fast, make all kind of checks for consistency (TO BE IMPLEMENTED),
* - if readonly, do not create but return with RET_NOTHING
* - lock database, waiting a given amount of time if already locked
*/
retvalue database_create(struct distribution *alldistributions, bool fast, bool nopackages, bool allowunused, bool readonly, size_t waitforlock, bool verbosedb) {
retvalue r;
bool packagesfileexists, trackingfileexists, nopackagesyet;
if (rdb_initialized || rdb_used) {
fputs("Internal Error: database initialized a 2nd time!\n",
stderr);
return RET_ERROR_INTERNAL;
}
if (readonly && !isdir(global.dbdir)) {
if (verbose >= 0)
fprintf(stderr,
"Exiting without doing anything, as there is no database yet that could result in other actions.\n");
return RET_NOTHING;
}
rdb_initialized = true;
rdb_used = true;
r = database_lock(waitforlock);
assert (r != RET_NOTHING);
if (!RET_IS_OK(r)) {
database_free();
return r;
}
rdb_readonly = readonly;
rdb_verbose = verbosedb;
r = database_hasdatabasefile("packages.db", &packagesfileexists);
if (RET_WAS_ERROR(r)) {
releaselock();
database_free();
return r;
}
r = database_hasdatabasefile("tracking.db", &trackingfileexists);
if (RET_WAS_ERROR(r)) {
releaselock();
database_free();
return r;
}
nopackagesyet = !packagesfileexists && !trackingfileexists;
r = readversionfile(nopackagesyet);
if (RET_WAS_ERROR(r)) {
releaselock();
database_free();
return r;
}
if (nopackages) {
rdb_nopackages = true;
return RET_OK;
}
if (nopackagesyet) {
// TODO: handle readonly, but only once packages files may no
// longer be generated when it is active...
r = createnewdatabase(alldistributions);
if (RET_WAS_ERROR(r)) {
database_close();
return r;
}
}
/* after this point we should call database_close,
* as other stuff was handled,
* so writing the version file cannot harm (and not doing so could) */
if (!allowunused && !fast && packagesfileexists) {
struct strlist identifiers;
r = database_listpackages(&identifiers);
if (RET_WAS_ERROR(r)) {
database_close();
return r;
}
if (r == RET_NOTHING)
strlist_init(&identifiers);
r = warnidentifiers(&identifiers,
alldistributions, readonly);
if (RET_WAS_ERROR(r)) {
strlist_done(&identifiers);
database_close();
return r;
}
strlist_done(&identifiers);
}
if (!allowunused && !fast && trackingfileexists) {
struct strlist codenames;
r = tracking_listdistributions(&codenames);
if (RET_WAS_ERROR(r)) {
database_close();
return r;
}
if (RET_IS_OK(r)) {
r = warnunusedtracking(&codenames, alldistributions);
if (RET_WAS_ERROR(r)) {
strlist_done(&codenames);
database_close();
return r;
}
strlist_done(&codenames);
}
}
return RET_OK;
}
/****************************************************************************
* Stuff string parts *
****************************************************************************/
static const char databaseerror[] = "Internal error of the underlying BerkeleyDB database:\n";
/****************************************************************************
* Stuff to handle data in tables *
****************************************************************************
There is nothing that cannot be solved by another layer of indirection, except
too many levels of indirection. (Source forgotten) */
struct cursor {
DBC *cursor;