Move db cache handling functions
[pacman-ng.git] / lib / libalpm / be_files.c
blob864e58ea8e4167ab9cf0d9083e7ca0e7e8924a53
1 /*
2 * be_files.c
4 * Copyright (c) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdint.h> /* intmax_t */
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <ctype.h>
32 #include <time.h>
33 #include <limits.h> /* PATH_MAX */
34 #include <locale.h> /* setlocale */
36 /* libarchive */
37 #include <archive.h>
38 #include <archive_entry.h>
40 /* libalpm */
41 #include "db.h"
42 #include "alpm_list.h"
43 #include "log.h"
44 #include "util.h"
45 #include "alpm.h"
46 #include "handle.h"
47 #include "package.h"
48 #include "group.h"
49 #include "delta.h"
50 #include "deps.h"
51 #include "dload.h"
54 #define LAZY_LOAD(info, errret) \
55 do { \
56 ALPM_LOG_FUNC; \
57 ASSERT(handle != NULL, return(errret)); \
58 ASSERT(pkg != NULL, return(errret)); \
59 if(pkg->origin != PKG_FROM_FILE && !(pkg->infolevel & info)) { \
60 _alpm_db_read(pkg->origin_data.db, pkg, info); \
61 } \
62 } while(0)
65 /* Cache-specific accessor functions. These implementations allow for lazy
66 * loading by the files backend when a data member is actually needed
67 * rather than loading all pieces of information when the package is first
68 * initialized.
71 const char *_cache_get_filename(pmpkg_t *pkg)
73 LAZY_LOAD(INFRQ_DESC, NULL);
74 return pkg->filename;
77 const char *_cache_get_name(pmpkg_t *pkg)
79 ASSERT(pkg != NULL, return(NULL));
80 return pkg->name;
83 static const char *_cache_get_version(pmpkg_t *pkg)
85 ASSERT(pkg != NULL, return(NULL));
86 return pkg->version;
89 static const char *_cache_get_desc(pmpkg_t *pkg)
91 LAZY_LOAD(INFRQ_DESC, NULL);
92 return pkg->desc;
95 const char *_cache_get_url(pmpkg_t *pkg)
97 LAZY_LOAD(INFRQ_DESC, NULL);
98 return pkg->url;
101 time_t _cache_get_builddate(pmpkg_t *pkg)
103 LAZY_LOAD(INFRQ_DESC, 0);
104 return pkg->builddate;
107 time_t _cache_get_installdate(pmpkg_t *pkg)
109 LAZY_LOAD(INFRQ_DESC, 0);
110 return pkg->installdate;
113 const char *_cache_get_packager(pmpkg_t *pkg)
115 LAZY_LOAD(INFRQ_DESC, NULL);
116 return pkg->packager;
119 const char *_cache_get_md5sum(pmpkg_t *pkg)
121 LAZY_LOAD(INFRQ_DESC, NULL);
122 return pkg->md5sum;
125 const char *_cache_get_arch(pmpkg_t *pkg)
127 LAZY_LOAD(INFRQ_DESC, NULL);
128 return pkg->arch;
131 off_t _cache_get_size(pmpkg_t *pkg)
133 LAZY_LOAD(INFRQ_DESC, -1);
134 return pkg->size;
137 off_t _cache_get_isize(pmpkg_t *pkg)
139 LAZY_LOAD(INFRQ_DESC, -1);
140 return pkg->isize;
143 pmpkgreason_t _cache_get_reason(pmpkg_t *pkg)
145 LAZY_LOAD(INFRQ_DESC, -1);
146 return pkg->reason;
149 alpm_list_t *_cache_get_licenses(pmpkg_t *pkg)
151 LAZY_LOAD(INFRQ_DESC, NULL);
152 return pkg->licenses;
155 alpm_list_t *_cache_get_groups(pmpkg_t *pkg)
157 LAZY_LOAD(INFRQ_DESC, NULL);
158 return pkg->groups;
161 int _cache_has_force(pmpkg_t *pkg)
163 LAZY_LOAD(INFRQ_DESC, -1);
164 return pkg->force;
167 alpm_list_t *_cache_get_depends(pmpkg_t *pkg)
169 LAZY_LOAD(INFRQ_DEPENDS, NULL);
170 return pkg->depends;
173 alpm_list_t *_cache_get_optdepends(pmpkg_t *pkg)
175 LAZY_LOAD(INFRQ_DEPENDS, NULL);
176 return pkg->optdepends;
179 alpm_list_t *_cache_get_conflicts(pmpkg_t *pkg)
181 LAZY_LOAD(INFRQ_DEPENDS, NULL);
182 return pkg->conflicts;
185 alpm_list_t *_cache_get_provides(pmpkg_t *pkg)
187 LAZY_LOAD(INFRQ_DEPENDS, NULL);
188 return pkg->provides;
191 alpm_list_t *_cache_get_replaces(pmpkg_t *pkg)
193 LAZY_LOAD(INFRQ_DESC, NULL);
194 return pkg->replaces;
197 alpm_list_t *_cache_get_deltas(pmpkg_t *pkg)
199 LAZY_LOAD(INFRQ_DELTAS, NULL);
200 return pkg->deltas;
203 alpm_list_t *_cache_get_files(pmpkg_t *pkg)
205 ALPM_LOG_FUNC;
207 /* Sanity checks */
208 ASSERT(handle != NULL, return(NULL));
209 ASSERT(pkg != NULL, return(NULL));
211 if(pkg->origin == PKG_FROM_LOCALDB
212 && !(pkg->infolevel & INFRQ_FILES)) {
213 _alpm_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
215 return pkg->files;
218 alpm_list_t *_cache_get_backup(pmpkg_t *pkg)
220 ALPM_LOG_FUNC;
222 /* Sanity checks */
223 ASSERT(handle != NULL, return(NULL));
224 ASSERT(pkg != NULL, return(NULL));
226 if(pkg->origin == PKG_FROM_LOCALDB
227 && !(pkg->infolevel & INFRQ_FILES)) {
228 _alpm_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
230 return pkg->backup;
234 * Open a package changelog for reading. Similar to fopen in functionality,
235 * except that the returned 'file stream' is from the database.
236 * @param pkg the package (from db) to read the changelog
237 * @return a 'file stream' to the package changelog
239 void *_cache_changelog_open(pmpkg_t *pkg)
241 ALPM_LOG_FUNC;
243 /* Sanity checks */
244 ASSERT(handle != NULL, return(NULL));
245 ASSERT(pkg != NULL, return(NULL));
247 char clfile[PATH_MAX];
248 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
249 alpm_option_get_dbpath(),
250 alpm_db_get_name(alpm_pkg_get_db(pkg)),
251 alpm_pkg_get_name(pkg),
252 alpm_pkg_get_version(pkg));
253 return fopen(clfile, "r");
257 * Read data from an open changelog 'file stream'. Similar to fread in
258 * functionality, this function takes a buffer and amount of data to read.
259 * @param ptr a buffer to fill with raw changelog data
260 * @param size the size of the buffer
261 * @param pkg the package that the changelog is being read from
262 * @param fp a 'file stream' to the package changelog
263 * @return the number of characters read, or 0 if there is no more data
265 size_t _cache_changelog_read(void *ptr, size_t size,
266 const pmpkg_t *pkg, const void *fp)
268 return ( fread(ptr, 1, size, (FILE*)fp) );
272 int _cache_changelog_feof(const pmpkg_t *pkg, void *fp)
274 return( feof((FILE*)fp) );
279 * Close a package changelog for reading. Similar to fclose in functionality,
280 * except that the 'file stream' is from the database.
281 * @param pkg the package that the changelog was read from
282 * @param fp a 'file stream' to the package changelog
283 * @return whether closing the package changelog stream was successful
285 int _cache_changelog_close(const pmpkg_t *pkg, void *fp)
287 return( fclose((FILE*)fp) );
290 /** The sync database operations struct. Get package fields through
291 * lazy accessor methods that handle any backend loading and caching
292 * logic.
294 static struct pkg_operations sync_pkg_ops = {
295 .get_filename = _cache_get_filename,
296 .get_name = _cache_get_name,
297 .get_version = _cache_get_version,
298 .get_desc = _cache_get_desc,
299 .get_url = _cache_get_url,
300 .get_builddate = _cache_get_builddate,
301 .get_installdate = _cache_get_installdate,
302 .get_packager = _cache_get_packager,
303 .get_md5sum = _cache_get_md5sum,
304 .get_arch = _cache_get_arch,
305 .get_size = _cache_get_size,
306 .get_isize = _cache_get_isize,
307 .get_reason = _cache_get_reason,
308 .has_force = _cache_has_force,
309 .get_licenses = _cache_get_licenses,
310 .get_groups = _cache_get_groups,
311 .get_depends = _cache_get_depends,
312 .get_optdepends = _cache_get_optdepends,
313 .get_conflicts = _cache_get_conflicts,
314 .get_provides = _cache_get_provides,
315 .get_replaces = _cache_get_replaces,
316 .get_deltas = _cache_get_deltas,
317 .get_files = _cache_get_files,
318 .get_backup = _cache_get_backup,
321 /** The local database operations struct. Get package fields through
322 * lazy accessor methods that handle any backend loading and caching
323 * logic.
325 static struct pkg_operations local_pkg_ops = {
326 .get_filename = _cache_get_filename,
327 .get_name = _cache_get_name,
328 .get_version = _cache_get_version,
329 .get_desc = _cache_get_desc,
330 .get_url = _cache_get_url,
331 .get_builddate = _cache_get_builddate,
332 .get_installdate = _cache_get_installdate,
333 .get_packager = _cache_get_packager,
334 .get_md5sum = _cache_get_md5sum,
335 .get_arch = _cache_get_arch,
336 .get_size = _cache_get_size,
337 .get_isize = _cache_get_isize,
338 .get_reason = _cache_get_reason,
339 .has_force = _cache_has_force,
340 .get_licenses = _cache_get_licenses,
341 .get_groups = _cache_get_groups,
342 .get_depends = _cache_get_depends,
343 .get_optdepends = _cache_get_optdepends,
344 .get_conflicts = _cache_get_conflicts,
345 .get_provides = _cache_get_provides,
346 .get_replaces = _cache_get_replaces,
347 .get_deltas = _cache_get_deltas,
348 .get_files = _cache_get_files,
349 .get_backup = _cache_get_backup,
351 .changelog_open = _cache_changelog_open,
352 .changelog_read = _cache_changelog_read,
353 .changelog_close = _cache_changelog_close,
356 /* create list of directories in db */
357 static int dirlist_from_tar(const char *archive, alpm_list_t **dirlist)
359 struct archive *_archive;
360 struct archive_entry *entry;
362 if((_archive = archive_read_new()) == NULL)
363 RET_ERR(PM_ERR_LIBARCHIVE, -1);
365 archive_read_support_compression_all(_archive);
366 archive_read_support_format_all(_archive);
368 if(archive_read_open_filename(_archive, archive,
369 ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
370 _alpm_log(PM_LOG_ERROR, _("could not open %s: %s\n"), archive,
371 archive_error_string(_archive));
372 RET_ERR(PM_ERR_PKG_OPEN, -1);
375 while(archive_read_next_header(_archive, &entry) == ARCHIVE_OK) {
376 const struct stat *st;
377 const char *entryname; /* the name of the file in the archive */
379 st = archive_entry_stat(entry);
380 entryname = archive_entry_pathname(entry);
382 if(S_ISDIR(st->st_mode)) {
383 char *name = strdup(entryname);
384 *dirlist = alpm_list_add(*dirlist, name);
387 archive_read_finish(_archive);
389 *dirlist = alpm_list_msort(*dirlist, alpm_list_count(*dirlist), _alpm_str_cmp);
390 return(0);
393 static int is_dir(const char *path, struct dirent *entry)
395 #ifdef DT_DIR
396 return(entry->d_type == DT_DIR);
397 #else
398 char buffer[PATH_MAX];
399 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
401 struct stat sbuf;
402 if (!stat(buffer, &sbuf)) {
403 return(S_ISDIR(sbuf.st_mode));
406 return(0);
407 #endif
410 /* create list of directories in db */
411 static int dirlist_from_fs(const char *syncdbpath, alpm_list_t **dirlist)
413 DIR *dbdir;
414 struct dirent *ent = NULL;
416 dbdir = opendir(syncdbpath);
417 if (dbdir != NULL) {
418 while((ent = readdir(dbdir)) != NULL) {
419 char *name = ent->d_name;
420 size_t len;
421 char *entry;
423 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
424 continue;
427 if(!is_dir(syncdbpath, ent)) {
428 continue;
431 len = strlen(name);
432 MALLOC(entry, len + 2, RET_ERR(PM_ERR_MEMORY, -1));
433 strcpy(entry, name);
434 entry[len] = '/';
435 entry[len+1] = '\0';
436 *dirlist = alpm_list_add(*dirlist, entry);
438 closedir(dbdir);
441 *dirlist = alpm_list_msort(*dirlist, alpm_list_count(*dirlist), _alpm_str_cmp);
442 return(0);
445 /* remove old directories from dbdir */
446 static int remove_olddir(const char *syncdbpath, alpm_list_t *dirlist)
448 alpm_list_t *i;
449 for (i = dirlist; i; i = i->next) {
450 const char *name = i->data;
451 char *dbdir;
452 size_t len = strlen(syncdbpath) + strlen(name) + 2;
453 MALLOC(dbdir, len, RET_ERR(PM_ERR_MEMORY, -1));
454 snprintf(dbdir, len, "%s%s", syncdbpath, name);
455 _alpm_log(PM_LOG_DEBUG, "removing: %s\n", dbdir);
456 if(_alpm_rmrf(dbdir) != 0) {
457 _alpm_log(PM_LOG_ERROR, _("could not remove database directory %s\n"), dbdir);
458 free(dbdir);
459 RET_ERR(PM_ERR_DB_REMOVE, -1);
461 free(dbdir);
463 return(0);
466 /** Update a package database
468 * An update of the package database \a db will be attempted. Unless
469 * \a force is true, the update will only be performed if the remote
470 * database was modified since the last update.
472 * A transaction is necessary for this operation, in order to obtain a
473 * database lock. During this transaction the front-end will be informed
474 * of the download progress of the database via the download callback.
476 * Example:
477 * @code
478 * pmdb_t *db;
479 * int result;
480 * db = alpm_list_getdata(alpm_option_get_syncdbs());
481 * if(alpm_trans_init(0, NULL, NULL, NULL) == 0) {
482 * result = alpm_db_update(0, db);
483 * alpm_trans_release();
485 * if(result > 0) {
486 * printf("Unable to update database: %s\n", alpm_strerrorlast());
487 * } else if(result < 0) {
488 * printf("Database already up to date\n");
489 * } else {
490 * printf("Database updated\n");
493 * @endcode
495 * @ingroup alpm_databases
496 * @note After a successful update, the \link alpm_db_get_pkgcache()
497 * package cache \endlink will be invalidated
498 * @param force if true, then forces the update, otherwise update only in case
499 * the database isn't up to date
500 * @param db pointer to the package database to update
501 * @return 0 on success, > 0 on error (pm_errno is set accordingly), < 0 if up
502 * to date
504 int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
506 char *dbfile, *dbfilepath, *syncpath;
507 const char *dbpath, *syncdbpath;
508 alpm_list_t *newdirlist = NULL, *olddirlist = NULL;
509 alpm_list_t *onlynew = NULL, *onlyold = NULL;
510 size_t len;
511 int ret;
513 ALPM_LOG_FUNC;
515 /* Sanity checks */
516 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
517 ASSERT(db != NULL && db != handle->db_local, RET_ERR(PM_ERR_WRONG_ARGS, -1));
518 /* Verify we are in a transaction. This is done _mainly_ because we need a DB
519 * lock - if we update without a db lock, we may kludge some other pacman
520 * process that _has_ a lock.
522 ASSERT(handle->trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
523 ASSERT(handle->trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
525 if(!alpm_list_find_ptr(handle->dbs_sync, db)) {
526 RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
529 len = strlen(db->treename) + 4;
530 MALLOC(dbfile, len, RET_ERR(PM_ERR_MEMORY, -1));
531 sprintf(dbfile, "%s.db", db->treename);
533 dbpath = alpm_option_get_dbpath();
534 len = strlen(dbpath) + 6;
535 MALLOC(syncpath, len, RET_ERR(PM_ERR_MEMORY, -1));
536 sprintf(syncpath, "%s%s", dbpath, "sync/");
538 ret = _alpm_download_single_file(dbfile, db->servers, syncpath, force);
539 free(dbfile);
540 free(syncpath);
542 if(ret == 1) {
543 /* files match, do nothing */
544 pm_errno = 0;
545 return(1);
546 } else if(ret == -1) {
547 /* pm_errno was set by the download code */
548 _alpm_log(PM_LOG_DEBUG, "failed to sync db: %s\n", alpm_strerrorlast());
549 return(-1);
552 syncdbpath = _alpm_db_path(db);
554 /* form the path to the db location */
555 len = strlen(dbpath) + strlen(db->treename) + 9;
556 MALLOC(dbfilepath, len, RET_ERR(PM_ERR_MEMORY, -1));
557 sprintf(dbfilepath, "%ssync/%s.db", dbpath, db->treename);
559 if(force) {
560 /* if forcing update, remove the old dir and extract the db */
561 if(_alpm_rmrf(syncdbpath) != 0) {
562 _alpm_log(PM_LOG_ERROR, _("could not remove database %s\n"), db->treename);
563 RET_ERR(PM_ERR_DB_REMOVE, -1);
564 } else {
565 _alpm_log(PM_LOG_DEBUG, "database dir %s removed\n", _alpm_db_path(db));
567 } else {
568 /* if not forcing, only remove and extract what is necessary */
569 ret = dirlist_from_tar(dbfilepath, &newdirlist);
570 if(ret) {
571 goto cleanup;
573 ret = dirlist_from_fs(syncdbpath, &olddirlist);
574 if(ret) {
575 goto cleanup;
578 alpm_list_diff_sorted(olddirlist, newdirlist, _alpm_str_cmp, &onlyold, &onlynew);
580 ret = remove_olddir(syncdbpath, onlyold);
581 if(ret) {
582 goto cleanup;
586 /* Cache needs to be rebuilt */
587 _alpm_db_free_pkgcache(db);
589 checkdbdir(db);
590 ret = _alpm_unpack(dbfilepath, syncdbpath, onlynew, 0);
592 cleanup:
593 FREELIST(newdirlist);
594 FREELIST(olddirlist);
595 alpm_list_free(onlynew);
596 alpm_list_free(onlyold);
598 free(dbfilepath);
600 if(ret) {
601 RET_ERR(PM_ERR_SYSTEM, -1);
604 return(0);
607 int _alpm_db_populate(pmdb_t *db)
609 int count = 0;
610 struct dirent *ent = NULL;
611 const char *dbpath;
612 DIR *dbdir;
614 ALPM_LOG_FUNC;
616 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
618 dbpath = _alpm_db_path(db);
619 dbdir = opendir(dbpath);
620 if(dbdir == NULL) {
621 return(0);
623 while((ent = readdir(dbdir)) != NULL) {
624 const char *name = ent->d_name;
625 pmpkg_t *pkg;
627 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
628 continue;
630 if(!is_dir(dbpath, ent)) {
631 continue;
634 pkg = _alpm_pkg_new();
635 if(pkg == NULL) {
636 closedir(dbdir);
637 return(-1);
639 /* split the db entry name */
640 if(splitname(name, pkg) != 0) {
641 _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
642 name);
643 _alpm_pkg_free(pkg);
644 continue;
647 /* duplicated database entries are not allowed */
648 if(_alpm_pkg_find(db->pkgcache, pkg->name)) {
649 _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
650 _alpm_pkg_free(pkg);
651 continue;
654 /* explicitly read with only 'BASE' data, accessors will handle the rest */
655 if(_alpm_db_read(db, pkg, INFRQ_BASE) == -1) {
656 _alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
657 _alpm_pkg_free(pkg);
658 continue;
660 if(db == handle->db_local) {
661 pkg->origin = PKG_FROM_LOCALDB;
662 pkg->ops = &local_pkg_ops;
663 } else {
664 pkg->origin = PKG_FROM_SYNCDB;
665 pkg->ops = &sync_pkg_ops;
667 pkg->origin_data.db = db;
668 /* add to the collection */
669 _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
670 pkg->name, db->treename);
671 db->pkgcache = alpm_list_add(db->pkgcache, pkg);
672 count++;
675 closedir(dbdir);
676 db->pkgcache = alpm_list_msort(db->pkgcache, count, _alpm_pkg_cmp);
677 return(count);
680 int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
682 FILE *fp = NULL;
683 char path[PATH_MAX];
684 char line[1024];
685 char *pkgpath = NULL;
687 ALPM_LOG_FUNC;
689 if(db == NULL) {
690 RET_ERR(PM_ERR_DB_NULL, -1);
693 if(info == NULL || info->name == NULL || info->version == NULL) {
694 _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_db_read, skipping\n");
695 return(-1);
698 if(info->origin == PKG_FROM_FILE) {
699 _alpm_log(PM_LOG_DEBUG, "request to read database info for a file-based package '%s', skipping...\n", info->name);
700 return(-1);
703 /* bitmask logic here:
704 * infolevel: 00001111
705 * inforeq: 00010100
706 * & result: 00000100
707 * == to inforeq? nope, we need to load more info. */
708 if((info->infolevel & inforeq) == inforeq) {
709 /* already loaded this info, do nothing */
710 return(0);
712 _alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
713 info->name, inforeq);
715 /* clear out 'line', to be certain - and to make valgrind happy */
716 memset(line, 0, sizeof(line));
718 pkgpath = get_pkgpath(db, info);
720 if(access(pkgpath, F_OK)) {
721 /* directory doesn't exist or can't be opened */
722 _alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
723 info->name, info->version, db->treename);
724 goto error;
727 /* DESC */
728 if(inforeq & INFRQ_DESC) {
729 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
730 if((fp = fopen(path, "r")) == NULL) {
731 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
732 goto error;
734 while(!feof(fp)) {
735 if(fgets(line, sizeof(line), fp) == NULL) {
736 break;
738 _alpm_strtrim(line);
739 if(strcmp(line, "%NAME%") == 0) {
740 if(fgets(line, sizeof(line), fp) == NULL) {
741 goto error;
743 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
744 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
745 "mismatch on package %s\n"), db->treename, info->name);
747 } else if(strcmp(line, "%VERSION%") == 0) {
748 if(fgets(line, sizeof(line), fp) == NULL) {
749 goto error;
751 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
752 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
753 "mismatch on package %s\n"), db->treename, info->name);
755 } else if(strcmp(line, "%FILENAME%") == 0) {
756 if(fgets(line, sizeof(line), fp) == NULL) {
757 goto error;
759 STRDUP(info->filename, _alpm_strtrim(line), goto error);
760 } else if(strcmp(line, "%DESC%") == 0) {
761 if(fgets(line, sizeof(line), fp) == NULL) {
762 goto error;
764 STRDUP(info->desc, _alpm_strtrim(line), goto error);
765 } else if(strcmp(line, "%GROUPS%") == 0) {
766 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
767 char *linedup;
768 STRDUP(linedup, _alpm_strtrim(line), goto error);
769 info->groups = alpm_list_add(info->groups, linedup);
771 } else if(strcmp(line, "%URL%") == 0) {
772 if(fgets(line, sizeof(line), fp) == NULL) {
773 goto error;
775 STRDUP(info->url, _alpm_strtrim(line), goto error);
776 } else if(strcmp(line, "%LICENSE%") == 0) {
777 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
778 char *linedup;
779 STRDUP(linedup, _alpm_strtrim(line), goto error);
780 info->licenses = alpm_list_add(info->licenses, linedup);
782 } else if(strcmp(line, "%ARCH%") == 0) {
783 if(fgets(line, sizeof(line), fp) == NULL) {
784 goto error;
786 STRDUP(info->arch, _alpm_strtrim(line), goto error);
787 } else if(strcmp(line, "%BUILDDATE%") == 0) {
788 if(fgets(line, sizeof(line), fp) == NULL) {
789 goto error;
791 _alpm_strtrim(line);
793 char first = tolower((unsigned char)line[0]);
794 if(first > 'a' && first < 'z') {
795 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
796 setlocale(LC_TIME, "C");
797 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
798 info->builddate = mktime(&tmp_tm);
799 setlocale(LC_TIME, "");
800 } else {
801 info->builddate = atol(line);
803 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
804 if(fgets(line, sizeof(line), fp) == NULL) {
805 goto error;
807 _alpm_strtrim(line);
809 char first = tolower((unsigned char)line[0]);
810 if(first > 'a' && first < 'z') {
811 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
812 setlocale(LC_TIME, "C");
813 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
814 info->installdate = mktime(&tmp_tm);
815 setlocale(LC_TIME, "");
816 } else {
817 info->installdate = atol(line);
819 } else if(strcmp(line, "%PACKAGER%") == 0) {
820 if(fgets(line, sizeof(line), fp) == NULL) {
821 goto error;
823 STRDUP(info->packager, _alpm_strtrim(line), goto error);
824 } else if(strcmp(line, "%REASON%") == 0) {
825 if(fgets(line, sizeof(line), fp) == NULL) {
826 goto error;
828 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
829 } else if(strcmp(line, "%SIZE%") == 0 || strcmp(line, "%CSIZE%") == 0) {
830 /* NOTE: the CSIZE and SIZE fields both share the "size" field
831 * in the pkginfo_t struct. This can be done b/c CSIZE
832 * is currently only used in sync databases, and SIZE is
833 * only used in local databases.
835 if(fgets(line, sizeof(line), fp) == NULL) {
836 goto error;
838 info->size = atol(_alpm_strtrim(line));
839 /* also store this value to isize if isize is unset */
840 if(info->isize == 0) {
841 info->isize = info->size;
843 } else if(strcmp(line, "%ISIZE%") == 0) {
844 /* ISIZE (installed size) tag only appears in sync repositories,
845 * not the local one. */
846 if(fgets(line, sizeof(line), fp) == NULL) {
847 goto error;
849 info->isize = atol(_alpm_strtrim(line));
850 } else if(strcmp(line, "%MD5SUM%") == 0) {
851 /* MD5SUM tag only appears in sync repositories,
852 * not the local one. */
853 if(fgets(line, sizeof(line), fp) == NULL) {
854 goto error;
856 STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
857 } else if(strcmp(line, "%REPLACES%") == 0) {
858 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
859 char *linedup;
860 STRDUP(linedup, _alpm_strtrim(line), goto error);
861 info->replaces = alpm_list_add(info->replaces, linedup);
863 } else if(strcmp(line, "%FORCE%") == 0) {
864 info->force = 1;
867 fclose(fp);
868 fp = NULL;
871 /* FILES */
872 if(inforeq & INFRQ_FILES) {
873 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
874 if((fp = fopen(path, "r")) == NULL) {
875 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
876 goto error;
878 while(fgets(line, sizeof(line), fp)) {
879 _alpm_strtrim(line);
880 if(strcmp(line, "%FILES%") == 0) {
881 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
882 char *linedup;
883 STRDUP(linedup, _alpm_strtrim(line), goto error);
884 info->files = alpm_list_add(info->files, linedup);
886 } else if(strcmp(line, "%BACKUP%") == 0) {
887 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
888 char *linedup;
889 STRDUP(linedup, _alpm_strtrim(line), goto error);
890 info->backup = alpm_list_add(info->backup, linedup);
894 fclose(fp);
895 fp = NULL;
898 /* DEPENDS */
899 if(inforeq & INFRQ_DEPENDS) {
900 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
901 if((fp = fopen(path, "r")) == NULL) {
902 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
903 goto error;
905 while(!feof(fp)) {
906 if(fgets(line, sizeof(line), fp) == NULL) {
907 break;
909 _alpm_strtrim(line);
910 if(strcmp(line, "%DEPENDS%") == 0) {
911 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
912 pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
913 info->depends = alpm_list_add(info->depends, dep);
915 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
916 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
917 char *linedup;
918 STRDUP(linedup, _alpm_strtrim(line), goto error);
919 info->optdepends = alpm_list_add(info->optdepends, linedup);
921 } else if(strcmp(line, "%CONFLICTS%") == 0) {
922 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
923 char *linedup;
924 STRDUP(linedup, _alpm_strtrim(line), goto error);
925 info->conflicts = alpm_list_add(info->conflicts, linedup);
927 } else if(strcmp(line, "%PROVIDES%") == 0) {
928 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
929 char *linedup;
930 STRDUP(linedup, _alpm_strtrim(line), goto error);
931 info->provides = alpm_list_add(info->provides, linedup);
935 fclose(fp);
936 fp = NULL;
939 /* DELTAS */
940 if(inforeq & INFRQ_DELTAS) {
941 snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
942 if((fp = fopen(path, "r"))) {
943 while(!feof(fp)) {
944 if(fgets(line, sizeof(line), fp) == NULL) {
945 break;
947 _alpm_strtrim(line);
948 if(strcmp(line, "%DELTAS%") == 0) {
949 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
950 pmdelta_t *delta = _alpm_delta_parse(line);
951 if(delta) {
952 info->deltas = alpm_list_add(info->deltas, delta);
957 fclose(fp);
958 fp = NULL;
962 /* INSTALL */
963 if(inforeq & INFRQ_SCRIPTLET) {
964 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
965 if(access(path, F_OK) == 0) {
966 info->scriptlet = 1;
970 /* internal */
971 info->infolevel |= inforeq;
973 free(pkgpath);
974 return(0);
976 error:
977 free(pkgpath);
978 if(fp) {
979 fclose(fp);
981 return(-1);
984 int _alpm_db_prepare(pmdb_t *db, pmpkg_t *info)
986 mode_t oldmask;
987 int retval = 0;
988 char *pkgpath = NULL;
990 if(checkdbdir(db) != 0) {
991 return(-1);
994 oldmask = umask(0000);
995 pkgpath = get_pkgpath(db, info);
997 if((retval = mkdir(pkgpath, 0755)) != 0) {
998 _alpm_log(PM_LOG_ERROR, _("could not create directory %s: %s\n"),
999 pkgpath, strerror(errno));
1002 free(pkgpath);
1003 umask(oldmask);
1005 return(retval);
1008 int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
1010 FILE *fp = NULL;
1011 char path[PATH_MAX];
1012 mode_t oldmask;
1013 alpm_list_t *lp = NULL;
1014 int retval = 0;
1015 int local = 0;
1016 char *pkgpath = NULL;
1018 ALPM_LOG_FUNC;
1020 if(db == NULL || info == NULL) {
1021 return(-1);
1024 pkgpath = get_pkgpath(db, info);
1026 /* make sure we have a sane umask */
1027 oldmask = umask(0022);
1029 if(strcmp(db->treename, "local") == 0) {
1030 local = 1;
1033 /* DESC */
1034 if(inforeq & INFRQ_DESC) {
1035 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
1036 info->name, info->version);
1037 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
1038 if((fp = fopen(path, "w")) == NULL) {
1039 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
1040 retval = -1;
1041 goto cleanup;
1043 fprintf(fp, "%%NAME%%\n%s\n\n"
1044 "%%VERSION%%\n%s\n\n", info->name, info->version);
1045 if(info->desc) {
1046 fprintf(fp, "%%DESC%%\n"
1047 "%s\n\n", info->desc);
1049 if(info->groups) {
1050 fputs("%GROUPS%\n", fp);
1051 for(lp = info->groups; lp; lp = lp->next) {
1052 fprintf(fp, "%s\n", (char *)lp->data);
1054 fprintf(fp, "\n");
1056 if(info->replaces) {
1057 fputs("%REPLACES%\n", fp);
1058 for(lp = info->replaces; lp; lp = lp->next) {
1059 fprintf(fp, "%s\n", (char *)lp->data);
1061 fprintf(fp, "\n");
1063 if(info->force) {
1064 fprintf(fp, "%%FORCE%%\n\n");
1066 if(local) {
1067 if(info->url) {
1068 fprintf(fp, "%%URL%%\n"
1069 "%s\n\n", info->url);
1071 if(info->licenses) {
1072 fputs("%LICENSE%\n", fp);
1073 for(lp = info->licenses; lp; lp = lp->next) {
1074 fprintf(fp, "%s\n", (char *)lp->data);
1076 fprintf(fp, "\n");
1078 if(info->arch) {
1079 fprintf(fp, "%%ARCH%%\n"
1080 "%s\n\n", info->arch);
1082 if(info->builddate) {
1083 fprintf(fp, "%%BUILDDATE%%\n"
1084 "%ld\n\n", info->builddate);
1086 if(info->installdate) {
1087 fprintf(fp, "%%INSTALLDATE%%\n"
1088 "%ld\n\n", info->installdate);
1090 if(info->packager) {
1091 fprintf(fp, "%%PACKAGER%%\n"
1092 "%s\n\n", info->packager);
1094 if(info->isize) {
1095 /* only write installed size, csize is irrelevant once installed */
1096 fprintf(fp, "%%SIZE%%\n"
1097 "%jd\n\n", (intmax_t)info->isize);
1099 if(info->reason) {
1100 fprintf(fp, "%%REASON%%\n"
1101 "%u\n\n", info->reason);
1103 } else {
1104 if(info->size) {
1105 fprintf(fp, "%%CSIZE%%\n"
1106 "%jd\n\n", (intmax_t)info->size);
1108 if(info->isize) {
1109 fprintf(fp, "%%ISIZE%%\n"
1110 "%jd\n\n", (intmax_t)info->isize);
1112 if(info->md5sum) {
1113 fprintf(fp, "%%MD5SUM%%\n"
1114 "%s\n\n", info->md5sum);
1117 fclose(fp);
1118 fp = NULL;
1121 /* FILES */
1122 if(local && (inforeq & INFRQ_FILES)) {
1123 _alpm_log(PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
1124 info->name, info->version);
1125 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
1126 if((fp = fopen(path, "w")) == NULL) {
1127 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
1128 retval = -1;
1129 goto cleanup;
1131 if(info->files) {
1132 fprintf(fp, "%%FILES%%\n");
1133 for(lp = info->files; lp; lp = lp->next) {
1134 fprintf(fp, "%s\n", (char *)lp->data);
1136 fprintf(fp, "\n");
1138 if(info->backup) {
1139 fprintf(fp, "%%BACKUP%%\n");
1140 for(lp = info->backup; lp; lp = lp->next) {
1141 fprintf(fp, "%s\n", (char *)lp->data);
1143 fprintf(fp, "\n");
1145 fclose(fp);
1146 fp = NULL;
1149 /* DEPENDS */
1150 if(inforeq & INFRQ_DEPENDS) {
1151 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DEPENDS information back to db\n",
1152 info->name, info->version);
1153 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
1154 if((fp = fopen(path, "w")) == NULL) {
1155 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
1156 retval = -1;
1157 goto cleanup;
1159 if(info->depends) {
1160 fputs("%DEPENDS%\n", fp);
1161 for(lp = info->depends; lp; lp = lp->next) {
1162 char *depstring = alpm_dep_compute_string(lp->data);
1163 fprintf(fp, "%s\n", depstring);
1164 free(depstring);
1166 fprintf(fp, "\n");
1168 if(info->optdepends) {
1169 fputs("%OPTDEPENDS%\n", fp);
1170 for(lp = info->optdepends; lp; lp = lp->next) {
1171 fprintf(fp, "%s\n", (char *)lp->data);
1173 fprintf(fp, "\n");
1175 if(info->conflicts) {
1176 fputs("%CONFLICTS%\n", fp);
1177 for(lp = info->conflicts; lp; lp = lp->next) {
1178 fprintf(fp, "%s\n", (char *)lp->data);
1180 fprintf(fp, "\n");
1182 if(info->provides) {
1183 fputs("%PROVIDES%\n", fp);
1184 for(lp = info->provides; lp; lp = lp->next) {
1185 fprintf(fp, "%s\n", (char *)lp->data);
1187 fprintf(fp, "\n");
1189 fclose(fp);
1190 fp = NULL;
1193 /* INSTALL */
1194 /* nothing needed here (script is automatically extracted) */
1196 cleanup:
1197 umask(oldmask);
1198 free(pkgpath);
1200 if(fp) {
1201 fclose(fp);
1204 return(retval);
1207 int _alpm_db_remove(pmdb_t *db, pmpkg_t *info)
1209 int ret = 0;
1210 char *pkgpath = NULL;
1212 ALPM_LOG_FUNC;
1214 if(db == NULL || info == NULL) {
1215 RET_ERR(PM_ERR_DB_NULL, -1);
1218 pkgpath = get_pkgpath(db, info);
1220 ret = _alpm_rmrf(pkgpath);
1221 free(pkgpath);
1222 if(ret != 0) {
1223 ret = -1;
1225 return(ret);
1228 /* vim: set ts=2 sw=2 noet: */