blob: 4e53652e4fbdf446329e3dbdc795a4a3c775bb35 [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
Glenn L McGrath3af1f882001-02-12 11:33:09 +000014#define DEPENDSMAX 64 /* maximum number of depends we can handle */
Glenn L McGrathc9005752001-02-10 02:05:24 +000015
Glenn L McGrath3af1f882001-02-12 11:33:09 +000016/* Should we do full dependency checking? */
Glenn L McGrathc9005752001-02-10 02:05:24 +000017#define DODEPENDS 1
18
Glenn L McGrath3af1f882001-02-12 11:33:09 +000019/* Should we do debugging? */
20#define DODEBUG 1
Glenn L McGrathc9005752001-02-10 02:05:24 +000021
22#ifdef DODEBUG
Glenn L McGrathc9005752001-02-10 02:05:24 +000023#define SYSTEM(x) do_system(x)
24#define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
25#else
Glenn L McGrathc9005752001-02-10 02:05:24 +000026#define SYSTEM(x) system(x)
27#define DPRINTF(fmt,args...) /* nothing */
28#endif
29
Glenn L McGrath3af1f882001-02-12 11:33:09 +000030/* from dpkg-deb.c */
31extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
32static const int dpkg_deb_contents = 1;
33static const int dpkg_deb_control = 2;
34// const int dpkg_deb_info = 4;
35static const int dpkg_deb_extract = 8;
36static const int dpkg_deb_verbose_extract = 16;
37static const int dpkg_deb_list = 32;
Glenn L McGrath510f0dd2001-02-10 14:53:08 +000038
Glenn L McGrath63106462001-02-11 01:40:23 +000039static const char statusfile[] = "/var/lib/dpkg/status.udeb";
40static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
41static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
42
43static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
Glenn L McGrath63106462001-02-11 01:40:23 +000044
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
Eric Andersen3e6ff902001-03-09 21:24:12 +000082static const int color_white = 0;
83static const int color_grey = 1;
84static const 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
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000118static int remove_dpkgcidir()
119{
120 char *rm_dpkgcidir = NULL;
121
122 rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8);
123 strcpy(rm_dpkgcidir, "rm -rf ");
124 strcat(rm_dpkgcidir, dpkgcidir);
125
126 if (SYSTEM(rm_dpkgcidir) != 0) {
127 perror("mkdir ");
128 return EXIT_FAILURE;
129 }
130 return EXIT_SUCCESS;
131}
132
Glenn L McGrathc9005752001-02-10 02:05:24 +0000133#ifdef DODEPENDS
134#include <ctype.h>
135
136static char **depends_split(const char *dependsstr)
137{
138 static char *dependsvec[DEPENDSMAX];
139 char *p;
140 int i = 0;
141
142 dependsvec[0] = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000143 if (dependsstr == 0) {
144 goto end;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000145 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000146
147 p = strdup(dependsstr);
148 while (*p != 0 && *p != '\n') {
149 if (*p != ' ') {
150 if (*p == ',') {
151 *p = 0;
152 dependsvec[++i] = 0;
153 } else {
154 if (dependsvec[i] == 0) {
155 dependsvec[i] = p;
156 }
157 }
158 } else {
159 *p = 0; /* eat the space... */
160 }
161 p++;
162 }
163 *p = 0;
164
165end:
Glenn L McGrathc9005752001-02-10 02:05:24 +0000166 dependsvec[i+1] = 0;
167 return dependsvec;
168}
169
Glenn L McGrath63106462001-02-11 01:40:23 +0000170/* Topological sort algorithm:
171 * ordered is the output list, pkgs is the dependency graph, pkg is
172 * the current node
173 *
174 * recursively add all the adjacent nodes to the ordered list, marking
175 * each one as visited along the way
176 *
177 * yes, this algorithm looks a bit odd when all the params have the
178 * same type :-)
179 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000180static void depends_sort_visit(package_t **ordered, package_t *pkgs,
181 package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000182{
Glenn L McGrathc9005752001-02-10 02:05:24 +0000183 unsigned short i;
184
185 /* mark node as processing */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000186 pkg->color = color_grey;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000187
188 /* visit each not-yet-visited node */
189 for (i = 0; i < pkg->requiredcount; i++)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000190 if (pkg->requiredfor[i]->color == color_white)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000191 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
192
193#if 0
194 /* add it to the list */
Eric Andersenb50d7072001-02-15 19:50:11 +0000195 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000196 /* make a shallow copy */
197 *newnode = *pkg;
198 newnode->next = *ordered;
199 *ordered = newnode;
200#endif
Glenn L McGrath63106462001-02-11 01:40:23 +0000201
Glenn L McGrathc9005752001-02-10 02:05:24 +0000202 pkg->next = *ordered;
203 *ordered = pkg;
204
205 /* mark node as done */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000206 pkg->color = color_black;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000207}
208
Glenn L McGrath649968c2001-02-10 14:26:48 +0000209static package_t *depends_sort(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000210{
211 /* TODO: it needs to break cycles in the to-be-installed package
212 * graph... */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000213 package_t *ordered = NULL;
214 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000215
Glenn L McGrath63106462001-02-11 01:40:23 +0000216 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000217 pkg->color = color_white;
Glenn L McGrath63106462001-02-11 01:40:23 +0000218 }
219 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
220 if (pkg->color == color_white) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000221 depends_sort_visit(&ordered, pkgs, pkg);
Glenn L McGrath63106462001-02-11 01:40:23 +0000222 }
223 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000224
225 /* Leaks the old list... return the new one... */
226 return ordered;
227}
228
229
230/* resolve package dependencies --
231 * for each package in the list of packages to be installed, we parse its
232 * dependency info to determine if the dependent packages are either
233 * already installed, or are scheduled to be installed. If both tests fail
234 * than bail.
235 *
236 * The algorithm here is O(n^2*m) where n = number of packages to be
237 * installed and m is the # of dependencies per package. Not a terribly
238 * efficient algorithm, but given that at any one time you are unlikely
239 * to install a very large number of packages it doesn't really matter
240 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000241static package_t *depends_resolve(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000242{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000243 package_t *pkg, *chk;
244 package_t dependpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000245 char **dependsvec;
246 int i;
247 void *found;
248
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000249 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000250 dependsvec = depends_split(pkg->depends);
251 i = 0;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000252 while (dependsvec[i] != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000253 /* Check for dependencies; first look for installed packages */
254 dependpkg.package = dependsvec[i];
255 if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
Glenn L McGrath649968c2001-02-10 14:26:48 +0000256 ((chk = *(package_t **)found) &&
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000257 (chk->status & (status_flagok | status_statusinstalled)) !=
Glenn L McGrath63106462001-02-11 01:40:23 +0000258 (status_flagok | status_statusinstalled))) {
259
Glenn L McGrathc9005752001-02-10 02:05:24 +0000260 /* if it fails, we look through the list of packages we are going to
261 * install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000262 for (chk = pkgs; chk != 0; chk = chk->next) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000263 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000264 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
265 if (chk->requiredcount >= DEPENDSMAX) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000266 fprintf(stderr, "Too many dependencies for %s\n", chk->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000267 return 0;
268 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000269 if (chk != pkg) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000270 chk->requiredfor[chk->requiredcount++] = pkg;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000271 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000272 break;
273 }
274 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000275 if (chk == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000276 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
277 return 0;
278 }
279 }
280 i++;
281 }
282 }
283
284 return depends_sort(pkgs);
285}
286#endif
287
288/* Status file handling routines
289 *
290 * This is a fairly minimalistic implementation. there are two main functions
291 * that are supported:
292 *
293 * 1) reading the entire status file:
294 * the status file is read into memory as a binary-tree, with just the
295 * package and status info preserved
296 *
297 * 2) merging the status file
298 * control info from (new) packages is merged into the status file,
299 * replacing any pre-existing entries. when a merge happens, status info
300 * read using the status_read function is written back to the status file
301 */
Glenn L McGrathc9005752001-02-10 02:05:24 +0000302static unsigned long status_parse(const char *line)
303{
304 char *p;
305 int i, j;
306 unsigned long l = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000307
308 for (i = 0; i < 3; i++) {
309 if ((p = strchr(line, ' ')) != NULL) {
310 *p = 0;
311 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000312 j = 1;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000313 while (statuswords[i][j] != 0) {
314 if (strcmp(line, statuswords[i][j]) == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000315 l |= (1 << ((int)statuswords[i][0] + j - 1));
316 break;
317 }
318 j++;
319 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000320 /* parse error */
321 if (statuswords[i][j] == 0) {
322 return 0;
323 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000324 line = p+1;
325 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000326
Glenn L McGrathc9005752001-02-10 02:05:24 +0000327 return l;
328}
329
330static const char *status_print(unsigned long flags)
331{
332 /* this function returns a static buffer... */
333 static char buf[256];
334 int i, j;
335
336 buf[0] = 0;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000337 for (i = 0; i < 3; i++) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000338 j = 1;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000339 while (statuswords[i][j] != 0) {
340 if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000341 strcat(buf, statuswords[i][j]);
342 if (i < 2) strcat(buf, " ");
343 break;
344 }
345 j++;
346 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000347 if (statuswords[i][j] == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000348 fprintf(stderr, "corrupted status flag!!\n");
349 return NULL;
350 }
351 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000352
Glenn L McGrathc9005752001-02-10 02:05:24 +0000353 return buf;
354}
355
356/*
357 * Read a control file (or a stanza of a status file) and parse it,
358 * filling parsed fields into the package structure
359 */
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000360static int control_read(FILE *file, package_t *p)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000361{
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000362 char *line;
363
364 while ((line = get_line_from_file(file)) != NULL) {
365 line[strlen(line)] = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000366
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000367 if (strlen(line) == 0) {
368 break;
369 } else
370 if (strstr(line, "Package: ") == line) {
371 p->package = strdup(line + 9);
372 } else
373 if (strstr(line, "Status: ") == line) {
374 p->status = status_parse(line + 8);
375 } else
376 if (strstr(line, "Depends: ") == line) {
377 p->depends = strdup(line + 9);
378 } else
379 if (strstr(line, "Provides: ") == line) {
380 p->provides = strdup(line + 10);
381 } else
382 if (strstr(line, "Description: ") == line) {
383 p->description = strdup(line + 13);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000384 /* This is specific to the Debian Installer. Ifdef? */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000385 } else
386 if (strstr(line, "installer-menu-item: ") == line) {
387 p->installer_menu_item = atoi(line + 21);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000388 }
389 /* TODO: localized descriptions */
390 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000391 free(line);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000392 return EXIT_SUCCESS;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000393}
394
Glenn L McGrath649968c2001-02-10 14:26:48 +0000395static void *status_read(void)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000396{
397 FILE *f;
398 void *status = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000399 package_t *m = 0, *p = 0, *t = 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000400
Glenn L McGrath63106462001-02-11 01:40:23 +0000401 if ((f = fopen(statusfile, "r")) == NULL) {
402 perror(statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000403 return 0;
404 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000405
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000406 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000407 printf("(Reading database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000408 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000409
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000410 while (!feof(f)) {
411 m = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000412 memset(m, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000413 control_read(f, m);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000414 if (m->package) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000415 /*
416 * If there is an item in the tree by this name,
417 * it must be a virtual package; insert real
418 * package in preference.
419 */
420 tdelete(m, &status, package_compare);
421 tsearch(m, &status, package_compare);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000422 if (m->provides) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000423 /*
424 * A "Provides" triggers the insertion
425 * of a pseudo package into the status
426 * binary-tree.
427 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000428 p = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000429 memset(p, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000430 p->package = strdup(m->provides);
431
Glenn L McGrath649968c2001-02-10 14:26:48 +0000432 t = *(package_t **)tsearch(p, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000433 if (t != p) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000434 free(p->package);
435 free(p);
436 }
437 else {
438 /*
439 * Pseudo package status is the
440 * same as the status of the
441 * package providing it
442 * FIXME: (not quite right, if 2
443 * packages of different statuses
444 * provide it).
445 */
446 t->status = m->status;
447 }
448 }
449 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000450 else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000451 free(m);
452 }
453 }
454 fclose(f);
455 return status;
456}
457
Glenn L McGrath649968c2001-02-10 14:26:48 +0000458static int status_merge(void *status, package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000459{
460 FILE *fin, *fout;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000461 char *line;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000462 package_t *pkg = 0, *statpkg = 0;
463 package_t locpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000464 int r = 0;
465
Glenn L McGrath63106462001-02-11 01:40:23 +0000466 if ((fin = fopen(statusfile, "r")) == NULL) {
467 perror(statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000468 return 0;
469 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000470 if ((fout = fopen(new_statusfile, "w")) == NULL) {
471 perror(new_statusfile);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000472 return 0;
473 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000474 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000475 printf("(Updating database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000476 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000477
478 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000479 line[strlen(line)] = 0; /* trim newline */
Glenn L McGrathc9005752001-02-10 02:05:24 +0000480 /* If we see a package header, find out if it's a package
481 * that we have processed. if so, we skip that block for
482 * now (write it at the end).
483 *
484 * we also look at packages in the status cache and update
485 * their status fields
486 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000487 if (strstr(line, "Package: ") == line) {
488 for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
489 pkg->package, strlen(line) - 9) != 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000490 pkg = pkg->next) ;
491
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000492 locpkg.package = line + 9;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000493 statpkg = tfind(&locpkg, &status, package_compare);
494
495 /* note: statpkg should be non-zero, unless the status
496 * file was changed while we are processing (no locking
497 * is currently done...
498 */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000499 if (statpkg != 0) {
500 statpkg = *(package_t **)statpkg;
501 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000502 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000503 if (pkg != 0) {
504 continue;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000505 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000506 if (strstr(line, "Status: ") == line && statpkg != 0) {
507 snprintf(line, sizeof(line), "Status: %s",
508 status_print(statpkg->status));
509 }
510 fputs(line, fout);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000511 fputc('\n', fout);
512 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000513 free(line);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000514
515 // Print out packages we processed.
516 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
517 fprintf(fout, "Package: %s\nStatus: %s\n",
518 pkg->package, status_print(pkg->status));
519 if (pkg->depends)
520 fprintf(fout, "Depends: %s\n", pkg->depends);
521 if (pkg->provides)
522 fprintf(fout, "Provides: %s\n", pkg->provides);
523 if (pkg->installer_menu_item)
524 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
525 if (pkg->description)
526 fprintf(fout, "Description: %s\n", pkg->description);
527 fputc('\n', fout);
528 }
529
530 fclose(fin);
531 fclose(fout);
532
Glenn L McGrath63106462001-02-11 01:40:23 +0000533 r = rename(statusfile, bak_statusfile);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000534 if (r == 0) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000535 r = rename(new_statusfile, statusfile);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000536 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000537
Glenn L McGrathc9005752001-02-10 02:05:24 +0000538 return 0;
539}
540
Glenn L McGrathc9005752001-02-10 02:05:24 +0000541static int is_file(const char *fn)
542{
543 struct stat statbuf;
544
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000545 if (stat(fn, &statbuf) < 0) {
546 return 0;
547 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000548 return S_ISREG(statbuf.st_mode);
549}
550
Glenn L McGrath649968c2001-02-10 14:26:48 +0000551static int dpkg_doconfigure(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000552{
553 int r;
554 char postinst[1024];
555 char buf[1024];
Glenn L McGrath63106462001-02-11 01:40:23 +0000556
Glenn L McGrathc9005752001-02-10 02:05:24 +0000557 DPRINTF("Configuring %s\n", pkg->package);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000558 pkg->status &= status_statusmask;
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000559 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
Glenn L McGrath63106462001-02-11 01:40:23 +0000560
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000561 if (is_file(postinst)) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000562 snprintf(buf, sizeof(buf), "%s configure", postinst);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000563 if ((r = do_system(buf)) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000564 fprintf(stderr, "postinst exited with status %d\n", r);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000565 pkg->status |= status_statushalfconfigured;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000566 return 1;
567 }
568 }
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000569 pkg->status |= status_statusinstalled;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000570
571 return 0;
572}
573
Glenn L McGrath649968c2001-02-10 14:26:48 +0000574static int dpkg_dounpack(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000575{
Glenn L McGrath63106462001-02-11 01:40:23 +0000576 int r = 0, i;
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000577 char *cwd;
578 FILE *outfp;
579 char *src_file = NULL;
580 char *dst_file = NULL;
581 char *lst_file = NULL;
582 char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst",
583 "/conffiles", "/md5sums", "/shlibs", "/templates" };
Glenn L McGrathc9005752001-02-10 02:05:24 +0000584 char buf[1024], buf2[1024];
Glenn L McGrathc9005752001-02-10 02:05:24 +0000585
586 DPRINTF("Unpacking %s\n", pkg->package);
587
588 cwd = getcwd(0, 0);
589 chdir("/");
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000590 deb_extract(dpkg_deb_extract, "/", pkg->file);
591
Glenn L McGrath63106462001-02-11 01:40:23 +0000592 /* Installs the package scripts into the info directory */
593 for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000594 /* The full path of the current location of the admin file */
595 src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
596 strcpy(src_file, dpkgcidir);
597 strcat(src_file, pkg->package);
598 strcat(src_file, adminscripts[i]);
599
600 /* the full path of where we want the file to be copied to */
601 dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
602 strcpy(dst_file, infodir);
603 strcat(dst_file, pkg->package);
604 strcat(dst_file, adminscripts[i]);
605
606 /* copy admin file to permanent home */
607 if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) {
608 error_msg_and_die("Cannot copy %s to %s ", buf, buf2);
Glenn L McGrath63106462001-02-11 01:40:23 +0000609 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000610
611 /* create the list file */
Eric Andersenb50d7072001-02-15 19:50:11 +0000612 lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000613 strcpy(lst_file, infodir);
614 strcat(lst_file, pkg->package);
615 strcat(lst_file, ".list");
616 outfp = freopen(lst_file, "w", stdout);
617 deb_extract(dpkg_deb_list, NULL, pkg->file);
618 stdout = freopen(NULL, "w", outfp);
619
620 printf("done\n");
621 getchar();
622
Glenn L McGrath63106462001-02-11 01:40:23 +0000623 fclose(outfp);
624 }
625
626 pkg->status &= status_wantmask;
627 pkg->status |= status_wantinstall;
628 pkg->status &= status_flagmask;
629 pkg->status |= status_flagok;
630 pkg->status &= status_statusmask;
631
632 if (r == 0) {
633 pkg->status |= status_statusunpacked;
634 } else {
635 pkg->status |= status_statushalfinstalled;
636 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000637 chdir(cwd);
638 return r;
639}
640
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000641/*
642 * Extract and parse the control.tar.gz from the specified package
643 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000644static int dpkg_unpackcontrol(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000645{
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000646 char *tmp_name;
647 FILE *file;
648 int length;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000649
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000650 /* clean the temp directory (dpkgcidir) be recreating it */
651 remove_dpkgcidir();
652 if (mkdir(dpkgcidir, S_IRWXU) != 0) {
653 perror("mkdir");
654 return EXIT_FAILURE;
Glenn L McGrath63106462001-02-11 01:40:23 +0000655 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000656
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000657 /*
658 * Get the package name from the file name,
659 * first remove the directories
660 */
661 if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
662 tmp_name = pkg->file;
663 } else {
664 tmp_name++;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000665 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000666 /* now remove trailing version numbers etc */
667 length = strcspn(tmp_name, "_.");
668 pkg->package = (char *) xmalloc(length + 1);
669 /* store the package name */
670 strncpy(pkg->package, tmp_name, length);
Glenn L McGrath63106462001-02-11 01:40:23 +0000671
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000672 /* work out the full extraction path */
673 tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9);
674 memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
675 strcpy(tmp_name, dpkgcidir);
676 strcat(tmp_name, pkg->package);
677
678 /* extract control.tar.gz to the full extraction path */
679 deb_extract(dpkg_deb_control, tmp_name, pkg->file);
680
681 /* parse the extracted control file */
682 strcat(tmp_name, "/control");
683 if ((file = fopen(tmp_name, "r")) == NULL) {
684 return EXIT_FAILURE;
685 }
686 if (control_read(file, pkg) == EXIT_FAILURE) {
687 return EXIT_FAILURE;
688 }
689
690 return EXIT_SUCCESS;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000691}
692
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000693static int dpkg_unpack(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000694{
695 int r = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000696 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000697
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000698 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000699 if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
700 return EXIT_FAILURE;
701 }
702 if ((r = dpkg_dounpack(pkg)) != 0 ) {
703 break;
704 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000705 }
706 status_merge(status, pkgs);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000707 remove_dpkgcidir();
Glenn L McGrath63106462001-02-11 01:40:23 +0000708
Glenn L McGrathc9005752001-02-10 02:05:24 +0000709 return r;
710}
711
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000712static int dpkg_configure(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000713{
714 int r = 0;
715 void *found;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000716 package_t *pkg;
Glenn L McGrath63106462001-02-11 01:40:23 +0000717
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000718 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000719 found = tfind(pkg, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000720
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000721 if (found == 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000722 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
723 r = 1;
Glenn L McGrath63106462001-02-11 01:40:23 +0000724 }
725 /* configure the package listed in the status file;
726 * not pkg, as we have info only for the latter
727 */
728 else {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000729 r = dpkg_doconfigure(*(package_t **)found);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000730 }
731 }
732 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000733
Glenn L McGrathc9005752001-02-10 02:05:24 +0000734 return r;
735}
736
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000737static int dpkg_install(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000738{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000739 package_t *p, *ordered = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000740
Glenn L McGrathc9005752001-02-10 02:05:24 +0000741 /* Stage 1: parse all the control information */
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000742 for (p = pkgs; p != 0; p = p->next) {
743 if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000744 perror(p->file);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000745 return EXIT_FAILURE;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000746 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000747 }
748
Glenn L McGrathc9005752001-02-10 02:05:24 +0000749 /* Stage 2: resolve dependencies */
750#ifdef DODEPENDS
751 ordered = depends_resolve(pkgs, status);
752#else
753 ordered = pkgs;
754#endif
755
756 /* Stage 3: install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000757 for (p = ordered; p != 0; p = p->next) {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000758 p->status &= status_wantmask;
759 p->status |= status_wantinstall;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000760
761 /* for now the flag is always set to ok... this is probably
762 * not what we want
763 */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000764 p->status &= status_flagmask;
765 p->status |= status_flagok;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000766
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000767 DPRINTF("Installing %s\n", p->package);
768 if (dpkg_dounpack(p) != 0) {
769 perror(p->file);
770 }
771 if (dpkg_doconfigure(p) != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000772 perror(p->file);
773 }
774 }
775
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000776 if (ordered != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000777 status_merge(status, pkgs);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000778 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000779 remove_dpkgcidir();
Glenn L McGrath63106462001-02-11 01:40:23 +0000780
Glenn L McGrathc9005752001-02-10 02:05:24 +0000781 return 0;
782}
783
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000784static int dpkg_remove(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000785{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000786 package_t *p;
Glenn L McGrath63106462001-02-11 01:40:23 +0000787
Glenn L McGrathc9005752001-02-10 02:05:24 +0000788 for (p = pkgs; p != 0; p = p->next)
789 {
790 }
791 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000792
Glenn L McGrathc9005752001-02-10 02:05:24 +0000793 return 0;
794}
795
Glenn L McGrath649968c2001-02-10 14:26:48 +0000796extern int dpkg_main(int argc, char **argv)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000797{
798 char opt = 0;
799 char *s;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000800 package_t *p, *packages = NULL;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000801 char *cwd = getcwd(0, 0);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000802 void *status = NULL;
Glenn L McGrath63106462001-02-11 01:40:23 +0000803
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000804 while (*++argv) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000805 if (**argv == '-') {
806 /* Nasty little hack to "parse" long options. */
807 s = *argv;
Glenn L McGrath63106462001-02-11 01:40:23 +0000808 while (*s == '-') {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000809 s++;
Glenn L McGrath63106462001-02-11 01:40:23 +0000810 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000811 opt=s[0];
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000812 } else {
813 p = (package_t *)xmalloc(sizeof(package_t));
Glenn L McGrath649968c2001-02-10 14:26:48 +0000814 memset(p, 0, sizeof(package_t));
Glenn L McGrath63106462001-02-11 01:40:23 +0000815
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000816 if (**argv == '/') {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000817 p->file = *argv;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000818 } else
819 if (opt != 'c') {
820 p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
821 sprintf(p->file, "%s/%s", cwd, *argv);
822 } else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000823 p->package = strdup(*argv);
824 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000825
Glenn L McGrathc9005752001-02-10 02:05:24 +0000826 p->next = packages;
827 packages = p;
Glenn L McGrath63106462001-02-11 01:40:23 +0000828 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000829 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000830
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000831 status = status_read();
832
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000833 switch (opt) {
834 case 'i':
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000835 return dpkg_install(packages, status);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000836 case 'r':
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000837 return dpkg_remove(packages, status);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000838 case 'u':
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000839 return dpkg_unpack(packages, status);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000840 case 'c':
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000841 return dpkg_configure(packages, status);
842 default :
Eric Andersen67991cf2001-02-14 21:23:06 +0000843 show_usage();
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000844 return EXIT_FAILURE;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000845 }
Eric Andersen67991cf2001-02-14 21:23:06 +0000846}