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