-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.c
208 lines (190 loc) · 5.16 KB
/
cache.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
/*
* multihash - compute hashes on collections of files
* Copyright (c) 2016 Nicolas George <[email protected]>
*
* 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.
*/
#define _DEFAULT_SOURCE /* for db.h */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <db.h>
#include "cache.h"
struct Stat_cache {
DB_ENV *db_env;
DB *db;
};
static void
create_parent_directory(char *path, char **end)
{
char *sep = path, *p;
assert(*sep == '/');
for (p = path + 1; *p != 0; p++) {
if (*p == '/') {
*sep = '/';
sep = p;
*(p++) = 0;
if (mkdir(path, 0700) < 0 && errno != EEXIST) {
perror(path);
exit(1);
}
}
}
*end = sep;
}
int
stat_cache_alloc(Stat_cache **rcache)
{
Stat_cache *cache;
cache = malloc(sizeof(*cache));
if (cache == NULL) {
perror("malloc");
return -1;
}
cache->db_env = NULL;
cache->db = NULL;
*rcache = cache;
return 0;
}
void
stat_cache_free(Stat_cache **rcache)
{
Stat_cache *cache = *rcache;
if (cache->db != NULL) {
cache->db->close(cache->db, 0);
cache->db_env->close(cache->db_env, 0);
}
free(cache);
*rcache = NULL;
}
static int
stat_cache_open(Stat_cache *cache)
{
int ret;
char path[2048], *home, *dir_end;
if (cache->db != NULL)
return 0;
home = getenv("HOME");
if (home == NULL) {
fprintf(stderr, "$HOME required\n");
exit(1);
}
ret = snprintf(path, sizeof(path), "%s/.cache/multihash/files.db", home);
if (ret >= (int)sizeof(path)) {
fprintf(stderr, "$HOME too long\n");
exit(1);
}
create_parent_directory(path, &dir_end);
/* leaves \0 at the last / in the path */
ret = db_env_create(&cache->db_env, 0);
if (ret != 0) {
fprintf(stderr, "Failed to create cache database environment: %s\n",
db_strerror(ret));
exit(1);
}
ret = cache->db_env->open(cache->db_env, path,
DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK, 0666);
if (ret != 0) {
fprintf(stderr, "Failed to open cache database environment: %s\n",
db_strerror(ret));
exit(1);
}
ret = db_create(&cache->db, cache->db_env, 0);
if (ret != 0) {
fprintf(stderr, "Failed to create cache database: %s\n",
db_strerror(ret));
exit(1);
}
*dir_end = '/';
ret = cache->db->open(cache->db, NULL, path, "file_hash",
DB_BTREE, DB_CREATE, 0666);
if (ret != 0) {
fprintf(stderr, "Failed to open cache database %s: %s\n",
path, db_strerror(ret));
exit(1);
}
cache->db->sync(cache->db, 0);
return 0;
}
static void
key_from_filename(DBT *dbt, const char *path, const struct stat *st,
const char *hash)
{
unsigned i, l, bufsize = 0;
char *buf = NULL;
for (i = 0; i < 2; i++) {
l = snprintf(buf, bufsize, "%s%c%ju:%ju:%ju.%09u:%s",
path, 0, (uintmax_t)st->st_size, (uintmax_t)st->st_ino,
(uintmax_t)st->st_ctim.tv_sec, (unsigned)st->st_ctim.tv_nsec,
hash);
if (l <= 0) {
perror("snprintf");
exit(1);
}
if (buf == NULL) {
bufsize = l + 1;
buf = malloc(bufsize);
if (buf == NULL) {
perror("malloc");
exit(1);
}
}
}
dbt->data = buf;
dbt->size = bufsize - 1;
}
int
stat_cache_get(Stat_cache *cache, const char *path,
const struct stat *st, const char *hash,
uint8_t *data, size_t size)
{
DBT tkey = { 0 }, tdata = { 0 };
int ret;
if (stat_cache_open(cache) < 0)
return -1;
key_from_filename(&tkey, path, st, hash);
tdata.data = data;
tdata.ulen = size;
tdata.flags = DB_DBT_USERMEM;
ret = cache->db->get(cache->db, NULL, &tkey, &tdata, 0);
free(tkey.data);
if (ret != 0) {
if (ret == DB_NOTFOUND)
return 0;
fprintf(stderr, "Failed to find in cache: %s\n", db_strerror(ret));
return -1;
}
if (tdata.size != size) {
fprintf(stderr, "Inconsistent size for %s:%s\n", hash, path);
return -1;
}
return 1;
}
int stat_cache_set(Stat_cache *cache, const char *path,
const struct stat *st, const char *hash,
uint8_t *data, size_t size)
{
DBT tkey = { 0 }, tdata = { 0 };
int ret;
if (stat_cache_open(cache) < 0)
return -1;
key_from_filename(&tkey, path, st, hash);
tdata.data = data;
tdata.size = size;
ret = cache->db->put(cache->db, NULL, &tkey, &tdata, 0);
if (ret != 0) {
fprintf(stderr, "Failed to insert in cache: %s\n", db_strerror(ret));
exit(1);
}
free(tkey.data);
return 0;
}