Separate be_files into be_sync and be_local
[pacman-ng.git] / lib / libalpm / be_local.c
blob2cd8ae146621610a0640acd76811df927c4c8f92
1 /*
2 * be_local.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 local 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 local_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,
320 .changelog_open = _cache_changelog_open,
321 .changelog_read = _cache_changelog_read,
322 .changelog_close = _cache_changelog_close,
326 static int is_dir(const char *path, struct dirent *entry)
328 #ifdef DT_DIR
329 return(entry->d_type == DT_DIR);
330 #else
331 char buffer[PATH_MAX];
332 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
334 struct stat sbuf;
335 if (!stat(buffer, &sbuf)) {
336 return(S_ISDIR(sbuf.st_mode));
339 return(0);
340 #endif
343 pmdb_t *_alpm_db_register_local(void)
345 pmdb_t *db;
347 ALPM_LOG_FUNC;
349 if(handle->db_local != NULL) {
350 _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n"));
351 RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
354 _alpm_log(PM_LOG_DEBUG, "registering local database\n");
356 db = _alpm_db_new("local", 1);
357 db->ops = &default_db_ops;
358 if(db == NULL) {
359 RET_ERR(PM_ERR_DB_CREATE, NULL);
362 handle->db_local = db;
363 return(db);
367 int _alpm_db_populate(pmdb_t *db)
369 int count = 0;
370 struct dirent *ent = NULL;
371 const char *dbpath;
372 DIR *dbdir;
374 ALPM_LOG_FUNC;
376 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
378 dbpath = _alpm_db_path(db);
379 dbdir = opendir(dbpath);
380 if(dbdir == NULL) {
381 return(0);
383 while((ent = readdir(dbdir)) != NULL) {
384 const char *name = ent->d_name;
385 pmpkg_t *pkg;
387 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
388 continue;
390 if(!is_dir(dbpath, ent)) {
391 continue;
394 pkg = _alpm_pkg_new();
395 if(pkg == NULL) {
396 closedir(dbdir);
397 return(-1);
399 /* split the db entry name */
400 if(splitname(name, pkg) != 0) {
401 _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
402 name);
403 _alpm_pkg_free(pkg);
404 continue;
407 /* duplicated database entries are not allowed */
408 if(_alpm_pkg_find(db->pkgcache, pkg->name)) {
409 _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
410 _alpm_pkg_free(pkg);
411 continue;
414 /* explicitly read with only 'BASE' data, accessors will handle the rest */
415 if(_alpm_db_read(db, pkg, INFRQ_BASE) == -1) {
416 _alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
417 _alpm_pkg_free(pkg);
418 continue;
421 pkg->origin = PKG_FROM_LOCALDB;
422 pkg->ops = &local_pkg_ops;
424 pkg->origin_data.db = db;
425 /* add to the collection */
426 _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
427 pkg->name, db->treename);
428 db->pkgcache = alpm_list_add(db->pkgcache, pkg);
429 count++;
432 closedir(dbdir);
433 db->pkgcache = alpm_list_msort(db->pkgcache, count, _alpm_pkg_cmp);
434 return(count);
437 int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
439 FILE *fp = NULL;
440 char path[PATH_MAX];
441 char line[1024];
442 char *pkgpath = NULL;
444 ALPM_LOG_FUNC;
446 if(db == NULL) {
447 RET_ERR(PM_ERR_DB_NULL, -1);
450 if(info == NULL || info->name == NULL || info->version == NULL) {
451 _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_db_read, skipping\n");
452 return(-1);
455 if(info->origin == PKG_FROM_FILE) {
456 _alpm_log(PM_LOG_DEBUG, "request to read database info for a file-based package '%s', skipping...\n", info->name);
457 return(-1);
460 /* bitmask logic here:
461 * infolevel: 00001111
462 * inforeq: 00010100
463 * & result: 00000100
464 * == to inforeq? nope, we need to load more info. */
465 if((info->infolevel & inforeq) == inforeq) {
466 /* already loaded this info, do nothing */
467 return(0);
469 _alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
470 info->name, inforeq);
472 /* clear out 'line', to be certain - and to make valgrind happy */
473 memset(line, 0, sizeof(line));
475 pkgpath = get_pkgpath(db, info);
477 if(access(pkgpath, F_OK)) {
478 /* directory doesn't exist or can't be opened */
479 _alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
480 info->name, info->version, db->treename);
481 goto error;
484 /* DESC */
485 if(inforeq & INFRQ_DESC) {
486 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
487 if((fp = fopen(path, "r")) == NULL) {
488 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
489 goto error;
491 while(!feof(fp)) {
492 if(fgets(line, sizeof(line), fp) == NULL) {
493 break;
495 _alpm_strtrim(line);
496 if(strcmp(line, "%NAME%") == 0) {
497 if(fgets(line, sizeof(line), fp) == NULL) {
498 goto error;
500 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
501 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
502 "mismatch on package %s\n"), db->treename, info->name);
504 } else if(strcmp(line, "%VERSION%") == 0) {
505 if(fgets(line, sizeof(line), fp) == NULL) {
506 goto error;
508 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
509 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
510 "mismatch on package %s\n"), db->treename, info->name);
512 } else if(strcmp(line, "%FILENAME%") == 0) {
513 if(fgets(line, sizeof(line), fp) == NULL) {
514 goto error;
516 STRDUP(info->filename, _alpm_strtrim(line), goto error);
517 } else if(strcmp(line, "%DESC%") == 0) {
518 if(fgets(line, sizeof(line), fp) == NULL) {
519 goto error;
521 STRDUP(info->desc, _alpm_strtrim(line), goto error);
522 } else if(strcmp(line, "%GROUPS%") == 0) {
523 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
524 char *linedup;
525 STRDUP(linedup, _alpm_strtrim(line), goto error);
526 info->groups = alpm_list_add(info->groups, linedup);
528 } else if(strcmp(line, "%URL%") == 0) {
529 if(fgets(line, sizeof(line), fp) == NULL) {
530 goto error;
532 STRDUP(info->url, _alpm_strtrim(line), goto error);
533 } else if(strcmp(line, "%LICENSE%") == 0) {
534 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
535 char *linedup;
536 STRDUP(linedup, _alpm_strtrim(line), goto error);
537 info->licenses = alpm_list_add(info->licenses, linedup);
539 } else if(strcmp(line, "%ARCH%") == 0) {
540 if(fgets(line, sizeof(line), fp) == NULL) {
541 goto error;
543 STRDUP(info->arch, _alpm_strtrim(line), goto error);
544 } else if(strcmp(line, "%BUILDDATE%") == 0) {
545 if(fgets(line, sizeof(line), fp) == NULL) {
546 goto error;
548 _alpm_strtrim(line);
550 char first = tolower((unsigned char)line[0]);
551 if(first > 'a' && first < 'z') {
552 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
553 setlocale(LC_TIME, "C");
554 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
555 info->builddate = mktime(&tmp_tm);
556 setlocale(LC_TIME, "");
557 } else {
558 info->builddate = atol(line);
560 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
561 if(fgets(line, sizeof(line), fp) == NULL) {
562 goto error;
564 _alpm_strtrim(line);
566 char first = tolower((unsigned char)line[0]);
567 if(first > 'a' && first < 'z') {
568 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
569 setlocale(LC_TIME, "C");
570 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
571 info->installdate = mktime(&tmp_tm);
572 setlocale(LC_TIME, "");
573 } else {
574 info->installdate = atol(line);
576 } else if(strcmp(line, "%PACKAGER%") == 0) {
577 if(fgets(line, sizeof(line), fp) == NULL) {
578 goto error;
580 STRDUP(info->packager, _alpm_strtrim(line), goto error);
581 } else if(strcmp(line, "%REASON%") == 0) {
582 if(fgets(line, sizeof(line), fp) == NULL) {
583 goto error;
585 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
586 } else if(strcmp(line, "%SIZE%") == 0 || strcmp(line, "%CSIZE%") == 0) {
587 /* NOTE: the CSIZE and SIZE fields both share the "size" field
588 * in the pkginfo_t struct. This can be done b/c CSIZE
589 * is currently only used in sync databases, and SIZE is
590 * only used in local databases.
592 if(fgets(line, sizeof(line), fp) == NULL) {
593 goto error;
595 info->size = atol(_alpm_strtrim(line));
596 /* also store this value to isize if isize is unset */
597 if(info->isize == 0) {
598 info->isize = info->size;
600 } else if(strcmp(line, "%ISIZE%") == 0) {
601 /* ISIZE (installed size) tag only appears in sync repositories,
602 * not the local one. */
603 if(fgets(line, sizeof(line), fp) == NULL) {
604 goto error;
606 info->isize = atol(_alpm_strtrim(line));
607 } else if(strcmp(line, "%MD5SUM%") == 0) {
608 /* MD5SUM tag only appears in sync repositories,
609 * not the local one. */
610 if(fgets(line, sizeof(line), fp) == NULL) {
611 goto error;
613 STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
614 } else if(strcmp(line, "%REPLACES%") == 0) {
615 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
616 char *linedup;
617 STRDUP(linedup, _alpm_strtrim(line), goto error);
618 info->replaces = alpm_list_add(info->replaces, linedup);
620 } else if(strcmp(line, "%FORCE%") == 0) {
621 info->force = 1;
624 fclose(fp);
625 fp = NULL;
628 /* FILES */
629 if(inforeq & INFRQ_FILES) {
630 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
631 if((fp = fopen(path, "r")) == NULL) {
632 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
633 goto error;
635 while(fgets(line, sizeof(line), fp)) {
636 _alpm_strtrim(line);
637 if(strcmp(line, "%FILES%") == 0) {
638 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
639 char *linedup;
640 STRDUP(linedup, _alpm_strtrim(line), goto error);
641 info->files = alpm_list_add(info->files, linedup);
643 } else if(strcmp(line, "%BACKUP%") == 0) {
644 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
645 char *linedup;
646 STRDUP(linedup, _alpm_strtrim(line), goto error);
647 info->backup = alpm_list_add(info->backup, linedup);
651 fclose(fp);
652 fp = NULL;
655 /* DEPENDS */
656 if(inforeq & INFRQ_DEPENDS) {
657 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
658 if((fp = fopen(path, "r")) == NULL) {
659 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
660 goto error;
662 while(!feof(fp)) {
663 if(fgets(line, sizeof(line), fp) == NULL) {
664 break;
666 _alpm_strtrim(line);
667 if(strcmp(line, "%DEPENDS%") == 0) {
668 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
669 pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
670 info->depends = alpm_list_add(info->depends, dep);
672 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
673 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
674 char *linedup;
675 STRDUP(linedup, _alpm_strtrim(line), goto error);
676 info->optdepends = alpm_list_add(info->optdepends, linedup);
678 } else if(strcmp(line, "%CONFLICTS%") == 0) {
679 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
680 char *linedup;
681 STRDUP(linedup, _alpm_strtrim(line), goto error);
682 info->conflicts = alpm_list_add(info->conflicts, linedup);
684 } else if(strcmp(line, "%PROVIDES%") == 0) {
685 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
686 char *linedup;
687 STRDUP(linedup, _alpm_strtrim(line), goto error);
688 info->provides = alpm_list_add(info->provides, linedup);
692 fclose(fp);
693 fp = NULL;
696 /* DELTAS */
697 if(inforeq & INFRQ_DELTAS) {
698 snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
699 if((fp = fopen(path, "r"))) {
700 while(!feof(fp)) {
701 if(fgets(line, sizeof(line), fp) == NULL) {
702 break;
704 _alpm_strtrim(line);
705 if(strcmp(line, "%DELTAS%") == 0) {
706 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
707 pmdelta_t *delta = _alpm_delta_parse(line);
708 if(delta) {
709 info->deltas = alpm_list_add(info->deltas, delta);
714 fclose(fp);
715 fp = NULL;
719 /* INSTALL */
720 if(inforeq & INFRQ_SCRIPTLET) {
721 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
722 if(access(path, F_OK) == 0) {
723 info->scriptlet = 1;
727 /* internal */
728 info->infolevel |= inforeq;
730 free(pkgpath);
731 return(0);
733 error:
734 free(pkgpath);
735 if(fp) {
736 fclose(fp);
738 return(-1);
741 int _alpm_local_db_prepare(pmdb_t *db, pmpkg_t *info)
743 mode_t oldmask;
744 int retval = 0;
745 char *pkgpath = NULL;
747 if(checkdbdir(db) != 0) {
748 return(-1);
751 oldmask = umask(0000);
752 pkgpath = get_pkgpath(db, info);
754 if((retval = mkdir(pkgpath, 0755)) != 0) {
755 _alpm_log(PM_LOG_ERROR, _("could not create directory %s: %s\n"),
756 pkgpath, strerror(errno));
759 free(pkgpath);
760 umask(oldmask);
762 return(retval);
765 int _alpm_local_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
767 FILE *fp = NULL;
768 char path[PATH_MAX];
769 mode_t oldmask;
770 alpm_list_t *lp = NULL;
771 int retval = 0;
772 int local = 0;
773 char *pkgpath = NULL;
775 ALPM_LOG_FUNC;
777 if(db == NULL || info == NULL) {
778 return(-1);
781 pkgpath = get_pkgpath(db, info);
783 /* make sure we have a sane umask */
784 oldmask = umask(0022);
786 if(strcmp(db->treename, "local") == 0) {
787 local = 1;
790 /* DESC */
791 if(inforeq & INFRQ_DESC) {
792 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
793 info->name, info->version);
794 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
795 if((fp = fopen(path, "w")) == NULL) {
796 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
797 retval = -1;
798 goto cleanup;
800 fprintf(fp, "%%NAME%%\n%s\n\n"
801 "%%VERSION%%\n%s\n\n", info->name, info->version);
802 if(info->desc) {
803 fprintf(fp, "%%DESC%%\n"
804 "%s\n\n", info->desc);
806 if(info->groups) {
807 fputs("%GROUPS%\n", fp);
808 for(lp = info->groups; lp; lp = lp->next) {
809 fprintf(fp, "%s\n", (char *)lp->data);
811 fprintf(fp, "\n");
813 if(info->replaces) {
814 fputs("%REPLACES%\n", fp);
815 for(lp = info->replaces; lp; lp = lp->next) {
816 fprintf(fp, "%s\n", (char *)lp->data);
818 fprintf(fp, "\n");
820 if(info->force) {
821 fprintf(fp, "%%FORCE%%\n\n");
823 if(local) {
824 if(info->url) {
825 fprintf(fp, "%%URL%%\n"
826 "%s\n\n", info->url);
828 if(info->licenses) {
829 fputs("%LICENSE%\n", fp);
830 for(lp = info->licenses; lp; lp = lp->next) {
831 fprintf(fp, "%s\n", (char *)lp->data);
833 fprintf(fp, "\n");
835 if(info->arch) {
836 fprintf(fp, "%%ARCH%%\n"
837 "%s\n\n", info->arch);
839 if(info->builddate) {
840 fprintf(fp, "%%BUILDDATE%%\n"
841 "%ld\n\n", info->builddate);
843 if(info->installdate) {
844 fprintf(fp, "%%INSTALLDATE%%\n"
845 "%ld\n\n", info->installdate);
847 if(info->packager) {
848 fprintf(fp, "%%PACKAGER%%\n"
849 "%s\n\n", info->packager);
851 if(info->isize) {
852 /* only write installed size, csize is irrelevant once installed */
853 fprintf(fp, "%%SIZE%%\n"
854 "%jd\n\n", (intmax_t)info->isize);
856 if(info->reason) {
857 fprintf(fp, "%%REASON%%\n"
858 "%u\n\n", info->reason);
860 } else {
861 if(info->size) {
862 fprintf(fp, "%%CSIZE%%\n"
863 "%jd\n\n", (intmax_t)info->size);
865 if(info->isize) {
866 fprintf(fp, "%%ISIZE%%\n"
867 "%jd\n\n", (intmax_t)info->isize);
869 if(info->md5sum) {
870 fprintf(fp, "%%MD5SUM%%\n"
871 "%s\n\n", info->md5sum);
874 fclose(fp);
875 fp = NULL;
878 /* FILES */
879 if(local && (inforeq & INFRQ_FILES)) {
880 _alpm_log(PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
881 info->name, info->version);
882 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
883 if((fp = fopen(path, "w")) == NULL) {
884 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
885 retval = -1;
886 goto cleanup;
888 if(info->files) {
889 fprintf(fp, "%%FILES%%\n");
890 for(lp = info->files; lp; lp = lp->next) {
891 fprintf(fp, "%s\n", (char *)lp->data);
893 fprintf(fp, "\n");
895 if(info->backup) {
896 fprintf(fp, "%%BACKUP%%\n");
897 for(lp = info->backup; lp; lp = lp->next) {
898 fprintf(fp, "%s\n", (char *)lp->data);
900 fprintf(fp, "\n");
902 fclose(fp);
903 fp = NULL;
906 /* DEPENDS */
907 if(inforeq & INFRQ_DEPENDS) {
908 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DEPENDS information back to db\n",
909 info->name, info->version);
910 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
911 if((fp = fopen(path, "w")) == NULL) {
912 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
913 retval = -1;
914 goto cleanup;
916 if(info->depends) {
917 fputs("%DEPENDS%\n", fp);
918 for(lp = info->depends; lp; lp = lp->next) {
919 char *depstring = alpm_dep_compute_string(lp->data);
920 fprintf(fp, "%s\n", depstring);
921 free(depstring);
923 fprintf(fp, "\n");
925 if(info->optdepends) {
926 fputs("%OPTDEPENDS%\n", fp);
927 for(lp = info->optdepends; lp; lp = lp->next) {
928 fprintf(fp, "%s\n", (char *)lp->data);
930 fprintf(fp, "\n");
932 if(info->conflicts) {
933 fputs("%CONFLICTS%\n", fp);
934 for(lp = info->conflicts; lp; lp = lp->next) {
935 fprintf(fp, "%s\n", (char *)lp->data);
937 fprintf(fp, "\n");
939 if(info->provides) {
940 fputs("%PROVIDES%\n", fp);
941 for(lp = info->provides; lp; lp = lp->next) {
942 fprintf(fp, "%s\n", (char *)lp->data);
944 fprintf(fp, "\n");
946 fclose(fp);
947 fp = NULL;
950 /* INSTALL */
951 /* nothing needed here (script is automatically extracted) */
953 cleanup:
954 umask(oldmask);
955 free(pkgpath);
957 if(fp) {
958 fclose(fp);
961 return(retval);
964 int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
966 int ret = 0;
967 char *pkgpath = NULL;
969 ALPM_LOG_FUNC;
971 if(db == NULL || info == NULL) {
972 RET_ERR(PM_ERR_DB_NULL, -1);
975 pkgpath = get_pkgpath(db, info);
977 ret = _alpm_rmrf(pkgpath);
978 free(pkgpath);
979 if(ret != 0) {
980 ret = -1;
982 return(ret);
985 /* vim: set ts=2 sw=2 noet: */