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