2 * be_local.c : backend for the local database
4 * Copyright (c) 2006-2012 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"
42 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
);
44 #define LAZY_LOAD(info, errret) \
46 if(!(pkg->infolevel & info)) { \
47 local_db_read(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_desc(alpm_pkg_t
*pkg
)
60 LAZY_LOAD(INFRQ_DESC
, NULL
);
64 static const char *_cache_get_url(alpm_pkg_t
*pkg
)
66 LAZY_LOAD(INFRQ_DESC
, NULL
);
70 static alpm_time_t
_cache_get_builddate(alpm_pkg_t
*pkg
)
72 LAZY_LOAD(INFRQ_DESC
, 0);
73 return pkg
->builddate
;
76 static alpm_time_t
_cache_get_installdate(alpm_pkg_t
*pkg
)
78 LAZY_LOAD(INFRQ_DESC
, 0);
79 return pkg
->installdate
;
82 static const char *_cache_get_packager(alpm_pkg_t
*pkg
)
84 LAZY_LOAD(INFRQ_DESC
, NULL
);
88 static const char *_cache_get_arch(alpm_pkg_t
*pkg
)
90 LAZY_LOAD(INFRQ_DESC
, NULL
);
94 static off_t
_cache_get_isize(alpm_pkg_t
*pkg
)
96 LAZY_LOAD(INFRQ_DESC
, -1);
100 static alpm_pkgreason_t
_cache_get_reason(alpm_pkg_t
*pkg
)
102 LAZY_LOAD(INFRQ_DESC
, -1);
106 static alpm_pkgvalidation_t
_cache_get_validation(alpm_pkg_t
*pkg
)
108 LAZY_LOAD(INFRQ_DESC
, -1);
109 return pkg
->validation
;
112 static alpm_list_t
*_cache_get_licenses(alpm_pkg_t
*pkg
)
114 LAZY_LOAD(INFRQ_DESC
, NULL
);
115 return pkg
->licenses
;
118 static alpm_list_t
*_cache_get_groups(alpm_pkg_t
*pkg
)
120 LAZY_LOAD(INFRQ_DESC
, NULL
);
124 static int _cache_has_scriptlet(alpm_pkg_t
*pkg
)
126 LAZY_LOAD(INFRQ_SCRIPTLET
, NULL
);
127 return pkg
->scriptlet
;
130 static alpm_list_t
*_cache_get_depends(alpm_pkg_t
*pkg
)
132 LAZY_LOAD(INFRQ_DESC
, NULL
);
136 static alpm_list_t
*_cache_get_optdepends(alpm_pkg_t
*pkg
)
138 LAZY_LOAD(INFRQ_DESC
, NULL
);
139 return pkg
->optdepends
;
142 static alpm_list_t
*_cache_get_conflicts(alpm_pkg_t
*pkg
)
144 LAZY_LOAD(INFRQ_DESC
, NULL
);
145 return pkg
->conflicts
;
148 static alpm_list_t
*_cache_get_provides(alpm_pkg_t
*pkg
)
150 LAZY_LOAD(INFRQ_DESC
, NULL
);
151 return pkg
->provides
;
154 static alpm_list_t
*_cache_get_replaces(alpm_pkg_t
*pkg
)
156 LAZY_LOAD(INFRQ_DESC
, NULL
);
157 return pkg
->replaces
;
160 static alpm_filelist_t
*_cache_get_files(alpm_pkg_t
*pkg
)
162 LAZY_LOAD(INFRQ_FILES
, NULL
);
163 return &(pkg
->files
);
166 static alpm_list_t
*_cache_get_backup(alpm_pkg_t
*pkg
)
168 LAZY_LOAD(INFRQ_FILES
, NULL
);
173 * Open a package changelog for reading. Similar to fopen in functionality,
174 * except that the returned 'file stream' is from the database.
175 * @param pkg the package (from db) to read the changelog
176 * @return a 'file stream' to the package changelog
178 static void *_cache_changelog_open(alpm_pkg_t
*pkg
)
180 alpm_db_t
*db
= alpm_pkg_get_db(pkg
);
181 char *clfile
= _alpm_local_db_pkgpath(db
, pkg
, "changelog");
182 FILE *f
= fopen(clfile
, "r");
188 * Read data from an open changelog 'file stream'. Similar to fread in
189 * functionality, this function takes a buffer and amount of data to read.
190 * @param ptr a buffer to fill with raw changelog data
191 * @param size the size of the buffer
192 * @param pkg the package that the changelog is being read from
193 * @param fp a 'file stream' to the package changelog
194 * @return the number of characters read, or 0 if there is no more data
196 static size_t _cache_changelog_read(void *ptr
, size_t size
,
197 const alpm_pkg_t UNUSED
*pkg
, void *fp
)
199 return fread(ptr
, 1, size
, (FILE *)fp
);
203 * Close a package changelog for reading. Similar to fclose in functionality,
204 * except that the 'file stream' is from the database.
205 * @param pkg the package that the changelog was read from
206 * @param fp a 'file stream' to the package changelog
207 * @return whether closing the package changelog stream was successful
209 static int _cache_changelog_close(const alpm_pkg_t UNUSED
*pkg
, void *fp
)
211 return fclose((FILE *)fp
);
214 static int _cache_force_load(alpm_pkg_t
*pkg
)
216 return local_db_read(pkg
, INFRQ_ALL
);
220 /** The local database operations struct. Get package fields through
221 * lazy accessor methods that handle any backend loading and caching
224 static struct pkg_operations local_pkg_ops
= {
225 .get_desc
= _cache_get_desc
,
226 .get_url
= _cache_get_url
,
227 .get_builddate
= _cache_get_builddate
,
228 .get_installdate
= _cache_get_installdate
,
229 .get_packager
= _cache_get_packager
,
230 .get_arch
= _cache_get_arch
,
231 .get_isize
= _cache_get_isize
,
232 .get_reason
= _cache_get_reason
,
233 .get_validation
= _cache_get_validation
,
234 .has_scriptlet
= _cache_has_scriptlet
,
235 .get_licenses
= _cache_get_licenses
,
236 .get_groups
= _cache_get_groups
,
237 .get_depends
= _cache_get_depends
,
238 .get_optdepends
= _cache_get_optdepends
,
239 .get_conflicts
= _cache_get_conflicts
,
240 .get_provides
= _cache_get_provides
,
241 .get_replaces
= _cache_get_replaces
,
242 .get_files
= _cache_get_files
,
243 .get_backup
= _cache_get_backup
,
245 .changelog_open
= _cache_changelog_open
,
246 .changelog_read
= _cache_changelog_read
,
247 .changelog_close
= _cache_changelog_close
,
249 .force_load
= _cache_force_load
,
252 static int checkdbdir(alpm_db_t
*db
)
255 const char *path
= _alpm_db_path(db
);
257 if(stat(path
, &buf
) != 0) {
258 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "database dir '%s' does not exist, creating it\n",
260 if(_alpm_makepath(path
) != 0) {
261 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
263 } else if(!S_ISDIR(buf
.st_mode
)) {
264 _alpm_log(db
->handle
, ALPM_LOG_WARNING
, _("removing invalid database: %s\n"), path
);
265 if(unlink(path
) != 0 || _alpm_makepath(path
) != 0) {
266 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
272 static int is_dir(const char *path
, struct dirent
*entry
)
274 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
275 if(entry
->d_type
!= DT_UNKNOWN
) {
276 return (entry
->d_type
== DT_DIR
);
280 char buffer
[PATH_MAX
];
283 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
285 if(!stat(buffer
, &sbuf
)) {
286 return S_ISDIR(sbuf
.st_mode
);
293 static int local_db_validate(alpm_db_t
*db
)
295 struct dirent
*ent
= NULL
;
300 if(db
->status
& DB_STATUS_VALID
) {
303 if(db
->status
& DB_STATUS_INVALID
) {
307 dbpath
= _alpm_db_path(db
);
309 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
311 dbdir
= opendir(dbpath
);
313 if(errno
== ENOENT
) {
314 /* database dir doesn't exist yet */
315 db
->status
|= DB_STATUS_VALID
;
316 db
->status
&= ~DB_STATUS_INVALID
;
317 db
->status
&= ~DB_STATUS_EXISTS
;
318 db
->status
|= DB_STATUS_MISSING
;
321 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
324 db
->status
|= DB_STATUS_EXISTS
;
325 db
->status
&= ~DB_STATUS_MISSING
;
327 while((ent
= readdir(dbdir
)) != NULL
) {
328 const char *name
= ent
->d_name
;
331 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
334 if(!is_dir(dbpath
, ent
)) {
338 snprintf(path
, PATH_MAX
, "%s%s/depends", dbpath
, name
);
339 if(access(path
, F_OK
) == 0) {
340 /* we found a depends file- bail */
341 db
->status
&= ~DB_STATUS_VALID
;
342 db
->status
|= DB_STATUS_INVALID
;
343 db
->handle
->pm_errno
= ALPM_ERR_DB_VERSION
;
347 /* we found no depends file after full scan */
348 db
->status
|= DB_STATUS_VALID
;
349 db
->status
&= ~DB_STATUS_INVALID
;
360 static int local_db_populate(alpm_db_t
*db
)
365 struct dirent
*ent
= NULL
;
369 if(db
->status
& DB_STATUS_INVALID
) {
370 RET_ERR(db
->handle
, ALPM_ERR_DB_INVALID
, -1);
372 /* note: DB_STATUS_MISSING is not fatal for local database */
374 dbpath
= _alpm_db_path(db
);
376 /* pm_errno set in _alpm_db_path() */
380 dbdir
= opendir(dbpath
);
382 if(errno
== ENOENT
) {
383 /* no database existing yet is not an error */
384 db
->status
&= ~DB_STATUS_EXISTS
;
385 db
->status
|= DB_STATUS_MISSING
;
388 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
390 if(fstat(dirfd(dbdir
), &buf
) != 0) {
391 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
393 db
->status
|= DB_STATUS_EXISTS
;
394 db
->status
&= ~DB_STATUS_MISSING
;
395 if(buf
.st_nlink
>= 2) {
396 est_count
= buf
.st_nlink
;
398 /* Some filesystems don't subscribe to the two-implicit links school of
399 * thought, e.g. BTRFS, HFS+. See
400 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
403 while(readdir(dbdir
) != NULL
) {
409 /* subtract the '.' and '..' pointers to get # of children */
413 db
->pkgcache
= _alpm_pkghash_create(est_count
);
414 if(db
->pkgcache
== NULL
){
416 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
419 while((ent
= readdir(dbdir
)) != NULL
) {
420 const char *name
= ent
->d_name
;
424 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
427 if(!is_dir(dbpath
, ent
)) {
431 pkg
= _alpm_pkg_new();
434 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
436 /* split the db entry name */
437 if(_alpm_splitname(name
, &(pkg
->name
), &(pkg
->version
),
438 &(pkg
->name_hash
)) != 0) {
439 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("invalid name for database entry '%s'\n"),
445 /* duplicated database entries are not allowed */
446 if(_alpm_pkghash_find(db
->pkgcache
, pkg
->name
)) {
447 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("duplicated database entry '%s'\n"), pkg
->name
);
452 pkg
->origin
= ALPM_PKG_FROM_LOCALDB
;
453 pkg
->origin_data
.db
= db
;
454 pkg
->ops
= &local_pkg_ops
;
455 pkg
->handle
= db
->handle
;
457 /* explicitly read with only 'BASE' data, accessors will handle the rest */
458 if(local_db_read(pkg
, INFRQ_BASE
) == -1) {
459 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("corrupted database entry '%s'\n"), name
);
464 /* add to the collection */
465 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
, "adding '%s' to package cache for db '%s'\n",
466 pkg
->name
, db
->treename
);
467 db
->pkgcache
= _alpm_pkghash_add(db
->pkgcache
, pkg
);
473 db
->pkgcache
->list
= alpm_list_msort(db
->pkgcache
->list
, (size_t)count
, _alpm_pkg_cmp
);
475 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "added %d packages to package cache for db '%s'\n",
476 count
, db
->treename
);
481 /* Note: the return value must be freed by the caller */
482 char *_alpm_local_db_pkgpath(alpm_db_t
*db
, alpm_pkg_t
*info
,
483 const char *filename
)
489 dbpath
= _alpm_db_path(db
);
490 len
= strlen(dbpath
) + strlen(info
->name
) + strlen(info
->version
) + 3;
491 len
+= filename
? strlen(filename
) : 0;
492 MALLOC(pkgpath
, len
, RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, NULL
));
493 sprintf(pkgpath
, "%s%s-%s/%s", dbpath
, info
->name
, info
->version
,
494 filename
? filename
: "");
498 #define READ_NEXT() do { \
499 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
500 _alpm_strip_newline(line, 0); \
503 #define READ_AND_STORE(f) do { \
505 STRDUP(f, line, goto error); \
508 #define READ_AND_STORE_ALL(f) do { \
510 if(fgets(line, sizeof(line), fp) == NULL) {\
511 if(!feof(fp)) goto error; else break; \
513 if(_alpm_strip_newline(line, 0) == 0) break; \
514 STRDUP(linedup, line, goto error); \
515 f = alpm_list_add(f, linedup); \
516 } while(1) /* note the while(1) and not (0) */
518 #define READ_AND_SPLITDEP(f) do { \
519 if(fgets(line, sizeof(line), fp) == NULL) {\
520 if(!feof(fp)) goto error; else break; \
522 if(_alpm_strip_newline(line, 0) == 0) break; \
523 f = alpm_list_add(f, _alpm_splitdep(line)); \
524 } while(1) /* note the while(1) and not (0) */
526 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
530 alpm_db_t
*db
= info
->origin_data
.db
;
532 /* bitmask logic here:
533 * infolevel: 00001111
536 * == to inforeq? nope, we need to load more info. */
537 if((info
->infolevel
& inforeq
) == inforeq
) {
538 /* already loaded all of this info, do nothing */
542 if(info
->infolevel
& INFRQ_ERROR
) {
543 /* We've encountered an error loading this package before. Don't attempt
544 * repeated reloads, just give up. */
548 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
,
549 "loading package data for %s : level=0x%x\n",
550 info
->name
, inforeq
);
552 /* clear out 'line', to be certain - and to make valgrind happy */
553 memset(line
, 0, sizeof(line
));
556 if(inforeq
& INFRQ_DESC
&& !(info
->infolevel
& INFRQ_DESC
)) {
557 char *path
= _alpm_local_db_pkgpath(db
, info
, "desc");
558 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
559 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
565 if(fgets(line
, sizeof(line
), fp
) == NULL
&& !feof(fp
)) {
568 if(_alpm_strip_newline(line
, 0) == 0) {
569 /* length of stripped line was zero */
572 if(strcmp(line
, "%NAME%") == 0) {
574 if(strcmp(line
, info
->name
) != 0) {
575 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: name "
576 "mismatch on package %s\n"), db
->treename
, info
->name
);
578 } else if(strcmp(line
, "%VERSION%") == 0) {
580 if(strcmp(line
, info
->version
) != 0) {
581 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: version "
582 "mismatch on package %s\n"), db
->treename
, info
->name
);
584 } else if(strcmp(line
, "%DESC%") == 0) {
585 READ_AND_STORE(info
->desc
);
586 } else if(strcmp(line
, "%GROUPS%") == 0) {
587 READ_AND_STORE_ALL(info
->groups
);
588 } else if(strcmp(line
, "%URL%") == 0) {
589 READ_AND_STORE(info
->url
);
590 } else if(strcmp(line
, "%LICENSE%") == 0) {
591 READ_AND_STORE_ALL(info
->licenses
);
592 } else if(strcmp(line
, "%ARCH%") == 0) {
593 READ_AND_STORE(info
->arch
);
594 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
596 info
->builddate
= _alpm_parsedate(line
);
597 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
599 info
->installdate
= _alpm_parsedate(line
);
600 } else if(strcmp(line
, "%PACKAGER%") == 0) {
601 READ_AND_STORE(info
->packager
);
602 } else if(strcmp(line
, "%REASON%") == 0) {
604 info
->reason
= (alpm_pkgreason_t
)atoi(line
);
605 } else if(strcmp(line
, "%VALIDATION%") == 0) {
606 alpm_list_t
*i
, *v
= NULL
;
607 READ_AND_STORE_ALL(v
);
608 for(i
= v
; i
; i
= alpm_list_next(i
))
610 if(strcmp(i
->data
, "none") == 0) {
611 info
->validation
|= ALPM_PKG_VALIDATION_NONE
;
612 } else if(strcmp(i
->data
, "md5") == 0) {
613 info
->validation
|= ALPM_PKG_VALIDATION_MD5SUM
;
614 } else if(strcmp(i
->data
, "sha256") == 0) {
615 info
->validation
|= ALPM_PKG_VALIDATION_SHA256SUM
;
616 } else if(strcmp(i
->data
, "pgp") == 0) {
617 info
->validation
|= ALPM_PKG_VALIDATION_SIGNATURE
;
619 _alpm_log(db
->handle
, ALPM_LOG_WARNING
,
620 _("unknown validation type for package %s: %s\n"),
621 info
->name
, (const char *)i
->data
);
625 } else if(strcmp(line
, "%SIZE%") == 0) {
627 info
->isize
= _alpm_strtoofft(line
);
628 } else if(strcmp(line
, "%REPLACES%") == 0) {
629 READ_AND_SPLITDEP(info
->replaces
);
630 } else if(strcmp(line
, "%DEPENDS%") == 0) {
631 READ_AND_SPLITDEP(info
->depends
);
632 } else if(strcmp(line
, "%OPTDEPENDS%") == 0) {
633 READ_AND_SPLITDEP(info
->optdepends
);
634 } else if(strcmp(line
, "%CONFLICTS%") == 0) {
635 READ_AND_SPLITDEP(info
->conflicts
);
636 } else if(strcmp(line
, "%PROVIDES%") == 0) {
637 READ_AND_SPLITDEP(info
->provides
);
642 info
->infolevel
|= INFRQ_DESC
;
646 if(inforeq
& INFRQ_FILES
&& !(info
->infolevel
& INFRQ_FILES
)) {
647 char *path
= _alpm_local_db_pkgpath(db
, info
, "files");
648 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
649 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
654 while(fgets(line
, sizeof(line
), fp
)) {
655 _alpm_strip_newline(line
, 0);
656 if(strcmp(line
, "%FILES%") == 0) {
657 size_t files_count
= 0, files_size
= 0, len
;
658 alpm_file_t
*files
= NULL
;
660 while(fgets(line
, sizeof(line
), fp
) &&
661 (len
= _alpm_strip_newline(line
, 0))) {
662 if(files_count
>= files_size
) {
663 size_t old_size
= files_size
;
664 if(files_size
== 0) {
669 files
= realloc(files
, sizeof(alpm_file_t
) * files_size
);
671 _alpm_alloc_fail(sizeof(alpm_file_t
) * files_size
);
674 /* ensure all new memory is zeroed out, in both the initial
675 * allocation and later reallocs */
676 memset(files
+ old_size
, 0,
677 sizeof(alpm_file_t
) * (files_size
- old_size
));
679 /* since we know the length of the file string already,
680 * we can do malloc + memcpy rather than strdup */
682 files
[files_count
].name
= malloc(len
);
683 if(files
[files_count
].name
== NULL
) {
684 _alpm_alloc_fail(len
);
687 memcpy(files
[files_count
].name
, line
, len
);
690 /* attempt to hand back any memory we don't need */
691 files
= realloc(files
, sizeof(alpm_file_t
) * files_count
);
692 /* make sure the list is sorted */
693 qsort(files
, files_count
, sizeof(alpm_file_t
), _alpm_files_cmp
);
694 info
->files
.count
= files_count
;
695 info
->files
.files
= files
;
696 } else if(strcmp(line
, "%BACKUP%") == 0) {
697 while(fgets(line
, sizeof(line
), fp
) && _alpm_strip_newline(line
, 0)) {
698 alpm_backup_t
*backup
;
699 CALLOC(backup
, 1, sizeof(alpm_backup_t
), goto error
);
700 if(_alpm_split_backup(line
, &backup
)) {
703 info
->backup
= alpm_list_add(info
->backup
, backup
);
709 info
->infolevel
|= INFRQ_FILES
;
713 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
714 char *path
= _alpm_local_db_pkgpath(db
, info
, "install");
715 if(access(path
, F_OK
) == 0) {
719 info
->infolevel
|= INFRQ_SCRIPTLET
;
725 info
->infolevel
|= INFRQ_ERROR
;
732 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
738 if(checkdbdir(db
) != 0) {
742 oldmask
= umask(0000);
743 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
745 if((retval
= mkdir(pkgpath
, 0755)) != 0) {
746 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not create directory %s: %s\n"),
747 pkgpath
, strerror(errno
));
756 static void write_deps(FILE *fp
, const char *header
, alpm_list_t
*deplist
)
764 for(lp
= deplist
; lp
; lp
= lp
->next
) {
765 char *depstring
= alpm_dep_compute_string(lp
->data
);
766 fputs(depstring
, fp
);
773 int _alpm_local_db_write(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
780 if(db
== NULL
|| info
== NULL
|| !(db
->status
& DB_STATUS_LOCAL
)) {
784 /* make sure we have a sane umask */
785 oldmask
= umask(0022);
788 if(inforeq
& INFRQ_DESC
) {
790 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
791 "writing %s-%s DESC information back to db\n",
792 info
->name
, info
->version
);
793 path
= _alpm_local_db_pkgpath(db
, info
, "desc");
794 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
795 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
796 path
, strerror(errno
));
802 fprintf(fp
, "%%NAME%%\n%s\n\n"
803 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
805 fprintf(fp
, "%%DESC%%\n"
806 "%s\n\n", info
->desc
);
809 fprintf(fp
, "%%URL%%\n"
810 "%s\n\n", info
->url
);
813 fprintf(fp
, "%%ARCH%%\n"
814 "%s\n\n", info
->arch
);
816 if(info
->builddate
) {
817 fprintf(fp
, "%%BUILDDATE%%\n"
818 "%jd\n\n", (intmax_t)info
->builddate
);
820 if(info
->installdate
) {
821 fprintf(fp
, "%%INSTALLDATE%%\n"
822 "%jd\n\n", (intmax_t)info
->installdate
);
825 fprintf(fp
, "%%PACKAGER%%\n"
826 "%s\n\n", info
->packager
);
829 /* only write installed size, csize is irrelevant once installed */
830 fprintf(fp
, "%%SIZE%%\n"
831 "%jd\n\n", (intmax_t)info
->isize
);
834 fprintf(fp
, "%%REASON%%\n"
835 "%u\n\n", info
->reason
);
838 fputs("%GROUPS%\n", fp
);
839 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
846 fputs("%LICENSE%\n", fp
);
847 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
853 if(info
->validation
) {
854 fputs("%VALIDATION%\n", fp
);
855 if(info
->validation
& ALPM_PKG_VALIDATION_NONE
) {
858 if(info
->validation
& ALPM_PKG_VALIDATION_MD5SUM
) {
861 if(info
->validation
& ALPM_PKG_VALIDATION_SHA256SUM
) {
862 fputs("sha256\n", fp
);
864 if(info
->validation
& ALPM_PKG_VALIDATION_SIGNATURE
) {
870 write_deps(fp
, "%REPLACES%", info
->replaces
);
871 write_deps(fp
, "%DEPENDS%", info
->depends
);
872 write_deps(fp
, "%OPTDEPENDS%", info
->optdepends
);
873 write_deps(fp
, "%CONFLICTS%", info
->conflicts
);
874 write_deps(fp
, "%PROVIDES%", info
->provides
);
881 if(inforeq
& INFRQ_FILES
) {
883 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
884 "writing %s-%s FILES information back to db\n",
885 info
->name
, info
->version
);
886 path
= _alpm_local_db_pkgpath(db
, info
, "files");
887 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
888 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
889 path
, strerror(errno
));
895 if(info
->files
.count
) {
897 fputs("%FILES%\n", fp
);
898 for(i
= 0; i
< info
->files
.count
; i
++) {
899 const alpm_file_t
*file
= info
->files
.files
+ i
;
900 fputs(file
->name
, fp
);
906 fputs("%BACKUP%\n", fp
);
907 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
908 const alpm_backup_t
*backup
= lp
->data
;
909 fprintf(fp
, "%s\t%s\n", backup
->name
, backup
->hash
);
918 /* nothing needed here (script is automatically extracted) */
930 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
938 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
942 pkgpath_len
= strlen(pkgpath
);
944 dirp
= opendir(pkgpath
);
948 /* go through the local DB entry, removing the files within, which we know
949 * are not nested directories of any kind. */
950 for(dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)) {
951 if(strcmp(dp
->d_name
, "..") != 0 && strcmp(dp
->d_name
, ".") != 0) {
953 if(pkgpath_len
+ strlen(dp
->d_name
) + 2 > PATH_MAX
) {
954 /* file path is too long to remove, hmm. */
957 sprintf(name
, "%s/%s", pkgpath
, dp
->d_name
);
966 /* after removing all enclosed files, we can remove the directory itself. */
974 int SYMEXPORT
alpm_pkg_set_reason(alpm_pkg_t
*pkg
, alpm_pkgreason_t reason
)
976 ASSERT(pkg
!= NULL
, return -1);
977 ASSERT(pkg
->origin
== ALPM_PKG_FROM_LOCALDB
,
978 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
979 ASSERT(pkg
->origin_data
.db
== pkg
->handle
->db_local
,
980 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
982 _alpm_log(pkg
->handle
, ALPM_LOG_DEBUG
,
983 "setting install reason %u for %s\n", reason
, pkg
->name
);
984 if(alpm_pkg_get_reason(pkg
) == reason
) {
988 /* set reason (in pkgcache) */
989 pkg
->reason
= reason
;
991 if(_alpm_local_db_write(pkg
->handle
->db_local
, pkg
, INFRQ_DESC
)) {
992 RET_ERR(pkg
->handle
, ALPM_ERR_DB_WRITE
, -1);
998 struct db_operations local_db_ops
= {
999 .validate
= local_db_validate
,
1000 .populate
= local_db_populate
,
1001 .unregister
= _alpm_db_unregister
,
1004 alpm_db_t
*_alpm_db_register_local(alpm_handle_t
*handle
)
1008 _alpm_log(handle
, ALPM_LOG_DEBUG
, "registering local database\n");
1010 db
= _alpm_db_new("local", 1);
1012 handle
->pm_errno
= ALPM_ERR_DB_CREATE
;
1015 db
->ops
= &local_db_ops
;
1016 db
->handle
= handle
;
1018 if(local_db_validate(db
)) {
1019 /* pm_errno set in local_db_validate() */
1024 handle
->db_local
= db
;
1028 /* vim: set ts=2 sw=2 noet: */