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"
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_pkgvalidation_t
_cache_get_validation(alpm_pkg_t
*pkg
)
107 LAZY_LOAD(INFRQ_DESC
, -1);
108 return pkg
->validation
;
111 static alpm_list_t
*_cache_get_licenses(alpm_pkg_t
*pkg
)
113 LAZY_LOAD(INFRQ_DESC
, NULL
);
114 return pkg
->licenses
;
117 static alpm_list_t
*_cache_get_groups(alpm_pkg_t
*pkg
)
119 LAZY_LOAD(INFRQ_DESC
, NULL
);
123 static int _cache_has_scriptlet(alpm_pkg_t
*pkg
)
125 LAZY_LOAD(INFRQ_SCRIPTLET
, NULL
);
126 return pkg
->scriptlet
;
129 static alpm_list_t
*_cache_get_depends(alpm_pkg_t
*pkg
)
131 LAZY_LOAD(INFRQ_DESC
, NULL
);
135 static alpm_list_t
*_cache_get_optdepends(alpm_pkg_t
*pkg
)
137 LAZY_LOAD(INFRQ_DESC
, NULL
);
138 return pkg
->optdepends
;
141 static alpm_list_t
*_cache_get_conflicts(alpm_pkg_t
*pkg
)
143 LAZY_LOAD(INFRQ_DESC
, NULL
);
144 return pkg
->conflicts
;
147 static alpm_list_t
*_cache_get_provides(alpm_pkg_t
*pkg
)
149 LAZY_LOAD(INFRQ_DESC
, NULL
);
150 return pkg
->provides
;
153 static alpm_list_t
*_cache_get_replaces(alpm_pkg_t
*pkg
)
155 LAZY_LOAD(INFRQ_DESC
, NULL
);
156 return pkg
->replaces
;
159 static alpm_filelist_t
*_cache_get_files(alpm_pkg_t
*pkg
)
161 LAZY_LOAD(INFRQ_FILES
, NULL
);
162 return &(pkg
->files
);
165 static alpm_list_t
*_cache_get_backup(alpm_pkg_t
*pkg
)
167 LAZY_LOAD(INFRQ_FILES
, NULL
);
172 * Open a package changelog for reading. Similar to fopen in functionality,
173 * except that the returned 'file stream' is from the database.
174 * @param pkg the package (from db) to read the changelog
175 * @return a 'file stream' to the package changelog
177 static void *_cache_changelog_open(alpm_pkg_t
*pkg
)
179 alpm_db_t
*db
= alpm_pkg_get_db(pkg
);
180 char *clfile
= _alpm_local_db_pkgpath(db
, pkg
, "changelog");
181 FILE *f
= fopen(clfile
, "r");
187 * Read data from an open changelog 'file stream'. Similar to fread in
188 * functionality, this function takes a buffer and amount of data to read.
189 * @param ptr a buffer to fill with raw changelog data
190 * @param size the size of the buffer
191 * @param pkg the package that the changelog is being read from
192 * @param fp a 'file stream' to the package changelog
193 * @return the number of characters read, or 0 if there is no more data
195 static size_t _cache_changelog_read(void *ptr
, size_t size
,
196 const alpm_pkg_t UNUSED
*pkg
, void *fp
)
198 return fread(ptr
, 1, size
, (FILE *)fp
);
202 * Close a package changelog for reading. Similar to fclose in functionality,
203 * except that the 'file stream' is from the database.
204 * @param pkg the package that the changelog was read from
205 * @param fp a 'file stream' to the package changelog
206 * @return whether closing the package changelog stream was successful
208 static int _cache_changelog_close(const alpm_pkg_t UNUSED
*pkg
, void *fp
)
210 return fclose((FILE *)fp
);
213 static int _cache_force_load(alpm_pkg_t
*pkg
)
215 return local_db_read(pkg
, INFRQ_ALL
);
219 /** The local database operations struct. Get package fields through
220 * lazy accessor methods that handle any backend loading and caching
223 static struct pkg_operations local_pkg_ops
= {
224 .get_desc
= _cache_get_desc
,
225 .get_url
= _cache_get_url
,
226 .get_builddate
= _cache_get_builddate
,
227 .get_installdate
= _cache_get_installdate
,
228 .get_packager
= _cache_get_packager
,
229 .get_arch
= _cache_get_arch
,
230 .get_isize
= _cache_get_isize
,
231 .get_reason
= _cache_get_reason
,
232 .get_validation
= _cache_get_validation
,
233 .has_scriptlet
= _cache_has_scriptlet
,
234 .get_licenses
= _cache_get_licenses
,
235 .get_groups
= _cache_get_groups
,
236 .get_depends
= _cache_get_depends
,
237 .get_optdepends
= _cache_get_optdepends
,
238 .get_conflicts
= _cache_get_conflicts
,
239 .get_provides
= _cache_get_provides
,
240 .get_replaces
= _cache_get_replaces
,
241 .get_files
= _cache_get_files
,
242 .get_backup
= _cache_get_backup
,
244 .changelog_open
= _cache_changelog_open
,
245 .changelog_read
= _cache_changelog_read
,
246 .changelog_close
= _cache_changelog_close
,
248 .force_load
= _cache_force_load
,
251 static int checkdbdir(alpm_db_t
*db
)
254 const char *path
= _alpm_db_path(db
);
256 if(stat(path
, &buf
) != 0) {
257 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "database dir '%s' does not exist, creating it\n",
259 if(_alpm_makepath(path
) != 0) {
260 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
262 } else if(!S_ISDIR(buf
.st_mode
)) {
263 _alpm_log(db
->handle
, ALPM_LOG_WARNING
, _("removing invalid database: %s\n"), path
);
264 if(unlink(path
) != 0 || _alpm_makepath(path
) != 0) {
265 RET_ERR(db
->handle
, ALPM_ERR_SYSTEM
, -1);
271 static int is_dir(const char *path
, struct dirent
*entry
)
273 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
274 if(entry
->d_type
!= DT_UNKNOWN
) {
275 return (entry
->d_type
== DT_DIR
);
279 char buffer
[PATH_MAX
];
282 snprintf(buffer
, PATH_MAX
, "%s/%s", path
, entry
->d_name
);
284 if(!stat(buffer
, &sbuf
)) {
285 return S_ISDIR(sbuf
.st_mode
);
292 static int local_db_validate(alpm_db_t
*db
)
294 struct dirent
*ent
= NULL
;
299 if(db
->status
& DB_STATUS_VALID
) {
302 if(db
->status
& DB_STATUS_INVALID
) {
306 dbpath
= _alpm_db_path(db
);
308 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
310 dbdir
= opendir(dbpath
);
312 if(errno
== ENOENT
) {
313 /* database dir doesn't exist yet */
314 db
->status
|= DB_STATUS_VALID
;
315 db
->status
&= ~DB_STATUS_INVALID
;
316 db
->status
&= ~DB_STATUS_EXISTS
;
317 db
->status
|= DB_STATUS_MISSING
;
320 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
323 db
->status
|= DB_STATUS_EXISTS
;
324 db
->status
&= ~DB_STATUS_MISSING
;
326 while((ent
= readdir(dbdir
)) != NULL
) {
327 const char *name
= ent
->d_name
;
330 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
333 if(!is_dir(dbpath
, ent
)) {
337 snprintf(path
, PATH_MAX
, "%s%s/depends", dbpath
, name
);
338 if(access(path
, F_OK
) == 0) {
339 /* we found a depends file- bail */
340 db
->status
&= ~DB_STATUS_VALID
;
341 db
->status
|= DB_STATUS_INVALID
;
342 db
->handle
->pm_errno
= ALPM_ERR_DB_VERSION
;
346 /* we found no depends file after full scan */
347 db
->status
|= DB_STATUS_VALID
;
348 db
->status
&= ~DB_STATUS_INVALID
;
359 static int local_db_populate(alpm_db_t
*db
)
364 struct dirent
*ent
= NULL
;
368 if(db
->status
& DB_STATUS_INVALID
) {
369 RET_ERR(db
->handle
, ALPM_ERR_DB_INVALID
, -1);
371 /* note: DB_STATUS_MISSING is not fatal for local database */
373 dbpath
= _alpm_db_path(db
);
375 /* pm_errno set in _alpm_db_path() */
379 dbdir
= opendir(dbpath
);
381 if(errno
== ENOENT
) {
382 /* no database existing yet is not an error */
383 db
->status
&= ~DB_STATUS_EXISTS
;
384 db
->status
|= DB_STATUS_MISSING
;
387 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
389 if(fstat(dirfd(dbdir
), &buf
) != 0) {
390 RET_ERR(db
->handle
, ALPM_ERR_DB_OPEN
, -1);
392 db
->status
|= DB_STATUS_EXISTS
;
393 db
->status
&= ~DB_STATUS_MISSING
;
394 if(buf
.st_nlink
>= 2) {
395 est_count
= buf
.st_nlink
;
397 /* Some filesystems don't subscribe to the two-implicit links school of
398 * thought, e.g. BTRFS, HFS+. See
399 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
402 while(readdir(dbdir
) != NULL
) {
408 /* subtract the '.' and '..' pointers to get # of children */
412 db
->pkgcache
= _alpm_pkghash_create(est_count
);
413 if(db
->pkgcache
== NULL
){
415 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
418 while((ent
= readdir(dbdir
)) != NULL
) {
419 const char *name
= ent
->d_name
;
423 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
426 if(!is_dir(dbpath
, ent
)) {
430 pkg
= _alpm_pkg_new();
433 RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, -1);
435 /* split the db entry name */
436 if(_alpm_splitname(name
, &(pkg
->name
), &(pkg
->version
),
437 &(pkg
->name_hash
)) != 0) {
438 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("invalid name for database entry '%s'\n"),
444 /* duplicated database entries are not allowed */
445 if(_alpm_pkghash_find(db
->pkgcache
, pkg
->name
)) {
446 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("duplicated database entry '%s'\n"), pkg
->name
);
451 pkg
->origin
= ALPM_PKG_FROM_LOCALDB
;
452 pkg
->origin_data
.db
= db
;
453 pkg
->ops
= &local_pkg_ops
;
454 pkg
->handle
= db
->handle
;
456 /* explicitly read with only 'BASE' data, accessors will handle the rest */
457 if(local_db_read(pkg
, INFRQ_BASE
) == -1) {
458 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("corrupted database entry '%s'\n"), name
);
463 /* add to the collection */
464 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
, "adding '%s' to package cache for db '%s'\n",
465 pkg
->name
, db
->treename
);
466 db
->pkgcache
= _alpm_pkghash_add(db
->pkgcache
, pkg
);
472 db
->pkgcache
->list
= alpm_list_msort(db
->pkgcache
->list
, (size_t)count
, _alpm_pkg_cmp
);
474 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "added %d packages to package cache for db '%s'\n",
475 count
, db
->treename
);
480 /* Note: the return value must be freed by the caller */
481 char *_alpm_local_db_pkgpath(alpm_db_t
*db
, alpm_pkg_t
*info
,
482 const char *filename
)
488 dbpath
= _alpm_db_path(db
);
489 len
= strlen(dbpath
) + strlen(info
->name
) + strlen(info
->version
) + 3;
490 len
+= filename
? strlen(filename
) : 0;
491 MALLOC(pkgpath
, len
, RET_ERR(db
->handle
, ALPM_ERR_MEMORY
, NULL
));
492 sprintf(pkgpath
, "%s%s-%s/%s", dbpath
, info
->name
, info
->version
,
493 filename
? filename
: "");
497 #define READ_NEXT() do { \
498 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
499 _alpm_strip_newline(line, 0); \
502 #define READ_AND_STORE(f) do { \
504 STRDUP(f, line, goto error); \
507 #define READ_AND_STORE_ALL(f) do { \
509 if(fgets(line, sizeof(line), fp) == NULL) {\
510 if(!feof(fp)) goto error; else break; \
512 if(_alpm_strip_newline(line, 0) == 0) break; \
513 STRDUP(linedup, line, goto error); \
514 f = alpm_list_add(f, linedup); \
515 } while(1) /* note the while(1) and not (0) */
517 #define READ_AND_SPLITDEP(f) do { \
518 if(fgets(line, sizeof(line), fp) == NULL) {\
519 if(!feof(fp)) goto error; else break; \
521 if(_alpm_strip_newline(line, 0) == 0) break; \
522 f = alpm_list_add(f, _alpm_splitdep(line)); \
523 } while(1) /* note the while(1) and not (0) */
525 static int local_db_read(alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
529 alpm_db_t
*db
= info
->origin_data
.db
;
531 /* bitmask logic here:
532 * infolevel: 00001111
535 * == to inforeq? nope, we need to load more info. */
536 if((info
->infolevel
& inforeq
) == inforeq
) {
537 /* already loaded all of this info, do nothing */
541 if(info
->infolevel
& INFRQ_ERROR
) {
542 /* We've encountered an error loading this package before. Don't attempt
543 * repeated reloads, just give up. */
547 _alpm_log(db
->handle
, ALPM_LOG_FUNCTION
,
548 "loading package data for %s : level=0x%x\n",
549 info
->name
, inforeq
);
551 /* clear out 'line', to be certain - and to make valgrind happy */
552 memset(line
, 0, sizeof(line
));
555 if(inforeq
& INFRQ_DESC
&& !(info
->infolevel
& INFRQ_DESC
)) {
556 char *path
= _alpm_local_db_pkgpath(db
, info
, "desc");
557 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
558 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
564 if(fgets(line
, sizeof(line
), fp
) == NULL
&& !feof(fp
)) {
567 if(_alpm_strip_newline(line
, 0) == 0) {
568 /* length of stripped line was zero */
571 if(strcmp(line
, "%NAME%") == 0) {
573 if(strcmp(line
, info
->name
) != 0) {
574 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: name "
575 "mismatch on package %s\n"), db
->treename
, info
->name
);
577 } else if(strcmp(line
, "%VERSION%") == 0) {
579 if(strcmp(line
, info
->version
) != 0) {
580 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("%s database is inconsistent: version "
581 "mismatch on package %s\n"), db
->treename
, info
->name
);
583 } else if(strcmp(line
, "%DESC%") == 0) {
584 READ_AND_STORE(info
->desc
);
585 } else if(strcmp(line
, "%GROUPS%") == 0) {
586 READ_AND_STORE_ALL(info
->groups
);
587 } else if(strcmp(line
, "%URL%") == 0) {
588 READ_AND_STORE(info
->url
);
589 } else if(strcmp(line
, "%LICENSE%") == 0) {
590 READ_AND_STORE_ALL(info
->licenses
);
591 } else if(strcmp(line
, "%ARCH%") == 0) {
592 READ_AND_STORE(info
->arch
);
593 } else if(strcmp(line
, "%BUILDDATE%") == 0) {
595 info
->builddate
= _alpm_parsedate(line
);
596 } else if(strcmp(line
, "%INSTALLDATE%") == 0) {
598 info
->installdate
= _alpm_parsedate(line
);
599 } else if(strcmp(line
, "%PACKAGER%") == 0) {
600 READ_AND_STORE(info
->packager
);
601 } else if(strcmp(line
, "%REASON%") == 0) {
603 info
->reason
= (alpm_pkgreason_t
)atoi(line
);
604 } else if(strcmp(line
, "%VALIDATION%") == 0) {
605 alpm_list_t
*i
, *v
= NULL
;
606 READ_AND_STORE_ALL(v
);
607 for(i
= v
; i
; i
= alpm_list_next(i
))
609 if(strcmp(i
->data
, "none") == 0) {
610 info
->validation
|= ALPM_PKG_VALIDATION_NONE
;
611 } else if(strcmp(i
->data
, "md5") == 0) {
612 info
->validation
|= ALPM_PKG_VALIDATION_MD5SUM
;
613 } else if(strcmp(i
->data
, "sha256") == 0) {
614 info
->validation
|= ALPM_PKG_VALIDATION_SHA256SUM
;
615 } else if(strcmp(i
->data
, "pgp") == 0) {
616 info
->validation
|= ALPM_PKG_VALIDATION_SIGNATURE
;
618 _alpm_log(db
->handle
, ALPM_LOG_WARNING
,
619 _("unknown validation type for package %s: %s\n"),
620 info
->name
, (const char *)i
->data
);
624 } else if(strcmp(line
, "%SIZE%") == 0) {
626 info
->isize
= _alpm_strtoofft(line
);
627 } else if(strcmp(line
, "%REPLACES%") == 0) {
628 READ_AND_SPLITDEP(info
->replaces
);
629 } else if(strcmp(line
, "%DEPENDS%") == 0) {
630 READ_AND_SPLITDEP(info
->depends
);
631 } else if(strcmp(line
, "%OPTDEPENDS%") == 0) {
632 READ_AND_SPLITDEP(info
->optdepends
);
633 } else if(strcmp(line
, "%CONFLICTS%") == 0) {
634 READ_AND_SPLITDEP(info
->conflicts
);
635 } else if(strcmp(line
, "%PROVIDES%") == 0) {
636 READ_AND_SPLITDEP(info
->provides
);
641 info
->infolevel
|= INFRQ_DESC
;
645 if(inforeq
& INFRQ_FILES
&& !(info
->infolevel
& INFRQ_FILES
)) {
646 char *path
= _alpm_local_db_pkgpath(db
, info
, "files");
647 if(!path
|| (fp
= fopen(path
, "r")) == NULL
) {
648 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"), path
, strerror(errno
));
653 while(fgets(line
, sizeof(line
), fp
)) {
654 _alpm_strip_newline(line
, 0);
655 if(strcmp(line
, "%FILES%") == 0) {
656 size_t files_count
= 0, files_size
= 0, len
;
657 alpm_file_t
*files
= NULL
;
659 while(fgets(line
, sizeof(line
), fp
) &&
660 (len
= _alpm_strip_newline(line
, 0))) {
661 if(files_count
>= files_size
) {
662 size_t old_size
= files_size
;
663 if(files_size
== 0) {
668 files
= realloc(files
, sizeof(alpm_file_t
) * files_size
);
670 _alpm_alloc_fail(sizeof(alpm_file_t
) * files_size
);
673 /* ensure all new memory is zeroed out, in both the initial
674 * allocation and later reallocs */
675 memset(files
+ old_size
, 0,
676 sizeof(alpm_file_t
) * (files_size
- old_size
));
678 /* since we know the length of the file string already,
679 * we can do malloc + memcpy rather than strdup */
681 files
[files_count
].name
= malloc(len
);
682 if(files
[files_count
].name
== NULL
) {
683 _alpm_alloc_fail(len
);
686 memcpy(files
[files_count
].name
, line
, len
);
689 /* attempt to hand back any memory we don't need */
690 files
= realloc(files
, sizeof(alpm_file_t
) * files_count
);
691 info
->files
.count
= files_count
;
692 info
->files
.files
= files
;
693 } else if(strcmp(line
, "%BACKUP%") == 0) {
694 while(fgets(line
, sizeof(line
), fp
) && _alpm_strip_newline(line
, 0)) {
695 alpm_backup_t
*backup
;
696 CALLOC(backup
, 1, sizeof(alpm_backup_t
), goto error
);
697 if(_alpm_split_backup(line
, &backup
)) {
700 info
->backup
= alpm_list_add(info
->backup
, backup
);
706 info
->infolevel
|= INFRQ_FILES
;
710 if(inforeq
& INFRQ_SCRIPTLET
&& !(info
->infolevel
& INFRQ_SCRIPTLET
)) {
711 char *path
= _alpm_local_db_pkgpath(db
, info
, "install");
712 if(access(path
, F_OK
) == 0) {
716 info
->infolevel
|= INFRQ_SCRIPTLET
;
722 info
->infolevel
|= INFRQ_ERROR
;
729 int _alpm_local_db_prepare(alpm_db_t
*db
, alpm_pkg_t
*info
)
735 if(checkdbdir(db
) != 0) {
739 oldmask
= umask(0000);
740 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
742 if((retval
= mkdir(pkgpath
, 0755)) != 0) {
743 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not create directory %s: %s\n"),
744 pkgpath
, strerror(errno
));
753 static void write_deps(FILE *fp
, const char *header
, alpm_list_t
*deplist
)
761 for(lp
= deplist
; lp
; lp
= lp
->next
) {
762 char *depstring
= alpm_dep_compute_string(lp
->data
);
763 fputs(depstring
, fp
);
770 int _alpm_local_db_write(alpm_db_t
*db
, alpm_pkg_t
*info
, alpm_dbinfrq_t inforeq
)
777 if(db
== NULL
|| info
== NULL
|| !(db
->status
& DB_STATUS_LOCAL
)) {
781 /* make sure we have a sane umask */
782 oldmask
= umask(0022);
785 if(inforeq
& INFRQ_DESC
) {
787 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
788 "writing %s-%s DESC information back to db\n",
789 info
->name
, info
->version
);
790 path
= _alpm_local_db_pkgpath(db
, info
, "desc");
791 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
792 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
793 path
, strerror(errno
));
799 fprintf(fp
, "%%NAME%%\n%s\n\n"
800 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
802 fprintf(fp
, "%%DESC%%\n"
803 "%s\n\n", info
->desc
);
806 fprintf(fp
, "%%URL%%\n"
807 "%s\n\n", info
->url
);
810 fprintf(fp
, "%%ARCH%%\n"
811 "%s\n\n", info
->arch
);
813 if(info
->builddate
) {
814 fprintf(fp
, "%%BUILDDATE%%\n"
815 "%jd\n\n", (intmax_t)info
->builddate
);
817 if(info
->installdate
) {
818 fprintf(fp
, "%%INSTALLDATE%%\n"
819 "%jd\n\n", (intmax_t)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("%GROUPS%\n", fp
);
836 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
843 fputs("%LICENSE%\n", fp
);
844 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
850 if(info
->validation
) {
851 fputs("%VALIDATION%\n", fp
);
852 if(info
->validation
& ALPM_PKG_VALIDATION_NONE
) {
855 if(info
->validation
& ALPM_PKG_VALIDATION_MD5SUM
) {
858 if(info
->validation
& ALPM_PKG_VALIDATION_SHA256SUM
) {
859 fputs("sha256\n", fp
);
861 if(info
->validation
& ALPM_PKG_VALIDATION_SIGNATURE
) {
867 write_deps(fp
, "%REPLACES%", info
->replaces
);
868 write_deps(fp
, "%DEPENDS%", info
->depends
);
869 write_deps(fp
, "%OPTDEPENDS%", info
->optdepends
);
870 write_deps(fp
, "%CONFLICTS%", info
->conflicts
);
871 write_deps(fp
, "%PROVIDES%", info
->provides
);
878 if(inforeq
& INFRQ_FILES
) {
880 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
,
881 "writing %s-%s FILES information back to db\n",
882 info
->name
, info
->version
);
883 path
= _alpm_local_db_pkgpath(db
, info
, "files");
884 if(!path
|| (fp
= fopen(path
, "w")) == NULL
) {
885 _alpm_log(db
->handle
, ALPM_LOG_ERROR
, _("could not open file %s: %s\n"),
886 path
, strerror(errno
));
892 if(info
->files
.count
) {
894 fputs("%FILES%\n", fp
);
895 for(i
= 0; i
< info
->files
.count
; i
++) {
896 const alpm_file_t
*file
= info
->files
.files
+ i
;
897 fputs(file
->name
, fp
);
903 fputs("%BACKUP%\n", fp
);
904 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
905 const alpm_backup_t
*backup
= lp
->data
;
906 fprintf(fp
, "%s\t%s\n", backup
->name
, backup
->hash
);
915 /* nothing needed here (script is automatically extracted) */
927 int _alpm_local_db_remove(alpm_db_t
*db
, alpm_pkg_t
*info
)
935 pkgpath
= _alpm_local_db_pkgpath(db
, info
, NULL
);
939 pkgpath_len
= strlen(pkgpath
);
941 dirp
= opendir(pkgpath
);
945 /* go through the local DB entry, removing the files within, which we know
946 * are not nested directories of any kind. */
947 for(dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)) {
948 if(strcmp(dp
->d_name
, "..") != 0 && strcmp(dp
->d_name
, ".") != 0) {
950 if(pkgpath_len
+ strlen(dp
->d_name
) + 2 > PATH_MAX
) {
951 /* file path is too long to remove, hmm. */
954 sprintf(name
, "%s/%s", pkgpath
, dp
->d_name
);
963 /* after removing all enclosed files, we can remove the directory itself. */
971 int SYMEXPORT
alpm_pkg_set_reason(alpm_pkg_t
*pkg
, alpm_pkgreason_t reason
)
973 ASSERT(pkg
!= NULL
, return -1);
974 ASSERT(pkg
->origin
== ALPM_PKG_FROM_LOCALDB
,
975 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
976 ASSERT(pkg
->origin_data
.db
== pkg
->handle
->db_local
,
977 RET_ERR(pkg
->handle
, ALPM_ERR_WRONG_ARGS
, -1));
979 _alpm_log(pkg
->handle
, ALPM_LOG_DEBUG
,
980 "setting install reason %u for %s\n", reason
, pkg
->name
);
981 if(alpm_pkg_get_reason(pkg
) == reason
) {
985 /* set reason (in pkgcache) */
986 pkg
->reason
= reason
;
988 if(_alpm_local_db_write(pkg
->handle
->db_local
, pkg
, INFRQ_DESC
)) {
989 RET_ERR(pkg
->handle
, ALPM_ERR_DB_WRITE
, -1);
995 struct db_operations local_db_ops
= {
996 .validate
= local_db_validate
,
997 .populate
= local_db_populate
,
998 .unregister
= _alpm_db_unregister
,
1001 alpm_db_t
*_alpm_db_register_local(alpm_handle_t
*handle
)
1005 _alpm_log(handle
, ALPM_LOG_DEBUG
, "registering local database\n");
1007 db
= _alpm_db_new("local", 1);
1009 handle
->pm_errno
= ALPM_ERR_DB_CREATE
;
1012 db
->ops
= &local_db_ops
;
1013 db
->handle
= handle
;
1015 if(local_db_validate(db
)) {
1016 /* pm_errno set in local_db_validate() */
1021 handle
->db_local
= db
;
1025 /* vim: set ts=2 sw=2 noet: */