blob: 7b4c74d7dd8fa0e7ad85d827e5232b40e71415c4 [file] [log] [blame]
Glenn L McGrathc9005752001-02-10 02:05:24 +00001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <search.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include <utime.h>
9#include <sys/types.h>
10#include <sys/stat.h>
Glenn L McGrath649968c2001-02-10 14:26:48 +000011
Glenn L McGrathc9005752001-02-10 02:05:24 +000012#include "busybox.h"
13
14//#define PACKAGE "udpkg"
15//#define VERSION "0.1"
16
17/*
18 * Should we do full dependency checking?
19 */
20#define DODEPENDS 1
21
22/*
23 * Should we do debugging?
24 */
25#define DODEBUG 0
26
27#ifdef DODEBUG
Glenn L McGrathc9005752001-02-10 02:05:24 +000028#define SYSTEM(x) do_system(x)
29#define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
30#else
Glenn L McGrathc9005752001-02-10 02:05:24 +000031#define SYSTEM(x) system(x)
32#define DPRINTF(fmt,args...) /* nothing */
33#endif
34
35#define BUFSIZE 4096
Glenn L McGrath510f0dd2001-02-10 14:53:08 +000036#define DEPENDSMAX 64 /* maximum number of depends we can handle */
37
Glenn L McGrath63106462001-02-11 01:40:23 +000038static const char statusfile[] = "/var/lib/dpkg/status.udeb";
39static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
40static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
41
42static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
43static const char rm_dpkgcidir[] = "rm -rf /var/lib/dpkg/tmp.ci/";
44
Glenn L McGrath510f0dd2001-02-10 14:53:08 +000045static const char infodir[] = "/var/lib/dpkg/info/";
46static const char udpkg_quiet[] = "UDPKG_QUIET";
Glenn L McGrathc9005752001-02-10 02:05:24 +000047
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000048//static const int status_wantstart = 0;
49//static const int status_wantunknown = (1 << 0);
50static const int status_wantinstall = (1 << 1);
51//static const int status_wanthold = (1 << 2);
52//static const int status_wantdeinstall = (1 << 3);
53//static const int status_wantpurge = (1 << 4);
54static const int status_wantmask = 31;
Glenn L McGrathc9005752001-02-10 02:05:24 +000055
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000056//static const int status_flagstart = 5;
57static const int status_flagok = (1 << 5); /* 32 */
58//static const int status_flagreinstreq = (1 << 6);
59//static const int status_flaghold = (1 << 7);
60//static const int status_flagholdreinstreq = (1 << 8);
61static const int status_flagmask = 480;
Glenn L McGrathc9005752001-02-10 02:05:24 +000062
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000063//static const int status_statusstart = 9;
64//static const int status_statusnoninstalled = (1 << 9); /* 512 */
65static const int status_statusunpacked = (1 << 10);
66static const int status_statushalfconfigured = (1 << 11);
67static const int status_statusinstalled = (1 << 12);
68static const int status_statushalfinstalled = (1 << 13);
69//static const int status_statusconfigfiles = (1 << 14);
70//static const int status_statuspostinstfailed = (1 << 15);
71//static const int status_statusremovalfailed = (1 << 16);
72static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */
Glenn L McGrathc9005752001-02-10 02:05:24 +000073
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000074static const char *statuswords[][10] = {
75 { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
76 { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
77 { (char *) 9, "not-installed", "unpacked", "half-configured",
78 "installed", "half-installed", "config-files",
79 "post-inst-failed", "removal-failed", 0 }
80};
81
82const int color_white = 0;
83const int color_grey = 1;
84const int color_black = 2;
Glenn L McGrathc9005752001-02-10 02:05:24 +000085
86/* data structures */
Glenn L McGrath649968c2001-02-10 14:26:48 +000087typedef struct package_s {
Glenn L McGrathc9005752001-02-10 02:05:24 +000088 char *file;
89 char *package;
90 char *version;
91 char *depends;
92 char *provides;
93 char *description;
94 int installer_menu_item;
95 unsigned long status;
96 char color; /* for topo-sort */
Glenn L McGrath649968c2001-02-10 14:26:48 +000097 struct package_s *requiredfor[DEPENDSMAX];
Glenn L McGrathc9005752001-02-10 02:05:24 +000098 unsigned short requiredcount;
Glenn L McGrath649968c2001-02-10 14:26:48 +000099 struct package_s *next;
100} package_t;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000101
Glenn L McGrath63106462001-02-11 01:40:23 +0000102#ifdef DODEBUG
103static int do_system(const char *cmd)
104{
105 DPRINTF("cmd is %s\n", cmd);
106 return system(cmd);
107}
108#else
109#define do_system(cmd) system(cmd)
110#endif
111
Glenn L McGrath649968c2001-02-10 14:26:48 +0000112static int package_compare(const void *p1, const void *p2)
113{
114 return strcmp(((package_t *)p1)->package,
115 ((package_t *)p2)->package);
116}
Glenn L McGrathc9005752001-02-10 02:05:24 +0000117
118#ifdef DODEPENDS
119#include <ctype.h>
120
121static char **depends_split(const char *dependsstr)
122{
123 static char *dependsvec[DEPENDSMAX];
124 char *p;
125 int i = 0;
126
127 dependsvec[0] = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000128 if (dependsstr == 0) {
129 goto end;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000130 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000131
132 p = strdup(dependsstr);
133 while (*p != 0 && *p != '\n') {
134 if (*p != ' ') {
135 if (*p == ',') {
136 *p = 0;
137 dependsvec[++i] = 0;
138 } else {
139 if (dependsvec[i] == 0) {
140 dependsvec[i] = p;
141 }
142 }
143 } else {
144 *p = 0; /* eat the space... */
145 }
146 p++;
147 }
148 *p = 0;
149
150end:
Glenn L McGrathc9005752001-02-10 02:05:24 +0000151 dependsvec[i+1] = 0;
152 return dependsvec;
153}
154
Glenn L McGrath63106462001-02-11 01:40:23 +0000155/* Topological sort algorithm:
156 * ordered is the output list, pkgs is the dependency graph, pkg is
157 * the current node
158 *
159 * recursively add all the adjacent nodes to the ordered list, marking
160 * each one as visited along the way
161 *
162 * yes, this algorithm looks a bit odd when all the params have the
163 * same type :-)
164 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000165static void depends_sort_visit(package_t **ordered, package_t *pkgs,
166 package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000167{
Glenn L McGrathc9005752001-02-10 02:05:24 +0000168 unsigned short i;
169
170 /* mark node as processing */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000171 pkg->color = color_grey;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000172
173 /* visit each not-yet-visited node */
174 for (i = 0; i < pkg->requiredcount; i++)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000175 if (pkg->requiredfor[i]->color == color_white)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000176 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
177
178#if 0
179 /* add it to the list */
180 newnode = (struct package_t *)malloc(sizeof(struct package_t));
181 /* make a shallow copy */
182 *newnode = *pkg;
183 newnode->next = *ordered;
184 *ordered = newnode;
185#endif
Glenn L McGrath63106462001-02-11 01:40:23 +0000186
Glenn L McGrathc9005752001-02-10 02:05:24 +0000187 pkg->next = *ordered;
188 *ordered = pkg;
189
190 /* mark node as done */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000191 pkg->color = color_black;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000192}
193
Glenn L McGrath649968c2001-02-10 14:26:48 +0000194static package_t *depends_sort(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000195{
196 /* TODO: it needs to break cycles in the to-be-installed package
197 * graph... */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000198 package_t *ordered = NULL;
199 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000200
Glenn L McGrath63106462001-02-11 01:40:23 +0000201 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000202 pkg->color = color_white;
Glenn L McGrath63106462001-02-11 01:40:23 +0000203 }
204 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
205 if (pkg->color == color_white) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000206 depends_sort_visit(&ordered, pkgs, pkg);
Glenn L McGrath63106462001-02-11 01:40:23 +0000207 }
208 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000209
210 /* Leaks the old list... return the new one... */
211 return ordered;
212}
213
214
215/* resolve package dependencies --
216 * for each package in the list of packages to be installed, we parse its
217 * dependency info to determine if the dependent packages are either
218 * already installed, or are scheduled to be installed. If both tests fail
219 * than bail.
220 *
221 * The algorithm here is O(n^2*m) where n = number of packages to be
222 * installed and m is the # of dependencies per package. Not a terribly
223 * efficient algorithm, but given that at any one time you are unlikely
224 * to install a very large number of packages it doesn't really matter
225 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000226static package_t *depends_resolve(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000227{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000228 package_t *pkg, *chk;
229 package_t dependpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000230 char **dependsvec;
231 int i;
232 void *found;
233
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000234 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000235 dependsvec = depends_split(pkg->depends);
236 i = 0;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000237 while (dependsvec[i] != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000238 /* Check for dependencies; first look for installed packages */
239 dependpkg.package = dependsvec[i];
240 if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
Glenn L McGrath649968c2001-02-10 14:26:48 +0000241 ((chk = *(package_t **)found) &&
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000242 (chk->status & (status_flagok | status_statusinstalled)) !=
Glenn L McGrath63106462001-02-11 01:40:23 +0000243 (status_flagok | status_statusinstalled))) {
244
Glenn L McGrathc9005752001-02-10 02:05:24 +0000245 /* if it fails, we look through the list of packages we are going to
246 * install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000247 for (chk = pkgs; chk != 0; chk = chk->next) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000248 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000249 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
250 if (chk->requiredcount >= DEPENDSMAX) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000251 fprintf(stderr, "Too many dependencies for %s\n", chk->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000252 return 0;
253 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000254 if (chk != pkg) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000255 chk->requiredfor[chk->requiredcount++] = pkg;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000256 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000257 break;
258 }
259 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000260 if (chk == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000261 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
262 return 0;
263 }
264 }
265 i++;
266 }
267 }
268
269 return depends_sort(pkgs);
270}
271#endif
272
273/* Status file handling routines
274 *
275 * This is a fairly minimalistic implementation. there are two main functions
276 * that are supported:
277 *
278 * 1) reading the entire status file:
279 * the status file is read into memory as a binary-tree, with just the
280 * package and status info preserved
281 *
282 * 2) merging the status file
283 * control info from (new) packages is merged into the status file,
284 * replacing any pre-existing entries. when a merge happens, status info
285 * read using the status_read function is written back to the status file
286 */
Glenn L McGrathc9005752001-02-10 02:05:24 +0000287static unsigned long status_parse(const char *line)
288{
289 char *p;
290 int i, j;
291 unsigned long l = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000292
293 for (i = 0; i < 3; i++) {
294 if ((p = strchr(line, ' ')) != NULL) {
295 *p = 0;
296 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000297 j = 1;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000298 while (statuswords[i][j] != 0) {
299 if (strcmp(line, statuswords[i][j]) == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000300 l |= (1 << ((int)statuswords[i][0] + j - 1));
301 break;
302 }
303 j++;
304 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000305 /* parse error */
306 if (statuswords[i][j] == 0) {
307 return 0;
308 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000309 line = p+1;
310 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000311
Glenn L McGrathc9005752001-02-10 02:05:24 +0000312 return l;
313}
314
315static const char *status_print(unsigned long flags)
316{
317 /* this function returns a static buffer... */
318 static char buf[256];
319 int i, j;
320
321 buf[0] = 0;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000322 for (i = 0; i < 3; i++) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000323 j = 1;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000324 while (statuswords[i][j] != 0) {
325 if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000326 strcat(buf, statuswords[i][j]);
327 if (i < 2) strcat(buf, " ");
328 break;
329 }
330 j++;
331 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000332 if (statuswords[i][j] == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000333 fprintf(stderr, "corrupted status flag!!\n");
334 return NULL;
335 }
336 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000337
Glenn L McGrathc9005752001-02-10 02:05:24 +0000338 return buf;
339}
340
341/*
342 * Read a control file (or a stanza of a status file) and parse it,
343 * filling parsed fields into the package structure
344 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000345static void control_read(FILE *file, package_t *p)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000346{
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000347 char *line;
348
349 while ((line = get_line_from_file(file)) != NULL) {
350 line[strlen(line)] = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000351
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000352 if (strlen(line) == 0) {
353 break;
354 } else
355 if (strstr(line, "Package: ") == line) {
356 p->package = strdup(line + 9);
357 } else
358 if (strstr(line, "Status: ") == line) {
359 p->status = status_parse(line + 8);
360 } else
361 if (strstr(line, "Depends: ") == line) {
362 p->depends = strdup(line + 9);
363 } else
364 if (strstr(line, "Provides: ") == line) {
365 p->provides = strdup(line + 10);
366 } else
367 if (strstr(line, "Description: ") == line) {
368 p->description = strdup(line + 13);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000369 /* This is specific to the Debian Installer. Ifdef? */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000370 } else
371 if (strstr(line, "installer-menu-item: ") == line) {
372 p->installer_menu_item = atoi(line + 21);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000373 }
374 /* TODO: localized descriptions */
375 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000376 free(line);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000377}
378
Glenn L McGrath649968c2001-02-10 14:26:48 +0000379static void *status_read(void)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000380{
381 FILE *f;
382 void *status = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000383 package_t *m = 0, *p = 0, *t = 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000384
Glenn L McGrath63106462001-02-11 01:40:23 +0000385 if ((f = fopen(statusfile, "r")) == NULL) {
386 perror(statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000387 return 0;
388 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000389
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000390 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000391 printf("(Reading database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000392 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000393
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000394 while (!feof(f)) {
395 m = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000396 memset(m, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000397 control_read(f, m);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000398 if (m->package) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000399 /*
400 * If there is an item in the tree by this name,
401 * it must be a virtual package; insert real
402 * package in preference.
403 */
404 tdelete(m, &status, package_compare);
405 tsearch(m, &status, package_compare);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000406 if (m->provides) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000407 /*
408 * A "Provides" triggers the insertion
409 * of a pseudo package into the status
410 * binary-tree.
411 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000412 p = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000413 memset(p, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000414 p->package = strdup(m->provides);
415
Glenn L McGrath649968c2001-02-10 14:26:48 +0000416 t = *(package_t **)tsearch(p, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000417 if (t != p) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000418 free(p->package);
419 free(p);
420 }
421 else {
422 /*
423 * Pseudo package status is the
424 * same as the status of the
425 * package providing it
426 * FIXME: (not quite right, if 2
427 * packages of different statuses
428 * provide it).
429 */
430 t->status = m->status;
431 }
432 }
433 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000434 else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000435 free(m);
436 }
437 }
438 fclose(f);
439 return status;
440}
441
Glenn L McGrath649968c2001-02-10 14:26:48 +0000442static int status_merge(void *status, package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000443{
444 FILE *fin, *fout;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000445 char *line;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000446 package_t *pkg = 0, *statpkg = 0;
447 package_t locpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000448 int r = 0;
449
Glenn L McGrath63106462001-02-11 01:40:23 +0000450 if ((fin = fopen(statusfile, "r")) == NULL) {
451 perror(statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000452 return 0;
453 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000454 if ((fout = fopen(new_statusfile, "w")) == NULL) {
455 perror(new_statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000456 return 0;
457 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000458 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000459 printf("(Updating database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000460 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000461
462 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000463 line[strlen(line)] = 0; /* trim newline */
Glenn L McGrathc9005752001-02-10 02:05:24 +0000464 /* If we see a package header, find out if it's a package
465 * that we have processed. if so, we skip that block for
466 * now (write it at the end).
467 *
468 * we also look at packages in the status cache and update
469 * their status fields
470 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000471 if (strstr(line, "Package: ") == line) {
472 for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
473 pkg->package, strlen(line) - 9) != 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000474 pkg = pkg->next) ;
475
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000476 locpkg.package = line + 9;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000477 statpkg = tfind(&locpkg, &status, package_compare);
478
479 /* note: statpkg should be non-zero, unless the status
480 * file was changed while we are processing (no locking
481 * is currently done...
482 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000483 if (statpkg != 0) {
484 statpkg = *(package_t **)statpkg;
485 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000486 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000487 if (pkg != 0) {
488 continue;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000489 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000490 if (strstr(line, "Status: ") == line && statpkg != 0) {
491 snprintf(line, sizeof(line), "Status: %s",
492 status_print(statpkg->status));
493 }
494 fputs(line, fout);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000495 fputc('\n', fout);
496 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000497 free(line);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000498
499 // Print out packages we processed.
500 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
501 fprintf(fout, "Package: %s\nStatus: %s\n",
502 pkg->package, status_print(pkg->status));
503 if (pkg->depends)
504 fprintf(fout, "Depends: %s\n", pkg->depends);
505 if (pkg->provides)
506 fprintf(fout, "Provides: %s\n", pkg->provides);
507 if (pkg->installer_menu_item)
508 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
509 if (pkg->description)
510 fprintf(fout, "Description: %s\n", pkg->description);
511 fputc('\n', fout);
512 }
513
514 fclose(fin);
515 fclose(fout);
516
Glenn L McGrath63106462001-02-11 01:40:23 +0000517 r = rename(statusfile, bak_statusfile);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000518 if (r == 0) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000519 r = rename(new_statusfile, statusfile);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000520 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000521
Glenn L McGrathc9005752001-02-10 02:05:24 +0000522 return 0;
523}
524
Glenn L McGrathc9005752001-02-10 02:05:24 +0000525static int is_file(const char *fn)
526{
527 struct stat statbuf;
528
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000529 if (stat(fn, &statbuf) < 0) {
530 return 0;
531 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000532 return S_ISREG(statbuf.st_mode);
533}
534
Glenn L McGrath649968c2001-02-10 14:26:48 +0000535static int dpkg_doconfigure(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000536{
537 int r;
538 char postinst[1024];
539 char buf[1024];
Glenn L McGrath63106462001-02-11 01:40:23 +0000540
Glenn L McGrathc9005752001-02-10 02:05:24 +0000541 DPRINTF("Configuring %s\n", pkg->package);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000542 pkg->status &= status_statusmask;
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000543 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
Glenn L McGrath63106462001-02-11 01:40:23 +0000544
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000545 if (is_file(postinst)) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000546 snprintf(buf, sizeof(buf), "%s configure", postinst);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000547 if ((r = do_system(buf)) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000548 fprintf(stderr, "postinst exited with status %d\n", r);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000549 pkg->status |= status_statushalfconfigured;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000550 return 1;
551 }
552 }
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000553 pkg->status |= status_statusinstalled;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000554
555 return 0;
556}
557
Glenn L McGrath649968c2001-02-10 14:26:48 +0000558static int dpkg_dounpack(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000559{
Glenn L McGrath63106462001-02-11 01:40:23 +0000560 int r = 0, i;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000561 char *cwd, *p;
562 FILE *infp, *outfp;
563 char buf[1024], buf2[1024];
Glenn L McGrathc9005752001-02-10 02:05:24 +0000564 char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
Glenn L McGrath63106462001-02-11 01:40:23 +0000565 "conffiles", "md5sums", "shlibs", "templates" };
Glenn L McGrathc9005752001-02-10 02:05:24 +0000566
567 DPRINTF("Unpacking %s\n", pkg->package);
568
569 cwd = getcwd(0, 0);
570 chdir("/");
571 snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
Glenn L McGrath63106462001-02-11 01:40:23 +0000572 if (SYSTEM(buf) != 0) {
573 goto end;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000574 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000575 /* Installs the package scripts into the info directory */
576 for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
577 snprintf(buf, sizeof(buf), "%s%s/%s",
578 dpkgcidir, pkg->package, adminscripts[i]);
579 snprintf(buf2, sizeof(buf), "%s%s.%s",
580 infodir, pkg->package, adminscripts[i]);
581 if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0) {
582 fprintf(stderr, "Cannot copy %s to %s: %s\n",
583 buf, buf2, strerror(errno));
584 r = 1;
585 break;
586 }
587 /* ugly hack to create the list file; should
588 * probably do something more elegant
589 *
590 * why oh why does dpkg create the list file
591 * so oddly...
592 */
593 snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -tf -", pkg->file);
594 snprintf(buf2, sizeof(buf2), "%s%s.list", infodir, pkg->package);
595 if ((infp = popen(buf, "r")) == NULL || (outfp = fopen(buf2, "w")) == NULL) {
596 fprintf(stderr, "Cannot create %s\n", buf2);
597 r = 1;
598 break;
599 }
600 while (fgets(buf, sizeof(buf), infp) && !feof(infp)) {
601 p = buf;
602 if (*p == '.') {
603 p++;
604 }
605 if ((*p == '/') && (*(p + 1) == '\n')) {
606 *(p + 1) = '.';
607 *(p + 2) = '\n';
608 *(p + 3) = 0;
609 }
610 if (p[strlen(p) - 2] == '/') {
611 p[strlen(p) - 2] = '\n';
612 p[strlen(p) - 1] = 0;
613 }
614 fputs(p, outfp);
615 }
616 fclose(infp);
617 fclose(outfp);
618 }
619
620 pkg->status &= status_wantmask;
621 pkg->status |= status_wantinstall;
622 pkg->status &= status_flagmask;
623 pkg->status |= status_flagok;
624 pkg->status &= status_statusmask;
625
626 if (r == 0) {
627 pkg->status |= status_statusunpacked;
628 } else {
629 pkg->status |= status_statushalfinstalled;
630 }
631end:
Glenn L McGrathc9005752001-02-10 02:05:24 +0000632 chdir(cwd);
633 return r;
634}
635
Glenn L McGrath649968c2001-02-10 14:26:48 +0000636static int dpkg_doinstall(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000637{
638 DPRINTF("Installing %s\n", pkg->package);
639 return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
640}
641
Glenn L McGrath649968c2001-02-10 14:26:48 +0000642static int dpkg_unpackcontrol(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000643{
644 int r = 1;
645 char *cwd = 0;
646 char *p;
647 char buf[1024];
648 FILE *f;
649
650 p = strrchr(pkg->file, '/');
651 if (p) p++; else p = pkg->file;
652 p = pkg->package = strdup(p);
Glenn L McGrath63106462001-02-11 01:40:23 +0000653 while (*p != 0 && *p != '_' && *p != '.') {
654 p++;
655 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000656 *p = 0;
657
658 cwd = getcwd(0, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000659 snprintf(buf, sizeof(buf), "%s%s", dpkgcidir, pkg->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000660 DPRINTF("dir = %s\n", buf);
Glenn L McGrath63106462001-02-11 01:40:23 +0000661
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000662 if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000663 snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -", pkg->file);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000664 if (SYSTEM(buf) == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000665 if ((f = fopen("control", "r")) != NULL) {
666 control_read(f, pkg);
667 r = 0;
668 }
669 }
670 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000671 chdir(cwd);
672 free(cwd);
Glenn L McGrath63106462001-02-11 01:40:23 +0000673
Glenn L McGrathc9005752001-02-10 02:05:24 +0000674 return r;
675}
676
Glenn L McGrath649968c2001-02-10 14:26:48 +0000677static int dpkg_unpack(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000678{
679 int r = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000680 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000681 void *status = status_read();
682
Glenn L McGrath63106462001-02-11 01:40:23 +0000683 if (SYSTEM(rm_dpkgcidir) != 0 ||
684 mkdir(dpkgcidir, S_IRWXU) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000685 perror("mkdir");
686 return 1;
687 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000688 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000689 dpkg_unpackcontrol(pkg);
690 r = dpkg_dounpack(pkg);
691 if (r != 0) break;
692 }
693 status_merge(status, pkgs);
Glenn L McGrath63106462001-02-11 01:40:23 +0000694 SYSTEM(rm_dpkgcidir);
695
Glenn L McGrathc9005752001-02-10 02:05:24 +0000696 return r;
697}
698
Glenn L McGrath649968c2001-02-10 14:26:48 +0000699static int dpkg_configure(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000700{
701 int r = 0;
702 void *found;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000703 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000704 void *status = status_read();
Glenn L McGrath63106462001-02-11 01:40:23 +0000705
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000706 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000707 found = tfind(pkg, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000708
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000709 if (found == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000710 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
711 r = 1;
Glenn L McGrath63106462001-02-11 01:40:23 +0000712 }
713 /* configure the package listed in the status file;
714 * not pkg, as we have info only for the latter
715 */
716 else {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000717 r = dpkg_doconfigure(*(package_t **)found);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000718 }
719 }
720 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000721
Glenn L McGrathc9005752001-02-10 02:05:24 +0000722 return r;
723}
724
Glenn L McGrath649968c2001-02-10 14:26:48 +0000725static int dpkg_install(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000726{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000727 package_t *p, *ordered = 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000728 void *status = status_read();
Glenn L McGrath63106462001-02-11 01:40:23 +0000729
730 if (SYSTEM(rm_dpkgcidir) != 0 ||
731 mkdir(dpkgcidir, S_IRWXU) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000732 perror("mkdir");
733 return 1;
734 }
735
736 /* Stage 1: parse all the control information */
737 for (p = pkgs; p != 0; p = p->next)
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000738 if (dpkg_unpackcontrol(p) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000739 perror(p->file);
740 /* force loop break, and prevents further ops */
741 pkgs = 0;
742 }
743
744 /* Stage 2: resolve dependencies */
745#ifdef DODEPENDS
746 ordered = depends_resolve(pkgs, status);
747#else
748 ordered = pkgs;
749#endif
750
751 /* Stage 3: install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000752 for (p = ordered; p != 0; p = p->next) {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000753 p->status &= status_wantmask;
754 p->status |= status_wantinstall;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000755
756 /* for now the flag is always set to ok... this is probably
757 * not what we want
758 */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000759 p->status &= status_flagmask;
760 p->status |= status_flagok;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000761
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000762 if (dpkg_doinstall(p) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000763 perror(p->file);
764 }
765 }
766
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000767 if (ordered != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000768 status_merge(status, pkgs);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000769 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000770 SYSTEM(rm_dpkgcidir);
771
Glenn L McGrathc9005752001-02-10 02:05:24 +0000772 return 0;
773}
774
Glenn L McGrath649968c2001-02-10 14:26:48 +0000775static int dpkg_remove(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000776{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000777 package_t *p;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000778 void *status = status_read();
Glenn L McGrath63106462001-02-11 01:40:23 +0000779
Glenn L McGrathc9005752001-02-10 02:05:24 +0000780 for (p = pkgs; p != 0; p = p->next)
781 {
782 }
783 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000784
Glenn L McGrathc9005752001-02-10 02:05:24 +0000785 return 0;
786}
787
Glenn L McGrath649968c2001-02-10 14:26:48 +0000788extern int dpkg_main(int argc, char **argv)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000789{
790 char opt = 0;
791 char *s;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000792 package_t *p, *packages = NULL;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000793 char *cwd = getcwd(0, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000794
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000795 while (*++argv) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000796 if (**argv == '-') {
797 /* Nasty little hack to "parse" long options. */
798 s = *argv;
Glenn L McGrath63106462001-02-11 01:40:23 +0000799 while (*s == '-') {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000800 s++;
Glenn L McGrath63106462001-02-11 01:40:23 +0000801 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000802 opt=s[0];
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000803 } else {
804 p = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000805 memset(p, 0, sizeof(package_t));
Glenn L McGrath63106462001-02-11 01:40:23 +0000806
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000807 if (**argv == '/') {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000808 p->file = *argv;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000809 } else
810 if (opt != 'c') {
811 p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
812 sprintf(p->file, "%s/%s", cwd, *argv);
813 } else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000814 p->package = strdup(*argv);
815 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000816
Glenn L McGrathc9005752001-02-10 02:05:24 +0000817 p->next = packages;
818 packages = p;
Glenn L McGrath63106462001-02-11 01:40:23 +0000819 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000820 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000821
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000822 switch (opt) {
823 case 'i':
824 return dpkg_install(packages);
825 case 'r':
826 return dpkg_remove(packages);
827 case 'u':
828 return dpkg_unpack(packages);
829 case 'c':
830 return dpkg_configure(packages);
Glenn L McGrath63106462001-02-11 01:40:23 +0000831 default :
832 usage(dpkg_usage);
833 return 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000834 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000835}