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/>.
26 #include <stdint.h> /* intmax_t */
29 #include <limits.h> /* PATH_MAX */
33 #include "alpm_list.h"
41 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
);
43 #define LAZY_LOAD(info, errret) \
45 if(!(pkg->infolevel & info)) { \
46 local_db_read(pkg, info); \
51 /* Cache-specific accessor functions. These implementations allow for lazy
52 * loading by the files backend when a data member is actually needed
53 * rather than loading all pieces of information when the package is first
57 static const char *_cache_get_desc(alpm_pkg_t
*pkg
)
59 LAZY_LOAD(INFRQ_DESC
, NULL
);
63 static const char *_cache_get_url(alpm_pkg_t
*pkg
)
65 LAZY_LOAD(INFRQ_DESC
, NULL
);
69 static alpm_time_t
_cache_get_builddate(alpm_pkg_t
*pkg
)
71 LAZY_LOAD(INFRQ_DESC
, 0);
72 return pkg
->builddate
;
75 static alpm_time_t
_cache_get_installdate(alpm_pkg_t
*pkg
)
77 LAZY_LOAD(INFRQ_DESC
, 0);
78 return pkg
->installdate
;
81 static const char *_cache_get_packager(alpm_pkg_t
*pkg
)
83 LAZY_LOAD(INFRQ_DESC
, NULL
);
87 static const char *_cache_get_arch(alpm_pkg_t
*pkg
)
89 LAZY_LOAD(INFRQ_DESC
, NULL
);
93 static off_t
_cache_get_isize(alpm_pkg_t
*pkg
)
95 LAZY_LOAD(INFRQ_DESC
, -1);
99 static alpm_pkgreason_t
_cache_get_reason(alpm_pkg_t
*pkg
)
101 LAZY_LOAD(INFRQ_DESC
, -1);
105 static alpm_list_t
*_cache_get_licenses(alpm_pkg_t
*pkg
)
107 LAZY_LOAD(INFRQ_DESC
, NULL
);
108 return pkg
->licenses
;
111 static alpm_list_t
*_cache_get_groups(alpm_pkg_t
*pkg
)
113 LAZY_LOAD(INFRQ_DESC
, NULL
);
117 static int _cache_has_scriptlet(alpm_pkg_t
*pkg
)
119 LAZY_LOAD(INFRQ_SCRIPTLET
, NULL
);
120 return pkg
->scriptlet
;
123 static alpm_list_t
*_cache_get_depends(alpm_pkg_t
*pkg
)
125 LAZY_LOAD(INFRQ_DESC
, NULL
);
129 static alpm_list_t
*_cache_get_optdepends(alpm_pkg_t
*pkg
)
131 LAZY_LOAD(INFRQ_DESC
, NULL
);
132 return pkg
->optdepends
;
135 static alpm_list_t
*_cache_get_conflicts(alpm_pkg_t
*pkg
)
137 LAZY_LOAD(INFRQ_DESC
, NULL
);
138 return pkg
->conflicts
;
141 static alpm_list_t
*_cache_get_provides(alpm_pkg_t
*pkg
)
143 LAZY_LOAD(INFRQ_DESC
, NULL
);
144 return pkg
->provides
;
147 static alpm_list_t
*_cache_get_replaces(alpm_pkg_t
*pkg
)
149 LAZY_LOAD(INFRQ_DESC
, NULL
);
150 return pkg
->replaces
;
153 static alpm_filelist_t
*_cache_get_files(alpm_pkg_t
*pkg
)
155 LAZY_LOAD(INFRQ_FILES
, NULL
);
156 return &(pkg
->files
);
159 static alpm_list_t
*_cache_get_backup(alpm_pkg_t
*pkg
)
161 LAZY_LOAD(INFRQ_FILES
, NULL
);
166 * Open a package changelog for reading. Similar to fopen in functionality,
167 * except that the returned 'file stream' is from the database.
168 * @param pkg the package (from db) to read the changelog
169 * @return a 'file stream' to the package changelog
171 static void *_cache_changelog_open(alpm_pkg_t
*pkg
)
173 alpm_db_t
*db
= alpm_pkg_get_db(pkg
);
174 char *clfile
= _alpm_local_db_pkgpath(db
, pkg
, "changelog");
175 FILE *f
= fopen(clfile
, "r");
181 * Read data from an open changelog 'file stream'. Similar to fread in
182 * functionality, this function takes a buffer and amount of data to read.
183 * @param ptr a buffer to fill with raw changelog data
184 * @param size the size of the buffer
185 * @param pkg the package that the changelog is being read from
186 * @param fp a 'file stream' to the package changelog
187 * @return the number of characters read, or 0 if there is no more data
189 static size_t _cache_changelog_read(void *ptr
, size_t size
,
190 const alpm_pkg_t UNUSED
*pkg
, void *fp
)
192 return fread(ptr
, 1, size
, (FILE *)fp
);
196 * Close a package changelog for reading. Similar to fclose in functionality,
197 * except that the 'file stream' is from the database.
198 * @param pkg the package that the changelog was read from
199 * @param fp a 'file stream' to the package changelog
200 * @return whether closing the package changelog stream was successful
202 static int _cache_changelog_close(const alpm_pkg_t UNUSED
*pkg
, void *fp
)
204 return fclose((FILE *)fp
);
207 static int _cache_force_load(alpm_pkg_t
*pkg
)
209 return local_db_read(pkg
, INFRQ_ALL
);
213 /** The local database operations struct. Get package fields through
214 * lazy accessor methods that handle any backend loading and caching
217 static struct pkg_operations local_pkg_ops
= {
218 .get_desc
= _cache_get_desc
,
219 .get_url
= _cache_get_url
,
220 .get_builddate
= _cache_get_builddate
,
221 .get_installdate
= _cache_get_installdate
,
222 .get_packager
= _cache_get_packager
,
223 .get_arch
= _cache_get_arch
,
224 .get_isize
= _cache_get_isize
,
225 .get_reason
= _cache_get_reason
,
226 .has_scriptlet
= _cache_has_scriptlet
,
227 .get_licenses
= _cache_get_licenses
,
228 .get_groups
= _cache_get_groups
,
229 .get_depends
= _cache_get_depends
,
230 .get_optdepends
= _cache_get_optdepends
,
231 .get_conflicts
= _cache_get_conflicts
,
232 .get_provides
= _cache_get_provides
,
233 .get_replaces
= _cache_get_replaces
,
234 .get_files
= _cache_get_files
,
235 .get_backup
= _cache_get_backup
,
237 .changelog_open
= _cache_changelog_open
,
238 .changelog_read
= _cache_changelog_read
,
239 .changelog_close
= _cache_changelog_close
,
241 .force_load
= _cache_force_load
,
244 static int checkdbdir(alpm_db_t
*db
)
247 const char *path
= _alpm_db_path(db
);
249 if(stat(path
, &buf
) != 0) {
250 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "database dir '%s' does not exist, creating it\n",
252 if(_alpm_makepath(path
) != 0) {
253 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
255 } else if(!S_ISDIR(buf
.st_mode
)) {
256 _alpm_log(db
->handle
, ALPM_LOG_WARNING
, _("removing invalid database: %s\n"), path
);
257 if(unlink(path
) != 0 || _alpm_makepath(path
) != 0) {
258 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
264 static int is_dir(const char *path
, struct dirent
*entry
)
266 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
267 if(entry
->d_type
!= DT_UNKNOWN
) {
268 return (entry
->d_type
== DT_DIR
);
272 char buffer
[PATH_MAX
];
275 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
277 if(!stat(buffer
, &sbuf
)) {
278 return S_ISDIR(sbuf
.st_mode
);
285 static int local_db_validate(alpm_db_t
*db
)
287 struct dirent
*ent
= NULL
;
292 if(db
->status
& DB_STATUS_VALID
) {
295 if(db
->status
& DB_STATUS_INVALID
) {
299 dbpath
= _alpm_db_path(db
);
301 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
303 dbdir
= opendir(dbpath
);
305 if(errno
== ENOENT
) {
306 /* database dir doesn't exist yet */
307 db
->status
|= DB_STATUS_VALID
;
308 db
->status
&= ~DB_STATUS_INVALID
;
309 db
->status
&= ~DB_STATUS_EXISTS
;
310 db
->status
|= DB_STATUS_MISSING
;
313 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
316 db
->status
|= DB_STATUS_EXISTS
;
317 db
->status
&= ~DB_STATUS_MISSING
;
319 while((ent
= readdir(dbdir
)) != NULL
) {
320 const char *name
= ent
->d_name
;
323 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
326 if(!is_dir(dbpath
, ent
)) {
330 snprintf(path
, PATH_MAX
, "%s%s/depends", dbpath
, name
);
331 if(access(path
, F_OK
) == 0) {
332 /* we found a depends file- bail */
333 db
->status
&= ~DB_STATUS_VALID
;
334 db
->status
|= DB_STATUS_INVALID
;
335 db
->handle
->pm_errno
= ALPM_ERR_DB_VERSION
;
339 /* we found no depends file after full scan */
340 db
->status
|= DB_STATUS_VALID
;
341 db
->status
&= ~DB_STATUS_INVALID
;
352 static int local_db_populate(alpm_db_t
*db
)
357 struct dirent
*ent
= NULL
;
361 if(db
->status
& DB_STATUS_INVALID
) {
362 RET_ERR(db
->handle
, ALPM_ERR_DB_INVALID
, -1);
364 /* note: DB_STATUS_MISSING is not fatal for local database */
366 dbpath
= _alpm_db_path(db
);
368 /* pm_errno set in _alpm_db_path() */
372 dbdir
= opendir(dbpath
);
374 if(errno
== ENOENT
) {
375 /* no database existing yet is not an error */
376 db
->status
&= ~DB_STATUS_EXISTS
;
377 db
->status
|= DB_STATUS_MISSING
;
380 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
382 if(fstat(dirfd(dbdir
), &buf
) != 0) {
383 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
385 db
->status
|= DB_STATUS_EXISTS
;
386 db
->status
&= ~DB_STATUS_MISSING
;
387 if(buf
.st_nlink
>= 2) {
388 est_count
= buf
.st_nlink
;
390 /* Some filesystems don't subscribe to the two-implicit links school of
391 * thought, e.g. BTRFS, HFS+. See
392 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
395 while(readdir(dbdir
) != NULL
) {
401 /* subtract the two extra pointers to get # of children */
405 db
->pkgcache
= _alpm_pkghash_create(est_count
);
406 if(db
->pkgcache
== NULL
){
408 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
411 while((ent
= readdir(dbdir
)) != NULL
) {
412 const char *name
= ent
->d_name
;
416 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
419 if(!is_dir(dbpath
, ent
)) {
423 pkg
= _alpm_pkg_new();
426 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
428 /* split the db entry name */
429 if(_alpm_splitname(name
, &(pkg
->name
), &(pkg
->version
),
430 &(pkg
->name_hash
)) != 0) {
431 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("invalid name for database entry '%s'\n"),
437 /* duplicated database entries are not allowed */
438 if(_alpm_pkghash_find(db
->pkgcache
, pkg
->name
)) {
439 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("duplicated database entry '%s'\n"), pkg
->name
);
444 pkg
->origin
= PKG_FROM_LOCALDB
;
445 pkg
->origin_data
.db
= db
;
446 pkg
->ops
= &local_pkg_ops
;
447 pkg
->handle
= db
->handle
;
449 /* explicitly read with only 'BASE' data, accessors will handle the rest */
450 if(local_db_read(pkg
, INFRQ_BASE
) == -1) {
451 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("corrupted database entry '%s'\n"), name
);
456 /* add to the collection */
457 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
, "adding '%s' to package cache for db '%s'\n",
458 pkg
->name
, db
->treename
);
459 db
->pkgcache
= _alpm_pkghash_add(db
->pkgcache
, pkg
);
465 db
->pkgcache
->list
= alpm_list_msort(db
->pkgcache
->list
, (size_t)count
, _alpm_pkg_cmp
);
467 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "added %d packages to package cache for db '%s'\n",
468 count
, db
->treename
);
473 /* Note: the return value must be freed by the caller */
474 char *_alpm_local_db_pkgpath(alpm_db_t
*db
, alpm_pkg_t
*info
,
475 const char *filename
)
481 dbpath
= _alpm_db_path(db
);
482 len
= strlen(dbpath
) + strlen(info
->name
) + strlen(info
->version
) + 3;
483 len
+= filename
? strlen(filename
) : 0;
484 MALLOC(pkgpath
, len
, RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, NULL
));
485 sprintf(pkgpath
, "%s%s-%s/%s", dbpath
, info
->name
, info
->version
,
486 filename
? filename
: "");
490 #define READ_NEXT() do { \
491 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
492 _alpm_strip_newline(line); \
495 #define READ_AND_STORE(f) do { \
497 STRDUP(f, line, goto error); \
500 #define READ_AND_STORE_ALL(f) do { \
502 if(fgets(line, sizeof(line), fp) == NULL) {\
503 if(!feof(fp)) goto error; else break; \
505 if(_alpm_strip_newline(line) == 0) break; \
506 STRDUP(linedup, line, goto error); \
507 f = alpm_list_add(f, linedup); \
508 } while(1) /* note the while(1) and not (0) */
510 #define READ_AND_SPLITDEP(f) do { \
511 if(fgets(line, sizeof(line), fp) == NULL) {\
512 if(!feof(fp)) goto error; else break; \
514 if(_alpm_strip_newline(line) == 0) break; \
515 f = alpm_list_add(f, _alpm_splitdep(line)); \
516 } while(1) /* note the while(1) and not (0) */
518 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
523 alpm_db_t
*db
= info
->origin_data
.db
;
525 /* bitmask logic here:
526 * infolevel: 00001111
529 * == to inforeq? nope, we need to load more info. */
530 if((info
->infolevel
& inforeq
) == inforeq
) {
531 /* already loaded all of this info, do nothing */
535 if(info
->infolevel
& INFRQ_ERROR
) {
536 /* We've encountered an error loading this package before. Don't attempt
537 * repeated reloads, just give up. */
541 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
, "loading package data for %s : level=0x%x\n",
542 info
->name
, inforeq
);
544 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
545 if(!pkgpath
|| access(pkgpath
, F_OK
)) {
546 /* directory doesn't exist or can't be opened */
547 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "cannot find '%s-%s' in db '%s'\n",
548 info
->name
, info
->version
, db
->treename
);
553 /* clear out 'line', to be certain - and to make valgrind happy */
554 memset(line
, 0, sizeof(line
));
557 if(inforeq
& INFRQ_DESC
&& !(info
->infolevel
& INFRQ_DESC
)) {
558 char *path
= _alpm_local_db_pkgpath(db
, info
, "desc");
559 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
560 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
566 if(fgets(line
, sizeof(line
), fp
) == NULL
&& !feof(fp
)) {
569 if(_alpm_strip_newline(line
) == 0) {
570 /* length of stripped line was zero */
573 if(strcmp(line
, "%NAME%") == 0) {
575 if(strcmp(line
, info
->name
) != 0) {
576 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: name "
577 "mismatch on package %s\n"), db
->treename
, info
->name
);
579 } else if(strcmp(line
, "%VERSION%") == 0) {
581 if(strcmp(line
, info
->version
) != 0) {
582 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: version "
583 "mismatch on package %s\n"), db
->treename
, info
->name
);
585 } else if(strcmp(line
, "%DESC%") == 0) {
586 READ_AND_STORE(info
->desc
);
587 } else if(strcmp(line
, "%GROUPS%") == 0) {
588 READ_AND_STORE_ALL(info
->groups
);
589 } else if(strcmp(line
, "%URL%") == 0) {
590 READ_AND_STORE(info
->url
);
591 } else if(strcmp(line
, "%LICENSE%") == 0) {
592 READ_AND_STORE_ALL(info
->licenses
);
593 } else if(strcmp(line
, "%ARCH%") == 0) {
594 READ_AND_STORE(info
->arch
);
595 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
597 info
->builddate
= _alpm_parsedate(line
);
598 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
600 info
->installdate
= _alpm_parsedate(line
);
601 } else if(strcmp(line
, "%PACKAGER%") == 0) {
602 READ_AND_STORE(info
->packager
);
603 } else if(strcmp(line
, "%REASON%") == 0) {
605 info
->reason
= (alpm_pkgreason_t
)atoi(line
);
606 } else if(strcmp(line
, "%SIZE%") == 0) {
608 info
->isize
= _alpm_strtoofft(line
);
609 } else if(strcmp(line
, "%REPLACES%") == 0) {
610 READ_AND_SPLITDEP(info
->replaces
);
611 } else if(strcmp(line
, "%DEPENDS%") == 0) {
612 READ_AND_SPLITDEP(info
->depends
);
613 } else if(strcmp(line
, "%OPTDEPENDS%") == 0) {
614 READ_AND_STORE_ALL(info
->optdepends
);
615 } else if(strcmp(line
, "%CONFLICTS%") == 0) {
616 READ_AND_SPLITDEP(info
->conflicts
);
617 } else if(strcmp(line
, "%PROVIDES%") == 0) {
618 READ_AND_SPLITDEP(info
->provides
);
623 info
->infolevel
|= INFRQ_DESC
;
627 if(inforeq
& INFRQ_FILES
&& !(info
->infolevel
& INFRQ_FILES
)) {
628 char *path
= _alpm_local_db_pkgpath(db
, info
, "files");
629 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
630 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
635 while(fgets(line
, sizeof(line
), fp
)) {
636 _alpm_strip_newline(line
);
637 if(strcmp(line
, "%FILES%") == 0) {
638 size_t files_count
= 0, files_size
= 0, len
;
639 alpm_file_t
*files
= NULL
;
641 while(fgets(line
, sizeof(line
), fp
) &&
642 (len
= _alpm_strip_newline(line
))) {
643 if(files_count
>= files_size
) {
644 size_t old_size
= files_size
;
645 if(files_size
== 0) {
650 files
= realloc(files
, sizeof(alpm_file_t
) * files_size
);
652 ALLOC_FAIL(sizeof(alpm_file_t
) * files_size
);
655 /* ensure all new memory is zeroed out, in both the initial
656 * allocation and later reallocs */
657 memset(files
+ old_size
, 0,
658 sizeof(alpm_file_t
) * (files_size
- old_size
));
660 /* since we know the length of the file string already,
661 * we can do malloc + memcpy rather than strdup */
662 files
[files_count
].name
= malloc(len
+ 1);
663 if(files
[files_count
].name
== NULL
) {
667 memcpy(files
[files_count
].name
, line
, len
+ 1);
670 /* attempt to hand back any memory we don't need */
671 files
= realloc(files
, sizeof(alpm_file_t
) * files_count
);
672 info
->files
.count
= files_count
;
673 info
->files
.files
= files
;
674 } else if(strcmp(line
, "%BACKUP%") == 0) {
675 while(fgets(line
, sizeof(line
), fp
) && _alpm_strip_newline(line
)) {
676 alpm_backup_t
*backup
;
677 CALLOC(backup
, 1, sizeof(alpm_backup_t
), goto error
);
678 if(_alpm_split_backup(line
, &backup
)) {
681 info
->backup
= alpm_list_add(info
->backup
, backup
);
687 info
->infolevel
|= INFRQ_FILES
;
691 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
692 char *path
= _alpm_local_db_pkgpath(db
, info
, "install");
693 if(access(path
, F_OK
) == 0) {
697 info
->infolevel
|= INFRQ_SCRIPTLET
;
703 info
->infolevel
|= INFRQ_ERROR
;
710 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
716 if(checkdbdir(db
) != 0) {
720 oldmask
= umask(0000);
721 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
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
)
741 if(db
== NULL
|| info
== NULL
|| !(db
->status
& DB_STATUS_LOCAL
)) {
745 /* make sure we have a sane umask */
746 oldmask
= umask(0022);
749 if(inforeq
& INFRQ_DESC
) {
751 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "writing %s-%s DESC information back to db\n",
752 info
->name
, info
->version
);
753 path
= _alpm_local_db_pkgpath(db
, info
, "desc");
754 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
755 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
756 path
, strerror(errno
));
762 fprintf(fp
, "%%NAME%%\n%s\n\n"
763 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
765 fprintf(fp
, "%%DESC%%\n"
766 "%s\n\n", info
->desc
);
769 fputs("%GROUPS%\n", fp
);
770 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
771 fprintf(fp
, "%s\n", (char *)lp
->data
);
776 fputs("%REPLACES%\n", fp
);
777 for(lp
= info
->replaces
; lp
; lp
= lp
->next
) {
778 char *depstring
= alpm_dep_compute_string(lp
->data
);
779 fprintf(fp
, "%s\n", depstring
);
785 fprintf(fp
, "%%URL%%\n"
786 "%s\n\n", info
->url
);
789 fputs("%LICENSE%\n", fp
);
790 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
791 fprintf(fp
, "%s\n", (char *)lp
->data
);
796 fprintf(fp
, "%%ARCH%%\n"
797 "%s\n\n", info
->arch
);
799 if(info
->builddate
) {
800 fprintf(fp
, "%%BUILDDATE%%\n"
801 "%jd\n\n", (intmax_t)info
->builddate
);
803 if(info
->installdate
) {
804 fprintf(fp
, "%%INSTALLDATE%%\n"
805 "%jd\n\n", (intmax_t)info
->installdate
);
808 fprintf(fp
, "%%PACKAGER%%\n"
809 "%s\n\n", info
->packager
);
812 /* only write installed size, csize is irrelevant once installed */
813 fprintf(fp
, "%%SIZE%%\n"
814 "%jd\n\n", (intmax_t)info
->isize
);
817 fprintf(fp
, "%%REASON%%\n"
818 "%u\n\n", info
->reason
);
821 fputs("%DEPENDS%\n", fp
);
822 for(lp
= info
->depends
; lp
; lp
= lp
->next
) {
823 char *depstring
= alpm_dep_compute_string(lp
->data
);
824 fprintf(fp
, "%s\n", depstring
);
829 if(info
->optdepends
) {
830 fputs("%OPTDEPENDS%\n", fp
);
831 for(lp
= info
->optdepends
; lp
; lp
= lp
->next
) {
832 fprintf(fp
, "%s\n", (char *)lp
->data
);
836 if(info
->conflicts
) {
837 fputs("%CONFLICTS%\n", fp
);
838 for(lp
= info
->conflicts
; lp
; lp
= lp
->next
) {
839 char *depstring
= alpm_dep_compute_string(lp
->data
);
840 fprintf(fp
, "%s\n", depstring
);
846 fputs("%PROVIDES%\n", fp
);
847 for(lp
= info
->provides
; lp
; lp
= lp
->next
) {
848 char *depstring
= alpm_dep_compute_string(lp
->data
);
849 fprintf(fp
, "%s\n", depstring
);
860 if(inforeq
& INFRQ_FILES
) {
862 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "writing %s-%s FILES information back to db\n",
863 info
->name
, info
->version
);
864 path
= _alpm_local_db_pkgpath(db
, info
, "files");
865 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
866 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
867 path
, strerror(errno
));
873 if(info
->files
.count
) {
875 fprintf(fp
, "%%FILES%%\n");
876 for(i
= 0; i
< info
->files
.count
; i
++) {
877 const alpm_file_t
*file
= info
->files
.files
+ i
;
878 fprintf(fp
, "%s\n", file
->name
);
883 fprintf(fp
, "%%BACKUP%%\n");
884 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
885 const alpm_backup_t
*backup
= lp
->data
;
886 fprintf(fp
, "%s\t%s\n", backup
->name
, backup
->hash
);
895 /* nothing needed here (script is automatically extracted) */
907 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
915 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
919 pkgpath_len
= strlen(pkgpath
);
921 dirp
= opendir(pkgpath
);
925 /* go through the local DB entry, removing the files within, which we know
926 * are not nested directories of any kind. */
927 for(dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)) {
928 if(strcmp(dp
->d_name
, "..") != 0 && strcmp(dp
->d_name
, ".") != 0) {
930 if(pkgpath_len
+ strlen(dp
->d_name
) + 2 > PATH_MAX
) {
931 /* file path is too long to remove, hmm. */
934 sprintf(name
, "%s/%s", pkgpath
, dp
->d_name
);
943 /* after removing all enclosed files, we can remove the directory itself. */
951 int SYMEXPORT
alpm_pkg_set_reason(alpm_pkg_t
*pkg
, alpm_pkgreason_t reason
)
953 ASSERT(pkg
!= NULL
, return -1);
954 ASSERT(pkg
->origin
== PKG_FROM_LOCALDB
,
955 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
956 ASSERT(pkg
->origin_data
.db
== pkg
->handle
->db_local
,
957 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
959 _alpm_log(pkg
->handle
, ALPM_LOG_DEBUG
,
960 "setting install reason %u for %s\n", reason
, pkg
->name
);
961 if(alpm_pkg_get_reason(pkg
) == reason
) {
965 /* set reason (in pkgcache) */
966 pkg
->reason
= reason
;
968 if(_alpm_local_db_write(pkg
->handle
->db_local
, pkg
, INFRQ_DESC
)) {
969 RET_ERR(pkg
->handle
, ALPM_ERR_DB_WRITE
, -1);
975 struct db_operations local_db_ops
= {
976 .validate
= local_db_validate
,
977 .populate
= local_db_populate
,
978 .unregister
= _alpm_db_unregister
,
981 alpm_db_t
*_alpm_db_register_local(alpm_handle_t
*handle
)
985 _alpm_log(handle
, ALPM_LOG_DEBUG
, "registering local database\n");
987 db
= _alpm_db_new("local", 1);
989 handle
->pm_errno
= ALPM_ERR_DB_CREATE
;
992 db
->ops
= &local_db_ops
;
995 if(local_db_validate(db
)) {
996 /* pm_errno set in local_db_validate() */
1001 handle
->db_local
= db
;
1005 /* vim: set ts=2 sw=2 noet: */