-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-tokyo.c
251 lines (213 loc) · 5.45 KB
/
cache-tokyo.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
/*
Copyright © 2010 Luca Bruno
This file is part of tdpkg.
tdpkg 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 3 of the License, or
(at your option) any later version.
tdpkg 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 tdpkg. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glob.h>
#include <sys/stat.h>
#include <errno.h>
#include <tchdb.h>
#include "cache.h"
#include "util.h"
#define CACHE_FILE "/var/lib/dpkg/info/tdpkg.cache"
static TCHDB* db = NULL;
static int in_transaction = 0;
static int is_write = 0;
#define tc_error(ret) { fprintf (stderr, "tdpkg tokio: %s\n", tchdberrmsg (tchdbecode (db))); return ret; }
static int
_tokyo_init (int write)
{
if (db && is_write >= write)
return 0;
if (db)
tdpkg_cache_finalize ();
db = tchdbnew ();
int flags = HDBOREADER | HDBOLCKNB;
if (write)
flags |= HDBOWRITER | HDBOCREAT;
if (!tchdbopen (db, CACHE_FILE, flags))
{
int ecode = tchdbecode (db);
if (ecode == TCEMETA || ecode == TCEREAD)
{
tchdbdel (db);
db = NULL;
if (unlink (CACHE_FILE))
return -1;
db = tchdbnew ();
if (!tchdbopen (db, CACHE_FILE, flags))
{
if (tchdbecode (db) != TCENOFILE)
tc_error (-1);
}
}
else if (ecode != TCENOFILE)
tc_error (-1);
}
if (write && !tchdbsync (db))
tc_error (-1);
/* ensure cache consistency with the file system */
struct stat stat_buf;
if (tdpkg_stat (CACHE_FILE, &stat_buf))
{
if (tdpkg_cache_rebuild ())
{
tdpkg_cache_finalize ();
return -1;
}
return 0;
}
time_t db_time = stat_buf.st_mtime;
glob_t glob_list;
if (glob ("/var/lib/dpkg/info/*.list", 0, NULL, &glob_list))
{
fprintf (stderr, "tdpkg sqlite: can't glob /var/lib/dpkg/info/*.list\n");
tdpkg_cache_finalize ();
return -1;
}
int i;
for (i=0; i < glob_list.gl_pathc; i++)
{
const char* filename = glob_list.gl_pathv[i];
struct stat stat_buf;
/* we don't use fstat because it's been wrapped */
if (tdpkg_stat (filename, &stat_buf))
{
fprintf (stderr, "tdpkg sqlite: can't stat %s: %s\n", filename, strerror (errno));
tdpkg_cache_finalize ();
return -1;
}
/* list file more recent than cache */
if (stat_buf.st_mtime > db_time)
{
if (tdpkg_cache_rebuild ())
{
globfree (&glob_list);
tdpkg_cache_finalize ();
return -1;
}
break;
}
}
globfree (&glob_list);
is_write = write;
return 0;
}
int
tdpkg_cache_initialize (void)
{
return 0;
}
void
tdpkg_cache_finalize (void)
{
if (db && !tchdbclose (db))
{
if (tchdbecode (db) != TCENOFILE)
tc_error ();
}
if (db)
tchdbdel (db);
db = NULL;
}
char*
tdpkg_cache_read_filename (const char* filename)
{
if (_tokyo_init (0))
return NULL;
return tchdbget2 (db, filename);
}
int
tdpkg_cache_write_filename (const char* filename)
{
if (_tokyo_init (1))
return -1;
struct stat stat_buf;
if (tdpkg_stat (filename, &stat_buf))
{
fprintf (stderr, "tdpkg tokyo: can't stat %s: %s\n", filename, strerror (errno));
return -1;
}
size_t size = stat_buf.st_size;
FILE* file = fopen (filename, "r");
if (!file)
{
fprintf (stderr, "tdpkg tokyo: can't open %s\n", filename);
return -1;
}
char* contents = malloc (size+1);
if (fread (contents, sizeof (char), size, file) < size)
{
// FIXME: let's handle this?
fprintf (stderr, "tdpkg tokyo: can't read full file %s of size %u\n", filename, size);
fclose (file);
return -1;
}
fclose (file);
contents[size] = '\0';
if (!tchdbputasync2 (db, filename, contents))
{
free (contents);
tc_error (-1);
}
free (contents);
if (!in_transaction && !tchdbsync (db))
tc_error (-1);
return 0;
}
int
tdpkg_cache_delete_filename (const char* filename)
{
if (!tchdbout2 (db, filename))
tc_error (-1);
if (!tchdbsync (db))
tc_error (-1);
return 0;
}
int
tdpkg_cache_rebuild (void)
{
if (_tokyo_init (1))
return -1;
glob_t glob_list;
if (glob ("/var/lib/dpkg/info/*.list", 0, NULL, &glob_list))
{
fprintf (stderr, "tdpkg sqlite: can't glob /var/lib/dpkg/info/*.list\n");
return -1;
}
if (!tchdbvanish (db))
tc_error (-1);
in_transaction = 1;
int i;
for (i=0; i < glob_list.gl_pathc; i++)
{
const char* filename = glob_list.gl_pathv[i];
printf ("tdpkg: (Indexing list file %d...)\r", i+1);
if (tdpkg_cache_write_filename (filename))
{
globfree (&glob_list);
in_transaction = 0;
tchdbvanish (db);
return -1;
}
}
globfree (&glob_list);
in_transaction = 0;
if (!tchdbsync (db))
tc_error (-1);
printf ("tdpkg: %d list files cached succefully\n", i);
return 0;
}