blob: 24b02041a950a66dd275a4cd7f068471f3691eef [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 McGrathc9005752001-02-10 02:05:24 +000038#define ADMINDIR "/var/lib/dpkg"
Glenn L McGrath649968c2001-02-10 14:26:48 +000039#define STATUSFILE ADMINDIR ## "/status.udeb"
Glenn L McGrathc9005752001-02-10 02:05:24 +000040#define DPKGCIDIR ADMINDIR ## "/tmp.ci/"
Glenn L McGrath510f0dd2001-02-10 14:53:08 +000041static const char infodir[] = "/var/lib/dpkg/info/";
42static const char udpkg_quiet[] = "UDPKG_QUIET";
Glenn L McGrathc9005752001-02-10 02:05:24 +000043
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000044//static const int status_wantstart = 0;
45//static const int status_wantunknown = (1 << 0);
46static const int status_wantinstall = (1 << 1);
47//static const int status_wanthold = (1 << 2);
48//static const int status_wantdeinstall = (1 << 3);
49//static const int status_wantpurge = (1 << 4);
50static const int status_wantmask = 31;
Glenn L McGrathc9005752001-02-10 02:05:24 +000051
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000052//static const int status_flagstart = 5;
53static const int status_flagok = (1 << 5); /* 32 */
54//static const int status_flagreinstreq = (1 << 6);
55//static const int status_flaghold = (1 << 7);
56//static const int status_flagholdreinstreq = (1 << 8);
57static const int status_flagmask = 480;
Glenn L McGrathc9005752001-02-10 02:05:24 +000058
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000059//static const int status_statusstart = 9;
60//static const int status_statusnoninstalled = (1 << 9); /* 512 */
61static const int status_statusunpacked = (1 << 10);
62static const int status_statushalfconfigured = (1 << 11);
63static const int status_statusinstalled = (1 << 12);
64static const int status_statushalfinstalled = (1 << 13);
65//static const int status_statusconfigfiles = (1 << 14);
66//static const int status_statuspostinstfailed = (1 << 15);
67//static const int status_statusremovalfailed = (1 << 16);
68static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */
Glenn L McGrathc9005752001-02-10 02:05:24 +000069
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000070static const char *statuswords[][10] = {
71 { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
72 { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
73 { (char *) 9, "not-installed", "unpacked", "half-configured",
74 "installed", "half-installed", "config-files",
75 "post-inst-failed", "removal-failed", 0 }
76};
77
78const int color_white = 0;
79const int color_grey = 1;
80const int color_black = 2;
Glenn L McGrathc9005752001-02-10 02:05:24 +000081
82/* data structures */
Glenn L McGrath649968c2001-02-10 14:26:48 +000083typedef struct package_s {
Glenn L McGrathc9005752001-02-10 02:05:24 +000084 char *file;
85 char *package;
86 char *version;
87 char *depends;
88 char *provides;
89 char *description;
90 int installer_menu_item;
91 unsigned long status;
92 char color; /* for topo-sort */
Glenn L McGrath649968c2001-02-10 14:26:48 +000093 struct package_s *requiredfor[DEPENDSMAX];
Glenn L McGrathc9005752001-02-10 02:05:24 +000094 unsigned short requiredcount;
Glenn L McGrath649968c2001-02-10 14:26:48 +000095 struct package_s *next;
96} package_t;
Glenn L McGrathc9005752001-02-10 02:05:24 +000097
Glenn L McGrath649968c2001-02-10 14:26:48 +000098static int package_compare(const void *p1, const void *p2)
99{
100 return strcmp(((package_t *)p1)->package,
101 ((package_t *)p2)->package);
102}
Glenn L McGrathc9005752001-02-10 02:05:24 +0000103
104#ifdef DODEPENDS
105#include <ctype.h>
106
107static char **depends_split(const char *dependsstr)
108{
109 static char *dependsvec[DEPENDSMAX];
110 char *p;
111 int i = 0;
112
113 dependsvec[0] = 0;
114
115 if (dependsstr != 0)
116 {
117 p = strdup(dependsstr);
118 while (*p != 0 && *p != '\n')
119 {
120 if (*p != ' ')
121 {
122 if (*p == ',')
123 {
124 *p = 0;
125 dependsvec[++i] = 0;
126 }
127 else if (dependsvec[i] == 0)
128 dependsvec[i] = p;
129 }
130 else
131 *p = 0; /* eat the space... */
132 p++;
133 }
134 *p = 0;
135 }
136 dependsvec[i+1] = 0;
137 return dependsvec;
138}
139
Glenn L McGrath649968c2001-02-10 14:26:48 +0000140static void depends_sort_visit(package_t **ordered, package_t *pkgs,
141 package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000142{
143 /* Topological sort algorithm:
144 * ordered is the output list, pkgs is the dependency graph, pkg is
145 * the current node
146 *
147 * recursively add all the adjacent nodes to the ordered list, marking
148 * each one as visited along the way
149 *
150 * yes, this algorithm looks a bit odd when all the params have the
151 * same type :-)
152 */
153 unsigned short i;
154
155 /* mark node as processing */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000156 pkg->color = color_grey;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000157
158 /* visit each not-yet-visited node */
159 for (i = 0; i < pkg->requiredcount; i++)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000160 if (pkg->requiredfor[i]->color == color_white)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000161 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
162
163#if 0
164 /* add it to the list */
165 newnode = (struct package_t *)malloc(sizeof(struct package_t));
166 /* make a shallow copy */
167 *newnode = *pkg;
168 newnode->next = *ordered;
169 *ordered = newnode;
170#endif
171 pkg->next = *ordered;
172 *ordered = pkg;
173
174 /* mark node as done */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000175 pkg->color = color_black;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000176}
177
Glenn L McGrath649968c2001-02-10 14:26:48 +0000178static package_t *depends_sort(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000179{
180 /* TODO: it needs to break cycles in the to-be-installed package
181 * graph... */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000182 package_t *ordered = NULL;
183 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000184
185 for (pkg = pkgs; pkg != 0; pkg = pkg->next)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000186 pkg->color = color_white;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000187
188 for (pkg = pkgs; pkg != 0; pkg = pkg->next)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000189 if (pkg->color == color_white)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000190 depends_sort_visit(&ordered, pkgs, pkg);
191
192 /* Leaks the old list... return the new one... */
193 return ordered;
194}
195
196
197/* resolve package dependencies --
198 * for each package in the list of packages to be installed, we parse its
199 * dependency info to determine if the dependent packages are either
200 * already installed, or are scheduled to be installed. If both tests fail
201 * than bail.
202 *
203 * The algorithm here is O(n^2*m) where n = number of packages to be
204 * installed and m is the # of dependencies per package. Not a terribly
205 * efficient algorithm, but given that at any one time you are unlikely
206 * to install a very large number of packages it doesn't really matter
207 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000208static package_t *depends_resolve(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000209{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000210 package_t *pkg, *chk;
211 package_t dependpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000212 char **dependsvec;
213 int i;
214 void *found;
215
216 for (pkg = pkgs; pkg != 0; pkg = pkg->next)
217 {
218 dependsvec = depends_split(pkg->depends);
219 i = 0;
220 while (dependsvec[i] != 0)
221 {
222 /* Check for dependencies; first look for installed packages */
223 dependpkg.package = dependsvec[i];
224 if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
Glenn L McGrath649968c2001-02-10 14:26:48 +0000225 ((chk = *(package_t **)found) &&
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000226 (chk->status & (status_flagok | status_statusinstalled)) !=
227 (status_flagok | status_statusinstalled)))
Glenn L McGrathc9005752001-02-10 02:05:24 +0000228 {
229 /* if it fails, we look through the list of packages we are going to
230 * install */
231 for (chk = pkgs; chk != 0; chk = chk->next)
232 {
233 if (strcmp(chk->package, dependsvec[i]) == 0 ||
234 (chk->provides &&
235 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0))
236 {
237 if (chk->requiredcount >= DEPENDSMAX)
238 {
239 fprintf(stderr, "Too many dependencies for %s\n",
240 chk->package);
241 return 0;
242 }
243 if (chk != pkg)
244 chk->requiredfor[chk->requiredcount++] = pkg;
245 break;
246 }
247 }
248 if (chk == 0)
249 {
250 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
251 return 0;
252 }
253 }
254 i++;
255 }
256 }
257
258 return depends_sort(pkgs);
259}
260#endif
261
262/* Status file handling routines
263 *
264 * This is a fairly minimalistic implementation. there are two main functions
265 * that are supported:
266 *
267 * 1) reading the entire status file:
268 * the status file is read into memory as a binary-tree, with just the
269 * package and status info preserved
270 *
271 * 2) merging the status file
272 * control info from (new) packages is merged into the status file,
273 * replacing any pre-existing entries. when a merge happens, status info
274 * read using the status_read function is written back to the status file
275 */
276
Glenn L McGrathc9005752001-02-10 02:05:24 +0000277static unsigned long status_parse(const char *line)
278{
279 char *p;
280 int i, j;
281 unsigned long l = 0;
282 for (i = 0; i < 3; i++)
283 {
284 p = strchr(line, ' ');
285 if (p) *p = 0;
286 j = 1;
287 while (statuswords[i][j] != 0)
288 {
289 if (strcmp(line, statuswords[i][j]) == 0)
290 {
291 l |= (1 << ((int)statuswords[i][0] + j - 1));
292 break;
293 }
294 j++;
295 }
296 if (statuswords[i][j] == 0) return 0; /* parse error */
297 line = p+1;
298 }
299 return l;
300}
301
302static const char *status_print(unsigned long flags)
303{
304 /* this function returns a static buffer... */
305 static char buf[256];
306 int i, j;
307
308 buf[0] = 0;
309 for (i = 0; i < 3; i++)
310 {
311 j = 1;
312 while (statuswords[i][j] != 0)
313 {
314 if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0)
315 {
316 strcat(buf, statuswords[i][j]);
317 if (i < 2) strcat(buf, " ");
318 break;
319 }
320 j++;
321 }
322 if (statuswords[i][j] == 0)
323 {
324 fprintf(stderr, "corrupted status flag!!\n");
325 return NULL;
326 }
327 }
328 return buf;
329}
330
331/*
332 * Read a control file (or a stanza of a status file) and parse it,
333 * filling parsed fields into the package structure
334 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000335static void control_read(FILE *f, package_t *p)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000336{
337 char buf[BUFSIZE];
338 while (fgets(buf, BUFSIZE, f) && !feof(f))
339 {
340 buf[strlen(buf)-1] = 0;
341 if (*buf == 0)
342 return;
343 else if (strstr(buf, "Package: ") == buf)
344 {
345 p->package = strdup(buf+9);
346 }
347 else if (strstr(buf, "Status: ") == buf)
348 {
349 p->status = status_parse(buf+8);
350 }
351 else if (strstr(buf, "Depends: ") == buf)
352 {
353 p->depends = strdup(buf+9);
354 }
355 else if (strstr(buf, "Provides: ") == buf)
356 {
357 p->provides = strdup(buf+10);
358 }
359 /* This is specific to the Debian Installer. Ifdef? */
360 else if (strstr(buf, "installer-menu-item: ") == buf)
361 {
362 p->installer_menu_item = atoi(buf+21);
363 }
364 else if (strstr(buf, "Description: ") == buf)
365 {
366 p->description = strdup(buf+13);
367 }
368 /* TODO: localized descriptions */
369 }
370}
371
Glenn L McGrath649968c2001-02-10 14:26:48 +0000372static void *status_read(void)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000373{
374 FILE *f;
375 void *status = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000376 package_t *m = 0, *p = 0, *t = 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000377
378 if ((f = fopen(STATUSFILE, "r")) == NULL)
379 {
380 perror(STATUSFILE);
381 return 0;
382 }
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000383 if (getenv(udpkg_quiet) == NULL)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000384 printf("(Reading database...)\n");
385 while (!feof(f))
386 {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000387 m = (package_t *)malloc(sizeof(package_t));
388 memset(m, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000389 control_read(f, m);
390 if (m->package)
391 {
392 /*
393 * If there is an item in the tree by this name,
394 * it must be a virtual package; insert real
395 * package in preference.
396 */
397 tdelete(m, &status, package_compare);
398 tsearch(m, &status, package_compare);
399 if (m->provides)
400 {
401 /*
402 * A "Provides" triggers the insertion
403 * of a pseudo package into the status
404 * binary-tree.
405 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000406 p = (package_t *)malloc(sizeof(package_t));
407 memset(p, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000408 p->package = strdup(m->provides);
409
Glenn L McGrath649968c2001-02-10 14:26:48 +0000410 t = *(package_t **)tsearch(p, &status, package_compare);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000411 if (!(t == p))
412 {
413 free(p->package);
414 free(p);
415 }
416 else {
417 /*
418 * Pseudo package status is the
419 * same as the status of the
420 * package providing it
421 * FIXME: (not quite right, if 2
422 * packages of different statuses
423 * provide it).
424 */
425 t->status = m->status;
426 }
427 }
428 }
429 else
430 {
431 free(m);
432 }
433 }
434 fclose(f);
435 return status;
436}
437
Glenn L McGrath649968c2001-02-10 14:26:48 +0000438static int status_merge(void *status, package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000439{
440 FILE *fin, *fout;
441 char buf[BUFSIZE];
Glenn L McGrath649968c2001-02-10 14:26:48 +0000442 package_t *pkg = 0, *statpkg = 0;
443 package_t locpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000444 int r = 0;
445
446 if ((fin = fopen(STATUSFILE, "r")) == NULL)
447 {
448 perror(STATUSFILE);
449 return 0;
450 }
451 if ((fout = fopen(STATUSFILE ".new", "w")) == NULL)
452 {
453 perror(STATUSFILE ".new");
454 return 0;
455 }
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000456 if (getenv(udpkg_quiet) == NULL)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000457 printf("(Updating database...)\n");
458 while (fgets(buf, BUFSIZE, fin) && !feof(fin))
459 {
460 buf[strlen(buf)-1] = 0; /* trim newline */
461 /* If we see a package header, find out if it's a package
462 * that we have processed. if so, we skip that block for
463 * now (write it at the end).
464 *
465 * we also look at packages in the status cache and update
466 * their status fields
467 */
468 if (strstr(buf, "Package: ") == buf)
469 {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000470 for (pkg = pkgs; pkg != 0 && strncmp(buf + 9,
471 pkg->package, strlen(buf) - 9)!=0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000472 pkg = pkg->next) ;
473
474 locpkg.package = buf+9;
475 statpkg = tfind(&locpkg, &status, package_compare);
476
477 /* note: statpkg should be non-zero, unless the status
478 * file was changed while we are processing (no locking
479 * is currently done...
480 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000481 if (statpkg != 0) statpkg = *(package_t **)statpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000482 }
483 if (pkg != 0) continue;
484
485 if (strstr(buf, "Status: ") == buf && statpkg != 0)
486 {
487 snprintf(buf, sizeof(buf), "Status: %s",
488 status_print(statpkg->status));
489 }
490 fputs(buf, fout);
491 fputc('\n', fout);
492 }
493
494 // Print out packages we processed.
495 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
496 fprintf(fout, "Package: %s\nStatus: %s\n",
497 pkg->package, status_print(pkg->status));
498 if (pkg->depends)
499 fprintf(fout, "Depends: %s\n", pkg->depends);
500 if (pkg->provides)
501 fprintf(fout, "Provides: %s\n", pkg->provides);
502 if (pkg->installer_menu_item)
503 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
504 if (pkg->description)
505 fprintf(fout, "Description: %s\n", pkg->description);
506 fputc('\n', fout);
507 }
508
509 fclose(fin);
510 fclose(fout);
511
512 r = rename(STATUSFILE, STATUSFILE ".bak");
513 if (r == 0) r = rename(STATUSFILE ".new", STATUSFILE);
514 return 0;
515}
516
Glenn L McGrathc9005752001-02-10 02:05:24 +0000517/*
518 * Main udpkg implementation routines
519 */
520
521#ifdef DODEBUG
522static int do_system(const char *cmd)
523{
524 DPRINTF("cmd is %s\n", cmd);
525 return system(cmd);
526}
527#else
528#define do_system(cmd) system(cmd)
529#endif
530
531static int is_file(const char *fn)
532{
533 struct stat statbuf;
534
535 if (stat(fn, &statbuf) < 0) return 0;
536 return S_ISREG(statbuf.st_mode);
537}
538
Glenn L McGrath649968c2001-02-10 14:26:48 +0000539static int dpkg_doconfigure(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000540{
541 int r;
542 char postinst[1024];
543 char buf[1024];
544 DPRINTF("Configuring %s\n", pkg->package);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000545 pkg->status &= status_statusmask;
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000546 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000547 if (is_file(postinst))
548 {
549 snprintf(buf, sizeof(buf), "%s configure", postinst);
550 if ((r = do_system(buf)) != 0)
551 {
552 fprintf(stderr, "postinst exited with status %d\n", r);
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000553 pkg->status |= status_statushalfconfigured;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000554 return 1;
555 }
556 }
557
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000558 pkg->status |= status_statusinstalled;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000559
560 return 0;
561}
562
Glenn L McGrath649968c2001-02-10 14:26:48 +0000563static int dpkg_dounpack(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000564{
565 int r = 0;
566 char *cwd, *p;
567 FILE *infp, *outfp;
568 char buf[1024], buf2[1024];
569 int i;
570 char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
571 "conffiles", "md5sums", "shlibs",
572 "templates" };
573
574 DPRINTF("Unpacking %s\n", pkg->package);
575
576 cwd = getcwd(0, 0);
577 chdir("/");
578 snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
579 if (SYSTEM(buf) == 0)
580 {
581 /* Installs the package scripts into the info directory */
582 for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]);
583 i++)
584 {
585 snprintf(buf, sizeof(buf), "%s%s/%s",
586 DPKGCIDIR, pkg->package, adminscripts[i]);
587 snprintf(buf2, sizeof(buf), "%s%s.%s",
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000588 infodir, pkg->package, adminscripts[i]);
589 if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000590 {
591 fprintf(stderr, "Cannot copy %s to %s: %s\n",
592 buf, buf2, strerror(errno));
593 r = 1;
594 break;
595 }
596 else
597 {
598 /* ugly hack to create the list file; should
599 * probably do something more elegant
600 *
601 * why oh why does dpkg create the list file
602 * so oddly...
603 */
604 snprintf(buf, sizeof(buf),
605 "ar -p %s data.tar.gz|zcat|tar -tf -",
606 pkg->file);
607 snprintf(buf2, sizeof(buf2),
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000608 "%s%s.list", infodir, pkg->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000609 if ((infp = popen(buf, "r")) == NULL ||
610 (outfp = fopen(buf2, "w")) == NULL)
611 {
612 fprintf(stderr, "Cannot create %s\n",
613 buf2);
614 r = 1;
615 break;
616 }
617 while (fgets(buf, sizeof(buf), infp) &&
618 !feof(infp))
619 {
620 p = buf;
621 if (*p == '.') p++;
622 if (*p == '/' && *(p+1) == '\n')
623 {
624 *(p+1) = '.';
625 *(p+2) = '\n';
626 *(p+3) = 0;
627 }
628 if (p[strlen(p)-2] == '/')
629 {
630 p[strlen(p)-2] = '\n';
631 p[strlen(p)-1] = 0;
632 }
633 fputs(p, outfp);
634 }
635 fclose(infp);
636 fclose(outfp);
637 }
638 }
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000639 pkg->status &= status_wantmask;
640 pkg->status |= status_wantinstall;
641 pkg->status &= status_flagmask;
642 pkg->status |= status_flagok;
643 pkg->status &= status_statusmask;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000644 if (r == 0)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000645 pkg->status |= status_statusunpacked;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000646 else
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000647 pkg->status |= status_statushalfinstalled;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000648 }
649 chdir(cwd);
650 return r;
651}
652
Glenn L McGrath649968c2001-02-10 14:26:48 +0000653static int dpkg_doinstall(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000654{
655 DPRINTF("Installing %s\n", pkg->package);
656 return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
657}
658
Glenn L McGrath649968c2001-02-10 14:26:48 +0000659static int dpkg_unpackcontrol(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000660{
661 int r = 1;
662 char *cwd = 0;
663 char *p;
664 char buf[1024];
665 FILE *f;
666
667 p = strrchr(pkg->file, '/');
668 if (p) p++; else p = pkg->file;
669 p = pkg->package = strdup(p);
670 while (*p != 0 && *p != '_' && *p != '.') p++;
671 *p = 0;
672
673 cwd = getcwd(0, 0);
674 snprintf(buf, sizeof(buf), "%s%s", DPKGCIDIR, pkg->package);
675 DPRINTF("dir = %s\n", buf);
676 if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0)
677 {
678 snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -",
679 pkg->file);
680 if (SYSTEM(buf) == 0)
681 {
682 if ((f = fopen("control", "r")) != NULL) {
683 control_read(f, pkg);
684 r = 0;
685 }
686 }
687 }
688
689 chdir(cwd);
690 free(cwd);
691 return r;
692}
693
Glenn L McGrath649968c2001-02-10 14:26:48 +0000694static int dpkg_unpack(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000695{
696 int r = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000697 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000698 void *status = status_read();
699
700 if (SYSTEM("rm -rf -- " DPKGCIDIR) != 0 ||
701 mkdir(DPKGCIDIR, S_IRWXU) != 0)
702 {
703 perror("mkdir");
704 return 1;
705 }
706
707 for (pkg = pkgs; pkg != 0; pkg = pkg->next)
708 {
709 dpkg_unpackcontrol(pkg);
710 r = dpkg_dounpack(pkg);
711 if (r != 0) break;
712 }
713 status_merge(status, pkgs);
714 SYSTEM("rm -rf -- " DPKGCIDIR);
715 return r;
716}
717
Glenn L McGrath649968c2001-02-10 14:26:48 +0000718static int dpkg_configure(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000719{
720 int r = 0;
721 void *found;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000722 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000723 void *status = status_read();
724 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next)
725 {
726 found = tfind(pkg, &status, package_compare);
727 if (found == 0)
728 {
729 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
730 r = 1;
731 }
732 else
733 {
734 /* configure the package listed in the status file;
735 * not pkg, as we have info only for the latter */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000736 r = dpkg_doconfigure(*(package_t **)found);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000737 }
738 }
739 status_merge(status, 0);
740 return r;
741}
742
Glenn L McGrath649968c2001-02-10 14:26:48 +0000743static int dpkg_install(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000744{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000745 package_t *p, *ordered = 0;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000746 void *status = status_read();
747 if (SYSTEM("rm -rf -- " DPKGCIDIR) != 0 ||
748 mkdir(DPKGCIDIR, S_IRWXU) != 0)
749 {
750 perror("mkdir");
751 return 1;
752 }
753
754 /* Stage 1: parse all the control information */
755 for (p = pkgs; p != 0; p = p->next)
756 if (dpkg_unpackcontrol(p) != 0)
757 {
758 perror(p->file);
759 /* force loop break, and prevents further ops */
760 pkgs = 0;
761 }
762
763 /* Stage 2: resolve dependencies */
764#ifdef DODEPENDS
765 ordered = depends_resolve(pkgs, status);
766#else
767 ordered = pkgs;
768#endif
769
770 /* Stage 3: install */
771 for (p = ordered; p != 0; p = p->next)
772 {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000773 p->status &= status_wantmask;
774 p->status |= status_wantinstall;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000775
776 /* for now the flag is always set to ok... this is probably
777 * not what we want
778 */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000779 p->status &= status_flagmask;
780 p->status |= status_flagok;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000781
782 if (dpkg_doinstall(p) != 0)
783 {
784 perror(p->file);
785 }
786 }
787
788 if (ordered != 0)
789 status_merge(status, pkgs);
790 SYSTEM("rm -rf -- " DPKGCIDIR);
791 return 0;
792}
793
Glenn L McGrath649968c2001-02-10 14:26:48 +0000794static int dpkg_remove(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000795{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000796 package_t *p;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000797 void *status = status_read();
798 for (p = pkgs; p != 0; p = p->next)
799 {
800 }
801 status_merge(status, 0);
802 return 0;
803}
804
Glenn L McGrath649968c2001-02-10 14:26:48 +0000805extern int dpkg_main(int argc, char **argv)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000806{
807 char opt = 0;
808 char *s;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000809 package_t *p, *packages = NULL;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000810 char *cwd = getcwd(0, 0);
811 while (*++argv)
812 {
813 if (**argv == '-') {
814 /* Nasty little hack to "parse" long options. */
815 s = *argv;
816 while (*s == '-')
817 s++;
818 opt=s[0];
819 }
820 else
821 {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000822 p = (package_t *)malloc(sizeof(package_t));
823 memset(p, 0, sizeof(package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000824 if (**argv == '/')
825 p->file = *argv;
826 else if (opt != 'c')
827 {
828 p->file = malloc(strlen(cwd) + strlen(*argv) + 2);
829 sprintf(p->file, "%s/%s", cwd, *argv);
830 }
831 else {
832 p->package = strdup(*argv);
833 }
834 p->next = packages;
835 packages = p;
836 }
837
838 }
839 switch (opt)
840 {
841 case 'i': return dpkg_install(packages); break;
842 case 'r': return dpkg_remove(packages); break;
843 case 'u': return dpkg_unpack(packages); break;
844 case 'c': return dpkg_configure(packages); break;
845 }
846
847 /* if it falls through to here, some of the command line options were
848 wrong */
849 usage(dpkg_usage);
850 return 0;
851}