]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * be_sync.c : backend for sync databases | |
3 | * | |
4 | * Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org> | |
5 | * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> | |
6 | * | |
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 | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <limits.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | /* libarchive */ | |
29 | #include <archive.h> | |
30 | #include <archive_entry.h> | |
31 | ||
32 | /* libalpm */ | |
33 | #include "util.h" | |
34 | #include "log.h" | |
35 | #include "libarchive-compat.h" | |
36 | #include "alpm.h" | |
37 | #include "alpm_list.h" | |
38 | #include "package.h" | |
39 | #include "handle.h" | |
40 | #include "delta.h" | |
41 | #include "deps.h" | |
42 | #include "dload.h" | |
43 | ||
44 | static char *get_sync_dir(alpm_handle_t *handle) | |
45 | { | |
46 | size_t len = strlen(handle->dbpath) + 6; | |
47 | char *syncpath; | |
48 | struct stat buf; | |
49 | ||
50 | MALLOC(syncpath, len, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); | |
51 | sprintf(syncpath, "%s%s", handle->dbpath, "sync/"); | |
52 | ||
53 | if(stat(syncpath, &buf) != 0) { | |
54 | _alpm_log(handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", | |
55 | syncpath); | |
56 | if(_alpm_makepath(syncpath) != 0) { | |
57 | free(syncpath); | |
58 | RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); | |
59 | } | |
60 | } else if(!S_ISDIR(buf.st_mode)) { | |
61 | _alpm_log(handle, ALPM_LOG_WARNING, _("removing invalid file: %s\n"), syncpath); | |
62 | if(unlink(syncpath) != 0 || _alpm_makepath(syncpath) != 0) { | |
63 | free(syncpath); | |
64 | RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); | |
65 | } | |
66 | } | |
67 | ||
68 | return syncpath; | |
69 | } | |
70 | ||
71 | static int sync_db_validate(alpm_db_t *db) | |
72 | { | |
73 | alpm_siglevel_t level; | |
74 | const char *dbpath; | |
75 | ||
76 | if(db->status & DB_STATUS_VALID || db->status & DB_STATUS_MISSING) { | |
77 | return 0; | |
78 | } | |
79 | if(db->status & DB_STATUS_INVALID) { | |
80 | db->handle->pm_errno = ALPM_ERR_DB_INVALID_SIG; | |
81 | return -1; | |
82 | } | |
83 | ||
84 | dbpath = _alpm_db_path(db); | |
85 | if(!dbpath) { | |
86 | /* pm_errno set in _alpm_db_path() */ | |
87 | return -1; | |
88 | } | |
89 | ||
90 | /* we can skip any validation if the database doesn't exist */ | |
91 | if(_alpm_access(db->handle, NULL, dbpath, R_OK) != 0 && errno == ENOENT) { | |
92 | db->status &= ~DB_STATUS_EXISTS; | |
93 | db->status |= DB_STATUS_MISSING; | |
94 | EVENT(db->handle, ALPM_EVENT_DATABASE_MISSING, db->treename, NULL); | |
95 | goto valid; | |
96 | } | |
97 | db->status |= DB_STATUS_EXISTS; | |
98 | db->status &= ~DB_STATUS_MISSING; | |
99 | ||
100 | /* this takes into account the default verification level if UNKNOWN | |
101 | * was assigned to this db */ | |
102 | level = alpm_db_get_siglevel(db); | |
103 | ||
104 | if(level & ALPM_SIG_DATABASE) { | |
105 | int retry, ret; | |
106 | do { | |
107 | retry = 0; | |
108 | alpm_siglist_t *siglist; | |
109 | ret = _alpm_check_pgp_helper(db->handle, dbpath, NULL, | |
110 | level & ALPM_SIG_DATABASE_OPTIONAL, level & ALPM_SIG_DATABASE_MARGINAL_OK, | |
111 | level & ALPM_SIG_DATABASE_UNKNOWN_OK, &siglist); | |
112 | if(ret) { | |
113 | retry = _alpm_process_siglist(db->handle, db->treename, siglist, | |
114 | level & ALPM_SIG_DATABASE_OPTIONAL, level & ALPM_SIG_DATABASE_MARGINAL_OK, | |
115 | level & ALPM_SIG_DATABASE_UNKNOWN_OK); | |
116 | } | |
117 | alpm_siglist_cleanup(siglist); | |
118 | free(siglist); | |
119 | } while(retry); | |
120 | ||
121 | if(ret) { | |
122 | db->status &= ~DB_STATUS_VALID; | |
123 | db->status |= DB_STATUS_INVALID; | |
124 | db->handle->pm_errno = ALPM_ERR_DB_INVALID_SIG; | |
125 | return 1; | |
126 | } | |
127 | } | |
128 | ||
129 | valid: | |
130 | db->status |= DB_STATUS_VALID; | |
131 | db->status &= ~DB_STATUS_INVALID; | |
132 | return 0; | |
133 | } | |
134 | ||
135 | /** Update a package database | |
136 | * | |
137 | * An update of the package database \a db will be attempted. Unless | |
138 | * \a force is true, the update will only be performed if the remote | |
139 | * database was modified since the last update. | |
140 | * | |
141 | * This operation requires a database lock, and will return an applicable error | |
142 | * if the lock could not be obtained. | |
143 | * | |
144 | * Example: | |
145 | * @code | |
146 | * alpm_list_t *syncs = alpm_get_syncdbs(); | |
147 | * for(i = syncs; i; i = alpm_list_next(i)) { | |
148 | * alpm_db_t *db = alpm_list_getdata(i); | |
149 | * result = alpm_db_update(0, db); | |
150 | * | |
151 | * if(result < 0) { | |
152 | * printf("Unable to update database: %s\n", alpm_strerrorlast()); | |
153 | * } else if(result == 1) { | |
154 | * printf("Database already up to date\n"); | |
155 | * } else { | |
156 | * printf("Database updated\n"); | |
157 | * } | |
158 | * } | |
159 | * @endcode | |
160 | * | |
161 | * @ingroup alpm_databases | |
162 | * @note After a successful update, the \link alpm_db_get_pkgcache() | |
163 | * package cache \endlink will be invalidated | |
164 | * @param force if true, then forces the update, otherwise update only in case | |
165 | * the database isn't up to date | |
166 | * @param db pointer to the package database to update | |
167 | * @return 0 on success, -1 on error (pm_errno is set accordingly), 1 if up to | |
168 | * to date | |
169 | */ | |
170 | int SYMEXPORT alpm_db_update(int force, alpm_db_t *db) | |
171 | { | |
172 | char *syncpath; | |
173 | alpm_list_t *i; | |
174 | int ret = -1; | |
175 | mode_t oldmask; | |
176 | alpm_handle_t *handle; | |
177 | alpm_siglevel_t level; | |
178 | ||
179 | /* Sanity checks */ | |
180 | ASSERT(db != NULL, return -1); | |
181 | handle = db->handle; | |
182 | handle->pm_errno = 0; | |
183 | ASSERT(db != handle->db_local, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1)); | |
184 | ASSERT(db->servers != NULL, RET_ERR(handle, ALPM_ERR_SERVER_NONE, -1)); | |
185 | ||
186 | if(!(db->usage & ALPM_DB_USAGE_SYNC)) { | |
187 | return 0; | |
188 | } | |
189 | ||
190 | syncpath = get_sync_dir(handle); | |
191 | if(!syncpath) { | |
192 | return -1; | |
193 | } | |
194 | ||
195 | /* make sure we have a sane umask */ | |
196 | oldmask = umask(0022); | |
197 | ||
198 | level = alpm_db_get_siglevel(db); | |
199 | ||
200 | /* attempt to grab a lock */ | |
201 | if(_alpm_handle_lock(handle)) { | |
202 | free(syncpath); | |
203 | umask(oldmask); | |
204 | RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1); | |
205 | } | |
206 | ||
207 | for(i = db->servers; i; i = i->next) { | |
208 | const char *server = i->data; | |
209 | struct dload_payload payload; | |
210 | size_t len; | |
211 | int sig_ret = 0; | |
212 | ||
213 | memset(&payload, 0, sizeof(struct dload_payload)); | |
214 | ||
215 | /* set hard upper limit of 25MiB */ | |
216 | payload.max_size = 25 * 1024 * 1024; | |
217 | ||
218 | /* print server + filename into a buffer */ | |
219 | len = strlen(server) + strlen(db->treename) + 5; | |
220 | /* TODO fix leak syncpath and umask unset */ | |
221 | MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); | |
222 | snprintf(payload.fileurl, len, "%s/%s.db", server, db->treename); | |
223 | payload.handle = handle; | |
224 | payload.force = force; | |
225 | payload.unlink_on_fail = 1; | |
226 | ||
227 | ret = _alpm_download(&payload, syncpath, NULL, NULL); | |
228 | _alpm_dload_payload_reset(&payload); | |
229 | ||
230 | if(ret == 0 && (level & ALPM_SIG_DATABASE)) { | |
231 | /* an existing sig file is no good at this point */ | |
232 | char *sigpath = _alpm_sigpath(handle, _alpm_db_path(db)); | |
233 | if(!sigpath) { | |
234 | ret = -1; | |
235 | break; | |
236 | } | |
237 | unlink(sigpath); | |
238 | free(sigpath); | |
239 | ||
240 | /* if we downloaded a DB, we want the .sig from the same server */ | |
241 | /* print server + filename into a buffer (leave space for .sig) */ | |
242 | len = strlen(server) + strlen(db->treename) + 9; | |
243 | /* TODO fix leak syncpath and umask unset */ | |
244 | MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); | |
245 | snprintf(payload.fileurl, len, "%s/%s.db.sig", server, db->treename); | |
246 | payload.handle = handle; | |
247 | payload.force = 1; | |
248 | payload.errors_ok = (level & ALPM_SIG_DATABASE_OPTIONAL); | |
249 | ||
250 | /* set hard upper limit of 16KiB */ | |
251 | payload.max_size = 16 * 1024; | |
252 | ||
253 | sig_ret = _alpm_download(&payload, syncpath, NULL, NULL); | |
254 | /* errors_ok suppresses error messages, but not the return code */ | |
255 | sig_ret = payload.errors_ok ? 0 : sig_ret; | |
256 | _alpm_dload_payload_reset(&payload); | |
257 | } | |
258 | ||
259 | if(ret != -1 && sig_ret != -1) { | |
260 | break; | |
261 | } | |
262 | } | |
263 | ||
264 | if(ret == 1) { | |
265 | /* files match, do nothing */ | |
266 | handle->pm_errno = 0; | |
267 | goto cleanup; | |
268 | } else if(ret == -1) { | |
269 | /* pm_errno was set by the download code */ | |
270 | _alpm_log(handle, ALPM_LOG_DEBUG, "failed to sync db: %s\n", | |
271 | alpm_strerror(handle->pm_errno)); | |
272 | goto cleanup; | |
273 | } | |
274 | ||
275 | /* Cache needs to be rebuilt */ | |
276 | _alpm_db_free_pkgcache(db); | |
277 | ||
278 | /* clear all status flags regarding validity/existence */ | |
279 | db->status &= ~DB_STATUS_VALID; | |
280 | db->status &= ~DB_STATUS_INVALID; | |
281 | db->status &= ~DB_STATUS_EXISTS; | |
282 | db->status &= ~DB_STATUS_MISSING; | |
283 | ||
284 | if(sync_db_validate(db)) { | |
285 | /* pm_errno should be set */ | |
286 | ret = -1; | |
287 | } | |
288 | ||
289 | cleanup: | |
290 | ||
291 | if(_alpm_handle_unlock(handle)) { | |
292 | _alpm_log(handle, ALPM_LOG_WARNING, _("could not remove lock file %s\n"), | |
293 | handle->lockfile); | |
294 | } | |
295 | free(syncpath); | |
296 | umask(oldmask); | |
297 | return ret; | |
298 | } | |
299 | ||
300 | /* Forward decl so I don't reorganize the whole file right now */ | |
301 | static int sync_db_read(alpm_db_t *db, struct archive *archive, | |
302 | struct archive_entry *entry, alpm_pkg_t **likely_pkg); | |
303 | ||
304 | static alpm_pkgvalidation_t _sync_get_validation(alpm_pkg_t *pkg) | |
305 | { | |
306 | if(pkg->validation) { | |
307 | return pkg->validation; | |
308 | } | |
309 | ||
310 | if(pkg->md5sum) { | |
311 | pkg->validation |= ALPM_PKG_VALIDATION_MD5SUM; | |
312 | } | |
313 | if(pkg->sha256sum) { | |
314 | pkg->validation |= ALPM_PKG_VALIDATION_SHA256SUM; | |
315 | } | |
316 | if(pkg->base64_sig) { | |
317 | pkg->validation |= ALPM_PKG_VALIDATION_SIGNATURE; | |
318 | } | |
319 | ||
320 | if(!pkg->validation) { | |
321 | pkg->validation |= ALPM_PKG_VALIDATION_NONE; | |
322 | } | |
323 | ||
324 | return pkg->validation; | |
325 | } | |
326 | ||
327 | static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname, | |
328 | const char **entry_filename, alpm_pkg_t *likely_pkg) | |
329 | { | |
330 | char *pkgname = NULL, *pkgver = NULL; | |
331 | unsigned long pkgname_hash; | |
332 | alpm_pkg_t *pkg; | |
333 | ||
334 | /* get package and db file names */ | |
335 | if(entry_filename) { | |
336 | char *fname = strrchr(entryname, '/'); | |
337 | if(fname) { | |
338 | *entry_filename = fname + 1; | |
339 | } else { | |
340 | *entry_filename = NULL; | |
341 | } | |
342 | } | |
343 | if(_alpm_splitname(entryname, &pkgname, &pkgver, &pkgname_hash) != 0) { | |
344 | _alpm_log(db->handle, ALPM_LOG_ERROR, | |
345 | _("invalid name for database entry '%s'\n"), entryname); | |
346 | return NULL; | |
347 | } | |
348 | ||
349 | if(likely_pkg && pkgname_hash == likely_pkg->name_hash | |
350 | && strcmp(likely_pkg->name, pkgname) == 0) { | |
351 | pkg = likely_pkg; | |
352 | } else { | |
353 | pkg = _alpm_pkghash_find(db->pkgcache, pkgname); | |
354 | } | |
355 | if(pkg == NULL) { | |
356 | pkg = _alpm_pkg_new(); | |
357 | if(pkg == NULL) { | |
358 | RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL); | |
359 | } | |
360 | ||
361 | pkg->name = pkgname; | |
362 | pkg->version = pkgver; | |
363 | pkg->name_hash = pkgname_hash; | |
364 | ||
365 | pkg->origin = ALPM_PKG_FROM_SYNCDB; | |
366 | pkg->origin_data.db = db; | |
367 | pkg->ops = &default_pkg_ops; | |
368 | pkg->ops->get_validation = _sync_get_validation; | |
369 | pkg->handle = db->handle; | |
370 | ||
371 | /* add to the collection */ | |
372 | _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n", | |
373 | pkg->name, db->treename); | |
374 | db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg); | |
375 | } else { | |
376 | free(pkgname); | |
377 | free(pkgver); | |
378 | } | |
379 | ||
380 | return pkg; | |
381 | } | |
382 | ||
383 | /* This function doesn't work as well as one might think, as size of database | |
384 | * entries varies considerably. Adding signatures nearly doubles the size of a | |
385 | * single entry; deltas also can make for large variations in size. These | |
386 | * current values are heavily influenced by Arch Linux; databases with no | |
387 | * deltas and a single signature per package. */ | |
388 | static size_t estimate_package_count(struct stat *st, struct archive *archive) | |
389 | { | |
390 | int per_package; | |
391 | ||
392 | switch(_alpm_archive_filter_code(archive)) { | |
393 | case ARCHIVE_COMPRESSION_NONE: | |
394 | per_package = 3015; | |
395 | break; | |
396 | case ARCHIVE_COMPRESSION_GZIP: | |
397 | case ARCHIVE_COMPRESSION_COMPRESS: | |
398 | per_package = 464; | |
399 | break; | |
400 | case ARCHIVE_COMPRESSION_BZIP2: | |
401 | per_package = 394; | |
402 | break; | |
403 | case ARCHIVE_COMPRESSION_LZMA: | |
404 | case ARCHIVE_COMPRESSION_XZ: | |
405 | per_package = 400; | |
406 | break; | |
407 | #ifdef ARCHIVE_COMPRESSION_UU | |
408 | case ARCHIVE_COMPRESSION_UU: | |
409 | per_package = 3015 * 4 / 3; | |
410 | break; | |
411 | #endif | |
412 | default: | |
413 | /* assume it is at least somewhat compressed */ | |
414 | per_package = 500; | |
415 | } | |
416 | return (size_t)((st->st_size / per_package) + 1); | |
417 | } | |
418 | ||
419 | static int sync_db_populate(alpm_db_t *db) | |
420 | { | |
421 | const char *dbpath; | |
422 | size_t est_count; | |
423 | int count, fd; | |
424 | struct stat buf; | |
425 | struct archive *archive; | |
426 | struct archive_entry *entry; | |
427 | alpm_pkg_t *pkg = NULL; | |
428 | ||
429 | if(db->status & DB_STATUS_INVALID) { | |
430 | RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1); | |
431 | } | |
432 | if(db->status & DB_STATUS_MISSING) { | |
433 | RET_ERR(db->handle, ALPM_ERR_DB_NOT_FOUND, -1); | |
434 | } | |
435 | dbpath = _alpm_db_path(db); | |
436 | if(!dbpath) { | |
437 | /* pm_errno set in _alpm_db_path() */ | |
438 | return -1; | |
439 | } | |
440 | ||
441 | fd = _alpm_open_archive(db->handle, dbpath, &buf, | |
442 | &archive, ALPM_ERR_DB_OPEN); | |
443 | if(fd < 0) { | |
444 | return -1; | |
445 | } | |
446 | est_count = estimate_package_count(&buf, archive); | |
447 | ||
448 | db->pkgcache = _alpm_pkghash_create(est_count); | |
449 | if(db->pkgcache == NULL) { | |
450 | db->handle->pm_errno = ALPM_ERR_MEMORY; | |
451 | count = -1; | |
452 | goto cleanup; | |
453 | } | |
454 | ||
455 | while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { | |
456 | mode_t mode = archive_entry_mode(entry); | |
457 | if(S_ISDIR(mode)) { | |
458 | continue; | |
459 | } else { | |
460 | /* we have desc, depends or deltas - parse it */ | |
461 | if(sync_db_read(db, archive, entry, &pkg) != 0) { | |
462 | _alpm_log(db->handle, ALPM_LOG_ERROR, | |
463 | _("could not parse package description file '%s' from db '%s'\n"), | |
464 | archive_entry_pathname(entry), db->treename); | |
465 | continue; | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
470 | count = alpm_list_count(db->pkgcache->list); | |
471 | if(count > 0) { | |
472 | db->pkgcache->list = alpm_list_msort(db->pkgcache->list, | |
473 | (size_t)count, _alpm_pkg_cmp); | |
474 | } | |
475 | _alpm_log(db->handle, ALPM_LOG_DEBUG, | |
476 | "added %d packages to package cache for db '%s'\n", | |
477 | count, db->treename); | |
478 | ||
479 | cleanup: | |
480 | _alpm_archive_read_free(archive); | |
481 | if(fd >= 0) { | |
482 | close(fd); | |
483 | } | |
484 | return count; | |
485 | } | |
486 | ||
487 | /* This function validates %FILENAME%. filename must be between 3 and | |
488 | * PATH_MAX characters and cannot be contain a path */ | |
489 | static int _alpm_validate_filename(alpm_db_t *db, const char *pkgname, | |
490 | const char *filename) | |
491 | { | |
492 | size_t len = strlen(filename); | |
493 | ||
494 | if(filename[0] == '.') { | |
495 | errno = EINVAL; | |
496 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: filename " | |
497 | "of package %s is illegal\n"), db->treename, pkgname); | |
498 | return -1; | |
499 | } else if(memchr(filename, '/', len) != NULL) { | |
500 | errno = EINVAL; | |
501 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: filename " | |
502 | "of package %s is illegal\n"), db->treename, pkgname); | |
503 | return -1; | |
504 | } else if(len > PATH_MAX) { | |
505 | errno = EINVAL; | |
506 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: filename " | |
507 | "of package %s is too long\n"), db->treename, pkgname); | |
508 | return -1; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | #define READ_NEXT() do { \ | |
515 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ | |
516 | line = buf.line; \ | |
517 | _alpm_strip_newline(line, buf.real_line_size); \ | |
518 | } while(0) | |
519 | ||
520 | #define READ_AND_STORE(f) do { \ | |
521 | READ_NEXT(); \ | |
522 | STRDUP(f, line, goto error); \ | |
523 | } while(0) | |
524 | ||
525 | #define READ_AND_STORE_ALL(f) do { \ | |
526 | char *linedup; \ | |
527 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ | |
528 | if(_alpm_strip_newline(buf.line, buf.real_line_size) == 0) break; \ | |
529 | STRDUP(linedup, buf.line, goto error); \ | |
530 | f = alpm_list_add(f, linedup); \ | |
531 | } while(1) /* note the while(1) and not (0) */ | |
532 | ||
533 | #define READ_AND_SPLITDEP(f) do { \ | |
534 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ | |
535 | if(_alpm_strip_newline(buf.line, buf.real_line_size) == 0) break; \ | |
536 | f = alpm_list_add(f, _alpm_splitdep(line)); \ | |
537 | } while(1) /* note the while(1) and not (0) */ | |
538 | ||
539 | static int sync_db_read(alpm_db_t *db, struct archive *archive, | |
540 | struct archive_entry *entry, alpm_pkg_t **likely_pkg) | |
541 | { | |
542 | const char *entryname, *filename; | |
543 | alpm_pkg_t *pkg; | |
544 | struct archive_read_buffer buf; | |
545 | ||
546 | entryname = archive_entry_pathname(entry); | |
547 | if(entryname == NULL) { | |
548 | _alpm_log(db->handle, ALPM_LOG_DEBUG, | |
549 | "invalid archive entry provided to _alpm_sync_db_read, skipping\n"); | |
550 | return -1; | |
551 | } | |
552 | ||
553 | _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data from archive entry %s\n", | |
554 | entryname); | |
555 | ||
556 | memset(&buf, 0, sizeof(buf)); | |
557 | /* 512K for a line length seems reasonable */ | |
558 | buf.max_line_size = 512 * 1024; | |
559 | ||
560 | pkg = load_pkg_for_entry(db, entryname, &filename, *likely_pkg); | |
561 | ||
562 | if(pkg == NULL) { | |
563 | _alpm_log(db->handle, ALPM_LOG_DEBUG, | |
564 | "entry %s could not be loaded into %s sync database", | |
565 | entryname, db->treename); | |
566 | return -1; | |
567 | } | |
568 | ||
569 | if(filename == NULL) { | |
570 | /* A file exists outside of a subdirectory. This isn't a read error, so return | |
571 | * success and try to continue on. */ | |
572 | _alpm_log(db->handle, ALPM_LOG_WARNING, _("unknown database file: %s\n"), | |
573 | filename); | |
574 | return 0; | |
575 | } | |
576 | ||
577 | if(strcmp(filename, "desc") == 0 || strcmp(filename, "depends") == 0 | |
578 | || (strcmp(filename, "deltas") == 0 && db->handle->deltaratio > 0.0) ) { | |
579 | int ret; | |
580 | while((ret = _alpm_archive_fgets(archive, &buf)) == ARCHIVE_OK) { | |
581 | char *line = buf.line; | |
582 | if(_alpm_strip_newline(line, buf.real_line_size) == 0) { | |
583 | /* length of stripped line was zero */ | |
584 | continue; | |
585 | } | |
586 | ||
587 | if(strcmp(line, "%NAME%") == 0) { | |
588 | READ_NEXT(); | |
589 | if(strcmp(line, pkg->name) != 0) { | |
590 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name " | |
591 | "mismatch on package %s\n"), db->treename, pkg->name); | |
592 | } | |
593 | } else if(strcmp(line, "%VERSION%") == 0) { | |
594 | READ_NEXT(); | |
595 | if(strcmp(line, pkg->version) != 0) { | |
596 | _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version " | |
597 | "mismatch on package %s\n"), db->treename, pkg->name); | |
598 | } | |
599 | } else if(strcmp(line, "%FILENAME%") == 0) { | |
600 | READ_AND_STORE(pkg->filename); | |
601 | if(_alpm_validate_filename(db, pkg->name, pkg->filename) < 0) { | |
602 | return -1; | |
603 | } | |
604 | } else if(strcmp(line, "%DESC%") == 0) { | |
605 | READ_AND_STORE(pkg->desc); | |
606 | } else if(strcmp(line, "%GROUPS%") == 0) { | |
607 | READ_AND_STORE_ALL(pkg->groups); | |
608 | } else if(strcmp(line, "%URL%") == 0) { | |
609 | READ_AND_STORE(pkg->url); | |
610 | } else if(strcmp(line, "%LICENSE%") == 0) { | |
611 | READ_AND_STORE_ALL(pkg->licenses); | |
612 | } else if(strcmp(line, "%ARCH%") == 0) { | |
613 | READ_AND_STORE(pkg->arch); | |
614 | } else if(strcmp(line, "%BUILDDATE%") == 0) { | |
615 | READ_NEXT(); | |
616 | pkg->builddate = _alpm_parsedate(line); | |
617 | } else if(strcmp(line, "%PACKAGER%") == 0) { | |
618 | READ_AND_STORE(pkg->packager); | |
619 | } else if(strcmp(line, "%CSIZE%") == 0) { | |
620 | READ_NEXT(); | |
621 | pkg->size = _alpm_strtoofft(line); | |
622 | } else if(strcmp(line, "%ISIZE%") == 0) { | |
623 | READ_NEXT(); | |
624 | pkg->isize = _alpm_strtoofft(line); | |
625 | } else if(strcmp(line, "%MD5SUM%") == 0) { | |
626 | READ_AND_STORE(pkg->md5sum); | |
627 | } else if(strcmp(line, "%SHA256SUM%") == 0) { | |
628 | READ_AND_STORE(pkg->sha256sum); | |
629 | } else if(strcmp(line, "%PGPSIG%") == 0) { | |
630 | READ_AND_STORE(pkg->base64_sig); | |
631 | } else if(strcmp(line, "%REPLACES%") == 0) { | |
632 | READ_AND_SPLITDEP(pkg->replaces); | |
633 | } else if(strcmp(line, "%DEPENDS%") == 0) { | |
634 | READ_AND_SPLITDEP(pkg->depends); | |
635 | } else if(strcmp(line, "%OPTDEPENDS%") == 0) { | |
636 | READ_AND_SPLITDEP(pkg->optdepends); | |
637 | } else if(strcmp(line, "%MAKEDEPENDS%") == 0) { | |
638 | /* currently unused */ | |
639 | while(1) { | |
640 | READ_NEXT(); | |
641 | if(strlen(line) == 0) break; | |
642 | } | |
643 | } else if(strcmp(line, "%CHECKDEPENDS%") == 0) { | |
644 | /* currently unused */ | |
645 | while(1) { | |
646 | READ_NEXT(); | |
647 | if(strlen(line) == 0) break; | |
648 | } | |
649 | } else if(strcmp(line, "%CONFLICTS%") == 0) { | |
650 | READ_AND_SPLITDEP(pkg->conflicts); | |
651 | } else if(strcmp(line, "%PROVIDES%") == 0) { | |
652 | READ_AND_SPLITDEP(pkg->provides); | |
653 | } else if(strcmp(line, "%DELTAS%") == 0) { | |
654 | /* Different than the rest because of the _alpm_delta_parse call. */ | |
655 | while(1) { | |
656 | READ_NEXT(); | |
657 | if(strlen(line) == 0) break; | |
658 | pkg->deltas = alpm_list_add(pkg->deltas, | |
659 | _alpm_delta_parse(db->handle, line)); | |
660 | } | |
661 | } | |
662 | } | |
663 | if(ret != ARCHIVE_EOF) { | |
664 | goto error; | |
665 | } | |
666 | *likely_pkg = pkg; | |
667 | } else if(strcmp(filename, "deltas") == 0) { | |
668 | /* skip reading delta files if UseDelta is unset */ | |
669 | } else if(strcmp(filename, "files") == 0) { | |
670 | /* currently do nothing with this file */ | |
671 | } else { | |
672 | /* unknown database file */ | |
673 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "unknown database file: %s\n", filename); | |
674 | } | |
675 | ||
676 | return 0; | |
677 | ||
678 | error: | |
679 | _alpm_log(db->handle, ALPM_LOG_DEBUG, "error parsing database file: %s\n", filename); | |
680 | return -1; | |
681 | } | |
682 | ||
683 | struct db_operations sync_db_ops = { | |
684 | .validate = sync_db_validate, | |
685 | .populate = sync_db_populate, | |
686 | .unregister = _alpm_db_unregister, | |
687 | }; | |
688 | ||
689 | alpm_db_t *_alpm_db_register_sync(alpm_handle_t *handle, const char *treename, | |
690 | alpm_siglevel_t level) | |
691 | { | |
692 | alpm_db_t *db; | |
693 | ||
694 | _alpm_log(handle, ALPM_LOG_DEBUG, "registering sync database '%s'\n", treename); | |
695 | ||
696 | #ifndef HAVE_LIBGPGME | |
697 | if((level &= ~ALPM_SIG_PACKAGE_SET) != 0 && level != ALPM_SIG_USE_DEFAULT) { | |
698 | RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL); | |
699 | } | |
700 | #endif | |
701 | ||
702 | db = _alpm_db_new(treename, 0); | |
703 | if(db == NULL) { | |
704 | RET_ERR(handle, ALPM_ERR_DB_CREATE, NULL); | |
705 | } | |
706 | db->ops = &sync_db_ops; | |
707 | db->handle = handle; | |
708 | db->siglevel = level; | |
709 | ||
710 | sync_db_validate(db); | |
711 | ||
712 | handle->dbs_sync = alpm_list_add(handle->dbs_sync, db); | |
713 | return db; | |
714 | } | |
715 | ||
716 | /* vim: set ts=2 sw=2 noet: */ |