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 */
31 #include <limits.h> /* PATH_MAX */
35 #include "alpm_list.h"
43 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
);
45 #define LAZY_LOAD(info, errret) \
47 if(!(pkg->infolevel & info)) { \
48 local_db_read(pkg, info); \
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
59 static const char *_cache_get_desc(alpm_pkg_t
*pkg
)
61 LAZY_LOAD(INFRQ_DESC
, NULL
);
65 static const char *_cache_get_url(alpm_pkg_t
*pkg
)
67 LAZY_LOAD(INFRQ_DESC
, NULL
);
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
);
89 static const char *_cache_get_arch(alpm_pkg_t
*pkg
)
91 LAZY_LOAD(INFRQ_DESC
, NULL
);
95 static off_t
_cache_get_isize(alpm_pkg_t
*pkg
)
97 LAZY_LOAD(INFRQ_DESC
, -1);
101 static alpm_pkgreason_t
_cache_get_reason(alpm_pkg_t
*pkg
)
103 LAZY_LOAD(INFRQ_DESC
, -1);
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
);
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
);
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
);
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");
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
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
)
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",
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);
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
);
274 char buffer
[PATH_MAX
];
277 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
279 if(!stat(buffer
, &sbuf
)) {
280 return S_ISDIR(sbuf
.st_mode
);
287 static int local_db_validate(alpm_db_t
*db
)
289 struct dirent
*ent
= NULL
;
294 if(db
->status
& DB_STATUS_VALID
) {
297 if(db
->status
& DB_STATUS_INVALID
) {
301 dbpath
= _alpm_db_path(db
);
303 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
305 dbdir
= opendir(dbpath
);
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
;
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
;
325 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
328 if(!is_dir(dbpath
, ent
)) {
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
;
341 /* we found no depends file after full scan */
342 db
->status
|= DB_STATUS_VALID
;
343 db
->status
&= ~DB_STATUS_INVALID
;
354 static int local_db_populate(alpm_db_t
*db
)
359 struct dirent
*ent
= NULL
;
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
);
370 /* pm_errno set in _alpm_db_path() */
374 dbdir
= opendir(dbpath
);
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
;
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
;
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
397 while(readdir(dbdir
) != NULL
) {
403 /* subtract the two extra pointers to get # of children */
407 /* initialize hash at 50% full */
408 db
->pkgcache
= _alpm_pkghash_create(est_count
* 2);
409 if(db
->pkgcache
== NULL
){
411 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
414 while((ent
= readdir(dbdir
)) != NULL
) {
415 const char *name
= ent
->d_name
;
419 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
422 if(!is_dir(dbpath
, ent
)) {
426 pkg
= _alpm_pkg_new();
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"),
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
);
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
);
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
);
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
);
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
)
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
: "");
493 #define READ_NEXT() do { \
494 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
495 _alpm_strip_newline(line); \
498 #define READ_AND_STORE(f) do { \
500 STRDUP(f, line, goto error); \
503 #define READ_AND_STORE_ALL(f) do { \
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
)
526 alpm_db_t
*db
= info
->origin_data
.db
;
528 /* bitmask logic here:
529 * infolevel: 00001111
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 */
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. */
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
);
556 /* clear out 'line', to be certain - and to make valgrind happy */
557 memset(line
, 0, sizeof(line
));
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
));
569 if(fgets(line
, sizeof(line
), fp
) == NULL
&& !feof(fp
)) {
572 if(_alpm_strip_newline(line
) == 0) {
573 /* length of stripped line was zero */
576 if(strcmp(line
, "%NAME%") == 0) {
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) {
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) {
600 info
->builddate
= _alpm_parsedate(line
);
601 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
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) {
608 info
->reason
= (alpm_pkgreason_t
)atoi(line
);
609 } else if(strcmp(line
, "%SIZE%") == 0) {
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
);
626 info
->infolevel
|= INFRQ_DESC
;
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
));
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) {
653 files
= realloc(files
, sizeof(alpm_file_t
) * files_size
);
655 ALLOC_FAIL(sizeof(alpm_file_t
) * files_size
);
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
) {
670 memcpy(files
[files_count
].name
, line
, len
+ 1);
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
)) {
684 info
->backup
= alpm_list_add(info
->backup
, backup
);
690 info
->infolevel
|= INFRQ_FILES
;
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) {
700 info
->infolevel
|= INFRQ_SCRIPTLET
;
706 info
->infolevel
|= INFRQ_ERROR
;
713 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
719 if(checkdbdir(db
) != 0) {
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
));
737 int _alpm_local_db_write(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
744 if(db
== NULL
|| info
== NULL
|| !(db
->status
& DB_STATUS_LOCAL
)) {
748 /* make sure we have a sane umask */
749 oldmask
= umask(0022);
752 if(inforeq
& INFRQ_DESC
) {
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
));
765 fprintf(fp
, "%%NAME%%\n%s\n\n"
766 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
768 fprintf(fp
, "%%DESC%%\n"
769 "%s\n\n", info
->desc
);
772 fputs("%GROUPS%\n", fp
);
773 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
774 fprintf(fp
, "%s\n", (char *)lp
->data
);
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
);
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 "%jd\n\n", (intmax_t)info
->builddate
);
806 if(info
->installdate
) {
807 fprintf(fp
, "%%INSTALLDATE%%\n"
808 "%jd\n\n", (intmax_t)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 char *depstring
= alpm_dep_compute_string(lp
->data
);
843 fprintf(fp
, "%s\n", depstring
);
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
);
863 if(inforeq
& INFRQ_FILES
) {
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
));
876 if(info
->files
.count
) {
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
);
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
);
898 /* nothing needed here (script is automatically extracted) */
910 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
913 char *pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
915 /* TODO explicit file removes and then an rmdir? */
916 ret
= _alpm_rmrf(pkgpath
);
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
)
934 _alpm_log(handle
, ALPM_LOG_DEBUG
, "registering local database\n");
936 db
= _alpm_db_new("local", 1);
938 handle
->pm_errno
= ALPM_ERR_DB_CREATE
;
941 db
->ops
= &local_db_ops
;
944 if(local_db_validate(db
)) {
945 /* pm_errno set in local_db_validate() */
950 handle
->db_local
= db
;
954 /* vim: set ts=2 sw=2 noet: */