First steps for libapk

cute-signatures
Natanael Copa 2010-03-05 08:13:25 +00:00
parent 60b537e356
commit 3ec82a5a42
25 changed files with 165 additions and 77 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ apk
*.o *.o
*.d *.d
*.cmd *.cmd
*.so

View File

@ -70,7 +70,7 @@ INSTALL := install
INSTALLDIR := $(INSTALL) -d INSTALLDIR := $(INSTALL) -d
CFLAGS ?= -g -O2 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) CFLAGS_ALL += $(CFLAGS)
LDFLAGS ?= -g LDFLAGS ?= -g
@ -196,6 +196,24 @@ $(obj)/%.o: override local-target-prereqs=%
$(obj)/%.o: $(src)/%.c FORCE $(obj)/%.o: $(src)/%.c FORCE
$(call if_changed_rule,cc_o_c) $(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 # Link programs
@ -256,7 +274,7 @@ ifeq ($(toplevelrun),yes)
\( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \ \( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \
-type f -print | xargs rm -f -type f -print | xargs rm -f
endif 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) ifeq ($(origin VERSION),command line)
DIST_VERSION=$(VERSION) DIST_VERSION=$(VERSION)

View File

@ -9,11 +9,12 @@ VERSION := 2.0.3
DESTDIR := DESTDIR :=
SBINDIR := /sbin SBINDIR := /sbin
LIBDIR := /lib
CONFDIR := /etc/apk CONFDIR := /etc/apk
MANDIR := /usr/share/man MANDIR := /usr/share/man
DOCDIR := /usr/share/doc/apk DOCDIR := /usr/share/doc/apk
export DESTDIR SBINDIR CONFDIR MANDIR DOCDIR export DESTDIR SBINDIR LIBDIR CONFDIR MANDIR DOCDIR
## ##
# Top-level rules and targets # Top-level rules and targets

View File

@ -4,24 +4,34 @@ ifeq ($(shell pkg-config --print-errors --exists $(PKGDEPS) || echo fail),fail)
$(error Build dependencies are not met) $(error Build dependencies are not met)
endif 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 progs-y += apk
apk-objs := common.o state.o database.o package.o archive.o \ apk-objs := apk.o add.o del.o fix.o update.o info.o \
version.o io.o url.o gunzip.o blob.o hash.o apk.o \ search.o upgrade.o cache.o ver.o index.o fetch.o \
add.o del.o fix.o update.o info.o search.o upgrade.o \ audit.o verify.o
cache.o ver.o index.o fetch.o audit.o verify.o
CFLAGS += -D_ATFILE_SOURCE CFLAGS += -D_ATFILE_SOURCE
CFLAGS_apk.o := -DAPK_VERSION=\"$(FULL_VERSION)\" CFLAGS_apk.o := -DAPK_VERSION=\"$(FULL_VERSION)\"
LIBS_apk := -lapk
progs-$(STATIC) += apk.static progs-$(STATIC) += apk.static
apk.static-objs := $(apk-objs) apk.static-objs := $(apk-objs)
LDFLAGS_apk.static := -static LDFLAGS_apk.static := -static
LDFLAGS_apk += -nopie LDFLAGS_apk += -nopie -L$(obj)
CFLAGS += $(shell pkg-config --cflags $(PKGDEPS)) CFLAGS += $(shell pkg-config --cflags $(PKGDEPS))
LIBS := -Wl,--as-needed \ LIBS := -Wl,--as-needed \
$(shell pkg-config --libs $(PKGDEPS)) \ $(shell pkg-config --libs $(PKGDEPS)) \
-Wl,--no-as-needed -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)/apk $(DESTDIR)$(SBINDIR)
$(INSTALL) $(obj)/libapk.so $(DESTDIR)$(LIBDIR)

View File

@ -14,6 +14,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_print.h"
struct add_ctx { struct add_ctx {
const char *virtpkg; const char *virtpkg;

View File

@ -24,9 +24,7 @@
#include "apk_database.h" #include "apk_database.h"
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_blob.h" #include "apk_blob.h"
#include "apk_print.h"
int apk_verbosity = 1, apk_wait;
unsigned int apk_flags = 0;
static struct apk_option generic_options[] = { static struct apk_option generic_options[] = {
{ 'h', "help", "Show generic help or applet specific help" }, { '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" }, { 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) static int version(void)
{ {
printf("apk-tools " APK_VERSION "\n"); printf("apk-tools " APK_VERSION "\n");
return 0; 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, static int format_option(char *buf, size_t len, struct apk_option *o,
const char *separator) const char *separator)
{ {
@ -289,7 +235,7 @@ int main(int argc, char **argv)
struct apk_applet *applet; struct apk_applet *applet;
char short_options[256], *sopt; char short_options[256], *sopt;
struct option *opt, *all_options; struct option *opt, *all_options;
int r, optindex, num_options; int r, optindex, num_options, apk_wait = 0;
void *ctx = NULL; void *ctx = NULL;
struct apk_repository_list *repo = NULL; struct apk_repository_list *repo = NULL;
struct apk_database db; struct apk_database db;
@ -410,7 +356,7 @@ int main(int argc, char **argv)
argv++; argv++;
} }
r = apk_db_open(&db, &dbopts); r = apk_db_open(&db, &dbopts, apk_wait);
if (r != 0) { if (r != 0) {
apk_error("Failed to open apk database: %s", apk_error("Failed to open apk database: %s",
apk_error_str(r)); apk_error_str(r));

View File

@ -155,7 +155,7 @@ struct apk_db_file *apk_db_file_query(struct apk_database *db,
APK_OPENF_NO_SCRIPTS | \ APK_OPENF_NO_SCRIPTS | \
APK_OPENF_NO_WORLD) 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); void apk_db_close(struct apk_database *db);
int apk_db_write_config(struct apk_database *db); int apk_db_write_config(struct apk_database *db);
int apk_db_run_triggers(struct apk_database *db); int apk_db_run_triggers(struct apk_database *db);

View File

@ -47,7 +47,7 @@
(type *)( (char *)__mptr - offsetof(type,member) );}) (type *)( (char *)__mptr - offsetof(type,member) );})
#endif #endif
extern int apk_verbosity, apk_wait; extern int apk_verbosity;
extern unsigned int apk_flags; extern unsigned int apk_flags;
#define APK_FORCE 0x0001 #define APK_FORCE 0x0001
@ -65,13 +65,6 @@ extern unsigned int apk_flags;
#define APK_NO_NETWORK 0x1000 #define APK_NO_NETWORK 0x1000
#define APK_OVERLAY_FROM_STDIN 0x2000 #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) static inline size_t apk_calc_installed_size(size_t size)
{ {
const size_t bsize = 4 * 1024; const size_t bsize = 4 * 1024;

27
src/apk_print.h Normal file
View File

@ -0,0 +1,27 @@
/* apk_state.h - Alpine Package Keeper (APK)
*
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
* Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
* 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

View File

@ -43,4 +43,7 @@ int apk_state_lock_name(struct apk_state *state,
struct apk_name *name, struct apk_name *name,
struct apk_package *newpkg); 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 #endif

View File

@ -24,6 +24,7 @@
#include <stdint.h> #include <stdint.h>
#include "apk_defines.h" #include "apk_defines.h"
#include "apk_print.h"
#include "apk_archive.h" #include "apk_archive.h"
struct tar_header { struct tar_header {

View File

@ -19,6 +19,7 @@
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_package.h" #include "apk_package.h"
#include "apk_print.h"
#define CACHE_CLEAN BIT(0) #define CACHE_CLEAN BIT(0)
#define CACHE_DOWNLOAD BIT(1) #define CACHE_DOWNLOAD BIT(1)

View File

@ -27,6 +27,10 @@
#include "apk_state.h" #include "apk_state.h"
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_archive.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 apkindex_tar_gz = "APKINDEX.tar.gz";
const char * const apk_index_gz = "APK_INDEX.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; const char *msg = NULL;
struct apk_repository_list *repo = NULL; struct apk_repository_list *repo = NULL;

View File

@ -13,6 +13,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_print.h"
static int del_parse(void *ctx, struct apk_db_options *db, static int del_parse(void *ctx, struct apk_db_options *db,
int optch, int optindex, const char *optarg) int optch, int optindex, const char *optarg)

View File

@ -20,6 +20,7 @@
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_io.h" #include "apk_io.h"
#include "apk_print.h"
#define FETCH_RECURSIVE 1 #define FETCH_RECURSIVE 1
#define FETCH_STDOUT 2 #define FETCH_STDOUT 2

View File

@ -14,6 +14,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_print.h"
struct fix_ctx { struct fix_ctx {
unsigned int reinstall : 1; unsigned int reinstall : 1;

View File

@ -15,6 +15,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_print.h"
#define INDEX_OLD_FORMAT 0x10000 #define INDEX_OLD_FORMAT 0x10000

View File

@ -16,6 +16,7 @@
#include "apk_package.h" #include "apk_package.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_print.h"
struct info_ctx { struct info_ctx {
int (*action)(struct info_ctx *ctx, struct apk_database *db, int (*action)(struct info_ctx *ctx, struct apk_database *db,

View File

@ -27,6 +27,7 @@
#include "apk_package.h" #include "apk_package.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_print.h"
void apk_pkg_format_plain(struct apk_package *pkg, apk_blob_t to) void apk_pkg_format_plain(struct apk_package *pkg, apk_blob_t to)
{ {

70
src/print.c Normal file
View File

@ -0,0 +1,70 @@
/* state.c - Alpine Package Keeper (APK)
*
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
* Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#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");
}

View File

@ -16,6 +16,7 @@
#include "apk_state.h" #include "apk_state.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_print.h"
struct apk_name_choices { struct apk_name_choices {
unsigned short refs, num; unsigned short refs, num;

View File

@ -14,6 +14,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_version.h" #include "apk_version.h"
#include "apk_print.h"
static int update_main(void *ctx, struct apk_database *db, int argc, char **argv) static int update_main(void *ctx, struct apk_database *db, int argc, char **argv)
{ {

View File

@ -15,6 +15,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_state.h" #include "apk_state.h"
#include "apk_print.h"
static int upgrade_parse(void *ctx, struct apk_db_options *dbopts, static int upgrade_parse(void *ctx, struct apk_db_options *dbopts,
int optch, int optindex, const char *optarg) int optch, int optindex, const char *optarg)

View File

@ -14,6 +14,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_version.h" #include "apk_version.h"
#include "apk_print.h"
struct ver_ctx { struct ver_ctx {
int (*action)(struct apk_database *db, int argc, char **argv); int (*action)(struct apk_database *db, int argc, char **argv);

View File

@ -15,6 +15,7 @@
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_database.h" #include "apk_database.h"
#include "apk_print.h"
static int verify_main(void *ctx, struct apk_database *db, int argc, char **argv) static int verify_main(void *ctx, struct apk_database *db, int argc, char **argv)
{ {