summaryrefslogtreecommitdiff
path: root/lib/libalpm/be_package.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/be_package.c')
-rw-r--r--lib/libalpm/be_package.c176
1 files changed, 139 insertions, 37 deletions
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index ff266ae8..c272bd47 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -1,7 +1,7 @@
/*
* be_package.c
*
- * Copyright (c) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>
+ * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
@@ -24,8 +24,8 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
-#include <ctype.h>
#include <locale.h> /* setlocale */
+#include <errno.h>
/* libarchive */
#include <archive.h>
@@ -39,6 +39,109 @@
#include "deps.h" /* _alpm_splitdep */
/**
+ * Open a package changelog for reading. Similar to fopen in functionality,
+ * except that the returned 'file stream' is from an archive.
+ * @param pkg the package (file) to read the changelog
+ * @return a 'file stream' to the package changelog
+ */
+static void *_package_changelog_open(pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ ASSERT(pkg != NULL, return(NULL));
+
+ struct archive *archive = NULL;
+ struct archive_entry *entry;
+ const char *pkgfile = pkg->origin_data.file;
+
+ if((archive = archive_read_new()) == NULL) {
+ RET_ERR(PM_ERR_LIBARCHIVE, NULL);
+ }
+
+ archive_read_support_compression_all(archive);
+ archive_read_support_format_all(archive);
+
+ if (archive_read_open_filename(archive, pkgfile,
+ ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
+ RET_ERR(PM_ERR_PKG_OPEN, NULL);
+ }
+
+ while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
+ const char *entry_name = archive_entry_pathname(entry);
+
+ if(strcmp(entry_name, ".CHANGELOG") == 0) {
+ return(archive);
+ }
+ }
+ /* we didn't find a changelog */
+ archive_read_finish(archive);
+ errno = ENOENT;
+
+ return(NULL);
+}
+
+/**
+ * Read data from an open changelog 'file stream'. Similar to fread in
+ * functionality, this function takes a buffer and amount of data to read.
+ * @param ptr a buffer to fill with raw changelog data
+ * @param size the size of the buffer
+ * @param pkg the package that the changelog is being read from
+ * @param fp a 'file stream' to the package changelog
+ * @return the number of characters read, or 0 if there is no more data
+ */
+static size_t _package_changelog_read(void *ptr, size_t size,
+ const pmpkg_t *pkg, const void *fp)
+{
+ ssize_t sret = archive_read_data((struct archive*)fp, ptr, size);
+ /* Report error (negative values) */
+ if(sret < 0) {
+ pm_errno = PM_ERR_LIBARCHIVE;
+ return(0);
+ } else {
+ return((size_t)sret);
+ }
+}
+
+/*
+static int _package_changelog_feof(const pmpkg_t *pkg, void *fp)
+{
+ // note: this doesn't quite work, no feof in libarchive
+ return( archive_read_data((struct archive*)fp, NULL, 0) );
+}
+*/
+
+/**
+ * Close a package changelog for reading. Similar to fclose in functionality,
+ * except that the 'file stream' is from an archive.
+ * @param pkg the package (file) that the changelog was read from
+ * @param fp a 'file stream' to the package changelog
+ * @return whether closing the package changelog stream was successful
+ */
+static int _package_changelog_close(const pmpkg_t *pkg, void *fp)
+{
+ return( archive_read_finish((struct archive *)fp) );
+}
+
+/** Package file operations struct accessor. We implement this as a method
+ * rather than a static struct as in be_files because we want to reuse the
+ * majority of the default_pkg_ops struct and add only a few operations of
+ * our own on top.
+ */
+static struct pkg_operations *get_file_pkg_ops(void)
+{
+ static struct pkg_operations file_pkg_ops;
+ static int file_pkg_ops_initialized = 0;
+ if(!file_pkg_ops_initialized) {
+ file_pkg_ops = default_pkg_ops;
+ file_pkg_ops.changelog_open = _package_changelog_open;
+ file_pkg_ops.changelog_read = _package_changelog_read;
+ file_pkg_ops.changelog_close = _package_changelog_close;
+ file_pkg_ops_initialized = 1;
+ }
+ return(&file_pkg_ops);
+}
+
+/**
* Parses the package description file for a package into a pmpkg_t struct.
* @param archive the archive to read from, pointed at the .PKGINFO entry
* @param newpkg an empty pmpkg_t struct to fill with package info
@@ -47,17 +150,22 @@
*/
static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
{
- char line[PATH_MAX];
char *ptr = NULL;
char *key = NULL;
int linenum = 0;
+ struct archive_read_buffer buf;
ALPM_LOG_FUNC;
- /* loop until we reach EOF (where archive_fgets will return NULL) */
- while(_alpm_archive_fgets(line, PATH_MAX, a) != NULL) {
+ memset(&buf, 0, sizeof(buf));
+ /* 512K for a line length seems reasonable */
+ buf.max_line_size = 512 * 1024;
+
+ /* loop until we reach EOF or other error */
+ while(_alpm_archive_fgets(a, &buf) == ARCHIVE_OK) {
+ char *line = _alpm_strtrim(buf.line);
+
linenum++;
- _alpm_strtrim(line);
if(strlen(line) == 0 || line[0] == '#') {
continue;
}
@@ -69,52 +177,42 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
} else {
key = _alpm_strtrim(key);
ptr = _alpm_strtrim(ptr);
- if(!strcmp(key, "pkgname")) {
+ if(strcmp(key, "pkgname") == 0) {
STRDUP(newpkg->name, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "pkgver")) {
+ newpkg->name_hash = _alpm_hash_sdbm(newpkg->name);
+ } else if(strcmp(key, "pkgver") == 0) {
STRDUP(newpkg->version, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "pkgdesc")) {
+ } else if(strcmp(key, "pkgdesc") == 0) {
STRDUP(newpkg->desc, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "force")) {
- newpkg->force = 1;
- } else if(!strcmp(key, "group")) {
+ } else if(strcmp(key, "group") == 0) {
newpkg->groups = alpm_list_add(newpkg->groups, strdup(ptr));
- } else if(!strcmp(key, "url")) {
+ } else if(strcmp(key, "url") == 0) {
STRDUP(newpkg->url, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "license")) {
+ } else if(strcmp(key, "license") == 0) {
newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr));
- } else if(!strcmp(key, "builddate")) {
- char first = tolower((unsigned char)ptr[0]);
- if(first > 'a' && first < 'z') {
- struct tm tmp_tm = {0}; /* initialize to null in case of failure */
- setlocale(LC_TIME, "C");
- strptime(ptr, "%a %b %e %H:%M:%S %Y", &tmp_tm);
- newpkg->builddate = mktime(&tmp_tm);
- setlocale(LC_TIME, "");
- } else {
- newpkg->builddate = atol(ptr);
- }
- } else if(!strcmp(key, "packager")) {
+ } else if(strcmp(key, "builddate") == 0) {
+ newpkg->builddate = _alpm_parsedate(ptr);
+ } else if(strcmp(key, "packager") == 0) {
STRDUP(newpkg->packager, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "arch")) {
+ } else if(strcmp(key, "arch") == 0) {
STRDUP(newpkg->arch, ptr, RET_ERR(PM_ERR_MEMORY, -1));
- } else if(!strcmp(key, "size")) {
+ } else if(strcmp(key, "size") == 0) {
/* size in the raw package is uncompressed (installed) size */
newpkg->isize = atol(ptr);
- } else if(!strcmp(key, "depend")) {
+ } else if(strcmp(key, "depend") == 0) {
pmdepend_t *dep = _alpm_splitdep(ptr);
newpkg->depends = alpm_list_add(newpkg->depends, dep);
- } else if(!strcmp(key, "optdepend")) {
+ } else if(strcmp(key, "optdepend") == 0) {
newpkg->optdepends = alpm_list_add(newpkg->optdepends, strdup(ptr));
- } else if(!strcmp(key, "conflict")) {
+ } else if(strcmp(key, "conflict") == 0) {
newpkg->conflicts = alpm_list_add(newpkg->conflicts, strdup(ptr));
- } else if(!strcmp(key, "replaces")) {
+ } else if(strcmp(key, "replaces") == 0) {
newpkg->replaces = alpm_list_add(newpkg->replaces, strdup(ptr));
- } else if(!strcmp(key, "provides")) {
+ } else if(strcmp(key, "provides") == 0) {
newpkg->provides = alpm_list_add(newpkg->provides, strdup(ptr));
- } else if(!strcmp(key, "backup")) {
+ } else if(strcmp(key, "backup") == 0) {
newpkg->backup = alpm_list_add(newpkg->backup, strdup(ptr));
- } else if(!strcmp(key, "makepkgopt")) {
+ } else if(strcmp(key, "makepkgopt") == 0) {
/* not used atm */
} else {
_alpm_log(PM_LOG_DEBUG, "%s: syntax error in description file line %d\n",
@@ -174,6 +272,8 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
newpkg->filename = strdup(pkgfile);
newpkg->size = st.st_size;
+ _alpm_log(PM_LOG_DEBUG, "starting package load for %s\n", pkgfile);
+
/* If full is false, only read through the archive until we find our needed
* metadata. If it is true, read through the entire archive, which serves
* as a verfication of integrity and allows us to create the filelist. */
@@ -232,11 +332,13 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
goto pkg_invalid;
}
- archive_read_finish(archive);
+ archive_read_finish(archive);
/* internal fields for package struct */
newpkg->origin = PKG_FROM_FILE;
+ /* TODO eventually kill/move this? */
newpkg->origin_data.file = strdup(pkgfile);
+ newpkg->ops = get_file_pkg_ops();
if(full) {
/* "checking for conflicts" requires a sorted list, ensure that here */
@@ -247,7 +349,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, int full)
} else {
/* get rid of any partial filelist we may have collected, it is invalid */
FREELIST(newpkg->files);
- newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_DEPENDS;
+ newpkg->infolevel = INFRQ_BASE | INFRQ_DESC;
}
return(newpkg);