replace access() calls for debug info where applicable
[pacman-ng.git] / lib / libalpm / be_local.c
blob70f242d2f34700fead1b7ea311ce78d1d0daf156
1 /*
2 * be_local.c : backend for the local database
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"
44 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
46 #define LAZY_LOAD(info, errret) \
47 do { \
48 if(!(pkg->infolevel & info)) { \
49 local_db_read(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(alpm_pkg_t *pkg)
62 LAZY_LOAD(INFRQ_DESC, NULL);
63 return pkg->filename;
66 static const char *_cache_get_desc(alpm_pkg_t *pkg)
68 LAZY_LOAD(INFRQ_DESC, NULL);
69 return pkg->desc;
72 static const char *_cache_get_url(alpm_pkg_t *pkg)
74 LAZY_LOAD(INFRQ_DESC, NULL);
75 return pkg->url;
78 static time_t _cache_get_builddate(alpm_pkg_t *pkg)
80 LAZY_LOAD(INFRQ_DESC, 0);
81 return pkg->builddate;
84 static time_t _cache_get_installdate(alpm_pkg_t *pkg)
86 LAZY_LOAD(INFRQ_DESC, 0);
87 return pkg->installdate;
90 static const char *_cache_get_packager(alpm_pkg_t *pkg)
92 LAZY_LOAD(INFRQ_DESC, NULL);
93 return pkg->packager;
96 static const char *_cache_get_md5sum(alpm_pkg_t *pkg)
98 LAZY_LOAD(INFRQ_DESC, NULL);
99 return pkg->md5sum;
102 static const char *_cache_get_arch(alpm_pkg_t *pkg)
104 LAZY_LOAD(INFRQ_DESC, NULL);
105 return pkg->arch;
108 static off_t _cache_get_size(alpm_pkg_t *pkg)
110 LAZY_LOAD(INFRQ_DESC, -1);
111 return pkg->size;
114 static off_t _cache_get_isize(alpm_pkg_t *pkg)
116 LAZY_LOAD(INFRQ_DESC, -1);
117 return pkg->isize;
120 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
122 LAZY_LOAD(INFRQ_DESC, -1);
123 return pkg->reason;
126 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
128 LAZY_LOAD(INFRQ_DESC, NULL);
129 return pkg->licenses;
132 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
134 LAZY_LOAD(INFRQ_DESC, NULL);
135 return pkg->groups;
138 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
140 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
141 return pkg->scriptlet;
144 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
146 LAZY_LOAD(INFRQ_DESC, NULL);
147 return pkg->depends;
150 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
152 LAZY_LOAD(INFRQ_DESC, NULL);
153 return pkg->optdepends;
156 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
158 LAZY_LOAD(INFRQ_DESC, NULL);
159 return pkg->conflicts;
162 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
164 LAZY_LOAD(INFRQ_DESC, NULL);
165 return pkg->provides;
168 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
170 LAZY_LOAD(INFRQ_DESC, NULL);
171 return pkg->replaces;
174 /* local packages can not have deltas */
175 static alpm_list_t *_cache_get_deltas(alpm_pkg_t UNUSED *pkg)
177 return NULL;
180 static alpm_list_t *_cache_get_files(alpm_pkg_t *pkg)
182 LAZY_LOAD(INFRQ_FILES, NULL);
183 return pkg->files;
186 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
188 LAZY_LOAD(INFRQ_FILES, NULL);
189 return pkg->backup;
193 * Open a package changelog for reading. Similar to fopen in functionality,
194 * except that the returned 'file stream' is from the database.
195 * @param pkg the package (from db) to read the changelog
196 * @return a 'file stream' to the package changelog
198 static void *_cache_changelog_open(alpm_pkg_t *pkg)
200 char clfile[PATH_MAX];
201 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
202 alpm_option_get_dbpath(pkg->handle),
203 alpm_db_get_name(alpm_pkg_get_db(pkg)),
204 alpm_pkg_get_name(pkg),
205 alpm_pkg_get_version(pkg));
206 return fopen(clfile, "r");
210 * Read data from an open changelog 'file stream'. Similar to fread in
211 * functionality, this function takes a buffer and amount of data to read.
212 * @param ptr a buffer to fill with raw changelog data
213 * @param size the size of the buffer
214 * @param pkg the package that the changelog is being read from
215 * @param fp a 'file stream' to the package changelog
216 * @return the number of characters read, or 0 if there is no more data
218 static size_t _cache_changelog_read(void *ptr, size_t size,
219 const alpm_pkg_t UNUSED *pkg, const void *fp)
221 return fread(ptr, 1, size, (FILE *)fp);
225 * Close a package changelog for reading. Similar to fclose in functionality,
226 * except that the 'file stream' is from the database.
227 * @param pkg the package that the changelog was read from
228 * @param fp a 'file stream' to the package changelog
229 * @return whether closing the package changelog stream was successful
231 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
233 return fclose((FILE *)fp);
236 static int _cache_force_load(alpm_pkg_t *pkg)
238 return local_db_read(pkg, INFRQ_ALL);
242 /** The local database operations struct. Get package fields through
243 * lazy accessor methods that handle any backend loading and caching
244 * logic.
246 static struct pkg_operations local_pkg_ops = {
247 .get_filename = _cache_get_filename,
248 .get_desc = _cache_get_desc,
249 .get_url = _cache_get_url,
250 .get_builddate = _cache_get_builddate,
251 .get_installdate = _cache_get_installdate,
252 .get_packager = _cache_get_packager,
253 .get_md5sum = _cache_get_md5sum,
254 .get_arch = _cache_get_arch,
255 .get_size = _cache_get_size,
256 .get_isize = _cache_get_isize,
257 .get_reason = _cache_get_reason,
258 .has_scriptlet = _cache_has_scriptlet,
259 .get_licenses = _cache_get_licenses,
260 .get_groups = _cache_get_groups,
261 .get_depends = _cache_get_depends,
262 .get_optdepends = _cache_get_optdepends,
263 .get_conflicts = _cache_get_conflicts,
264 .get_provides = _cache_get_provides,
265 .get_replaces = _cache_get_replaces,
266 .get_deltas = _cache_get_deltas,
267 .get_files = _cache_get_files,
268 .get_backup = _cache_get_backup,
270 .changelog_open = _cache_changelog_open,
271 .changelog_read = _cache_changelog_read,
272 .changelog_close = _cache_changelog_close,
274 .force_load = _cache_force_load,
277 static int checkdbdir(alpm_db_t *db)
279 struct stat buf;
280 const char *path = _alpm_db_path(db);
282 if(stat(path, &buf) != 0) {
283 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
284 path);
285 if(_alpm_makepath(path) != 0) {
286 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
288 } else if(!S_ISDIR(buf.st_mode)) {
289 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
290 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
291 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
294 return 0;
297 static int is_dir(const char *path, struct dirent *entry)
299 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
300 if(entry->d_type != DT_UNKNOWN) {
301 return (entry->d_type == DT_DIR);
303 #endif
305 char buffer[PATH_MAX];
306 struct stat sbuf;
308 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
310 if(!stat(buffer, &sbuf)) {
311 return S_ISDIR(sbuf.st_mode);
315 return 0;
318 static int local_db_validate(alpm_db_t *db)
320 struct dirent *ent = NULL;
321 const char *dbpath;
322 DIR *dbdir;
323 int ret = -1;
325 if(db->status & DB_STATUS_VALID) {
326 return 0;
329 dbpath = _alpm_db_path(db);
330 if(dbpath == NULL) {
331 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
333 dbdir = opendir(dbpath);
334 if(dbdir == NULL) {
335 if(errno == ENOENT) {
336 /* database dir doesn't exist yet */
337 db->status |= DB_STATUS_VALID;
338 return 0;
339 } else {
340 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
344 while((ent = readdir(dbdir)) != NULL) {
345 const char *name = ent->d_name;
346 char path[PATH_MAX];
348 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
349 continue;
351 if(!is_dir(dbpath, ent)) {
352 continue;
355 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
356 if(access(path, F_OK) == 0) {
357 /* we found a depends file- bail */
358 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
359 goto done;
362 /* we found no depends file after full scan */
363 db->status |= DB_STATUS_VALID;
364 ret = 0;
366 done:
367 if(dbdir) {
368 closedir(dbdir);
371 return ret;
374 static int local_db_populate(alpm_db_t *db)
376 size_t est_count;
377 int count = 0;
378 struct stat buf;
379 struct dirent *ent = NULL;
380 const char *dbpath;
381 DIR *dbdir;
383 dbpath = _alpm_db_path(db);
384 if(dbpath == NULL) {
385 /* pm_errno set in _alpm_db_path() */
386 return -1;
389 dbdir = opendir(dbpath);
390 if(dbdir == NULL) {
391 if(errno == ENOENT) {
392 /* no database existing yet is not an error */
393 return 0;
395 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
397 if(fstat(dirfd(dbdir), &buf) != 0) {
398 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
400 if(buf.st_nlink >= 2) {
401 est_count = buf.st_nlink;
402 } else {
403 /* Some filesystems don't subscribe to the two-implicit links school of
404 * thought, e.g. BTRFS, HFS+. See
405 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
407 est_count = 0;
408 while(readdir(dbdir) != NULL) {
409 est_count++;
411 rewinddir(dbdir);
413 if(est_count >= 2) {
414 /* subtract the two extra pointers to get # of children */
415 est_count -= 2;
418 /* initialize hash at 50% full */
419 db->pkgcache = _alpm_pkghash_create(est_count * 2);
420 if(db->pkgcache == NULL){
421 closedir(dbdir);
422 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
425 while((ent = readdir(dbdir)) != NULL) {
426 const char *name = ent->d_name;
428 alpm_pkg_t *pkg;
430 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
431 continue;
433 if(!is_dir(dbpath, ent)) {
434 continue;
437 pkg = _alpm_pkg_new();
438 if(pkg == NULL) {
439 closedir(dbdir);
440 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
442 /* split the db entry name */
443 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
444 &(pkg->name_hash)) != 0) {
445 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
446 name);
447 _alpm_pkg_free(pkg);
448 continue;
451 /* duplicated database entries are not allowed */
452 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
453 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
454 _alpm_pkg_free(pkg);
455 continue;
458 pkg->origin = PKG_FROM_LOCALDB;
459 pkg->origin_data.db = db;
460 pkg->ops = &local_pkg_ops;
461 pkg->handle = db->handle;
463 /* explicitly read with only 'BASE' data, accessors will handle the rest */
464 if(local_db_read(pkg, INFRQ_BASE) == -1) {
465 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
466 _alpm_pkg_free(pkg);
467 continue;
470 /* add to the collection */
471 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
472 pkg->name, db->treename);
473 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
474 count++;
477 closedir(dbdir);
478 if(count > 0) {
479 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
481 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
482 count, db->treename);
484 return count;
487 /* Note: the return value must be freed by the caller */
488 static char *get_pkgpath(alpm_db_t *db, alpm_pkg_t *info)
490 size_t len;
491 char *pkgpath;
492 const char *dbpath;
494 dbpath = _alpm_db_path(db);
495 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
496 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
497 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
498 return pkgpath;
501 #define READ_NEXT() do { \
502 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
503 _alpm_strtrim(line); \
504 } while(0)
506 #define READ_AND_STORE(f) do { \
507 READ_NEXT(); \
508 STRDUP(f, line, goto error); \
509 } while(0)
511 #define READ_AND_STORE_ALL(f) do { \
512 char *linedup; \
513 READ_NEXT(); \
514 if(strlen(line) == 0) break; \
515 STRDUP(linedup, line, goto error); \
516 f = alpm_list_add(f, linedup); \
517 } while(1) /* note the while(1) and not (0) */
519 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
521 FILE *fp = NULL;
522 char path[PATH_MAX];
523 char line[1024];
524 char *pkgpath = NULL;
525 alpm_db_t *db = info->origin_data.db;
527 /* bitmask logic here:
528 * infolevel: 00001111
529 * inforeq: 00010100
530 * & result: 00000100
531 * == to inforeq? nope, we need to load more info. */
532 if((info->infolevel & inforeq) == inforeq) {
533 /* already loaded all of this info, do nothing */
534 return 0;
536 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
537 info->name, inforeq);
539 /* clear out 'line', to be certain - and to make valgrind happy */
540 memset(line, 0, sizeof(line));
542 pkgpath = get_pkgpath(db, info);
544 if(access(pkgpath, F_OK)) {
545 /* directory doesn't exist or can't be opened */
546 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
547 info->name, info->version, db->treename);
548 goto error;
551 /* DESC */
552 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
553 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
554 if((fp = fopen(path, "r")) == NULL) {
555 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
556 goto error;
558 while(!feof(fp)) {
559 READ_NEXT();
560 if(strcmp(line, "%NAME%") == 0) {
561 READ_NEXT();
562 if(strcmp(line, info->name) != 0) {
563 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
564 "mismatch on package %s\n"), db->treename, info->name);
566 } else if(strcmp(line, "%VERSION%") == 0) {
567 READ_NEXT();
568 if(strcmp(line, info->version) != 0) {
569 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
570 "mismatch on package %s\n"), db->treename, info->name);
572 } else if(strcmp(line, "%DESC%") == 0) {
573 READ_AND_STORE(info->desc);
574 } else if(strcmp(line, "%GROUPS%") == 0) {
575 READ_AND_STORE_ALL(info->groups);
576 } else if(strcmp(line, "%URL%") == 0) {
577 READ_AND_STORE(info->url);
578 } else if(strcmp(line, "%LICENSE%") == 0) {
579 READ_AND_STORE_ALL(info->licenses);
580 } else if(strcmp(line, "%ARCH%") == 0) {
581 READ_AND_STORE(info->arch);
582 } else if(strcmp(line, "%BUILDDATE%") == 0) {
583 READ_NEXT();
584 info->builddate = _alpm_parsedate(line);
585 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
586 READ_NEXT();
587 info->installdate = _alpm_parsedate(line);
588 } else if(strcmp(line, "%PACKAGER%") == 0) {
589 READ_AND_STORE(info->packager);
590 } else if(strcmp(line, "%REASON%") == 0) {
591 READ_NEXT();
592 info->reason = (alpm_pkgreason_t)atol(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 READ_NEXT();
600 info->size = atol(line);
601 /* also store this value to isize */
602 info->isize = info->size;
603 } else if(strcmp(line, "%REPLACES%") == 0) {
604 READ_AND_STORE_ALL(info->replaces);
605 } else if(strcmp(line, "%DEPENDS%") == 0) {
606 /* Different than the rest because of the _alpm_splitdep call. */
607 while(1) {
608 READ_NEXT();
609 if(strlen(line) == 0) break;
610 info->depends = alpm_list_add(info->depends, _alpm_splitdep(line));
612 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
613 READ_AND_STORE_ALL(info->optdepends);
614 } else if(strcmp(line, "%CONFLICTS%") == 0) {
615 READ_AND_STORE_ALL(info->conflicts);
616 } else if(strcmp(line, "%PROVIDES%") == 0) {
617 READ_AND_STORE_ALL(info->provides);
620 fclose(fp);
621 fp = NULL;
624 /* FILES */
625 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
626 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
627 if((fp = fopen(path, "r")) == NULL) {
628 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
629 goto error;
631 while(fgets(line, sizeof(line), fp)) {
632 _alpm_strtrim(line);
633 if(strcmp(line, "%FILES%") == 0) {
634 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
635 alpm_file_t *file;
636 CALLOC(file, 1, sizeof(alpm_file_t), goto error);
637 STRDUP(file->name, line, goto error);
638 /* TODO: lstat file, get mode/size */
639 info->files = alpm_list_add(info->files, file);
641 } else if(strcmp(line, "%BACKUP%") == 0) {
642 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
643 alpm_backup_t *backup;
644 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
645 if(_alpm_split_backup(line, &backup)) {
646 goto error;
648 info->backup = alpm_list_add(info->backup, backup);
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(alpm_db_t *db, alpm_pkg_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(db->handle, ALPM_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(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_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(db->handle, ALPM_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(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
731 path, strerror(errno));
732 retval = -1;
733 goto cleanup;
735 fprintf(fp, "%%NAME%%\n%s\n\n"
736 "%%VERSION%%\n%s\n\n", info->name, info->version);
737 if(info->desc) {
738 fprintf(fp, "%%DESC%%\n"
739 "%s\n\n", info->desc);
741 if(info->groups) {
742 fputs("%GROUPS%\n", fp);
743 for(lp = info->groups; lp; lp = lp->next) {
744 fprintf(fp, "%s\n", (char *)lp->data);
746 fprintf(fp, "\n");
748 if(info->replaces) {
749 fputs("%REPLACES%\n", fp);
750 for(lp = info->replaces; lp; lp = lp->next) {
751 fprintf(fp, "%s\n", (char *)lp->data);
753 fprintf(fp, "\n");
755 if(info->url) {
756 fprintf(fp, "%%URL%%\n"
757 "%s\n\n", info->url);
759 if(info->licenses) {
760 fputs("%LICENSE%\n", fp);
761 for(lp = info->licenses; lp; lp = lp->next) {
762 fprintf(fp, "%s\n", (char *)lp->data);
764 fprintf(fp, "\n");
766 if(info->arch) {
767 fprintf(fp, "%%ARCH%%\n"
768 "%s\n\n", info->arch);
770 if(info->builddate) {
771 fprintf(fp, "%%BUILDDATE%%\n"
772 "%ld\n\n", info->builddate);
774 if(info->installdate) {
775 fprintf(fp, "%%INSTALLDATE%%\n"
776 "%ld\n\n", info->installdate);
778 if(info->packager) {
779 fprintf(fp, "%%PACKAGER%%\n"
780 "%s\n\n", info->packager);
782 if(info->isize) {
783 /* only write installed size, csize is irrelevant once installed */
784 fprintf(fp, "%%SIZE%%\n"
785 "%jd\n\n", (intmax_t)info->isize);
787 if(info->reason) {
788 fprintf(fp, "%%REASON%%\n"
789 "%u\n\n", info->reason);
791 if(info->depends) {
792 fputs("%DEPENDS%\n", fp);
793 for(lp = info->depends; lp; lp = lp->next) {
794 char *depstring = alpm_dep_compute_string(lp->data);
795 fprintf(fp, "%s\n", depstring);
796 free(depstring);
798 fprintf(fp, "\n");
800 if(info->optdepends) {
801 fputs("%OPTDEPENDS%\n", fp);
802 for(lp = info->optdepends; lp; lp = lp->next) {
803 fprintf(fp, "%s\n", (char *)lp->data);
805 fprintf(fp, "\n");
807 if(info->conflicts) {
808 fputs("%CONFLICTS%\n", fp);
809 for(lp = info->conflicts; lp; lp = lp->next) {
810 fprintf(fp, "%s\n", (char *)lp->data);
812 fprintf(fp, "\n");
814 if(info->provides) {
815 fputs("%PROVIDES%\n", fp);
816 for(lp = info->provides; lp; lp = lp->next) {
817 fprintf(fp, "%s\n", (char *)lp->data);
819 fprintf(fp, "\n");
822 fclose(fp);
823 fp = NULL;
826 /* FILES */
827 if(inforeq & INFRQ_FILES) {
828 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
829 info->name, info->version);
830 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
831 if((fp = fopen(path, "w")) == NULL) {
832 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
833 path, strerror(errno));
834 retval = -1;
835 goto cleanup;
837 if(info->files) {
838 fprintf(fp, "%%FILES%%\n");
839 for(lp = info->files; lp; lp = lp->next) {
840 const alpm_file_t *file = lp->data;
841 fprintf(fp, "%s\n", file->name);
843 fprintf(fp, "\n");
845 if(info->backup) {
846 fprintf(fp, "%%BACKUP%%\n");
847 for(lp = info->backup; lp; lp = lp->next) {
848 const alpm_backup_t *backup = lp->data;
849 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
851 fprintf(fp, "\n");
853 fclose(fp);
854 fp = NULL;
857 /* INSTALL */
858 /* nothing needed here (script is automatically extracted) */
860 cleanup:
861 umask(oldmask);
862 free(pkgpath);
864 if(fp) {
865 fclose(fp);
868 return retval;
871 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
873 int ret = 0;
874 char *pkgpath = NULL;
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 struct db_operations local_db_ops = {
887 .validate = local_db_validate,
888 .populate = local_db_populate,
889 .unregister = _alpm_db_unregister,
892 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
894 alpm_db_t *db;
896 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
898 db = _alpm_db_new("local", 1);
899 if(db == NULL) {
900 handle->pm_errno = ALPM_ERR_DB_CREATE;
901 return NULL;
903 db->ops = &local_db_ops;
904 db->handle = handle;
906 if(local_db_validate(db)) {
907 /* pm_errno set in local_db_validate() */
908 _alpm_db_free(db);
909 return NULL;
912 handle->db_local = db;
913 return db;
916 /* vim: set ts=2 sw=2 noet: */