diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/.gitignore | 2 | ||||
-rw-r--r-- | src/util/Makefile.am | 22 | ||||
-rw-r--r-- | src/util/cleanupdelta.c | 39 | ||||
-rw-r--r-- | src/util/pacsort.c | 394 | ||||
-rw-r--r-- | src/util/pactree.c | 281 | ||||
-rw-r--r-- | src/util/testdb.c | 94 | ||||
-rw-r--r-- | src/util/testpkg.c | 42 | ||||
-rw-r--r-- | src/util/vercmp.c | 8 |
8 files changed, 690 insertions, 192 deletions
diff --git a/src/util/.gitignore b/src/util/.gitignore index 2880ce2a..1813506b 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore @@ -2,6 +2,8 @@ .libs cleanupdelta cleanupdelta.exe +pacsort +pacsort.exe pactree pactree.exe testdb diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 30a2ee35..11308fbf 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -3,7 +3,7 @@ conffile = ${sysconfdir}/pacman.conf dbpath = ${localstatedir}/lib/pacman/ cachedir = ${localstatedir}/cache/pacman/pkg/ -bin_PROGRAMS = vercmp testpkg testdb cleanupdelta pactree +bin_PROGRAMS = vercmp testpkg testdb cleanupdelta pacsort pactree DEFS = -DLOCALEDIR=\"@localedir@\" \ -DCONFFILE=\"$(conffile)\" \ @@ -15,20 +15,22 @@ INCLUDES = -I$(top_srcdir)/lib/libalpm AM_CFLAGS = -pedantic -D_GNU_SOURCE -vercmp_SOURCES = vercmp.c -vercmp_LDADD = $(top_builddir)/lib/libalpm/version.o - -testpkg_SOURCES = testpkg.c -testpkg_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la - -testdb_SOURCES = testdb.c -testdb_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la - cleanupdelta_SOURCES = cleanupdelta.c cleanupdelta_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la +pacsort_SOURCES = pacsort.c +pacsort_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la + pactree_SOURCES = pactree.c pactree_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la +testdb_SOURCES = testdb.c +testdb_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la + +testpkg_SOURCES = testpkg.c +testpkg_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la + +vercmp_SOURCES = vercmp.c +vercmp_LDADD = $(top_builddir)/lib/libalpm/version.o # vim:set ts=2 sw=2 noet: diff --git a/src/util/cleanupdelta.c b/src/util/cleanupdelta.c index 71ba3a47..a45efdcc 100644 --- a/src/util/cleanupdelta.c +++ b/src/util/cleanupdelta.c @@ -29,21 +29,23 @@ #define BASENAME "cleanupdelta" +alpm_handle_t *handle = NULL; + static void cleanup(int signum) { - if(alpm_release() == -1) { - fprintf(stderr, "error releasing alpm: %s\n", alpm_strerrorlast()); + if(handle && alpm_release(handle) == -1) { + fprintf(stderr, "error releasing alpm\n"); } exit(signum); } -static void output_cb(pmloglevel_t level, const char *fmt, va_list args) +static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args) { if(strlen(fmt)) { switch(level) { - case PM_LOG_ERROR: printf("error: "); break; - case PM_LOG_WARNING: printf("warning: "); break; - //case PM_LOG_DEBUG: printf("debug: "); break; + case ALPM_LOG_ERROR: printf("error: "); break; + case ALPM_LOG_WARNING: printf("warning: "); break; + //case ALPM_LOG_DEBUG: printf("debug: "); break; default: return; } vprintf(fmt, args); @@ -55,7 +57,7 @@ static void checkpkgs(alpm_list_t *pkglist) { alpm_list_t *i, *j; for(i = pkglist; i; i = alpm_list_next(i)) { - pmpkg_t *pkg = alpm_list_getdata(i); + alpm_pkg_t *pkg = alpm_list_getdata(i); alpm_list_t *unused = alpm_pkg_unused_deltas(pkg); for(j = unused; j; j = alpm_list_next(j)) { char *delta = alpm_list_getdata(j); @@ -65,18 +67,19 @@ static void checkpkgs(alpm_list_t *pkglist) } } -static void checkdbs(char *dbpath, alpm_list_t *dbnames) { +static void checkdbs(const char *dbpath, alpm_list_t *dbnames) { char syncdbpath[PATH_MAX]; - pmdb_t *db = NULL; + alpm_db_t *db = NULL; alpm_list_t *i; + const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL; for(i = dbnames; i; i = alpm_list_next(i)) { char *dbname = alpm_list_getdata(i); snprintf(syncdbpath, PATH_MAX, "%s/sync/%s", dbpath, dbname); - db = alpm_db_register_sync(dbname); + db = alpm_db_register_sync(handle, dbname, level); if(db == NULL) { fprintf(stderr, "error: could not register sync database (%s)\n", - alpm_strerrorlast()); + alpm_strerror(alpm_errno(handle))); return; } checkpkgs(alpm_db_get_pkgcache(db)); @@ -93,7 +96,8 @@ static void usage(void) { int main(int argc, char *argv[]) { - char *dbpath = DBPATH; + const char *dbpath = DBPATH; + enum _alpm_errno_t err; int a = 1; alpm_list_t *dbnames = NULL; @@ -117,15 +121,14 @@ int main(int argc, char *argv[]) usage(); } - if(alpm_initialize() == -1) { - fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerrorlast()); - return(1); + handle = alpm_initialize(ROOTDIR, dbpath, &err); + if(!handle) { + fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err)); + return 1; } /* let us get log messages from libalpm */ - alpm_option_set_logcb(output_cb); - - alpm_option_set_dbpath(dbpath); + alpm_option_set_logcb(handle, output_cb); checkdbs(dbpath,dbnames); alpm_list_free(dbnames); diff --git a/src/util/pacsort.c b/src/util/pacsort.c new file mode 100644 index 00000000..109d1a16 --- /dev/null +++ b/src/util/pacsort.c @@ -0,0 +1,394 @@ +/* + * pacsort.c - a sort utility implementing alpm_pkg_vercmp + * + * Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "config.h" + +#include <errno.h> +#include <getopt.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <alpm.h> + +#define DELIM ' ' + +struct buffer_t { + char *mem; + size_t len; + size_t maxlen; +}; + +struct list_t { + char **list; + size_t count; + size_t maxcount; +}; + +static struct options_t { + int order; + int sortkey; + int null; + char delim; +} opts; + +static struct buffer_t *buffer_new(size_t initial_size) +{ + struct buffer_t *buf; + + buf = calloc(1, sizeof(*buf)); + if(!buf) { + return NULL; + } + + buf->mem = calloc(initial_size, sizeof(char)); + if(!buf->mem) { + free(buf); + return NULL; + } + + buf->len = 0; + buf->maxlen = initial_size; + + return buf; +} + +static void buffer_free(struct buffer_t *buf) +{ + if(!buf) { + return; + } + + if(buf->mem) { + free(buf->mem); + } + + free(buf); +} + +static int buffer_grow(struct buffer_t *buffer) +{ + size_t newsz = buffer->maxlen * 2.5; + buffer->mem = realloc(buffer->mem, newsz * sizeof(char)); + if(!buffer->mem) { + return 1; + } + buffer->maxlen = newsz; + + return 0; +} + +static struct list_t *list_new(size_t initial_size) +{ + struct list_t *list; + + list = calloc(1, sizeof(struct list_t)); + if(!list) { + return NULL; + } + + list->list = calloc(initial_size, sizeof(char **)); + if(!list->list) { + free(list); + return NULL; + } + + list->maxcount = initial_size; + + return list; +} + +static int list_grow(struct list_t *list) +{ + size_t newsz = list->maxcount * 2.5; + list->list = realloc(list->list, newsz * sizeof(char*)); + if(!list->list) { + return 1; + } + + list->maxcount = newsz; + + return 0; +} + +static int list_add(struct list_t *list, char *name) +{ + if(!list|!name) { + return 1; + } + + if(list->count + 1 >= list->maxcount) { + if(list_grow(list) != 0) { + return 1; + } + } + + list->list[list->count] = name; + list->count++; + + return 0; +} + +static void list_free(struct list_t *list) +{ + size_t i; + + if(!list) { + return; + } + + if(list->list) { + for(i = 0; i < list->count; i++) { + free(list->list[i]); + } + free(list->list); + free(list); + } +} + +static char *explode(struct buffer_t *buffer, struct list_t *list) +{ + char *name, *ptr, *end; + const char linedelim = opts.null ? '\0' : '\n'; + + ptr = buffer->mem; + while((end = memchr(ptr, linedelim, &buffer->mem[buffer->len] - ptr))) { + *end = '\0'; + name = strdup(ptr); + list_add(list, name); + ptr = end + 1; + } + + return ptr; +} + +static int splitfile(FILE *stream, struct buffer_t *buffer, struct list_t *list) +{ + size_t nread; + char *ptr; + + while(!feof(stream)) { + /* check if a read of BUFSIZ chars will overflow */ + if (buffer->len + BUFSIZ + 1 >= buffer->maxlen) { + if(buffer_grow(buffer) != 0) { + return 1; + } + } + + nread = fread(&buffer->mem[buffer->len], 1, BUFSIZ, stream); + if(nread == 0) { + break; /* EOF */ + } + buffer->len += nread; + + if((ptr = explode(buffer, list)) == NULL) { + return 1; + } + + if(ptr != buffer->mem) { + /* realign the data in the buffer */ + buffer->len = &buffer->mem[buffer->len] - ptr; + memmove(&buffer->mem[0], ptr, buffer->len + 1); + } + } + + if(buffer->len) { + char *name = strndup(buffer->mem, buffer->len + 1); + if(list_add(list, name) != 0) { + return 1; + } + } + + return 0; +} + +/* returns a pointer to the nth column of a string without being destructive */ +static const char *nth_column(const char *string) +{ + const char *prev, *ptr; + int col; + + ptr = prev = string; + for(col = 1; ptr && col <= opts.sortkey; col++) { + prev = ptr; + ptr = strchr(ptr, opts.delim); + if(ptr) { + ptr++; + } + } + + return prev; +} + +static int vercmp(const void *p1, const void *p2) +{ + const char *name1, *name2; + + name1 = *(const char **)p1; + name2 = *(const char **)p2; + + if(opts.sortkey == 0) { + return opts.order * alpm_pkg_vercmp(name1, name2); + } else { + return opts.order * alpm_pkg_vercmp(nth_column(name1), nth_column(name2)); + } +} + +static char escape_char(const char *string) +{ + const size_t len = strlen(string); + + if(!string || len > 2) { + return -1; + } + + if(len == 1) { + return *string; + } + + if(*string != '\\') { + return -1; + } + + switch(string[1]) { + case 't': + return '\t'; + case 'n': + return '\n'; + case 'v': + return '\v'; + case '0': + return '\0'; + default: + return -1; + } +} + +static void usage(void) +{ + fprintf(stderr, "pacsort v" PACKAGE_VERSION "\n" + "Usage: pacsort [options] [files...]\n\n" + " -h, --help display this help message\n" + " -k, --key <index> sort input starting on specified column\n" + " -r, --reverse sort in reverse order (default: oldest to newest)\n" + " -t, --separator <sep> specify field separator (default: space)\n" + " -z, --null lines end with null bytes, not newlines\n\n"); +} + +static int parse_options(int argc, char **argv) +{ + int opt; + + static const struct option opttable[] = { + {"help", no_argument, 0, 'h'}, + {"key", required_argument, 0, 'k'}, + {"reverse", no_argument, 0, 'r'}, + {"separator", required_argument, 0, 't'}, + {"null", no_argument, 0, 'z'}, + {0, 0, 0, 0} + }; + + while((opt = getopt_long(argc, argv, "hk:rt:z", opttable, NULL)) != -1) { + switch(opt) { + case 'h': + return 1; + case 'k': + opts.sortkey = (int)strtol(optarg, NULL, 10); + if(opts.sortkey <= 0) { + fprintf(stderr, "error: invalid sort key -- %s\n", optarg); + return 1; + } + break; + case 'r': + opts.order = -1; + break; + case 't': + opts.delim = escape_char(optarg); + if(opts.delim == -1) { + fprintf(stderr, "error: invalid field separator -- `%s'\n", optarg); + return 1; + } + break; + case 'z': + opts.null = 1; + break; + default: + return 1; + } + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct list_t *list; + struct buffer_t *buffer; + size_t i; + + /* option defaults */ + opts.order = 1; + opts.delim = DELIM; + opts.sortkey = 0; + opts.null = 0; + + if(parse_options(argc, argv) != 0) { + usage(); + return 2; + } + + list = list_new(100); + buffer = buffer_new(BUFSIZ * 3); + + if(optind == argc) { + if(splitfile(stdin, buffer, list) != 0) { + fprintf(stderr, "%s: memory exhausted\n", argv[0]); + return ENOMEM; + } + } else { + while(optind < argc) { + FILE *input = fopen(argv[optind], "r"); + if(input) { + if(splitfile(input, buffer, list) != 0) { + fprintf(stderr, "%s: memory exhausted\n", argv[0]); + return ENOMEM; + } + fclose(input); + } else { + fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind], strerror(errno)); + } + optind++; + } + } + + if(list->count) { + const char linedelim = opts.null ? '\0' : '\n'; + qsort(list->list, list->count, sizeof(char *), vercmp); + for(i = 0; i < list->count; i++) { + printf("%s%c", list->list[i], linedelim); + } + list_free(list); + } + + buffer_free(buffer); + + return 0; +} + +/* vim: set ts=2 sw=2 noet: */ diff --git a/src/util/pactree.c b/src/util/pactree.c index 6a10006f..5a27cc77 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -26,63 +26,137 @@ #include <alpm.h> #include <alpm_list.h> +#define LINE_MAX 512 + /* output */ -char *provides = " provides"; -char *unresolvable = " [unresolvable]"; -char *branch_tip1 = "|--"; -char *branch_tip2 = "+--"; -int indent_size = 3; - -/* color */ -char *branch1_color = "\033[0;33m"; /* yellow */ -char *branch2_color = "\033[0;37m"; /* white */ -char *leaf1_color = "\033[1;32m"; /* bold green */ -char *leaf2_color = "\033[0;32m"; /* green */ -char *color_off = "\033[0m"; +struct graph_style { + const char *provides; + const char *tip1; + const char *tip2; + int indent; +}; + +static struct graph_style graph_default = { + " provides", + "|--", + "+--", + 3 +}; + +static struct graph_style graph_linear = { + "", + "", + "", + 0 +}; + +/* color choices */ +struct color_choices { + const char *branch1; + const char *branch2; + const char *leaf1; + const char *leaf2; + const char *off; +}; + +static struct color_choices use_color = { + "\033[0;33m", /* yellow */ + "\033[0;37m", /* white */ + "\033[1;32m", /* bold green */ + "\033[0;32m", /* green */ + "\033[0m" +}; + +static struct color_choices no_color = { + "", + "", + "", + "", + "" +}; /* globals */ -pmdb_t *db_local; +alpm_handle_t *handle = NULL; alpm_list_t *walked = NULL; alpm_list_t *provisions = NULL; /* options */ -int color = 0; +struct color_choices *color = &no_color; +struct graph_style *style = &graph_default; int graphviz = 0; -int linear = 0; int max_depth = -1; int reverse = 0; int unique = 0; -char *dbpath = NULL; +int searchsyncs = 0; +const char *dbpath = DBPATH; -static int alpm_local_init(void) +static char *strtrim(char *str) { - int ret; + char *pch = str; - ret = alpm_initialize(); - if(ret != 0) { - return(ret); + if(str == NULL || *str == '\0') { + /* string is empty, so we're done. */ + return str; } - ret = alpm_option_set_root(ROOTDIR); - if(ret != 0) { - return(ret); + while(isspace((unsigned char)*pch)) { + pch++; + } + if(pch != str) { + memmove(str, pch, (strlen(pch) + 1)); } - if(dbpath) { - ret = alpm_option_set_dbpath(dbpath); - } else { - ret = alpm_option_set_dbpath(DBPATH); + /* check if there wasn't anything but whitespace in the string. */ + if(*str == '\0') { + return str; } - if(ret != 0) { - return(ret); + + pch = (str + (strlen(str) - 1)); + while(isspace((unsigned char)*pch)) { + pch--; } + *++pch = '\0'; + + return str; +} + +static int register_syncs(void) { + FILE *fp; + char *ptr, *section = NULL; + char line[LINE_MAX]; + const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL; - db_local = alpm_option_get_localdb(); - if(!db_local) { - return(1); + fp = fopen(CONFFILE, "r"); + if(!fp) { + return 1; } - return(0); + while(fgets(line, LINE_MAX, fp)) { + strtrim(line); + + if(line[0] == '#' || !strlen(line)) { + continue; + } + + if((ptr = strchr(line, '#'))) { + *ptr = '\0'; + strtrim(line); + } + + if(line[0] == '[' && line[strlen(line) - 1] == ']') { + free(section); + section = strndup(&line[1], strlen(line) - 2); + + if(section && strcmp(section, "options") != 0) { + alpm_db_register_sync(handle, section, level); + } + } + } + + free(section); + fclose(fp); + + return 0; } static int parse_options(int argc, char *argv[]) @@ -90,7 +164,7 @@ static int parse_options(int argc, char *argv[]) int opt, option_index = 0; char *endptr = NULL; - static struct option opts[] = { + static const struct option opts[] = { {"dbpath", required_argument, 0, 'b'}, {"color", no_argument, 0, 'c'}, {"depth", required_argument, 0, 'd'}, @@ -98,21 +172,22 @@ static int parse_options(int argc, char *argv[]) {"help", no_argument, 0, 'h'}, {"linear", no_argument, 0, 'l'}, {"reverse", no_argument, 0, 'r'}, + {"sync", no_argument, 0, 'S'}, {"unique", no_argument, 0, 'u'}, {0, 0, 0, 0} }; - while((opt = getopt_long(argc, argv, "b:cd:ghlru", opts, &option_index))) { + while((opt = getopt_long(argc, argv, "b:cd:ghlrsu", opts, &option_index))) { if(opt < 0) { break; } switch(opt) { case 'b': - dbpath = strdup(optarg); + dbpath = optarg; break; case 'c': - color = 1; + color = &use_color; break; case 'd': /* validate depth */ @@ -126,40 +201,30 @@ static int parse_options(int argc, char *argv[]) graphviz = 1; break; case 'l': - linear = 1; + style = &graph_linear; break; case 'r': reverse = 1; break; + case 's': + searchsyncs = 1; + break; case 'u': - unique = linear = 1; + unique = 1; + style = &graph_linear; break; case 'h': case '?': default: - return(1); + return 1; } } if(!argv[optind]) { - return(1); + return 1; } - if(!color) { - branch1_color = ""; - branch2_color = ""; - leaf1_color = ""; - leaf2_color = ""; - color_off = ""; - } - if(linear) { - provides = ""; - branch_tip1 = ""; - branch_tip2 = ""; - indent_size = 0; - } - - return(0); + return 0; } static void usage(void) @@ -172,25 +237,22 @@ static void usage(void) " -g, --graph generate output for graphviz's dot\n" " -l, --linear enable linear output\n" " -r, --reverse show reverse dependencies\n" + " -s, --sync search sync DBs instead of local\n" " -u, --unique show dependencies with no duplicates (implies -l)\n\n" " -h, --help display this help message\n"); } static void cleanup(void) { - if(dbpath) { - free(dbpath); - } - alpm_list_free(walked); alpm_list_free(provisions); - alpm_release(); + alpm_release(handle); } /* pkg provides provision */ static void print_text(const char *pkg, const char *provision, int depth) { - int indent_sz = (depth + 1) * indent_size; + int indent_sz = (depth + 1) * style->indent; if(!pkg && !provision) { /* not much we can do */ @@ -199,17 +261,17 @@ static void print_text(const char *pkg, const char *provision, int depth) if(!pkg && provision) { /* we failed to resolve provision */ - printf("%s%*s%s%s%s%s%s\n", branch1_color, indent_sz, branch_tip1, - leaf1_color, provision, branch1_color, unresolvable, color_off); + printf("%s%*s%s%s%s [unresolvable]%s\n", color->branch1, indent_sz, + style->tip1, color->leaf1, provision, color->branch1, color->off); } else if(provision && strcmp(pkg, provision) != 0) { /* pkg provides provision */ - printf("%s%*s%s%s%s%s %s%s%s\n", branch2_color, indent_sz, branch_tip2, - leaf1_color, pkg, leaf2_color, provides, leaf1_color, provision, - color_off); + printf("%s%*s%s%s%s%s %s%s%s\n", color->branch2, indent_sz, style->tip2, + color->leaf1, pkg, color->leaf2, style->provides, color->leaf1, provision, + color->off); } else { /* pkg is a normal package */ - printf("%s%*s%s%s%s\n", branch1_color, indent_sz, branch_tip1, leaf1_color, - pkg, color_off); + printf("%s%*s%s%s%s\n", color->branch1, indent_sz, style->tip1, color->leaf1, + pkg, color->off); } } @@ -255,19 +317,31 @@ static void print_end(void) } } +static alpm_pkg_t *get_pkg_from_dbs(alpm_list_t *dbs, const char *needle) { + alpm_list_t *i; + alpm_pkg_t *ret; + + for(i = dbs; i; i = alpm_list_next(i)) { + ret = alpm_db_get_pkg(alpm_list_getdata(i), needle); + if(ret) { + return ret; + } + } + return NULL; +} /** * walk dependencies in reverse, showing packages which require the target */ -static void walk_reverse_deps(pmpkg_t *pkg, int depth) +static void walk_reverse_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, int depth) { alpm_list_t *required_by, *i; - if((max_depth >= 0) && (depth == max_depth + 1)) { + if(!pkg || ((max_depth >= 0) && (depth == max_depth + 1))) { return; } - walked = alpm_list_add(walked, (void*)alpm_pkg_get_name(pkg)); + walked = alpm_list_add(walked, (void *)alpm_pkg_get_name(pkg)); required_by = alpm_pkg_compute_requiredby(pkg); for(i = required_by; i; i = alpm_list_next(i)) { @@ -281,7 +355,7 @@ static void walk_reverse_deps(pmpkg_t *pkg, int depth) } } else { print(alpm_pkg_get_name(pkg), pkgname, NULL, depth); - walk_reverse_deps(alpm_db_get_pkg(db_local, pkgname), depth + 1); + walk_reverse_deps(dblist, get_pkg_from_dbs(dblist, pkgname), depth + 1); } } @@ -291,7 +365,7 @@ static void walk_reverse_deps(pmpkg_t *pkg, int depth) /** * walk dependencies, showing dependencies of the target */ -static void walk_deps(pmpkg_t *pkg, int depth) +static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, int depth) { alpm_list_t *i; @@ -299,12 +373,11 @@ static void walk_deps(pmpkg_t *pkg, int depth) return; } - walked = alpm_list_add(walked, (void*)alpm_pkg_get_name(pkg)); + walked = alpm_list_add(walked, (void *)alpm_pkg_get_name(pkg)); for(i = alpm_pkg_get_depends(pkg); i; i = alpm_list_next(i)) { - pmdepend_t *depend = alpm_list_getdata(i); - pmpkg_t *provider = alpm_find_satisfier(alpm_db_get_pkgcache(db_local), - alpm_dep_get_name(depend)); + alpm_depend_t *depend = alpm_list_getdata(i); + alpm_pkg_t *provider = alpm_find_dbs_satisfier(handle, dblist, depend->name); if(provider) { const char *provname = alpm_pkg_get_name(provider); @@ -313,41 +386,57 @@ static void walk_deps(pmpkg_t *pkg, int depth) /* if we've already seen this package, don't print in "unique" output * and don't recurse */ if(!unique) { - print(alpm_pkg_get_name(pkg), provname, alpm_dep_get_name(depend), depth); + print(alpm_pkg_get_name(pkg), provname, depend->name, depth); } } else { - print(alpm_pkg_get_name(pkg), provname, alpm_dep_get_name(depend), depth); - walk_deps(provider, depth + 1); + print(alpm_pkg_get_name(pkg), provname, depend->name, depth); + walk_deps(dblist, provider, depth + 1); } } else { /* unresolvable package */ - print(alpm_pkg_get_name(pkg), NULL, alpm_dep_get_name(depend), depth); + print(alpm_pkg_get_name(pkg), NULL, depend->name, depth); } } } int main(int argc, char *argv[]) { - int ret; + int freelist = 0, ret = 0; + enum _alpm_errno_t err; const char *target_name; - pmpkg_t *pkg; + alpm_pkg_t *pkg; + alpm_list_t *dblist = NULL; - ret = parse_options(argc, argv); - if(ret != 0) { + if(parse_options(argc, argv) != 0) { usage(); + ret = 1; goto finish; } - ret = alpm_local_init(); - if(ret != 0) { - fprintf(stderr, "error: cannot initialize alpm: %s\n", alpm_strerrorlast()); + handle = alpm_initialize(ROOTDIR, dbpath, &err); + if(!handle) { + fprintf(stderr, "error: cannot initialize alpm: %s\n", + alpm_strerror(err)); + ret = 1; goto finish; } + if(searchsyncs) { + if(register_syncs() != 0) { + fprintf(stderr, "error: failed to register sync DBs\n"); + ret = 1; + goto finish; + } + dblist = alpm_option_get_syncdbs(handle); + } else { + dblist = alpm_list_add(dblist, alpm_option_get_localdb(handle)); + freelist = 1; + } + /* we only care about the first non option arg for walking */ target_name = argv[optind]; - pkg = alpm_find_satisfier(alpm_db_get_pkgcache(db_local), target_name); + pkg = alpm_find_dbs_satisfier(handle, dblist, target_name); if(!pkg) { fprintf(stderr, "error: package '%s' not found\n", target_name); ret = 1; @@ -357,16 +446,20 @@ int main(int argc, char *argv[]) print_start(alpm_pkg_get_name(pkg), target_name); if(reverse) { - walk_reverse_deps(pkg, 1); + walk_reverse_deps(dblist, pkg, 1); } else { - walk_deps(pkg, 1); + walk_deps(dblist, pkg, 1); } print_end(); + if(freelist) { + alpm_list_free(dblist); + } + finish: cleanup(); - return(ret); + return ret; } /* vim: set ts=2 sw=2 noet: */ diff --git a/src/util/testdb.c b/src/util/testdb.c index 0436a23f..d85687a4 100644 --- a/src/util/testdb.c +++ b/src/util/testdb.c @@ -31,20 +31,23 @@ #define BASENAME "testdb" -static void cleanup(int signum) { - if(alpm_release() == -1) { - fprintf(stderr, "error releasing alpm: %s\n", alpm_strerrorlast()); +alpm_handle_t *handle = NULL; + +static void cleanup(int signum) +{ + if(handle && alpm_release(handle) == -1) { + fprintf(stderr, "error releasing alpm\n"); } exit(signum); } -static void output_cb(pmloglevel_t level, const char *fmt, va_list args) +static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args) { if(strlen(fmt)) { switch(level) { - case PM_LOG_ERROR: printf("error: "); break; - case PM_LOG_WARNING: printf("warning: "); break; + case ALPM_LOG_ERROR: printf("error: "); break; + case ALPM_LOG_WARNING: printf("warning: "); break; default: return; } vprintf(fmt, args); @@ -59,14 +62,14 @@ static int check_localdb_files(void) int ret = 0; DIR *dir; - dbpath = alpm_option_get_dbpath(); + dbpath = alpm_option_get_dbpath(handle); snprintf(path, sizeof(path), "%slocal", dbpath); if(!(dir = opendir(path))) { fprintf(stderr, "error : %s : %s\n", path, strerror(errno)); - return(1); + return 1; } - while ((ent = readdir(dir)) != NULL) { + while((ent = readdir(dir)) != NULL) { if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0 || ent->d_name[0] == '.') { continue; @@ -85,10 +88,10 @@ static int check_localdb_files(void) } if(closedir(dir)) { fprintf(stderr, "error closing dbpath : %s\n", strerror(errno)); - return(1); + return 1; } - return(ret); + return ret; } static int checkdeps(alpm_list_t *pkglist) @@ -96,18 +99,17 @@ static int checkdeps(alpm_list_t *pkglist) alpm_list_t *data, *i; int ret = 0; /* check dependencies */ - data = alpm_checkdeps(pkglist, 0, NULL, pkglist); + data = alpm_checkdeps(handle, pkglist, NULL, pkglist, 0); for(i = data; i; i = alpm_list_next(i)) { - pmdepmissing_t *miss = alpm_list_getdata(i); - pmdepend_t *dep = alpm_miss_get_dep(miss); - char *depstring = alpm_dep_compute_string(dep); - printf("missing dependency for %s : %s\n", alpm_miss_get_target(miss), + alpm_depmissing_t *miss = alpm_list_getdata(i); + char *depstring = alpm_dep_compute_string(miss->depend); + printf("missing dependency for %s : %s\n", miss->target, depstring); free(depstring); ret++; } FREELIST(data); - return(ret); + return ret; } static int checkconflicts(alpm_list_t *pkglist) @@ -115,50 +117,48 @@ static int checkconflicts(alpm_list_t *pkglist) alpm_list_t *data, *i; int ret = 0; /* check conflicts */ - data = alpm_checkconflicts(pkglist); + data = alpm_checkconflicts(handle, pkglist); for(i = data; i; i = i->next) { - pmconflict_t *conflict = alpm_list_getdata(i); - printf("%s conflicts with %s\n", alpm_conflict_get_package1(conflict), - alpm_conflict_get_package2(conflict)); + alpm_conflict_t *conflict = alpm_list_getdata(i); + printf("%s conflicts with %s\n", + conflict->package1, conflict->package2); ret++; } FREELIST(data); - return(ret); + return ret; } -static int check_localdb(void) { +static int check_localdb(void) +{ int ret = 0; - pmdb_t *db = NULL; + alpm_db_t *db = NULL; alpm_list_t *pkglist; ret = check_localdb_files(); if(ret) { - return(ret); + return ret; } - db = alpm_option_get_localdb(); - if(db == NULL) { - fprintf(stderr, "error: could not register 'local' database (%s)\n", - alpm_strerrorlast()); - cleanup(EXIT_FAILURE); - } + db = alpm_option_get_localdb(handle); pkglist = alpm_db_get_pkgcache(db); ret += checkdeps(pkglist); ret += checkconflicts(pkglist); - return(ret); + return ret; } -static int check_syncdbs(alpm_list_t *dbnames) { +static int check_syncdbs(alpm_list_t *dbnames) +{ int ret = 0; - pmdb_t *db = NULL; + alpm_db_t *db = NULL; alpm_list_t *i, *pkglist, *syncpkglist = NULL; + const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL; for(i = dbnames; i; i = alpm_list_next(i)) { char *dbname = alpm_list_getdata(i); - db = alpm_db_register_sync(dbname); + db = alpm_db_register_sync(handle, dbname, level); if(db == NULL) { fprintf(stderr, "error: could not register sync database (%s)\n", - alpm_strerrorlast()); + alpm_strerror(alpm_errno(handle))); ret = 1; goto cleanup; } @@ -169,10 +169,11 @@ static int check_syncdbs(alpm_list_t *dbnames) { cleanup: alpm_list_free(syncpkglist); - return(ret); + return ret; } -static void usage(void) { +static void usage(void) +{ fprintf(stderr, "usage:\n"); fprintf(stderr, "\t%s [-b <pacman db>] : check the local database\n", BASENAME); @@ -184,7 +185,8 @@ static void usage(void) { int main(int argc, char *argv[]) { int ret = 0; - char *dbpath = DBPATH; + enum _alpm_errno_t err; + const char *dbpath = DBPATH; int a = 1; alpm_list_t *dbnames = NULL; @@ -204,18 +206,14 @@ int main(int argc, char *argv[]) a++; } - if(alpm_initialize() == -1) { - fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerrorlast()); - return(EXIT_FAILURE); + handle = alpm_initialize(ROOTDIR, dbpath, &err); + if(!handle) { + fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err)); + return EXIT_FAILURE; } /* let us get log messages from libalpm */ - alpm_option_set_logcb(output_cb); - - if(alpm_option_set_dbpath(dbpath) != 0) { - fprintf(stderr, "cannot set dbpath: %s\n", alpm_strerrorlast()); - return(EXIT_FAILURE); - } + alpm_option_set_logcb(handle, output_cb); if(!dbnames) { ret = check_localdb(); diff --git a/src/util/testpkg.c b/src/util/testpkg.c index d0d9cac1..ac2dde28 100644 --- a/src/util/testpkg.c +++ b/src/util/testpkg.c @@ -24,14 +24,14 @@ #define BASENAME "testpkg" -static void output_cb(pmloglevel_t level, const char *fmt, va_list args) +static void output_cb(alpm_loglevel_t level, const char *fmt, va_list args) { if(fmt[0] == '\0') { return; } switch(level) { - case PM_LOG_ERROR: printf("error: "); break; - case PM_LOG_WARNING: printf("warning: "); break; + case ALPM_LOG_ERROR: printf("error: "); break; + case ALPM_LOG_WARNING: printf("warning: "); break; default: return; /* skip other messages */ } vprintf(fmt, args); @@ -40,32 +40,38 @@ static void output_cb(pmloglevel_t level, const char *fmt, va_list args) int main(int argc, char *argv[]) { int retval = 1; /* default = false */ - pmpkg_t *pkg = NULL; + alpm_handle_t *handle; + enum _alpm_errno_t err; + alpm_pkg_t *pkg = NULL; + const alpm_siglevel_t level = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL; if(argc != 2) { fprintf(stderr, "usage: %s <package file>\n", BASENAME); - return(1); + return 1; } - if(alpm_initialize() == -1) { - fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerrorlast()); - return(1); + handle = alpm_initialize(ROOTDIR, DBPATH, &err); + if(!handle) { + fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(err)); + return 1; } /* let us get log messages from libalpm */ - alpm_option_set_logcb(output_cb); + alpm_option_set_logcb(handle, output_cb); - if(alpm_pkg_load(argv[1], 1, &pkg) == -1 || pkg == NULL) { - switch(pm_errno) { - case PM_ERR_PKG_OPEN: + if(alpm_pkg_load(handle, argv[1], 1, level, &pkg) == -1 + || pkg == NULL) { + err = alpm_errno(handle); + switch(err) { + case ALPM_ERR_PKG_OPEN: printf("Cannot open the given file.\n"); break; - case PM_ERR_LIBARCHIVE: - case PM_ERR_PKG_INVALID: + case ALPM_ERR_LIBARCHIVE: + case ALPM_ERR_PKG_INVALID: printf("Package is invalid.\n"); break; default: - printf("libalpm error: %s\n", alpm_strerrorlast()); + printf("libalpm error: %s\n", alpm_strerror(err)); break; } retval = 1; @@ -75,9 +81,9 @@ int main(int argc, char *argv[]) retval = 0; } - if(alpm_release() == -1) { - fprintf(stderr, "error releasing alpm: %s\n", alpm_strerrorlast()); + if(alpm_release(handle) == -1) { + fprintf(stderr, "error releasing alpm\n"); } - return(retval); + return retval; } diff --git a/src/util/vercmp.c b/src/util/vercmp.c index adb5a42a..f4356fb4 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -20,7 +20,7 @@ #include <stdlib.h> #include <stdio.h> /* printf */ -#include <string.h> /* strncpy */ +#include <string.h> #define BASENAME "vercmp" @@ -45,13 +45,13 @@ int main(int argc, char *argv[]) if(argc == 1) { usage(); - return(2); + return 2; } if(argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "--usage") == 0)) { usage(); - return(0); + return 0; } if(argc > 2) { s2 = argv[2]; @@ -62,5 +62,5 @@ int main(int argc, char *argv[]) ret = alpm_pkg_vercmp(s1, s2); printf("%d\n", ret); - return(EXIT_SUCCESS); + return EXIT_SUCCESS; } |