fix.c 3.41 KB
Newer Older
1 2 3
/* fix.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6 7 8 9 10 11 12 13 14 15
 * 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 <errno.h>
#include <stdio.h>
#include "apk_applet.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
16
#include "apk_print.h"
17
#include "apk_solver.h"
18 19

struct fix_ctx {
20
	unsigned short solver_flags;
21
	int fix_depends : 1;
22
	int fix_xattrs : 1;
23
	int fix_directory_permissions : 1;
24
	int errors;
25 26
};

27
static int option_parse_applet(void *pctx, struct apk_db_options *dbopts, int optch, const char *optarg)
28 29 30
{
	struct fix_ctx *ctx = (struct fix_ctx *) pctx;
	switch (optch) {
31 32 33
	case 'd':
		ctx->fix_depends = 1;
		break;
34 35 36
	case 'x':
		ctx->fix_xattrs = 1;
		break;
37
	case 'u':
38
		ctx->solver_flags |= APK_SOLVERF_UPGRADE;
39 40
		break;
	case 'r':
41
		ctx->solver_flags |= APK_SOLVERF_REINSTALL;
42
		break;
43 44 45
	case 0x10000:
		ctx->fix_directory_permissions = 1;
		break;
46
	default:
47
		return -ENOTSUP;
48 49 50 51
	}
	return 0;
}

52 53 54 55
static const struct apk_option options_applet[] = {
	{ 'd',		"depends",	"Fix all dependencies too" },
	{ 'r',		"reinstall",	"Reinstall the package (default)" },
	{ 'u',		"upgrade",	"Prefer to upgrade package" },
56
	{ 'x',		"xattr",	"Fix packages with broken xattrs" },
57 58 59 60 61 62 63 64 65 66
	{ 0x10000,	"directory-permissions", "Reset all directory permissions" },
};

static const struct apk_option_group optgroup_applet = {
	.name = "Fix",
	.options = options_applet,
	.num_options = ARRAY_SIZE(options_applet),
	.parse = option_parse_applet,
};

67 68 69
static int mark_recalculate(apk_hash_item item, void *ctx)
{
	struct apk_db_dir *dir = (struct apk_db_dir *) item;
70
	if (dir->refs == 0) return 0;
71
	dir->update_permissions = 1;
72 73 74
	return 0;
}

75
static void mark_fix(struct fix_ctx *ctx, struct apk_name *name)
76 77 78 79
{
	apk_solver_set_name_flags(name, ctx->solver_flags, ctx->fix_depends ? ctx->solver_flags : 0);
}

80
static void set_solver_flags(struct apk_database *db, const char *match, struct apk_name *name, void *pctx)
81
{
82 83 84 85 86 87 88
	struct fix_ctx *ctx = pctx;

	if (!name) {
		apk_error("Package '%s' not found", match);
		ctx->errors++;
	} else
		mark_fix(ctx, name);
89 90
}

91
static int fix_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
92 93
{
	struct fix_ctx *ctx = (struct fix_ctx *) pctx;
94
	struct apk_installed_package *ipkg;
95

96 97
	if (!ctx->solver_flags)
		ctx->solver_flags = APK_SOLVERF_REINSTALL;
98

99 100 101
	if (ctx->fix_directory_permissions)
		apk_hash_foreach(&db->installed.dirs, mark_recalculate, db);

102 103
	if (args->num == 0) {
		list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
104 105
			if (ipkg->broken_files || ipkg->broken_script ||
			    (ipkg->broken_xattr && ctx->fix_xattrs))
106 107 108 109
				mark_fix(ctx, ipkg->pkg->name);
		}
	} else
		apk_name_foreach_matching(db, args, apk_foreach_genid(), set_solver_flags, ctx);
110

111 112
	if (ctx->errors) return ctx->errors;

113
	return apk_solver_commit(db, 0, db->world);
114 115 116 117 118
}

static struct apk_applet apk_fix = {
	.name = "fix",
	.help = "Repair package or upgrade it without modifying main "
119
		"dependencies",
120
	.arguments = "PACKAGE...",
121
	.open_flags = APK_OPENF_WRITE,
122
	.command_groups = APK_COMMAND_GROUP_SYSTEM,
123
	.context_size = sizeof(struct fix_ctx),
124
	.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
125 126 127 128 129
	.main = fix_main,
};

APK_DEFINE_APPLET(apk_fix);