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/>.
28 #include <stdint.h> /* intmax_t */
32 #include <limits.h> /* PATH_MAX */
36 #include "alpm_list.h"
44 #define LAZY_LOAD(info, errret) \
46 if(pkg->origin != PKG_FROM_FILE && !(pkg->infolevel & info)) { \
47 _alpm_local_db_read(pkg->origin_data.db, pkg, info); \
52 /* Cache-specific accessor functions. These implementations allow for lazy
53 * loading by the files backend when a data member is actually needed
54 * rather than loading all pieces of information when the package is first
58 static const char *_cache_get_filename(alpm_pkg_t
*pkg
)
60 LAZY_LOAD(INFRQ_DESC
, NULL
);
64 static const char *_cache_get_desc(alpm_pkg_t
*pkg
)
66 LAZY_LOAD(INFRQ_DESC
, NULL
);
70 static const char *_cache_get_url(alpm_pkg_t
*pkg
)
72 LAZY_LOAD(INFRQ_DESC
, NULL
);
76 static time_t _cache_get_builddate(alpm_pkg_t
*pkg
)
78 LAZY_LOAD(INFRQ_DESC
, 0);
79 return pkg
->builddate
;
82 static time_t _cache_get_installdate(alpm_pkg_t
*pkg
)
84 LAZY_LOAD(INFRQ_DESC
, 0);
85 return pkg
->installdate
;
88 static const char *_cache_get_packager(alpm_pkg_t
*pkg
)
90 LAZY_LOAD(INFRQ_DESC
, NULL
);
94 static const char *_cache_get_md5sum(alpm_pkg_t
*pkg
)
96 LAZY_LOAD(INFRQ_DESC
, NULL
);
100 static const char *_cache_get_arch(alpm_pkg_t
*pkg
)
102 LAZY_LOAD(INFRQ_DESC
, NULL
);
106 static off_t
_cache_get_size(alpm_pkg_t
*pkg
)
108 LAZY_LOAD(INFRQ_DESC
, -1);
112 static off_t
_cache_get_isize(alpm_pkg_t
*pkg
)
114 LAZY_LOAD(INFRQ_DESC
, -1);
118 static alpm_pkgreason_t
_cache_get_reason(alpm_pkg_t
*pkg
)
120 LAZY_LOAD(INFRQ_DESC
, -1);
124 static alpm_list_t
*_cache_get_licenses(alpm_pkg_t
*pkg
)
126 LAZY_LOAD(INFRQ_DESC
, NULL
);
127 return pkg
->licenses
;
130 static alpm_list_t
*_cache_get_groups(alpm_pkg_t
*pkg
)
132 LAZY_LOAD(INFRQ_DESC
, NULL
);
136 static int _cache_has_scriptlet(alpm_pkg_t
*pkg
)
138 if(!(pkg
->infolevel
& INFRQ_SCRIPTLET
)) {
139 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_SCRIPTLET
);
141 return pkg
->scriptlet
;
144 static alpm_list_t
*_cache_get_depends(alpm_pkg_t
*pkg
)
146 LAZY_LOAD(INFRQ_DESC
, NULL
);
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
)
180 static alpm_list_t
*_cache_get_files(alpm_pkg_t
*pkg
)
182 if(pkg
->origin
== PKG_FROM_LOCALDB
183 && !(pkg
->infolevel
& INFRQ_FILES
)) {
184 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_FILES
);
189 static alpm_list_t
*_cache_get_backup(alpm_pkg_t
*pkg
)
191 if(pkg
->origin
== PKG_FROM_LOCALDB
192 && !(pkg
->infolevel
& INFRQ_FILES
)) {
193 _alpm_local_db_read(pkg
->origin_data
.db
, pkg
, INFRQ_FILES
);
199 * Open a package changelog for reading. Similar to fopen in functionality,
200 * except that the returned 'file stream' is from the database.
201 * @param pkg the package (from db) to read the changelog
202 * @return a 'file stream' to the package changelog
204 static void *_cache_changelog_open(alpm_pkg_t
*pkg
)
206 char clfile
[PATH_MAX
];
207 snprintf(clfile
, PATH_MAX
, "%s/%s/%s-%s/changelog",
208 alpm_option_get_dbpath(pkg
->handle
),
209 alpm_db_get_name(alpm_pkg_get_db(pkg
)),
210 alpm_pkg_get_name(pkg
),
211 alpm_pkg_get_version(pkg
));
212 return fopen(clfile
, "r");
216 * Read data from an open changelog 'file stream'. Similar to fread in
217 * functionality, this function takes a buffer and amount of data to read.
218 * @param ptr a buffer to fill with raw changelog data
219 * @param size the size of the buffer
220 * @param pkg the package that the changelog is being read from
221 * @param fp a 'file stream' to the package changelog
222 * @return the number of characters read, or 0 if there is no more data
224 static size_t _cache_changelog_read(void *ptr
, size_t size
,
225 const alpm_pkg_t UNUSED
*pkg
, const void *fp
)
227 return fread(ptr
, 1, size
, (FILE *)fp
);
231 * Close a package changelog for reading. Similar to fclose in functionality,
232 * except that the 'file stream' is from the database.
233 * @param pkg the package that the changelog was read from
234 * @param fp a 'file stream' to the package changelog
235 * @return whether closing the package changelog stream was successful
237 static int _cache_changelog_close(const alpm_pkg_t UNUSED
*pkg
, void *fp
)
239 return fclose((FILE *)fp
);
243 /** The local database operations struct. Get package fields through
244 * lazy accessor methods that handle any backend loading and caching
247 static struct pkg_operations local_pkg_ops
= {
248 .get_filename
= _cache_get_filename
,
249 .get_desc
= _cache_get_desc
,
250 .get_url
= _cache_get_url
,
251 .get_builddate
= _cache_get_builddate
,
252 .get_installdate
= _cache_get_installdate
,
253 .get_packager
= _cache_get_packager
,
254 .get_md5sum
= _cache_get_md5sum
,
255 .get_arch
= _cache_get_arch
,
256 .get_size
= _cache_get_size
,
257 .get_isize
= _cache_get_isize
,
258 .get_reason
= _cache_get_reason
,
259 .has_scriptlet
= _cache_has_scriptlet
,
260 .get_licenses
= _cache_get_licenses
,
261 .get_groups
= _cache_get_groups
,
262 .get_depends
= _cache_get_depends
,
263 .get_optdepends
= _cache_get_optdepends
,
264 .get_conflicts
= _cache_get_conflicts
,
265 .get_provides
= _cache_get_provides
,
266 .get_replaces
= _cache_get_replaces
,
267 .get_deltas
= _cache_get_deltas
,
268 .get_files
= _cache_get_files
,
269 .get_backup
= _cache_get_backup
,
271 .changelog_open
= _cache_changelog_open
,
272 .changelog_read
= _cache_changelog_read
,
273 .changelog_close
= _cache_changelog_close
,
276 static int checkdbdir(alpm_db_t
*db
)
279 const char *path
= _alpm_db_path(db
);
281 if(stat(path
, &buf
) != 0) {
282 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "database dir '%s' does not exist, creating it\n",
284 if(_alpm_makepath(path
) != 0) {
285 RET_ERR(db
->handle
, PM_ERR_SYSTEM
, -1);
287 } else if(!S_ISDIR(buf
.st_mode
)) {
288 _alpm_log(db
->handle
, PM_LOG_WARNING
, _("removing invalid database: %s\n"), path
);
289 if(unlink(path
) != 0 || _alpm_makepath(path
) != 0) {
290 RET_ERR(db
->handle
, PM_ERR_SYSTEM
, -1);
296 static int is_dir(const char *path
, struct dirent
*entry
)
298 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
299 if(entry
->d_type
!= DT_UNKNOWN
) {
300 return (entry
->d_type
== DT_DIR
);
304 char buffer
[PATH_MAX
];
307 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
309 if(!stat(buffer
, &sbuf
)) {
310 return S_ISDIR(sbuf
.st_mode
);
317 static int local_db_validate(alpm_db_t
*db
)
319 struct dirent
*ent
= NULL
;
324 if(db
->status
& DB_STATUS_VALID
) {
328 dbpath
= _alpm_db_path(db
);
330 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
332 dbdir
= opendir(dbpath
);
334 if(errno
== ENOENT
) {
335 /* database dir doesn't exist yet */
336 db
->status
|= DB_STATUS_VALID
;
339 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
343 while((ent
= readdir(dbdir
)) != NULL
) {
344 const char *name
= ent
->d_name
;
347 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
350 if(!is_dir(dbpath
, ent
)) {
354 snprintf(path
, PATH_MAX
, "%s%s/depends", dbpath
, name
);
355 if(access(path
, F_OK
) == 0) {
356 /* we found a depends file- bail */
357 db
->handle
->pm_errno
= PM_ERR_DB_VERSION
;
361 /* we found no depends file after full scan */
362 db
->status
|= DB_STATUS_VALID
;
373 static int local_db_populate(alpm_db_t
*db
)
378 struct dirent
*ent
= NULL
;
382 dbpath
= _alpm_db_path(db
);
384 /* pm_errno set in _alpm_db_path() */
388 dbdir
= opendir(dbpath
);
390 if(errno
== ENOENT
) {
391 /* no database existing yet is not an error */
394 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
396 if(fstat(dirfd(dbdir
), &buf
) != 0) {
397 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
399 if(buf
.st_nlink
>= 2) {
400 est_count
= buf
.st_nlink
;
402 /* Some filesystems don't subscribe to the two-implicit links school of
403 * thought, e.g. BTRFS, HFS+. See
404 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
407 while(readdir(dbdir
) != NULL
) {
413 /* subtract the two extra pointers to get # of children */
417 /* initialize hash at 50% full */
418 db
->pkgcache
= _alpm_pkghash_create(est_count
* 2);
419 if(db
->pkgcache
== NULL
){
421 RET_ERR(db
->handle
, PM_ERR_MEMORY
, -1);
424 while((ent
= readdir(dbdir
)) != NULL
) {
425 const char *name
= ent
->d_name
;
429 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
432 if(!is_dir(dbpath
, ent
)) {
436 pkg
= _alpm_pkg_new();
439 RET_ERR(db
->handle
, PM_ERR_MEMORY
, -1);
441 /* split the db entry name */
442 if(_alpm_splitname(name
, &(pkg
->name
), &(pkg
->version
),
443 &(pkg
->name_hash
)) != 0) {
444 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("invalid name for database entry '%s'\n"),
450 /* duplicated database entries are not allowed */
451 if(_alpm_pkghash_find(db
->pkgcache
, pkg
->name
)) {
452 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("duplicated database entry '%s'\n"), pkg
->name
);
457 pkg
->origin
= PKG_FROM_LOCALDB
;
458 pkg
->origin_data
.db
= db
;
459 pkg
->ops
= &local_pkg_ops
;
460 pkg
->handle
= db
->handle
;
462 /* explicitly read with only 'BASE' data, accessors will handle the rest */
463 if(_alpm_local_db_read(db
, pkg
, INFRQ_BASE
) == -1) {
464 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("corrupted database entry '%s'\n"), name
);
469 /* add to the collection */
470 _alpm_log(db
->handle
, PM_LOG_FUNCTION
, "adding '%s' to package cache for db '%s'\n",
471 pkg
->name
, db
->treename
);
472 db
->pkgcache
= _alpm_pkghash_add(db
->pkgcache
, pkg
);
478 db
->pkgcache
->list
= alpm_list_msort(db
->pkgcache
->list
, (size_t)count
, _alpm_pkg_cmp
);
480 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "added %d packages to package cache for db '%s'\n",
481 count
, db
->treename
);
486 /* Note: the return value must be freed by the caller */
487 static char *get_pkgpath(alpm_db_t
*db
, alpm_pkg_t
*info
)
493 dbpath
= _alpm_db_path(db
);
494 len
= strlen(dbpath
) + strlen(info
->name
) + strlen(info
->version
) + 3;
495 MALLOC(pkgpath
, len
, RET_ERR(db
->handle
, PM_ERR_MEMORY
, NULL
));
496 sprintf(pkgpath
, "%s%s-%s/", dbpath
, info
->name
, info
->version
);
501 int _alpm_local_db_read(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
506 char *pkgpath
= NULL
;
508 if(info
== NULL
|| info
->name
== NULL
|| info
->version
== NULL
) {
509 _alpm_log(db
->handle
, PM_LOG_DEBUG
,
510 "invalid package entry provided to _alpm_local_db_read, skipping\n");
514 if(info
->origin
!= PKG_FROM_LOCALDB
) {
515 _alpm_log(db
->handle
, PM_LOG_DEBUG
,
516 "request to read info for a non-local package '%s', skipping...\n",
521 /* bitmask logic here:
522 * infolevel: 00001111
525 * == to inforeq? nope, we need to load more info. */
526 if((info
->infolevel
& inforeq
) == inforeq
) {
527 /* already loaded all of this info, do nothing */
530 _alpm_log(db
->handle
, PM_LOG_FUNCTION
, "loading package data for %s : level=0x%x\n",
531 info
->name
, inforeq
);
533 /* clear out 'line', to be certain - and to make valgrind happy */
534 memset(line
, 0, sizeof(line
));
536 pkgpath
= get_pkgpath(db
, info
);
538 if(access(pkgpath
, F_OK
)) {
539 /* directory doesn't exist or can't be opened */
540 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "cannot find '%s-%s' in db '%s'\n",
541 info
->name
, info
->version
, db
->treename
);
546 if(inforeq
& INFRQ_DESC
&& !(info
->infolevel
& INFRQ_DESC
)) {
547 snprintf(path
, PATH_MAX
, "%sdesc", pkgpath
);
548 if((fp
= fopen(path
, "r")) == NULL
) {
549 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
553 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
557 if(strcmp(line
, "%NAME%") == 0) {
558 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
561 if(strcmp(_alpm_strtrim(line
), info
->name
) != 0) {
562 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("%s database is inconsistent: name "
563 "mismatch on package %s\n"), db
->treename
, info
->name
);
565 } else if(strcmp(line
, "%VERSION%") == 0) {
566 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
569 if(strcmp(_alpm_strtrim(line
), info
->version
) != 0) {
570 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("%s database is inconsistent: version "
571 "mismatch on package %s\n"), db
->treename
, info
->name
);
573 } else if(strcmp(line
, "%DESC%") == 0) {
574 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
577 STRDUP(info
->desc
, _alpm_strtrim(line
), goto error
);
578 } else if(strcmp(line
, "%GROUPS%") == 0) {
579 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
581 STRDUP(linedup
, line
, goto error
);
582 info
->groups
= alpm_list_add(info
->groups
, linedup
);
584 } else if(strcmp(line
, "%URL%") == 0) {
585 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
588 STRDUP(info
->url
, _alpm_strtrim(line
), goto error
);
589 } else if(strcmp(line
, "%LICENSE%") == 0) {
590 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
592 STRDUP(linedup
, line
, goto error
);
593 info
->licenses
= alpm_list_add(info
->licenses
, linedup
);
595 } else if(strcmp(line
, "%ARCH%") == 0) {
596 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
599 STRDUP(info
->arch
, _alpm_strtrim(line
), goto error
);
600 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
601 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
605 info
->builddate
= _alpm_parsedate(line
);
606 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
607 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
611 info
->installdate
= _alpm_parsedate(line
);
612 } else if(strcmp(line
, "%PACKAGER%") == 0) {
613 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
616 STRDUP(info
->packager
, _alpm_strtrim(line
), goto error
);
617 } else if(strcmp(line
, "%REASON%") == 0) {
618 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
621 info
->reason
= (alpm_pkgreason_t
)atol(_alpm_strtrim(line
));
622 } else if(strcmp(line
, "%SIZE%") == 0) {
623 /* NOTE: the CSIZE and SIZE fields both share the "size" field
624 * in the pkginfo_t struct. This can be done b/c CSIZE
625 * is currently only used in sync databases, and SIZE is
626 * only used in local databases.
628 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
631 info
->size
= atol(_alpm_strtrim(line
));
632 /* also store this value to isize */
633 info
->isize
= info
->size
;
634 } else if(strcmp(line
, "%REPLACES%") == 0) {
635 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
637 STRDUP(linedup
, line
, goto error
);
638 info
->replaces
= alpm_list_add(info
->replaces
, linedup
);
640 } else if(strcmp(line
, "%DEPENDS%") == 0) {
641 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
642 alpm_depend_t
*dep
= _alpm_splitdep(line
);
643 info
->depends
= alpm_list_add(info
->depends
, dep
);
645 } else if(strcmp(line
, "%OPTDEPENDS%") == 0) {
646 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
648 STRDUP(linedup
, line
, goto error
);
649 info
->optdepends
= alpm_list_add(info
->optdepends
, linedup
);
651 } else if(strcmp(line
, "%CONFLICTS%") == 0) {
652 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
654 STRDUP(linedup
, line
, goto error
);
655 info
->conflicts
= alpm_list_add(info
->conflicts
, linedup
);
657 } else if(strcmp(line
, "%PROVIDES%") == 0) {
658 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
660 STRDUP(linedup
, line
, goto error
);
661 info
->provides
= alpm_list_add(info
->provides
, linedup
);
670 if(inforeq
& INFRQ_FILES
&& !(info
->infolevel
& INFRQ_FILES
)) {
671 snprintf(path
, PATH_MAX
, "%sfiles", pkgpath
);
672 if((fp
= fopen(path
, "r")) == NULL
) {
673 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
676 while(fgets(line
, sizeof(line
), fp
)) {
678 if(strcmp(line
, "%FILES%") == 0) {
679 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
681 STRDUP(linedup
, line
, goto error
);
682 info
->files
= alpm_list_add(info
->files
, linedup
);
684 } else if(strcmp(line
, "%BACKUP%") == 0) {
685 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
686 alpm_backup_t
*backup
;
687 CALLOC(backup
, 1, sizeof(alpm_backup_t
), goto error
);
688 if(_alpm_split_backup(line
, &backup
)) {
691 info
->backup
= alpm_list_add(info
->backup
, backup
);
700 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
701 snprintf(path
, PATH_MAX
, "%sinstall", pkgpath
);
702 if(access(path
, F_OK
) == 0) {
708 info
->infolevel
|= inforeq
;
721 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
725 char *pkgpath
= NULL
;
727 if(checkdbdir(db
) != 0) {
731 oldmask
= umask(0000);
732 pkgpath
= get_pkgpath(db
, info
);
734 if((retval
= mkdir(pkgpath
, 0755)) != 0) {
735 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("could not create directory %s: %s\n"),
736 pkgpath
, strerror(errno
));
745 int _alpm_local_db_write(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
750 alpm_list_t
*lp
= NULL
;
752 char *pkgpath
= NULL
;
754 if(db
== NULL
|| info
== NULL
) {
758 pkgpath
= get_pkgpath(db
, info
);
760 /* make sure we have a sane umask */
761 oldmask
= umask(0022);
763 if(strcmp(db
->treename
, "local") != 0) {
768 if(inforeq
& INFRQ_DESC
) {
769 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "writing %s-%s DESC information back to db\n",
770 info
->name
, info
->version
);
771 snprintf(path
, PATH_MAX
, "%sdesc", pkgpath
);
772 if((fp
= fopen(path
, "w")) == NULL
) {
773 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("could not open file %s: %s\n"),
774 path
, strerror(errno
));
778 fprintf(fp
, "%%NAME%%\n%s\n\n"
779 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
781 fprintf(fp
, "%%DESC%%\n"
782 "%s\n\n", info
->desc
);
785 fputs("%GROUPS%\n", fp
);
786 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
787 fprintf(fp
, "%s\n", (char *)lp
->data
);
792 fputs("%REPLACES%\n", fp
);
793 for(lp
= info
->replaces
; lp
; lp
= lp
->next
) {
794 fprintf(fp
, "%s\n", (char *)lp
->data
);
799 fprintf(fp
, "%%URL%%\n"
800 "%s\n\n", info
->url
);
803 fputs("%LICENSE%\n", fp
);
804 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
805 fprintf(fp
, "%s\n", (char *)lp
->data
);
810 fprintf(fp
, "%%ARCH%%\n"
811 "%s\n\n", info
->arch
);
813 if(info
->builddate
) {
814 fprintf(fp
, "%%BUILDDATE%%\n"
815 "%ld\n\n", info
->builddate
);
817 if(info
->installdate
) {
818 fprintf(fp
, "%%INSTALLDATE%%\n"
819 "%ld\n\n", info
->installdate
);
822 fprintf(fp
, "%%PACKAGER%%\n"
823 "%s\n\n", info
->packager
);
826 /* only write installed size, csize is irrelevant once installed */
827 fprintf(fp
, "%%SIZE%%\n"
828 "%jd\n\n", (intmax_t)info
->isize
);
831 fprintf(fp
, "%%REASON%%\n"
832 "%u\n\n", info
->reason
);
835 fputs("%DEPENDS%\n", fp
);
836 for(lp
= info
->depends
; lp
; lp
= lp
->next
) {
837 char *depstring
= alpm_dep_compute_string(lp
->data
);
838 fprintf(fp
, "%s\n", depstring
);
843 if(info
->optdepends
) {
844 fputs("%OPTDEPENDS%\n", fp
);
845 for(lp
= info
->optdepends
; lp
; lp
= lp
->next
) {
846 fprintf(fp
, "%s\n", (char *)lp
->data
);
850 if(info
->conflicts
) {
851 fputs("%CONFLICTS%\n", fp
);
852 for(lp
= info
->conflicts
; lp
; lp
= lp
->next
) {
853 fprintf(fp
, "%s\n", (char *)lp
->data
);
858 fputs("%PROVIDES%\n", fp
);
859 for(lp
= info
->provides
; lp
; lp
= lp
->next
) {
860 fprintf(fp
, "%s\n", (char *)lp
->data
);
870 if(inforeq
& INFRQ_FILES
) {
871 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "writing %s-%s FILES information back to db\n",
872 info
->name
, info
->version
);
873 snprintf(path
, PATH_MAX
, "%sfiles", pkgpath
);
874 if((fp
= fopen(path
, "w")) == NULL
) {
875 _alpm_log(db
->handle
, PM_LOG_ERROR
, _("could not open file %s: %s\n"),
876 path
, strerror(errno
));
881 fprintf(fp
, "%%FILES%%\n");
882 for(lp
= info
->files
; lp
; lp
= lp
->next
) {
883 fprintf(fp
, "%s\n", (char *)lp
->data
);
888 fprintf(fp
, "%%BACKUP%%\n");
889 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
890 alpm_backup_t
*backup
= lp
->data
;
891 fprintf(fp
, "%s\t%s\n", backup
->name
, backup
->hash
);
900 /* nothing needed here (script is automatically extracted) */
913 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
916 char *pkgpath
= NULL
;
918 pkgpath
= get_pkgpath(db
, info
);
920 ret
= _alpm_rmrf(pkgpath
);
928 struct db_operations local_db_ops
= {
929 .populate
= local_db_populate
,
930 .unregister
= _alpm_db_unregister
,
933 alpm_db_t
*_alpm_db_register_local(alpm_handle_t
*handle
)
937 _alpm_log(handle
, PM_LOG_DEBUG
, "registering local database\n");
939 db
= _alpm_db_new("local", 1);
941 handle
->pm_errno
= PM_ERR_DB_CREATE
;
944 db
->ops
= &local_db_ops
;
947 if(local_db_validate(db
)) {
948 /* pm_errno set in local_db_validate() */
953 handle
->db_local
= db
;
957 /* vim: set ts=2 sw=2 noet: */