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