pacman: list all unknown targets on removal operation
[pacman-ng.git] / lib / libalpm / be_local.c
blob606f9e1a38298ea09fead03b3bbe72bdebcaebe7
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 <limits.h> /* PATH_MAX */
33 /* libalpm */
34 #include "db.h"
35 #include "alpm_list.h"
36 #include "log.h"
37 #include "util.h"
38 #include "alpm.h"
39 #include "handle.h"
40 #include "package.h"
41 #include "deps.h"
43 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
45 #define LAZY_LOAD(info, errret) \
46 do { \
47 if(!(pkg->infolevel & info)) { \
48 local_db_read(pkg, info); \
49 } \
50 } while(0)
53 /* Cache-specific accessor functions. These implementations allow for lazy
54 * loading by the files backend when a data member is actually needed
55 * rather than loading all pieces of information when the package is first
56 * initialized.
59 static const char *_cache_get_desc(alpm_pkg_t *pkg)
61 LAZY_LOAD(INFRQ_DESC, NULL);
62 return pkg->desc;
65 static const char *_cache_get_url(alpm_pkg_t *pkg)
67 LAZY_LOAD(INFRQ_DESC, NULL);
68 return pkg->url;
71 static alpm_time_t _cache_get_builddate(alpm_pkg_t *pkg)
73 LAZY_LOAD(INFRQ_DESC, 0);
74 return pkg->builddate;
77 static alpm_time_t _cache_get_installdate(alpm_pkg_t *pkg)
79 LAZY_LOAD(INFRQ_DESC, 0);
80 return pkg->installdate;
83 static const char *_cache_get_packager(alpm_pkg_t *pkg)
85 LAZY_LOAD(INFRQ_DESC, NULL);
86 return pkg->packager;
89 static const char *_cache_get_arch(alpm_pkg_t *pkg)
91 LAZY_LOAD(INFRQ_DESC, NULL);
92 return pkg->arch;
95 static off_t _cache_get_isize(alpm_pkg_t *pkg)
97 LAZY_LOAD(INFRQ_DESC, -1);
98 return pkg->isize;
101 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
103 LAZY_LOAD(INFRQ_DESC, -1);
104 return pkg->reason;
107 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
109 LAZY_LOAD(INFRQ_DESC, NULL);
110 return pkg->licenses;
113 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
115 LAZY_LOAD(INFRQ_DESC, NULL);
116 return pkg->groups;
119 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
121 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
122 return pkg->scriptlet;
125 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
127 LAZY_LOAD(INFRQ_DESC, NULL);
128 return pkg->depends;
131 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
133 LAZY_LOAD(INFRQ_DESC, NULL);
134 return pkg->optdepends;
137 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
139 LAZY_LOAD(INFRQ_DESC, NULL);
140 return pkg->conflicts;
143 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
145 LAZY_LOAD(INFRQ_DESC, NULL);
146 return pkg->provides;
149 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
151 LAZY_LOAD(INFRQ_DESC, NULL);
152 return pkg->replaces;
155 static alpm_filelist_t *_cache_get_files(alpm_pkg_t *pkg)
157 LAZY_LOAD(INFRQ_FILES, NULL);
158 return &(pkg->files);
161 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
163 LAZY_LOAD(INFRQ_FILES, NULL);
164 return pkg->backup;
168 * Open a package changelog for reading. Similar to fopen in functionality,
169 * except that the returned 'file stream' is from the database.
170 * @param pkg the package (from db) to read the changelog
171 * @return a 'file stream' to the package changelog
173 static void *_cache_changelog_open(alpm_pkg_t *pkg)
175 alpm_db_t *db = alpm_pkg_get_db(pkg);
176 char *clfile = _alpm_local_db_pkgpath(db, pkg, "changelog");
177 FILE *f = fopen(clfile, "r");
178 free(clfile);
179 return f;
183 * Read data from an open changelog 'file stream'. Similar to fread in
184 * functionality, this function takes a buffer and amount of data to read.
185 * @param ptr a buffer to fill with raw changelog data
186 * @param size the size of the buffer
187 * @param pkg the package that the changelog is being read from
188 * @param fp a 'file stream' to the package changelog
189 * @return the number of characters read, or 0 if there is no more data
191 static size_t _cache_changelog_read(void *ptr, size_t size,
192 const alpm_pkg_t UNUSED *pkg, void *fp)
194 return fread(ptr, 1, size, (FILE *)fp);
198 * Close a package changelog for reading. Similar to fclose in functionality,
199 * except that the 'file stream' is from the database.
200 * @param pkg the package that the changelog was read from
201 * @param fp a 'file stream' to the package changelog
202 * @return whether closing the package changelog stream was successful
204 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
206 return fclose((FILE *)fp);
209 static int _cache_force_load(alpm_pkg_t *pkg)
211 return local_db_read(pkg, INFRQ_ALL);
215 /** The local database operations struct. Get package fields through
216 * lazy accessor methods that handle any backend loading and caching
217 * logic.
219 static struct pkg_operations local_pkg_ops = {
220 .get_desc = _cache_get_desc,
221 .get_url = _cache_get_url,
222 .get_builddate = _cache_get_builddate,
223 .get_installdate = _cache_get_installdate,
224 .get_packager = _cache_get_packager,
225 .get_arch = _cache_get_arch,
226 .get_isize = _cache_get_isize,
227 .get_reason = _cache_get_reason,
228 .has_scriptlet = _cache_has_scriptlet,
229 .get_licenses = _cache_get_licenses,
230 .get_groups = _cache_get_groups,
231 .get_depends = _cache_get_depends,
232 .get_optdepends = _cache_get_optdepends,
233 .get_conflicts = _cache_get_conflicts,
234 .get_provides = _cache_get_provides,
235 .get_replaces = _cache_get_replaces,
236 .get_files = _cache_get_files,
237 .get_backup = _cache_get_backup,
239 .changelog_open = _cache_changelog_open,
240 .changelog_read = _cache_changelog_read,
241 .changelog_close = _cache_changelog_close,
243 .force_load = _cache_force_load,
246 static int checkdbdir(alpm_db_t *db)
248 struct stat buf;
249 const char *path = _alpm_db_path(db);
251 if(stat(path, &buf) != 0) {
252 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
253 path);
254 if(_alpm_makepath(path) != 0) {
255 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
257 } else if(!S_ISDIR(buf.st_mode)) {
258 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
259 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
260 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
263 return 0;
266 static int is_dir(const char *path, struct dirent *entry)
268 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
269 if(entry->d_type != DT_UNKNOWN) {
270 return (entry->d_type == DT_DIR);
272 #endif
274 char buffer[PATH_MAX];
275 struct stat sbuf;
277 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
279 if(!stat(buffer, &sbuf)) {
280 return S_ISDIR(sbuf.st_mode);
284 return 0;
287 static int local_db_validate(alpm_db_t *db)
289 struct dirent *ent = NULL;
290 const char *dbpath;
291 DIR *dbdir;
292 int ret = -1;
294 if(db->status & DB_STATUS_VALID) {
295 return 0;
297 if(db->status & DB_STATUS_INVALID) {
298 return -1;
301 dbpath = _alpm_db_path(db);
302 if(dbpath == NULL) {
303 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
305 dbdir = opendir(dbpath);
306 if(dbdir == NULL) {
307 if(errno == ENOENT) {
308 /* database dir doesn't exist yet */
309 db->status |= DB_STATUS_VALID;
310 db->status &= ~DB_STATUS_INVALID;
311 db->status &= ~DB_STATUS_EXISTS;
312 db->status |= DB_STATUS_MISSING;
313 return 0;
314 } else {
315 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
318 db->status |= DB_STATUS_EXISTS;
319 db->status &= ~DB_STATUS_MISSING;
321 while((ent = readdir(dbdir)) != NULL) {
322 const char *name = ent->d_name;
323 char path[PATH_MAX];
325 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
326 continue;
328 if(!is_dir(dbpath, ent)) {
329 continue;
332 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
333 if(access(path, F_OK) == 0) {
334 /* we found a depends file- bail */
335 db->status &= ~DB_STATUS_VALID;
336 db->status |= DB_STATUS_INVALID;
337 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
338 goto done;
341 /* we found no depends file after full scan */
342 db->status |= DB_STATUS_VALID;
343 db->status &= ~DB_STATUS_INVALID;
344 ret = 0;
346 done:
347 if(dbdir) {
348 closedir(dbdir);
351 return ret;
354 static int local_db_populate(alpm_db_t *db)
356 size_t est_count;
357 int count = 0;
358 struct stat buf;
359 struct dirent *ent = NULL;
360 const char *dbpath;
361 DIR *dbdir;
363 if(db->status & DB_STATUS_INVALID) {
364 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
366 /* note: DB_STATUS_MISSING is not fatal for local database */
368 dbpath = _alpm_db_path(db);
369 if(dbpath == NULL) {
370 /* pm_errno set in _alpm_db_path() */
371 return -1;
374 dbdir = opendir(dbpath);
375 if(dbdir == NULL) {
376 if(errno == ENOENT) {
377 /* no database existing yet is not an error */
378 db->status &= ~DB_STATUS_EXISTS;
379 db->status |= DB_STATUS_MISSING;
380 return 0;
382 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
384 if(fstat(dirfd(dbdir), &buf) != 0) {
385 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
387 db->status |= DB_STATUS_EXISTS;
388 db->status &= ~DB_STATUS_MISSING;
389 if(buf.st_nlink >= 2) {
390 est_count = buf.st_nlink;
391 } else {
392 /* Some filesystems don't subscribe to the two-implicit links school of
393 * thought, e.g. BTRFS, HFS+. See
394 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
396 est_count = 0;
397 while(readdir(dbdir) != NULL) {
398 est_count++;
400 rewinddir(dbdir);
402 if(est_count >= 2) {
403 /* subtract the two extra pointers to get # of children */
404 est_count -= 2;
407 /* initialize hash at 50% full */
408 db->pkgcache = _alpm_pkghash_create(est_count * 2);
409 if(db->pkgcache == NULL){
410 closedir(dbdir);
411 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
414 while((ent = readdir(dbdir)) != NULL) {
415 const char *name = ent->d_name;
417 alpm_pkg_t *pkg;
419 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
420 continue;
422 if(!is_dir(dbpath, ent)) {
423 continue;
426 pkg = _alpm_pkg_new();
427 if(pkg == NULL) {
428 closedir(dbdir);
429 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
431 /* split the db entry name */
432 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
433 &(pkg->name_hash)) != 0) {
434 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
435 name);
436 _alpm_pkg_free(pkg);
437 continue;
440 /* duplicated database entries are not allowed */
441 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
442 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
443 _alpm_pkg_free(pkg);
444 continue;
447 pkg->origin = PKG_FROM_LOCALDB;
448 pkg->origin_data.db = db;
449 pkg->ops = &local_pkg_ops;
450 pkg->handle = db->handle;
452 /* explicitly read with only 'BASE' data, accessors will handle the rest */
453 if(local_db_read(pkg, INFRQ_BASE) == -1) {
454 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
455 _alpm_pkg_free(pkg);
456 continue;
459 /* add to the collection */
460 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
461 pkg->name, db->treename);
462 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
463 count++;
466 closedir(dbdir);
467 if(count > 0) {
468 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
470 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
471 count, db->treename);
473 return count;
476 /* Note: the return value must be freed by the caller */
477 char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
478 const char *filename)
480 size_t len;
481 char *pkgpath;
482 const char *dbpath;
484 dbpath = _alpm_db_path(db);
485 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
486 len += filename ? strlen(filename) : 0;
487 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
488 sprintf(pkgpath, "%s%s-%s/%s", dbpath, info->name, info->version,
489 filename ? filename : "");
490 return pkgpath;
493 #define READ_NEXT() do { \
494 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
495 _alpm_strip_newline(line); \
496 } while(0)
498 #define READ_AND_STORE(f) do { \
499 READ_NEXT(); \
500 STRDUP(f, line, goto error); \
501 } while(0)
503 #define READ_AND_STORE_ALL(f) do { \
504 char *linedup; \
505 if(fgets(line, sizeof(line), fp) == NULL) {\
506 if(!feof(fp)) goto error; else break; \
508 if(_alpm_strip_newline(line) == 0) break; \
509 STRDUP(linedup, line, goto error); \
510 f = alpm_list_add(f, linedup); \
511 } while(1) /* note the while(1) and not (0) */
513 #define READ_AND_SPLITDEP(f) do { \
514 if(fgets(line, sizeof(line), fp) == NULL) {\
515 if(!feof(fp)) goto error; else break; \
517 if(_alpm_strip_newline(line) == 0) break; \
518 f = alpm_list_add(f, _alpm_splitdep(line)); \
519 } while(1) /* note the while(1) and not (0) */
521 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
523 FILE *fp = NULL;
524 char line[1024];
525 char *pkgpath;
526 alpm_db_t *db = info->origin_data.db;
528 /* bitmask logic here:
529 * infolevel: 00001111
530 * inforeq: 00010100
531 * & result: 00000100
532 * == to inforeq? nope, we need to load more info. */
533 if((info->infolevel & inforeq) == inforeq) {
534 /* already loaded all of this info, do nothing */
535 return 0;
538 if(info->infolevel & INFRQ_ERROR) {
539 /* We've encountered an error loading this package before. Don't attempt
540 * repeated reloads, just give up. */
541 return -1;
544 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
545 info->name, inforeq);
547 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
548 if(!pkgpath || access(pkgpath, F_OK)) {
549 /* directory doesn't exist or can't be opened */
550 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
551 info->name, info->version, db->treename);
552 goto error;
554 free(pkgpath);
556 /* clear out 'line', to be certain - and to make valgrind happy */
557 memset(line, 0, sizeof(line));
559 /* DESC */
560 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
561 char *path = _alpm_local_db_pkgpath(db, info, "desc");
562 if(!path || (fp = fopen(path, "r")) == NULL) {
563 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
564 free(path);
565 goto error;
567 free(path);
568 while(!feof(fp)) {
569 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
570 goto error;
572 if(_alpm_strip_newline(line) == 0) {
573 /* length of stripped line was zero */
574 continue;
576 if(strcmp(line, "%NAME%") == 0) {
577 READ_NEXT();
578 if(strcmp(line, info->name) != 0) {
579 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
580 "mismatch on package %s\n"), db->treename, info->name);
582 } else if(strcmp(line, "%VERSION%") == 0) {
583 READ_NEXT();
584 if(strcmp(line, info->version) != 0) {
585 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
586 "mismatch on package %s\n"), db->treename, info->name);
588 } else if(strcmp(line, "%DESC%") == 0) {
589 READ_AND_STORE(info->desc);
590 } else if(strcmp(line, "%GROUPS%") == 0) {
591 READ_AND_STORE_ALL(info->groups);
592 } else if(strcmp(line, "%URL%") == 0) {
593 READ_AND_STORE(info->url);
594 } else if(strcmp(line, "%LICENSE%") == 0) {
595 READ_AND_STORE_ALL(info->licenses);
596 } else if(strcmp(line, "%ARCH%") == 0) {
597 READ_AND_STORE(info->arch);
598 } else if(strcmp(line, "%BUILDDATE%") == 0) {
599 READ_NEXT();
600 info->builddate = _alpm_parsedate(line);
601 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
602 READ_NEXT();
603 info->installdate = _alpm_parsedate(line);
604 } else if(strcmp(line, "%PACKAGER%") == 0) {
605 READ_AND_STORE(info->packager);
606 } else if(strcmp(line, "%REASON%") == 0) {
607 READ_NEXT();
608 info->reason = (alpm_pkgreason_t)atoi(line);
609 } else if(strcmp(line, "%SIZE%") == 0) {
610 READ_NEXT();
611 info->isize = _alpm_strtoofft(line);
612 } else if(strcmp(line, "%REPLACES%") == 0) {
613 READ_AND_SPLITDEP(info->replaces);
614 } else if(strcmp(line, "%DEPENDS%") == 0) {
615 READ_AND_SPLITDEP(info->depends);
616 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
617 READ_AND_STORE_ALL(info->optdepends);
618 } else if(strcmp(line, "%CONFLICTS%") == 0) {
619 READ_AND_SPLITDEP(info->conflicts);
620 } else if(strcmp(line, "%PROVIDES%") == 0) {
621 READ_AND_SPLITDEP(info->provides);
624 fclose(fp);
625 fp = NULL;
626 info->infolevel |= INFRQ_DESC;
629 /* FILES */
630 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
631 char *path = _alpm_local_db_pkgpath(db, info, "files");
632 if(!path || (fp = fopen(path, "r")) == NULL) {
633 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
634 free(path);
635 goto error;
637 free(path);
638 while(fgets(line, sizeof(line), fp)) {
639 _alpm_strip_newline(line);
640 if(strcmp(line, "%FILES%") == 0) {
641 size_t files_count = 0, files_size = 0, len;
642 alpm_file_t *files = NULL;
644 while(fgets(line, sizeof(line), fp) &&
645 (len = _alpm_strip_newline(line))) {
646 if(files_count >= files_size) {
647 size_t old_size = files_size;
648 if(files_size == 0) {
649 files_size = 8;
650 } else {
651 files_size *= 2;
653 files = realloc(files, sizeof(alpm_file_t) * files_size);
654 if(!files) {
655 ALLOC_FAIL(sizeof(alpm_file_t) * files_size);
656 goto error;
658 /* ensure all new memory is zeroed out, in both the initial
659 * allocation and later reallocs */
660 memset(files + old_size, 0,
661 sizeof(alpm_file_t) * (files_size - old_size));
663 /* since we know the length of the file string already,
664 * we can do malloc + memcpy rather than strdup */
665 files[files_count].name = malloc(len + 1);
666 if(files[files_count].name == NULL) {
667 ALLOC_FAIL(len);
668 goto error;
670 memcpy(files[files_count].name, line, len + 1);
671 files_count++;
673 /* attempt to hand back any memory we don't need */
674 files = realloc(files, sizeof(alpm_file_t) * files_count);
675 info->files.count = files_count;
676 info->files.files = files;
677 } else if(strcmp(line, "%BACKUP%") == 0) {
678 while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line)) {
679 alpm_backup_t *backup;
680 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
681 if(_alpm_split_backup(line, &backup)) {
682 goto error;
684 info->backup = alpm_list_add(info->backup, backup);
688 fclose(fp);
689 fp = NULL;
690 info->infolevel |= INFRQ_FILES;
693 /* INSTALL */
694 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
695 char *path = _alpm_local_db_pkgpath(db, info, "install");
696 if(access(path, F_OK) == 0) {
697 info->scriptlet = 1;
699 free(path);
700 info->infolevel |= INFRQ_SCRIPTLET;
703 return 0;
705 error:
706 info->infolevel |= INFRQ_ERROR;
707 if(fp) {
708 fclose(fp);
710 return -1;
713 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
715 mode_t oldmask;
716 int retval = 0;
717 char *pkgpath;
719 if(checkdbdir(db) != 0) {
720 return -1;
723 oldmask = umask(0000);
724 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
726 if((retval = mkdir(pkgpath, 0755)) != 0) {
727 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not create directory %s: %s\n"),
728 pkgpath, strerror(errno));
731 free(pkgpath);
732 umask(oldmask);
734 return retval;
737 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
739 FILE *fp = NULL;
740 mode_t oldmask;
741 alpm_list_t *lp;
742 int retval = 0;
744 if(db == NULL || info == NULL || !(db->status & DB_STATUS_LOCAL)) {
745 return -1;
748 /* make sure we have a sane umask */
749 oldmask = umask(0022);
751 /* DESC */
752 if(inforeq & INFRQ_DESC) {
753 char *path;
754 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
755 info->name, info->version);
756 path = _alpm_local_db_pkgpath(db, info, "desc");
757 if(!path || (fp = fopen(path, "w")) == NULL) {
758 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
759 path, strerror(errno));
760 retval = -1;
761 free(path);
762 goto cleanup;
764 free(path);
765 fprintf(fp, "%%NAME%%\n%s\n\n"
766 "%%VERSION%%\n%s\n\n", info->name, info->version);
767 if(info->desc) {
768 fprintf(fp, "%%DESC%%\n"
769 "%s\n\n", info->desc);
771 if(info->groups) {
772 fputs("%GROUPS%\n", fp);
773 for(lp = info->groups; lp; lp = lp->next) {
774 fprintf(fp, "%s\n", (char *)lp->data);
776 fprintf(fp, "\n");
778 if(info->replaces) {
779 fputs("%REPLACES%\n", fp);
780 for(lp = info->replaces; lp; lp = lp->next) {
781 char *depstring = alpm_dep_compute_string(lp->data);
782 fprintf(fp, "%s\n", depstring);
783 free(depstring);
785 fprintf(fp, "\n");
787 if(info->url) {
788 fprintf(fp, "%%URL%%\n"
789 "%s\n\n", info->url);
791 if(info->licenses) {
792 fputs("%LICENSE%\n", fp);
793 for(lp = info->licenses; lp; lp = lp->next) {
794 fprintf(fp, "%s\n", (char *)lp->data);
796 fprintf(fp, "\n");
798 if(info->arch) {
799 fprintf(fp, "%%ARCH%%\n"
800 "%s\n\n", info->arch);
802 if(info->builddate) {
803 fprintf(fp, "%%BUILDDATE%%\n"
804 "%jd\n\n", (intmax_t)info->builddate);
806 if(info->installdate) {
807 fprintf(fp, "%%INSTALLDATE%%\n"
808 "%jd\n\n", (intmax_t)info->installdate);
810 if(info->packager) {
811 fprintf(fp, "%%PACKAGER%%\n"
812 "%s\n\n", info->packager);
814 if(info->isize) {
815 /* only write installed size, csize is irrelevant once installed */
816 fprintf(fp, "%%SIZE%%\n"
817 "%jd\n\n", (intmax_t)info->isize);
819 if(info->reason) {
820 fprintf(fp, "%%REASON%%\n"
821 "%u\n\n", info->reason);
823 if(info->depends) {
824 fputs("%DEPENDS%\n", fp);
825 for(lp = info->depends; lp; lp = lp->next) {
826 char *depstring = alpm_dep_compute_string(lp->data);
827 fprintf(fp, "%s\n", depstring);
828 free(depstring);
830 fprintf(fp, "\n");
832 if(info->optdepends) {
833 fputs("%OPTDEPENDS%\n", fp);
834 for(lp = info->optdepends; lp; lp = lp->next) {
835 fprintf(fp, "%s\n", (char *)lp->data);
837 fprintf(fp, "\n");
839 if(info->conflicts) {
840 fputs("%CONFLICTS%\n", fp);
841 for(lp = info->conflicts; lp; lp = lp->next) {
842 char *depstring = alpm_dep_compute_string(lp->data);
843 fprintf(fp, "%s\n", depstring);
844 free(depstring);
846 fprintf(fp, "\n");
848 if(info->provides) {
849 fputs("%PROVIDES%\n", fp);
850 for(lp = info->provides; lp; lp = lp->next) {
851 char *depstring = alpm_dep_compute_string(lp->data);
852 fprintf(fp, "%s\n", depstring);
853 free(depstring);
855 fprintf(fp, "\n");
858 fclose(fp);
859 fp = NULL;
862 /* FILES */
863 if(inforeq & INFRQ_FILES) {
864 char *path;
865 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
866 info->name, info->version);
867 path = _alpm_local_db_pkgpath(db, info, "files");
868 if(!path || (fp = fopen(path, "w")) == NULL) {
869 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
870 path, strerror(errno));
871 retval = -1;
872 free(path);
873 goto cleanup;
875 free(path);
876 if(info->files.count) {
877 size_t i;
878 fprintf(fp, "%%FILES%%\n");
879 for(i = 0; i < info->files.count; i++) {
880 const alpm_file_t *file = info->files.files + i;
881 fprintf(fp, "%s\n", file->name);
883 fprintf(fp, "\n");
885 if(info->backup) {
886 fprintf(fp, "%%BACKUP%%\n");
887 for(lp = info->backup; lp; lp = lp->next) {
888 const alpm_backup_t *backup = lp->data;
889 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
891 fprintf(fp, "\n");
893 fclose(fp);
894 fp = NULL;
897 /* INSTALL */
898 /* nothing needed here (script is automatically extracted) */
900 cleanup:
901 umask(oldmask);
903 if(fp) {
904 fclose(fp);
907 return retval;
910 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
912 int ret = 0;
913 char *pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
915 /* TODO explicit file removes and then an rmdir? */
916 ret = _alpm_rmrf(pkgpath);
917 free(pkgpath);
918 if(ret != 0) {
919 ret = -1;
921 return ret;
924 struct db_operations local_db_ops = {
925 .validate = local_db_validate,
926 .populate = local_db_populate,
927 .unregister = _alpm_db_unregister,
930 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
932 alpm_db_t *db;
934 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
936 db = _alpm_db_new("local", 1);
937 if(db == NULL) {
938 handle->pm_errno = ALPM_ERR_DB_CREATE;
939 return NULL;
941 db->ops = &local_db_ops;
942 db->handle = handle;
944 if(local_db_validate(db)) {
945 /* pm_errno set in local_db_validate() */
946 _alpm_db_free(db);
947 return NULL;
950 handle->db_local = db;
951 return db;
954 /* vim: set ts=2 sw=2 noet: */