2020-02-14 11:49:41 +00:00
|
|
|
/* app_fix.c - Alpine Package Keeper (APK)
|
2009-08-04 12:19:29 +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>
|
2009-08-04 12:19:29 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2009-08-04 12:19:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_database.h"
|
2010-03-05 08:13:25 +00:00
|
|
|
#include "apk_print.h"
|
2011-09-13 08:47:34 +00:00
|
|
|
#include "apk_solver.h"
|
2009-08-04 12:19:29 +00:00
|
|
|
|
|
|
|
struct fix_ctx {
|
2011-09-13 08:47:34 +00:00
|
|
|
unsigned short solver_flags;
|
2011-09-16 14:10:50 +00:00
|
|
|
int fix_depends : 1;
|
2015-04-17 06:44:52 +00:00
|
|
|
int fix_xattrs : 1;
|
2012-02-10 14:40:01 +00:00
|
|
|
int fix_directory_permissions : 1;
|
2014-12-08 06:30:58 +00:00
|
|
|
int errors;
|
2009-08-04 12:19:29 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
#define FIX_OPTIONS(OPT) \
|
|
|
|
OPT(OPT_FIX_depends, APK_OPT_SH("d") "depends") \
|
|
|
|
OPT(OPT_FIX_directory_permissions, "directory-permissions") \
|
|
|
|
OPT(OPT_FIX_reinstall, APK_OPT_SH("r") "reinstall") \
|
|
|
|
OPT(OPT_FIX_upgrade, APK_OPT_SH("u") "upgrade") \
|
|
|
|
OPT(OPT_FIX_xattr, APK_OPT_SH("x") "xattr")
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
APK_OPT_APPLET(option_desc, FIX_OPTIONS);
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
static int option_parse_applet(void *pctx, struct apk_ctx *ac, int opt, const char *optarg)
|
2009-08-04 12:19:29 +00:00
|
|
|
{
|
|
|
|
struct fix_ctx *ctx = (struct fix_ctx *) pctx;
|
2020-05-04 18:45:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case OPT_FIX_depends:
|
2011-09-16 14:10:50 +00:00
|
|
|
ctx->fix_depends = 1;
|
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_FIX_directory_permissions:
|
|
|
|
ctx->fix_directory_permissions = 1;
|
2009-08-04 12:19:29 +00:00
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_FIX_reinstall:
|
2011-09-13 08:47:34 +00:00
|
|
|
ctx->solver_flags |= APK_SOLVERF_REINSTALL;
|
2009-08-04 12:19:29 +00:00
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_FIX_upgrade:
|
|
|
|
ctx->solver_flags |= APK_SOLVERF_UPGRADE;
|
|
|
|
break;
|
|
|
|
case OPT_FIX_xattr:
|
|
|
|
ctx->fix_xattrs = 1;
|
2012-02-10 14:40:01 +00:00
|
|
|
break;
|
2009-08-04 12:19:29 +00:00
|
|
|
default:
|
2014-10-08 12:29:27 +00:00
|
|
|
return -ENOTSUP;
|
2009-08-04 12:19:29 +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,
|
|
|
|
};
|
|
|
|
|
2012-02-10 14:40:01 +00:00
|
|
|
static int mark_recalculate(apk_hash_item item, void *ctx)
|
|
|
|
{
|
|
|
|
struct apk_db_dir *dir = (struct apk_db_dir *) item;
|
2018-01-03 07:31:10 +00:00
|
|
|
if (dir->refs == 0) return 0;
|
2014-10-07 10:29:34 +00:00
|
|
|
dir->update_permissions = 1;
|
2012-02-10 14:40:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 10:12:44 +00:00
|
|
|
static void mark_fix(struct fix_ctx *ctx, struct apk_name *name)
|
2013-06-18 10:34:01 +00:00
|
|
|
{
|
|
|
|
apk_solver_set_name_flags(name, ctx->solver_flags, ctx->fix_depends ? ctx->solver_flags : 0);
|
|
|
|
}
|
|
|
|
|
2014-12-08 06:30:58 +00:00
|
|
|
static void set_solver_flags(struct apk_database *db, const char *match, struct apk_name *name, void *pctx)
|
2013-06-20 10:12:44 +00:00
|
|
|
{
|
2020-10-05 15:52:51 +00:00
|
|
|
struct apk_out *out = &db->ctx->out;
|
2014-12-08 06:30:58 +00:00
|
|
|
struct fix_ctx *ctx = pctx;
|
|
|
|
|
|
|
|
if (!name) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "Package '%s' not found", match);
|
2014-12-08 06:30:58 +00:00
|
|
|
ctx->errors++;
|
|
|
|
} else
|
|
|
|
mark_fix(ctx, name);
|
2013-06-20 10:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
static int fix_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *args)
|
2009-08-04 12:19:29 +00:00
|
|
|
{
|
2020-10-09 09:44:32 +00:00
|
|
|
struct apk_database *db = ac->db;
|
2009-08-04 12:19:29 +00:00
|
|
|
struct fix_ctx *ctx = (struct fix_ctx *) pctx;
|
2013-06-20 10:12:44 +00:00
|
|
|
struct apk_installed_package *ipkg;
|
2009-08-04 12:19:29 +00:00
|
|
|
|
2011-09-13 08:47:34 +00:00
|
|
|
if (!ctx->solver_flags)
|
|
|
|
ctx->solver_flags = APK_SOLVERF_REINSTALL;
|
2009-08-04 12:19:29 +00:00
|
|
|
|
2012-02-10 14:40:01 +00:00
|
|
|
if (ctx->fix_directory_permissions)
|
|
|
|
apk_hash_foreach(&db->installed.dirs, mark_recalculate, db);
|
|
|
|
|
2013-06-20 10:12:44 +00:00
|
|
|
if (args->num == 0) {
|
|
|
|
list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
|
2015-04-17 06:44:52 +00:00
|
|
|
if (ipkg->broken_files || ipkg->broken_script ||
|
|
|
|
(ipkg->broken_xattr && ctx->fix_xattrs))
|
2013-06-20 10:12:44 +00:00
|
|
|
mark_fix(ctx, ipkg->pkg->name);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
apk_name_foreach_matching(db, args, apk_foreach_genid(), set_solver_flags, ctx);
|
2009-08-04 12:19:29 +00:00
|
|
|
|
2014-12-08 06:30:58 +00:00
|
|
|
if (ctx->errors) return ctx->errors;
|
|
|
|
|
2013-06-18 10:34:01 +00:00
|
|
|
return apk_solver_commit(db, 0, db->world);
|
2009-08-04 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_applet apk_fix = {
|
|
|
|
.name = "fix",
|
2009-08-06 11:25:03 +00:00
|
|
|
.open_flags = APK_OPENF_WRITE,
|
2009-08-04 12:19:29 +00:00
|
|
|
.context_size = sizeof(struct fix_ctx),
|
2014-10-08 12:29:27 +00:00
|
|
|
.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
|
2009-08-04 12:19:29 +00:00
|
|
|
.main = fix_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_fix);
|
|
|
|
|