Prefix alpm_loglevel_t members with ALPM
[pacman-ng.git] / lib / libalpm / be_local.c
bloba98b1bba2633d405efb5f110c92cc5f7aac84d72
1 /*
2 * be_local.c : backend for the local database
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdint.h> /* intmax_t */
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <time.h>
32 #include <limits.h> /* PATH_MAX */
34 /* libalpm */
35 #include "db.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "util.h"
39 #include "alpm.h"
40 #include "handle.h"
41 #include "package.h"
42 #include "deps.h"
44 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
46 #define LAZY_LOAD(info, errret) \
47 do { \
48 if(!(pkg->infolevel & info)) { \
49 local_db_read(pkg, info); \
50 } \
51 } while(0)
54 /* Cache-specific accessor functions. These implementations allow for lazy
55 * loading by the files backend when a data member is actually needed
56 * rather than loading all pieces of information when the package is first
57 * initialized.
60 static const char *_cache_get_filename(alpm_pkg_t *pkg)
62 LAZY_LOAD(INFRQ_DESC, NULL);
63 return pkg->filename;
66 static const char *_cache_get_desc(alpm_pkg_t *pkg)
68 LAZY_LOAD(INFRQ_DESC, NULL);
69 return pkg->desc;
72 static const char *_cache_get_url(alpm_pkg_t *pkg)
74 LAZY_LOAD(INFRQ_DESC, NULL);
75 return pkg->url;
78 static time_t _cache_get_builddate(alpm_pkg_t *pkg)
80 LAZY_LOAD(INFRQ_DESC, 0);
81 return pkg->builddate;
84 static time_t _cache_get_installdate(alpm_pkg_t *pkg)
86 LAZY_LOAD(INFRQ_DESC, 0);
87 return pkg->installdate;
90 static const char *_cache_get_packager(alpm_pkg_t *pkg)
92 LAZY_LOAD(INFRQ_DESC, NULL);
93 return pkg->packager;
96 static const char *_cache_get_md5sum(alpm_pkg_t *pkg)
98 LAZY_LOAD(INFRQ_DESC, NULL);
99 return pkg->md5sum;
102 static const char *_cache_get_arch(alpm_pkg_t *pkg)
104 LAZY_LOAD(INFRQ_DESC, NULL);
105 return pkg->arch;
108 static off_t _cache_get_size(alpm_pkg_t *pkg)
110 LAZY_LOAD(INFRQ_DESC, -1);
111 return pkg->size;
114 static off_t _cache_get_isize(alpm_pkg_t *pkg)
116 LAZY_LOAD(INFRQ_DESC, -1);
117 return pkg->isize;
120 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
122 LAZY_LOAD(INFRQ_DESC, -1);
123 return pkg->reason;
126 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
128 LAZY_LOAD(INFRQ_DESC, NULL);
129 return pkg->licenses;
132 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
134 LAZY_LOAD(INFRQ_DESC, NULL);
135 return pkg->groups;
138 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
140 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
141 return pkg->scriptlet;
144 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
146 LAZY_LOAD(INFRQ_DESC, NULL);
147 return pkg->depends;
150 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
152 LAZY_LOAD(INFRQ_DESC, NULL);
153 return pkg->optdepends;
156 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
158 LAZY_LOAD(INFRQ_DESC, NULL);
159 return pkg->conflicts;
162 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
164 LAZY_LOAD(INFRQ_DESC, NULL);
165 return pkg->provides;
168 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
170 LAZY_LOAD(INFRQ_DESC, NULL);
171 return pkg->replaces;
174 /* local packages can not have deltas */
175 static alpm_list_t *_cache_get_deltas(alpm_pkg_t UNUSED *pkg)
177 return NULL;
180 static alpm_list_t *_cache_get_files(alpm_pkg_t *pkg)
182 LAZY_LOAD(INFRQ_FILES, NULL);
183 return pkg->files;
186 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
188 LAZY_LOAD(INFRQ_FILES, NULL);
189 return pkg->backup;
193 * Open a package changelog for reading. Similar to fopen in functionality,
194 * except that the returned 'file stream' is from the database.
195 * @param pkg the package (from db) to read the changelog
196 * @return a 'file stream' to the package changelog
198 static void *_cache_changelog_open(alpm_pkg_t *pkg)
200 char clfile[PATH_MAX];
201 snprintf(clfile, PATH_MAX, "%s/%s/%s-%s/changelog",
202 alpm_option_get_dbpath(pkg->handle),
203 alpm_db_get_name(alpm_pkg_get_db(pkg)),
204 alpm_pkg_get_name(pkg),
205 alpm_pkg_get_version(pkg));
206 return fopen(clfile, "r");
210 * Read data from an open changelog 'file stream'. Similar to fread in
211 * functionality, this function takes a buffer and amount of data to read.
212 * @param ptr a buffer to fill with raw changelog data
213 * @param size the size of the buffer
214 * @param pkg the package that the changelog is being read from
215 * @param fp a 'file stream' to the package changelog
216 * @return the number of characters read, or 0 if there is no more data
218 static size_t _cache_changelog_read(void *ptr, size_t size,
219 const alpm_pkg_t UNUSED *pkg, const void *fp)
221 return fread(ptr, 1, size, (FILE *)fp);
225 * Close a package changelog for reading. Similar to fclose in functionality,
226 * except that the 'file stream' is from the database.
227 * @param pkg the package that the changelog was read from
228 * @param fp a 'file stream' to the package changelog
229 * @return whether closing the package changelog stream was successful
231 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
233 return fclose((FILE *)fp);
236 static int _cache_force_load(alpm_pkg_t *pkg)
238 return local_db_read(pkg, INFRQ_ALL);
242 /** The local database operations struct. Get package fields through
243 * lazy accessor methods that handle any backend loading and caching
244 * logic.
246 static struct pkg_operations local_pkg_ops = {
247 .get_filename = _cache_get_filename,
248 .get_desc = _cache_get_desc,
249 .get_url = _cache_get_url,
250 .get_builddate = _cache_get_builddate,
251 .get_installdate = _cache_get_installdate,
252 .get_packager = _cache_get_packager,
253 .get_md5sum = _cache_get_md5sum,
254 .get_arch = _cache_get_arch,
255 .get_size = _cache_get_size,
256 .get_isize = _cache_get_isize,
257 .get_reason = _cache_get_reason,
258 .has_scriptlet = _cache_has_scriptlet,
259 .get_licenses = _cache_get_licenses,
260 .get_groups = _cache_get_groups,
261 .get_depends = _cache_get_depends,
262 .get_optdepends = _cache_get_optdepends,
263 .get_conflicts = _cache_get_conflicts,
264 .get_provides = _cache_get_provides,
265 .get_replaces = _cache_get_replaces,
266 .get_deltas = _cache_get_deltas,
267 .get_files = _cache_get_files,
268 .get_backup = _cache_get_backup,
270 .changelog_open = _cache_changelog_open,
271 .changelog_read = _cache_changelog_read,
272 .changelog_close = _cache_changelog_close,
274 .force_load = _cache_force_load,
277 static int checkdbdir(alpm_db_t *db)
279 struct stat buf;
280 const char *path = _alpm_db_path(db);
282 if(stat(path, &buf) != 0) {
283 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
284 path);
285 if(_alpm_makepath(path) != 0) {
286 RET_ERR(db->handle, PM_ERR_SYSTEM, -1);
288 } else if(!S_ISDIR(buf.st_mode)) {
289 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
290 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
291 RET_ERR(db->handle, PM_ERR_SYSTEM, -1);
294 return 0;
297 static int is_dir(const char *path, struct dirent *entry)
299 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
300 if(entry->d_type != DT_UNKNOWN) {
301 return (entry->d_type == DT_DIR);
303 #endif
305 char buffer[PATH_MAX];
306 struct stat sbuf;
308 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
310 if(!stat(buffer, &sbuf)) {
311 return S_ISDIR(sbuf.st_mode);
315 return 0;
318 static int local_db_validate(alpm_db_t *db)
320 struct dirent *ent = NULL;
321 const char *dbpath;
322 DIR *dbdir;
323 int ret = -1;
325 if(db->status & DB_STATUS_VALID) {
326 return 0;
329 dbpath = _alpm_db_path(db);
330 if(dbpath == NULL) {
331 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
333 dbdir = opendir(dbpath);
334 if(dbdir == NULL) {
335 if(errno == ENOENT) {
336 /* database dir doesn't exist yet */
337 db->status |= DB_STATUS_VALID;
338 return 0;
339 } else {
340 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
344 while((ent = readdir(dbdir)) != NULL) {
345 const char *name = ent->d_name;
346 char path[PATH_MAX];
348 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
349 continue;
351 if(!is_dir(dbpath, ent)) {
352 continue;
355 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
356 if(access(path, F_OK) == 0) {
357 /* we found a depends file- bail */
358 db->handle->pm_errno = PM_ERR_DB_VERSION;
359 goto done;
362 /* we found no depends file after full scan */
363 db->status |= DB_STATUS_VALID;
364 ret = 0;
366 done:
367 if(dbdir) {
368 closedir(dbdir);
371 return ret;
374 static int local_db_populate(alpm_db_t *db)
376 size_t est_count;
377 int count = 0;
378 struct stat buf;
379 struct dirent *ent = NULL;
380 const char *dbpath;
381 DIR *dbdir;
383 dbpath = _alpm_db_path(db);
384 if(dbpath == NULL) {
385 /* pm_errno set in _alpm_db_path() */
386 return -1;
389 dbdir = opendir(dbpath);
390 if(dbdir == NULL) {
391 if(errno == ENOENT) {
392 /* no database existing yet is not an error */
393 return 0;
395 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
397 if(fstat(dirfd(dbdir), &buf) != 0) {
398 RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
400 if(buf.st_nlink >= 2) {
401 est_count = buf.st_nlink;
402 } else {
403 /* Some filesystems don't subscribe to the two-implicit links school of
404 * thought, e.g. BTRFS, HFS+. See
405 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
407 est_count = 0;
408 while(readdir(dbdir) != NULL) {
409 est_count++;
411 rewinddir(dbdir);
413 if(est_count >= 2) {
414 /* subtract the two extra pointers to get # of children */
415 est_count -= 2;
418 /* initialize hash at 50% full */
419 db->pkgcache = _alpm_pkghash_create(est_count * 2);
420 if(db->pkgcache == NULL){
421 closedir(dbdir);
422 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
425 while((ent = readdir(dbdir)) != NULL) {
426 const char *name = ent->d_name;
428 alpm_pkg_t *pkg;
430 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
431 continue;
433 if(!is_dir(dbpath, ent)) {
434 continue;
437 pkg = _alpm_pkg_new();
438 if(pkg == NULL) {
439 closedir(dbdir);
440 RET_ERR(db->handle, PM_ERR_MEMORY, -1);
442 /* split the db entry name */
443 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
444 &(pkg->name_hash)) != 0) {
445 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
446 name);
447 _alpm_pkg_free(pkg);
448 continue;
451 /* duplicated database entries are not allowed */
452 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
453 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
454 _alpm_pkg_free(pkg);
455 continue;
458 pkg->origin = PKG_FROM_LOCALDB;
459 pkg->origin_data.db = db;
460 pkg->ops = &local_pkg_ops;
461 pkg->handle = db->handle;
463 /* explicitly read with only 'BASE' data, accessors will handle the rest */
464 if(local_db_read(pkg, INFRQ_BASE) == -1) {
465 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
466 _alpm_pkg_free(pkg);
467 continue;
470 /* add to the collection */
471 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
472 pkg->name, db->treename);
473 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
474 count++;
477 closedir(dbdir);
478 if(count > 0) {
479 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
481 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
482 count, db->treename);
484 return count;
487 /* Note: the return value must be freed by the caller */
488 static char *get_pkgpath(alpm_db_t *db, alpm_pkg_t *info)
490 size_t len;
491 char *pkgpath;
492 const char *dbpath;
494 dbpath = _alpm_db_path(db);
495 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
496 MALLOC(pkgpath, len, RET_ERR(db->handle, PM_ERR_MEMORY, NULL));
497 sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
498 return pkgpath;
502 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
504 FILE *fp = NULL;
505 char path[PATH_MAX];
506 char line[1024];
507 char *pkgpath = NULL;
508 alpm_db_t *db = info->origin_data.db;
510 /* bitmask logic here:
511 * infolevel: 00001111
512 * inforeq: 00010100
513 * & result: 00000100
514 * == to inforeq? nope, we need to load more info. */
515 if((info->infolevel & inforeq) == inforeq) {
516 /* already loaded all of this info, do nothing */
517 return 0;
519 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
520 info->name, inforeq);
522 /* clear out 'line', to be certain - and to make valgrind happy */
523 memset(line, 0, sizeof(line));
525 pkgpath = get_pkgpath(db, info);
527 if(access(pkgpath, F_OK)) {
528 /* directory doesn't exist or can't be opened */
529 _alpm_log(db->handle, ALPM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'\n",
530 info->name, info->version, db->treename);
531 goto error;
534 /* DESC */
535 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
536 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
537 if((fp = fopen(path, "r")) == NULL) {
538 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
539 goto error;
541 while(!feof(fp)) {
542 if(fgets(line, sizeof(line), fp) == NULL) {
543 break;
545 _alpm_strtrim(line);
546 if(strcmp(line, "%NAME%") == 0) {
547 if(fgets(line, sizeof(line), fp) == NULL) {
548 goto error;
550 if(strcmp(_alpm_strtrim(line), info->name) != 0) {
551 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
552 "mismatch on package %s\n"), db->treename, info->name);
554 } else if(strcmp(line, "%VERSION%") == 0) {
555 if(fgets(line, sizeof(line), fp) == NULL) {
556 goto error;
558 if(strcmp(_alpm_strtrim(line), info->version) != 0) {
559 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
560 "mismatch on package %s\n"), db->treename, info->name);
562 } else if(strcmp(line, "%DESC%") == 0) {
563 if(fgets(line, sizeof(line), fp) == NULL) {
564 goto error;
566 STRDUP(info->desc, _alpm_strtrim(line), goto error);
567 } else if(strcmp(line, "%GROUPS%") == 0) {
568 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
569 char *linedup;
570 STRDUP(linedup, line, goto error);
571 info->groups = alpm_list_add(info->groups, linedup);
573 } else if(strcmp(line, "%URL%") == 0) {
574 if(fgets(line, sizeof(line), fp) == NULL) {
575 goto error;
577 STRDUP(info->url, _alpm_strtrim(line), goto error);
578 } else if(strcmp(line, "%LICENSE%") == 0) {
579 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
580 char *linedup;
581 STRDUP(linedup, line, goto error);
582 info->licenses = alpm_list_add(info->licenses, linedup);
584 } else if(strcmp(line, "%ARCH%") == 0) {
585 if(fgets(line, sizeof(line), fp) == NULL) {
586 goto error;
588 STRDUP(info->arch, _alpm_strtrim(line), goto error);
589 } else if(strcmp(line, "%BUILDDATE%") == 0) {
590 if(fgets(line, sizeof(line), fp) == NULL) {
591 goto error;
593 _alpm_strtrim(line);
594 info->builddate = _alpm_parsedate(line);
595 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
596 if(fgets(line, sizeof(line), fp) == NULL) {
597 goto error;
599 _alpm_strtrim(line);
600 info->installdate = _alpm_parsedate(line);
601 } else if(strcmp(line, "%PACKAGER%") == 0) {
602 if(fgets(line, sizeof(line), fp) == NULL) {
603 goto error;
605 STRDUP(info->packager, _alpm_strtrim(line), goto error);
606 } else if(strcmp(line, "%REASON%") == 0) {
607 if(fgets(line, sizeof(line), fp) == NULL) {
608 goto error;
610 info->reason = (alpm_pkgreason_t)atol(_alpm_strtrim(line));
611 } else if(strcmp(line, "%SIZE%") == 0) {
612 /* NOTE: the CSIZE and SIZE fields both share the "size" field
613 * in the pkginfo_t struct. This can be done b/c CSIZE
614 * is currently only used in sync databases, and SIZE is
615 * only used in local databases.
617 if(fgets(line, sizeof(line), fp) == NULL) {
618 goto error;
620 info->size = atol(_alpm_strtrim(line));
621 /* also store this value to isize */
622 info->isize = info->size;
623 } else if(strcmp(line, "%REPLACES%") == 0) {
624 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
625 char *linedup;
626 STRDUP(linedup, line, goto error);
627 info->replaces = alpm_list_add(info->replaces, linedup);
629 } else if(strcmp(line, "%DEPENDS%") == 0) {
630 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
631 alpm_depend_t *dep = _alpm_splitdep(line);
632 info->depends = alpm_list_add(info->depends, dep);
634 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
635 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
636 char *linedup;
637 STRDUP(linedup, line, goto error);
638 info->optdepends = alpm_list_add(info->optdepends, linedup);
640 } else if(strcmp(line, "%CONFLICTS%") == 0) {
641 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
642 char *linedup;
643 STRDUP(linedup, line, goto error);
644 info->conflicts = alpm_list_add(info->conflicts, linedup);
646 } else if(strcmp(line, "%PROVIDES%") == 0) {
647 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
648 char *linedup;
649 STRDUP(linedup, line, goto error);
650 info->provides = alpm_list_add(info->provides, linedup);
654 fclose(fp);
655 fp = NULL;
658 /* FILES */
659 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
660 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
661 if((fp = fopen(path, "r")) == NULL) {
662 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
663 goto error;
665 while(fgets(line, sizeof(line), fp)) {
666 _alpm_strtrim(line);
667 if(strcmp(line, "%FILES%") == 0) {
668 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
669 char *linedup;
670 STRDUP(linedup, line, goto error);
671 info->files = alpm_list_add(info->files, linedup);
673 } else if(strcmp(line, "%BACKUP%") == 0) {
674 while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
675 alpm_backup_t *backup;
676 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
677 if(_alpm_split_backup(line, &backup)) {
678 goto error;
680 info->backup = alpm_list_add(info->backup, backup);
684 fclose(fp);
685 fp = NULL;
688 /* INSTALL */
689 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
690 snprintf(path, PATH_MAX, "%sinstall", pkgpath);
691 if(access(path, F_OK) == 0) {
692 info->scriptlet = 1;
696 /* internal */
697 info->infolevel |= inforeq;
699 free(pkgpath);
700 return 0;
702 error:
703 free(pkgpath);
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 = NULL;
716 if(checkdbdir(db) != 0) {
717 return -1;
720 oldmask = umask(0000);
721 pkgpath = get_pkgpath(db, info);
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 char path[PATH_MAX];
738 mode_t oldmask;
739 alpm_list_t *lp = NULL;
740 int retval = 0;
741 char *pkgpath = NULL;
743 if(db == NULL || info == NULL) {
744 return -1;
747 pkgpath = get_pkgpath(db, info);
749 /* make sure we have a sane umask */
750 oldmask = umask(0022);
752 if(strcmp(db->treename, "local") != 0) {
753 return -1;
756 /* DESC */
757 if(inforeq & INFRQ_DESC) {
758 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s DESC information back to db\n",
759 info->name, info->version);
760 snprintf(path, PATH_MAX, "%sdesc", pkgpath);
761 if((fp = fopen(path, "w")) == NULL) {
762 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
763 path, strerror(errno));
764 retval = -1;
765 goto cleanup;
767 fprintf(fp, "%%NAME%%\n%s\n\n"
768 "%%VERSION%%\n%s\n\n", info->name, info->version);
769 if(info->desc) {
770 fprintf(fp, "%%DESC%%\n"
771 "%s\n\n", info->desc);
773 if(info->groups) {
774 fputs("%GROUPS%\n", fp);
775 for(lp = info->groups; lp; lp = lp->next) {
776 fprintf(fp, "%s\n", (char *)lp->data);
778 fprintf(fp, "\n");
780 if(info->replaces) {
781 fputs("%REPLACES%\n", fp);
782 for(lp = info->replaces; lp; lp = lp->next) {
783 fprintf(fp, "%s\n", (char *)lp->data);
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 "%ld\n\n", info->builddate);
806 if(info->installdate) {
807 fprintf(fp, "%%INSTALLDATE%%\n"
808 "%ld\n\n", 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 fprintf(fp, "%s\n", (char *)lp->data);
844 fprintf(fp, "\n");
846 if(info->provides) {
847 fputs("%PROVIDES%\n", fp);
848 for(lp = info->provides; lp; lp = lp->next) {
849 fprintf(fp, "%s\n", (char *)lp->data);
851 fprintf(fp, "\n");
854 fclose(fp);
855 fp = NULL;
858 /* FILES */
859 if(inforeq & INFRQ_FILES) {
860 _alpm_log(db->handle, ALPM_LOG_DEBUG, "writing %s-%s FILES information back to db\n",
861 info->name, info->version);
862 snprintf(path, PATH_MAX, "%sfiles", pkgpath);
863 if((fp = fopen(path, "w")) == NULL) {
864 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
865 path, strerror(errno));
866 retval = -1;
867 goto cleanup;
869 if(info->files) {
870 fprintf(fp, "%%FILES%%\n");
871 for(lp = info->files; lp; lp = lp->next) {
872 fprintf(fp, "%s\n", (char *)lp->data);
874 fprintf(fp, "\n");
876 if(info->backup) {
877 fprintf(fp, "%%BACKUP%%\n");
878 for(lp = info->backup; lp; lp = lp->next) {
879 alpm_backup_t *backup = lp->data;
880 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
882 fprintf(fp, "\n");
884 fclose(fp);
885 fp = NULL;
888 /* INSTALL */
889 /* nothing needed here (script is automatically extracted) */
891 cleanup:
892 umask(oldmask);
893 free(pkgpath);
895 if(fp) {
896 fclose(fp);
899 return retval;
902 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
904 int ret = 0;
905 char *pkgpath = NULL;
907 pkgpath = get_pkgpath(db, info);
909 ret = _alpm_rmrf(pkgpath);
910 free(pkgpath);
911 if(ret != 0) {
912 ret = -1;
914 return ret;
917 struct db_operations local_db_ops = {
918 .validate = local_db_validate,
919 .populate = local_db_populate,
920 .unregister = _alpm_db_unregister,
923 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
925 alpm_db_t *db;
927 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
929 db = _alpm_db_new("local", 1);
930 if(db == NULL) {
931 handle->pm_errno = PM_ERR_DB_CREATE;
932 return NULL;
934 db->ops = &local_db_ops;
935 db->handle = handle;
937 if(local_db_validate(db)) {
938 /* pm_errno set in local_db_validate() */
939 _alpm_db_free(db);
940 return NULL;
943 handle->db_local = db;
944 return db;
947 /* vim: set ts=2 sw=2 noet: */