Hook new optdepend structures up
[pacman-ng.git] / lib / libalpm / be_local.c
blob0d423fa76937d41eb25b24a70735858bbf8c5a5d
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_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
107 LAZY_LOAD(INFRQ_DESC, NULL);
108 return pkg->licenses;
111 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
113 LAZY_LOAD(INFRQ_DESC, NULL);
114 return pkg->groups;
117 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
119 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
120 return pkg->scriptlet;
123 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
125 LAZY_LOAD(INFRQ_DESC, NULL);
126 return pkg->depends;
129 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
131 LAZY_LOAD(INFRQ_DESC, NULL);
132 return pkg->optdepends;
135 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
137 LAZY_LOAD(INFRQ_DESC, NULL);
138 return pkg->conflicts;
141 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
143 LAZY_LOAD(INFRQ_DESC, NULL);
144 return pkg->provides;
147 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
149 LAZY_LOAD(INFRQ_DESC, NULL);
150 return pkg->replaces;
153 static alpm_filelist_t *_cache_get_files(alpm_pkg_t *pkg)
155 LAZY_LOAD(INFRQ_FILES, NULL);
156 return &(pkg->files);
159 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
161 LAZY_LOAD(INFRQ_FILES, NULL);
162 return pkg->backup;
166 * Open a package changelog for reading. Similar to fopen in functionality,
167 * except that the returned 'file stream' is from the database.
168 * @param pkg the package (from db) to read the changelog
169 * @return a 'file stream' to the package changelog
171 static void *_cache_changelog_open(alpm_pkg_t *pkg)
173 alpm_db_t *db = alpm_pkg_get_db(pkg);
174 char *clfile = _alpm_local_db_pkgpath(db, pkg, "changelog");
175 FILE *f = fopen(clfile, "r");
176 free(clfile);
177 return f;
181 * Read data from an open changelog 'file stream'. Similar to fread in
182 * functionality, this function takes a buffer and amount of data to read.
183 * @param ptr a buffer to fill with raw changelog data
184 * @param size the size of the buffer
185 * @param pkg the package that the changelog is being read from
186 * @param fp a 'file stream' to the package changelog
187 * @return the number of characters read, or 0 if there is no more data
189 static size_t _cache_changelog_read(void *ptr, size_t size,
190 const alpm_pkg_t UNUSED *pkg, void *fp)
192 return fread(ptr, 1, size, (FILE *)fp);
196 * Close a package changelog for reading. Similar to fclose in functionality,
197 * except that the 'file stream' is from the database.
198 * @param pkg the package that the changelog was read from
199 * @param fp a 'file stream' to the package changelog
200 * @return whether closing the package changelog stream was successful
202 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
204 return fclose((FILE *)fp);
207 static int _cache_force_load(alpm_pkg_t *pkg)
209 return local_db_read(pkg, INFRQ_ALL);
213 /** The local database operations struct. Get package fields through
214 * lazy accessor methods that handle any backend loading and caching
215 * logic.
217 static struct pkg_operations local_pkg_ops = {
218 .get_desc = _cache_get_desc,
219 .get_url = _cache_get_url,
220 .get_builddate = _cache_get_builddate,
221 .get_installdate = _cache_get_installdate,
222 .get_packager = _cache_get_packager,
223 .get_arch = _cache_get_arch,
224 .get_isize = _cache_get_isize,
225 .get_reason = _cache_get_reason,
226 .has_scriptlet = _cache_has_scriptlet,
227 .get_licenses = _cache_get_licenses,
228 .get_groups = _cache_get_groups,
229 .get_depends = _cache_get_depends,
230 .get_optdepends = _cache_get_optdepends,
231 .get_conflicts = _cache_get_conflicts,
232 .get_provides = _cache_get_provides,
233 .get_replaces = _cache_get_replaces,
234 .get_files = _cache_get_files,
235 .get_backup = _cache_get_backup,
237 .changelog_open = _cache_changelog_open,
238 .changelog_read = _cache_changelog_read,
239 .changelog_close = _cache_changelog_close,
241 .force_load = _cache_force_load,
244 static int checkdbdir(alpm_db_t *db)
246 struct stat buf;
247 const char *path = _alpm_db_path(db);
249 if(stat(path, &buf) != 0) {
250 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
251 path);
252 if(_alpm_makepath(path) != 0) {
253 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
255 } else if(!S_ISDIR(buf.st_mode)) {
256 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
257 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
258 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
261 return 0;
264 static int is_dir(const char *path, struct dirent *entry)
266 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
267 if(entry->d_type != DT_UNKNOWN) {
268 return (entry->d_type == DT_DIR);
270 #endif
272 char buffer[PATH_MAX];
273 struct stat sbuf;
275 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
277 if(!stat(buffer, &sbuf)) {
278 return S_ISDIR(sbuf.st_mode);
282 return 0;
285 static int local_db_validate(alpm_db_t *db)
287 struct dirent *ent = NULL;
288 const char *dbpath;
289 DIR *dbdir;
290 int ret = -1;
292 if(db->status & DB_STATUS_VALID) {
293 return 0;
295 if(db->status & DB_STATUS_INVALID) {
296 return -1;
299 dbpath = _alpm_db_path(db);
300 if(dbpath == NULL) {
301 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
303 dbdir = opendir(dbpath);
304 if(dbdir == NULL) {
305 if(errno == ENOENT) {
306 /* database dir doesn't exist yet */
307 db->status |= DB_STATUS_VALID;
308 db->status &= ~DB_STATUS_INVALID;
309 db->status &= ~DB_STATUS_EXISTS;
310 db->status |= DB_STATUS_MISSING;
311 return 0;
312 } else {
313 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
316 db->status |= DB_STATUS_EXISTS;
317 db->status &= ~DB_STATUS_MISSING;
319 while((ent = readdir(dbdir)) != NULL) {
320 const char *name = ent->d_name;
321 char path[PATH_MAX];
323 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
324 continue;
326 if(!is_dir(dbpath, ent)) {
327 continue;
330 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
331 if(access(path, F_OK) == 0) {
332 /* we found a depends file- bail */
333 db->status &= ~DB_STATUS_VALID;
334 db->status |= DB_STATUS_INVALID;
335 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
336 goto done;
339 /* we found no depends file after full scan */
340 db->status |= DB_STATUS_VALID;
341 db->status &= ~DB_STATUS_INVALID;
342 ret = 0;
344 done:
345 if(dbdir) {
346 closedir(dbdir);
349 return ret;
352 static int local_db_populate(alpm_db_t *db)
354 size_t est_count;
355 int count = 0;
356 struct stat buf;
357 struct dirent *ent = NULL;
358 const char *dbpath;
359 DIR *dbdir;
361 if(db->status & DB_STATUS_INVALID) {
362 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
364 /* note: DB_STATUS_MISSING is not fatal for local database */
366 dbpath = _alpm_db_path(db);
367 if(dbpath == NULL) {
368 /* pm_errno set in _alpm_db_path() */
369 return -1;
372 dbdir = opendir(dbpath);
373 if(dbdir == NULL) {
374 if(errno == ENOENT) {
375 /* no database existing yet is not an error */
376 db->status &= ~DB_STATUS_EXISTS;
377 db->status |= DB_STATUS_MISSING;
378 return 0;
380 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
382 if(fstat(dirfd(dbdir), &buf) != 0) {
383 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
385 db->status |= DB_STATUS_EXISTS;
386 db->status &= ~DB_STATUS_MISSING;
387 if(buf.st_nlink >= 2) {
388 est_count = buf.st_nlink;
389 } else {
390 /* Some filesystems don't subscribe to the two-implicit links school of
391 * thought, e.g. BTRFS, HFS+. See
392 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
394 est_count = 0;
395 while(readdir(dbdir) != NULL) {
396 est_count++;
398 rewinddir(dbdir);
400 if(est_count >= 2) {
401 /* subtract the two extra pointers to get # of children */
402 est_count -= 2;
405 db->pkgcache = _alpm_pkghash_create(est_count);
406 if(db->pkgcache == NULL){
407 closedir(dbdir);
408 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
411 while((ent = readdir(dbdir)) != NULL) {
412 const char *name = ent->d_name;
414 alpm_pkg_t *pkg;
416 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
417 continue;
419 if(!is_dir(dbpath, ent)) {
420 continue;
423 pkg = _alpm_pkg_new();
424 if(pkg == NULL) {
425 closedir(dbdir);
426 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
428 /* split the db entry name */
429 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
430 &(pkg->name_hash)) != 0) {
431 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
432 name);
433 _alpm_pkg_free(pkg);
434 continue;
437 /* duplicated database entries are not allowed */
438 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
439 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
440 _alpm_pkg_free(pkg);
441 continue;
444 pkg->origin = PKG_FROM_LOCALDB;
445 pkg->origin_data.db = db;
446 pkg->ops = &local_pkg_ops;
447 pkg->handle = db->handle;
449 /* explicitly read with only 'BASE' data, accessors will handle the rest */
450 if(local_db_read(pkg, INFRQ_BASE) == -1) {
451 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
452 _alpm_pkg_free(pkg);
453 continue;
456 /* add to the collection */
457 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
458 pkg->name, db->treename);
459 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
460 count++;
463 closedir(dbdir);
464 if(count > 0) {
465 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
467 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
468 count, db->treename);
470 return count;
473 /* Note: the return value must be freed by the caller */
474 char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
475 const char *filename)
477 size_t len;
478 char *pkgpath;
479 const char *dbpath;
481 dbpath = _alpm_db_path(db);
482 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
483 len += filename ? strlen(filename) : 0;
484 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
485 sprintf(pkgpath, "%s%s-%s/%s", dbpath, info->name, info->version,
486 filename ? filename : "");
487 return pkgpath;
490 #define READ_NEXT() do { \
491 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
492 _alpm_strip_newline(line); \
493 } while(0)
495 #define READ_AND_STORE(f) do { \
496 READ_NEXT(); \
497 STRDUP(f, line, goto error); \
498 } while(0)
500 #define READ_AND_STORE_ALL(f) do { \
501 char *linedup; \
502 if(fgets(line, sizeof(line), fp) == NULL) {\
503 if(!feof(fp)) goto error; else break; \
505 if(_alpm_strip_newline(line) == 0) break; \
506 STRDUP(linedup, line, goto error); \
507 f = alpm_list_add(f, linedup); \
508 } while(1) /* note the while(1) and not (0) */
510 #define READ_AND_SPLITDEP(f) do { \
511 if(fgets(line, sizeof(line), fp) == NULL) {\
512 if(!feof(fp)) goto error; else break; \
514 if(_alpm_strip_newline(line) == 0) break; \
515 f = alpm_list_add(f, _alpm_splitdep(line)); \
516 } while(1) /* note the while(1) and not (0) */
518 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
520 FILE *fp = NULL;
521 char line[1024];
522 char *pkgpath;
523 alpm_db_t *db = info->origin_data.db;
525 /* bitmask logic here:
526 * infolevel: 00001111
527 * inforeq: 00010100
528 * & result: 00000100
529 * == to inforeq? nope, we need to load more info. */
530 if((info->infolevel & inforeq) == inforeq) {
531 /* already loaded all of this info, do nothing */
532 return 0;
535 if(info->infolevel & INFRQ_ERROR) {
536 /* We've encountered an error loading this package before. Don't attempt
537 * repeated reloads, just give up. */
538 return -1;
541 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
542 info->name, inforeq);
544 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
545 if(!pkgpath || access(pkgpath, F_OK)) {
546 /* directory doesn't exist or can't be opened */
547 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
548 info->name, info->version, db->treename);
549 goto error;
551 free(pkgpath);
553 /* clear out 'line', to be certain - and to make valgrind happy */
554 memset(line, 0, sizeof(line));
556 /* DESC */
557 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
558 char *path = _alpm_local_db_pkgpath(db, info, "desc");
559 if(!path || (fp = fopen(path, "r")) == NULL) {
560 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
561 free(path);
562 goto error;
564 free(path);
565 while(!feof(fp)) {
566 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
567 goto error;
569 if(_alpm_strip_newline(line) == 0) {
570 /* length of stripped line was zero */
571 continue;
573 if(strcmp(line, "%NAME%") == 0) {
574 READ_NEXT();
575 if(strcmp(line, info->name) != 0) {
576 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
577 "mismatch on package %s\n"), db->treename, info->name);
579 } else if(strcmp(line, "%VERSION%") == 0) {
580 READ_NEXT();
581 if(strcmp(line, info->version) != 0) {
582 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
583 "mismatch on package %s\n"), db->treename, info->name);
585 } else if(strcmp(line, "%DESC%") == 0) {
586 READ_AND_STORE(info->desc);
587 } else if(strcmp(line, "%GROUPS%") == 0) {
588 READ_AND_STORE_ALL(info->groups);
589 } else if(strcmp(line, "%URL%") == 0) {
590 READ_AND_STORE(info->url);
591 } else if(strcmp(line, "%LICENSE%") == 0) {
592 READ_AND_STORE_ALL(info->licenses);
593 } else if(strcmp(line, "%ARCH%") == 0) {
594 READ_AND_STORE(info->arch);
595 } else if(strcmp(line, "%BUILDDATE%") == 0) {
596 READ_NEXT();
597 info->builddate = _alpm_parsedate(line);
598 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
599 READ_NEXT();
600 info->installdate = _alpm_parsedate(line);
601 } else if(strcmp(line, "%PACKAGER%") == 0) {
602 READ_AND_STORE(info->packager);
603 } else if(strcmp(line, "%REASON%") == 0) {
604 READ_NEXT();
605 info->reason = (alpm_pkgreason_t)atoi(line);
606 } else if(strcmp(line, "%SIZE%") == 0) {
607 READ_NEXT();
608 info->isize = _alpm_strtoofft(line);
609 } else if(strcmp(line, "%REPLACES%") == 0) {
610 READ_AND_SPLITDEP(info->replaces);
611 } else if(strcmp(line, "%DEPENDS%") == 0) {
612 READ_AND_SPLITDEP(info->depends);
613 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
614 READ_AND_SPLITDEP(info->optdepends);
615 } else if(strcmp(line, "%CONFLICTS%") == 0) {
616 READ_AND_SPLITDEP(info->conflicts);
617 } else if(strcmp(line, "%PROVIDES%") == 0) {
618 READ_AND_SPLITDEP(info->provides);
621 fclose(fp);
622 fp = NULL;
623 info->infolevel |= INFRQ_DESC;
626 /* FILES */
627 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
628 char *path = _alpm_local_db_pkgpath(db, info, "files");
629 if(!path || (fp = fopen(path, "r")) == NULL) {
630 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
631 free(path);
632 goto error;
634 free(path);
635 while(fgets(line, sizeof(line), fp)) {
636 _alpm_strip_newline(line);
637 if(strcmp(line, "%FILES%") == 0) {
638 size_t files_count = 0, files_size = 0, len;
639 alpm_file_t *files = NULL;
641 while(fgets(line, sizeof(line), fp) &&
642 (len = _alpm_strip_newline(line))) {
643 if(files_count >= files_size) {
644 size_t old_size = files_size;
645 if(files_size == 0) {
646 files_size = 8;
647 } else {
648 files_size *= 2;
650 files = realloc(files, sizeof(alpm_file_t) * files_size);
651 if(!files) {
652 ALLOC_FAIL(sizeof(alpm_file_t) * files_size);
653 goto error;
655 /* ensure all new memory is zeroed out, in both the initial
656 * allocation and later reallocs */
657 memset(files + old_size, 0,
658 sizeof(alpm_file_t) * (files_size - old_size));
660 /* since we know the length of the file string already,
661 * we can do malloc + memcpy rather than strdup */
662 files[files_count].name = malloc(len + 1);
663 if(files[files_count].name == NULL) {
664 ALLOC_FAIL(len);
665 goto error;
667 memcpy(files[files_count].name, line, len + 1);
668 files_count++;
670 /* attempt to hand back any memory we don't need */
671 files = realloc(files, sizeof(alpm_file_t) * files_count);
672 info->files.count = files_count;
673 info->files.files = files;
674 } else if(strcmp(line, "%BACKUP%") == 0) {
675 while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line)) {
676 alpm_backup_t *backup;
677 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
678 if(_alpm_split_backup(line, &backup)) {
679 goto error;
681 info->backup = alpm_list_add(info->backup, backup);
685 fclose(fp);
686 fp = NULL;
687 info->infolevel |= INFRQ_FILES;
690 /* INSTALL */
691 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
692 char *path = _alpm_local_db_pkgpath(db, info, "install");
693 if(access(path, F_OK) == 0) {
694 info->scriptlet = 1;
696 free(path);
697 info->infolevel |= INFRQ_SCRIPTLET;
700 return 0;
702 error:
703 info->infolevel |= INFRQ_ERROR;
704 if(fp) {
705 fclose(fp);
707 return -1;
710 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
712 mode_t oldmask;
713 int retval = 0;
714 char *pkgpath;
716 if(checkdbdir(db) != 0) {
717 return -1;
720 oldmask = umask(0000);
721 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
723 if((retval = mkdir(pkgpath, 0755)) != 0) {
724 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not create directory %s: %s\n"),
725 pkgpath, strerror(errno));
728 free(pkgpath);
729 umask(oldmask);
731 return retval;
734 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
736 FILE *fp = NULL;
737 mode_t oldmask;
738 alpm_list_t *lp;
739 int retval = 0;
741 if(db == NULL || info == NULL || !(db->status & DB_STATUS_LOCAL)) {
742 return -1;
745 /* make sure we have a sane umask */
746 oldmask = umask(0022);
748 /* DESC */
749 if(inforeq & INFRQ_DESC) {
750 char *path;
751 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
752 info->name, info->version);
753 path = _alpm_local_db_pkgpath(db, info, "desc");
754 if(!path || (fp = fopen(path, "w")) == NULL) {
755 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
756 path, strerror(errno));
757 retval = -1;
758 free(path);
759 goto cleanup;
761 free(path);
762 fprintf(fp, "%%NAME%%\n%s\n\n"
763 "%%VERSION%%\n%s\n\n", info->name, info->version);
764 if(info->desc) {
765 fprintf(fp, "%%DESC%%\n"
766 "%s\n\n", info->desc);
768 if(info->groups) {
769 fputs("%GROUPS%\n", fp);
770 for(lp = info->groups; lp; lp = lp->next) {
771 fprintf(fp, "%s\n", (char *)lp->data);
773 fprintf(fp, "\n");
775 if(info->replaces) {
776 fputs("%REPLACES%\n", fp);
777 for(lp = info->replaces; lp; lp = lp->next) {
778 char *depstring = alpm_dep_compute_string(lp->data);
779 fprintf(fp, "%s\n", depstring);
780 free(depstring);
782 fprintf(fp, "\n");
784 if(info->url) {
785 fprintf(fp, "%%URL%%\n"
786 "%s\n\n", info->url);
788 if(info->licenses) {
789 fputs("%LICENSE%\n", fp);
790 for(lp = info->licenses; lp; lp = lp->next) {
791 fprintf(fp, "%s\n", (char *)lp->data);
793 fprintf(fp, "\n");
795 if(info->arch) {
796 fprintf(fp, "%%ARCH%%\n"
797 "%s\n\n", info->arch);
799 if(info->builddate) {
800 fprintf(fp, "%%BUILDDATE%%\n"
801 "%jd\n\n", (intmax_t)info->builddate);
803 if(info->installdate) {
804 fprintf(fp, "%%INSTALLDATE%%\n"
805 "%jd\n\n", (intmax_t)info->installdate);
807 if(info->packager) {
808 fprintf(fp, "%%PACKAGER%%\n"
809 "%s\n\n", info->packager);
811 if(info->isize) {
812 /* only write installed size, csize is irrelevant once installed */
813 fprintf(fp, "%%SIZE%%\n"
814 "%jd\n\n", (intmax_t)info->isize);
816 if(info->reason) {
817 fprintf(fp, "%%REASON%%\n"
818 "%u\n\n", info->reason);
820 if(info->depends) {
821 fputs("%DEPENDS%\n", fp);
822 for(lp = info->depends; lp; lp = lp->next) {
823 char *depstring = alpm_dep_compute_string(lp->data);
824 fprintf(fp, "%s\n", depstring);
825 free(depstring);
827 fprintf(fp, "\n");
829 if(info->optdepends) {
830 fputs("%OPTDEPENDS%\n", fp);
831 for(lp = info->optdepends; lp; lp = lp->next) {
832 char *optstring = alpm_dep_compute_string(lp->data);
833 fprintf(fp, "%s\n", optstring);
834 free(optstring);
836 fprintf(fp, "\n");
838 if(info->conflicts) {
839 fputs("%CONFLICTS%\n", fp);
840 for(lp = info->conflicts; lp; lp = lp->next) {
841 char *depstring = alpm_dep_compute_string(lp->data);
842 fprintf(fp, "%s\n", depstring);
843 free(depstring);
845 fprintf(fp, "\n");
847 if(info->provides) {
848 fputs("%PROVIDES%\n", fp);
849 for(lp = info->provides; lp; lp = lp->next) {
850 char *depstring = alpm_dep_compute_string(lp->data);
851 fprintf(fp, "%s\n", depstring);
852 free(depstring);
854 fprintf(fp, "\n");
857 fclose(fp);
858 fp = NULL;
861 /* FILES */
862 if(inforeq & INFRQ_FILES) {
863 char *path;
864 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
865 info->name, info->version);
866 path = _alpm_local_db_pkgpath(db, info, "files");
867 if(!path || (fp = fopen(path, "w")) == NULL) {
868 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
869 path, strerror(errno));
870 retval = -1;
871 free(path);
872 goto cleanup;
874 free(path);
875 if(info->files.count) {
876 size_t i;
877 fprintf(fp, "%%FILES%%\n");
878 for(i = 0; i < info->files.count; i++) {
879 const alpm_file_t *file = info->files.files + i;
880 fprintf(fp, "%s\n", file->name);
882 fprintf(fp, "\n");
884 if(info->backup) {
885 fprintf(fp, "%%BACKUP%%\n");
886 for(lp = info->backup; lp; lp = lp->next) {
887 const alpm_backup_t *backup = lp->data;
888 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
890 fprintf(fp, "\n");
892 fclose(fp);
893 fp = NULL;
896 /* INSTALL */
897 /* nothing needed here (script is automatically extracted) */
899 cleanup:
900 umask(oldmask);
902 if(fp) {
903 fclose(fp);
906 return retval;
909 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
911 int ret = 0;
912 DIR *dirp;
913 struct dirent *dp;
914 char *pkgpath;
915 size_t pkgpath_len;
917 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
918 if(!pkgpath) {
919 return -1;
921 pkgpath_len = strlen(pkgpath);
923 dirp = opendir(pkgpath);
924 if(!dirp) {
925 return -1;
927 /* go through the local DB entry, removing the files within, which we know
928 * are not nested directories of any kind. */
929 for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
930 if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
931 char name[PATH_MAX];
932 if(pkgpath_len + strlen(dp->d_name) + 2 > PATH_MAX) {
933 /* file path is too long to remove, hmm. */
934 ret = -1;
935 } else {
936 sprintf(name, "%s/%s", pkgpath, dp->d_name);
937 if(unlink(name)) {
938 ret = -1;
943 closedir(dirp);
945 /* after removing all enclosed files, we can remove the directory itself. */
946 if(rmdir(pkgpath)) {
947 ret = -1;
949 free(pkgpath);
950 return ret;
953 int SYMEXPORT alpm_pkg_set_reason(alpm_pkg_t *pkg, alpm_pkgreason_t reason)
955 ASSERT(pkg != NULL, return -1);
956 ASSERT(pkg->origin == PKG_FROM_LOCALDB,
957 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
958 ASSERT(pkg->origin_data.db == pkg->handle->db_local,
959 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
961 _alpm_log(pkg->handle, ALPM_LOG_DEBUG,
962 "setting install reason %u for %s\n", reason, pkg->name);
963 if(alpm_pkg_get_reason(pkg) == reason) {
964 /* we are done */
965 return 0;
967 /* set reason (in pkgcache) */
968 pkg->reason = reason;
969 /* write DESC */
970 if(_alpm_local_db_write(pkg->handle->db_local, pkg, INFRQ_DESC)) {
971 RET_ERR(pkg->handle, ALPM_ERR_DB_WRITE, -1);
974 return 0;
977 struct db_operations local_db_ops = {
978 .validate = local_db_validate,
979 .populate = local_db_populate,
980 .unregister = _alpm_db_unregister,
983 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
985 alpm_db_t *db;
987 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
989 db = _alpm_db_new("local", 1);
990 if(db == NULL) {
991 handle->pm_errno = ALPM_ERR_DB_CREATE;
992 return NULL;
994 db->ops = &local_db_ops;
995 db->handle = handle;
997 if(local_db_validate(db)) {
998 /* pm_errno set in local_db_validate() */
999 _alpm_db_free(db);
1000 return NULL;
1003 handle->db_local = db;
1004 return db;
1007 /* vim: set ts=2 sw=2 noet: */