Rename pmbackup_t to alpm_backup_t
[pacman-ng.git] / lib / libalpm / be_local.c
blob19b06bb59efe204c61e021241ed47d790cd8204f
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(alpm_pkg_t *pkg)
60 LAZY_LOAD(INFRQ_DESC, NULL);
61 return pkg->filename;
64 static const char *_cache_get_desc(alpm_pkg_t *pkg)
66 LAZY_LOAD(INFRQ_DESC, NULL);
67 return pkg->desc;
70 static const char *_cache_get_url(alpm_pkg_t *pkg)
72 LAZY_LOAD(INFRQ_DESC, NULL);
73 return pkg->url;
76 static time_t _cache_get_builddate(alpm_pkg_t *pkg)
78 LAZY_LOAD(INFRQ_DESC, 0);
79 return pkg->builddate;
82 static time_t _cache_get_installdate(alpm_pkg_t *pkg)
84 LAZY_LOAD(INFRQ_DESC, 0);
85 return pkg->installdate;
88 static const char *_cache_get_packager(alpm_pkg_t *pkg)
90 LAZY_LOAD(INFRQ_DESC, NULL);
91 return pkg->packager;
94 static const char *_cache_get_md5sum(alpm_pkg_t *pkg)
96 LAZY_LOAD(INFRQ_DESC, NULL);
97 return pkg->md5sum;
100 static const char *_cache_get_arch(alpm_pkg_t *pkg)
102 LAZY_LOAD(INFRQ_DESC, NULL);
103 return pkg->arch;
106 static off_t _cache_get_size(alpm_pkg_t *pkg)
108 LAZY_LOAD(INFRQ_DESC, -1);
109 return pkg->size;
112 static off_t _cache_get_isize(alpm_pkg_t *pkg)
114 LAZY_LOAD(INFRQ_DESC, -1);
115 return pkg->isize;
118 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
120 LAZY_LOAD(INFRQ_DESC, -1);
121 return pkg->reason;
124 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
126 LAZY_LOAD(INFRQ_DESC, NULL);
127 return pkg->licenses;
130 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
132 LAZY_LOAD(INFRQ_DESC, NULL);
133 return pkg->groups;
136 static int _cache_has_scriptlet(alpm_pkg_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(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 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(alpm_pkg_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(alpm_pkg_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 alpm_pkg_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 alpm_pkg_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(alpm_db_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(alpm_db_t *db)
319 struct dirent *ent = NULL;
320 const char *dbpath;
321 DIR *dbdir;
322 int ret = -1;
324 if(db->status & DB_STATUS_VALID) {
325 return 0;
328 dbpath = _alpm_db_path(db);
329 if(dbpath == NULL) {
330 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
332 dbdir = opendir(dbpath);
333 if(dbdir == NULL) {
334 if(errno == ENOENT) {
335 /* database dir doesn't exist yet */
336 db->status |= DB_STATUS_VALID;
337 return 0;
338 } else {
339 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
343 while((ent = readdir(dbdir)) != NULL) {
344 const char *name = ent->d_name;
345 char path[PATH_MAX];
347 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
348 continue;
350 if(!is_dir(dbpath, ent)) {
351 continue;
354 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
355 if(access(path, F_OK) == 0) {
356 /* we found a depends file- bail */
357 db->handle->pm_errno = PM_ERR_DB_VERSION;
358 goto done;
361 /* we found no depends file after full scan */
362 db->status |= DB_STATUS_VALID;
363 ret = 0;
365 done:
366 if(dbdir) {
367 closedir(dbdir);
370 return ret;
373 static int local_db_populate(alpm_db_t *db)
375 size_t est_count;
376 int count = 0;
377 struct stat buf;
378 struct dirent *ent = NULL;
379 const char *dbpath;
380 DIR *dbdir;
382 dbpath = _alpm_db_path(db);
383 if(dbpath == NULL) {
384 /* pm_errno set in _alpm_db_path() */
385 return -1;
388 dbdir = opendir(dbpath);
389 if(dbdir == NULL) {
390 if(errno == ENOENT) {
391 /* no database existing yet is not an error */
392 return 0;
394 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
396 if(fstat(dirfd(dbdir), &buf) != 0) {
397 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
399 if(buf.st_nlink >= 2) {
400 est_count = buf.st_nlink;
401 } else {
402 /* Some filesystems don't subscribe to the two-implicit links school of
403 * thought, e.g. BTRFS, HFS+. See
404 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
406 est_count = 0;
407 while(readdir(dbdir) != NULL) {
408 est_count++;
410 rewinddir(dbdir);
412 if(est_count >= 2) {
413 /* subtract the two extra pointers to get # of children */
414 est_count -= 2;
417 /* initialize hash at 50% full */
418 db->pkgcache = _alpm_pkghash_create(est_count * 2);
419 if(db->pkgcache == NULL){
420 closedir(dbdir);
421 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
424 while((ent = readdir(dbdir)) != NULL) {
425 const char *name = ent->d_name;
427 alpm_pkg_t *pkg;
429 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
430 continue;
432 if(!is_dir(dbpath, ent)) {
433 continue;
436 pkg = _alpm_pkg_new();
437 if(pkg == NULL) {
438 closedir(dbdir);
439 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
441 /* split the db entry name */
442 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
443 &(pkg->name_hash)) != 0) {
444 _alpm_log(db->handle, PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
445 name);
446 _alpm_pkg_free(pkg);
447 continue;
450 /* duplicated database entries are not allowed */
451 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
452 _alpm_log(db->handle, PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
453 _alpm_pkg_free(pkg);
454 continue;
457 pkg->origin = PKG_FROM_LOCALDB;
458 pkg->origin_data.db = db;
459 pkg->ops = &local_pkg_ops;
460 pkg->handle = db->handle;
462 /* explicitly read with only 'BASE' data, accessors will handle the rest */
463 if(_alpm_local_db_read(db, pkg, INFRQ_BASE) == -1) {
464 _alpm_log(db->handle, PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
465 _alpm_pkg_free(pkg);
466 continue;
469 /* add to the collection */
470 _alpm_log(db->handle, PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
471 pkg->name, db->treename);
472 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
473 count++;
476 closedir(dbdir);
477 if(count > 0) {
478 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
480 _alpm_log(db->handle, PM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
481 count, db->treename);
483 return count;
486 /* Note: the return value must be freed by the caller */
487 static char *get_pkgpath(alpm_db_t *db, alpm_pkg_t *info)
489 size_t len;
490 char *pkgpath;
491 const char *dbpath;
493 dbpath = _alpm_db_path(db);
494 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
495 MALLOC(pkgpath, len, RET_ERR(db->handle, PM_ERR_MEMORY, NULL));
496 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
497 return pkgpath;
501 int _alpm_local_db_read(alpm_db_t *db, alpm_pkg_t *info, pmdbinfrq_t inforeq)
503 FILE *fp = NULL;
504 char path[PATH_MAX];
505 char line[1024];
506 char *pkgpath = NULL;
508 if(info == NULL || info->name == NULL || info->version == NULL) {
509 _alpm_log(db->handle, PM_LOG_DEBUG,
510 "invalid package entry provided to _alpm_local_db_read, skipping\n");
511 return -1;
514 if(info->origin != PKG_FROM_LOCALDB) {
515 _alpm_log(db->handle, PM_LOG_DEBUG,
516 "request to read info for a non-local package '%s', skipping...\n",
517 info->name);
518 return -1;
521 /* bitmask logic here:
522 * infolevel: 00001111
523 * inforeq: 00010100
524 * & result: 00000100
525 * == to inforeq? nope, we need to load more info. */
526 if((info->infolevel & inforeq) == inforeq) {
527 /* already loaded all of this info, do nothing */
528 return 0;
530 _alpm_log(db->handle, PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
531 info->name, inforeq);
533 /* clear out 'line', to be certain - and to make valgrind happy */
534 memset(line, 0, sizeof(line));
536 pkgpath = get_pkgpath(db, info);
538 if(access(pkgpath, F_OK)) {
539 /* directory doesn't exist or can't be opened */
540 _alpm_log(db->handle, PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
541 info->name, info->version, db->treename);
542 goto error;
545 /* DESC */
546 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
547 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
548 if((fp = fopen(path, "r")) == NULL) {
549 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
550 goto error;
552 while(!feof(fp)) {
553 if(fgets(line, sizeof(line), fp) == NULL) {
554 break;
556 _alpm_strtrim(line);
557 if(strcmp(line, "%NAME%") == 0) {
558 if(fgets(line, sizeof(line), fp) == NULL) {
559 goto error;
561 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
562 _alpm_log(db->handle, PM_LOG_ERROR, _("%s database is inconsistent: name "
563 "mismatch on package %s\n"), db->treename, info->name);
565 } else if(strcmp(line, "%VERSION%") == 0) {
566 if(fgets(line, sizeof(line), fp) == NULL) {
567 goto error;
569 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
570 _alpm_log(db->handle, PM_LOG_ERROR, _("%s database is inconsistent: version "
571 "mismatch on package %s\n"), db->treename, info->name);
573 } else if(strcmp(line, "%DESC%") == 0) {
574 if(fgets(line, sizeof(line), fp) == NULL) {
575 goto error;
577 STRDUP(info->desc, _alpm_strtrim(line), goto error);
578 } else if(strcmp(line, "%GROUPS%") == 0) {
579 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
580 char *linedup;
581 STRDUP(linedup, line, goto error);
582 info->groups = alpm_list_add(info->groups, linedup);
584 } else if(strcmp(line, "%URL%") == 0) {
585 if(fgets(line, sizeof(line), fp) == NULL) {
586 goto error;
588 STRDUP(info->url, _alpm_strtrim(line), goto error);
589 } else if(strcmp(line, "%LICENSE%") == 0) {
590 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
591 char *linedup;
592 STRDUP(linedup, line, goto error);
593 info->licenses = alpm_list_add(info->licenses, linedup);
595 } else if(strcmp(line, "%ARCH%") == 0) {
596 if(fgets(line, sizeof(line), fp) == NULL) {
597 goto error;
599 STRDUP(info->arch, _alpm_strtrim(line), goto error);
600 } else if(strcmp(line, "%BUILDDATE%") == 0) {
601 if(fgets(line, sizeof(line), fp) == NULL) {
602 goto error;
604 _alpm_strtrim(line);
605 info->builddate = _alpm_parsedate(line);
606 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
607 if(fgets(line, sizeof(line), fp) == NULL) {
608 goto error;
610 _alpm_strtrim(line);
611 info->installdate = _alpm_parsedate(line);
612 } else if(strcmp(line, "%PACKAGER%") == 0) {
613 if(fgets(line, sizeof(line), fp) == NULL) {
614 goto error;
616 STRDUP(info->packager, _alpm_strtrim(line), goto error);
617 } else if(strcmp(line, "%REASON%") == 0) {
618 if(fgets(line, sizeof(line), fp) == NULL) {
619 goto error;
621 info->reason = (alpm_pkgreason_t)atol(_alpm_strtrim(line));
622 } else if(strcmp(line, "%SIZE%") == 0) {
623 /* NOTE: the CSIZE and SIZE fields both share the "size" field
624 * in the pkginfo_t struct. This can be done b/c CSIZE
625 * is currently only used in sync databases, and SIZE is
626 * only used in local databases.
628 if(fgets(line, sizeof(line), fp) == NULL) {
629 goto error;
631 info->size = atol(_alpm_strtrim(line));
632 /* also store this value to isize */
633 info->isize = info->size;
634 } else if(strcmp(line, "%REPLACES%") == 0) {
635 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
636 char *linedup;
637 STRDUP(linedup, line, goto error);
638 info->replaces = alpm_list_add(info->replaces, linedup);
640 } else if(strcmp(line, "%DEPENDS%") == 0) {
641 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
642 alpm_depend_t *dep = _alpm_splitdep(line);
643 info->depends = alpm_list_add(info->depends, dep);
645 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
646 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
647 char *linedup;
648 STRDUP(linedup, line, goto error);
649 info->optdepends = alpm_list_add(info->optdepends, linedup);
651 } else if(strcmp(line, "%CONFLICTS%") == 0) {
652 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
653 char *linedup;
654 STRDUP(linedup, line, goto error);
655 info->conflicts = alpm_list_add(info->conflicts, linedup);
657 } else if(strcmp(line, "%PROVIDES%") == 0) {
658 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
659 char *linedup;
660 STRDUP(linedup, line, goto error);
661 info->provides = alpm_list_add(info->provides, linedup);
665 fclose(fp);
666 fp = NULL;
669 /* FILES */
670 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
671 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
672 if((fp = fopen(path, "r")) == NULL) {
673 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
674 goto error;
676 while(fgets(line, sizeof(line), fp)) {
677 _alpm_strtrim(line);
678 if(strcmp(line, "%FILES%") == 0) {
679 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
680 char *linedup;
681 STRDUP(linedup, line, goto error);
682 info->files = alpm_list_add(info->files, linedup);
684 } else if(strcmp(line, "%BACKUP%") == 0) {
685 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
686 alpm_backup_t *backup;
687 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
688 if(_alpm_split_backup(line, &backup)) {
689 goto error;
691 info->backup = alpm_list_add(info->backup, backup);
695 fclose(fp);
696 fp = NULL;
699 /* INSTALL */
700 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
701 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
702 if(access(path, F_OK) == 0) {
703 info->scriptlet = 1;
707 /* internal */
708 info->infolevel |= inforeq;
710 free(pkgpath);
711 return 0;
713 error:
714 free(pkgpath);
715 if(fp) {
716 fclose(fp);
718 return -1;
721 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
723 mode_t oldmask;
724 int retval = 0;
725 char *pkgpath = NULL;
727 if(checkdbdir(db) != 0) {
728 return -1;
731 oldmask = umask(0000);
732 pkgpath = get_pkgpath(db, info);
734 if((retval = mkdir(pkgpath, 0755)) != 0) {
735 _alpm_log(db->handle, PM_LOG_ERROR, _("could not create directory %s: %s\n"),
736 pkgpath, strerror(errno));
739 free(pkgpath);
740 umask(oldmask);
742 return retval;
745 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, pmdbinfrq_t inforeq)
747 FILE *fp = NULL;
748 char path[PATH_MAX];
749 mode_t oldmask;
750 alpm_list_t *lp = NULL;
751 int retval = 0;
752 char *pkgpath = NULL;
754 if(db == NULL || info == NULL) {
755 return -1;
758 pkgpath = get_pkgpath(db, info);
760 /* make sure we have a sane umask */
761 oldmask = umask(0022);
763 if(strcmp(db->treename, "local") != 0) {
764 return -1;
767 /* DESC */
768 if(inforeq & INFRQ_DESC) {
769 _alpm_log(db->handle, PM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
770 info->name, info->version);
771 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
772 if((fp = fopen(path, "w")) == NULL) {
773 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"),
774 path, strerror(errno));
775 retval = -1;
776 goto cleanup;
778 fprintf(fp, "%%NAME%%\n%s\n\n"
779 "%%VERSION%%\n%s\n\n", info->name, info->version);
780 if(info->desc) {
781 fprintf(fp, "%%DESC%%\n"
782 "%s\n\n", info->desc);
784 if(info->groups) {
785 fputs("%GROUPS%\n", fp);
786 for(lp = info->groups; lp; lp = lp->next) {
787 fprintf(fp, "%s\n", (char *)lp->data);
789 fprintf(fp, "\n");
791 if(info->replaces) {
792 fputs("%REPLACES%\n", fp);
793 for(lp = info->replaces; lp; lp = lp->next) {
794 fprintf(fp, "%s\n", (char *)lp->data);
796 fprintf(fp, "\n");
798 if(info->url) {
799 fprintf(fp, "%%URL%%\n"
800 "%s\n\n", info->url);
802 if(info->licenses) {
803 fputs("%LICENSE%\n", fp);
804 for(lp = info->licenses; lp; lp = lp->next) {
805 fprintf(fp, "%s\n", (char *)lp->data);
807 fprintf(fp, "\n");
809 if(info->arch) {
810 fprintf(fp, "%%ARCH%%\n"
811 "%s\n\n", info->arch);
813 if(info->builddate) {
814 fprintf(fp, "%%BUILDDATE%%\n"
815 "%ld\n\n", info->builddate);
817 if(info->installdate) {
818 fprintf(fp, "%%INSTALLDATE%%\n"
819 "%ld\n\n", info->installdate);
821 if(info->packager) {
822 fprintf(fp, "%%PACKAGER%%\n"
823 "%s\n\n", info->packager);
825 if(info->isize) {
826 /* only write installed size, csize is irrelevant once installed */
827 fprintf(fp, "%%SIZE%%\n"
828 "%jd\n\n", (intmax_t)info->isize);
830 if(info->reason) {
831 fprintf(fp, "%%REASON%%\n"
832 "%u\n\n", info->reason);
834 if(info->depends) {
835 fputs("%DEPENDS%\n", fp);
836 for(lp = info->depends; lp; lp = lp->next) {
837 char *depstring = alpm_dep_compute_string(lp->data);
838 fprintf(fp, "%s\n", depstring);
839 free(depstring);
841 fprintf(fp, "\n");
843 if(info->optdepends) {
844 fputs("%OPTDEPENDS%\n", fp);
845 for(lp = info->optdepends; lp; lp = lp->next) {
846 fprintf(fp, "%s\n", (char *)lp->data);
848 fprintf(fp, "\n");
850 if(info->conflicts) {
851 fputs("%CONFLICTS%\n", fp);
852 for(lp = info->conflicts; lp; lp = lp->next) {
853 fprintf(fp, "%s\n", (char *)lp->data);
855 fprintf(fp, "\n");
857 if(info->provides) {
858 fputs("%PROVIDES%\n", fp);
859 for(lp = info->provides; lp; lp = lp->next) {
860 fprintf(fp, "%s\n", (char *)lp->data);
862 fprintf(fp, "\n");
865 fclose(fp);
866 fp = NULL;
869 /* FILES */
870 if(inforeq & INFRQ_FILES) {
871 _alpm_log(db->handle, PM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
872 info->name, info->version);
873 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
874 if((fp = fopen(path, "w")) == NULL) {
875 _alpm_log(db->handle, PM_LOG_ERROR, _("could not open file %s: %s\n"),
876 path, strerror(errno));
877 retval = -1;
878 goto cleanup;
880 if(info->files) {
881 fprintf(fp, "%%FILES%%\n");
882 for(lp = info->files; lp; lp = lp->next) {
883 fprintf(fp, "%s\n", (char *)lp->data);
885 fprintf(fp, "\n");
887 if(info->backup) {
888 fprintf(fp, "%%BACKUP%%\n");
889 for(lp = info->backup; lp; lp = lp->next) {
890 alpm_backup_t *backup = lp->data;
891 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
893 fprintf(fp, "\n");
895 fclose(fp);
896 fp = NULL;
899 /* INSTALL */
900 /* nothing needed here (script is automatically extracted) */
902 cleanup:
903 umask(oldmask);
904 free(pkgpath);
906 if(fp) {
907 fclose(fp);
910 return retval;
913 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
915 int ret = 0;
916 char *pkgpath = NULL;
918 pkgpath = get_pkgpath(db, info);
920 ret = _alpm_rmrf(pkgpath);
921 free(pkgpath);
922 if(ret != 0) {
923 ret = -1;
925 return ret;
928 struct db_operations local_db_ops = {
929 .populate = local_db_populate,
930 .unregister = _alpm_db_unregister,
933 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
935 alpm_db_t *db;
937 _alpm_log(handle, PM_LOG_DEBUG, "registering local database\n");
939 db = _alpm_db_new("local", 1);
940 if(db == NULL) {
941 handle->pm_errno = PM_ERR_DB_CREATE;
942 return NULL;
944 db->ops = &local_db_ops;
945 db->handle = handle;
947 if(local_db_validate(db)) {
948 /* pm_errno set in local_db_validate() */
949 _alpm_db_free(db);
950 return NULL;
953 handle->db_local = db;
954 return db;
957 /* vim: set ts=2 sw=2 noet: */