summaryrefslogtreecommitdiff
path: root/lib/dbindex.c
blob: f4ea52a8e9c35c996c23cf341585f45db64da6ab (plain)
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
#include "system.h"

#include "rpmlib.h"

dbiIndex * dbiOpenIndex(char * filename, int flags, int perms) {
    dbiIndex * db;
        
    db = malloc(sizeof(*db));
    db->indexname = strdup(filename);
    db->db = dbopen(filename, flags, perms, DB_HASH, NULL);
    if (!db->db) {
	free(db->indexname);
	free(db);
	rpmError(RPMERR_DBOPEN, _("cannot open file %s: "), filename, 
			      strerror(errno));
	return NULL;
    }

    return db;
}

void dbiCloseIndex(dbiIndex * dbi) {
    dbi->db->close(dbi->db);
    free(dbi->indexname);
    free(dbi);
}

void dbiSyncIndex(dbiIndex * dbi) {
    dbi->db->sync(dbi->db, 0);
}

int dbiSearchIndex(dbiIndex * dbi, char * str, dbiIndexSet * set) {
    DBT key, data;
    int rc;

    key.data = str;
    key.size = strlen(str);

    rc = dbi->db->get(dbi->db, &key, &data, 0);
    if (rc == -1) {
	rpmError(RPMERR_DBGETINDEX, _("error getting record %s from %s"),
		str, dbi->indexname);
	return -1;
    } else if (rc == 1) {
	return 1;
    } 

    set->recs = data.data;
    set->recs = malloc(data.size);
    memcpy(set->recs, data.data, data.size);
    set->count = data.size / sizeof(dbiIndexRecord);
    return 0;
}

int dbiUpdateIndex(dbiIndex * dbi, char * str, dbiIndexSet * set) {
   /* 0 on success */
    DBT key, data;
    int rc;

    key.data = str;
    key.size = strlen(str);

    if (set->count) {
	data.data = set->recs;
	data.size = set->count * sizeof(dbiIndexRecord);

	rc = dbi->db->put(dbi->db, &key, &data, 0);
	if (rc) {
	    rpmError(RPMERR_DBPUTINDEX, _("error storing record %s into %s"),
		    str, dbi->indexname);
	    return 1;
	}
    } else {
	rc = dbi->db->del(dbi->db, &key, 0);
	if (rc) {
	    rpmError(RPMERR_DBPUTINDEX, _("error removing record %s into %s"),
		    str, dbi->indexname);
	    return 1;
	}
    }

    return 0;
}

int dbiAppendIndexRecord(dbiIndexSet * set, dbiIndexRecord rec) {
    set->count++;

    if (set->count == 1) {
	set->recs = malloc(set->count * sizeof(dbiIndexRecord));
    } else {
	set->recs = realloc(set->recs, set->count * sizeof(dbiIndexRecord));
    }
    set->recs[set->count - 1] = rec;

    return 0;
}

/* structure return */
dbiIndexSet dbiCreateIndexRecord(void) {
    dbiIndexSet set;

    set.recs = NULL;
    set.count = 0;
    return set;
}
    
void dbiFreeIndexRecord(dbiIndexSet set) {
    free(set.recs);
}

/* returns 1 on failure */
int dbiRemoveIndexRecord(dbiIndexSet * set, dbiIndexRecord rec) {
    int from;
    int to = 0;
    int num = set->count;
    int numCopied = 0;
  
    for (from = 0; from < num; from++) {
	if (rec.recOffset != set->recs[from].recOffset ||
	    rec.fileNumber != set->recs[from].fileNumber) {
	    if (from != to) set->recs[to] = set->recs[from];
	    to++;
	    numCopied++;
	} else {
	    set->count--;
	}
    }

    return (numCopied == num);
}