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/>.
28 #include <stdint.h> /* intmax_t */
32 #include <limits.h> /* PATH_MAX */
36 #include "alpm_list.h"
45 #define LAZY_LOAD(info, errret) \
47 ASSERT(handle != NULL, return (errret)); \
48 if(pkg->origin != PKG_FROM_FILE && !(pkg->infolevel & info)) { \
49 _alpm_local_db_read(pkg->origin_data.db, pkg, info); \
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
60 static const char *_cache_get_filename(pmpkg_t
*pkg
)
62 LAZY_LOAD(INFRQ_DESC
, NULL
);
66 static const char *_cache_get_desc(pmpkg_t
*pkg
)
68 LAZY_LOAD(INFRQ_DESC
, NULL
);
72 static const char *_cache_get_url(pmpkg_t
*pkg
)
74 LAZY_LOAD(INFRQ_DESC
, NULL
);
78 static time_t _cache_get_builddate(pmpkg_t
*pkg
)
80 LAZY_LOAD(INFRQ_DESC
, 0);
81 return pkg
->builddate
;
84 static time_t _cache_get_installdate(pmpkg_t
*pkg
)
86 LAZY_LOAD(INFRQ_DESC
, 0);
87 return pkg
->installdate
;
90 static const char *_cache_get_packager(pmpkg_t
*pkg
)
92 LAZY_LOAD(INFRQ_DESC
, NULL
);
96 static const char *_cache_get_md5sum(pmpkg_t
*pkg
)
98 LAZY_LOAD(INFRQ_DESC
, NULL
);
102 static const char *_cache_get_arch(pmpkg_t
*pkg
)
104 LAZY_LOAD(INFRQ_DESC
, NULL
);
108 static off_t
_cache_get_size(pmpkg_t
*pkg
)
110 LAZY_LOAD(INFRQ_DESC
, -1);
114 static off_t
_cache_get_isize(pmpkg_t
*pkg
)
116 LAZY_LOAD(INFRQ_DESC
, -1);
120 static pmpkgreason_t
_cache_get_reason(pmpkg_t
*pkg
)
122 LAZY_LOAD(INFRQ_DESC
, -1);
126 static alpm_list_t
*_cache_get_licenses(pmpkg_t
*pkg
)
128 LAZY_LOAD(INFRQ_DESC
, NULL
);
129 return pkg
->licenses
;
132 static alpm_list_t
*_cache_get_groups(pmpkg_t
*pkg
)
134 LAZY_LOAD(INFRQ_DESC
, NULL
);
138 static int _cache_has_scriptlet(pmpkg_t
*pkg
)
141 ASSERT(handle
!= NULL
, return -1);
143 if(!(pkg
->infolevel
& INFRQ_SCRIPTLET
)) {
144 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_SCRIPTLET
);
146 return pkg
->scriptlet
;
149 static alpm_list_t
*_cache_get_depends(pmpkg_t
*pkg
)
151 LAZY_LOAD(INFRQ_DESC
, NULL
);
155 static alpm_list_t
*_cache_get_optdepends(pmpkg_t
*pkg
)
157 LAZY_LOAD(INFRQ_DESC
, NULL
);
158 return pkg
->optdepends
;
161 static alpm_list_t
*_cache_get_conflicts(pmpkg_t
*pkg
)
163 LAZY_LOAD(INFRQ_DESC
, NULL
);
164 return pkg
->conflicts
;
167 static alpm_list_t
*_cache_get_provides(pmpkg_t
*pkg
)
169 LAZY_LOAD(INFRQ_DESC
, NULL
);
170 return pkg
->provides
;
173 static alpm_list_t
*_cache_get_replaces(pmpkg_t
*pkg
)
175 LAZY_LOAD(INFRQ_DESC
, NULL
);
176 return pkg
->replaces
;
179 /* local packages can not have deltas */
180 static alpm_list_t
*_cache_get_deltas(pmpkg_t UNUSED
*pkg
)
185 static alpm_list_t
*_cache_get_files(pmpkg_t
*pkg
)
188 ASSERT(handle
!= NULL
, return NULL
);
190 if(pkg
->origin
== PKG_FROM_LOCALDB
191 && !(pkg
->infolevel
& INFRQ_FILES
)) {
192 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_FILES
);
197 static alpm_list_t
*_cache_get_backup(pmpkg_t
*pkg
)
200 ASSERT(handle
!= NULL
, return NULL
);
202 if(pkg
->origin
== PKG_FROM_LOCALDB
203 && !(pkg
->infolevel
& INFRQ_FILES
)) {
204 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_FILES
);
210 * Open a package changelog for reading. Similar to fopen in functionality,
211 * except that the returned 'file stream' is from the database.
212 * @param pkg the package (from db) to read the changelog
213 * @return a 'file stream' to the package changelog
215 static void *_cache_changelog_open(pmpkg_t
*pkg
)
218 ASSERT(handle
!= NULL
, return NULL
);
220 char clfile
[PATH_MAX
];
221 snprintf(clfile
, PATH_MAX
, "%s/%s/%s-%s/changelog",
222 alpm_option_get_dbpath(),
223 alpm_db_get_name(alpm_pkg_get_db(pkg
)),
224 alpm_pkg_get_name(pkg
),
225 alpm_pkg_get_version(pkg
));
226 return fopen(clfile
, "r");
230 * Read data from an open changelog 'file stream'. Similar to fread in
231 * functionality, this function takes a buffer and amount of data to read.
232 * @param ptr a buffer to fill with raw changelog data
233 * @param size the size of the buffer
234 * @param pkg the package that the changelog is being read from
235 * @param fp a 'file stream' to the package changelog
236 * @return the number of characters read, or 0 if there is no more data
238 static size_t _cache_changelog_read(void *ptr
, size_t size
,
239 const pmpkg_t UNUSED
*pkg
, const void *fp
)
241 return fread(ptr
, 1, size
, (FILE *)fp
);
245 * Close a package changelog for reading. Similar to fclose in functionality,
246 * except that the 'file stream' is from the database.
247 * @param pkg the package that the changelog was read from
248 * @param fp a 'file stream' to the package changelog
249 * @return whether closing the package changelog stream was successful
251 static int _cache_changelog_close(const pmpkg_t UNUSED
*pkg
, void *fp
)
253 return fclose((FILE *)fp
);
257 /** The local database operations struct. Get package fields through
258 * lazy accessor methods that handle any backend loading and caching
261 static struct pkg_operations local_pkg_ops
= {
262 .get_filename
= _cache_get_filename
,
263 .get_desc
= _cache_get_desc
,
264 .get_url
= _cache_get_url
,
265 .get_builddate
= _cache_get_builddate
,
266 .get_installdate
= _cache_get_installdate
,
267 .get_packager
= _cache_get_packager
,
268 .get_md5sum
= _cache_get_md5sum
,
269 .get_arch
= _cache_get_arch
,
270 .get_size
= _cache_get_size
,
271 .get_isize
= _cache_get_isize
,
272 .get_reason
= _cache_get_reason
,
273 .has_scriptlet
= _cache_has_scriptlet
,
274 .get_licenses
= _cache_get_licenses
,
275 .get_groups
= _cache_get_groups
,
276 .get_depends
= _cache_get_depends
,
277 .get_optdepends
= _cache_get_optdepends
,
278 .get_conflicts
= _cache_get_conflicts
,
279 .get_provides
= _cache_get_provides
,
280 .get_replaces
= _cache_get_replaces
,
281 .get_deltas
= _cache_get_deltas
,
282 .get_files
= _cache_get_files
,
283 .get_backup
= _cache_get_backup
,
285 .changelog_open
= _cache_changelog_open
,
286 .changelog_read
= _cache_changelog_read
,
287 .changelog_close
= _cache_changelog_close
,
290 static int checkdbdir(pmdb_t
*db
)
293 const char *path
= _alpm_db_path(db
);
295 if(stat(path
, &buf
) != 0) {
296 _alpm_log(PM_LOG_DEBUG
, "database dir '%s' does not exist, creating it\n",
298 if(_alpm_makepath(path
) != 0) {
299 RET_ERR(PM_ERR_SYSTEM
, -1);
301 } else if(!S_ISDIR(buf
.st_mode
)) {
302 _alpm_log(PM_LOG_WARNING
, _("removing invalid database: %s\n"), path
);
303 if(unlink(path
) != 0 || _alpm_makepath(path
) != 0) {
304 RET_ERR(PM_ERR_SYSTEM
, -1);
310 static int is_dir(const char *path
, struct dirent
*entry
)
312 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
313 if(entry
->d_type
!= DT_UNKNOWN
) {
314 return (entry
->d_type
== DT_DIR
);
318 char buffer
[PATH_MAX
];
321 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
323 if(!stat(buffer
, &sbuf
)) {
324 return S_ISDIR(sbuf
.st_mode
);
331 static int local_db_populate(pmdb_t
*db
)
336 struct dirent
*ent
= NULL
;
340 ASSERT(db
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
342 dbpath
= _alpm_db_path(db
);
344 /* pm_errno set in _alpm_db_path() */
347 dbdir
= opendir(dbpath
);
349 if(errno
== ENOENT
) {
350 /* no database existing yet is not an error */
353 RET_ERR(PM_ERR_DB_OPEN
, -1);
355 if(fstat(dirfd(dbdir
), &buf
) != 0) {
356 RET_ERR(PM_ERR_DB_OPEN
, -1);
358 if(buf
.st_nlink
>= 2) {
359 est_count
= buf
.st_nlink
;
361 /* Some filesystems don't subscribe to the two-implicit links school of
362 * thought, e.g. BTRFS, HFS+. See
363 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
366 while(readdir(dbdir
) != NULL
) {
372 /* subtract the two extra pointers to get # of children */
376 /* initialize hash at 50% full */
377 db
->pkgcache
= _alpm_pkghash_create(est_count
* 2);
378 if(db
->pkgcache
== NULL
){
380 RET_ERR(PM_ERR_MEMORY
, -1);
383 while((ent
= readdir(dbdir
)) != NULL
) {
384 const char *name
= ent
->d_name
;
388 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
391 if(!is_dir(dbpath
, ent
)) {
395 pkg
= _alpm_pkg_new();
398 RET_ERR(PM_ERR_MEMORY
, -1);
400 /* split the db entry name */
401 if(_alpm_splitname(name
, pkg
) != 0) {
402 _alpm_log(PM_LOG_ERROR
, _("invalid name for database entry '%s'\n"),
408 /* duplicated database entries are not allowed */
409 if(_alpm_pkghash_find(db
->pkgcache
, pkg
->name
)) {
410 _alpm_log(PM_LOG_ERROR
, _("duplicated database entry '%s'\n"), pkg
->name
);
415 pkg
->origin
= PKG_FROM_LOCALDB
;
416 pkg
->origin_data
.db
= db
;
417 pkg
->ops
= &local_pkg_ops
;
419 /* explicitly read with only 'BASE' data, accessors will handle the rest */
420 if(_alpm_local_db_read(db
, pkg
, INFRQ_BASE
) == -1) {
421 _alpm_log(PM_LOG_ERROR
, _("corrupted database entry '%s'\n"), name
);
426 /* add to the collection */
427 _alpm_log(PM_LOG_FUNCTION
, "adding '%s' to package cache for db '%s'\n",
428 pkg
->name
, db
->treename
);
429 db
->pkgcache
= _alpm_pkghash_add(db
->pkgcache
, pkg
);
435 db
->pkgcache
->list
= alpm_list_msort(db
->pkgcache
->list
, (size_t)count
, _alpm_pkg_cmp
);
437 _alpm_log(PM_LOG_DEBUG
, "added %d packages to package cache for db '%s'\n",
438 count
, db
->treename
);
443 /* Note: the return value must be freed by the caller */
444 static char *get_pkgpath(pmdb_t
*db
, pmpkg_t
*info
)
450 dbpath
= _alpm_db_path(db
);
451 len
= strlen(dbpath
) + strlen(info
->name
) + strlen(info
->version
) + 3;
452 MALLOC(pkgpath
, len
, RET_ERR(PM_ERR_MEMORY
, NULL
));
453 sprintf(pkgpath
, "%s%s-%s/", dbpath
, info
->name
, info
->version
);
458 int _alpm_local_db_read(pmdb_t
*db
, pmpkg_t
*info
, pmdbinfrq_t inforeq
)
463 char *pkgpath
= NULL
;
466 RET_ERR(PM_ERR_DB_NULL
, -1);
469 if(info
== NULL
|| info
->name
== NULL
|| info
->version
== NULL
) {
470 _alpm_log(PM_LOG_DEBUG
, "invalid package entry provided to _alpm_local_db_read, skipping\n");
474 if(info
->origin
!= PKG_FROM_LOCALDB
) {
475 _alpm_log(PM_LOG_DEBUG
,
476 "request to read info for a non-local package '%s', skipping...\n",
481 /* bitmask logic here:
482 * infolevel: 00001111
485 * == to inforeq? nope, we need to load more info. */
486 if((info
->infolevel
& inforeq
) == inforeq
) {
487 /* already loaded all of this info, do nothing */
490 _alpm_log(PM_LOG_FUNCTION
, "loading package data for %s : level=0x%x\n",
491 info
->name
, inforeq
);
493 /* clear out 'line', to be certain - and to make valgrind happy */
494 memset(line
, 0, sizeof(line
));
496 pkgpath
= get_pkgpath(db
, info
);
498 if(access(pkgpath
, F_OK
)) {
499 /* directory doesn't exist or can't be opened */
500 _alpm_log(PM_LOG_DEBUG
, "cannot find '%s-%s' in db '%s'\n",
501 info
->name
, info
->version
, db
->treename
);
506 if(inforeq
& INFRQ_DESC
&& !(info
->infolevel
& INFRQ_DESC
)) {
507 snprintf(path
, PATH_MAX
, "%sdesc", pkgpath
);
508 if((fp
= fopen(path
, "r")) == NULL
) {
509 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
513 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
517 if(strcmp(line
, "%NAME%") == 0) {
518 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
521 if(strcmp(_alpm_strtrim(line
), info
->name
) != 0) {
522 _alpm_log(PM_LOG_ERROR
, _("%s database is inconsistent: name "
523 "mismatch on package %s\n"), db
->treename
, info
->name
);
525 } else if(strcmp(line
, "%VERSION%") == 0) {
526 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
529 if(strcmp(_alpm_strtrim(line
), info
->version
) != 0) {
530 _alpm_log(PM_LOG_ERROR
, _("%s database is inconsistent: version "
531 "mismatch on package %s\n"), db
->treename
, info
->name
);
533 } else if(strcmp(line
, "%DESC%") == 0) {
534 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
537 STRDUP(info
->desc
, _alpm_strtrim(line
), goto error
);
538 } else if(strcmp(line
, "%GROUPS%") == 0) {
539 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
541 STRDUP(linedup
, line
, goto error
);
542 info
->groups
= alpm_list_add(info
->groups
, linedup
);
544 } else if(strcmp(line
, "%URL%") == 0) {
545 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
548 STRDUP(info
->url
, _alpm_strtrim(line
), goto error
);
549 } else if(strcmp(line
, "%LICENSE%") == 0) {
550 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
552 STRDUP(linedup
, line
, goto error
);
553 info
->licenses
= alpm_list_add(info
->licenses
, linedup
);
555 } else if(strcmp(line
, "%ARCH%") == 0) {
556 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
559 STRDUP(info
->arch
, _alpm_strtrim(line
), goto error
);
560 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
561 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
565 info
->builddate
= _alpm_parsedate(line
);
566 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
567 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
571 info
->installdate
= _alpm_parsedate(line
);
572 } else if(strcmp(line
, "%PACKAGER%") == 0) {
573 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
576 STRDUP(info
->packager
, _alpm_strtrim(line
), goto error
);
577 } else if(strcmp(line
, "%REASON%") == 0) {
578 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
581 info
->reason
= (pmpkgreason_t
)atol(_alpm_strtrim(line
));
582 } else if(strcmp(line
, "%SIZE%") == 0) {
583 /* NOTE: the CSIZE and SIZE fields both share the "size" field
584 * in the pkginfo_t struct. This can be done b/c CSIZE
585 * is currently only used in sync databases, and SIZE is
586 * only used in local databases.
588 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
591 info
->size
= atol(_alpm_strtrim(line
));
592 /* also store this value to isize */
593 info
->isize
= info
->size
;
594 } else if(strcmp(line
, "%REPLACES%") == 0) {
595 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
597 STRDUP(linedup
, line
, goto error
);
598 info
->replaces
= alpm_list_add(info
->replaces
, linedup
);
600 } else if(strcmp(line
, "%DEPENDS%") == 0) {
601 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
602 pmdepend_t
*dep
= _alpm_splitdep(line
);
603 info
->depends
= alpm_list_add(info
->depends
, dep
);
605 } else if(strcmp(line
, "%OPTDEPENDS%") == 0) {
606 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
608 STRDUP(linedup
, line
, goto error
);
609 info
->optdepends
= alpm_list_add(info
->optdepends
, linedup
);
611 } else if(strcmp(line
, "%CONFLICTS%") == 0) {
612 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
614 STRDUP(linedup
, line
, goto error
);
615 info
->conflicts
= alpm_list_add(info
->conflicts
, linedup
);
617 } else if(strcmp(line
, "%PROVIDES%") == 0) {
618 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
620 STRDUP(linedup
, line
, goto error
);
621 info
->provides
= alpm_list_add(info
->provides
, linedup
);
630 if(inforeq
& INFRQ_FILES
&& !(info
->infolevel
& INFRQ_FILES
)) {
631 snprintf(path
, PATH_MAX
, "%sfiles", pkgpath
);
632 if((fp
= fopen(path
, "r")) == NULL
) {
633 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
636 while(fgets(line
, sizeof(line
), fp
)) {
638 if(strcmp(line
, "%FILES%") == 0) {
639 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
641 STRDUP(linedup
, line
, goto error
);
642 info
->files
= alpm_list_add(info
->files
, linedup
);
644 } else if(strcmp(line
, "%BACKUP%") == 0) {
645 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
647 STRDUP(linedup
, line
, goto error
);
648 info
->backup
= alpm_list_add(info
->backup
, linedup
);
657 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
658 snprintf(path
, PATH_MAX
, "%sinstall", pkgpath
);
659 if(access(path
, F_OK
) == 0) {
665 info
->infolevel
|= inforeq
;
678 int _alpm_local_db_prepare(pmdb_t
*db
, pmpkg_t
*info
)
682 char *pkgpath
= NULL
;
684 if(checkdbdir(db
) != 0) {
688 oldmask
= umask(0000);
689 pkgpath
= get_pkgpath(db
, info
);
691 if((retval
= mkdir(pkgpath
, 0755)) != 0) {
692 _alpm_log(PM_LOG_ERROR
, _("could not create directory %s: %s\n"),
693 pkgpath
, strerror(errno
));
702 int _alpm_local_db_write(pmdb_t
*db
, pmpkg_t
*info
, pmdbinfrq_t inforeq
)
707 alpm_list_t
*lp
= NULL
;
709 char *pkgpath
= NULL
;
711 if(db
== NULL
|| info
== NULL
) {
715 pkgpath
= get_pkgpath(db
, info
);
717 /* make sure we have a sane umask */
718 oldmask
= umask(0022);
720 if(strcmp(db
->treename
, "local") != 0) {
725 if(inforeq
& INFRQ_DESC
) {
726 _alpm_log(PM_LOG_DEBUG
, "writing %s-%s DESC information back to db\n",
727 info
->name
, info
->version
);
728 snprintf(path
, PATH_MAX
, "%sdesc", pkgpath
);
729 if((fp
= fopen(path
, "w")) == NULL
) {
730 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
734 fprintf(fp
, "%%NAME%%\n%s\n\n"
735 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
737 fprintf(fp
, "%%DESC%%\n"
738 "%s\n\n", info
->desc
);
741 fputs("%GROUPS%\n", fp
);
742 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
743 fprintf(fp
, "%s\n", (char *)lp
->data
);
748 fputs("%REPLACES%\n", fp
);
749 for(lp
= info
->replaces
; lp
; lp
= lp
->next
) {
750 fprintf(fp
, "%s\n", (char *)lp
->data
);
755 fprintf(fp
, "%%URL%%\n"
756 "%s\n\n", info
->url
);
759 fputs("%LICENSE%\n", fp
);
760 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
761 fprintf(fp
, "%s\n", (char *)lp
->data
);
766 fprintf(fp
, "%%ARCH%%\n"
767 "%s\n\n", info
->arch
);
769 if(info
->builddate
) {
770 fprintf(fp
, "%%BUILDDATE%%\n"
771 "%ld\n\n", info
->builddate
);
773 if(info
->installdate
) {
774 fprintf(fp
, "%%INSTALLDATE%%\n"
775 "%ld\n\n", info
->installdate
);
778 fprintf(fp
, "%%PACKAGER%%\n"
779 "%s\n\n", info
->packager
);
782 /* only write installed size, csize is irrelevant once installed */
783 fprintf(fp
, "%%SIZE%%\n"
784 "%jd\n\n", (intmax_t)info
->isize
);
787 fprintf(fp
, "%%REASON%%\n"
788 "%u\n\n", info
->reason
);
791 fputs("%DEPENDS%\n", fp
);
792 for(lp
= info
->depends
; lp
; lp
= lp
->next
) {
793 char *depstring
= alpm_dep_compute_string(lp
->data
);
794 fprintf(fp
, "%s\n", depstring
);
799 if(info
->optdepends
) {
800 fputs("%OPTDEPENDS%\n", fp
);
801 for(lp
= info
->optdepends
; lp
; lp
= lp
->next
) {
802 fprintf(fp
, "%s\n", (char *)lp
->data
);
806 if(info
->conflicts
) {
807 fputs("%CONFLICTS%\n", fp
);
808 for(lp
= info
->conflicts
; lp
; lp
= lp
->next
) {
809 fprintf(fp
, "%s\n", (char *)lp
->data
);
814 fputs("%PROVIDES%\n", fp
);
815 for(lp
= info
->provides
; lp
; lp
= lp
->next
) {
816 fprintf(fp
, "%s\n", (char *)lp
->data
);
826 if(inforeq
& INFRQ_FILES
) {
827 _alpm_log(PM_LOG_DEBUG
, "writing %s-%s FILES information back to db\n",
828 info
->name
, info
->version
);
829 snprintf(path
, PATH_MAX
, "%sfiles", pkgpath
);
830 if((fp
= fopen(path
, "w")) == NULL
) {
831 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
836 fprintf(fp
, "%%FILES%%\n");
837 for(lp
= info
->files
; lp
; lp
= lp
->next
) {
838 fprintf(fp
, "%s\n", (char *)lp
->data
);
843 fprintf(fp
, "%%BACKUP%%\n");
844 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
845 fprintf(fp
, "%s\n", (char *)lp
->data
);
854 /* nothing needed here (script is automatically extracted) */
867 int _alpm_local_db_remove(pmdb_t
*db
, pmpkg_t
*info
)
870 char *pkgpath
= NULL
;
872 if(db
== NULL
|| info
== NULL
) {
873 RET_ERR(PM_ERR_DB_NULL
, -1);
876 pkgpath
= get_pkgpath(db
, info
);
878 ret
= _alpm_rmrf(pkgpath
);
886 static int local_db_version(pmdb_t
*db
)
888 struct dirent
*ent
= NULL
;
893 dbpath
= _alpm_db_path(db
);
895 RET_ERR(PM_ERR_DB_OPEN
, -1);
897 dbdir
= opendir(dbpath
);
899 if(errno
== ENOENT
) {
900 /* database dir doesn't exist yet */
904 RET_ERR(PM_ERR_DB_OPEN
, -1);
908 while((ent
= readdir(dbdir
)) != NULL
) {
909 const char *name
= ent
->d_name
;
912 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
915 if(!is_dir(dbpath
, ent
)) {
919 snprintf(path
, PATH_MAX
, "%s%s/depends", dbpath
, name
);
920 if(access(path
, F_OK
) == 0) {
921 /* we found a depends file- bail */
926 /* we found no depends file after full scan */
934 _alpm_log(PM_LOG_DEBUG
, "local database version %d\n", version
);
938 struct db_operations local_db_ops
= {
939 .populate
= local_db_populate
,
940 .unregister
= _alpm_db_unregister
,
941 .version
= local_db_version
,
944 pmdb_t
*_alpm_db_register_local(void)
948 _alpm_log(PM_LOG_DEBUG
, "registering local database\n");
950 db
= _alpm_db_new("local", 1);
952 RET_ERR(PM_ERR_DB_CREATE
, NULL
);
954 db
->ops
= &local_db_ops
;
956 handle
->db_local
= db
;
960 /* vim: set ts=2 sw=2 noet: */