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 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
);
46 #define LAZY_LOAD(info, errret) \
48 if(!(pkg->infolevel & info)) { \
49 local_db_read(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(alpm_pkg_t
*pkg
)
62 LAZY_LOAD(INFRQ_DESC
, NULL
);
66 static const char *_cache_get_desc(alpm_pkg_t
*pkg
)
68 LAZY_LOAD(INFRQ_DESC
, NULL
);
72 static const char *_cache_get_url(alpm_pkg_t
*pkg
)
74 LAZY_LOAD(INFRQ_DESC
, NULL
);
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
);
96 static const char *_cache_get_md5sum(alpm_pkg_t
*pkg
)
98 LAZY_LOAD(INFRQ_DESC
, NULL
);
102 static const char *_cache_get_arch(alpm_pkg_t
*pkg
)
104 LAZY_LOAD(INFRQ_DESC
, NULL
);
108 static off_t
_cache_get_size(alpm_pkg_t
*pkg
)
110 LAZY_LOAD(INFRQ_DESC
, -1);
114 static off_t
_cache_get_isize(alpm_pkg_t
*pkg
)
116 LAZY_LOAD(INFRQ_DESC
, -1);
120 static alpm_pkgreason_t
_cache_get_reason(alpm_pkg_t
*pkg
)
122 LAZY_LOAD(INFRQ_DESC
, -1);
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
);
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
);
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 LAZY_LOAD(INFRQ_FILES
, NULL
);
186 static alpm_list_t
*_cache_get_backup(alpm_pkg_t
*pkg
)
188 LAZY_LOAD(INFRQ_FILES
, NULL
);
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
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
)
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",
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);
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
);
305 char buffer
[PATH_MAX
];
308 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
310 if(!stat(buffer
, &sbuf
)) {
311 return S_ISDIR(sbuf
.st_mode
);
318 static int local_db_validate(alpm_db_t
*db
)
320 struct dirent
*ent
= NULL
;
325 if(db
->status
& DB_STATUS_VALID
) {
329 dbpath
= _alpm_db_path(db
);
331 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
333 dbdir
= opendir(dbpath
);
335 if(errno
== ENOENT
) {
336 /* database dir doesn't exist yet */
337 db
->status
|= DB_STATUS_VALID
;
340 RET_ERR(db
->handle
, PM_ERR_DB_OPEN
, -1);
344 while((ent
= readdir(dbdir
)) != NULL
) {
345 const char *name
= ent
->d_name
;
348 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
351 if(!is_dir(dbpath
, ent
)) {
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
;
362 /* we found no depends file after full scan */
363 db
->status
|= DB_STATUS_VALID
;
374 static int local_db_populate(alpm_db_t
*db
)
379 struct dirent
*ent
= NULL
;
383 dbpath
= _alpm_db_path(db
);
385 /* pm_errno set in _alpm_db_path() */
389 dbdir
= opendir(dbpath
);
391 if(errno
== ENOENT
) {
392 /* no database existing yet is not an error */
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
;
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
408 while(readdir(dbdir
) != NULL
) {
414 /* subtract the two extra pointers to get # of children */
418 /* initialize hash at 50% full */
419 db
->pkgcache
= _alpm_pkghash_create(est_count
* 2);
420 if(db
->pkgcache
== NULL
){
422 RET_ERR(db
->handle
, PM_ERR_MEMORY
, -1);
425 while((ent
= readdir(dbdir
)) != NULL
) {
426 const char *name
= ent
->d_name
;
430 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
433 if(!is_dir(dbpath
, ent
)) {
437 pkg
= _alpm_pkg_new();
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"),
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
);
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
);
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
);
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
);
487 /* Note: the return value must be freed by the caller */
488 static char *get_pkgpath(alpm_db_t
*db
, alpm_pkg_t
*info
)
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
);
502 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
507 char *pkgpath
= NULL
;
508 alpm_db_t
*db
= info
->origin_data
.db
;
510 /* bitmask logic here:
511 * infolevel: 00001111
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 */
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
);
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
));
542 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
546 if(strcmp(line
, "%NAME%") == 0) {
547 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
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
) {
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
) {
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
))) {
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
) {
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
))) {
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
) {
588 STRDUP(info
->arch
, _alpm_strtrim(line
), goto error
);
589 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
590 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
594 info
->builddate
= _alpm_parsedate(line
);
595 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
596 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
600 info
->installdate
= _alpm_parsedate(line
);
601 } else if(strcmp(line
, "%PACKAGER%") == 0) {
602 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
605 STRDUP(info
->packager
, _alpm_strtrim(line
), goto error
);
606 } else if(strcmp(line
, "%REASON%") == 0) {
607 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
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
) {
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
))) {
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
))) {
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
))) {
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
))) {
649 STRDUP(linedup
, line
, goto error
);
650 info
->provides
= alpm_list_add(info
->provides
, linedup
);
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
));
665 while(fgets(line
, sizeof(line
), fp
)) {
667 if(strcmp(line
, "%FILES%") == 0) {
668 while(fgets(line
, sizeof(line
), fp
) && strlen(_alpm_strtrim(line
))) {
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
)) {
680 info
->backup
= alpm_list_add(info
->backup
, backup
);
689 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
690 snprintf(path
, PATH_MAX
, "%sinstall", pkgpath
);
691 if(access(path
, F_OK
) == 0) {
697 info
->infolevel
|= inforeq
;
710 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
714 char *pkgpath
= NULL
;
716 if(checkdbdir(db
) != 0) {
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
));
734 int _alpm_local_db_write(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
739 alpm_list_t
*lp
= NULL
;
741 char *pkgpath
= NULL
;
743 if(db
== NULL
|| info
== NULL
) {
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) {
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
));
767 fprintf(fp
, "%%NAME%%\n%s\n\n"
768 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
770 fprintf(fp
, "%%DESC%%\n"
771 "%s\n\n", info
->desc
);
774 fputs("%GROUPS%\n", fp
);
775 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
776 fprintf(fp
, "%s\n", (char *)lp
->data
);
781 fputs("%REPLACES%\n", fp
);
782 for(lp
= info
->replaces
; lp
; lp
= lp
->next
) {
783 fprintf(fp
, "%s\n", (char *)lp
->data
);
788 fprintf(fp
, "%%URL%%\n"
789 "%s\n\n", info
->url
);
792 fputs("%LICENSE%\n", fp
);
793 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
794 fprintf(fp
, "%s\n", (char *)lp
->data
);
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
);
811 fprintf(fp
, "%%PACKAGER%%\n"
812 "%s\n\n", info
->packager
);
815 /* only write installed size, csize is irrelevant once installed */
816 fprintf(fp
, "%%SIZE%%\n"
817 "%jd\n\n", (intmax_t)info
->isize
);
820 fprintf(fp
, "%%REASON%%\n"
821 "%u\n\n", info
->reason
);
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
);
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
);
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
);
847 fputs("%PROVIDES%\n", fp
);
848 for(lp
= info
->provides
; lp
; lp
= lp
->next
) {
849 fprintf(fp
, "%s\n", (char *)lp
->data
);
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
));
870 fprintf(fp
, "%%FILES%%\n");
871 for(lp
= info
->files
; lp
; lp
= lp
->next
) {
872 fprintf(fp
, "%s\n", (char *)lp
->data
);
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
);
889 /* nothing needed here (script is automatically extracted) */
902 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
905 char *pkgpath
= NULL
;
907 pkgpath
= get_pkgpath(db
, info
);
909 ret
= _alpm_rmrf(pkgpath
);
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
)
927 _alpm_log(handle
, ALPM_LOG_DEBUG
, "registering local database\n");
929 db
= _alpm_db_new("local", 1);
931 handle
->pm_errno
= PM_ERR_DB_CREATE
;
934 db
->ops
= &local_db_ops
;
937 if(local_db_validate(db
)) {
938 /* pm_errno set in local_db_validate() */
943 handle
->db_local
= db
;
947 /* vim: set ts=2 sw=2 noet: */