Remove ALPM_LOG_FUNC macro
[pacman-ng.git] / lib / libalpm / be_local.c
blobfc138e393ceb71997a9c3d74ff040a642552eea3
1 /*
2 * be_local.c
4 * Copyright (c) 2006-2011 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 <time.h>
32 #include <limits.h> /* PATH_MAX */
34 /* libalpm */
35 #include "db.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "util.h"
39 #include "alpm.h"
40 #include "handle.h"
41 #include "package.h"
42 #include "deps.h"
45 #define LAZY_LOAD(info, errret) \
46 do { \
47 ASSERT(handle != NULL, return (errret)); \
48 if(pkg->origin != PKG_FROM_FILE && !(pkg->infolevel & info)) { \
49 _alpm_local_db_read(pkg->origin_data.db, pkg, info); \
50 } \
51 } while(0)
54 /* Cache-specific accessor functions. These implementations allow for lazy
55 * loading by the files backend when a data member is actually needed
56 * rather than loading all pieces of information when the package is first
57 * initialized.
60 static const char *_cache_get_filename(pmpkg_t *pkg)
62 LAZY_LOAD(INFRQ_DESC, NULL);
63 return pkg->filename;
66 static const char *_cache_get_desc(pmpkg_t *pkg)
68 LAZY_LOAD(INFRQ_DESC, NULL);
69 return pkg->desc;
72 static const char *_cache_get_url(pmpkg_t *pkg)
74 LAZY_LOAD(INFRQ_DESC, NULL);
75 return pkg->url;
78 static time_t _cache_get_builddate(pmpkg_t *pkg)
80 LAZY_LOAD(INFRQ_DESC, 0);
81 return pkg->builddate;
84 static time_t _cache_get_installdate(pmpkg_t *pkg)
86 LAZY_LOAD(INFRQ_DESC, 0);
87 return pkg->installdate;
90 static const char *_cache_get_packager(pmpkg_t *pkg)
92 LAZY_LOAD(INFRQ_DESC, NULL);
93 return pkg->packager;
96 static const char *_cache_get_md5sum(pmpkg_t *pkg)
98 LAZY_LOAD(INFRQ_DESC, NULL);
99 return pkg->md5sum;
102 static const char *_cache_get_arch(pmpkg_t *pkg)
104 LAZY_LOAD(INFRQ_DESC, NULL);
105 return pkg->arch;
108 static off_t _cache_get_size(pmpkg_t *pkg)
110 LAZY_LOAD(INFRQ_DESC, -1);
111 return pkg->size;
114 static off_t _cache_get_isize(pmpkg_t *pkg)
116 LAZY_LOAD(INFRQ_DESC, -1);
117 return pkg->isize;
120 static pmpkgreason_t _cache_get_reason(pmpkg_t *pkg)
122 LAZY_LOAD(INFRQ_DESC, -1);
123 return pkg->reason;
126 static alpm_list_t *_cache_get_licenses(pmpkg_t *pkg)
128 LAZY_LOAD(INFRQ_DESC, NULL);
129 return pkg->licenses;
132 static alpm_list_t *_cache_get_groups(pmpkg_t *pkg)
134 LAZY_LOAD(INFRQ_DESC, NULL);
135 return pkg->groups;
138 static int _cache_has_scriptlet(pmpkg_t *pkg)
140 /* Sanity checks */
141 ASSERT(handle != NULL, return -1);
143 if(!(pkg->infolevel & INFRQ_SCRIPTLET)) {
144 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_SCRIPTLET);
146 return pkg->scriptlet;
149 static alpm_list_t *_cache_get_depends(pmpkg_t *pkg)
151 LAZY_LOAD(INFRQ_DESC, NULL);
152 return pkg->depends;
155 static alpm_list_t *_cache_get_optdepends(pmpkg_t *pkg)
157 LAZY_LOAD(INFRQ_DESC, NULL);
158 return pkg->optdepends;
161 static alpm_list_t *_cache_get_conflicts(pmpkg_t *pkg)
163 LAZY_LOAD(INFRQ_DESC, NULL);
164 return pkg->conflicts;
167 static alpm_list_t *_cache_get_provides(pmpkg_t *pkg)
169 LAZY_LOAD(INFRQ_DESC, NULL);
170 return pkg->provides;
173 static alpm_list_t *_cache_get_replaces(pmpkg_t *pkg)
175 LAZY_LOAD(INFRQ_DESC, NULL);
176 return pkg->replaces;
179 /* local packages can not have deltas */
180 static alpm_list_t *_cache_get_deltas(pmpkg_t UNUSED *pkg)
182 return NULL;
185 static alpm_list_t *_cache_get_files(pmpkg_t *pkg)
187 /* Sanity checks */
188 ASSERT(handle != NULL, return NULL);
190 if(pkg->origin == PKG_FROM_LOCALDB
191 && !(pkg->infolevel & INFRQ_FILES)) {
192 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
194 return pkg->files;
197 static alpm_list_t *_cache_get_backup(pmpkg_t *pkg)
199 /* Sanity checks */
200 ASSERT(handle != NULL, return NULL);
202 if(pkg->origin == PKG_FROM_LOCALDB
203 && !(pkg->infolevel & INFRQ_FILES)) {
204 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
206 return pkg->backup;
210 * Open a package changelog for reading. Similar to fopen in functionality,
211 * except that the returned 'file stream' is from the database.
212 * @param pkg the package (from db) to read the changelog
213 * @return a 'file stream' to the package changelog
215 static void *_cache_changelog_open(pmpkg_t *pkg)
217 /* Sanity checks */
218 ASSERT(handle != NULL, return NULL);
220 char clfile[PATH_MAX];
221 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
222 alpm_option_get_dbpath(),
223 alpm_db_get_name(alpm_pkg_get_db(pkg)),
224 alpm_pkg_get_name(pkg),
225 alpm_pkg_get_version(pkg));
226 return fopen(clfile, "r");
230 * Read data from an open changelog 'file stream'. Similar to fread in
231 * functionality, this function takes a buffer and amount of data to read.
232 * @param ptr a buffer to fill with raw changelog data
233 * @param size the size of the buffer
234 * @param pkg the package that the changelog is being read from
235 * @param fp a 'file stream' to the package changelog
236 * @return the number of characters read, or 0 if there is no more data
238 static size_t _cache_changelog_read(void *ptr, size_t size,
239 const pmpkg_t UNUSED *pkg, const void *fp)
241 return fread(ptr, 1, size, (FILE *)fp);
245 * Close a package changelog for reading. Similar to fclose in functionality,
246 * except that the 'file stream' is from the database.
247 * @param pkg the package that the changelog was read from
248 * @param fp a 'file stream' to the package changelog
249 * @return whether closing the package changelog stream was successful
251 static int _cache_changelog_close(const pmpkg_t UNUSED *pkg, void *fp)
253 return fclose((FILE *)fp);
257 /** The local database operations struct. Get package fields through
258 * lazy accessor methods that handle any backend loading and caching
259 * logic.
261 static struct pkg_operations local_pkg_ops = {
262 .get_filename = _cache_get_filename,
263 .get_desc = _cache_get_desc,
264 .get_url = _cache_get_url,
265 .get_builddate = _cache_get_builddate,
266 .get_installdate = _cache_get_installdate,
267 .get_packager = _cache_get_packager,
268 .get_md5sum = _cache_get_md5sum,
269 .get_arch = _cache_get_arch,
270 .get_size = _cache_get_size,
271 .get_isize = _cache_get_isize,
272 .get_reason = _cache_get_reason,
273 .has_scriptlet = _cache_has_scriptlet,
274 .get_licenses = _cache_get_licenses,
275 .get_groups = _cache_get_groups,
276 .get_depends = _cache_get_depends,
277 .get_optdepends = _cache_get_optdepends,
278 .get_conflicts = _cache_get_conflicts,
279 .get_provides = _cache_get_provides,
280 .get_replaces = _cache_get_replaces,
281 .get_deltas = _cache_get_deltas,
282 .get_files = _cache_get_files,
283 .get_backup = _cache_get_backup,
285 .changelog_open = _cache_changelog_open,
286 .changelog_read = _cache_changelog_read,
287 .changelog_close = _cache_changelog_close,
290 static int checkdbdir(pmdb_t *db)
292 struct stat buf;
293 const char *path = _alpm_db_path(db);
295 if(stat(path, &buf) != 0) {
296 _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
297 path);
298 if(_alpm_makepath(path) != 0) {
299 RET_ERR(PM_ERR_SYSTEM, -1);
301 } else if(!S_ISDIR(buf.st_mode)) {
302 _alpm_log(PM_LOG_WARNING, _("removing invalid database: %s\n"), path);
303 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
304 RET_ERR(PM_ERR_SYSTEM, -1);
307 return 0;
310 static int is_dir(const char *path, struct dirent *entry)
312 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
313 if(entry->d_type != DT_UNKNOWN) {
314 return (entry->d_type == DT_DIR);
316 #endif
318 char buffer[PATH_MAX];
319 struct stat sbuf;
321 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
323 if(!stat(buffer, &sbuf)) {
324 return S_ISDIR(sbuf.st_mode);
328 return 0;
331 static int local_db_populate(pmdb_t *db)
333 size_t est_count;
334 int count = 0;
335 struct stat buf;
336 struct dirent *ent = NULL;
337 const char *dbpath;
338 DIR *dbdir;
340 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
342 dbpath = _alpm_db_path(db);
343 if(dbpath == NULL) {
344 /* pm_errno set in _alpm_db_path() */
345 return -1;
347 dbdir = opendir(dbpath);
348 if(dbdir == NULL) {
349 if(errno == ENOENT) {
350 /* no database existing yet is not an error */
351 return 0;
353 RET_ERR(PM_ERR_DB_OPEN, -1);
355 if(fstat(dirfd(dbdir), &buf) != 0) {
356 RET_ERR(PM_ERR_DB_OPEN, -1);
358 if(buf.st_nlink >= 2) {
359 est_count = buf.st_nlink;
360 } else {
361 /* Some filesystems don't subscribe to the two-implicit links school of
362 * thought, e.g. BTRFS, HFS+. See
363 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
365 est_count = 0;
366 while(readdir(dbdir) != NULL) {
367 est_count++;
369 rewinddir(dbdir);
371 if(est_count >= 2) {
372 /* subtract the two extra pointers to get # of children */
373 est_count -= 2;
376 /* initialize hash at 50% full */
377 db->pkgcache = _alpm_pkghash_create(est_count * 2);
378 if(db->pkgcache == NULL){
379 closedir(dbdir);
380 RET_ERR(PM_ERR_MEMORY, -1);
383 while((ent = readdir(dbdir)) != NULL) {
384 const char *name = ent->d_name;
386 pmpkg_t *pkg;
388 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
389 continue;
391 if(!is_dir(dbpath, ent)) {
392 continue;
395 pkg = _alpm_pkg_new();
396 if(pkg == NULL) {
397 closedir(dbdir);
398 RET_ERR(PM_ERR_MEMORY, -1);
400 /* split the db entry name */
401 if(_alpm_splitname(name, pkg) != 0) {
402 _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
403 name);
404 _alpm_pkg_free(pkg);
405 continue;
408 /* duplicated database entries are not allowed */
409 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
410 _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
411 _alpm_pkg_free(pkg);
412 continue;
415 pkg->origin = PKG_FROM_LOCALDB;
416 pkg->origin_data.db = db;
417 pkg->ops = &local_pkg_ops;
419 /* explicitly read with only 'BASE' data, accessors will handle the rest */
420 if(_alpm_local_db_read(db, pkg, INFRQ_BASE) == -1) {
421 _alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
422 _alpm_pkg_free(pkg);
423 continue;
426 /* add to the collection */
427 _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
428 pkg->name, db->treename);
429 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
430 count++;
433 closedir(dbdir);
434 if(count > 0) {
435 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
437 _alpm_log(PM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
438 count, db->treename);
440 return count;
443 /* Note: the return value must be freed by the caller */
444 static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
446 size_t len;
447 char *pkgpath;
448 const char *dbpath;
450 dbpath = _alpm_db_path(db);
451 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
452 MALLOC(pkgpath, len, RET_ERR(PM_ERR_MEMORY, NULL));
453 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
454 return pkgpath;
458 int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
460 FILE *fp = NULL;
461 char path[PATH_MAX];
462 char line[1024];
463 char *pkgpath = NULL;
465 if(db == NULL) {
466 RET_ERR(PM_ERR_DB_NULL, -1);
469 if(info == NULL || info->name == NULL || info->version == NULL) {
470 _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_local_db_read, skipping\n");
471 return -1;
474 if(info->origin != PKG_FROM_LOCALDB) {
475 _alpm_log(PM_LOG_DEBUG,
476 "request to read info for a non-local package '%s', skipping...\n",
477 info->name);
478 return -1;
481 /* bitmask logic here:
482 * infolevel: 00001111
483 * inforeq: 00010100
484 * & result: 00000100
485 * == to inforeq? nope, we need to load more info. */
486 if((info->infolevel & inforeq) == inforeq) {
487 /* already loaded all of this info, do nothing */
488 return 0;
490 _alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
491 info->name, inforeq);
493 /* clear out 'line', to be certain - and to make valgrind happy */
494 memset(line, 0, sizeof(line));
496 pkgpath = get_pkgpath(db, info);
498 if(access(pkgpath, F_OK)) {
499 /* directory doesn't exist or can't be opened */
500 _alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
501 info->name, info->version, db->treename);
502 goto error;
505 /* DESC */
506 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
507 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
508 if((fp = fopen(path, "r")) == NULL) {
509 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
510 goto error;
512 while(!feof(fp)) {
513 if(fgets(line, sizeof(line), fp) == NULL) {
514 break;
516 _alpm_strtrim(line);
517 if(strcmp(line, "%NAME%") == 0) {
518 if(fgets(line, sizeof(line), fp) == NULL) {
519 goto error;
521 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
522 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
523 "mismatch on package %s\n"), db->treename, info->name);
525 } else if(strcmp(line, "%VERSION%") == 0) {
526 if(fgets(line, sizeof(line), fp) == NULL) {
527 goto error;
529 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
530 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
531 "mismatch on package %s\n"), db->treename, info->name);
533 } else if(strcmp(line, "%DESC%") == 0) {
534 if(fgets(line, sizeof(line), fp) == NULL) {
535 goto error;
537 STRDUP(info->desc, _alpm_strtrim(line), goto error);
538 } else if(strcmp(line, "%GROUPS%") == 0) {
539 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
540 char *linedup;
541 STRDUP(linedup, line, goto error);
542 info->groups = alpm_list_add(info->groups, linedup);
544 } else if(strcmp(line, "%URL%") == 0) {
545 if(fgets(line, sizeof(line), fp) == NULL) {
546 goto error;
548 STRDUP(info->url, _alpm_strtrim(line), goto error);
549 } else if(strcmp(line, "%LICENSE%") == 0) {
550 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
551 char *linedup;
552 STRDUP(linedup, line, goto error);
553 info->licenses = alpm_list_add(info->licenses, linedup);
555 } else if(strcmp(line, "%ARCH%") == 0) {
556 if(fgets(line, sizeof(line), fp) == NULL) {
557 goto error;
559 STRDUP(info->arch, _alpm_strtrim(line), goto error);
560 } else if(strcmp(line, "%BUILDDATE%") == 0) {
561 if(fgets(line, sizeof(line), fp) == NULL) {
562 goto error;
564 _alpm_strtrim(line);
565 info->builddate = _alpm_parsedate(line);
566 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
567 if(fgets(line, sizeof(line), fp) == NULL) {
568 goto error;
570 _alpm_strtrim(line);
571 info->installdate = _alpm_parsedate(line);
572 } else if(strcmp(line, "%PACKAGER%") == 0) {
573 if(fgets(line, sizeof(line), fp) == NULL) {
574 goto error;
576 STRDUP(info->packager, _alpm_strtrim(line), goto error);
577 } else if(strcmp(line, "%REASON%") == 0) {
578 if(fgets(line, sizeof(line), fp) == NULL) {
579 goto error;
581 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
582 } else if(strcmp(line, "%SIZE%") == 0) {
583 /* NOTE: the CSIZE and SIZE fields both share the "size" field
584 * in the pkginfo_t struct. This can be done b/c CSIZE
585 * is currently only used in sync databases, and SIZE is
586 * only used in local databases.
588 if(fgets(line, sizeof(line), fp) == NULL) {
589 goto error;
591 info->size = atol(_alpm_strtrim(line));
592 /* also store this value to isize */
593 info->isize = info->size;
594 } else if(strcmp(line, "%REPLACES%") == 0) {
595 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
596 char *linedup;
597 STRDUP(linedup, line, goto error);
598 info->replaces = alpm_list_add(info->replaces, linedup);
600 } else if(strcmp(line, "%DEPENDS%") == 0) {
601 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
602 pmdepend_t *dep = _alpm_splitdep(line);
603 info->depends = alpm_list_add(info->depends, dep);
605 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
606 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
607 char *linedup;
608 STRDUP(linedup, line, goto error);
609 info->optdepends = alpm_list_add(info->optdepends, linedup);
611 } else if(strcmp(line, "%CONFLICTS%") == 0) {
612 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
613 char *linedup;
614 STRDUP(linedup, line, goto error);
615 info->conflicts = alpm_list_add(info->conflicts, linedup);
617 } else if(strcmp(line, "%PROVIDES%") == 0) {
618 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
619 char *linedup;
620 STRDUP(linedup, line, goto error);
621 info->provides = alpm_list_add(info->provides, linedup);
625 fclose(fp);
626 fp = NULL;
629 /* FILES */
630 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
631 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
632 if((fp = fopen(path, "r")) == NULL) {
633 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
634 goto error;
636 while(fgets(line, sizeof(line), fp)) {
637 _alpm_strtrim(line);
638 if(strcmp(line, "%FILES%") == 0) {
639 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
640 char *linedup;
641 STRDUP(linedup, line, goto error);
642 info->files = alpm_list_add(info->files, linedup);
644 } else if(strcmp(line, "%BACKUP%") == 0) {
645 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
646 char *linedup;
647 STRDUP(linedup, line, goto error);
648 info->backup = alpm_list_add(info->backup, linedup);
652 fclose(fp);
653 fp = NULL;
656 /* INSTALL */
657 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
658 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
659 if(access(path, F_OK) == 0) {
660 info->scriptlet = 1;
664 /* internal */
665 info->infolevel |= inforeq;
667 free(pkgpath);
668 return 0;
670 error:
671 free(pkgpath);
672 if(fp) {
673 fclose(fp);
675 return -1;
678 int _alpm_local_db_prepare(pmdb_t *db, pmpkg_t *info)
680 mode_t oldmask;
681 int retval = 0;
682 char *pkgpath = NULL;
684 if(checkdbdir(db) != 0) {
685 return -1;
688 oldmask = umask(0000);
689 pkgpath = get_pkgpath(db, info);
691 if((retval = mkdir(pkgpath, 0755)) != 0) {
692 _alpm_log(PM_LOG_ERROR, _("could not create directory %s: %s\n"),
693 pkgpath, strerror(errno));
696 free(pkgpath);
697 umask(oldmask);
699 return retval;
702 int _alpm_local_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
704 FILE *fp = NULL;
705 char path[PATH_MAX];
706 mode_t oldmask;
707 alpm_list_t *lp = NULL;
708 int retval = 0;
709 char *pkgpath = NULL;
711 if(db == NULL || info == NULL) {
712 return -1;
715 pkgpath = get_pkgpath(db, info);
717 /* make sure we have a sane umask */
718 oldmask = umask(0022);
720 if(strcmp(db->treename, "local") != 0) {
721 return -1;
724 /* DESC */
725 if(inforeq & INFRQ_DESC) {
726 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
727 info->name, info->version);
728 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
729 if((fp = fopen(path, "w")) == NULL) {
730 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
731 retval = -1;
732 goto cleanup;
734 fprintf(fp, "%%NAME%%\n%s\n\n"
735 "%%VERSION%%\n%s\n\n", info->name, info->version);
736 if(info->desc) {
737 fprintf(fp, "%%DESC%%\n"
738 "%s\n\n", info->desc);
740 if(info->groups) {
741 fputs("%GROUPS%\n", fp);
742 for(lp = info->groups; lp; lp = lp->next) {
743 fprintf(fp, "%s\n", (char *)lp->data);
745 fprintf(fp, "\n");
747 if(info->replaces) {
748 fputs("%REPLACES%\n", fp);
749 for(lp = info->replaces; lp; lp = lp->next) {
750 fprintf(fp, "%s\n", (char *)lp->data);
752 fprintf(fp, "\n");
754 if(info->url) {
755 fprintf(fp, "%%URL%%\n"
756 "%s\n\n", info->url);
758 if(info->licenses) {
759 fputs("%LICENSE%\n", fp);
760 for(lp = info->licenses; lp; lp = lp->next) {
761 fprintf(fp, "%s\n", (char *)lp->data);
763 fprintf(fp, "\n");
765 if(info->arch) {
766 fprintf(fp, "%%ARCH%%\n"
767 "%s\n\n", info->arch);
769 if(info->builddate) {
770 fprintf(fp, "%%BUILDDATE%%\n"
771 "%ld\n\n", info->builddate);
773 if(info->installdate) {
774 fprintf(fp, "%%INSTALLDATE%%\n"
775 "%ld\n\n", info->installdate);
777 if(info->packager) {
778 fprintf(fp, "%%PACKAGER%%\n"
779 "%s\n\n", info->packager);
781 if(info->isize) {
782 /* only write installed size, csize is irrelevant once installed */
783 fprintf(fp, "%%SIZE%%\n"
784 "%jd\n\n", (intmax_t)info->isize);
786 if(info->reason) {
787 fprintf(fp, "%%REASON%%\n"
788 "%u\n\n", info->reason);
790 if(info->depends) {
791 fputs("%DEPENDS%\n", fp);
792 for(lp = info->depends; lp; lp = lp->next) {
793 char *depstring = alpm_dep_compute_string(lp->data);
794 fprintf(fp, "%s\n", depstring);
795 free(depstring);
797 fprintf(fp, "\n");
799 if(info->optdepends) {
800 fputs("%OPTDEPENDS%\n", fp);
801 for(lp = info->optdepends; lp; lp = lp->next) {
802 fprintf(fp, "%s\n", (char *)lp->data);
804 fprintf(fp, "\n");
806 if(info->conflicts) {
807 fputs("%CONFLICTS%\n", fp);
808 for(lp = info->conflicts; lp; lp = lp->next) {
809 fprintf(fp, "%s\n", (char *)lp->data);
811 fprintf(fp, "\n");
813 if(info->provides) {
814 fputs("%PROVIDES%\n", fp);
815 for(lp = info->provides; lp; lp = lp->next) {
816 fprintf(fp, "%s\n", (char *)lp->data);
818 fprintf(fp, "\n");
821 fclose(fp);
822 fp = NULL;
825 /* FILES */
826 if(inforeq & INFRQ_FILES) {
827 _alpm_log(PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
828 info->name, info->version);
829 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
830 if((fp = fopen(path, "w")) == NULL) {
831 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
832 retval = -1;
833 goto cleanup;
835 if(info->files) {
836 fprintf(fp, "%%FILES%%\n");
837 for(lp = info->files; lp; lp = lp->next) {
838 fprintf(fp, "%s\n", (char *)lp->data);
840 fprintf(fp, "\n");
842 if(info->backup) {
843 fprintf(fp, "%%BACKUP%%\n");
844 for(lp = info->backup; lp; lp = lp->next) {
845 fprintf(fp, "%s\n", (char *)lp->data);
847 fprintf(fp, "\n");
849 fclose(fp);
850 fp = NULL;
853 /* INSTALL */
854 /* nothing needed here (script is automatically extracted) */
856 cleanup:
857 umask(oldmask);
858 free(pkgpath);
860 if(fp) {
861 fclose(fp);
864 return retval;
867 int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
869 int ret = 0;
870 char *pkgpath = NULL;
872 if(db == NULL || info == NULL) {
873 RET_ERR(PM_ERR_DB_NULL, -1);
876 pkgpath = get_pkgpath(db, info);
878 ret = _alpm_rmrf(pkgpath);
879 free(pkgpath);
880 if(ret != 0) {
881 ret = -1;
883 return ret;
886 static int local_db_version(pmdb_t *db)
888 struct dirent *ent = NULL;
889 const char *dbpath;
890 DIR *dbdir;
891 int version;
893 dbpath = _alpm_db_path(db);
894 if(dbpath == NULL) {
895 RET_ERR(PM_ERR_DB_OPEN, -1);
897 dbdir = opendir(dbpath);
898 if(dbdir == NULL) {
899 if(errno == ENOENT) {
900 /* database dir doesn't exist yet */
901 version = 2;
902 goto done;
903 } else {
904 RET_ERR(PM_ERR_DB_OPEN, -1);
908 while((ent = readdir(dbdir)) != NULL) {
909 const char *name = ent->d_name;
910 char path[PATH_MAX];
912 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
913 continue;
915 if(!is_dir(dbpath, ent)) {
916 continue;
919 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
920 if(access(path, F_OK) == 0) {
921 /* we found a depends file- bail */
922 version = 1;
923 goto done;
926 /* we found no depends file after full scan */
927 version = 2;
929 done:
930 if(dbdir) {
931 closedir(dbdir);
934 _alpm_log(PM_LOG_DEBUG, "local database version %d\n", version);
935 return version;
938 struct db_operations local_db_ops = {
939 .populate = local_db_populate,
940 .unregister = _alpm_db_unregister,
941 .version = local_db_version,
944 pmdb_t *_alpm_db_register_local(void)
946 pmdb_t *db;
948 _alpm_log(PM_LOG_DEBUG, "registering local database\n");
950 db = _alpm_db_new("local", 1);
951 if(db == NULL) {
952 RET_ERR(PM_ERR_DB_CREATE, NULL);
954 db->ops = &local_db_ops;
956 handle->db_local = db;
957 return db;
960 /* vim: set ts=2 sw=2 noet: */