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