Move database 'version' check to registration time
[pacman-ng.git] / lib / libalpm / be_local.c
blob0ff51deb1d3676c42afdea66e6027966e78eac02
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 #define LAZY_LOAD(info, errret) \
45 do { \
46 if(pkg->origin != PKG_FROM_FILE && !(pkg->infolevel & info)) { \
47 _alpm_local_db_read(pkg->origin_data.db, pkg, info); \
48 } \
49 } while(0)
52 /* Cache-specific accessor functions. These implementations allow for lazy
53 * loading by the files backend when a data member is actually needed
54 * rather than loading all pieces of information when the package is first
55 * initialized.
58 static const char *_cache_get_filename(pmpkg_t *pkg)
60 LAZY_LOAD(INFRQ_DESC, NULL);
61 return pkg->filename;
64 static const char *_cache_get_desc(pmpkg_t *pkg)
66 LAZY_LOAD(INFRQ_DESC, NULL);
67 return pkg->desc;
70 static const char *_cache_get_url(pmpkg_t *pkg)
72 LAZY_LOAD(INFRQ_DESC, NULL);
73 return pkg->url;
76 static time_t _cache_get_builddate(pmpkg_t *pkg)
78 LAZY_LOAD(INFRQ_DESC, 0);
79 return pkg->builddate;
82 static time_t _cache_get_installdate(pmpkg_t *pkg)
84 LAZY_LOAD(INFRQ_DESC, 0);
85 return pkg->installdate;
88 static const char *_cache_get_packager(pmpkg_t *pkg)
90 LAZY_LOAD(INFRQ_DESC, NULL);
91 return pkg->packager;
94 static const char *_cache_get_md5sum(pmpkg_t *pkg)
96 LAZY_LOAD(INFRQ_DESC, NULL);
97 return pkg->md5sum;
100 static const char *_cache_get_arch(pmpkg_t *pkg)
102 LAZY_LOAD(INFRQ_DESC, NULL);
103 return pkg->arch;
106 static off_t _cache_get_size(pmpkg_t *pkg)
108 LAZY_LOAD(INFRQ_DESC, -1);
109 return pkg->size;
112 static off_t _cache_get_isize(pmpkg_t *pkg)
114 LAZY_LOAD(INFRQ_DESC, -1);
115 return pkg->isize;
118 static pmpkgreason_t _cache_get_reason(pmpkg_t *pkg)
120 LAZY_LOAD(INFRQ_DESC, -1);
121 return pkg->reason;
124 static alpm_list_t *_cache_get_licenses(pmpkg_t *pkg)
126 LAZY_LOAD(INFRQ_DESC, NULL);
127 return pkg->licenses;
130 static alpm_list_t *_cache_get_groups(pmpkg_t *pkg)
132 LAZY_LOAD(INFRQ_DESC, NULL);
133 return pkg->groups;
136 static int _cache_has_scriptlet(pmpkg_t *pkg)
138 if(!(pkg->infolevel & INFRQ_SCRIPTLET)) {
139 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_SCRIPTLET);
141 return pkg->scriptlet;
144 static alpm_list_t *_cache_get_depends(pmpkg_t *pkg)
146 LAZY_LOAD(INFRQ_DESC, NULL);
147 return pkg->depends;
150 static alpm_list_t *_cache_get_optdepends(pmpkg_t *pkg)
152 LAZY_LOAD(INFRQ_DESC, NULL);
153 return pkg->optdepends;
156 static alpm_list_t *_cache_get_conflicts(pmpkg_t *pkg)
158 LAZY_LOAD(INFRQ_DESC, NULL);
159 return pkg->conflicts;
162 static alpm_list_t *_cache_get_provides(pmpkg_t *pkg)
164 LAZY_LOAD(INFRQ_DESC, NULL);
165 return pkg->provides;
168 static alpm_list_t *_cache_get_replaces(pmpkg_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(pmpkg_t UNUSED *pkg)
177 return NULL;
180 static alpm_list_t *_cache_get_files(pmpkg_t *pkg)
182 if(pkg->origin == PKG_FROM_LOCALDB
183 && !(pkg->infolevel & INFRQ_FILES)) {
184 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
186 return pkg->files;
189 static alpm_list_t *_cache_get_backup(pmpkg_t *pkg)
191 if(pkg->origin == PKG_FROM_LOCALDB
192 && !(pkg->infolevel & INFRQ_FILES)) {
193 _alpm_local_db_read(pkg->origin_data.db, pkg, INFRQ_FILES);
195 return pkg->backup;
199 * Open a package changelog for reading. Similar to fopen in functionality,
200 * except that the returned 'file stream' is from the database.
201 * @param pkg the package (from db) to read the changelog
202 * @return a 'file stream' to the package changelog
204 static void *_cache_changelog_open(pmpkg_t *pkg)
206 char clfile[PATH_MAX];
207 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
208 alpm_option_get_dbpath(pkg->handle),
209 alpm_db_get_name(alpm_pkg_get_db(pkg)),
210 alpm_pkg_get_name(pkg),
211 alpm_pkg_get_version(pkg));
212 return fopen(clfile, "r");
216 * Read data from an open changelog 'file stream'. Similar to fread in
217 * functionality, this function takes a buffer and amount of data to read.
218 * @param ptr a buffer to fill with raw changelog data
219 * @param size the size of the buffer
220 * @param pkg the package that the changelog is being read from
221 * @param fp a 'file stream' to the package changelog
222 * @return the number of characters read, or 0 if there is no more data
224 static size_t _cache_changelog_read(void *ptr, size_t size,
225 const pmpkg_t UNUSED *pkg, const void *fp)
227 return fread(ptr, 1, size, (FILE *)fp);
231 * Close a package changelog for reading. Similar to fclose in functionality,
232 * except that the 'file stream' is from the database.
233 * @param pkg the package that the changelog was read from
234 * @param fp a 'file stream' to the package changelog
235 * @return whether closing the package changelog stream was successful
237 static int _cache_changelog_close(const pmpkg_t UNUSED *pkg, void *fp)
239 return fclose((FILE *)fp);
243 /** The local database operations struct. Get package fields through
244 * lazy accessor methods that handle any backend loading and caching
245 * logic.
247 static struct pkg_operations local_pkg_ops = {
248 .get_filename = _cache_get_filename,
249 .get_desc = _cache_get_desc,
250 .get_url = _cache_get_url,
251 .get_builddate = _cache_get_builddate,
252 .get_installdate = _cache_get_installdate,
253 .get_packager = _cache_get_packager,
254 .get_md5sum = _cache_get_md5sum,
255 .get_arch = _cache_get_arch,
256 .get_size = _cache_get_size,
257 .get_isize = _cache_get_isize,
258 .get_reason = _cache_get_reason,
259 .has_scriptlet = _cache_has_scriptlet,
260 .get_licenses = _cache_get_licenses,
261 .get_groups = _cache_get_groups,
262 .get_depends = _cache_get_depends,
263 .get_optdepends = _cache_get_optdepends,
264 .get_conflicts = _cache_get_conflicts,
265 .get_provides = _cache_get_provides,
266 .get_replaces = _cache_get_replaces,
267 .get_deltas = _cache_get_deltas,
268 .get_files = _cache_get_files,
269 .get_backup = _cache_get_backup,
271 .changelog_open = _cache_changelog_open,
272 .changelog_read = _cache_changelog_read,
273 .changelog_close = _cache_changelog_close,
276 static int checkdbdir(pmdb_t *db)
278 struct stat buf;
279 const char *path = _alpm_db_path(db);
281 if(stat(path, &buf) != 0) {
282 _alpm_log(db->handle, PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
283 path);
284 if(_alpm_makepath(path) != 0) {
285 RET_ERR(db->handle, PM_ERR_SYSTEM, -1);
287 } else if(!S_ISDIR(buf.st_mode)) {
288 _alpm_log(db->handle, PM_LOG_WARNING, _("removing invalid database: %s\n"), path);
289 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
290 RET_ERR(db->handle, PM_ERR_SYSTEM, -1);
293 return 0;
296 static int is_dir(const char *path, struct dirent *entry)
298 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
299 if(entry->d_type != DT_UNKNOWN) {
300 return (entry->d_type == DT_DIR);
302 #endif
304 char buffer[PATH_MAX];
305 struct stat sbuf;
307 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
309 if(!stat(buffer, &sbuf)) {
310 return S_ISDIR(sbuf.st_mode);
314 return 0;
317 static int local_db_validate(pmdb_t *db)
319 struct dirent *ent = NULL;
320 const char *dbpath;
321 DIR *dbdir;
322 int ret = -1;
324 dbpath = _alpm_db_path(db);
325 if(dbpath == NULL) {
326 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
328 dbdir = opendir(dbpath);
329 if(dbdir == NULL) {
330 if(errno == ENOENT) {
331 /* database dir doesn't exist yet */
332 return 0;
333 } else {
334 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
338 while((ent = readdir(dbdir)) != NULL) {
339 const char *name = ent->d_name;
340 char path[PATH_MAX];
342 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
343 continue;
345 if(!is_dir(dbpath, ent)) {
346 continue;
349 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
350 if(access(path, F_OK) == 0) {
351 /* we found a depends file- bail */
352 db->handle->pm_errno = PM_ERR_DB_VERSION;
353 goto done;
356 /* we found no depends file after full scan */
357 ret = 0;
359 done:
360 if(dbdir) {
361 closedir(dbdir);
364 return ret;
367 static int local_db_populate(pmdb_t *db)
369 size_t est_count;
370 int count = 0;
371 struct stat buf;
372 struct dirent *ent = NULL;
373 const char *dbpath;
374 DIR *dbdir;
376 dbpath = _alpm_db_path(db);
377 if(dbpath == NULL) {
378 /* pm_errno set in _alpm_db_path() */
379 return -1;
382 dbdir = opendir(dbpath);
383 if(dbdir == NULL) {
384 if(errno == ENOENT) {
385 /* no database existing yet is not an error */
386 return 0;
388 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
390 if(fstat(dirfd(dbdir), &buf) != 0) {
391 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
393 if(buf.st_nlink >= 2) {
394 est_count = buf.st_nlink;
395 } else {
396 /* Some filesystems don't subscribe to the two-implicit links school of
397 * thought, e.g. BTRFS, HFS+. See
398 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
400 est_count = 0;
401 while(readdir(dbdir) != NULL) {
402 est_count++;
404 rewinddir(dbdir);
406 if(est_count >= 2) {
407 /* subtract the two extra pointers to get # of children */
408 est_count -= 2;
411 /* initialize hash at 50% full */
412 db->pkgcache = _alpm_pkghash_create(est_count * 2);
413 if(db->pkgcache == NULL){
414 closedir(dbdir);
415 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
418 while((ent = readdir(dbdir)) != NULL) {
419 const char *name = ent->d_name;
421 pmpkg_t *pkg;
423 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
424 continue;
426 if(!is_dir(dbpath, ent)) {
427 continue;
430 pkg = _alpm_pkg_new();
431 if(pkg == NULL) {
432 closedir(dbdir);
433 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
435 /* split the db entry name */
436 if(_alpm_splitname(name, pkg) != 0) {
437 _alpm_log(db->handle, PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
438 name);
439 _alpm_pkg_free(pkg);
440 continue;
443 /* duplicated database entries are not allowed */
444 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
445 _alpm_log(db->handle, PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
446 _alpm_pkg_free(pkg);
447 continue;
450 pkg->origin = PKG_FROM_LOCALDB;
451 pkg->origin_data.db = db;
452 pkg->ops = &local_pkg_ops;
453 pkg->handle = db->handle;
455 /* explicitly read with only 'BASE' data, accessors will handle the rest */
456 if(_alpm_local_db_read(db, pkg, INFRQ_BASE) == -1) {
457 _alpm_log(db->handle, PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
458 _alpm_pkg_free(pkg);
459 continue;
462 /* add to the collection */
463 _alpm_log(db->handle, PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
464 pkg->name, db->treename);
465 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
466 count++;
469 closedir(dbdir);
470 if(count > 0) {
471 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
473 _alpm_log(db->handle, PM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
474 count, db->treename);
476 return count;
479 /* Note: the return value must be freed by the caller */
480 static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
482 size_t len;
483 char *pkgpath;
484 const char *dbpath;
486 dbpath = _alpm_db_path(db);
487 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
488 MALLOC(pkgpath, len, RET_ERR(db->handle, PM_ERR_MEMORY, NULL));
489 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
490 return pkgpath;
494 int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
496 FILE *fp = NULL;
497 char path[PATH_MAX];
498 char line[1024];
499 char *pkgpath = NULL;
501 if(info == NULL || info->name == NULL || info->version == NULL) {
502 _alpm_log(db->handle, PM_LOG_DEBUG,
503 "invalid package entry provided to _alpm_local_db_read, skipping\n");
504 return -1;
507 if(info->origin != PKG_FROM_LOCALDB) {
508 _alpm_log(db->handle, PM_LOG_DEBUG,
509 "request to read info for a non-local package '%s', skipping...\n",
510 info->name);
511 return -1;
514 /* bitmask logic here:
515 * infolevel: 00001111
516 * inforeq: 00010100
517 * & result: 00000100
518 * == to inforeq? nope, we need to load more info. */
519 if((info->infolevel & inforeq) == inforeq) {
520 /* already loaded all of this info, do nothing */
521 return 0;
523 _alpm_log(db->handle, PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
524 info->name, inforeq);
526 /* clear out 'line', to be certain - and to make valgrind happy */
527 memset(line, 0, sizeof(line));
529 pkgpath = get_pkgpath(db, info);
531 if(access(pkgpath, F_OK)) {
532 /* directory doesn't exist or can't be opened */
533 _alpm_log(db->handle, PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
534 info->name, info->version, db->treename);
535 goto error;
538 /* DESC */
539 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
540 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
541 if((fp = fopen(path, "r")) == NULL) {
542 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
543 goto error;
545 while(!feof(fp)) {
546 if(fgets(line, sizeof(line), fp) == NULL) {
547 break;
549 _alpm_strtrim(line);
550 if(strcmp(line, "%NAME%") == 0) {
551 if(fgets(line, sizeof(line), fp) == NULL) {
552 goto error;
554 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
555 _alpm_log(db->handle, PM_LOG_ERROR, _("%s database is inconsistent: name "
556 "mismatch on package %s\n"), db->treename, info->name);
558 } else if(strcmp(line, "%VERSION%") == 0) {
559 if(fgets(line, sizeof(line), fp) == NULL) {
560 goto error;
562 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
563 _alpm_log(db->handle, PM_LOG_ERROR, _("%s database is inconsistent: version "
564 "mismatch on package %s\n"), db->treename, info->name);
566 } else if(strcmp(line, "%DESC%") == 0) {
567 if(fgets(line, sizeof(line), fp) == NULL) {
568 goto error;
570 STRDUP(info->desc, _alpm_strtrim(line), goto error);
571 } else if(strcmp(line, "%GROUPS%") == 0) {
572 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
573 char *linedup;
574 STRDUP(linedup, line, goto error);
575 info->groups = alpm_list_add(info->groups, linedup);
577 } else if(strcmp(line, "%URL%") == 0) {
578 if(fgets(line, sizeof(line), fp) == NULL) {
579 goto error;
581 STRDUP(info->url, _alpm_strtrim(line), goto error);
582 } else if(strcmp(line, "%LICENSE%") == 0) {
583 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
584 char *linedup;
585 STRDUP(linedup, line, goto error);
586 info->licenses = alpm_list_add(info->licenses, linedup);
588 } else if(strcmp(line, "%ARCH%") == 0) {
589 if(fgets(line, sizeof(line), fp) == NULL) {
590 goto error;
592 STRDUP(info->arch, _alpm_strtrim(line), goto error);
593 } else if(strcmp(line, "%BUILDDATE%") == 0) {
594 if(fgets(line, sizeof(line), fp) == NULL) {
595 goto error;
597 _alpm_strtrim(line);
598 info->builddate = _alpm_parsedate(line);
599 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
600 if(fgets(line, sizeof(line), fp) == NULL) {
601 goto error;
603 _alpm_strtrim(line);
604 info->installdate = _alpm_parsedate(line);
605 } else if(strcmp(line, "%PACKAGER%") == 0) {
606 if(fgets(line, sizeof(line), fp) == NULL) {
607 goto error;
609 STRDUP(info->packager, _alpm_strtrim(line), goto error);
610 } else if(strcmp(line, "%REASON%") == 0) {
611 if(fgets(line, sizeof(line), fp) == NULL) {
612 goto error;
614 info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
615 } else if(strcmp(line, "%SIZE%") == 0) {
616 /* NOTE: the CSIZE and SIZE fields both share the "size" field
617 * in the pkginfo_t struct. This can be done b/c CSIZE
618 * is currently only used in sync databases, and SIZE is
619 * only used in local databases.
621 if(fgets(line, sizeof(line), fp) == NULL) {
622 goto error;
624 info->size = atol(_alpm_strtrim(line));
625 /* also store this value to isize */
626 info->isize = info->size;
627 } else if(strcmp(line, "%REPLACES%") == 0) {
628 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
629 char *linedup;
630 STRDUP(linedup, line, goto error);
631 info->replaces = alpm_list_add(info->replaces, linedup);
633 } else if(strcmp(line, "%DEPENDS%") == 0) {
634 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
635 pmdepend_t *dep = _alpm_splitdep(line);
636 info->depends = alpm_list_add(info->depends, dep);
638 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
639 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
640 char *linedup;
641 STRDUP(linedup, line, goto error);
642 info->optdepends = alpm_list_add(info->optdepends, linedup);
644 } else if(strcmp(line, "%CONFLICTS%") == 0) {
645 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
646 char *linedup;
647 STRDUP(linedup, line, goto error);
648 info->conflicts = alpm_list_add(info->conflicts, linedup);
650 } else if(strcmp(line, "%PROVIDES%") == 0) {
651 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
652 char *linedup;
653 STRDUP(linedup, line, goto error);
654 info->provides = alpm_list_add(info->provides, linedup);
658 fclose(fp);
659 fp = NULL;
662 /* FILES */
663 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
664 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
665 if((fp = fopen(path, "r")) == NULL) {
666 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
667 goto error;
669 while(fgets(line, sizeof(line), fp)) {
670 _alpm_strtrim(line);
671 if(strcmp(line, "%FILES%") == 0) {
672 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
673 char *linedup;
674 STRDUP(linedup, line, goto error);
675 info->files = alpm_list_add(info->files, linedup);
677 } else if(strcmp(line, "%BACKUP%") == 0) {
678 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
679 pmbackup_t *backup;
680 CALLOC(backup, 1, sizeof(pmbackup_t), goto error);
681 if(_alpm_split_backup(line, &backup)) {
682 goto error;
684 info->backup = alpm_list_add(info->backup, backup);
688 fclose(fp);
689 fp = NULL;
692 /* INSTALL */
693 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
694 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
695 if(access(path, F_OK) == 0) {
696 info->scriptlet = 1;
700 /* internal */
701 info->infolevel |= inforeq;
703 free(pkgpath);
704 return 0;
706 error:
707 free(pkgpath);
708 if(fp) {
709 fclose(fp);
711 return -1;
714 int _alpm_local_db_prepare(pmdb_t *db, pmpkg_t *info)
716 mode_t oldmask;
717 int retval = 0;
718 char *pkgpath = NULL;
720 if(checkdbdir(db) != 0) {
721 return -1;
724 oldmask = umask(0000);
725 pkgpath = get_pkgpath(db, info);
727 if((retval = mkdir(pkgpath, 0755)) != 0) {
728 _alpm_log(db->handle, PM_LOG_ERROR, _("could not create directory %s: %s\n"),
729 pkgpath, strerror(errno));
732 free(pkgpath);
733 umask(oldmask);
735 return retval;
738 int _alpm_local_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
740 FILE *fp = NULL;
741 char path[PATH_MAX];
742 mode_t oldmask;
743 alpm_list_t *lp = NULL;
744 int retval = 0;
745 char *pkgpath = NULL;
747 if(db == NULL || info == NULL) {
748 return -1;
751 pkgpath = get_pkgpath(db, info);
753 /* make sure we have a sane umask */
754 oldmask = umask(0022);
756 if(strcmp(db->treename, "local") != 0) {
757 return -1;
760 /* DESC */
761 if(inforeq & INFRQ_DESC) {
762 _alpm_log(db->handle, PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
763 info->name, info->version);
764 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
765 if((fp = fopen(path, "w")) == NULL) {
766 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"),
767 path, strerror(errno));
768 retval = -1;
769 goto cleanup;
771 fprintf(fp, "%%NAME%%\n%s\n\n"
772 "%%VERSION%%\n%s\n\n", info->name, info->version);
773 if(info->desc) {
774 fprintf(fp, "%%DESC%%\n"
775 "%s\n\n", info->desc);
777 if(info->groups) {
778 fputs("%GROUPS%\n", fp);
779 for(lp = info->groups; lp; lp = lp->next) {
780 fprintf(fp, "%s\n", (char *)lp->data);
782 fprintf(fp, "\n");
784 if(info->replaces) {
785 fputs("%REPLACES%\n", fp);
786 for(lp = info->replaces; lp; lp = lp->next) {
787 fprintf(fp, "%s\n", (char *)lp->data);
789 fprintf(fp, "\n");
791 if(info->url) {
792 fprintf(fp, "%%URL%%\n"
793 "%s\n\n", info->url);
795 if(info->licenses) {
796 fputs("%LICENSE%\n", fp);
797 for(lp = info->licenses; lp; lp = lp->next) {
798 fprintf(fp, "%s\n", (char *)lp->data);
800 fprintf(fp, "\n");
802 if(info->arch) {
803 fprintf(fp, "%%ARCH%%\n"
804 "%s\n\n", info->arch);
806 if(info->builddate) {
807 fprintf(fp, "%%BUILDDATE%%\n"
808 "%ld\n\n", info->builddate);
810 if(info->installdate) {
811 fprintf(fp, "%%INSTALLDATE%%\n"
812 "%ld\n\n", info->installdate);
814 if(info->packager) {
815 fprintf(fp, "%%PACKAGER%%\n"
816 "%s\n\n", info->packager);
818 if(info->isize) {
819 /* only write installed size, csize is irrelevant once installed */
820 fprintf(fp, "%%SIZE%%\n"
821 "%jd\n\n", (intmax_t)info->isize);
823 if(info->reason) {
824 fprintf(fp, "%%REASON%%\n"
825 "%u\n\n", info->reason);
827 if(info->depends) {
828 fputs("%DEPENDS%\n", fp);
829 for(lp = info->depends; lp; lp = lp->next) {
830 char *depstring = alpm_dep_compute_string(lp->data);
831 fprintf(fp, "%s\n", depstring);
832 free(depstring);
834 fprintf(fp, "\n");
836 if(info->optdepends) {
837 fputs("%OPTDEPENDS%\n", fp);
838 for(lp = info->optdepends; lp; lp = lp->next) {
839 fprintf(fp, "%s\n", (char *)lp->data);
841 fprintf(fp, "\n");
843 if(info->conflicts) {
844 fputs("%CONFLICTS%\n", fp);
845 for(lp = info->conflicts; lp; lp = lp->next) {
846 fprintf(fp, "%s\n", (char *)lp->data);
848 fprintf(fp, "\n");
850 if(info->provides) {
851 fputs("%PROVIDES%\n", fp);
852 for(lp = info->provides; lp; lp = lp->next) {
853 fprintf(fp, "%s\n", (char *)lp->data);
855 fprintf(fp, "\n");
858 fclose(fp);
859 fp = NULL;
862 /* FILES */
863 if(inforeq & INFRQ_FILES) {
864 _alpm_log(db->handle, PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
865 info->name, info->version);
866 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
867 if((fp = fopen(path, "w")) == NULL) {
868 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"),
869 path, strerror(errno));
870 retval = -1;
871 goto cleanup;
873 if(info->files) {
874 fprintf(fp, "%%FILES%%\n");
875 for(lp = info->files; lp; lp = lp->next) {
876 fprintf(fp, "%s\n", (char *)lp->data);
878 fprintf(fp, "\n");
880 if(info->backup) {
881 fprintf(fp, "%%BACKUP%%\n");
882 for(lp = info->backup; lp; lp = lp->next) {
883 pmbackup_t *backup = lp->data;
884 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
886 fprintf(fp, "\n");
888 fclose(fp);
889 fp = NULL;
892 /* INSTALL */
893 /* nothing needed here (script is automatically extracted) */
895 cleanup:
896 umask(oldmask);
897 free(pkgpath);
899 if(fp) {
900 fclose(fp);
903 return retval;
906 int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
908 int ret = 0;
909 char *pkgpath = NULL;
911 pkgpath = get_pkgpath(db, info);
913 ret = _alpm_rmrf(pkgpath);
914 free(pkgpath);
915 if(ret != 0) {
916 ret = -1;
918 return ret;
921 struct db_operations local_db_ops = {
922 .populate = local_db_populate,
923 .unregister = _alpm_db_unregister,
926 pmdb_t *_alpm_db_register_local(pmhandle_t *handle)
928 pmdb_t *db;
930 _alpm_log(handle, PM_LOG_DEBUG, "registering local database\n");
932 db = _alpm_db_new("local", 1);
933 if(db == NULL) {
934 handle->pm_errno = PM_ERR_DB_CREATE;
935 return NULL;
937 db->ops = &local_db_ops;
938 db->handle = handle;
940 if(local_db_validate(db)) {
941 /* pm_errno set in local_db_validate() */
942 _alpm_db_free(db);
943 return NULL;
946 handle->db_local = db;
947 return db;
950 /* vim: set ts=2 sw=2 noet: */