aboutsummaryrefslogtreecommitdiffstats
path: root/src/state.c
blob: 119bebc8974fa09eee5a12b4932cc1433f6042e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* 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 <malloc.h>

#include "apk_state.h"
#include "apk_database.h"

static int apk_state_commit_deps(struct apk_state *state,
				 struct apk_database *db,
				 struct apk_dependency_array *deps);

struct apk_state *apk_state_new(struct apk_database *db)
{
	struct apk_state *state;
	int num_bytes;

	num_bytes = sizeof(struct apk_state) + (db->pkg_id * 2 + 7) / 8;
	state = (struct apk_state*) calloc(1, num_bytes);
	state->refs = 1;

	return state;
}

struct apk_state *apk_state_dup(struct apk_state *state)
{
	state->refs++;
	return state;
}

void apk_state_unref(struct apk_state *state)
{
	if (--state->refs > 0)
		return;

	free(state);
}

static void apk_state_set(struct apk_state *state, int pos, int val)
{
	int byte = pos / 4, offs = pos % 4;

	state->bitarray[byte] &= ~(0x3 << (offs * 2));
	state->bitarray[byte] |= (val & 0x3) << (offs * 2);
}

static int apk_state_get(struct apk_state *state, int pos)
{
	int byte = pos / 4, offs = pos % 4;

	if (state == NULL)
		return APK_STATE_NOT_CONSIDERED;

	return (state->bitarray[byte] >> (offs * 2)) & 0x3;
}

static int apk_state_commit_pkg(struct apk_state *state,
				struct apk_database *db,
				struct apk_name *name,
				struct apk_package *oldpkg,
				struct apk_package *newpkg)
{
	const char *msg = NULL;
	int r, upgrade = 0;

	if (newpkg != NULL) {
		r = apk_state_commit_deps(state, db, newpkg->depends);
		if (r != 0)
			return r;
	}

	if (oldpkg == NULL) {
		apk_message("Installing %s (%s)",
			    name->name, newpkg->version);
	} else if (newpkg == NULL) {
		apk_message("Purging %s (%s)",
			    name->name, oldpkg->version);
	} else {
		r = apk_version_compare(APK_BLOB_STR(newpkg->version),
					APK_BLOB_STR(oldpkg->version));
		switch (r) {
		case APK_VERSION_LESS:
			msg = "Downgrading";
			upgrade = 1;
			break;
		case APK_VERSION_EQUAL:
			msg = "Re-installing";
			break;
		case APK_VERSION_GREATER:
			msg = "Upgrading";
			upgrade = 1;
			break;
		}
		apk_message("%s %s (%s -> %s)",
			    msg, name->name, oldpkg->version, newpkg->version);
	}

	return apk_db_install_pkg(db, oldpkg, newpkg);
}

static int apk_state_commit_name(struct apk_state *state,
				 struct apk_database *db,
				 struct apk_name *name)
{
	struct apk_package *oldpkg = NULL, *newpkg = NULL;
	int i;

	for (i = 0; i < name->pkgs->num; i++) {
		if (apk_pkg_get_state(name->pkgs->item[i]) == APK_STATE_INSTALL)
			oldpkg = name->pkgs->item[i];
		if (apk_state_get(state, name->pkgs->item[i]->id) == APK_STATE_INSTALL)
			newpkg = name->pkgs->item[i];
	}

	if (oldpkg == NULL && newpkg == NULL)
		return 0;

	/* No reinstallations for now */
	if (newpkg == oldpkg)
		return 0;

	return apk_state_commit_pkg(state, db, name, oldpkg, newpkg);
}

static int apk_state_commit_deps(struct apk_state *state,
				 struct apk_database *db,
				 struct apk_dependency_array *deps)
{
	int r, i;

	if (deps == NULL)
		return 0;

	for (i = 0; i < deps->num; i++) {
		r = apk_state_commit_name(state, db, deps->item[i].name);
		if (r != 0)
			return r;
	}

	return 0;
}

int apk_state_commit(struct apk_state *state,
		     struct apk_database *db)
{
	struct apk_package *pkg;
	struct hlist_node *c, *n;
	int r;

	/* Check all dependencies */
	r = apk_state_commit_deps(state, db, db->world);
	if (r != 0)
		return r;

	/* And purge all installed packages that were not considered */
	hlist_for_each_entry_safe(pkg, c, n, &db->installed.packages, installed_pkgs_list)
		apk_state_commit_name(state, db, pkg->name);

	return 0;
}

int apk_state_satisfy_name(struct apk_state *state,
			   struct apk_name *name)
{
	struct apk_package *preferred = NULL;
	int i;
	int upgrading = 1;

	/* Is something already installed? Or figure out the preferred
	 * package. */
	for (i = 0; i < name->pkgs->num; i++) {
		if (apk_state_get(state, name->pkgs->item[i]->id) ==
		    APK_STATE_INSTALL)
			return 0;

		if (preferred == NULL) {
			preferred = name->pkgs->item[i];
			continue;
		}

		if (upgrading) {
			if (apk_version_compare(APK_BLOB_STR(name->pkgs->item[i]->version),
						APK_BLOB_STR(preferred->version)) ==
			    APK_VERSION_GREATER) {
				preferred = name->pkgs->item[i];
				continue;
			}
		} else {
			if (apk_pkg_get_state(name->pkgs->item[i]) ==
			    APK_STATE_INSTALL) {
				preferred = name->pkgs->item[i];
				continue;
			}
		}
	}

	/* Mark conflicting names as no install */
	for (i = 0; i < name->pkgs->num; i++) {
		if (name->pkgs->item[i] != preferred)
			apk_state_set(state, name->pkgs->item[i]->id,
				      APK_STATE_NO_INSTALL);
	}

	return apk_state_pkg_install(state, preferred);
}

int apk_state_satisfy_deps(struct apk_state *state,
			   struct apk_dependency_array *deps)
{
	struct apk_name *name;
	int r, i;

	if (deps == NULL)
		return 0;

	for (i = 0; i < deps->num; i++) {
		name = deps->item[i].name;
		if (name->pkgs == NULL) {
			apk_error("No providers for '%s'", name->name);
			return -1;
		}
		r = apk_state_satisfy_name(state, name);
		if (r != 0)
			return r;
	}
	return 0;
}

void apk_state_pkg_set(struct apk_state *state,
		       struct apk_package *pkg)
{
	apk_state_set(state, pkg->id, APK_STATE_INSTALL);
}

int apk_state_pkg_install(struct apk_state *state,
			  struct apk_package *pkg)
{
	switch (apk_state_get(state, pkg->id)) {
	case APK_STATE_INSTALL:
		return 0;
	case APK_STATE_NO_INSTALL:
		return -1;
	}

	apk_state_set(state, pkg->id, APK_STATE_INSTALL);

	if (pkg->depends == NULL)
		return 0;

	return apk_state_satisfy_deps(state, pkg->depends);
}

int apk_state_pkg_is_installed(struct apk_state *state,
			       struct apk_package *pkg)
{
	return apk_state_get(state, pkg->id);
}