]>
Commit | Line | Data |
---|---|---|
e2faeb46 | 1 | /* |
d980bd40 | 2 | * be_sync.c : backend for sync databases |
23229097 | 3 | * |
d2882404 | 4 | * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org> |
6cebd4e6 | 5 | * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> |
23229097 | 6 | * |
e2faeb46 AF |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
9781d0d6 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
e2faeb46 AF |
19 | */ |
20 | ||
21 | #include "config.h" | |
869e81e1 | 22 | |
db3b86e7 | 23 | #include <errno.h> |
791928dc | 24 | #include <sys/stat.h> |
db3b86e7 | 25 | #include <unistd.h> |
869e81e1 | 26 | |
2f4ee434 XC |
27 | /* libarchive */ |
28 | #include <archive.h> | |
29 | #include <archive_entry.h> | |
30 | ||
869e81e1 | 31 | /* libalpm */ |
e2faeb46 | 32 | #include "util.h" |
5b2de3d8 | 33 | #include "log.h" |
e2faeb46 | 34 | #include "alpm.h" |
5b2de3d8 | 35 | #include "alpm_list.h" |
aa1c0ba9 | 36 | #include "package.h" |
5b2de3d8 | 37 | #include "handle.h" |
0c2cc108 | 38 | #include "delta.h" |
c244cfec | 39 | #include "deps.h" |
04600384 | 40 | #include "dload.h" |
d37ad048 | 41 | |
64c1cf79 | 42 | static char *get_sync_dir(alpm_handle_t *handle) |
f6700d5c DM |
43 | { |
44 | const char *dbpath = alpm_option_get_dbpath(handle); | |
45 | size_t len = strlen(dbpath) + 6; | |
46 | char *syncpath; | |
47 | struct stat buf; | |
48 | ||
afc96f2a | 49 | MALLOC(syncpath, len, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); |
f6700d5c DM |
50 | sprintf(syncpath, "%s%s", dbpath, "sync/"); |
51 | ||
52 | if(stat(syncpath, &buf) != 0) { | |
ca43fdd9 | 53 | _alpm_log(handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", |
f6700d5c DM |
54 | syncpath); |
55 | if(_alpm_makepath(syncpath) != 0) { | |
56 | free(syncpath); | |
afc96f2a | 57 | RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); |
f6700d5c DM |
58 | } |
59 | } else if(!S_ISDIR(buf.st_mode)) { | |
ca43fdd9 | 60 | _alpm_log(handle, ALPM_LOG_WARNING, _("removing invalid file: %s\n"), syncpath); |
f6700d5c DM |
61 | if(unlink(syncpath) != 0 || _alpm_makepath(syncpath) != 0) { |
62 | free(syncpath); | |
afc96f2a | 63 | RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); |
f6700d5c DM |
64 | } |
65 | } | |
66 | ||
67 | return syncpath; | |
68 | } | |
69 | ||
939d5a95 | 70 | static int sync_db_validate(alpm_db_t *db) |
db3b86e7 | 71 | { |
7af0ab1c | 72 | alpm_siglevel_t level; |
79e98316 DM |
73 | |
74 | if(db->status & DB_STATUS_VALID) { | |
75 | return 0; | |
76 | } | |
77 | ||
db3b86e7 DM |
78 | /* this takes into account the default verification level if UNKNOWN |
79 | * was assigned to this db */ | |
7af0ab1c | 80 | level = alpm_db_get_siglevel(db); |
db3b86e7 | 81 | |
7af0ab1c | 82 | if(level & ALPM_SIG_DATABASE) { |
db3b86e7 DM |
83 | const char *dbpath = _alpm_db_path(db); |
84 | if(!dbpath) { | |
85 | /* pm_errno set in _alpm_db_path() */ | |
86 | return -1; | |
87 | } | |
88 | ||
89 | /* we can skip any validation if the database doesn't exist */ | |
90 | if(access(dbpath, R_OK) != 0 && errno == ENOENT) { | |
79e98316 | 91 | goto valid; |
db3b86e7 DM |
92 | return 0; |
93 | } | |
94 | ||
7af0ab1c DM |
95 | if(_alpm_check_pgp_helper(db->handle, dbpath, NULL, |
96 | level & ALPM_SIG_DATABASE_OPTIONAL, level & ALPM_SIG_DATABASE_MARGINAL_OK, | |
97 | level & ALPM_SIG_DATABASE_UNKNOWN_OK, ALPM_ERR_DB_INVALID_SIG)) { | |
98 | return 1; | |
db3b86e7 DM |
99 | } |
100 | } | |
101 | ||
79e98316 DM |
102 | valid: |
103 | db->status |= DB_STATUS_VALID; | |
db3b86e7 DM |
104 | return 0; |
105 | } | |
106 | ||
04600384 | 107 | /** Update a package database |
63fc9360 SN |
108 | * |
109 | * An update of the package database \a db will be attempted. Unless | |
110 | * \a force is true, the update will only be performed if the remote | |
111 | * database was modified since the last update. | |
112 | * | |
4f8ae2ba DM |
113 | * This operation requires a database lock, and will return an applicable error |
114 | * if the lock could not be obtained. | |
63fc9360 SN |
115 | * |
116 | * Example: | |
117 | * @code | |
ed1aef7b | 118 | * alpm_list_t *syncs = alpm_option_get_syncdbs(); |
4f8ae2ba | 119 | * for(i = syncs; i; i = alpm_list_next(i)) { |
939d5a95 | 120 | * alpm_db_t *db = alpm_list_getdata(i); |
4f8ae2ba | 121 | * result = alpm_db_update(0, db); |
63fc9360 | 122 | * |
4f8ae2ba DM |
123 | * if(result < 0) { |
124 | * printf("Unable to update database: %s\n", alpm_strerrorlast()); | |
125 | * } else if(result == 1) { | |
126 | * printf("Database already up to date\n"); | |
127 | * } else { | |
128 | * printf("Database updated\n"); | |
63fc9360 SN |
129 | * } |
130 | * } | |
131 | * @endcode | |
132 | * | |
133 | * @ingroup alpm_databases | |
134 | * @note After a successful update, the \link alpm_db_get_pkgcache() | |
135 | * package cache \endlink will be invalidated | |
04600384 DM |
136 | * @param force if true, then forces the update, otherwise update only in case |
137 | * the database isn't up to date | |
138 | * @param db pointer to the package database to update | |
ed1aef7b | 139 | * @return 0 on success, -1 on error (pm_errno is set accordingly), 1 if up to |
04600384 | 140 | * to date |
e8275fa9 | 141 | */ |
939d5a95 | 142 | int SYMEXPORT alpm_db_update(int force, alpm_db_t *db) |
e8275fa9 | 143 | { |
9579879b | 144 | char *syncpath; |
9579879b | 145 | alpm_list_t *i; |
9579879b | 146 | int ret = -1; |
f6716ae9 | 147 | mode_t oldmask; |
64c1cf79 | 148 | alpm_handle_t *handle; |
7af0ab1c | 149 | alpm_siglevel_t level; |
04600384 | 150 | |
04600384 | 151 | /* Sanity checks */ |
e2aa9526 DM |
152 | ASSERT(db != NULL, return -1); |
153 | handle = db->handle; | |
ee015f08 | 154 | handle->pm_errno = 0; |
afc96f2a AM |
155 | ASSERT(db != handle->db_local, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1)); |
156 | ASSERT(db->servers != NULL, RET_ERR(handle, ALPM_ERR_SERVER_NONE, -1)); | |
0966c33a | 157 | |
f6700d5c DM |
158 | syncpath = get_sync_dir(handle); |
159 | if(!syncpath) { | |
160 | return -1; | |
1ff81182 | 161 | } |
4f8ae2ba DM |
162 | |
163 | /* make sure we have a sane umask */ | |
164 | oldmask = umask(0022); | |
165 | ||
7af0ab1c | 166 | level = alpm_db_get_siglevel(db); |
9579879b | 167 | |
4f8ae2ba DM |
168 | /* attempt to grab a lock */ |
169 | if(_alpm_handle_lock(handle)) { | |
afc96f2a | 170 | RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1); |
4f8ae2ba DM |
171 | } |
172 | ||
9579879b DM |
173 | for(i = db->servers; i; i = i->next) { |
174 | const char *server = i->data; | |
6dc71926 | 175 | struct dload_payload *payload; |
9579879b DM |
176 | size_t len; |
177 | int sig_ret = 0; | |
178 | ||
6dc71926 DR |
179 | CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); |
180 | ||
181 | /* set hard upper limit of 25MiB */ | |
182 | payload->max_size = 25 * 1024 * 1024; | |
183 | ||
9579879b DM |
184 | /* print server + filename into a buffer (leave space for .sig) */ |
185 | len = strlen(server) + strlen(db->treename) + 9; | |
6dc71926 DR |
186 | CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); |
187 | snprintf(payload->fileurl, len, "%s/%s.db", server, db->treename); | |
3eec7459 DR |
188 | payload->handle = handle; |
189 | payload->force = force; | |
9579879b | 190 | |
3eec7459 | 191 | ret = _alpm_download(payload, syncpath, NULL); |
9579879b | 192 | |
7af0ab1c | 193 | if(ret == 0 && (level & ALPM_SIG_DATABASE)) { |
db3b86e7 | 194 | /* an existing sig file is no good at this point */ |
11f4a7a4 | 195 | char *sigpath = _alpm_sigpath(handle, _alpm_db_path(db)); |
db3b86e7 DM |
196 | if(!sigpath) { |
197 | ret = -1; | |
198 | break; | |
199 | } | |
200 | unlink(sigpath); | |
201 | free(sigpath); | |
202 | ||
9579879b | 203 | /* if we downloaded a DB, we want the .sig from the same server */ |
6dc71926 | 204 | snprintf(payload->fileurl, len, "%s/%s.db.sig", server, db->treename); |
3eec7459 DR |
205 | payload->handle = handle; |
206 | payload->force = 1; | |
207 | payload->errors_ok = (level & ALPM_SIG_DATABASE_OPTIONAL); | |
6dc71926 DR |
208 | |
209 | /* set hard upper limit of 16KiB */ | |
210 | payload->max_size = 16 * 1024; | |
9579879b | 211 | |
3eec7459 | 212 | sig_ret = _alpm_download(payload, syncpath, NULL); |
9579879b | 213 | /* errors_ok suppresses error messages, but not the return code */ |
3eec7459 | 214 | sig_ret = payload->errors_ok ? 0 : sig_ret; |
9579879b DM |
215 | } |
216 | ||
6dc71926 | 217 | _alpm_dload_payload_free(payload); |
9579879b DM |
218 | if(ret != -1 && sig_ret != -1) { |
219 | break; | |
220 | } | |
221 | } | |
04600384 DM |
222 | |
223 | if(ret == 1) { | |
d2dbb04a | 224 | /* files match, do nothing */ |
e2aa9526 | 225 | handle->pm_errno = 0; |
38da050f | 226 | goto cleanup; |
04600384 DM |
227 | } else if(ret == -1) { |
228 | /* pm_errno was set by the download code */ | |
ca43fdd9 | 229 | _alpm_log(handle, ALPM_LOG_DEBUG, "failed to sync db: %s\n", |
52bffd24 | 230 | alpm_strerror(handle->pm_errno)); |
38da050f AM |
231 | goto cleanup; |
232 | } | |
233 | ||
2f4ee434 XC |
234 | /* Cache needs to be rebuilt */ |
235 | _alpm_db_free_pkgcache(db); | |
236 | ||
79e98316 | 237 | db->status &= ~DB_STATUS_VALID; |
db3b86e7 DM |
238 | if(sync_db_validate(db)) { |
239 | /* pm_errno should be set */ | |
240 | ret = -1; | |
241 | } | |
242 | ||
38da050f AM |
243 | cleanup: |
244 | ||
4f8ae2ba | 245 | if(_alpm_handle_unlock(handle)) { |
ca43fdd9 | 246 | _alpm_log(handle, ALPM_LOG_WARNING, _("could not remove lock file %s\n"), |
4f8ae2ba DM |
247 | alpm_option_get_lockfile(handle)); |
248 | } | |
38da050f AM |
249 | free(syncpath); |
250 | umask(oldmask); | |
251 | return ret; | |
e8275fa9 AG |
252 | } |
253 | ||
94d3d665 | 254 | /* Forward decl so I don't reorganize the whole file right now */ |
939d5a95 | 255 | static int sync_db_read(alpm_db_t *db, struct archive *archive, |
8a04bc25 | 256 | struct archive_entry *entry, alpm_pkg_t **likely_pkg); |
777bdc6c | 257 | |
8a04bc25 AM |
258 | static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname, |
259 | const char **entry_filename, alpm_pkg_t *likely_pkg) | |
777bdc6c DM |
260 | { |
261 | char *pkgname = NULL, *pkgver = NULL; | |
262 | unsigned long pkgname_hash; | |
8a04bc25 | 263 | alpm_pkg_t *pkg; |
777bdc6c DM |
264 | |
265 | /* get package and db file names */ | |
266 | if(entry_filename) { | |
267 | char *fname = strrchr(entryname, '/'); | |
268 | if(fname) { | |
269 | *entry_filename = fname + 1; | |
270 | } else { | |
271 | *entry_filename = NULL; | |
272 | } | |
273 | } | |
274 | if(_alpm_splitname(entryname, &pkgname, &pkgver, &pkgname_hash) != 0) { | |
ca43fdd9 | 275 | _alpm_log(db->handle, ALPM_LOG_ERROR, |
777bdc6c DM |
276 | _("invalid name for database entry '%s'\n"), entryname); |
277 | return NULL; | |
278 | } | |
279 | ||
280 | if(likely_pkg && strcmp(likely_pkg->name, pkgname) == 0) { | |
281 | pkg = likely_pkg; | |
282 | } else { | |
283 | pkg = _alpm_pkghash_find(db->pkgcache, pkgname); | |
284 | } | |
285 | if(pkg == NULL) { | |
286 | pkg = _alpm_pkg_new(); | |
287 | if(pkg == NULL) { | |
afc96f2a | 288 | RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL); |
777bdc6c DM |
289 | } |
290 | ||
291 | pkg->name = pkgname; | |
292 | pkg->version = pkgver; | |
293 | pkg->name_hash = pkgname_hash; | |
294 | ||
295 | pkg->origin = PKG_FROM_SYNCDB; | |
296 | pkg->origin_data.db = db; | |
297 | pkg->ops = &default_pkg_ops; | |
298 | pkg->handle = db->handle; | |
299 | ||
300 | /* add to the collection */ | |
ca43fdd9 | 301 | _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n", |
777bdc6c DM |
302 | pkg->name, db->treename); |
303 | db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg); | |
304 | } else { | |
305 | free(pkgname); | |
306 | free(pkgver); | |
307 | } | |
308 | ||
309 | return pkg; | |
310 | } | |
94d3d665 | 311 | |
5dae577a DM |
312 | /* |
313 | * This is the data table used to generate the estimating function below. | |
314 | * "Weighted Avg" means averaging the bottom table values; thus each repo, big | |
315 | * or small, will have equal influence. "Unweighted Avg" means averaging the | |
316 | * sums of the top table columns, thus each package has equal influence. The | |
317 | * final values are calculated by (surprise) averaging the averages, because | |
318 | * why the hell not. | |
319 | * | |
320 | * Database Pkgs tar bz2 gz xz | |
321 | * community 2096 5294080 256391 421227 301296 | |
322 | * core 180 460800 25257 36850 29356 | |
323 | * extra 2606 6635520 294647 470818 339392 | |
324 | * multilib 126 327680 16120 23261 18732 | |
325 | * testing 76 204800 10902 14348 12100 | |
326 | * | |
327 | * Bytes Per Package | |
328 | * community 2096 2525.80 122.32 200.97 143.75 | |
329 | * core 180 2560.00 140.32 204.72 163.09 | |
330 | * extra 2606 2546.25 113.06 180.67 130.23 | |
331 | * multilib 126 2600.63 127.94 184.61 148.67 | |
332 | * testing 76 2694.74 143.45 188.79 159.21 | |
333 | ||
334 | * Weighted Avg 2585.48 129.42 191.95 148.99 | |
335 | * Unweighted Avg 2543.39 118.74 190.16 137.93 | |
336 | * Average of Avgs 2564.44 124.08 191.06 143.46 | |
337 | */ | |
d1cc1ef6 | 338 | static size_t estimate_package_count(struct stat *st, struct archive *archive) |
5dae577a DM |
339 | { |
340 | unsigned int per_package; | |
341 | ||
342 | switch(archive_compression(archive)) { | |
343 | case ARCHIVE_COMPRESSION_NONE: | |
344 | per_package = 2564; | |
345 | break; | |
346 | case ARCHIVE_COMPRESSION_GZIP: | |
347 | per_package = 191; | |
348 | break; | |
349 | case ARCHIVE_COMPRESSION_BZIP2: | |
350 | per_package = 124; | |
351 | break; | |
352 | case ARCHIVE_COMPRESSION_COMPRESS: | |
353 | per_package = 193; | |
354 | break; | |
355 | case ARCHIVE_COMPRESSION_LZMA: | |
356 | case ARCHIVE_COMPRESSION_XZ: | |
357 | per_package = 143; | |
358 | break; | |
c3771073 | 359 | #ifdef ARCHIVE_COMPRESSION_UU |
5dae577a DM |
360 | case ARCHIVE_COMPRESSION_UU: |
361 | per_package = 3543; | |
362 | break; | |
c3771073 | 363 | #endif |
5dae577a DM |
364 | default: |
365 | /* assume it is at least somewhat compressed */ | |
366 | per_package = 200; | |
367 | } | |
0303b26b | 368 | return (size_t)((st->st_size / per_package) + 1); |
5dae577a DM |
369 | } |
370 | ||
939d5a95 | 371 | static int sync_db_populate(alpm_db_t *db) |
e2faeb46 | 372 | { |
39fd8bc3 | 373 | const char *dbpath; |
d1cc1ef6 DM |
374 | size_t est_count; |
375 | int count = 0; | |
5dae577a | 376 | struct stat buf; |
5e61f077 AM |
377 | struct archive *archive; |
378 | struct archive_entry *entry; | |
8a04bc25 | 379 | alpm_pkg_t *pkg = NULL; |
e2faeb46 | 380 | |
b14c5477 | 381 | if((archive = archive_read_new()) == NULL) { |
afc96f2a | 382 | RET_ERR(db->handle, ALPM_ERR_LIBARCHIVE, -1); |
b14c5477 | 383 | } |
5e61f077 AM |
384 | |
385 | archive_read_support_compression_all(archive); | |
386 | archive_read_support_format_all(archive); | |
387 | ||
39fd8bc3 DM |
388 | dbpath = _alpm_db_path(db); |
389 | if(!dbpath) { | |
390 | /* pm_errno set in _alpm_db_path() */ | |
b14c5477 | 391 | return -1; |
39fd8bc3 DM |
392 | } |
393 | ||
ca43fdd9 | 394 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "opening database archive %s\n", dbpath); |
39fd8bc3 DM |
395 | |
396 | if(archive_read_open_filename(archive, dbpath, | |
5e61f077 | 397 | ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { |
ca43fdd9 | 398 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), dbpath, |
5e61f077 | 399 | archive_error_string(archive)); |
fe76c353 | 400 | archive_read_finish(archive); |
afc96f2a | 401 | RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1); |
14230869 | 402 | } |
39fd8bc3 | 403 | if(stat(dbpath, &buf) != 0) { |
afc96f2a | 404 | RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1); |
5dae577a DM |
405 | } |
406 | est_count = estimate_package_count(&buf, archive); | |
5e61f077 | 407 | |
02108562 DM |
408 | /* initialize hash at 66% full */ |
409 | db->pkgcache = _alpm_pkghash_create(est_count * 3 / 2); | |
be9a60a3 | 410 | if(db->pkgcache == NULL) { |
afc96f2a | 411 | RET_ERR(db->handle, ALPM_ERR_MEMORY, -1); |
be9a60a3 | 412 | } |
f8fdce6c | 413 | |
5e61f077 AM |
414 | while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { |
415 | const struct stat *st; | |
ccc1c731 | 416 | |
5e61f077 AM |
417 | st = archive_entry_stat(entry); |
418 | ||
4a8e396a | 419 | if(S_ISDIR(st->st_mode)) { |
777bdc6c | 420 | continue; |
4a8e396a AM |
421 | } else { |
422 | /* we have desc, depends or deltas - parse it */ | |
777bdc6c | 423 | if(sync_db_read(db, archive, entry, &pkg) != 0) { |
ca43fdd9 | 424 | _alpm_log(db->handle, ALPM_LOG_ERROR, |
d589a7b5 DM |
425 | _("could not parse package description file '%s' from db '%s'\n"), |
426 | archive_entry_pathname(entry), db->treename); | |
cc25576f DM |
427 | continue; |
428 | } | |
4a8e396a | 429 | } |
e2faeb46 AF |
430 | } |
431 | ||
777bdc6c DM |
432 | count = alpm_list_count(db->pkgcache->list); |
433 | ||
f8fdce6c AM |
434 | if(count > 0) { |
435 | db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp); | |
436 | } | |
5e61f077 | 437 | archive_read_finish(archive); |
ca43fdd9 | 438 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n", |
3045f09e | 439 | count, db->treename); |
5e61f077 | 440 | |
0303b26b | 441 | return count; |
e2faeb46 AF |
442 | } |
443 | ||
de8b9a85 | 444 | #define READ_NEXT() do { \ |
f2dff086 | 445 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ |
cbaff216 DM |
446 | line = buf.line; \ |
447 | _alpm_strip_newline(line); \ | |
8ac7f7e6 DM |
448 | } while(0) |
449 | ||
450 | #define READ_AND_STORE(f) do { \ | |
de8b9a85 | 451 | READ_NEXT(); \ |
8ac7f7e6 DM |
452 | STRDUP(f, line, goto error); \ |
453 | } while(0) | |
454 | ||
455 | #define READ_AND_STORE_ALL(f) do { \ | |
456 | char *linedup; \ | |
cbaff216 DM |
457 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ |
458 | if(_alpm_strip_newline(buf.line) == 0) break; \ | |
459 | STRDUP(linedup, buf.line, goto error); \ | |
8ac7f7e6 DM |
460 | f = alpm_list_add(f, linedup); \ |
461 | } while(1) /* note the while(1) and not (0) */ | |
462 | ||
a628feee DM |
463 | #define READ_AND_SPLITDEP(f) do { \ |
464 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ | |
465 | if(_alpm_strip_newline(buf.line) == 0) break; \ | |
466 | f = alpm_list_add(f, _alpm_splitdep(line)); \ | |
467 | } while(1) /* note the while(1) and not (0) */ | |
468 | ||
939d5a95 | 469 | static int sync_db_read(alpm_db_t *db, struct archive *archive, |
8a04bc25 | 470 | struct archive_entry *entry, alpm_pkg_t **likely_pkg) |
e2faeb46 | 471 | { |
e2aa9526 | 472 | const char *entryname, *filename; |
8a04bc25 | 473 | alpm_pkg_t *pkg; |
f2dff086 | 474 | struct archive_read_buffer buf; |
e2faeb46 | 475 | |
e2aa9526 | 476 | entryname = archive_entry_pathname(entry); |
c2cce1f4 | 477 | if(entryname == NULL) { |
ca43fdd9 | 478 | _alpm_log(db->handle, ALPM_LOG_DEBUG, |
52bffd24 | 479 | "invalid archive entry provided to _alpm_sync_db_read, skipping\n"); |
0303b26b | 480 | return -1; |
e2faeb46 AF |
481 | } |
482 | ||
ca43fdd9 | 483 | _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data from archive entry %s\n", |
4a8e396a AM |
484 | entryname); |
485 | ||
f2dff086 DM |
486 | memset(&buf, 0, sizeof(buf)); |
487 | /* 512K for a line length seems reasonable */ | |
488 | buf.max_line_size = 512 * 1024; | |
489 | ||
777bdc6c DM |
490 | pkg = load_pkg_for_entry(db, entryname, &filename, *likely_pkg); |
491 | ||
4a8e396a | 492 | if(pkg == NULL) { |
ca43fdd9 | 493 | _alpm_log(db->handle, ALPM_LOG_DEBUG, |
777bdc6c DM |
494 | "entry %s could not be loaded into %s sync database", |
495 | entryname, db->treename); | |
0303b26b | 496 | return -1; |
3bf918dc AG |
497 | } |
498 | ||
3030542d DM |
499 | if(strcmp(filename, "desc") == 0 || strcmp(filename, "depends") == 0 |
500 | || strcmp(filename, "deltas") == 0) { | |
cc25576f DM |
501 | int ret; |
502 | while((ret = _alpm_archive_fgets(archive, &buf)) == ARCHIVE_OK) { | |
cbaff216 DM |
503 | char *line = buf.line; |
504 | _alpm_strip_newline(line); | |
f2dff086 | 505 | |
da1c11cc | 506 | if(strcmp(line, "%NAME%") == 0) { |
de8b9a85 | 507 | READ_NEXT(); |
8ac7f7e6 | 508 | if(strcmp(line, pkg->name) != 0) { |
ca43fdd9 | 509 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name " |
4a8e396a | 510 | "mismatch on package %s\n"), db->treename, pkg->name); |
da1c11cc NG |
511 | } |
512 | } else if(strcmp(line, "%VERSION%") == 0) { | |
de8b9a85 | 513 | READ_NEXT(); |
8ac7f7e6 | 514 | if(strcmp(line, pkg->version) != 0) { |
ca43fdd9 | 515 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version " |
4a8e396a | 516 | "mismatch on package %s\n"), db->treename, pkg->name); |
da1c11cc NG |
517 | } |
518 | } else if(strcmp(line, "%FILENAME%") == 0) { | |
8ac7f7e6 | 519 | READ_AND_STORE(pkg->filename); |
da1c11cc | 520 | } else if(strcmp(line, "%DESC%") == 0) { |
8ac7f7e6 | 521 | READ_AND_STORE(pkg->desc); |
ccc1c731 | 522 | } else if(strcmp(line, "%GROUPS%") == 0) { |
8ac7f7e6 | 523 | READ_AND_STORE_ALL(pkg->groups); |
ccc1c731 | 524 | } else if(strcmp(line, "%URL%") == 0) { |
8ac7f7e6 | 525 | READ_AND_STORE(pkg->url); |
ccc1c731 | 526 | } else if(strcmp(line, "%LICENSE%") == 0) { |
8ac7f7e6 | 527 | READ_AND_STORE_ALL(pkg->licenses); |
ccc1c731 | 528 | } else if(strcmp(line, "%ARCH%") == 0) { |
8ac7f7e6 | 529 | READ_AND_STORE(pkg->arch); |
ccc1c731 | 530 | } else if(strcmp(line, "%BUILDDATE%") == 0) { |
de8b9a85 | 531 | READ_NEXT(); |
4bc6ed56 | 532 | pkg->builddate = _alpm_parsedate(line); |
ccc1c731 | 533 | } else if(strcmp(line, "%PACKAGER%") == 0) { |
8ac7f7e6 | 534 | READ_AND_STORE(pkg->packager); |
5b17d8f2 | 535 | } else if(strcmp(line, "%CSIZE%") == 0) { |
8ac7f7e6 DM |
536 | /* Note: the CSIZE and SIZE fields both share the "size" field in the |
537 | * pkginfo_t struct. This can be done b/c CSIZE is currently only used | |
538 | * in sync databases, and SIZE is only used in local databases. | |
e2faeb46 | 539 | */ |
de8b9a85 | 540 | READ_NEXT(); |
8ac7f7e6 | 541 | pkg->size = atol(line); |
5e12d3de | 542 | /* also store this value to isize if isize is unset */ |
4a8e396a AM |
543 | if(pkg->isize == 0) { |
544 | pkg->isize = pkg->size; | |
5e12d3de | 545 | } |
ccc1c731 | 546 | } else if(strcmp(line, "%ISIZE%") == 0) { |
de8b9a85 | 547 | READ_NEXT(); |
8ac7f7e6 | 548 | pkg->isize = atol(line); |
ccc1c731 | 549 | } else if(strcmp(line, "%MD5SUM%") == 0) { |
8ac7f7e6 | 550 | READ_AND_STORE(pkg->md5sum); |
a31d091f | 551 | } else if(strcmp(line, "%SHA256SUM%") == 0) { |
ebb2e36c | 552 | READ_AND_STORE(pkg->sha256sum); |
39ce9b3a | 553 | } else if(strcmp(line, "%PGPSIG%") == 0) { |
225acbbf | 554 | READ_AND_STORE(pkg->base64_sig); |
ccc1c731 | 555 | } else if(strcmp(line, "%REPLACES%") == 0) { |
a628feee | 556 | READ_AND_SPLITDEP(pkg->replaces); |
a44c7b89 | 557 | } else if(strcmp(line, "%DEPENDS%") == 0) { |
a628feee | 558 | READ_AND_SPLITDEP(pkg->depends); |
ccc1c731 | 559 | } else if(strcmp(line, "%OPTDEPENDS%") == 0) { |
8ac7f7e6 | 560 | READ_AND_STORE_ALL(pkg->optdepends); |
ccc1c731 | 561 | } else if(strcmp(line, "%CONFLICTS%") == 0) { |
a628feee | 562 | READ_AND_SPLITDEP(pkg->conflicts); |
ccc1c731 | 563 | } else if(strcmp(line, "%PROVIDES%") == 0) { |
a628feee | 564 | READ_AND_SPLITDEP(pkg->provides); |
3030542d | 565 | } else if(strcmp(line, "%DELTAS%") == 0) { |
b48f7184 | 566 | /* Different than the rest because of the _alpm_delta_parse call. */ |
567 | while(1) { | |
de8b9a85 | 568 | READ_NEXT(); |
b48f7184 | 569 | if(strlen(line) == 0) break; |
570 | pkg->deltas = alpm_list_add(pkg->deltas, _alpm_delta_parse(line)); | |
571 | } | |
3030542d | 572 | } |
e2faeb46 | 573 | } |
cc25576f DM |
574 | if(ret != ARCHIVE_EOF) { |
575 | goto error; | |
576 | } | |
777bdc6c | 577 | *likely_pkg = pkg; |
f2023176 AM |
578 | } else if(strcmp(filename, "files") == 0) { |
579 | /* currently do nothing with this file */ | |
4a8e396a | 580 | } else { |
f2023176 | 581 | /* unknown database file */ |
ca43fdd9 | 582 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "unknown database file: %s\n", filename); |
e2faeb46 AF |
583 | } |
584 | ||
cc25576f DM |
585 | return 0; |
586 | ||
e2faeb46 | 587 | error: |
ca43fdd9 | 588 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "error parsing database file: %s\n", filename); |
cc25576f | 589 | return -1; |
e2faeb46 AF |
590 | } |
591 | ||
fc32faaa | 592 | struct db_operations sync_db_ops = { |
68284da0 | 593 | .validate = sync_db_validate, |
c00e0599 | 594 | .populate = sync_db_populate, |
fc32faaa AM |
595 | .unregister = _alpm_db_unregister, |
596 | }; | |
597 | ||
939d5a95 | 598 | alpm_db_t *_alpm_db_register_sync(alpm_handle_t *handle, const char *treename, |
7af0ab1c | 599 | alpm_siglevel_t level) |
fc32faaa | 600 | { |
939d5a95 | 601 | alpm_db_t *db; |
fc32faaa | 602 | |
ca43fdd9 | 603 | _alpm_log(handle, ALPM_LOG_DEBUG, "registering sync database '%s'\n", treename); |
fc32faaa | 604 | |
05f7c028 | 605 | #ifndef HAVE_LIBGPGME |
05f7c028 DM |
606 | if(level != 0 && level != ALPM_SIG_USE_DEFAULT) { |
607 | RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL); | |
608 | } | |
609 | #endif | |
610 | ||
fc32faaa | 611 | db = _alpm_db_new(treename, 0); |
fc32faaa | 612 | if(db == NULL) { |
afc96f2a | 613 | RET_ERR(handle, ALPM_ERR_DB_CREATE, NULL); |
fc32faaa | 614 | } |
be9a60a3 | 615 | db->ops = &sync_db_ops; |
c47d25d7 | 616 | db->handle = handle; |
7af0ab1c | 617 | db->siglevel = level; |
db3b86e7 | 618 | |
c748eadc | 619 | sync_db_validate(db); |
fc32faaa AM |
620 | |
621 | handle->dbs_sync = alpm_list_add(handle->dbs_sync, db); | |
0303b26b | 622 | return db; |
fc32faaa AM |
623 | } |
624 | ||
e2faeb46 | 625 | /* vim: set ts=2 sw=2 noet: */ |