From 3ec82a5a420fc00d38ad7dded819525a91533652 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 5 Mar 2010 08:13:25 +0000 Subject: First steps for libapk --- .gitignore | 1 + Make.rules | 22 +++++++++++++++-- Makefile | 3 ++- src/Makefile | 24 +++++++++++++------ src/add.c | 1 + src/apk.c | 60 +++------------------------------------------- src/apk_database.h | 2 +- src/apk_defines.h | 9 +------ src/apk_print.h | 27 +++++++++++++++++++++ src/apk_state.h | 3 +++ src/archive.c | 1 + src/cache.c | 1 + src/database.c | 7 +++++- src/del.c | 1 + src/fetch.c | 1 + src/fix.c | 1 + src/index.c | 1 + src/info.c | 1 + src/package.c | 1 + src/print.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/state.c | 1 + src/update.c | 1 + src/upgrade.c | 1 + src/ver.c | 1 + src/verify.c | 1 + 25 files changed, 165 insertions(+), 77 deletions(-) create mode 100644 src/apk_print.h create mode 100644 src/print.c diff --git a/.gitignore b/.gitignore index c49a2cc..f6f9cf6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ apk *.o *.d *.cmd +*.so diff --git a/Make.rules b/Make.rules index 3bdf267..9fd4862 100644 --- a/Make.rules +++ b/Make.rules @@ -70,7 +70,7 @@ INSTALL := install INSTALLDIR := $(INSTALL) -d CFLAGS ?= -g -O2 -CFLAGS_ALL := -Werror -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 +CFLAGS_ALL := -Werror -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 -fPIC CFLAGS_ALL += $(CFLAGS) LDFLAGS ?= -g @@ -196,6 +196,24 @@ $(obj)/%.o: override local-target-prereqs=% $(obj)/%.o: $(src)/%.c FORCE $(call if_changed_rule,cc_o_c) +##### +# Link shared libraries +# +__shlibs := $(addprefix $(obj)/,$(sort $(shlibs-y))) +shobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(shlibs-y),$($(m)-objs)))) + +# link shared library +quiet_cmd_shlib = LD -shared $@ + cmd_shlib = $(CC) $(ld_flags) -shared -o $@ \ + $(addprefix $(obj)/,$($(@F)-objs)) \ + $(LIBS) $(LIBS_$(@F)) + +$(__shlibs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs)) + +$(__shlibs): $(obj)/%: $(shobjs) FORCE + $(call if_changed,shlib) + +targets += $(__shlibs) $(shobjs) ##### # Link programs @@ -256,7 +274,7 @@ ifeq ($(toplevelrun),yes) \( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \ -type f -print | xargs rm -f endif - $(Q)rm -rf $(addprefix $(obj)/,$(sort $(progs-y) $(progs-n) $(progs-))) + $(Q)rm -rf $(addprefix $(obj)/,$(sort $(progs-y) $(progs-n) $(progs-) $(shlibs-y) $(shlibs-n) $(shlibs-))) ifeq ($(origin VERSION),command line) DIST_VERSION=$(VERSION) diff --git a/Makefile b/Makefile index e8e7195..50d5b03 100644 --- a/Makefile +++ b/Makefile @@ -9,11 +9,12 @@ VERSION := 2.0.3 DESTDIR := SBINDIR := /sbin +LIBDIR := /lib CONFDIR := /etc/apk MANDIR := /usr/share/man DOCDIR := /usr/share/doc/apk -export DESTDIR SBINDIR CONFDIR MANDIR DOCDIR +export DESTDIR SBINDIR LIBDIR CONFDIR MANDIR DOCDIR ## # Top-level rules and targets diff --git a/src/Makefile b/src/Makefile index 0f60fa9..b0b2a67 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,24 +4,34 @@ ifeq ($(shell pkg-config --print-errors --exists $(PKGDEPS) || echo fail),fail) $(error Build dependencies are not met) endif +shlibs-y += libapk.so +libapk.so-objs := common.o state.o database.o package.o archive.o \ + version.o io.o url.o gunzip.o blob.o hash.o print.o + progs-y += apk -apk-objs := common.o state.o database.o package.o archive.o \ - version.o io.o url.o gunzip.o blob.o hash.o apk.o \ - add.o del.o fix.o update.o info.o search.o upgrade.o \ - cache.o ver.o index.o fetch.o audit.o verify.o +apk-objs := apk.o add.o del.o fix.o update.o info.o \ + search.o upgrade.o cache.o ver.o index.o fetch.o \ + audit.o verify.o + CFLAGS += -D_ATFILE_SOURCE CFLAGS_apk.o := -DAPK_VERSION=\"$(FULL_VERSION)\" +LIBS_apk := -lapk progs-$(STATIC) += apk.static apk.static-objs := $(apk-objs) LDFLAGS_apk.static := -static -LDFLAGS_apk += -nopie +LDFLAGS_apk += -nopie -L$(obj) CFLAGS += $(shell pkg-config --cflags $(PKGDEPS)) LIBS := -Wl,--as-needed \ $(shell pkg-config --libs $(PKGDEPS)) \ -Wl,--no-as-needed -install: - $(INSTALLDIR) $(DESTDIR)$(SBINDIR) + +$(obj)/apk: $(obj)/libapk.so + +install: $(obj)/apk $(obj)/libapk.so + $(INSTALLDIR) $(DESTDIR)$(SBINDIR) $(DESTDIR)$(LIBDIR) $(INSTALL) $(obj)/apk $(DESTDIR)$(SBINDIR) + $(INSTALL) $(obj)/libapk.so $(DESTDIR)$(LIBDIR) + diff --git a/src/add.c b/src/add.c index 91570fe..f13215a 100644 --- a/src/add.c +++ b/src/add.c @@ -14,6 +14,7 @@ #include "apk_applet.h" #include "apk_database.h" #include "apk_state.h" +#include "apk_print.h" struct add_ctx { const char *virtpkg; diff --git a/src/apk.c b/src/apk.c index 2273fa4..8575f66 100644 --- a/src/apk.c +++ b/src/apk.c @@ -24,9 +24,7 @@ #include "apk_database.h" #include "apk_applet.h" #include "apk_blob.h" - -int apk_verbosity = 1, apk_wait; -unsigned int apk_flags = 0; +#include "apk_print.h" static struct apk_option generic_options[] = { { 'h', "help", "Show generic help or applet specific help" }, @@ -60,64 +58,12 @@ static struct apk_option generic_options[] = { { 0x111, "overlay-from-stdin", "Read list of overlay files from stdin" }, }; -const char *apk_error_str(int error) -{ - if (error < 0) - error = -error; - switch (error) { - case ENOKEY: - return "UNTRUSTED signature"; - case EKEYREJECTED: - return "BAD signature"; - case EIO: - return "IO ERROR"; - case EBADMSG: - return "BAD archive"; - case ENOMSG: - return "archive does not contain expected data"; - default: - return strerror(error); - } -} - -void apk_log(const char *prefix, const char *format, ...) -{ - va_list va; - - if (prefix != NULL) - fprintf(stderr, "%s", prefix); - va_start(va, format); - vfprintf(stderr, format, va); - va_end(va); - fprintf(stderr, "\n"); -} - static int version(void) { printf("apk-tools " APK_VERSION "\n"); return 0; } -int apk_print_indented(struct apk_indent *i, apk_blob_t blob) -{ - static const int wrap_length = 80; - - if (i->x + blob.len + 1 >= wrap_length) { - i->x = i->indent; - printf("\n%*s", i->indent - 1, ""); - } else if (i->x+1 < i->indent) { - printf("%*s", i->indent - i->x - 1, ""); - } - i->x += printf(" %.*s", blob.len, blob.ptr); - return 0; -} - -void apk_print_indented_words(struct apk_indent *i, const char *text) -{ - apk_blob_for_each_segment(APK_BLOB_STR(text), " ", - (apk_blob_cb) apk_print_indented, i); -} - static int format_option(char *buf, size_t len, struct apk_option *o, const char *separator) { @@ -289,7 +235,7 @@ int main(int argc, char **argv) struct apk_applet *applet; char short_options[256], *sopt; struct option *opt, *all_options; - int r, optindex, num_options; + int r, optindex, num_options, apk_wait = 0; void *ctx = NULL; struct apk_repository_list *repo = NULL; struct apk_database db; @@ -410,7 +356,7 @@ int main(int argc, char **argv) argv++; } - r = apk_db_open(&db, &dbopts); + r = apk_db_open(&db, &dbopts, apk_wait); if (r != 0) { apk_error("Failed to open apk database: %s", apk_error_str(r)); diff --git a/src/apk_database.h b/src/apk_database.h index da3b0ae..ffb1bce 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -155,7 +155,7 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db, APK_OPENF_NO_SCRIPTS | \ APK_OPENF_NO_WORLD) -int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts); +int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts, int apk_wait); void apk_db_close(struct apk_database *db); int apk_db_write_config(struct apk_database *db); int apk_db_run_triggers(struct apk_database *db); diff --git a/src/apk_defines.h b/src/apk_defines.h index 4a07414..30927bd 100644 --- a/src/apk_defines.h +++ b/src/apk_defines.h @@ -47,7 +47,7 @@ (type *)( (char *)__mptr - offsetof(type,member) );}) #endif -extern int apk_verbosity, apk_wait; +extern int apk_verbosity; extern unsigned int apk_flags; #define APK_FORCE 0x0001 @@ -65,13 +65,6 @@ extern unsigned int apk_flags; #define APK_NO_NETWORK 0x1000 #define APK_OVERLAY_FROM_STDIN 0x2000 -#define apk_error(args...) do { apk_log("ERROR: ", args); } while (0) -#define apk_warning(args...) do { if (apk_verbosity > 0) { apk_log("WARNING: ", args); } } while (0) -#define apk_message(args...) do { if (apk_verbosity > 0) { apk_log(NULL, args); } } while (0) - -void apk_log(const char *prefix, const char *format, ...); -const char *apk_error_str(int error); - static inline size_t apk_calc_installed_size(size_t size) { const size_t bsize = 4 * 1024; diff --git a/src/apk_print.h b/src/apk_print.h new file mode 100644 index 0000000..fdbab11 --- /dev/null +++ b/src/apk_print.h @@ -0,0 +1,27 @@ +/* apk_state.h - Alpine Package Keeper (APK) + * + * Copyright (C) 2005-2008 Natanael Copa + * Copyright (C) 2008 Timo Teräs + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. See http://www.gnu.org/ for details. + */ + +#ifndef APK_PRINT_H +#define APK_PRINT_H + +#include "apk_blob.h" + +#define apk_error(args...) do { apk_log("ERROR: ", args); } while (0) +#define apk_warning(args...) do { if (apk_verbosity > 0) { apk_log("WARNING: ", args); } } while (0) +#define apk_message(args...) do { if (apk_verbosity > 0) { apk_log(NULL, args); } } while (0) + +void apk_log(const char *prefix, const char *format, ...); +const char *apk_error_str(int error); + +int apk_print_indented(struct apk_indent *i, apk_blob_t blob); +void apk_print_indented_words(struct apk_indent *i, const char *text); + +#endif diff --git a/src/apk_state.h b/src/apk_state.h index 42d3f3f..345acf3 100644 --- a/src/apk_state.h +++ b/src/apk_state.h @@ -43,4 +43,7 @@ int apk_state_lock_name(struct apk_state *state, struct apk_name *name, struct apk_package *newpkg); +int apk_print_indented(struct apk_indent *i, apk_blob_t blob); +void apk_print_indented_words(struct apk_indent *i, const char *text); + #endif diff --git a/src/archive.c b/src/archive.c index b27c039..2df93d9 100644 --- a/src/archive.c +++ b/src/archive.c @@ -24,6 +24,7 @@ #include #include "apk_defines.h" +#include "apk_print.h" #include "apk_archive.h" struct tar_header { diff --git a/src/cache.c b/src/cache.c index 4ed89fa..5311815 100644 --- a/src/cache.c +++ b/src/cache.c @@ -19,6 +19,7 @@ #include "apk_database.h" #include "apk_state.h" #include "apk_package.h" +#include "apk_print.h" #define CACHE_CLEAN BIT(0) #define CACHE_DOWNLOAD BIT(1) diff --git a/src/database.c b/src/database.c index 11864cb..3d49c9e 100644 --- a/src/database.c +++ b/src/database.c @@ -27,6 +27,10 @@ #include "apk_state.h" #include "apk_applet.h" #include "apk_archive.h" +#include "apk_print.h" + +int apk_verbosity = 1; +unsigned int apk_flags = 0; const char * const apkindex_tar_gz = "APKINDEX.tar.gz"; const char * const apk_index_gz = "APK_INDEX.gz"; @@ -1021,7 +1025,8 @@ static void handle_alarm(int sig) { } -int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts) +int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts, + int apk_wait) { const char *msg = NULL; struct apk_repository_list *repo = NULL; diff --git a/src/del.c b/src/del.c index 7ac7625..f4437b9 100644 --- a/src/del.c +++ b/src/del.c @@ -13,6 +13,7 @@ #include "apk_applet.h" #include "apk_state.h" #include "apk_database.h" +#include "apk_print.h" static int del_parse(void *ctx, struct apk_db_options *db, int optch, int optindex, const char *optarg) diff --git a/src/fetch.c b/src/fetch.c index dc9bff2..b7da2f5 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -20,6 +20,7 @@ #include "apk_database.h" #include "apk_state.h" #include "apk_io.h" +#include "apk_print.h" #define FETCH_RECURSIVE 1 #define FETCH_STDOUT 2 diff --git a/src/fix.c b/src/fix.c index 8b9021a..1bacce2 100644 --- a/src/fix.c +++ b/src/fix.c @@ -14,6 +14,7 @@ #include "apk_applet.h" #include "apk_database.h" #include "apk_state.h" +#include "apk_print.h" struct fix_ctx { unsigned int reinstall : 1; diff --git a/src/index.c b/src/index.c index 2676269..e7ed0c3 100644 --- a/src/index.c +++ b/src/index.c @@ -15,6 +15,7 @@ #include "apk_applet.h" #include "apk_database.h" +#include "apk_print.h" #define INDEX_OLD_FORMAT 0x10000 diff --git a/src/info.c b/src/info.c index dfb3712..19a3f48 100644 --- a/src/info.c +++ b/src/info.c @@ -16,6 +16,7 @@ #include "apk_package.h" #include "apk_database.h" #include "apk_state.h" +#include "apk_print.h" struct info_ctx { int (*action)(struct info_ctx *ctx, struct apk_database *db, diff --git a/src/package.c b/src/package.c index f981f45..10f0d1b 100644 --- a/src/package.c +++ b/src/package.c @@ -27,6 +27,7 @@ #include "apk_package.h" #include "apk_database.h" #include "apk_state.h" +#include "apk_print.h" void apk_pkg_format_plain(struct apk_package *pkg, apk_blob_t to) { diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..4b3d246 --- /dev/null +++ b/src/print.c @@ -0,0 +1,70 @@ +/* state.c - Alpine Package Keeper (APK) + * + * Copyright (C) 2005-2008 Natanael Copa + * Copyright (C) 2008 Timo Teräs + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. See http://www.gnu.org/ for details. + */ + +#include +#include +#include +#include +#include + +#include "apk_defines.h" +#include "apk_print.h" + +int apk_print_indented(struct apk_indent *i, apk_blob_t blob) +{ + static const int wrap_length = 80; + + if (i->x + blob.len + 1 >= wrap_length) { + i->x = i->indent; + printf("\n%*s", i->indent - 1, ""); + } + i->x += printf(" %.*s", blob.len, blob.ptr); + return 0; +} + +void apk_print_indented_words(struct apk_indent *i, const char *text) +{ + apk_blob_for_each_segment(APK_BLOB_STR(text), " ", + (apk_blob_cb) apk_print_indented, i); +} + +const char *apk_error_str(int error) +{ + if (error < 0) + error = -error; + switch (error) { + case ENOKEY: + return "UNTRUSTED signature"; + case EKEYREJECTED: + return "BAD signature"; + case EIO: + return "IO ERROR"; + case EBADMSG: + return "BAD archive"; + case ENOMSG: + return "archive does not contain expected data"; + default: + return strerror(error); + } +} + +void apk_log(const char *prefix, const char *format, ...) +{ + va_list va; + + if (prefix != NULL) + fprintf(stderr, "%s", prefix); + va_start(va, format); + vfprintf(stderr, format, va); + va_end(va); + fprintf(stderr, "\n"); +} + diff --git a/src/state.c b/src/state.c index 178e4c9..7c758cc 100644 --- a/src/state.c +++ b/src/state.c @@ -16,6 +16,7 @@ #include "apk_state.h" #include "apk_database.h" +#include "apk_print.h" struct apk_name_choices { unsigned short refs, num; diff --git a/src/update.c b/src/update.c index b558342..945258d 100644 --- a/src/update.c +++ b/src/update.c @@ -14,6 +14,7 @@ #include "apk_applet.h" #include "apk_database.h" #include "apk_version.h" +#include "apk_print.h" static int update_main(void *ctx, struct apk_database *db, int argc, char **argv) { diff --git a/src/upgrade.c b/src/upgrade.c index 611f677..2d7fd39 100644 --- a/src/upgrade.c +++ b/src/upgrade.c @@ -15,6 +15,7 @@ #include "apk_applet.h" #include "apk_database.h" #include "apk_state.h" +#include "apk_print.h" static int upgrade_parse(void *ctx, struct apk_db_options *dbopts, int optch, int optindex, const char *optarg) diff --git a/src/ver.c b/src/ver.c index a617bdb..36c389d 100644 --- a/src/ver.c +++ b/src/ver.c @@ -14,6 +14,7 @@ #include "apk_applet.h" #include "apk_database.h" #include "apk_version.h" +#include "apk_print.h" struct ver_ctx { int (*action)(struct apk_database *db, int argc, char **argv); diff --git a/src/verify.c b/src/verify.c index e9505ca..006b548 100644 --- a/src/verify.c +++ b/src/verify.c @@ -15,6 +15,7 @@ #include "apk_applet.h" #include "apk_database.h" +#include "apk_print.h" static int verify_main(void *ctx, struct apk_database *db, int argc, char **argv) { -- cgit v1.2.3