Move important information up in -Si output
[pacman-ng.git] / lib / libalpm / be_local.c
blobbbc0a8acff39b60fb4b9545db1dc657d0a1aae27
1 /*
2 * be_local.c : backend for the local database
4 * Copyright (c) 2006-2012 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 <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdint.h> /* intmax_t */
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <limits.h> /* PATH_MAX */
31 /* libalpm */
32 #include "db.h"
33 #include "alpm_list.h"
34 #include "log.h"
35 #include "util.h"
36 #include "alpm.h"
37 #include "handle.h"
38 #include "package.h"
39 #include "deps.h"
40 #include "filelist.h"
42 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
44 #define LAZY_LOAD(info, errret) \
45 do { \
46 if(!(pkg->infolevel & info)) { \
47 local_db_read(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_desc(alpm_pkg_t *pkg)
60 LAZY_LOAD(INFRQ_DESC, NULL);
61 return pkg->desc;
64 static const char *_cache_get_url(alpm_pkg_t *pkg)
66 LAZY_LOAD(INFRQ_DESC, NULL);
67 return pkg->url;
70 static alpm_time_t _cache_get_builddate(alpm_pkg_t *pkg)
72 LAZY_LOAD(INFRQ_DESC, 0);
73 return pkg->builddate;
76 static alpm_time_t _cache_get_installdate(alpm_pkg_t *pkg)
78 LAZY_LOAD(INFRQ_DESC, 0);
79 return pkg->installdate;
82 static const char *_cache_get_packager(alpm_pkg_t *pkg)
84 LAZY_LOAD(INFRQ_DESC, NULL);
85 return pkg->packager;
88 static const char *_cache_get_arch(alpm_pkg_t *pkg)
90 LAZY_LOAD(INFRQ_DESC, NULL);
91 return pkg->arch;
94 static off_t _cache_get_isize(alpm_pkg_t *pkg)
96 LAZY_LOAD(INFRQ_DESC, -1);
97 return pkg->isize;
100 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
102 LAZY_LOAD(INFRQ_DESC, -1);
103 return pkg->reason;
106 static alpm_pkgvalidation_t _cache_get_validation(alpm_pkg_t *pkg)
108 LAZY_LOAD(INFRQ_DESC, -1);
109 return pkg->validation;
112 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
114 LAZY_LOAD(INFRQ_DESC, NULL);
115 return pkg->licenses;
118 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
120 LAZY_LOAD(INFRQ_DESC, NULL);
121 return pkg->groups;
124 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
126 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
127 return pkg->scriptlet;
130 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
132 LAZY_LOAD(INFRQ_DESC, NULL);
133 return pkg->depends;
136 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
138 LAZY_LOAD(INFRQ_DESC, NULL);
139 return pkg->optdepends;
142 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
144 LAZY_LOAD(INFRQ_DESC, NULL);
145 return pkg->conflicts;
148 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
150 LAZY_LOAD(INFRQ_DESC, NULL);
151 return pkg->provides;
154 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
156 LAZY_LOAD(INFRQ_DESC, NULL);
157 return pkg->replaces;
160 static alpm_filelist_t *_cache_get_files(alpm_pkg_t *pkg)
162 LAZY_LOAD(INFRQ_FILES, NULL);
163 return &(pkg->files);
166 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
168 LAZY_LOAD(INFRQ_FILES, NULL);
169 return pkg->backup;
173 * Open a package changelog for reading. Similar to fopen in functionality,
174 * except that the returned 'file stream' is from the database.
175 * @param pkg the package (from db) to read the changelog
176 * @return a 'file stream' to the package changelog
178 static void *_cache_changelog_open(alpm_pkg_t *pkg)
180 alpm_db_t *db = alpm_pkg_get_db(pkg);
181 char *clfile = _alpm_local_db_pkgpath(db, pkg, "changelog");
182 FILE *f = fopen(clfile, "r");
183 free(clfile);
184 return f;
188 * Read data from an open changelog 'file stream'. Similar to fread in
189 * functionality, this function takes a buffer and amount of data to read.
190 * @param ptr a buffer to fill with raw changelog data
191 * @param size the size of the buffer
192 * @param pkg the package that the changelog is being read from
193 * @param fp a 'file stream' to the package changelog
194 * @return the number of characters read, or 0 if there is no more data
196 static size_t _cache_changelog_read(void *ptr, size_t size,
197 const alpm_pkg_t UNUSED *pkg, void *fp)
199 return fread(ptr, 1, size, (FILE *)fp);
203 * Close a package changelog for reading. Similar to fclose in functionality,
204 * except that the 'file stream' is from the database.
205 * @param pkg the package that the changelog was read from
206 * @param fp a 'file stream' to the package changelog
207 * @return whether closing the package changelog stream was successful
209 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
211 return fclose((FILE *)fp);
214 static int _cache_force_load(alpm_pkg_t *pkg)
216 return local_db_read(pkg, INFRQ_ALL);
220 /** The local database operations struct. Get package fields through
221 * lazy accessor methods that handle any backend loading and caching
222 * logic.
224 static struct pkg_operations local_pkg_ops = {
225 .get_desc = _cache_get_desc,
226 .get_url = _cache_get_url,
227 .get_builddate = _cache_get_builddate,
228 .get_installdate = _cache_get_installdate,
229 .get_packager = _cache_get_packager,
230 .get_arch = _cache_get_arch,
231 .get_isize = _cache_get_isize,
232 .get_reason = _cache_get_reason,
233 .get_validation = _cache_get_validation,
234 .has_scriptlet = _cache_has_scriptlet,
235 .get_licenses = _cache_get_licenses,
236 .get_groups = _cache_get_groups,
237 .get_depends = _cache_get_depends,
238 .get_optdepends = _cache_get_optdepends,
239 .get_conflicts = _cache_get_conflicts,
240 .get_provides = _cache_get_provides,
241 .get_replaces = _cache_get_replaces,
242 .get_files = _cache_get_files,
243 .get_backup = _cache_get_backup,
245 .changelog_open = _cache_changelog_open,
246 .changelog_read = _cache_changelog_read,
247 .changelog_close = _cache_changelog_close,
249 .force_load = _cache_force_load,
252 static int checkdbdir(alpm_db_t *db)
254 struct stat buf;
255 const char *path = _alpm_db_path(db);
257 if(stat(path, &buf) != 0) {
258 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
259 path);
260 if(_alpm_makepath(path) != 0) {
261 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
263 } else if(!S_ISDIR(buf.st_mode)) {
264 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
265 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
266 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
269 return 0;
272 static int is_dir(const char *path, struct dirent *entry)
274 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
275 if(entry->d_type != DT_UNKNOWN) {
276 return (entry->d_type == DT_DIR);
278 #endif
280 char buffer[PATH_MAX];
281 struct stat sbuf;
283 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
285 if(!stat(buffer, &sbuf)) {
286 return S_ISDIR(sbuf.st_mode);
290 return 0;
293 static int local_db_validate(alpm_db_t *db)
295 struct dirent *ent = NULL;
296 const char *dbpath;
297 DIR *dbdir;
298 int ret = -1;
300 if(db->status & DB_STATUS_VALID) {
301 return 0;
303 if(db->status & DB_STATUS_INVALID) {
304 return -1;
307 dbpath = _alpm_db_path(db);
308 if(dbpath == NULL) {
309 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
311 dbdir = opendir(dbpath);
312 if(dbdir == NULL) {
313 if(errno == ENOENT) {
314 /* database dir doesn't exist yet */
315 db->status |= DB_STATUS_VALID;
316 db->status &= ~DB_STATUS_INVALID;
317 db->status &= ~DB_STATUS_EXISTS;
318 db->status |= DB_STATUS_MISSING;
319 return 0;
320 } else {
321 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
324 db->status |= DB_STATUS_EXISTS;
325 db->status &= ~DB_STATUS_MISSING;
327 while((ent = readdir(dbdir)) != NULL) {
328 const char *name = ent->d_name;
329 char path[PATH_MAX];
331 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
332 continue;
334 if(!is_dir(dbpath, ent)) {
335 continue;
338 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
339 if(access(path, F_OK) == 0) {
340 /* we found a depends file- bail */
341 db->status &= ~DB_STATUS_VALID;
342 db->status |= DB_STATUS_INVALID;
343 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
344 goto done;
347 /* we found no depends file after full scan */
348 db->status |= DB_STATUS_VALID;
349 db->status &= ~DB_STATUS_INVALID;
350 ret = 0;
352 done:
353 if(dbdir) {
354 closedir(dbdir);
357 return ret;
360 static int local_db_populate(alpm_db_t *db)
362 size_t est_count;
363 int count = 0;
364 struct stat buf;
365 struct dirent *ent = NULL;
366 const char *dbpath;
367 DIR *dbdir;
369 if(db->status & DB_STATUS_INVALID) {
370 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
372 /* note: DB_STATUS_MISSING is not fatal for local database */
374 dbpath = _alpm_db_path(db);
375 if(dbpath == NULL) {
376 /* pm_errno set in _alpm_db_path() */
377 return -1;
380 dbdir = opendir(dbpath);
381 if(dbdir == NULL) {
382 if(errno == ENOENT) {
383 /* no database existing yet is not an error */
384 db->status &= ~DB_STATUS_EXISTS;
385 db->status |= DB_STATUS_MISSING;
386 return 0;
388 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
390 if(fstat(dirfd(dbdir), &buf) != 0) {
391 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
393 db->status |= DB_STATUS_EXISTS;
394 db->status &= ~DB_STATUS_MISSING;
395 if(buf.st_nlink >= 2) {
396 est_count = buf.st_nlink;
397 } else {
398 /* Some filesystems don't subscribe to the two-implicit links school of
399 * thought, e.g. BTRFS, HFS+. See
400 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
402 est_count = 0;
403 while(readdir(dbdir) != NULL) {
404 est_count++;
406 rewinddir(dbdir);
408 if(est_count >= 2) {
409 /* subtract the '.' and '..' pointers to get # of children */
410 est_count -= 2;
413 db->pkgcache = _alpm_pkghash_create(est_count);
414 if(db->pkgcache == NULL){
415 closedir(dbdir);
416 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
419 while((ent = readdir(dbdir)) != NULL) {
420 const char *name = ent->d_name;
422 alpm_pkg_t *pkg;
424 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
425 continue;
427 if(!is_dir(dbpath, ent)) {
428 continue;
431 pkg = _alpm_pkg_new();
432 if(pkg == NULL) {
433 closedir(dbdir);
434 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
436 /* split the db entry name */
437 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
438 &(pkg->name_hash)) != 0) {
439 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
440 name);
441 _alpm_pkg_free(pkg);
442 continue;
445 /* duplicated database entries are not allowed */
446 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
447 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
448 _alpm_pkg_free(pkg);
449 continue;
452 pkg->origin = ALPM_PKG_FROM_LOCALDB;
453 pkg->origin_data.db = db;
454 pkg->ops = &local_pkg_ops;
455 pkg->handle = db->handle;
457 /* explicitly read with only 'BASE' data, accessors will handle the rest */
458 if(local_db_read(pkg, INFRQ_BASE) == -1) {
459 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
460 _alpm_pkg_free(pkg);
461 continue;
464 /* add to the collection */
465 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
466 pkg->name, db->treename);
467 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
468 count++;
471 closedir(dbdir);
472 if(count > 0) {
473 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
475 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
476 count, db->treename);
478 return count;
481 /* Note: the return value must be freed by the caller */
482 char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
483 const char *filename)
485 size_t len;
486 char *pkgpath;
487 const char *dbpath;
489 dbpath = _alpm_db_path(db);
490 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
491 len += filename ? strlen(filename) : 0;
492 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
493 sprintf(pkgpath, "%s%s-%s/%s", dbpath, info->name, info->version,
494 filename ? filename : "");
495 return pkgpath;
498 #define READ_NEXT() do { \
499 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
500 _alpm_strip_newline(line, 0); \
501 } while(0)
503 #define READ_AND_STORE(f) do { \
504 READ_NEXT(); \
505 STRDUP(f, line, goto error); \
506 } while(0)
508 #define READ_AND_STORE_ALL(f) do { \
509 char *linedup; \
510 if(fgets(line, sizeof(line), fp) == NULL) {\
511 if(!feof(fp)) goto error; else break; \
513 if(_alpm_strip_newline(line, 0) == 0) break; \
514 STRDUP(linedup, line, goto error); \
515 f = alpm_list_add(f, linedup); \
516 } while(1) /* note the while(1) and not (0) */
518 #define READ_AND_SPLITDEP(f) do { \
519 if(fgets(line, sizeof(line), fp) == NULL) {\
520 if(!feof(fp)) goto error; else break; \
522 if(_alpm_strip_newline(line, 0) == 0) break; \
523 f = alpm_list_add(f, _alpm_splitdep(line)); \
524 } while(1) /* note the while(1) and not (0) */
526 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
528 FILE *fp = NULL;
529 char line[1024];
530 alpm_db_t *db = info->origin_data.db;
532 /* bitmask logic here:
533 * infolevel: 00001111
534 * inforeq: 00010100
535 * & result: 00000100
536 * == to inforeq? nope, we need to load more info. */
537 if((info->infolevel & inforeq) == inforeq) {
538 /* already loaded all of this info, do nothing */
539 return 0;
542 if(info->infolevel & INFRQ_ERROR) {
543 /* We've encountered an error loading this package before. Don't attempt
544 * repeated reloads, just give up. */
545 return -1;
548 _alpm_log(db->handle, ALPM_LOG_FUNCTION,
549 "loading package data for %s : level=0x%x\n",
550 info->name, inforeq);
552 /* clear out 'line', to be certain - and to make valgrind happy */
553 memset(line, 0, sizeof(line));
555 /* DESC */
556 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
557 char *path = _alpm_local_db_pkgpath(db, info, "desc");
558 if(!path || (fp = fopen(path, "r")) == NULL) {
559 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
560 free(path);
561 goto error;
563 free(path);
564 while(!feof(fp)) {
565 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
566 goto error;
568 if(_alpm_strip_newline(line, 0) == 0) {
569 /* length of stripped line was zero */
570 continue;
572 if(strcmp(line, "%NAME%") == 0) {
573 READ_NEXT();
574 if(strcmp(line, info->name) != 0) {
575 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
576 "mismatch on package %s\n"), db->treename, info->name);
578 } else if(strcmp(line, "%VERSION%") == 0) {
579 READ_NEXT();
580 if(strcmp(line, info->version) != 0) {
581 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
582 "mismatch on package %s\n"), db->treename, info->name);
584 } else if(strcmp(line, "%DESC%") == 0) {
585 READ_AND_STORE(info->desc);
586 } else if(strcmp(line, "%GROUPS%") == 0) {
587 READ_AND_STORE_ALL(info->groups);
588 } else if(strcmp(line, "%URL%") == 0) {
589 READ_AND_STORE(info->url);
590 } else if(strcmp(line, "%LICENSE%") == 0) {
591 READ_AND_STORE_ALL(info->licenses);
592 } else if(strcmp(line, "%ARCH%") == 0) {
593 READ_AND_STORE(info->arch);
594 } else if(strcmp(line, "%BUILDDATE%") == 0) {
595 READ_NEXT();
596 info->builddate = _alpm_parsedate(line);
597 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
598 READ_NEXT();
599 info->installdate = _alpm_parsedate(line);
600 } else if(strcmp(line, "%PACKAGER%") == 0) {
601 READ_AND_STORE(info->packager);
602 } else if(strcmp(line, "%REASON%") == 0) {
603 READ_NEXT();
604 info->reason = (alpm_pkgreason_t)atoi(line);
605 } else if(strcmp(line, "%VALIDATION%") == 0) {
606 alpm_list_t *i, *v = NULL;
607 READ_AND_STORE_ALL(v);
608 for(i = v; i; i = alpm_list_next(i))
610 if(strcmp(i->data, "none") == 0) {
611 info->validation |= ALPM_PKG_VALIDATION_NONE;
612 } else if(strcmp(i->data, "md5") == 0) {
613 info->validation |= ALPM_PKG_VALIDATION_MD5SUM;
614 } else if(strcmp(i->data, "sha256") == 0) {
615 info->validation |= ALPM_PKG_VALIDATION_SHA256SUM;
616 } else if(strcmp(i->data, "pgp") == 0) {
617 info->validation |= ALPM_PKG_VALIDATION_SIGNATURE;
618 } else {
619 _alpm_log(db->handle, ALPM_LOG_WARNING,
620 _("unknown validation type for package %s: %s\n"),
621 info->name, (const char *)i->data);
624 FREELIST(v);
625 } else if(strcmp(line, "%SIZE%") == 0) {
626 READ_NEXT();
627 info->isize = _alpm_strtoofft(line);
628 } else if(strcmp(line, "%REPLACES%") == 0) {
629 READ_AND_SPLITDEP(info->replaces);
630 } else if(strcmp(line, "%DEPENDS%") == 0) {
631 READ_AND_SPLITDEP(info->depends);
632 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
633 READ_AND_SPLITDEP(info->optdepends);
634 } else if(strcmp(line, "%CONFLICTS%") == 0) {
635 READ_AND_SPLITDEP(info->conflicts);
636 } else if(strcmp(line, "%PROVIDES%") == 0) {
637 READ_AND_SPLITDEP(info->provides);
640 fclose(fp);
641 fp = NULL;
642 info->infolevel |= INFRQ_DESC;
645 /* FILES */
646 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
647 char *path = _alpm_local_db_pkgpath(db, info, "files");
648 if(!path || (fp = fopen(path, "r")) == NULL) {
649 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
650 free(path);
651 goto error;
653 free(path);
654 while(fgets(line, sizeof(line), fp)) {
655 _alpm_strip_newline(line, 0);
656 if(strcmp(line, "%FILES%") == 0) {
657 size_t files_count = 0, files_size = 0, len;
658 alpm_file_t *files = NULL;
660 while(fgets(line, sizeof(line), fp) &&
661 (len = _alpm_strip_newline(line, 0))) {
662 if(files_count >= files_size) {
663 size_t old_size = files_size;
664 if(files_size == 0) {
665 files_size = 8;
666 } else {
667 files_size *= 2;
669 files = realloc(files, sizeof(alpm_file_t) * files_size);
670 if(!files) {
671 _alpm_alloc_fail(sizeof(alpm_file_t) * files_size);
672 goto error;
674 /* ensure all new memory is zeroed out, in both the initial
675 * allocation and later reallocs */
676 memset(files + old_size, 0,
677 sizeof(alpm_file_t) * (files_size - old_size));
679 /* since we know the length of the file string already,
680 * we can do malloc + memcpy rather than strdup */
681 len += 1;
682 files[files_count].name = malloc(len);
683 if(files[files_count].name == NULL) {
684 _alpm_alloc_fail(len);
685 goto error;
687 memcpy(files[files_count].name, line, len);
688 files_count++;
690 /* attempt to hand back any memory we don't need */
691 files = realloc(files, sizeof(alpm_file_t) * files_count);
692 /* make sure the list is sorted */
693 qsort(files, files_count, sizeof(alpm_file_t), _alpm_files_cmp);
694 info->files.count = files_count;
695 info->files.files = files;
696 } else if(strcmp(line, "%BACKUP%") == 0) {
697 while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line, 0)) {
698 alpm_backup_t *backup;
699 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
700 if(_alpm_split_backup(line, &backup)) {
701 goto error;
703 info->backup = alpm_list_add(info->backup, backup);
707 fclose(fp);
708 fp = NULL;
709 info->infolevel |= INFRQ_FILES;
712 /* INSTALL */
713 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
714 char *path = _alpm_local_db_pkgpath(db, info, "install");
715 if(access(path, F_OK) == 0) {
716 info->scriptlet = 1;
718 free(path);
719 info->infolevel |= INFRQ_SCRIPTLET;
722 return 0;
724 error:
725 info->infolevel |= INFRQ_ERROR;
726 if(fp) {
727 fclose(fp);
729 return -1;
732 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
734 mode_t oldmask;
735 int retval = 0;
736 char *pkgpath;
738 if(checkdbdir(db) != 0) {
739 return -1;
742 oldmask = umask(0000);
743 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
745 if((retval = mkdir(pkgpath, 0755)) != 0) {
746 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not create directory %s: %s\n"),
747 pkgpath, strerror(errno));
750 free(pkgpath);
751 umask(oldmask);
753 return retval;
756 static void write_deps(FILE *fp, const char *header, alpm_list_t *deplist)
758 alpm_list_t *lp;
759 if(!deplist) {
760 return;
762 fputs(header, fp);
763 fputc('\n', fp);
764 for(lp = deplist; lp; lp = lp->next) {
765 char *depstring = alpm_dep_compute_string(lp->data);
766 fputs(depstring, fp);
767 fputc('\n', fp);
768 free(depstring);
770 fputc('\n', fp);
773 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
775 FILE *fp = NULL;
776 mode_t oldmask;
777 alpm_list_t *lp;
778 int retval = 0;
780 if(db == NULL || info == NULL || !(db->status & DB_STATUS_LOCAL)) {
781 return -1;
784 /* make sure we have a sane umask */
785 oldmask = umask(0022);
787 /* DESC */
788 if(inforeq & INFRQ_DESC) {
789 char *path;
790 _alpm_log(db->handle, ALPM_LOG_DEBUG,
791 "writing %s-%s DESC information back to db\n",
792 info->name, info->version);
793 path = _alpm_local_db_pkgpath(db, info, "desc");
794 if(!path || (fp = fopen(path, "w")) == NULL) {
795 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
796 path, strerror(errno));
797 retval = -1;
798 free(path);
799 goto cleanup;
801 free(path);
802 fprintf(fp, "%%NAME%%\n%s\n\n"
803 "%%VERSION%%\n%s\n\n", info->name, info->version);
804 if(info->desc) {
805 fprintf(fp, "%%DESC%%\n"
806 "%s\n\n", info->desc);
808 if(info->url) {
809 fprintf(fp, "%%URL%%\n"
810 "%s\n\n", info->url);
812 if(info->arch) {
813 fprintf(fp, "%%ARCH%%\n"
814 "%s\n\n", info->arch);
816 if(info->builddate) {
817 fprintf(fp, "%%BUILDDATE%%\n"
818 "%jd\n\n", (intmax_t)info->builddate);
820 if(info->installdate) {
821 fprintf(fp, "%%INSTALLDATE%%\n"
822 "%jd\n\n", (intmax_t)info->installdate);
824 if(info->packager) {
825 fprintf(fp, "%%PACKAGER%%\n"
826 "%s\n\n", info->packager);
828 if(info->isize) {
829 /* only write installed size, csize is irrelevant once installed */
830 fprintf(fp, "%%SIZE%%\n"
831 "%jd\n\n", (intmax_t)info->isize);
833 if(info->reason) {
834 fprintf(fp, "%%REASON%%\n"
835 "%u\n\n", info->reason);
837 if(info->groups) {
838 fputs("%GROUPS%\n", fp);
839 for(lp = info->groups; lp; lp = lp->next) {
840 fputs(lp->data, fp);
841 fputc('\n', fp);
843 fputc('\n', fp);
845 if(info->licenses) {
846 fputs("%LICENSE%\n", fp);
847 for(lp = info->licenses; lp; lp = lp->next) {
848 fputs(lp->data, fp);
849 fputc('\n', fp);
851 fputc('\n', fp);
853 if(info->validation) {
854 fputs("%VALIDATION%\n", fp);
855 if(info->validation & ALPM_PKG_VALIDATION_NONE) {
856 fputs("none\n", fp);
858 if(info->validation & ALPM_PKG_VALIDATION_MD5SUM) {
859 fputs("md5\n", fp);
861 if(info->validation & ALPM_PKG_VALIDATION_SHA256SUM) {
862 fputs("sha256\n", fp);
864 if(info->validation & ALPM_PKG_VALIDATION_SIGNATURE) {
865 fputs("pgp\n", fp);
867 fputc('\n', fp);
870 write_deps(fp, "%REPLACES%", info->replaces);
871 write_deps(fp, "%DEPENDS%", info->depends);
872 write_deps(fp, "%OPTDEPENDS%", info->optdepends);
873 write_deps(fp, "%CONFLICTS%", info->conflicts);
874 write_deps(fp, "%PROVIDES%", info->provides);
876 fclose(fp);
877 fp = NULL;
880 /* FILES */
881 if(inforeq & INFRQ_FILES) {
882 char *path;
883 _alpm_log(db->handle, ALPM_LOG_DEBUG,
884 "writing %s-%s FILES information back to db\n",
885 info->name, info->version);
886 path = _alpm_local_db_pkgpath(db, info, "files");
887 if(!path || (fp = fopen(path, "w")) == NULL) {
888 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
889 path, strerror(errno));
890 retval = -1;
891 free(path);
892 goto cleanup;
894 free(path);
895 if(info->files.count) {
896 size_t i;
897 fputs("%FILES%\n", fp);
898 for(i = 0; i < info->files.count; i++) {
899 const alpm_file_t *file = info->files.files + i;
900 fputs(file->name, fp);
901 fputc('\n', fp);
903 fputc('\n', fp);
905 if(info->backup) {
906 fputs("%BACKUP%\n", fp);
907 for(lp = info->backup; lp; lp = lp->next) {
908 const alpm_backup_t *backup = lp->data;
909 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
911 fputc('\n', fp);
913 fclose(fp);
914 fp = NULL;
917 /* INSTALL */
918 /* nothing needed here (script is automatically extracted) */
920 cleanup:
921 umask(oldmask);
923 if(fp) {
924 fclose(fp);
927 return retval;
930 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
932 int ret = 0;
933 DIR *dirp;
934 struct dirent *dp;
935 char *pkgpath;
936 size_t pkgpath_len;
938 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
939 if(!pkgpath) {
940 return -1;
942 pkgpath_len = strlen(pkgpath);
944 dirp = opendir(pkgpath);
945 if(!dirp) {
946 return -1;
948 /* go through the local DB entry, removing the files within, which we know
949 * are not nested directories of any kind. */
950 for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
951 if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
952 char name[PATH_MAX];
953 if(pkgpath_len + strlen(dp->d_name) + 2 > PATH_MAX) {
954 /* file path is too long to remove, hmm. */
955 ret = -1;
956 } else {
957 sprintf(name, "%s/%s", pkgpath, dp->d_name);
958 if(unlink(name)) {
959 ret = -1;
964 closedir(dirp);
966 /* after removing all enclosed files, we can remove the directory itself. */
967 if(rmdir(pkgpath)) {
968 ret = -1;
970 free(pkgpath);
971 return ret;
974 int SYMEXPORT alpm_pkg_set_reason(alpm_pkg_t *pkg, alpm_pkgreason_t reason)
976 ASSERT(pkg != NULL, return -1);
977 ASSERT(pkg->origin == ALPM_PKG_FROM_LOCALDB,
978 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
979 ASSERT(pkg->origin_data.db == pkg->handle->db_local,
980 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
982 _alpm_log(pkg->handle, ALPM_LOG_DEBUG,
983 "setting install reason %u for %s\n", reason, pkg->name);
984 if(alpm_pkg_get_reason(pkg) == reason) {
985 /* we are done */
986 return 0;
988 /* set reason (in pkgcache) */
989 pkg->reason = reason;
990 /* write DESC */
991 if(_alpm_local_db_write(pkg->handle->db_local, pkg, INFRQ_DESC)) {
992 RET_ERR(pkg->handle, ALPM_ERR_DB_WRITE, -1);
995 return 0;
998 struct db_operations local_db_ops = {
999 .validate = local_db_validate,
1000 .populate = local_db_populate,
1001 .unregister = _alpm_db_unregister,
1004 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
1006 alpm_db_t *db;
1008 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
1010 db = _alpm_db_new("local", 1);
1011 if(db == NULL) {
1012 handle->pm_errno = ALPM_ERR_DB_CREATE;
1013 return NULL;
1015 db->ops = &local_db_ops;
1016 db->handle = handle;
1018 if(local_db_validate(db)) {
1019 /* pm_errno set in local_db_validate() */
1020 _alpm_db_free(db);
1021 return NULL;
1024 handle->db_local = db;
1025 return db;
1028 /* vim: set ts=2 sw=2 noet: */