2008-04-17 14:09:13 +00:00
|
|
|
/* apk.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2008-04-17 14:09:13 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-06-25 12:14:07 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
2008-04-17 14:09:13 +00:00
|
|
|
* 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>
|
2008-04-22 08:16:26 +00:00
|
|
|
#include <fcntl.h>
|
2009-01-13 12:09:45 +00:00
|
|
|
#include <ctype.h>
|
2010-06-30 13:53:56 +00:00
|
|
|
#include <errno.h>
|
2011-07-22 09:08:35 +00:00
|
|
|
#include <signal.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
#include <stdarg.h>
|
2008-04-21 16:30:10 +00:00
|
|
|
#include <stdlib.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
#include <string.h>
|
2008-04-21 16:30:10 +00:00
|
|
|
#include <getopt.h>
|
2011-03-16 13:22:05 +00:00
|
|
|
#include <unistd.h>
|
2008-04-21 16:30:10 +00:00
|
|
|
#include <sys/stat.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2010-06-30 13:53:56 +00:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2009-07-08 13:19:06 +00:00
|
|
|
#include <openssl/engine.h>
|
2010-06-30 13:53:56 +00:00
|
|
|
#endif
|
2009-07-08 13:19:06 +00:00
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
#include <fetch.h>
|
|
|
|
|
2008-04-17 14:09:13 +00:00
|
|
|
#include "apk_defines.h"
|
2009-08-06 11:25:03 +00:00
|
|
|
#include "apk_database.h"
|
2008-04-17 14:09:13 +00:00
|
|
|
#include "apk_applet.h"
|
2009-06-25 12:14:07 +00:00
|
|
|
#include "apk_blob.h"
|
2010-03-05 08:13:25 +00:00
|
|
|
#include "apk_print.h"
|
2012-02-23 20:02:11 +00:00
|
|
|
#include "apk_io.h"
|
2008-04-21 16:30:10 +00:00
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static const struct apk_option_group *default_optgroups[] = { &optgroup_global, NULL };
|
2014-05-12 16:42:32 +00:00
|
|
|
static struct list_head apk_applet_list;
|
|
|
|
#define foreach_applet(iter) list_for_each_entry(iter, &apk_applet_list, node)
|
|
|
|
|
2014-12-08 06:30:35 +00:00
|
|
|
#ifdef TEST_MODE
|
|
|
|
static const char *test_installed_db = NULL;
|
|
|
|
static const char *test_world = NULL;
|
|
|
|
static struct apk_string_array *test_repos;
|
|
|
|
#endif
|
|
|
|
|
2011-01-01 13:48:10 +00:00
|
|
|
char **apk_argv;
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static void version(void)
|
|
|
|
{
|
|
|
|
printf("apk-tools " APK_VERSION ", compiled for " APK_DEFAULT_ARCH ".\n"
|
|
|
|
#ifdef TEST_MODE
|
|
|
|
"TEST MODE BUILD. NOT FOR PRODUCTION USE.\n"
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_repository_list *apk_repository_new(const char *url)
|
|
|
|
{
|
|
|
|
struct apk_repository_list *r = calloc(1, sizeof(struct apk_repository_list));
|
|
|
|
if (r) {
|
|
|
|
r->url = url;
|
|
|
|
list_init(&r->list);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int option_parse_global(void *ctx, struct apk_db_options *dbopts, int optch, const char *optarg)
|
|
|
|
{
|
|
|
|
struct apk_repository_list *repo;
|
|
|
|
|
|
|
|
switch (optch) {
|
|
|
|
case 'h': return -EINVAL;
|
|
|
|
case 'p':
|
|
|
|
dbopts->root = optarg;
|
|
|
|
break;
|
|
|
|
case 0x107:
|
|
|
|
dbopts->keys_dir = optarg;
|
|
|
|
break;
|
|
|
|
case 0x108:
|
|
|
|
dbopts->repositories_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
repo = apk_repository_new(optarg);
|
|
|
|
if (repo) list_add(&repo->list, &dbopts->repository_list);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
apk_verbosity--;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
apk_verbosity++;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
return -ESHUTDOWN;
|
|
|
|
case 'f':
|
|
|
|
apk_flags |= APK_FORCE;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
apk_flags |= APK_INTERACTIVE;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
apk_flags |= APK_UPDATE_CACHE;
|
|
|
|
break;
|
|
|
|
case 0x101:
|
|
|
|
apk_flags |= APK_PROGRESS;
|
|
|
|
break;
|
2016-02-16 13:59:55 +00:00
|
|
|
case 0x104:
|
|
|
|
apk_flags |= APK_SIMULATE;
|
|
|
|
break;
|
2014-10-08 12:29:27 +00:00
|
|
|
case 0x110:
|
|
|
|
apk_flags &= ~APK_PROGRESS;
|
|
|
|
break;
|
|
|
|
case 0x10f:
|
|
|
|
apk_progress_fd = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 0x103:
|
|
|
|
apk_flags |= APK_ALLOW_UNTRUSTED;
|
|
|
|
break;
|
|
|
|
case 0x106:
|
|
|
|
apk_flags |= APK_PURGE;
|
|
|
|
break;
|
|
|
|
case 0x105:
|
|
|
|
dbopts->lock_wait = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 0x109:
|
|
|
|
apk_flags |= APK_NO_NETWORK;
|
|
|
|
break;
|
2015-12-07 10:41:13 +00:00
|
|
|
case 0x115:
|
|
|
|
apk_flags |= APK_NO_CACHE;
|
|
|
|
break;
|
2017-02-27 09:12:42 +00:00
|
|
|
case 0x116:
|
|
|
|
dbopts->cache_dir = optarg;
|
|
|
|
break;
|
2014-10-08 12:29:27 +00:00
|
|
|
case 0x112:
|
|
|
|
dbopts->arch = optarg;
|
|
|
|
break;
|
|
|
|
case 0x114:
|
|
|
|
puts(APK_DEFAULT_ARCH);
|
|
|
|
return -ESHUTDOWN;
|
|
|
|
#ifdef TEST_MODE
|
|
|
|
case 0x200:
|
|
|
|
*apk_string_array_add(&test_repos) = (char*) optarg;
|
|
|
|
break;
|
|
|
|
case 0x201:
|
|
|
|
test_installed_db = optarg;
|
|
|
|
break;
|
|
|
|
case 0x202:
|
|
|
|
test_world = optarg;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct apk_option options_global[] = {
|
2009-06-25 12:14:07 +00:00
|
|
|
{ 'h', "help", "Show generic help or applet specific help" },
|
|
|
|
{ 'p', "root", "Install packages to DIR",
|
2009-07-07 07:30:54 +00:00
|
|
|
required_argument, "DIR" },
|
2009-06-25 12:14:07 +00:00
|
|
|
{ 'X', "repository", "Use packages from REPO",
|
2009-07-07 07:30:54 +00:00
|
|
|
required_argument, "REPO" },
|
2009-06-25 12:14:07 +00:00
|
|
|
{ 'q', "quiet", "Print less information" },
|
2013-06-27 19:28:46 +00:00
|
|
|
{ 'v', "verbose", "Print more information (can be doubled)" },
|
2009-07-24 11:18:07 +00:00
|
|
|
{ 'i', "interactive", "Ask confirmation for certain operations" },
|
2009-06-25 12:14:07 +00:00
|
|
|
{ 'V', "version", "Print program version and exit" },
|
|
|
|
{ 'f', "force", "Do what was asked even if it looks dangerous" },
|
2009-07-07 09:12:24 +00:00
|
|
|
{ 'U', "update-cache", "Update the repository cache" },
|
2009-06-25 12:14:07 +00:00
|
|
|
{ 0x101, "progress", "Show a progress bar" },
|
2012-09-20 12:12:15 +00:00
|
|
|
{ 0x10f, "progress-fd", "Write progress to fd", required_argument, "FD" },
|
2011-03-16 13:22:05 +00:00
|
|
|
{ 0x110, "no-progress", "Disable progress bar even for TTYs" },
|
2014-10-06 12:52:51 +00:00
|
|
|
{ 0x106, "purge", "Delete also modified configuration files (pkg removal) "
|
|
|
|
"and uninstalled packages from cache (cache clean)" },
|
2014-10-08 12:29:27 +00:00
|
|
|
{ 0x103, "allow-untrusted", "Install packages with untrusted signature or no signature" },
|
2009-07-07 07:30:54 +00:00
|
|
|
{ 0x105, "wait", "Wait for TIME seconds to get an exclusive "
|
|
|
|
"repository lock before failing",
|
|
|
|
required_argument, "TIME" },
|
2009-08-06 11:25:03 +00:00
|
|
|
{ 0x107, "keys-dir", "Override directory of trusted keys",
|
|
|
|
required_argument, "KEYSDIR" },
|
|
|
|
{ 0x108, "repositories-file", "Override repositories file",
|
2009-08-06 13:00:20 +00:00
|
|
|
required_argument, "REPOFILE" },
|
|
|
|
{ 0x109, "no-network", "Do not use network (cache is still used)" },
|
2015-12-07 10:41:13 +00:00
|
|
|
{ 0x115, "no-cache", "Read uncached index from network" },
|
2017-02-27 09:12:42 +00:00
|
|
|
{ 0x116, "cache-dir", "Override cache directory",
|
|
|
|
required_argument, "CACHEDIR" },
|
2013-08-28 14:22:48 +00:00
|
|
|
{ 0x112, "arch", "Use architecture with --root",
|
2011-04-14 19:48:49 +00:00
|
|
|
required_argument, "ARCH" },
|
2014-01-06 09:51:17 +00:00
|
|
|
{ 0x114, "print-arch", "Print default arch and exit" },
|
2012-02-23 20:02:11 +00:00
|
|
|
#ifdef TEST_MODE
|
|
|
|
{ 0x200, "test-repo", "Repository", required_argument, "REPO" },
|
|
|
|
{ 0x201, "test-instdb", "Installed db", required_argument, "INSTALLED" },
|
|
|
|
{ 0x202, "test-world", "World", required_argument, "WORLD DEPS" },
|
|
|
|
#endif
|
2009-06-25 12:14:07 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
const struct apk_option_group optgroup_global = {
|
|
|
|
.name = "Global",
|
|
|
|
.options = options_global,
|
|
|
|
.num_options = ARRAY_SIZE(options_global),
|
|
|
|
.parse = option_parse_global,
|
|
|
|
};
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static int option_parse_commit(void *ctx, struct apk_db_options *dbopts, int optch, const char *optarg)
|
2014-01-06 09:51:17 +00:00
|
|
|
{
|
2014-10-08 12:29:27 +00:00
|
|
|
switch (optch) {
|
2014-11-01 18:28:25 +00:00
|
|
|
case 's':
|
|
|
|
apk_flags |= APK_SIMULATE;
|
|
|
|
break;
|
2014-10-08 12:29:27 +00:00
|
|
|
case 0x102:
|
|
|
|
apk_flags |= APK_CLEAN_PROTECTED;
|
|
|
|
break;
|
|
|
|
case 0x111:
|
|
|
|
apk_flags |= APK_OVERLAY_FROM_STDIN;
|
|
|
|
break;
|
|
|
|
case 0x113:
|
|
|
|
apk_flags |= APK_NO_SCRIPTS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2014-01-06 09:51:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static const struct apk_option options_commit[] = {
|
2014-11-01 18:28:25 +00:00
|
|
|
{ 's', "simulate", "Show what would be done without actually doing it" },
|
2014-10-08 12:29:27 +00:00
|
|
|
{ 0x102, "clean-protected", "Do not create .apk-new files in configuration dirs" },
|
|
|
|
{ 0x111, "overlay-from-stdin", "Read list of overlay files from stdin" },
|
|
|
|
{ 0x113, "no-scripts", "Do not execute any scripts" },
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct apk_option_group optgroup_commit = {
|
|
|
|
.name = "Commit",
|
|
|
|
.options = options_commit,
|
|
|
|
.num_options = ARRAY_SIZE(options_commit),
|
|
|
|
.parse = option_parse_commit,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int format_option(char *buf, size_t len, const struct apk_option *o,
|
2009-06-25 12:14:07 +00:00
|
|
|
const char *separator)
|
2009-06-19 13:40:37 +00:00
|
|
|
{
|
2009-06-25 12:14:07 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (o->val <= 0xff && isalnum(o->val)) {
|
|
|
|
i += snprintf(&buf[i], len - i, "-%c", o->val);
|
|
|
|
if (o->name != NULL)
|
|
|
|
i += snprintf(&buf[i], len - i, "%s", separator);
|
|
|
|
}
|
|
|
|
if (o->name != NULL)
|
|
|
|
i += snprintf(&buf[i], len - i, "--%s", o->name);
|
|
|
|
if (o->arg_name != NULL)
|
|
|
|
i += snprintf(&buf[i], len - i, " %s", o->arg_name);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static void print_usage(const char *cmd, const char *args, const struct apk_option_group **optgroups)
|
2009-06-25 12:14:07 +00:00
|
|
|
{
|
2011-09-09 17:06:10 +00:00
|
|
|
struct apk_indent indent = { .indent = 11 };
|
2014-10-08 12:29:27 +00:00
|
|
|
const struct apk_option *opts;
|
2009-06-25 12:14:07 +00:00
|
|
|
char word[128];
|
2014-10-08 12:29:27 +00:00
|
|
|
int g, i, j;
|
2009-06-25 12:14:07 +00:00
|
|
|
|
|
|
|
indent.x = printf("\nusage: apk %s", cmd) - 1;
|
2014-10-08 12:29:27 +00:00
|
|
|
for (g = 0; optgroups[g]; g++) {
|
|
|
|
opts = optgroups[g]->options;
|
|
|
|
for (i = 0; i < optgroups[g]->num_options; i++) {
|
|
|
|
if (!opts[i].name) continue;
|
2013-06-30 08:02:19 +00:00
|
|
|
j = 0;
|
|
|
|
word[j++] = '[';
|
|
|
|
j += format_option(&word[j], sizeof(word) - j, &opts[i], "|");
|
|
|
|
word[j++] = ']';
|
|
|
|
apk_print_indented(&indent, APK_BLOB_PTR_LEN(word, j));
|
|
|
|
}
|
2014-10-08 12:29:27 +00:00
|
|
|
}
|
2009-06-25 12:14:07 +00:00
|
|
|
if (args != NULL)
|
2009-08-06 12:34:33 +00:00
|
|
|
apk_print_indented(&indent, APK_BLOB_STR(args));
|
2009-06-25 12:14:07 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static void print_options(int num_opts, const struct apk_option *opts)
|
2009-06-25 12:14:07 +00:00
|
|
|
{
|
2011-09-09 17:06:10 +00:00
|
|
|
struct apk_indent indent = { .indent = 26 };
|
2009-06-25 12:14:07 +00:00
|
|
|
char word[128];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_opts; i++) {
|
|
|
|
format_option(word, sizeof(word), &opts[i], ", ");
|
|
|
|
indent.x = printf(" %-*s", indent.indent - 3, word);
|
2009-08-06 12:34:33 +00:00
|
|
|
apk_print_indented_words(&indent, opts[i].help);
|
2009-06-25 12:14:07 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usage(struct apk_applet *applet)
|
|
|
|
{
|
2014-10-08 12:29:27 +00:00
|
|
|
const struct apk_option_group **optgroups = default_optgroups;
|
|
|
|
int i;
|
|
|
|
|
2009-06-19 13:40:37 +00:00
|
|
|
version();
|
2009-06-25 12:14:07 +00:00
|
|
|
if (applet == NULL) {
|
2014-05-12 16:42:32 +00:00
|
|
|
struct apk_applet *a;
|
2009-06-25 12:14:07 +00:00
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
print_usage("COMMAND", "[ARGS]...", default_optgroups);
|
2009-06-25 12:14:07 +00:00
|
|
|
|
2011-07-16 09:14:58 +00:00
|
|
|
printf("\nThe following commands are available:\n");
|
2014-05-12 16:42:32 +00:00
|
|
|
foreach_applet(a) {
|
2013-07-08 12:12:35 +00:00
|
|
|
struct apk_indent indent = { .indent = 12 };
|
2014-05-12 16:42:32 +00:00
|
|
|
indent.x = printf(" %-*s", indent.indent - 3, a->name);
|
|
|
|
apk_print_indented_words(&indent, a->help);
|
2011-07-16 09:14:58 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2009-06-25 12:14:07 +00:00
|
|
|
} else {
|
2011-09-09 17:06:10 +00:00
|
|
|
struct apk_indent indent = { .indent = 2 };
|
2011-07-22 09:08:35 +00:00
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
if (applet->optgroups[0]) optgroups = applet->optgroups;
|
|
|
|
print_usage(applet->name, applet->arguments, applet->optgroups);
|
2011-09-09 17:06:10 +00:00
|
|
|
printf("\nDescription:\n");
|
2009-08-06 12:34:33 +00:00
|
|
|
apk_print_indented_words(&indent, applet->help);
|
2011-07-22 09:08:35 +00:00
|
|
|
printf("\n");
|
2009-06-25 12:14:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
for (i = 0; optgroups[i]; i++) {
|
|
|
|
printf("\n%s options:\n", optgroups[i]->name);
|
|
|
|
print_options(optgroups[i]->num_options, optgroups[i]->options);
|
2009-06-25 12:14:07 +00:00
|
|
|
}
|
2014-10-08 12:29:27 +00:00
|
|
|
|
2011-07-16 09:14:58 +00:00
|
|
|
printf("\nThis apk has coffee making abilities.\n");
|
2009-06-25 12:14:07 +00:00
|
|
|
|
2008-04-17 14:09:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-04-21 16:30:10 +00:00
|
|
|
static struct apk_applet *find_applet(const char *name)
|
|
|
|
{
|
2014-05-12 16:42:32 +00:00
|
|
|
struct apk_applet *a;
|
2008-04-21 16:30:10 +00:00
|
|
|
|
2014-05-12 16:42:32 +00:00
|
|
|
foreach_applet(a) {
|
|
|
|
if (strcmp(name, a->name) == 0)
|
|
|
|
return a;
|
2008-04-21 16:30:10 +00:00
|
|
|
}
|
2009-06-25 12:14:07 +00:00
|
|
|
|
2008-04-21 16:30:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-13 12:09:45 +00:00
|
|
|
static struct apk_applet *deduce_applet(int argc, char **argv)
|
2008-04-17 14:09:13 +00:00
|
|
|
{
|
2009-01-13 12:09:45 +00:00
|
|
|
struct apk_applet *a;
|
2008-04-21 16:30:10 +00:00
|
|
|
const char *prog;
|
2009-01-13 12:09:45 +00:00
|
|
|
int i;
|
2008-04-17 14:09:13 +00:00
|
|
|
|
|
|
|
prog = strrchr(argv[0], '/');
|
|
|
|
if (prog == NULL)
|
|
|
|
prog = argv[0];
|
|
|
|
else
|
|
|
|
prog++;
|
|
|
|
|
2008-04-21 16:30:10 +00:00
|
|
|
if (strncmp(prog, "apk_", 4) == 0)
|
2009-01-13 12:09:45 +00:00
|
|
|
return find_applet(prog + 4);
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '-')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
a = find_applet(argv[i]);
|
|
|
|
if (a != NULL)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static void merge_options(struct option *opts, const struct apk_option *ao, int num)
|
2009-06-25 12:14:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++, opts++, ao++) {
|
2013-06-30 08:02:19 +00:00
|
|
|
opts->name = ao->name ?: "";
|
2009-06-25 12:14:07 +00:00
|
|
|
opts->has_arg = ao->has_arg;
|
|
|
|
opts->flag = NULL;
|
|
|
|
opts->val = ao->val;
|
|
|
|
}
|
|
|
|
opts->name = NULL;
|
|
|
|
}
|
2009-01-13 12:09:45 +00:00
|
|
|
|
2009-07-08 13:19:06 +00:00
|
|
|
static void fini_openssl(void)
|
|
|
|
{
|
|
|
|
EVP_cleanup();
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
ENGINE_cleanup();
|
|
|
|
#endif
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_openssl(void)
|
|
|
|
{
|
|
|
|
atexit(fini_openssl);
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
ENGINE_load_builtin_engines();
|
2009-07-13 11:28:52 +00:00
|
|
|
ENGINE_register_all_complete();
|
2009-07-08 13:19:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-22 09:08:35 +00:00
|
|
|
static void on_sigwinch(int s)
|
2011-03-19 13:20:47 +00:00
|
|
|
{
|
2011-07-22 09:08:35 +00:00
|
|
|
apk_reset_screen_width();
|
|
|
|
}
|
2011-03-19 13:20:47 +00:00
|
|
|
|
2011-07-22 09:08:35 +00:00
|
|
|
static void setup_terminal(void)
|
|
|
|
{
|
|
|
|
signal(SIGWINCH, on_sigwinch);
|
2012-09-20 12:12:15 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2011-03-19 13:20:47 +00:00
|
|
|
}
|
|
|
|
|
2012-02-24 06:47:19 +00:00
|
|
|
static void setup_automatic_flags(void)
|
|
|
|
{
|
|
|
|
if (!isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO) ||
|
|
|
|
!isatty(STDIN_FILENO))
|
|
|
|
return;
|
|
|
|
|
|
|
|
apk_flags |= APK_PROGRESS;
|
|
|
|
if (!(apk_flags & APK_SIMULATE) &&
|
|
|
|
access("/etc/apk/interactive", F_OK) == 0)
|
|
|
|
apk_flags |= APK_INTERACTIVE;
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:42:32 +00:00
|
|
|
void apk_applet_register(struct apk_applet *applet)
|
|
|
|
{
|
|
|
|
list_init(&applet->node);
|
|
|
|
list_add_tail(&applet->node, &apk_applet_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apk_applet_register_builtin(void)
|
|
|
|
{
|
|
|
|
extern apk_init_func_t __start_initapplets[], __stop_initapplets[];
|
|
|
|
apk_init_func_t *p;
|
|
|
|
|
|
|
|
list_init(&apk_applet_list);
|
|
|
|
for (p = __start_initapplets; p < __stop_initapplets; p++)
|
|
|
|
(*p)();
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:54:24 +00:00
|
|
|
static struct apk_database db;
|
|
|
|
|
|
|
|
static void on_sigint(int s)
|
|
|
|
{
|
|
|
|
apk_db_close(&db);
|
|
|
|
exit(128 + s);
|
|
|
|
}
|
|
|
|
|
2009-01-13 12:09:45 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct apk_applet *applet;
|
|
|
|
char short_options[256], *sopt;
|
2009-06-25 12:14:07 +00:00
|
|
|
struct option *opt, *all_options;
|
2014-10-08 12:29:27 +00:00
|
|
|
int i, p, r, num_options;
|
2009-01-13 12:09:45 +00:00
|
|
|
void *ctx = NULL;
|
2009-08-06 11:25:03 +00:00
|
|
|
struct apk_db_options dbopts;
|
2014-10-08 12:29:27 +00:00
|
|
|
const struct apk_option_group **optgroups = default_optgroups;
|
2013-06-18 10:01:51 +00:00
|
|
|
struct apk_string_array *args;
|
2012-02-23 20:02:11 +00:00
|
|
|
|
2014-12-08 06:30:35 +00:00
|
|
|
#ifdef TEST_MODE
|
2012-02-23 20:02:11 +00:00
|
|
|
apk_string_array_init(&test_repos);
|
|
|
|
#endif
|
2014-05-12 16:42:32 +00:00
|
|
|
apk_applet_register_builtin();
|
|
|
|
|
2011-09-14 08:07:45 +00:00
|
|
|
apk_argv = malloc(sizeof(char*[argc+2]));
|
2011-01-01 13:48:10 +00:00
|
|
|
memcpy(apk_argv, argv, sizeof(char*[argc]));
|
|
|
|
apk_argv[argc] = NULL;
|
2011-09-14 08:07:45 +00:00
|
|
|
apk_argv[argc+1] = NULL;
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
memset(&dbopts, 0, sizeof(dbopts));
|
|
|
|
list_init(&dbopts.repository_list);
|
2010-12-14 17:51:16 +00:00
|
|
|
apk_atom_init();
|
2009-01-13 12:09:45 +00:00
|
|
|
umask(0);
|
2011-03-19 13:20:47 +00:00
|
|
|
setup_terminal();
|
2011-03-16 13:22:05 +00:00
|
|
|
|
2009-01-13 12:09:45 +00:00
|
|
|
applet = deduce_applet(argc, argv);
|
2014-10-08 12:29:27 +00:00
|
|
|
if (applet && applet->optgroups[0]) optgroups = applet->optgroups;
|
|
|
|
|
|
|
|
for (i = 0, num_options = 0; optgroups[i]; i++)
|
|
|
|
num_options += optgroups[i]->num_options;
|
2009-06-25 12:14:07 +00:00
|
|
|
all_options = alloca(sizeof(struct option) * num_options);
|
2014-10-08 12:29:27 +00:00
|
|
|
for (i = r = 0; optgroups[i]; r += optgroups[i]->num_options, i++)
|
|
|
|
merge_options(&all_options[r], optgroups[i]->options, optgroups[i]->num_options);
|
2009-01-17 08:51:52 +00:00
|
|
|
if (applet != NULL) {
|
|
|
|
if (applet->context_size != 0)
|
|
|
|
ctx = calloc(1, applet->context_size);
|
2009-08-06 11:25:03 +00:00
|
|
|
dbopts.open_flags = applet->open_flags;
|
|
|
|
apk_flags |= applet->forced_flags;
|
2009-01-13 12:09:45 +00:00
|
|
|
}
|
2009-06-25 12:14:07 +00:00
|
|
|
for (opt = all_options, sopt = short_options; opt->name != NULL; opt++) {
|
2009-01-15 09:10:14 +00:00
|
|
|
if (opt->flag == NULL &&
|
|
|
|
opt->val <= 0xff && isalnum(opt->val)) {
|
2009-01-13 12:09:45 +00:00
|
|
|
*(sopt++) = opt->val;
|
|
|
|
if (opt->has_arg != no_argument)
|
|
|
|
*(sopt++) = ':';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 13:19:06 +00:00
|
|
|
init_openssl();
|
2013-06-19 09:25:26 +00:00
|
|
|
setup_automatic_flags();
|
2014-10-08 07:10:45 +00:00
|
|
|
fetchConnectionCacheInit(16, 1);
|
2009-07-08 13:19:06 +00:00
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
while ((p = getopt_long(argc, argv, short_options, all_options, NULL)) != -1) {
|
|
|
|
for (i = 0; optgroups[i]; i++) {
|
|
|
|
r = optgroups[i]->parse(ctx, &dbopts, p, optarg);
|
|
|
|
if (r == 0) break;
|
|
|
|
if (r != -ENOTSUP) goto err_and_usage;
|
2008-04-17 14:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 08:02:44 +00:00
|
|
|
if (applet == NULL) {
|
|
|
|
r = usage(NULL);
|
|
|
|
goto err;
|
|
|
|
}
|
2009-01-17 08:51:52 +00:00
|
|
|
|
2009-01-13 12:09:45 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc >= 1 && strcmp(argv[0], applet->name) == 0) {
|
2008-04-21 16:30:10 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:28:32 +00:00
|
|
|
apk_db_init(&db);
|
2017-04-17 12:54:24 +00:00
|
|
|
signal(SIGINT, on_sigint);
|
2017-04-28 10:28:32 +00:00
|
|
|
|
2012-02-23 20:02:11 +00:00
|
|
|
#ifdef TEST_MODE
|
|
|
|
dbopts.open_flags &= ~(APK_OPENF_WRITE | APK_OPENF_CACHE_WRITE | APK_OPENF_CREATE);
|
|
|
|
dbopts.open_flags |= APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS;
|
|
|
|
apk_flags |= APK_SIMULATE;
|
|
|
|
apk_flags &= ~APK_INTERACTIVE;
|
|
|
|
#endif
|
2010-03-06 19:22:01 +00:00
|
|
|
r = apk_db_open(&db, &dbopts);
|
2009-08-06 11:25:03 +00:00
|
|
|
if (r != 0) {
|
|
|
|
apk_error("Failed to open apk database: %s",
|
|
|
|
apk_error_str(r));
|
2012-02-17 08:02:44 +00:00
|
|
|
goto err;
|
2009-08-06 11:25:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 20:02:11 +00:00
|
|
|
#ifdef TEST_MODE
|
|
|
|
if (test_world != NULL) {
|
|
|
|
apk_blob_t b = APK_BLOB_STR(test_world);
|
|
|
|
apk_blob_pull_deps(&b, &db, &db.world);
|
|
|
|
}
|
|
|
|
if (test_installed_db != NULL) {
|
|
|
|
struct apk_bstream *bs = apk_bstream_from_file(AT_FDCWD, test_installed_db);
|
2015-03-10 11:04:14 +00:00
|
|
|
if (!IS_ERR_OR_NULL(bs)) {
|
2012-02-23 20:02:11 +00:00
|
|
|
apk_db_index_read(&db, bs, -1);
|
2017-06-21 13:07:58 +00:00
|
|
|
apk_bstream_close(bs, NULL);
|
2012-02-23 20:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < test_repos->num; i++) {
|
|
|
|
struct apk_bstream *bs;
|
2012-02-24 06:42:40 +00:00
|
|
|
apk_blob_t spec = APK_BLOB_STR(test_repos->item[i]), name, tag;
|
2013-05-30 05:46:30 +00:00
|
|
|
int repo_tag = 0, repo = APK_REPOSITORY_FIRST_CONFIGURED + i;
|
2012-02-24 06:42:40 +00:00
|
|
|
|
2014-05-19 08:48:36 +00:00
|
|
|
if (spec.ptr[0] == '!') {
|
|
|
|
/* cache's installed repository */
|
|
|
|
spec.ptr++;
|
|
|
|
spec.len--;
|
|
|
|
repo = -2;
|
|
|
|
}
|
|
|
|
|
2012-02-24 06:42:40 +00:00
|
|
|
if (apk_blob_split(spec, APK_BLOB_STR(":"), &tag, &name)) {
|
|
|
|
repo_tag = apk_db_get_tag_id(&db, tag);
|
|
|
|
} else {
|
|
|
|
name = spec;
|
2012-02-23 20:02:11 +00:00
|
|
|
}
|
2012-02-24 06:42:40 +00:00
|
|
|
|
|
|
|
bs = apk_bstream_from_file(AT_FDCWD, name.ptr);
|
2015-03-10 11:04:14 +00:00
|
|
|
if (IS_ERR_OR_NULL(bs)) {
|
2014-05-19 08:48:36 +00:00
|
|
|
apk_error("Failed to open repository: " BLOB_FMT, BLOB_PRINTF(name));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_db_index_read(&db, bs, repo);
|
2017-06-21 13:07:58 +00:00
|
|
|
apk_bstream_close(bs, NULL);
|
2014-05-19 08:48:36 +00:00
|
|
|
if (repo != -2) {
|
2013-05-30 05:46:30 +00:00
|
|
|
if (!(apk_flags & APK_NO_NETWORK))
|
|
|
|
db.available_repos |= BIT(repo);
|
|
|
|
db.repo_tags[repo_tag].allowed_repos |= BIT(repo);
|
2012-02-23 20:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
apk_string_array_init(&args);
|
|
|
|
apk_string_array_resize(&args, argc);
|
|
|
|
memcpy(args->item, argv, argc * sizeof(*argv));
|
|
|
|
|
|
|
|
r = applet->main(ctx, &db, args);
|
2009-08-06 11:25:03 +00:00
|
|
|
apk_db_close(&db);
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
err_and_usage:
|
2009-07-22 11:24:19 +00:00
|
|
|
if (r == -EINVAL)
|
2012-02-17 08:02:44 +00:00
|
|
|
r = usage(applet);
|
2016-04-19 05:55:40 +00:00
|
|
|
if (r == -ESHUTDOWN)
|
|
|
|
r = 0;
|
2012-02-17 08:02:44 +00:00
|
|
|
err:
|
|
|
|
if (ctx)
|
|
|
|
free(ctx);
|
2014-10-08 07:10:45 +00:00
|
|
|
|
|
|
|
fetchConnectionCacheClose();
|
2009-06-29 08:22:55 +00:00
|
|
|
return r;
|
2008-04-17 14:09:13 +00:00
|
|
|
}
|