Scripts testsuite output consistency
[pacman-ng.git] / lib / libalpm / be_local.c
blobee805b9e8a587a2ef09c555f31e0c78aa5bb62fb
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"
41 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
43 #define LAZY_LOAD(info, errret) \
44 do { \
45 if(!(pkg->infolevel & info)) { \
46 local_db_read(pkg, info); \
47 } \
48 } while(0)
51 /* Cache-specific accessor functions. These implementations allow for lazy
52 * loading by the files backend when a data member is actually needed
53 * rather than loading all pieces of information when the package is first
54 * initialized.
57 static const char *_cache_get_desc(alpm_pkg_t *pkg)
59 LAZY_LOAD(INFRQ_DESC, NULL);
60 return pkg->desc;
63 static const char *_cache_get_url(alpm_pkg_t *pkg)
65 LAZY_LOAD(INFRQ_DESC, NULL);
66 return pkg->url;
69 static alpm_time_t _cache_get_builddate(alpm_pkg_t *pkg)
71 LAZY_LOAD(INFRQ_DESC, 0);
72 return pkg->builddate;
75 static alpm_time_t _cache_get_installdate(alpm_pkg_t *pkg)
77 LAZY_LOAD(INFRQ_DESC, 0);
78 return pkg->installdate;
81 static const char *_cache_get_packager(alpm_pkg_t *pkg)
83 LAZY_LOAD(INFRQ_DESC, NULL);
84 return pkg->packager;
87 static const char *_cache_get_arch(alpm_pkg_t *pkg)
89 LAZY_LOAD(INFRQ_DESC, NULL);
90 return pkg->arch;
93 static off_t _cache_get_isize(alpm_pkg_t *pkg)
95 LAZY_LOAD(INFRQ_DESC, -1);
96 return pkg->isize;
99 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
101 LAZY_LOAD(INFRQ_DESC, -1);
102 return pkg->reason;
105 static alpm_pkgvalidation_t _cache_get_validation(alpm_pkg_t *pkg)
107 LAZY_LOAD(INFRQ_DESC, -1);
108 return pkg->validation;
111 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
113 LAZY_LOAD(INFRQ_DESC, NULL);
114 return pkg->licenses;
117 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
119 LAZY_LOAD(INFRQ_DESC, NULL);
120 return pkg->groups;
123 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
125 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
126 return pkg->scriptlet;
129 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
131 LAZY_LOAD(INFRQ_DESC, NULL);
132 return pkg->depends;
135 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
137 LAZY_LOAD(INFRQ_DESC, NULL);
138 return pkg->optdepends;
141 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
143 LAZY_LOAD(INFRQ_DESC, NULL);
144 return pkg->conflicts;
147 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
149 LAZY_LOAD(INFRQ_DESC, NULL);
150 return pkg->provides;
153 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
155 LAZY_LOAD(INFRQ_DESC, NULL);
156 return pkg->replaces;
159 static alpm_filelist_t *_cache_get_files(alpm_pkg_t *pkg)
161 LAZY_LOAD(INFRQ_FILES, NULL);
162 return &(pkg->files);
165 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
167 LAZY_LOAD(INFRQ_FILES, NULL);
168 return pkg->backup;
172 * Open a package changelog for reading. Similar to fopen in functionality,
173 * except that the returned 'file stream' is from the database.
174 * @param pkg the package (from db) to read the changelog
175 * @return a 'file stream' to the package changelog
177 static void *_cache_changelog_open(alpm_pkg_t *pkg)
179 alpm_db_t *db = alpm_pkg_get_db(pkg);
180 char *clfile = _alpm_local_db_pkgpath(db, pkg, "changelog");
181 FILE *f = fopen(clfile, "r");
182 free(clfile);
183 return f;
187 * Read data from an open changelog 'file stream'. Similar to fread in
188 * functionality, this function takes a buffer and amount of data to read.
189 * @param ptr a buffer to fill with raw changelog data
190 * @param size the size of the buffer
191 * @param pkg the package that the changelog is being read from
192 * @param fp a 'file stream' to the package changelog
193 * @return the number of characters read, or 0 if there is no more data
195 static size_t _cache_changelog_read(void *ptr, size_t size,
196 const alpm_pkg_t UNUSED *pkg, void *fp)
198 return fread(ptr, 1, size, (FILE *)fp);
202 * Close a package changelog for reading. Similar to fclose in functionality,
203 * except that the 'file stream' is from the database.
204 * @param pkg the package that the changelog was read from
205 * @param fp a 'file stream' to the package changelog
206 * @return whether closing the package changelog stream was successful
208 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
210 return fclose((FILE *)fp);
213 static int _cache_force_load(alpm_pkg_t *pkg)
215 return local_db_read(pkg, INFRQ_ALL);
219 /** The local database operations struct. Get package fields through
220 * lazy accessor methods that handle any backend loading and caching
221 * logic.
223 static struct pkg_operations local_pkg_ops = {
224 .get_desc = _cache_get_desc,
225 .get_url = _cache_get_url,
226 .get_builddate = _cache_get_builddate,
227 .get_installdate = _cache_get_installdate,
228 .get_packager = _cache_get_packager,
229 .get_arch = _cache_get_arch,
230 .get_isize = _cache_get_isize,
231 .get_reason = _cache_get_reason,
232 .get_validation = _cache_get_validation,
233 .has_scriptlet = _cache_has_scriptlet,
234 .get_licenses = _cache_get_licenses,
235 .get_groups = _cache_get_groups,
236 .get_depends = _cache_get_depends,
237 .get_optdepends = _cache_get_optdepends,
238 .get_conflicts = _cache_get_conflicts,
239 .get_provides = _cache_get_provides,
240 .get_replaces = _cache_get_replaces,
241 .get_files = _cache_get_files,
242 .get_backup = _cache_get_backup,
244 .changelog_open = _cache_changelog_open,
245 .changelog_read = _cache_changelog_read,
246 .changelog_close = _cache_changelog_close,
248 .force_load = _cache_force_load,
251 static int checkdbdir(alpm_db_t *db)
253 struct stat buf;
254 const char *path = _alpm_db_path(db);
256 if(stat(path, &buf) != 0) {
257 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
258 path);
259 if(_alpm_makepath(path) != 0) {
260 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
262 } else if(!S_ISDIR(buf.st_mode)) {
263 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
264 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
265 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
268 return 0;
271 static int is_dir(const char *path, struct dirent *entry)
273 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
274 if(entry->d_type != DT_UNKNOWN) {
275 return (entry->d_type == DT_DIR);
277 #endif
279 char buffer[PATH_MAX];
280 struct stat sbuf;
282 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
284 if(!stat(buffer, &sbuf)) {
285 return S_ISDIR(sbuf.st_mode);
289 return 0;
292 static int local_db_validate(alpm_db_t *db)
294 struct dirent *ent = NULL;
295 const char *dbpath;
296 DIR *dbdir;
297 int ret = -1;
299 if(db->status & DB_STATUS_VALID) {
300 return 0;
302 if(db->status & DB_STATUS_INVALID) {
303 return -1;
306 dbpath = _alpm_db_path(db);
307 if(dbpath == NULL) {
308 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
310 dbdir = opendir(dbpath);
311 if(dbdir == NULL) {
312 if(errno == ENOENT) {
313 /* database dir doesn't exist yet */
314 db->status |= DB_STATUS_VALID;
315 db->status &= ~DB_STATUS_INVALID;
316 db->status &= ~DB_STATUS_EXISTS;
317 db->status |= DB_STATUS_MISSING;
318 return 0;
319 } else {
320 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
323 db->status |= DB_STATUS_EXISTS;
324 db->status &= ~DB_STATUS_MISSING;
326 while((ent = readdir(dbdir)) != NULL) {
327 const char *name = ent->d_name;
328 char path[PATH_MAX];
330 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
331 continue;
333 if(!is_dir(dbpath, ent)) {
334 continue;
337 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
338 if(access(path, F_OK) == 0) {
339 /* we found a depends file- bail */
340 db->status &= ~DB_STATUS_VALID;
341 db->status |= DB_STATUS_INVALID;
342 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
343 goto done;
346 /* we found no depends file after full scan */
347 db->status |= DB_STATUS_VALID;
348 db->status &= ~DB_STATUS_INVALID;
349 ret = 0;
351 done:
352 if(dbdir) {
353 closedir(dbdir);
356 return ret;
359 static int local_db_populate(alpm_db_t *db)
361 size_t est_count;
362 int count = 0;
363 struct stat buf;
364 struct dirent *ent = NULL;
365 const char *dbpath;
366 DIR *dbdir;
368 if(db->status & DB_STATUS_INVALID) {
369 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
371 /* note: DB_STATUS_MISSING is not fatal for local database */
373 dbpath = _alpm_db_path(db);
374 if(dbpath == NULL) {
375 /* pm_errno set in _alpm_db_path() */
376 return -1;
379 dbdir = opendir(dbpath);
380 if(dbdir == NULL) {
381 if(errno == ENOENT) {
382 /* no database existing yet is not an error */
383 db->status &= ~DB_STATUS_EXISTS;
384 db->status |= DB_STATUS_MISSING;
385 return 0;
387 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
389 if(fstat(dirfd(dbdir), &buf) != 0) {
390 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
392 db->status |= DB_STATUS_EXISTS;
393 db->status &= ~DB_STATUS_MISSING;
394 if(buf.st_nlink >= 2) {
395 est_count = buf.st_nlink;
396 } else {
397 /* Some filesystems don't subscribe to the two-implicit links school of
398 * thought, e.g. BTRFS, HFS+. See
399 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
401 est_count = 0;
402 while(readdir(dbdir) != NULL) {
403 est_count++;
405 rewinddir(dbdir);
407 if(est_count >= 2) {
408 /* subtract the '.' and '..' pointers to get # of children */
409 est_count -= 2;
412 db->pkgcache = _alpm_pkghash_create(est_count);
413 if(db->pkgcache == NULL){
414 closedir(dbdir);
415 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
418 while((ent = readdir(dbdir)) != NULL) {
419 const char *name = ent->d_name;
421 alpm_pkg_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, ALPM_ERR_MEMORY, -1);
435 /* split the db entry name */
436 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
437 &(pkg->name_hash)) != 0) {
438 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
439 name);
440 _alpm_pkg_free(pkg);
441 continue;
444 /* duplicated database entries are not allowed */
445 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
446 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
447 _alpm_pkg_free(pkg);
448 continue;
451 pkg->origin = ALPM_PKG_FROM_LOCALDB;
452 pkg->origin_data.db = db;
453 pkg->ops = &local_pkg_ops;
454 pkg->handle = db->handle;
456 /* explicitly read with only 'BASE' data, accessors will handle the rest */
457 if(local_db_read(pkg, INFRQ_BASE) == -1) {
458 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
459 _alpm_pkg_free(pkg);
460 continue;
463 /* add to the collection */
464 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
465 pkg->name, db->treename);
466 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
467 count++;
470 closedir(dbdir);
471 if(count > 0) {
472 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
474 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
475 count, db->treename);
477 return count;
480 /* Note: the return value must be freed by the caller */
481 char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
482 const char *filename)
484 size_t len;
485 char *pkgpath;
486 const char *dbpath;
488 dbpath = _alpm_db_path(db);
489 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
490 len += filename ? strlen(filename) : 0;
491 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
492 sprintf(pkgpath, "%s%s-%s/%s", dbpath, info->name, info->version,
493 filename ? filename : "");
494 return pkgpath;
497 #define READ_NEXT() do { \
498 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
499 _alpm_strip_newline(line, 0); \
500 } while(0)
502 #define READ_AND_STORE(f) do { \
503 READ_NEXT(); \
504 STRDUP(f, line, goto error); \
505 } while(0)
507 #define READ_AND_STORE_ALL(f) do { \
508 char *linedup; \
509 if(fgets(line, sizeof(line), fp) == NULL) {\
510 if(!feof(fp)) goto error; else break; \
512 if(_alpm_strip_newline(line, 0) == 0) break; \
513 STRDUP(linedup, line, goto error); \
514 f = alpm_list_add(f, linedup); \
515 } while(1) /* note the while(1) and not (0) */
517 #define READ_AND_SPLITDEP(f) do { \
518 if(fgets(line, sizeof(line), fp) == NULL) {\
519 if(!feof(fp)) goto error; else break; \
521 if(_alpm_strip_newline(line, 0) == 0) break; \
522 f = alpm_list_add(f, _alpm_splitdep(line)); \
523 } while(1) /* note the while(1) and not (0) */
525 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
527 FILE *fp = NULL;
528 char line[1024];
529 alpm_db_t *db = info->origin_data.db;
531 /* bitmask logic here:
532 * infolevel: 00001111
533 * inforeq: 00010100
534 * & result: 00000100
535 * == to inforeq? nope, we need to load more info. */
536 if((info->infolevel & inforeq) == inforeq) {
537 /* already loaded all of this info, do nothing */
538 return 0;
541 if(info->infolevel & INFRQ_ERROR) {
542 /* We've encountered an error loading this package before. Don't attempt
543 * repeated reloads, just give up. */
544 return -1;
547 _alpm_log(db->handle, ALPM_LOG_FUNCTION,
548 "loading package data for %s : level=0x%x\n",
549 info->name, inforeq);
551 /* clear out 'line', to be certain - and to make valgrind happy */
552 memset(line, 0, sizeof(line));
554 /* DESC */
555 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
556 char *path = _alpm_local_db_pkgpath(db, info, "desc");
557 if(!path || (fp = fopen(path, "r")) == NULL) {
558 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
559 free(path);
560 goto error;
562 free(path);
563 while(!feof(fp)) {
564 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
565 goto error;
567 if(_alpm_strip_newline(line, 0) == 0) {
568 /* length of stripped line was zero */
569 continue;
571 if(strcmp(line, "%NAME%") == 0) {
572 READ_NEXT();
573 if(strcmp(line, info->name) != 0) {
574 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
575 "mismatch on package %s\n"), db->treename, info->name);
577 } else if(strcmp(line, "%VERSION%") == 0) {
578 READ_NEXT();
579 if(strcmp(line, info->version) != 0) {
580 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
581 "mismatch on package %s\n"), db->treename, info->name);
583 } else if(strcmp(line, "%DESC%") == 0) {
584 READ_AND_STORE(info->desc);
585 } else if(strcmp(line, "%GROUPS%") == 0) {
586 READ_AND_STORE_ALL(info->groups);
587 } else if(strcmp(line, "%URL%") == 0) {
588 READ_AND_STORE(info->url);
589 } else if(strcmp(line, "%LICENSE%") == 0) {
590 READ_AND_STORE_ALL(info->licenses);
591 } else if(strcmp(line, "%ARCH%") == 0) {
592 READ_AND_STORE(info->arch);
593 } else if(strcmp(line, "%BUILDDATE%") == 0) {
594 READ_NEXT();
595 info->builddate = _alpm_parsedate(line);
596 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
597 READ_NEXT();
598 info->installdate = _alpm_parsedate(line);
599 } else if(strcmp(line, "%PACKAGER%") == 0) {
600 READ_AND_STORE(info->packager);
601 } else if(strcmp(line, "%REASON%") == 0) {
602 READ_NEXT();
603 info->reason = (alpm_pkgreason_t)atoi(line);
604 } else if(strcmp(line, "%VALIDATION%") == 0) {
605 alpm_list_t *i, *v = NULL;
606 READ_AND_STORE_ALL(v);
607 for(i = v; i; i = alpm_list_next(i))
609 if(strcmp(i->data, "none") == 0) {
610 info->validation |= ALPM_PKG_VALIDATION_NONE;
611 } else if(strcmp(i->data, "md5") == 0) {
612 info->validation |= ALPM_PKG_VALIDATION_MD5SUM;
613 } else if(strcmp(i->data, "sha256") == 0) {
614 info->validation |= ALPM_PKG_VALIDATION_SHA256SUM;
615 } else if(strcmp(i->data, "pgp") == 0) {
616 info->validation |= ALPM_PKG_VALIDATION_SIGNATURE;
617 } else {
618 _alpm_log(db->handle, ALPM_LOG_WARNING,
619 _("unknown validation type for package %s: %s\n"),
620 info->name, (const char *)i->data);
623 FREELIST(v);
624 } else if(strcmp(line, "%SIZE%") == 0) {
625 READ_NEXT();
626 info->isize = _alpm_strtoofft(line);
627 } else if(strcmp(line, "%REPLACES%") == 0) {
628 READ_AND_SPLITDEP(info->replaces);
629 } else if(strcmp(line, "%DEPENDS%") == 0) {
630 READ_AND_SPLITDEP(info->depends);
631 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
632 READ_AND_SPLITDEP(info->optdepends);
633 } else if(strcmp(line, "%CONFLICTS%") == 0) {
634 READ_AND_SPLITDEP(info->conflicts);
635 } else if(strcmp(line, "%PROVIDES%") == 0) {
636 READ_AND_SPLITDEP(info->provides);
639 fclose(fp);
640 fp = NULL;
641 info->infolevel |= INFRQ_DESC;
644 /* FILES */
645 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
646 char *path = _alpm_local_db_pkgpath(db, info, "files");
647 if(!path || (fp = fopen(path, "r")) == NULL) {
648 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
649 free(path);
650 goto error;
652 free(path);
653 while(fgets(line, sizeof(line), fp)) {
654 _alpm_strip_newline(line, 0);
655 if(strcmp(line, "%FILES%") == 0) {
656 size_t files_count = 0, files_size = 0, len;
657 alpm_file_t *files = NULL;
659 while(fgets(line, sizeof(line), fp) &&
660 (len = _alpm_strip_newline(line, 0))) {
661 if(files_count >= files_size) {
662 size_t old_size = files_size;
663 if(files_size == 0) {
664 files_size = 8;
665 } else {
666 files_size *= 2;
668 files = realloc(files, sizeof(alpm_file_t) * files_size);
669 if(!files) {
670 _alpm_alloc_fail(sizeof(alpm_file_t) * files_size);
671 goto error;
673 /* ensure all new memory is zeroed out, in both the initial
674 * allocation and later reallocs */
675 memset(files + old_size, 0,
676 sizeof(alpm_file_t) * (files_size - old_size));
678 /* since we know the length of the file string already,
679 * we can do malloc + memcpy rather than strdup */
680 len += 1;
681 files[files_count].name = malloc(len);
682 if(files[files_count].name == NULL) {
683 _alpm_alloc_fail(len);
684 goto error;
686 memcpy(files[files_count].name, line, len);
687 files_count++;
689 /* attempt to hand back any memory we don't need */
690 files = realloc(files, sizeof(alpm_file_t) * files_count);
691 info->files.count = files_count;
692 info->files.files = files;
693 } else if(strcmp(line, "%BACKUP%") == 0) {
694 while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line, 0)) {
695 alpm_backup_t *backup;
696 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
697 if(_alpm_split_backup(line, &backup)) {
698 goto error;
700 info->backup = alpm_list_add(info->backup, backup);
704 fclose(fp);
705 fp = NULL;
706 info->infolevel |= INFRQ_FILES;
709 /* INSTALL */
710 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
711 char *path = _alpm_local_db_pkgpath(db, info, "install");
712 if(access(path, F_OK) == 0) {
713 info->scriptlet = 1;
715 free(path);
716 info->infolevel |= INFRQ_SCRIPTLET;
719 return 0;
721 error:
722 info->infolevel |= INFRQ_ERROR;
723 if(fp) {
724 fclose(fp);
726 return -1;
729 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
731 mode_t oldmask;
732 int retval = 0;
733 char *pkgpath;
735 if(checkdbdir(db) != 0) {
736 return -1;
739 oldmask = umask(0000);
740 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
742 if((retval = mkdir(pkgpath, 0755)) != 0) {
743 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not create directory %s: %s\n"),
744 pkgpath, strerror(errno));
747 free(pkgpath);
748 umask(oldmask);
750 return retval;
753 static void write_deps(FILE *fp, const char *header, alpm_list_t *deplist)
755 alpm_list_t *lp;
756 if(!deplist) {
757 return;
759 fputs(header, fp);
760 fputc('\n', fp);
761 for(lp = deplist; lp; lp = lp->next) {
762 char *depstring = alpm_dep_compute_string(lp->data);
763 fputs(depstring, fp);
764 fputc('\n', fp);
765 free(depstring);
767 fputc('\n', fp);
770 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
772 FILE *fp = NULL;
773 mode_t oldmask;
774 alpm_list_t *lp;
775 int retval = 0;
777 if(db == NULL || info == NULL || !(db->status & DB_STATUS_LOCAL)) {
778 return -1;
781 /* make sure we have a sane umask */
782 oldmask = umask(0022);
784 /* DESC */
785 if(inforeq & INFRQ_DESC) {
786 char *path;
787 _alpm_log(db->handle, ALPM_LOG_DEBUG,
788 "writing %s-%s DESC information back to db\n",
789 info->name, info->version);
790 path = _alpm_local_db_pkgpath(db, info, "desc");
791 if(!path || (fp = fopen(path, "w")) == NULL) {
792 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
793 path, strerror(errno));
794 retval = -1;
795 free(path);
796 goto cleanup;
798 free(path);
799 fprintf(fp, "%%NAME%%\n%s\n\n"
800 "%%VERSION%%\n%s\n\n", info->name, info->version);
801 if(info->desc) {
802 fprintf(fp, "%%DESC%%\n"
803 "%s\n\n", info->desc);
805 if(info->url) {
806 fprintf(fp, "%%URL%%\n"
807 "%s\n\n", info->url);
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 "%jd\n\n", (intmax_t)info->builddate);
817 if(info->installdate) {
818 fprintf(fp, "%%INSTALLDATE%%\n"
819 "%jd\n\n", (intmax_t)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->groups) {
835 fputs("%GROUPS%\n", fp);
836 for(lp = info->groups; lp; lp = lp->next) {
837 fputs(lp->data, fp);
838 fputc('\n', fp);
840 fputc('\n', fp);
842 if(info->licenses) {
843 fputs("%LICENSE%\n", fp);
844 for(lp = info->licenses; lp; lp = lp->next) {
845 fputs(lp->data, fp);
846 fputc('\n', fp);
848 fputc('\n', fp);
850 if(info->validation) {
851 fputs("%VALIDATION%\n", fp);
852 if(info->validation & ALPM_PKG_VALIDATION_NONE) {
853 fputs("none\n", fp);
855 if(info->validation & ALPM_PKG_VALIDATION_MD5SUM) {
856 fputs("md5\n", fp);
858 if(info->validation & ALPM_PKG_VALIDATION_SHA256SUM) {
859 fputs("sha256\n", fp);
861 if(info->validation & ALPM_PKG_VALIDATION_SIGNATURE) {
862 fputs("pgp\n", fp);
864 fputc('\n', fp);
867 write_deps(fp, "%REPLACES%", info->replaces);
868 write_deps(fp, "%DEPENDS%", info->depends);
869 write_deps(fp, "%OPTDEPENDS%", info->optdepends);
870 write_deps(fp, "%CONFLICTS%", info->conflicts);
871 write_deps(fp, "%PROVIDES%", info->provides);
873 fclose(fp);
874 fp = NULL;
877 /* FILES */
878 if(inforeq & INFRQ_FILES) {
879 char *path;
880 _alpm_log(db->handle, ALPM_LOG_DEBUG,
881 "writing %s-%s FILES information back to db\n",
882 info->name, info->version);
883 path = _alpm_local_db_pkgpath(db, info, "files");
884 if(!path || (fp = fopen(path, "w")) == NULL) {
885 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
886 path, strerror(errno));
887 retval = -1;
888 free(path);
889 goto cleanup;
891 free(path);
892 if(info->files.count) {
893 size_t i;
894 fputs("%FILES%\n", fp);
895 for(i = 0; i < info->files.count; i++) {
896 const alpm_file_t *file = info->files.files + i;
897 fputs(file->name, fp);
898 fputc('\n', fp);
900 fputc('\n', fp);
902 if(info->backup) {
903 fputs("%BACKUP%\n", fp);
904 for(lp = info->backup; lp; lp = lp->next) {
905 const alpm_backup_t *backup = lp->data;
906 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
908 fputc('\n', fp);
910 fclose(fp);
911 fp = NULL;
914 /* INSTALL */
915 /* nothing needed here (script is automatically extracted) */
917 cleanup:
918 umask(oldmask);
920 if(fp) {
921 fclose(fp);
924 return retval;
927 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
929 int ret = 0;
930 DIR *dirp;
931 struct dirent *dp;
932 char *pkgpath;
933 size_t pkgpath_len;
935 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
936 if(!pkgpath) {
937 return -1;
939 pkgpath_len = strlen(pkgpath);
941 dirp = opendir(pkgpath);
942 if(!dirp) {
943 return -1;
945 /* go through the local DB entry, removing the files within, which we know
946 * are not nested directories of any kind. */
947 for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
948 if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
949 char name[PATH_MAX];
950 if(pkgpath_len + strlen(dp->d_name) + 2 > PATH_MAX) {
951 /* file path is too long to remove, hmm. */
952 ret = -1;
953 } else {
954 sprintf(name, "%s/%s", pkgpath, dp->d_name);
955 if(unlink(name)) {
956 ret = -1;
961 closedir(dirp);
963 /* after removing all enclosed files, we can remove the directory itself. */
964 if(rmdir(pkgpath)) {
965 ret = -1;
967 free(pkgpath);
968 return ret;
971 int SYMEXPORT alpm_pkg_set_reason(alpm_pkg_t *pkg, alpm_pkgreason_t reason)
973 ASSERT(pkg != NULL, return -1);
974 ASSERT(pkg->origin == ALPM_PKG_FROM_LOCALDB,
975 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
976 ASSERT(pkg->origin_data.db == pkg->handle->db_local,
977 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
979 _alpm_log(pkg->handle, ALPM_LOG_DEBUG,
980 "setting install reason %u for %s\n", reason, pkg->name);
981 if(alpm_pkg_get_reason(pkg) == reason) {
982 /* we are done */
983 return 0;
985 /* set reason (in pkgcache) */
986 pkg->reason = reason;
987 /* write DESC */
988 if(_alpm_local_db_write(pkg->handle->db_local, pkg, INFRQ_DESC)) {
989 RET_ERR(pkg->handle, ALPM_ERR_DB_WRITE, -1);
992 return 0;
995 struct db_operations local_db_ops = {
996 .validate = local_db_validate,
997 .populate = local_db_populate,
998 .unregister = _alpm_db_unregister,
1001 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
1003 alpm_db_t *db;
1005 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
1007 db = _alpm_db_new("local", 1);
1008 if(db == NULL) {
1009 handle->pm_errno = ALPM_ERR_DB_CREATE;
1010 return NULL;
1012 db->ops = &local_db_ops;
1013 db->handle = handle;
1015 if(local_db_validate(db)) {
1016 /* pm_errno set in local_db_validate() */
1017 _alpm_db_free(db);
1018 return NULL;
1021 handle->db_local = db;
1022 return db;
1025 /* vim: set ts=2 sw=2 noet: */