Move and rename splitname
[pacman-ng.git] / lib / libalpm / be_local.c
blobabf0af4e3793ada961caddc921ba6f15408c076f
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_local_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_local_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_local_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,
325 static int checkdbdir(pmdb_t *db)
327 struct stat buf;
328 const char *path = _alpm_db_path(db);
330 if(stat(path, &buf) != 0) {
331 _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
332 path);
333 if(_alpm_makepath(path) != 0) {
334 RET_ERR(PM_ERR_SYSTEM, -1);
336 } else if(!S_ISDIR(buf.st_mode)) {
337 _alpm_log(PM_LOG_WARNING, _("removing invalid database: %s\n"), path);
338 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
339 RET_ERR(PM_ERR_SYSTEM, -1);
342 return(0);
345 static int is_dir(const char *path, struct dirent *entry)
347 #ifdef DT_DIR
348 return(entry->d_type == DT_DIR);
349 #else
350 char buffer[PATH_MAX];
351 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
353 struct stat sbuf;
354 if (!stat(buffer, &sbuf)) {
355 return(S_ISDIR(sbuf.st_mode));
358 return(0);
359 #endif
362 int _alpm_local_db_populate(pmdb_t *db)
364 int count = 0;
365 struct dirent *ent = NULL;
366 const char *dbpath;
367 DIR *dbdir;
369 ALPM_LOG_FUNC;
371 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
373 dbpath = _alpm_db_path(db);
374 dbdir = opendir(dbpath);
375 if(dbdir == NULL) {
376 return(0);
378 while((ent = readdir(dbdir)) != NULL) {
379 const char *name = ent->d_name;
381 pmpkg_t *pkg;
383 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
384 continue;
386 if(!is_dir(dbpath, ent)) {
387 continue;
390 pkg = _alpm_pkg_new();
391 if(pkg == NULL) {
392 closedir(dbdir);
393 return(-1);
395 /* split the db entry name */
396 if(_alpm_splitname(name, pkg) != 0) {
397 _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
398 name);
399 _alpm_pkg_free(pkg);
400 continue;
403 /* duplicated database entries are not allowed */
404 if(_alpm_pkg_find(db->pkgcache, pkg->name)) {
405 _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
406 _alpm_pkg_free(pkg);
407 continue;
410 /* explicitly read with only 'BASE' data, accessors will handle the rest */
411 if(_alpm_local_db_read(db, pkg, INFRQ_BASE) == -1) {
412 _alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
413 _alpm_pkg_free(pkg);
414 continue;
417 pkg->origin = PKG_FROM_LOCALDB;
418 pkg->ops = &local_pkg_ops;
420 pkg->origin_data.db = db;
421 /* add to the collection */
422 _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
423 pkg->name, db->treename);
424 db->pkgcache = alpm_list_add(db->pkgcache, pkg);
425 count++;
428 closedir(dbdir);
429 db->pkgcache = alpm_list_msort(db->pkgcache, count, _alpm_pkg_cmp);
430 return(count);
433 /* Note: the return value must be freed by the caller */
434 static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
436 size_t len;
437 char *pkgpath;
438 const char *dbpath;
440 dbpath = _alpm_db_path(db);
441 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
442 MALLOC(pkgpath, len, RET_ERR(PM_ERR_MEMORY, NULL));
443 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
444 return(pkgpath);
448 int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
450 FILE *fp = NULL;
451 char path[PATH_MAX];
452 char line[1024];
453 char *pkgpath = NULL;
455 ALPM_LOG_FUNC;
457 if(db == NULL) {
458 RET_ERR(PM_ERR_DB_NULL, -1);
461 if(info == NULL || info->name == NULL || info->version == NULL) {
462 _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_local_db_read, skipping\n");
463 return(-1);
466 if(info->origin == PKG_FROM_FILE) {
467 _alpm_log(PM_LOG_DEBUG, "request to read database info for a file-based package '%s', skipping...\n", info->name);
468 return(-1);
471 /* bitmask logic here:
472 * infolevel: 00001111
473 * inforeq: 00010100
474 * & result: 00000100
475 * == to inforeq? nope, we need to load more info. */
476 if((info->infolevel & inforeq) == inforeq) {
477 /* already loaded this info, do nothing */
478 return(0);
480 _alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
481 info->name, inforeq);
483 /* clear out 'line', to be certain - and to make valgrind happy */
484 memset(line, 0, sizeof(line));
486 pkgpath = get_pkgpath(db, info);
488 if(access(pkgpath, F_OK)) {
489 /* directory doesn't exist or can't be opened */
490 _alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
491 info->name, info->version, db->treename);
492 goto error;
495 /* DESC */
496 if(inforeq & INFRQ_DESC) {
497 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
498 if((fp = fopen(path, "r")) == NULL) {
499 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
500 goto error;
502 while(!feof(fp)) {
503 if(fgets(line, sizeof(line), fp) == NULL) {
504 break;
506 _alpm_strtrim(line);
507 if(strcmp(line, "%NAME%") == 0) {
508 if(fgets(line, sizeof(line), fp) == NULL) {
509 goto error;
511 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
512 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
513 "mismatch on package %s\n"), db->treename, info->name);
515 } else if(strcmp(line, "%VERSION%") == 0) {
516 if(fgets(line, sizeof(line), fp) == NULL) {
517 goto error;
519 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
520 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
521 "mismatch on package %s\n"), db->treename, info->name);
523 } else if(strcmp(line, "%FILENAME%") == 0) {
524 if(fgets(line, sizeof(line), fp) == NULL) {
525 goto error;
527 STRDUP(info->filename, _alpm_strtrim(line), goto error);
528 } else if(strcmp(line, "%DESC%") == 0) {
529 if(fgets(line, sizeof(line), fp) == NULL) {
530 goto error;
532 STRDUP(info->desc, _alpm_strtrim(line), goto error);
533 } else if(strcmp(line, "%GROUPS%") == 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->groups = alpm_list_add(info->groups, linedup);
539 } else if(strcmp(line, "%URL%") == 0) {
540 if(fgets(line, sizeof(line), fp) == NULL) {
541 goto error;
543 STRDUP(info->url, _alpm_strtrim(line), goto error);
544 } else if(strcmp(line, "%LICENSE%") == 0) {
545 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
546 char *linedup;
547 STRDUP(linedup, _alpm_strtrim(line), goto error);
548 info->licenses = alpm_list_add(info->licenses, linedup);
550 } else if(strcmp(line, "%ARCH%") == 0) {
551 if(fgets(line, sizeof(line), fp) == NULL) {
552 goto error;
554 STRDUP(info->arch, _alpm_strtrim(line), goto error);
555 } else if(strcmp(line, "%BUILDDATE%") == 0) {
556 if(fgets(line, sizeof(line), fp) == NULL) {
557 goto error;
559 _alpm_strtrim(line);
561 char first = tolower((unsigned char)line[0]);
562 if(first > 'a' && first < 'z') {
563 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
564 setlocale(LC_TIME, "C");
565 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
566 info->builddate = mktime(&tmp_tm);
567 setlocale(LC_TIME, "");
568 } else {
569 info->builddate = atol(line);
571 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
572 if(fgets(line, sizeof(line), fp) == NULL) {
573 goto error;
575 _alpm_strtrim(line);
577 char first = tolower((unsigned char)line[0]);
578 if(first > 'a' && first < 'z') {
579 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
580 setlocale(LC_TIME, "C");
581 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
582 info->installdate = mktime(&tmp_tm);
583 setlocale(LC_TIME, "");
584 } else {
585 info->installdate = atol(line);
587 } else if(strcmp(line, "%PACKAGER%") == 0) {
588 if(fgets(line, sizeof(line), fp) == NULL) {
589 goto error;
591 STRDUP(info->packager, _alpm_strtrim(line), goto error);
592 } else if(strcmp(line, "%REASON%") == 0) {
593 if(fgets(line, sizeof(line), fp) == NULL) {
594 goto error;
596 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
597 } else if(strcmp(line, "%SIZE%") == 0 || strcmp(line, "%CSIZE%") == 0) {
598 /* NOTE: the CSIZE and SIZE fields both share the "size" field
599 * in the pkginfo_t struct. This can be done b/c CSIZE
600 * is currently only used in sync databases, and SIZE is
601 * only used in local databases.
603 if(fgets(line, sizeof(line), fp) == NULL) {
604 goto error;
606 info->size = atol(_alpm_strtrim(line));
607 /* also store this value to isize if isize is unset */
608 if(info->isize == 0) {
609 info->isize = info->size;
611 } else if(strcmp(line, "%ISIZE%") == 0) {
612 /* ISIZE (installed size) tag only appears in sync repositories,
613 * not the local one. */
614 if(fgets(line, sizeof(line), fp) == NULL) {
615 goto error;
617 info->isize = atol(_alpm_strtrim(line));
618 } else if(strcmp(line, "%MD5SUM%") == 0) {
619 /* MD5SUM tag only appears in sync repositories,
620 * not the local one. */
621 if(fgets(line, sizeof(line), fp) == NULL) {
622 goto error;
624 STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
625 } else if(strcmp(line, "%REPLACES%") == 0) {
626 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
627 char *linedup;
628 STRDUP(linedup, _alpm_strtrim(line), goto error);
629 info->replaces = alpm_list_add(info->replaces, linedup);
631 } else if(strcmp(line, "%FORCE%") == 0) {
632 info->force = 1;
635 fclose(fp);
636 fp = NULL;
639 /* FILES */
640 if(inforeq & INFRQ_FILES) {
641 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
642 if((fp = fopen(path, "r")) == NULL) {
643 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
644 goto error;
646 while(fgets(line, sizeof(line), fp)) {
647 _alpm_strtrim(line);
648 if(strcmp(line, "%FILES%") == 0) {
649 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
650 char *linedup;
651 STRDUP(linedup, _alpm_strtrim(line), goto error);
652 info->files = alpm_list_add(info->files, linedup);
654 } else if(strcmp(line, "%BACKUP%") == 0) {
655 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
656 char *linedup;
657 STRDUP(linedup, _alpm_strtrim(line), goto error);
658 info->backup = alpm_list_add(info->backup, linedup);
662 fclose(fp);
663 fp = NULL;
666 /* DEPENDS */
667 if(inforeq & INFRQ_DEPENDS) {
668 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
669 if((fp = fopen(path, "r")) == NULL) {
670 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
671 goto error;
673 while(!feof(fp)) {
674 if(fgets(line, sizeof(line), fp) == NULL) {
675 break;
677 _alpm_strtrim(line);
678 if(strcmp(line, "%DEPENDS%") == 0) {
679 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
680 pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
681 info->depends = alpm_list_add(info->depends, dep);
683 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
684 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
685 char *linedup;
686 STRDUP(linedup, _alpm_strtrim(line), goto error);
687 info->optdepends = alpm_list_add(info->optdepends, linedup);
689 } else if(strcmp(line, "%CONFLICTS%") == 0) {
690 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
691 char *linedup;
692 STRDUP(linedup, _alpm_strtrim(line), goto error);
693 info->conflicts = alpm_list_add(info->conflicts, linedup);
695 } else if(strcmp(line, "%PROVIDES%") == 0) {
696 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
697 char *linedup;
698 STRDUP(linedup, _alpm_strtrim(line), goto error);
699 info->provides = alpm_list_add(info->provides, linedup);
703 fclose(fp);
704 fp = NULL;
707 /* DELTAS */
708 if(inforeq & INFRQ_DELTAS) {
709 snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
710 if((fp = fopen(path, "r"))) {
711 while(!feof(fp)) {
712 if(fgets(line, sizeof(line), fp) == NULL) {
713 break;
715 _alpm_strtrim(line);
716 if(strcmp(line, "%DELTAS%") == 0) {
717 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
718 pmdelta_t *delta = _alpm_delta_parse(line);
719 if(delta) {
720 info->deltas = alpm_list_add(info->deltas, delta);
725 fclose(fp);
726 fp = NULL;
730 /* INSTALL */
731 if(inforeq & INFRQ_SCRIPTLET) {
732 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
733 if(access(path, F_OK) == 0) {
734 info->scriptlet = 1;
738 /* internal */
739 info->infolevel |= inforeq;
741 free(pkgpath);
742 return(0);
744 error:
745 free(pkgpath);
746 if(fp) {
747 fclose(fp);
749 return(-1);
752 int _alpm_local_db_prepare(pmdb_t *db, pmpkg_t *info)
754 mode_t oldmask;
755 int retval = 0;
756 char *pkgpath = NULL;
758 if(checkdbdir(db) != 0) {
759 return(-1);
762 oldmask = umask(0000);
763 pkgpath = get_pkgpath(db, info);
765 if((retval = mkdir(pkgpath, 0755)) != 0) {
766 _alpm_log(PM_LOG_ERROR, _("could not create directory %s: %s\n"),
767 pkgpath, strerror(errno));
770 free(pkgpath);
771 umask(oldmask);
773 return(retval);
776 int _alpm_local_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
778 FILE *fp = NULL;
779 char path[PATH_MAX];
780 mode_t oldmask;
781 alpm_list_t *lp = NULL;
782 int retval = 0;
783 int local = 0;
784 char *pkgpath = NULL;
786 ALPM_LOG_FUNC;
788 if(db == NULL || info == NULL) {
789 return(-1);
792 pkgpath = get_pkgpath(db, info);
794 /* make sure we have a sane umask */
795 oldmask = umask(0022);
797 if(strcmp(db->treename, "local") == 0) {
798 local = 1;
801 /* DESC */
802 if(inforeq & INFRQ_DESC) {
803 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
804 info->name, info->version);
805 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
806 if((fp = fopen(path, "w")) == NULL) {
807 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
808 retval = -1;
809 goto cleanup;
811 fprintf(fp, "%%NAME%%\n%s\n\n"
812 "%%VERSION%%\n%s\n\n", info->name, info->version);
813 if(info->desc) {
814 fprintf(fp, "%%DESC%%\n"
815 "%s\n\n", info->desc);
817 if(info->groups) {
818 fputs("%GROUPS%\n", fp);
819 for(lp = info->groups; lp; lp = lp->next) {
820 fprintf(fp, "%s\n", (char *)lp->data);
822 fprintf(fp, "\n");
824 if(info->replaces) {
825 fputs("%REPLACES%\n", fp);
826 for(lp = info->replaces; lp; lp = lp->next) {
827 fprintf(fp, "%s\n", (char *)lp->data);
829 fprintf(fp, "\n");
831 if(info->force) {
832 fprintf(fp, "%%FORCE%%\n\n");
834 if(local) {
835 if(info->url) {
836 fprintf(fp, "%%URL%%\n"
837 "%s\n\n", info->url);
839 if(info->licenses) {
840 fputs("%LICENSE%\n", fp);
841 for(lp = info->licenses; lp; lp = lp->next) {
842 fprintf(fp, "%s\n", (char *)lp->data);
844 fprintf(fp, "\n");
846 if(info->arch) {
847 fprintf(fp, "%%ARCH%%\n"
848 "%s\n\n", info->arch);
850 if(info->builddate) {
851 fprintf(fp, "%%BUILDDATE%%\n"
852 "%ld\n\n", info->builddate);
854 if(info->installdate) {
855 fprintf(fp, "%%INSTALLDATE%%\n"
856 "%ld\n\n", info->installdate);
858 if(info->packager) {
859 fprintf(fp, "%%PACKAGER%%\n"
860 "%s\n\n", info->packager);
862 if(info->isize) {
863 /* only write installed size, csize is irrelevant once installed */
864 fprintf(fp, "%%SIZE%%\n"
865 "%jd\n\n", (intmax_t)info->isize);
867 if(info->reason) {
868 fprintf(fp, "%%REASON%%\n"
869 "%u\n\n", info->reason);
871 } else {
872 if(info->size) {
873 fprintf(fp, "%%CSIZE%%\n"
874 "%jd\n\n", (intmax_t)info->size);
876 if(info->isize) {
877 fprintf(fp, "%%ISIZE%%\n"
878 "%jd\n\n", (intmax_t)info->isize);
880 if(info->md5sum) {
881 fprintf(fp, "%%MD5SUM%%\n"
882 "%s\n\n", info->md5sum);
885 fclose(fp);
886 fp = NULL;
889 /* FILES */
890 if(local && (inforeq & INFRQ_FILES)) {
891 _alpm_log(PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
892 info->name, info->version);
893 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
894 if((fp = fopen(path, "w")) == NULL) {
895 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
896 retval = -1;
897 goto cleanup;
899 if(info->files) {
900 fprintf(fp, "%%FILES%%\n");
901 for(lp = info->files; lp; lp = lp->next) {
902 fprintf(fp, "%s\n", (char *)lp->data);
904 fprintf(fp, "\n");
906 if(info->backup) {
907 fprintf(fp, "%%BACKUP%%\n");
908 for(lp = info->backup; lp; lp = lp->next) {
909 fprintf(fp, "%s\n", (char *)lp->data);
911 fprintf(fp, "\n");
913 fclose(fp);
914 fp = NULL;
917 /* DEPENDS */
918 if(inforeq & INFRQ_DEPENDS) {
919 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DEPENDS information back to db\n",
920 info->name, info->version);
921 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
922 if((fp = fopen(path, "w")) == NULL) {
923 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
924 retval = -1;
925 goto cleanup;
927 if(info->depends) {
928 fputs("%DEPENDS%\n", fp);
929 for(lp = info->depends; lp; lp = lp->next) {
930 char *depstring = alpm_dep_compute_string(lp->data);
931 fprintf(fp, "%s\n", depstring);
932 free(depstring);
934 fprintf(fp, "\n");
936 if(info->optdepends) {
937 fputs("%OPTDEPENDS%\n", fp);
938 for(lp = info->optdepends; lp; lp = lp->next) {
939 fprintf(fp, "%s\n", (char *)lp->data);
941 fprintf(fp, "\n");
943 if(info->conflicts) {
944 fputs("%CONFLICTS%\n", fp);
945 for(lp = info->conflicts; lp; lp = lp->next) {
946 fprintf(fp, "%s\n", (char *)lp->data);
948 fprintf(fp, "\n");
950 if(info->provides) {
951 fputs("%PROVIDES%\n", fp);
952 for(lp = info->provides; lp; lp = lp->next) {
953 fprintf(fp, "%s\n", (char *)lp->data);
955 fprintf(fp, "\n");
957 fclose(fp);
958 fp = NULL;
961 /* INSTALL */
962 /* nothing needed here (script is automatically extracted) */
964 cleanup:
965 umask(oldmask);
966 free(pkgpath);
968 if(fp) {
969 fclose(fp);
972 return(retval);
975 int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
977 int ret = 0;
978 char *pkgpath = NULL;
980 ALPM_LOG_FUNC;
982 if(db == NULL || info == NULL) {
983 RET_ERR(PM_ERR_DB_NULL, -1);
986 pkgpath = get_pkgpath(db, info);
988 ret = _alpm_rmrf(pkgpath);
989 free(pkgpath);
990 if(ret != 0) {
991 ret = -1;
993 return(ret);
996 struct db_operations local_db_ops = {
997 .populate = _alpm_local_db_populate,
998 .unregister = _alpm_db_unregister,
1001 pmdb_t *_alpm_db_register_local(void)
1003 pmdb_t *db;
1005 ALPM_LOG_FUNC;
1007 if(handle->db_local != NULL) {
1008 _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n"));
1009 RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
1012 _alpm_log(PM_LOG_DEBUG, "registering local database\n");
1014 db = _alpm_db_new("local", 1);
1015 db->ops = &local_db_ops;
1016 if(db == NULL) {
1017 RET_ERR(PM_ERR_DB_CREATE, NULL);
1020 handle->db_local = db;
1021 return(db);
1025 /* vim: set ts=2 sw=2 noet: */