summaryrefslogtreecommitdiffstats
path: root/src/cache.c
blob: 6722b555b7f410e04d5439b06d533e9d039329d7 (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
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
/* $Id$ */

/***
  This file is part of syrep.

  syrep is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  syrep 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 syrep; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <db.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "cache.h"
#include "md5util.h"

struct syrep_cache_key {
    uint64_t dev;
    uint64_t inode;
    uint32_t date;
    uint64_t size;
};

struct syrep_cache_data {
    uint8_t digest[16];
    uint32_t timestamp;
};

struct syrep_md_cache {
    DB* db;
    uint32_t timestamp;
    int ro;
};

struct syrep_md_cache* md_cache_open(const char* fn, int ro) {
    struct syrep_md_cache *c = NULL;
    int ret;
    
    assert(fn);

    if (!(c = malloc(sizeof(struct syrep_md_cache))))
        goto fail;

    c->timestamp = time(NULL);
    c->ro = ro;

    if ((ret = db_create(&c->db, NULL, 0))) {
        fprintf(stderr, "db_create: %s\n", db_strerror(ret));
        goto fail;
    }

    if ((ret = c->db->open(c->db, NULL, fn, NULL, DB_BTREE, ro ? DB_RDONLY : DB_CREATE, 0664))) {
        c->db->err(c->db, ret, "open(%s)", fn);
        goto fail;
    }

    return c;

fail:

    if (c) {
        if (c->db)
            c->db->close(c->db, 0);

        free(c);
    }

    return NULL;
}

void md_cache_close(struct syrep_md_cache *c) {
    assert(c);

    if (c->db)
        c->db->close(c->db, 0);

    free(c);
}

static int get(struct syrep_md_cache *c, const struct syrep_cache_key *k, uint8_t digest[16]) {
    int ret;
    DBT key, data;

    assert(c && c->db && k);
    
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    key.data = (void*) k;
    key.size = sizeof(struct syrep_cache_key);
    
    if ((ret = c->db->get(c->db, NULL, &key, &data, 0)) != 0) {
        if (ret == DB_NOTFOUND) {
            //fprintf(stderr, "Cache MISS!\n");
            return 0;
        }
        
        c->db->err(c->db, ret, "cache::get");
        return -1;
    }

    //fprintf(stderr, "Cache HIT\n");

    assert(data.data);
    memcpy(digest, &((struct syrep_cache_data*) data.data)->digest, 16);
    
    return 1;
}

static int put(struct syrep_md_cache *c, const struct syrep_cache_key *k, const uint8_t digest[16], uint32_t timestamp) {
    int ret;
    DBT key, data;
    struct syrep_cache_data d;

    assert(c && c->db && k);
    
    memset(&key, 0, sizeof(key));
    key.data = (void*) k;
    key.size = sizeof(struct syrep_cache_key);

    memcpy(d.digest, digest, 16);
    d.timestamp = timestamp;
    
    memset(&data, 0, sizeof(data));
    data.data = &d;
    data.size = sizeof(struct syrep_cache_data);
        
    if ((ret = c->db->put(c->db, NULL, &key, &data, 0)) != 0) {
        c->db->err(c->db, ret, "cache::put");
        return -1;
    }

    return 0;
}

int md_cache_get(struct syrep_md_cache *c, const char *path, uint8_t digest[16]) {
    struct syrep_cache_key k;
    int r = -1, fd = -1, j;
    struct stat st;
    
    if ((fd = open(path, O_RDONLY)) < 0) {
        fprintf(stderr, "open(\"%s\"): %s\n", path, strerror(errno));
        goto finish;
    }
    
    if (fstat(fd, &st) < 0) {
        fprintf(stderr, "fstat(%s): %s\n", path, strerror(errno));
        goto finish;
    }

    if (!S_ISREG(st.st_mode)) {
        fprintf(stderr, "<%s> not a regular file: %s\n", path, strerror(errno));
        goto finish;
    }

    memset(&k, 0, sizeof(k));
    k.dev = (uint64_t) st.st_dev;
    k.inode = (uint64_t) st.st_ino;
    k.date = (uint32_t) st.st_mtime;
    k.size = (uint64_t) st.st_size;

    if (!c)
        j = 0;
    else
        if ((j = get(c, &k, digest)) < 0)
            goto finish;
    
    if (!j)
        if (fdmd5(fd, st.st_size, digest) < 0)
            goto finish;

    if (c && !c->ro)
        put(c, &k, digest, c->timestamp);

    r = 0;

finish:

    if (fd >= 0)
         close(fd);
    
    return r;
}

int md_cache_vacuum(struct syrep_md_cache*c) {
    int r = -1, ret;
    DBC *cursor;
    DBT key, data;
    int ndel = 0, ntotal = 0;

    if (c->ro)
        return 0;

    assert(c && c->db);
    
    if ((ret = c->db->cursor(c->db, NULL, &cursor, 0)) != 0) {
        c->db->err(c->db, ret, "cache::vacuum");
        return -1;
    }

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    
    while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
        struct syrep_cache_data *d;

        ntotal++;
        d = (struct syrep_cache_data*) data.data;
        assert(d);
        if (d->timestamp < c->timestamp) {
            cursor->c_del(cursor, 0);
            ndel++;
        }
    }

    if (ret != DB_NOTFOUND) {
        c->db->err(c->db, ret, "cache::vacuum()");
        goto finish;
    }

    /*fprintf(stderr, "Cache vacuum successfully completed, %i of %i entries deleted.\n", ndel, ntotal);*/
    
    r = 0;

finish:

    cursor->c_close(cursor);

    return r;
}