2020-02-14 11:49:41 +00:00
|
|
|
/* app_add.c - Alpine Package Keeper (APK)
|
2008-04-17 14:09:13 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2008-04-17 14:09:13 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-13 18:58:08 +00:00
|
|
|
#include <errno.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
#include <stdio.h>
|
2019-07-02 12:27:57 +00:00
|
|
|
#include <unistd.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_database.h"
|
2010-03-05 08:13:25 +00:00
|
|
|
#include "apk_print.h"
|
2011-09-09 13:31:11 +00:00
|
|
|
#include "apk_solver.h"
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2009-01-13 18:58:08 +00:00
|
|
|
struct add_ctx {
|
2009-05-14 12:01:09 +00:00
|
|
|
const char *virtpkg;
|
2011-09-09 13:31:11 +00:00
|
|
|
unsigned short solver_flags;
|
2019-11-20 10:48:48 +00:00
|
|
|
unsigned short extract_flags;
|
2009-01-13 18:58:08 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
#define ADD_OPTIONS(OPT) \
|
|
|
|
OPT(OPT_ADD_initdb, "initdb") \
|
|
|
|
OPT(OPT_ADD_latest, APK_OPT_SH("l") "latest") \
|
|
|
|
OPT(OPT_ADD_no_chown, "no-chown") \
|
|
|
|
OPT(OPT_ADD_upgrade, APK_OPT_SH("u") "upgrade") \
|
|
|
|
OPT(OPT_ADD_virtual, APK_OPT_ARG APK_OPT_SH("t") "virtual")
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
APK_OPT_APPLET(option_desc, ADD_OPTIONS);
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
static int option_parse_applet(void *ctx, struct apk_ctx *ac, int opt, const char *optarg)
|
2009-01-13 18:58:08 +00:00
|
|
|
{
|
|
|
|
struct add_ctx *actx = (struct add_ctx *) ctx;
|
|
|
|
|
2020-05-04 18:45:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case OPT_ADD_initdb:
|
2020-10-05 15:52:51 +00:00
|
|
|
ac->open_flags |= APK_OPENF_CREATE;
|
2009-01-13 18:58:08 +00:00
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_ADD_latest:
|
|
|
|
actx->solver_flags |= APK_SOLVERF_LATEST;
|
|
|
|
break;
|
|
|
|
case OPT_ADD_no_chown:
|
2019-11-20 10:48:48 +00:00
|
|
|
actx->extract_flags |= APK_EXTRACTF_NO_CHOWN;
|
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_ADD_upgrade:
|
2011-09-09 13:31:11 +00:00
|
|
|
actx->solver_flags |= APK_SOLVERF_UPGRADE;
|
2009-01-16 11:59:36 +00:00
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_ADD_virtual:
|
2009-05-14 12:01:09 +00:00
|
|
|
actx->virtpkg = optarg;
|
|
|
|
break;
|
2009-01-13 18:58:08 +00:00
|
|
|
default:
|
2014-10-08 12:29:27 +00:00
|
|
|
return -ENOTSUP;
|
2009-01-13 18:58:08 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static const struct apk_option_group optgroup_applet = {
|
2020-05-04 18:45:11 +00:00
|
|
|
.desc = option_desc,
|
2014-10-08 12:29:27 +00:00
|
|
|
.parse = option_parse_applet,
|
|
|
|
};
|
|
|
|
|
2009-07-24 11:00:57 +00:00
|
|
|
static int non_repository_check(struct apk_database *db)
|
|
|
|
{
|
2020-10-05 15:52:51 +00:00
|
|
|
if (db->ctx->force & APK_FORCE_NON_REPOSITORY)
|
2009-07-24 11:00:57 +00:00
|
|
|
return 0;
|
|
|
|
if (apk_db_cache_active(db))
|
|
|
|
return 0;
|
|
|
|
if (apk_db_permanent(db))
|
|
|
|
return 0;
|
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(&db->ctx->out,
|
|
|
|
"You tried to add a non-repository package to system, "
|
|
|
|
"but it would be lost on next reboot. Enable package caching "
|
|
|
|
"(apk cache --help) or use --force-non-repository "
|
|
|
|
"if you know what you are doing.");
|
2009-07-24 11:00:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-02 12:27:57 +00:00
|
|
|
static struct apk_package *create_virtual_package(struct apk_database *db, struct apk_name *name)
|
|
|
|
{
|
|
|
|
char ver[32];
|
|
|
|
struct apk_package *virtpkg;
|
|
|
|
struct tm tm;
|
|
|
|
EVP_MD_CTX *mdctx;
|
2020-02-04 08:31:10 +00:00
|
|
|
time_t now = time(NULL);
|
2019-07-02 12:27:57 +00:00
|
|
|
pid_t pid = getpid();
|
|
|
|
|
2019-07-09 06:45:24 +00:00
|
|
|
gmtime_r(&now, &tm);
|
2019-07-02 12:27:57 +00:00
|
|
|
strftime(ver, sizeof ver, "%Y%m%d.%H%M%S", &tm);
|
|
|
|
|
|
|
|
virtpkg = apk_pkg_new();
|
|
|
|
if (virtpkg == NULL) return 0;
|
|
|
|
|
|
|
|
virtpkg->name = name;
|
2020-05-19 08:39:21 +00:00
|
|
|
virtpkg->version = apk_atomize_dup(&db->atoms, APK_BLOB_STR(ver));
|
2019-07-02 12:27:57 +00:00
|
|
|
virtpkg->description = strdup("virtual meta package");
|
2020-05-19 08:39:21 +00:00
|
|
|
virtpkg->arch = apk_atomize(&db->atoms, APK_BLOB_STR("noarch"));
|
2019-07-02 12:27:57 +00:00
|
|
|
|
|
|
|
mdctx = EVP_MD_CTX_new();
|
|
|
|
EVP_DigestInit_ex(mdctx, apk_checksum_default(), NULL);
|
|
|
|
EVP_DigestUpdate(mdctx, &tm, sizeof tm);
|
|
|
|
EVP_DigestUpdate(mdctx, &pid, sizeof pid);
|
|
|
|
EVP_DigestUpdate(mdctx, virtpkg->name->name, strlen(virtpkg->name->name) + 1);
|
|
|
|
virtpkg->csum.type = EVP_MD_CTX_size(mdctx);
|
|
|
|
EVP_DigestFinal_ex(mdctx, virtpkg->csum.data, NULL);
|
|
|
|
EVP_MD_CTX_free(mdctx);
|
|
|
|
|
|
|
|
return virtpkg;
|
|
|
|
}
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
static int add_main(void *ctx, struct apk_ctx *ac, struct apk_string_array *args)
|
2008-04-17 14:09:13 +00:00
|
|
|
{
|
2020-10-09 09:44:32 +00:00
|
|
|
struct apk_out *out = &ac->out;
|
|
|
|
struct apk_database *db = ac->db;
|
2009-01-13 18:58:08 +00:00
|
|
|
struct add_ctx *actx = (struct add_ctx *) ctx;
|
2009-05-14 12:01:09 +00:00
|
|
|
struct apk_package *virtpkg = NULL;
|
2009-05-15 07:46:43 +00:00
|
|
|
struct apk_dependency virtdep;
|
2011-09-09 13:31:11 +00:00
|
|
|
struct apk_dependency_array *world = NULL;
|
2013-06-18 10:01:51 +00:00
|
|
|
char **parg;
|
|
|
|
int r = 0;
|
2011-09-09 13:31:11 +00:00
|
|
|
|
|
|
|
apk_dependency_array_copy(&world, db->world);
|
2009-06-25 12:14:07 +00:00
|
|
|
|
2019-11-20 10:48:48 +00:00
|
|
|
if (getuid() != 0 || (actx->extract_flags & APK_EXTRACTF_NO_CHOWN))
|
|
|
|
db->extract_flags |= APK_EXTRACTF_NO_CHOWN;
|
|
|
|
|
2009-05-14 12:01:09 +00:00
|
|
|
if (actx->virtpkg) {
|
2012-02-28 12:56:08 +00:00
|
|
|
apk_blob_t b = APK_BLOB_STR(actx->virtpkg);
|
|
|
|
apk_blob_pull_dep(&b, db, &virtdep);
|
2012-02-29 06:53:43 +00:00
|
|
|
if (APK_BLOB_IS_NULL(b) || virtdep.conflict ||
|
|
|
|
virtdep.result_mask != APK_DEPMASK_ANY ||
|
2020-05-19 08:39:21 +00:00
|
|
|
virtdep.version != &apk_atom_null) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "%s: bad package specifier");
|
2012-02-28 12:56:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-04-24 06:33:09 +00:00
|
|
|
if (virtdep.name->name[0] != '.' && non_repository_check(db))
|
|
|
|
return -1;
|
|
|
|
|
2019-07-02 12:27:57 +00:00
|
|
|
virtpkg = create_virtual_package(db, virtdep.name);
|
|
|
|
if (!virtpkg) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "Failed to allocate virtual meta package");
|
2010-06-01 08:49:32 +00:00
|
|
|
return -1;
|
2009-05-14 12:01:09 +00:00
|
|
|
}
|
2019-06-03 06:27:33 +00:00
|
|
|
|
|
|
|
virtdep.result_mask = APK_VERSION_EQUAL;
|
|
|
|
virtdep.version = virtpkg->version;
|
2011-09-09 13:31:11 +00:00
|
|
|
}
|
2008-04-21 16:30:10 +00:00
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
foreach_array_item(parg, args) {
|
2008-11-28 11:34:40 +00:00
|
|
|
struct apk_dependency dep;
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
if (strstr(*parg, ".apk") != NULL) {
|
2009-07-22 17:47:21 +00:00
|
|
|
struct apk_package *pkg = NULL;
|
2009-07-17 10:07:52 +00:00
|
|
|
struct apk_sign_ctx sctx;
|
2008-11-28 11:34:40 +00:00
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
if (non_repository_check(db))
|
2010-06-01 08:49:32 +00:00
|
|
|
return -1;
|
2009-07-22 17:47:21 +00:00
|
|
|
|
2009-07-22 18:04:54 +00:00
|
|
|
apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY_AND_GENERATE,
|
2020-10-05 15:52:51 +00:00
|
|
|
NULL, db->keys_fd, db->ctx->flags & APK_ALLOW_UNTRUSTED);
|
2013-06-18 10:01:51 +00:00
|
|
|
r = apk_pkg_read(db, *parg, &sctx, &pkg);
|
2009-07-17 10:07:52 +00:00
|
|
|
apk_sign_ctx_free(&sctx);
|
2009-07-22 11:56:27 +00:00
|
|
|
if (r != 0) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "%s: %s", *parg, apk_error_str(r));
|
2010-06-01 08:49:32 +00:00
|
|
|
return -1;
|
2009-07-22 17:47:21 +00:00
|
|
|
}
|
2009-08-06 11:25:03 +00:00
|
|
|
apk_dep_from_pkg(&dep, db, pkg);
|
2009-07-22 11:24:19 +00:00
|
|
|
} else {
|
2013-06-18 10:01:51 +00:00
|
|
|
apk_blob_t b = APK_BLOB_STR(*parg);
|
2012-01-12 08:27:09 +00:00
|
|
|
|
2011-10-18 22:11:26 +00:00
|
|
|
apk_blob_pull_dep(&b, db, &dep);
|
2018-02-20 22:22:02 +00:00
|
|
|
if (APK_BLOB_IS_NULL(b) || b.len > 0 || (virtpkg != NULL && dep.repository_tag)) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "'%s' is not a valid %s dependency, format is %s",
|
|
|
|
*parg, virtpkg == NULL ? "world" : "child",
|
|
|
|
virtpkg == NULL ? "name(@tag)([<>~=]version)" : "name([<>~=]version)");
|
2010-06-01 08:49:32 +00:00
|
|
|
return -1;
|
2012-01-06 11:40:55 +00:00
|
|
|
}
|
2009-07-22 11:24:19 +00:00
|
|
|
}
|
2009-06-16 09:59:21 +00:00
|
|
|
|
2011-09-13 08:47:34 +00:00
|
|
|
if (virtpkg == NULL) {
|
2011-09-09 13:31:11 +00:00
|
|
|
apk_deps_add(&world, &dep);
|
2011-09-16 14:10:50 +00:00
|
|
|
apk_solver_set_name_flags(dep.name,
|
2012-02-15 11:57:36 +00:00
|
|
|
actx->solver_flags,
|
2011-09-16 14:10:50 +00:00
|
|
|
actx->solver_flags);
|
2011-09-13 08:47:34 +00:00
|
|
|
} else {
|
|
|
|
apk_deps_add(&virtpkg->depends, &dep);
|
|
|
|
}
|
2009-05-15 11:47:11 +00:00
|
|
|
}
|
2011-09-13 08:47:34 +00:00
|
|
|
if (virtpkg) {
|
2018-11-02 15:41:34 +00:00
|
|
|
virtpkg = apk_db_pkg_add(db, virtpkg);
|
2011-09-09 13:31:11 +00:00
|
|
|
apk_deps_add(&world, &virtdep);
|
2011-09-16 14:10:50 +00:00
|
|
|
apk_solver_set_name_flags(virtdep.name,
|
|
|
|
actx->solver_flags,
|
|
|
|
actx->solver_flags);
|
2011-09-13 08:47:34 +00:00
|
|
|
}
|
2009-05-15 07:46:43 +00:00
|
|
|
|
2011-09-13 08:47:34 +00:00
|
|
|
r = apk_solver_commit(db, 0, world);
|
2011-09-09 13:31:11 +00:00
|
|
|
apk_dependency_array_free(&world);
|
2009-08-04 10:57:54 +00:00
|
|
|
|
2009-04-14 15:48:02 +00:00
|
|
|
return r;
|
2008-04-17 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_applet apk_add = {
|
|
|
|
.name = "add",
|
2009-08-06 11:25:03 +00:00
|
|
|
.open_flags = APK_OPENF_WRITE,
|
2009-01-13 18:58:08 +00:00
|
|
|
.context_size = sizeof(struct add_ctx),
|
2014-10-08 12:29:27 +00:00
|
|
|
.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
|
2008-04-17 14:09:13 +00:00
|
|
|
.main = add_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_add);
|