File: status.c

package info (click to toggle)
udpkg 1.04
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100 kB
  • ctags: 98
  • sloc: ansic: 962; sh: 152; makefile: 61
file content (390 lines) | stat: -rw-r--r-- 10,198 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* $Id: status.c 32636 2005-12-01 15:43:45Z sferriol-guest $ */
#include "udpkg.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#include <debian-installer.h>

/* Status file handling routines
 * 
 * This is a fairly minimalistic implementation. there are two main functions 
 * that are supported:
 * 
 * 1) reading the entire status file:
 *    the status file is read into memory as a binary-tree, with just the 
 *    package and status info preserved
 *
 * 2) merging the status file
 *    control info from (new) packages is merged into the status file, 
 *    replacing any pre-existing entries. when a merge happens, status info 
 *    read using the status_read function is written back to the status file
 */

static const char *statuswords[][10] = {
	{ (char *)STATUS_WANTSTART, "unknown", "install", "hold", 
		"deinstall", "purge", 0 },
	{ (char *)STATUS_FLAGSTART, "ok", "reinstreq", "hold", 
		"hold-reinstreq", 0 },
	{ (char *)STATUS_STATUSSTART, "not-installed", "unpacked", "half-configured",
		"installed", "half-installed",
		"config-files", "post-inst-failed", 
		"removal-failed", 0 }
};

int package_compare(const void *p1, const void *p2)
{
	return strcmp(((struct package_t *)p1)->package, 
		((struct package_t *)p2)->package);
}

static unsigned long status_parse(const char *line)
{
	char *p;
	int i, j;
	unsigned long l = 0;
	for (i = 0; i < 3; i++)
	{
		p = strchr(line, ' ');
		if (p) *p = 0; 
		j = 1;
		while (statuswords[i][j] != 0) 
		{
			if (strcmp(line, statuswords[i][j]) == 0) 
			{
				l |= (1 << ((long)statuswords[i][0] + j - 1));
				break;
			}
			j++;
		}
		if (statuswords[i][j] == 0) return 0; /* parse error */
		line = p+1;
	}
	return l;
}

static const char *status_print(unsigned long flags)
{
	/* this function returns a static buffer... */
	static char buf[256];
	int i, j;

	buf[0] = 0;
	for (i = 0; i < 3; i++)
	{
		j = 1;
		while (statuswords[i][j] != 0)
		{
			if ((flags & (1 << ((long)statuswords[i][0] + j - 1))) != 0)
			{
				strcat(buf, statuswords[i][j]);
				if (i < 2) strcat(buf, " ");
				break;
			}
			j++;
		}
		if (statuswords[i][j] == 0)
		{
			fprintf(stderr, "corrupted status flag!!: %x\n",flags);
			return NULL;
		}
	}
	return buf;
}

int read_block(FILE *f, char **ml)
{
	char ch;
        char *multiple_lines = *ml;
	char buf[BUFSIZE];

	while (((ch = fgetc(f)) == ' ') && !feof(f)) {
		fgets(buf, BUFSIZE, f);
		multiple_lines = (char *) di_realloc(multiple_lines, strlen(multiple_lines) + strlen(buf) + 2);
                memset(multiple_lines + strlen(multiple_lines), '\0', strlen(buf) + 2);
		strcat(multiple_lines, " ");
		strcat(multiple_lines, buf);
	}
        *ml = multiple_lines;
	ungetc(ch, f);
	return EXIT_SUCCESS;
}

/*
 * Read a control file (or a stanza of a status file) and parse it,
 * filling parsed fields into the package structure
 */
void control_read(FILE *f, struct package_t *p)
{
	char buf[BUFSIZE];
	while (fgets(buf, BUFSIZE, f) && !feof(f))
	{
		buf[strlen(buf)-1] = 0;
		if (*buf == 0)
			return;
		/* these are common to both installed and uninstalled packages */
		else if (strstr(buf, "Package: ") == buf)
		{
			p->package = strdup(buf+9);
		}
		else if (strstr(buf, "Status: ") == buf)
		{
			p->status = status_parse(buf+8);
		}
		else if (strstr(buf, "Depends: ") == buf)
		{
			p->depends = strdup(buf+9);
		}
		else if (strstr(buf, "Provides: ") == buf)
		{
			p->provides = strdup(buf+10);
		}
		else if (strstr(buf, "Description: ") == buf)
		{
			p->description = strdup(buf+13);
			p->long_description = strdup("");
			read_block(f, &p->long_description);
		}
#ifdef DOL18N
		else if (strstr(buf, "description-") == buf)
		{
			/* Localized description */
			struct language_description_t *l;
			l = di_malloc(sizeof(struct language_description_t));
                        memset(l,'\0',sizeof (struct language_description_t));
			l->next = p->localized_descriptions;
			p->localized_descriptions = l;
			buf[14] = '\0';
			l->language = strdup(buf+12);
			l->description = strdup(buf+16);
			l->long_description = strdup("");
			read_block(f, &l->long_description);
                        
		}
#endif
		/* This is specific to the Debian Installer. Ifdef? */
		else if (strcasestr(buf, "Installer-Menu-Item: ") == buf)
		{
			p->installer_menu_item = atoi(buf+21);
		}
		else if (strcasestr(buf, "Priority: ") == buf)
		{
			p->priority = strdup(buf + 10);
		}
		else if (strcasestr(buf, "Section: ") == buf)
		{
			p->section = strdup(buf + 9);
		}
		else if (strcasestr(buf, "Installed-Size: ") == buf)
		{
			p->installed_size = strdup(buf + 16);
		}
		else if (strcasestr(buf, "Maintainer: ") == buf)
		{
			p->maintainer = strdup(buf + 12);
		}
		else if (strcasestr(buf, "Version: ") == buf)
		{
			p->version = strdup(buf + 9);
		}
		else if (strcasestr(buf, "Suggests: ") == buf)
		{
			p->suggests = strdup(buf + 10);
		}
		else if (strcasestr(buf, "Recommends: ") == buf)
		{
			p->recommends = strdup(buf + 12);
		}
		else if (strcasestr(buf, "Conffiles: ") == buf)
		{
                        p->conffiles = strdup("");
			read_block(f, &p->conffiles);
		}

	}
}

void *status_read(void)
{
	FILE *f;
	void *status = 0;
	struct package_t *m = 0, *p = 0, *t = 0;

	if ((f = fopen(STATUSFILE, "r")) == NULL)
	{
		perror(STATUSFILE);
		return 0;
	}
	if (getenv(UDPKG_QUIET) == NULL)
		printf("(Reading database...)\n");
	while (!feof(f))
	{
		m = (struct package_t *)di_malloc(sizeof(struct package_t));
		memset(m, 0, sizeof(struct package_t));
		control_read(f, m);
		if (m->package)
		{
			/*
			 * If there is an item in the tree by this name,
			 * it must be a virtual package; insert real
			 * package in preference.
			 */
			tdelete(m, &status, package_compare);
			tsearch(m, &status, package_compare);
			if (m->provides)
			{
				/* 
				 * A "Provides" triggers the insertion
				 * of a pseudo package into the status
				 * binary-tree.
				 */
				p = (struct package_t *)di_malloc(sizeof(struct package_t));
				memset(p, 0, sizeof(struct package_t));
				p->package = strdup(m->provides);

				t = *(struct package_t **)tsearch(p, &status, package_compare);
				if (!(t == p))
				{
					di_free(p->package);
					di_free(p);
				}
				else {
					/*
					 * Pseudo package status is the
					 * same as the status of the
					 * package providing it 
					 * FIXME: (not quite right, if 2
					 * packages of different statuses
					 * provide it).
					 */
					t->status = m->status;
				}
			}
		}
		else
		{
			di_free(m);
		}
	}
	fclose(f);
	return status;
}

int status_merge(void *status, struct package_t *pkgs)
{
	FILE *fin, *fout;
	char buf[BUFSIZE];
	struct package_t *pkg = 0, *statpkg = 0;
	struct package_t locpkg;
	int r = 0;

	if ((fin = fopen(STATUSFILE, "r")) == NULL)
	{
		perror(STATUSFILE);
		return 0;
	}
	if ((fout = fopen(STATUSFILE ".new", "w")) == NULL)
	{
		perror(STATUSFILE ".new");
		return 0;
	}
	if (getenv(UDPKG_QUIET) == NULL)
		printf("(Updating database...)\n");
	while (fgets(buf, BUFSIZE, fin) && !feof(fin))
	{
		buf[strlen(buf)-1] = 0; /* trim newline */
		/* If we see a package header, find out if it's a package
		 * that we have processed. if so, we skip that block for
		 * now (write it at the end).
		 *
		 * we also look at packages in the status cache and update
		 * their status fields
		 */
		if (strstr(buf, "Package: ") == buf)
		{
			for (pkg = pkgs; pkg != 0 && strcmp(buf+9,
				pkg->package)!=0; pkg = pkg->next) ;

			locpkg.package = buf+9;
			statpkg = tfind(&locpkg, &status, package_compare);
			
			/* note: statpkg should be non-zero, unless the status
			 * file was changed while we are processing (no locking
			 * is currently done...
			 */
			if (statpkg != 0) statpkg = *(struct package_t **)statpkg;
		}
		if (pkg != 0) continue;

		if (strstr(buf, "Status: ") == buf && statpkg != 0)
		{
			  snprintf(buf, sizeof(buf), "Status: %s",
				 status_print(statpkg->status));
		}
		fputs(buf, fout);
		fputc('\n', fout);
	}

	// Print out packages we processed.
	for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
		fprintf(fout, "Package: %s\nStatus: %s\n", 
			pkg->package, status_print(pkg->status));
		if (pkg->priority)
			fprintf(fout, "Priority: %s\n", pkg->priority);
		if (pkg->section)
			fprintf(fout, "Section: %s\n", pkg->section);
		if (pkg->priority)
			fprintf(fout, "Installed-Size: %s\n", pkg->installed_size);
		if (pkg->maintainer)
			fprintf(fout, "Maintainer: %s\n", pkg->maintainer);
		if (pkg->source)
			fprintf(fout, "Source: %s\n", pkg->source);
		if (pkg->version)
			fprintf(fout, "Version: %s\n", pkg->version);
		if (pkg->pre_depends)
			fprintf(fout, "Pre-Depends: %s\n", pkg->pre_depends);
		if (pkg->depends)
			fprintf(fout, "Depends: %s\n", pkg->depends);
		if (pkg->replaces)
			fprintf(fout, "Replaces: %s\n", pkg->replaces);
		if (pkg->recommends)
			fprintf(fout, "Recommends: %s\n", pkg->recommends);
		if (pkg->suggests)
			fprintf(fout, "Suggests: %s\n", pkg->suggests);
		if (pkg->provides)
			fprintf(fout, "Provides: %s\n", pkg->provides);
		if (pkg->conflicts)
			fprintf(fout, "Conflicts: %s\n", pkg->conflicts);
		if (pkg->conffiles)
			fprintf(fout, "Conffiles:\n %s\n", pkg->conffiles);
		if (pkg->description)
			fprintf(fout, "Description: %s\n%s", pkg->description, pkg->long_description);
#ifdef DOL18N
		if (pkg->localized_descriptions) {
			struct language_description_t *ld;
			ld = pkg->localized_descriptions;
			while (ld) {
                                if (ld->language && ld->description && 
                                    ld->long_description) {
                                        fprintf(fout, "description-%s: %s\n%s", 
                                                ld->language,
                                                ld->description, 
                                                ld->long_description);
                                }
				ld = ld->next;
			}
		}
#endif
		if (pkg->installer_menu_item)
			fprintf(fout, "Installer-Menu-Item: %i\n", pkg->installer_menu_item);
		fputc('\n', fout);
	}
	
	fclose(fin);
	fclose(fout);

	r = rename(STATUSFILE, STATUSFILE ".bak");
	if (r == 0) r = rename(STATUSFILE ".new", STATUSFILE);
	return 0;
}