Only write to local repos
[pacman-ng.git] / lib / libalpm / be_local.c
blobcd6fac40eaec366482145d6d70e8050a9778274b
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 ASSERT(pkg != NULL, return(NULL));
200 /* local pkgs do not have deltas so nothing to load */
201 return pkg->deltas;
204 alpm_list_t *_cache_get_files(pmpkg_t *pkg)
206 ALPM_LOG_FUNC;
208 /* Sanity checks */
209 ASSERT(handle != NULL, return(NULL));
210 ASSERT(pkg != NULL, return(NULL));
212 if(pkg->origin == PKG_FROM_LOCALDB
213 && !(pkg->infolevel & INFRQ_FILES)) {
214 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
216 return pkg->files;
219 alpm_list_t *_cache_get_backup(pmpkg_t *pkg)
221 ALPM_LOG_FUNC;
223 /* Sanity checks */
224 ASSERT(handle != NULL, return(NULL));
225 ASSERT(pkg != NULL, return(NULL));
227 if(pkg->origin == PKG_FROM_LOCALDB
228 && !(pkg->infolevel & INFRQ_FILES)) {
229 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
231 return pkg->backup;
235 * Open a package changelog for reading. Similar to fopen in functionality,
236 * except that the returned 'file stream' is from the database.
237 * @param pkg the package (from db) to read the changelog
238 * @return a 'file stream' to the package changelog
240 void *_cache_changelog_open(pmpkg_t *pkg)
242 ALPM_LOG_FUNC;
244 /* Sanity checks */
245 ASSERT(handle != NULL, return(NULL));
246 ASSERT(pkg != NULL, return(NULL));
248 char clfile[PATH_MAX];
249 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
250 alpm_option_get_dbpath(),
251 alpm_db_get_name(alpm_pkg_get_db(pkg)),
252 alpm_pkg_get_name(pkg),
253 alpm_pkg_get_version(pkg));
254 return fopen(clfile, "r");
258 * Read data from an open changelog 'file stream'. Similar to fread in
259 * functionality, this function takes a buffer and amount of data to read.
260 * @param ptr a buffer to fill with raw changelog data
261 * @param size the size of the buffer
262 * @param pkg the package that the changelog is being read from
263 * @param fp a 'file stream' to the package changelog
264 * @return the number of characters read, or 0 if there is no more data
266 size_t _cache_changelog_read(void *ptr, size_t size,
267 const pmpkg_t *pkg, const void *fp)
269 return ( fread(ptr, 1, size, (FILE*)fp) );
273 int _cache_changelog_feof(const pmpkg_t *pkg, void *fp)
275 return( feof((FILE*)fp) );
280 * Close a package changelog for reading. Similar to fclose in functionality,
281 * except that the 'file stream' is from the database.
282 * @param pkg the package that the changelog was read from
283 * @param fp a 'file stream' to the package changelog
284 * @return whether closing the package changelog stream was successful
286 int _cache_changelog_close(const pmpkg_t *pkg, void *fp)
288 return( fclose((FILE*)fp) );
291 /** The local database operations struct. Get package fields through
292 * lazy accessor methods that handle any backend loading and caching
293 * logic.
295 static struct pkg_operations local_pkg_ops = {
296 .get_filename = _cache_get_filename,
297 .get_name = _cache_get_name,
298 .get_version = _cache_get_version,
299 .get_desc = _cache_get_desc,
300 .get_url = _cache_get_url,
301 .get_builddate = _cache_get_builddate,
302 .get_installdate = _cache_get_installdate,
303 .get_packager = _cache_get_packager,
304 .get_md5sum = _cache_get_md5sum,
305 .get_arch = _cache_get_arch,
306 .get_size = _cache_get_size,
307 .get_isize = _cache_get_isize,
308 .get_reason = _cache_get_reason,
309 .has_force = _cache_has_force,
310 .get_licenses = _cache_get_licenses,
311 .get_groups = _cache_get_groups,
312 .get_depends = _cache_get_depends,
313 .get_optdepends = _cache_get_optdepends,
314 .get_conflicts = _cache_get_conflicts,
315 .get_provides = _cache_get_provides,
316 .get_replaces = _cache_get_replaces,
317 .get_deltas = _cache_get_deltas,
318 .get_files = _cache_get_files,
319 .get_backup = _cache_get_backup,
321 .changelog_open = _cache_changelog_open,
322 .changelog_read = _cache_changelog_read,
323 .changelog_close = _cache_changelog_close,
326 static int checkdbdir(pmdb_t *db)
328 struct stat buf;
329 const char *path = _alpm_db_path(db);
331 if(stat(path, &buf) != 0) {
332 _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
333 path);
334 if(_alpm_makepath(path) != 0) {
335 RET_ERR(PM_ERR_SYSTEM, -1);
337 } else if(!S_ISDIR(buf.st_mode)) {
338 _alpm_log(PM_LOG_WARNING, _("removing invalid database: %s\n"), path);
339 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
340 RET_ERR(PM_ERR_SYSTEM, -1);
343 return(0);
346 static int is_dir(const char *path, struct dirent *entry)
348 #ifdef DT_DIR
349 return(entry->d_type == DT_DIR);
350 #else
351 char buffer[PATH_MAX];
352 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
354 struct stat sbuf;
355 if (!stat(buffer, &sbuf)) {
356 return(S_ISDIR(sbuf.st_mode));
359 return(0);
360 #endif
363 int _alpm_local_db_populate(pmdb_t *db)
365 int count = 0;
366 struct dirent *ent = NULL;
367 const char *dbpath;
368 DIR *dbdir;
370 ALPM_LOG_FUNC;
372 ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
374 dbpath = _alpm_db_path(db);
375 dbdir = opendir(dbpath);
376 if(dbdir == NULL) {
377 return(0);
379 while((ent = readdir(dbdir)) != NULL) {
380 const char *name = ent->d_name;
382 pmpkg_t *pkg;
384 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
385 continue;
387 if(!is_dir(dbpath, ent)) {
388 continue;
391 pkg = _alpm_pkg_new();
392 if(pkg == NULL) {
393 closedir(dbdir);
394 return(-1);
396 /* split the db entry name */
397 if(_alpm_splitname(name, pkg) != 0) {
398 _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
399 name);
400 _alpm_pkg_free(pkg);
401 continue;
404 /* duplicated database entries are not allowed */
405 if(_alpm_pkg_find(db->pkgcache, pkg->name)) {
406 _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
407 _alpm_pkg_free(pkg);
408 continue;
411 /* explicitly read with only 'BASE' data, accessors will handle the rest */
412 if(_alpm_local_db_read(db, pkg, INFRQ_BASE) == -1) {
413 _alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
414 _alpm_pkg_free(pkg);
415 continue;
418 pkg->origin = PKG_FROM_LOCALDB;
419 pkg->ops = &local_pkg_ops;
421 pkg->origin_data.db = db;
422 /* add to the collection */
423 _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
424 pkg->name, db->treename);
425 db->pkgcache = alpm_list_add(db->pkgcache, pkg);
426 count++;
429 closedir(dbdir);
430 db->pkgcache = alpm_list_msort(db->pkgcache, count, _alpm_pkg_cmp);
431 return(count);
434 /* Note: the return value must be freed by the caller */
435 static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
437 size_t len;
438 char *pkgpath;
439 const char *dbpath;
441 dbpath = _alpm_db_path(db);
442 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
443 MALLOC(pkgpath, len, RET_ERR(PM_ERR_MEMORY, NULL));
444 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
445 return(pkgpath);
449 int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
451 FILE *fp = NULL;
452 char path[PATH_MAX];
453 char line[1024];
454 char *pkgpath = NULL;
456 ALPM_LOG_FUNC;
458 if(db == NULL) {
459 RET_ERR(PM_ERR_DB_NULL, -1);
462 if(info == NULL || info->name == NULL || info->version == NULL) {
463 _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_local_db_read, skipping\n");
464 return(-1);
467 if(info->origin == PKG_FROM_FILE) {
468 _alpm_log(PM_LOG_DEBUG, "request to read database info for a file-based package '%s', skipping...\n", info->name);
469 return(-1);
472 /* bitmask logic here:
473 * infolevel: 00001111
474 * inforeq: 00010100
475 * & result: 00000100
476 * == to inforeq? nope, we need to load more info. */
477 if((info->infolevel & inforeq) == inforeq) {
478 /* already loaded this info, do nothing */
479 return(0);
481 _alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
482 info->name, inforeq);
484 /* clear out 'line', to be certain - and to make valgrind happy */
485 memset(line, 0, sizeof(line));
487 pkgpath = get_pkgpath(db, info);
489 if(access(pkgpath, F_OK)) {
490 /* directory doesn't exist or can't be opened */
491 _alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
492 info->name, info->version, db->treename);
493 goto error;
496 /* DESC */
497 if(inforeq & INFRQ_DESC) {
498 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
499 if((fp = fopen(path, "r")) == NULL) {
500 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
501 goto error;
503 while(!feof(fp)) {
504 if(fgets(line, sizeof(line), fp) == NULL) {
505 break;
507 _alpm_strtrim(line);
508 if(strcmp(line, "%NAME%") == 0) {
509 if(fgets(line, sizeof(line), fp) == NULL) {
510 goto error;
512 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
513 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
514 "mismatch on package %s\n"), db->treename, info->name);
516 } else if(strcmp(line, "%VERSION%") == 0) {
517 if(fgets(line, sizeof(line), fp) == NULL) {
518 goto error;
520 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
521 _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
522 "mismatch on package %s\n"), db->treename, info->name);
524 } else if(strcmp(line, "%DESC%") == 0) {
525 if(fgets(line, sizeof(line), fp) == NULL) {
526 goto error;
528 STRDUP(info->desc, _alpm_strtrim(line), goto error);
529 } else if(strcmp(line, "%GROUPS%") == 0) {
530 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
531 char *linedup;
532 STRDUP(linedup, _alpm_strtrim(line), goto error);
533 info->groups = alpm_list_add(info->groups, linedup);
535 } else if(strcmp(line, "%URL%") == 0) {
536 if(fgets(line, sizeof(line), fp) == NULL) {
537 goto error;
539 STRDUP(info->url, _alpm_strtrim(line), goto error);
540 } else if(strcmp(line, "%LICENSE%") == 0) {
541 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
542 char *linedup;
543 STRDUP(linedup, _alpm_strtrim(line), goto error);
544 info->licenses = alpm_list_add(info->licenses, linedup);
546 } else if(strcmp(line, "%ARCH%") == 0) {
547 if(fgets(line, sizeof(line), fp) == NULL) {
548 goto error;
550 STRDUP(info->arch, _alpm_strtrim(line), goto error);
551 } else if(strcmp(line, "%BUILDDATE%") == 0) {
552 if(fgets(line, sizeof(line), fp) == NULL) {
553 goto error;
555 _alpm_strtrim(line);
557 char first = tolower((unsigned char)line[0]);
558 if(first > 'a' && first < 'z') {
559 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
560 setlocale(LC_TIME, "C");
561 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
562 info->builddate = mktime(&tmp_tm);
563 setlocale(LC_TIME, "");
564 } else {
565 info->builddate = atol(line);
567 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
568 if(fgets(line, sizeof(line), fp) == NULL) {
569 goto error;
571 _alpm_strtrim(line);
573 char first = tolower((unsigned char)line[0]);
574 if(first > 'a' && first < 'z') {
575 struct tm tmp_tm = {0}; /* initialize to null in case of failure */
576 setlocale(LC_TIME, "C");
577 strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
578 info->installdate = mktime(&tmp_tm);
579 setlocale(LC_TIME, "");
580 } else {
581 info->installdate = atol(line);
583 } else if(strcmp(line, "%PACKAGER%") == 0) {
584 if(fgets(line, sizeof(line), fp) == NULL) {
585 goto error;
587 STRDUP(info->packager, _alpm_strtrim(line), goto error);
588 } else if(strcmp(line, "%REASON%") == 0) {
589 if(fgets(line, sizeof(line), fp) == NULL) {
590 goto error;
592 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
593 } else if(strcmp(line, "%SIZE%") == 0) {
594 /* NOTE: the CSIZE and SIZE fields both share the "size" field
595 * in the pkginfo_t struct. This can be done b/c CSIZE
596 * is currently only used in sync databases, and SIZE is
597 * only used in local databases.
599 if(fgets(line, sizeof(line), fp) == NULL) {
600 goto error;
602 info->size = atol(_alpm_strtrim(line));
603 /* also store this value to isize */
604 info->isize = info->size;
605 } else if(strcmp(line, "%REPLACES%") == 0) {
606 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
607 char *linedup;
608 STRDUP(linedup, _alpm_strtrim(line), goto error);
609 info->replaces = alpm_list_add(info->replaces, linedup);
611 } else if(strcmp(line, "%FORCE%") == 0) {
612 info->force = 1;
615 fclose(fp);
616 fp = NULL;
619 /* FILES */
620 if(inforeq & INFRQ_FILES) {
621 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
622 if((fp = fopen(path, "r")) == NULL) {
623 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
624 goto error;
626 while(fgets(line, sizeof(line), fp)) {
627 _alpm_strtrim(line);
628 if(strcmp(line, "%FILES%") == 0) {
629 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
630 char *linedup;
631 STRDUP(linedup, _alpm_strtrim(line), goto error);
632 info->files = alpm_list_add(info->files, linedup);
634 } else if(strcmp(line, "%BACKUP%") == 0) {
635 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
636 char *linedup;
637 STRDUP(linedup, _alpm_strtrim(line), goto error);
638 info->backup = alpm_list_add(info->backup, linedup);
642 fclose(fp);
643 fp = NULL;
646 /* DEPENDS */
647 if(inforeq & INFRQ_DEPENDS) {
648 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
649 if((fp = fopen(path, "r")) == NULL) {
650 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
651 goto error;
653 while(!feof(fp)) {
654 if(fgets(line, sizeof(line), fp) == NULL) {
655 break;
657 _alpm_strtrim(line);
658 if(strcmp(line, "%DEPENDS%") == 0) {
659 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
660 pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
661 info->depends = alpm_list_add(info->depends, dep);
663 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
664 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
665 char *linedup;
666 STRDUP(linedup, _alpm_strtrim(line), goto error);
667 info->optdepends = alpm_list_add(info->optdepends, linedup);
669 } else if(strcmp(line, "%CONFLICTS%") == 0) {
670 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
671 char *linedup;
672 STRDUP(linedup, _alpm_strtrim(line), goto error);
673 info->conflicts = alpm_list_add(info->conflicts, linedup);
675 } else if(strcmp(line, "%PROVIDES%") == 0) {
676 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
677 char *linedup;
678 STRDUP(linedup, _alpm_strtrim(line), goto error);
679 info->provides = alpm_list_add(info->provides, linedup);
683 fclose(fp);
684 fp = NULL;
687 /* INSTALL */
688 if(inforeq & INFRQ_SCRIPTLET) {
689 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
690 if(access(path, F_OK) == 0) {
691 info->scriptlet = 1;
695 /* internal */
696 info->infolevel |= inforeq;
698 free(pkgpath);
699 return(0);
701 error:
702 free(pkgpath);
703 if(fp) {
704 fclose(fp);
706 return(-1);
709 int _alpm_local_db_prepare(pmdb_t *db, pmpkg_t *info)
711 mode_t oldmask;
712 int retval = 0;
713 char *pkgpath = NULL;
715 if(checkdbdir(db) != 0) {
716 return(-1);
719 oldmask = umask(0000);
720 pkgpath = get_pkgpath(db, info);
722 if((retval = mkdir(pkgpath, 0755)) != 0) {
723 _alpm_log(PM_LOG_ERROR, _("could not create directory %s: %s\n"),
724 pkgpath, strerror(errno));
727 free(pkgpath);
728 umask(oldmask);
730 return(retval);
733 int _alpm_local_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
735 FILE *fp = NULL;
736 char path[PATH_MAX];
737 mode_t oldmask;
738 alpm_list_t *lp = NULL;
739 int retval = 0;
740 char *pkgpath = NULL;
742 ALPM_LOG_FUNC;
744 if(db == NULL || info == NULL) {
745 return(-1);
748 pkgpath = get_pkgpath(db, info);
750 /* make sure we have a sane umask */
751 oldmask = umask(0022);
753 if(strcmp(db->treename, "local") != 0) {
754 return(-1);
757 /* DESC */
758 if(inforeq & INFRQ_DESC) {
759 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
760 info->name, info->version);
761 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
762 if((fp = fopen(path, "w")) == NULL) {
763 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
764 retval = -1;
765 goto cleanup;
767 fprintf(fp, "%%NAME%%\n%s\n\n"
768 "%%VERSION%%\n%s\n\n", info->name, info->version);
769 if(info->desc) {
770 fprintf(fp, "%%DESC%%\n"
771 "%s\n\n", info->desc);
773 if(info->groups) {
774 fputs("%GROUPS%\n", fp);
775 for(lp = info->groups; lp; lp = lp->next) {
776 fprintf(fp, "%s\n", (char *)lp->data);
778 fprintf(fp, "\n");
780 if(info->replaces) {
781 fputs("%REPLACES%\n", fp);
782 for(lp = info->replaces; lp; lp = lp->next) {
783 fprintf(fp, "%s\n", (char *)lp->data);
785 fprintf(fp, "\n");
787 if(info->force) {
788 fprintf(fp, "%%FORCE%%\n\n");
790 if(info->url) {
791 fprintf(fp, "%%URL%%\n"
792 "%s\n\n", info->url);
794 if(info->licenses) {
795 fputs("%LICENSE%\n", fp);
796 for(lp = info->licenses; lp; lp = lp->next) {
797 fprintf(fp, "%s\n", (char *)lp->data);
799 fprintf(fp, "\n");
801 if(info->arch) {
802 fprintf(fp, "%%ARCH%%\n"
803 "%s\n\n", info->arch);
805 if(info->builddate) {
806 fprintf(fp, "%%BUILDDATE%%\n"
807 "%ld\n\n", info->builddate);
809 if(info->installdate) {
810 fprintf(fp, "%%INSTALLDATE%%\n"
811 "%ld\n\n", info->installdate);
813 if(info->packager) {
814 fprintf(fp, "%%PACKAGER%%\n"
815 "%s\n\n", info->packager);
817 if(info->isize) {
818 /* only write installed size, csize is irrelevant once installed */
819 fprintf(fp, "%%SIZE%%\n"
820 "%jd\n\n", (intmax_t)info->isize);
822 if(info->reason) {
823 fprintf(fp, "%%REASON%%\n"
824 "%u\n\n", info->reason);
827 fclose(fp);
828 fp = NULL;
831 /* FILES */
832 if(inforeq & INFRQ_FILES) {
833 _alpm_log(PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
834 info->name, info->version);
835 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
836 if((fp = fopen(path, "w")) == NULL) {
837 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
838 retval = -1;
839 goto cleanup;
841 if(info->files) {
842 fprintf(fp, "%%FILES%%\n");
843 for(lp = info->files; lp; lp = lp->next) {
844 fprintf(fp, "%s\n", (char *)lp->data);
846 fprintf(fp, "\n");
848 if(info->backup) {
849 fprintf(fp, "%%BACKUP%%\n");
850 for(lp = info->backup; lp; lp = lp->next) {
851 fprintf(fp, "%s\n", (char *)lp->data);
853 fprintf(fp, "\n");
855 fclose(fp);
856 fp = NULL;
859 /* DEPENDS */
860 if(inforeq & INFRQ_DEPENDS) {
861 _alpm_log(PM_LOG_DEBUG, "writing %s-%s DEPENDS information back to db\n",
862 info->name, info->version);
863 snprintf(path, PATH_MAX, "%sdepends", pkgpath);
864 if((fp = fopen(path, "w")) == NULL) {
865 _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
866 retval = -1;
867 goto cleanup;
869 if(info->depends) {
870 fputs("%DEPENDS%\n", fp);
871 for(lp = info->depends; lp; lp = lp->next) {
872 char *depstring = alpm_dep_compute_string(lp->data);
873 fprintf(fp, "%s\n", depstring);
874 free(depstring);
876 fprintf(fp, "\n");
878 if(info->optdepends) {
879 fputs("%OPTDEPENDS%\n", fp);
880 for(lp = info->optdepends; lp; lp = lp->next) {
881 fprintf(fp, "%s\n", (char *)lp->data);
883 fprintf(fp, "\n");
885 if(info->conflicts) {
886 fputs("%CONFLICTS%\n", fp);
887 for(lp = info->conflicts; lp; lp = lp->next) {
888 fprintf(fp, "%s\n", (char *)lp->data);
890 fprintf(fp, "\n");
892 if(info->provides) {
893 fputs("%PROVIDES%\n", fp);
894 for(lp = info->provides; lp; lp = lp->next) {
895 fprintf(fp, "%s\n", (char *)lp->data);
897 fprintf(fp, "\n");
899 fclose(fp);
900 fp = NULL;
903 /* INSTALL */
904 /* nothing needed here (script is automatically extracted) */
906 cleanup:
907 umask(oldmask);
908 free(pkgpath);
910 if(fp) {
911 fclose(fp);
914 return(retval);
917 int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
919 int ret = 0;
920 char *pkgpath = NULL;
922 ALPM_LOG_FUNC;
924 if(db == NULL || info == NULL) {
925 RET_ERR(PM_ERR_DB_NULL, -1);
928 pkgpath = get_pkgpath(db, info);
930 ret = _alpm_rmrf(pkgpath);
931 free(pkgpath);
932 if(ret != 0) {
933 ret = -1;
935 return(ret);
938 struct db_operations local_db_ops = {
939 .populate = _alpm_local_db_populate,
940 .unregister = _alpm_db_unregister,
943 pmdb_t *_alpm_db_register_local(void)
945 pmdb_t *db;
947 ALPM_LOG_FUNC;
949 if(handle->db_local != NULL) {
950 _alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n"));
951 RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
954 _alpm_log(PM_LOG_DEBUG, "registering local database\n");
956 db = _alpm_db_new("local", 1);
957 db->ops = &local_db_ops;
958 if(db == NULL) {
959 RET_ERR(PM_ERR_DB_CREATE, NULL);
962 handle->db_local = db;
963 return(db);
967 /* vim: set ts=2 sw=2 noet: */