15 #define DEPENDSMAX 64 /* maximum number of depends we can handle */
17 /* Should we do full dependency checking? */
20 /* Should we do debugging? */
24 #define SYSTEM(x) do_system(x)
25 #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
27 #define SYSTEM(x) system(x)
28 #define DPRINTF(fmt,args...) /* nothing */
32 extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
33 static const int dpkg_deb_contents = 1;
34 static const int dpkg_deb_control = 2;
35 // const int dpkg_deb_info = 4;
36 static const int dpkg_deb_extract = 8;
37 static const int dpkg_deb_verbose_extract = 16;
38 static const int dpkg_deb_list = 32;
40 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
41 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
42 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
44 static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
46 static const char infodir[] = "/var/lib/dpkg/info/";
47 static const char udpkg_quiet[] = "UDPKG_QUIET";
49 //static const int status_wantstart = 0;
50 //static const int status_wantunknown = (1 << 0);
51 static const int status_wantinstall = (1 << 1);
52 //static const int status_wanthold = (1 << 2);
53 //static const int status_wantdeinstall = (1 << 3);
54 //static const int status_wantpurge = (1 << 4);
55 static const int status_wantmask = 31;
57 //static const int status_flagstart = 5;
58 static const int status_flagok = (1 << 5); /* 32 */
59 //static const int status_flagreinstreq = (1 << 6);
60 //static const int status_flaghold = (1 << 7);
61 //static const int status_flagholdreinstreq = (1 << 8);
62 static const int status_flagmask = 480;
64 //static const int status_statusstart = 9;
65 //static const int status_statusnoninstalled = (1 << 9); /* 512 */
66 static const int status_statusunpacked = (1 << 10);
67 static const int status_statushalfconfigured = (1 << 11);
68 static const int status_statusinstalled = (1 << 12);
69 static const int status_statushalfinstalled = (1 << 13);
70 //static const int status_statusconfigfiles = (1 << 14);
71 //static const int status_statuspostinstfailed = (1 << 15);
72 //static const int status_statusremovalfailed = (1 << 16);
73 static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */
75 static const char *statuswords[][10] = {
76 { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
77 { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
78 { (char *) 9, "not-installed", "unpacked", "half-configured",
79 "installed", "half-installed", "config-files",
80 "post-inst-failed", "removal-failed", 0 }
83 static const int color_white = 0;
84 static const int color_grey = 1;
85 static const int color_black = 2;
88 typedef struct package_s {
95 int installer_menu_item;
97 char color; /* for topo-sort */
98 struct package_s *requiredfor[DEPENDSMAX];
99 unsigned short requiredcount;
100 struct package_s *next;
104 static int do_system(const char *cmd)
106 DPRINTF("cmd is %s\n", cmd);
110 #define do_system(cmd) system(cmd)
113 static int package_compare(const void *p1, const void *p2)
115 return strcmp(((package_t *)p1)->package,
116 ((package_t *)p2)->package);
119 static int remove_dpkgcidir()
121 char *rm_dpkgcidir = NULL;
123 rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8);
124 strcpy(rm_dpkgcidir, "rm -rf ");
125 strcat(rm_dpkgcidir, dpkgcidir);
127 if (SYSTEM(rm_dpkgcidir) != 0) {
137 static char **depends_split(const char *dependsstr)
139 static char *dependsvec[DEPENDSMAX];
144 if (dependsstr == 0) {
148 p = strdup(dependsstr);
149 while (*p != 0 && *p != '\n') {
155 if (dependsvec[i] == 0) {
160 *p = 0; /* eat the space... */
171 /* Topological sort algorithm:
172 * ordered is the output list, pkgs is the dependency graph, pkg is
175 * recursively add all the adjacent nodes to the ordered list, marking
176 * each one as visited along the way
178 * yes, this algorithm looks a bit odd when all the params have the
181 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
186 /* mark node as processing */
187 pkg->color = color_grey;
189 /* visit each not-yet-visited node */
190 for (i = 0; i < pkg->requiredcount; i++)
191 if (pkg->requiredfor[i]->color == color_white)
192 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
195 /* add it to the list */
196 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
197 /* make a shallow copy */
199 newnode->next = *ordered;
203 pkg->next = *ordered;
206 /* mark node as done */
207 pkg->color = color_black;
210 static package_t *depends_sort(package_t *pkgs)
212 /* TODO: it needs to break cycles in the to-be-installed package
214 package_t *ordered = NULL;
217 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
218 pkg->color = color_white;
220 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
221 if (pkg->color == color_white) {
222 depends_sort_visit(&ordered, pkgs, pkg);
226 /* Leaks the old list... return the new one... */
231 /* resolve package dependencies --
232 * for each package in the list of packages to be installed, we parse its
233 * dependency info to determine if the dependent packages are either
234 * already installed, or are scheduled to be installed. If both tests fail
237 * The algorithm here is O(n^2*m) where n = number of packages to be
238 * installed and m is the # of dependencies per package. Not a terribly
239 * efficient algorithm, but given that at any one time you are unlikely
240 * to install a very large number of packages it doesn't really matter
242 static package_t *depends_resolve(package_t *pkgs, void *status)
244 package_t *pkg, *chk;
250 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
251 dependsvec = depends_split(pkg->depends);
253 while (dependsvec[i] != 0) {
254 /* Check for dependencies; first look for installed packages */
255 dependpkg.package = dependsvec[i];
256 if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
257 ((chk = *(package_t **)found) &&
258 (chk->status & (status_flagok | status_statusinstalled)) !=
259 (status_flagok | status_statusinstalled))) {
261 /* if it fails, we look through the list of packages we are going to
263 for (chk = pkgs; chk != 0; chk = chk->next) {
264 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
265 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
266 if (chk->requiredcount >= DEPENDSMAX) {
267 fprintf(stderr, "Too many dependencies for %s\n", chk->package);
271 chk->requiredfor[chk->requiredcount++] = pkg;
277 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
285 return depends_sort(pkgs);
289 /* Status file handling routines
291 * This is a fairly minimalistic implementation. there are two main functions
292 * that are supported:
294 * 1) reading the entire status file:
295 * the status file is read into memory as a binary-tree, with just the
296 * package and status info preserved
298 * 2) merging the status file
299 * control info from (new) packages is merged into the status file,
300 * replacing any pre-existing entries. when a merge happens, status info
301 * read using the status_read function is written back to the status file
303 static unsigned long status_parse(const char *line)
309 for (i = 0; i < 3; i++) {
310 if ((p = strchr(line, ' ')) != NULL) {
314 while (statuswords[i][j] != 0) {
315 if (strcmp(line, statuswords[i][j]) == 0) {
316 l |= (1 << ((int)statuswords[i][0] + j - 1));
322 if (statuswords[i][j] == 0) {
331 static const char *status_print(unsigned long flags)
333 /* this function returns a static buffer... */
334 static char buf[256];
338 for (i = 0; i < 3; i++) {
340 while (statuswords[i][j] != 0) {
341 if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) {
342 strcat(buf, statuswords[i][j]);
343 if (i < 2) strcat(buf, " ");
348 if (statuswords[i][j] == 0) {
349 fprintf(stderr, "corrupted status flag!!\n");
358 * Read a control file (or a stanza of a status file) and parse it,
359 * filling parsed fields into the package structure
361 static int control_read(FILE *file, package_t *p)
365 while ((line = get_line_from_file(file)) != NULL) {
366 line[strlen(line)] = 0;
368 if (strlen(line) == 0) {
371 if (strstr(line, "Package: ") == line) {
372 p->package = strdup(line + 9);
374 if (strstr(line, "Status: ") == line) {
375 p->status = status_parse(line + 8);
377 if (strstr(line, "Depends: ") == line) {
378 p->depends = strdup(line + 9);
380 if (strstr(line, "Provides: ") == line) {
381 p->provides = strdup(line + 10);
383 if (strstr(line, "Description: ") == line) {
384 p->description = strdup(line + 13);
385 /* This is specific to the Debian Installer. Ifdef? */
387 if (strstr(line, "installer-menu-item: ") == line) {
388 p->installer_menu_item = atoi(line + 21);
390 /* TODO: localized descriptions */
396 static void *status_read(void)
400 package_t *m = 0, *p = 0, *t = 0;
402 if ((f = fopen(statusfile, "r")) == NULL) {
407 if (getenv(udpkg_quiet) == NULL) {
408 printf("(Reading database...)\n");
412 m = (package_t *)xmalloc(sizeof(package_t));
413 memset(m, 0, sizeof(package_t));
417 * If there is an item in the tree by this name,
418 * it must be a virtual package; insert real
419 * package in preference.
421 tdelete(m, &status, package_compare);
422 tsearch(m, &status, package_compare);
425 * A "Provides" triggers the insertion
426 * of a pseudo package into the status
429 p = (package_t *)xmalloc(sizeof(package_t));
430 memset(p, 0, sizeof(package_t));
431 p->package = strdup(m->provides);
433 t = *(package_t **)tsearch(p, &status, package_compare);
440 * Pseudo package status is the
441 * same as the status of the
442 * package providing it
443 * FIXME: (not quite right, if 2
444 * packages of different statuses
447 t->status = m->status;
459 static int status_merge(void *status, package_t *pkgs)
463 package_t *pkg = 0, *statpkg = 0;
467 if ((fin = fopen(statusfile, "r")) == NULL) {
471 if ((fout = fopen(new_statusfile, "w")) == NULL) {
472 perror(new_statusfile);
475 if (getenv(udpkg_quiet) == NULL) {
476 printf("(Updating database...)\n");
479 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
480 line[strlen(line)] = 0; /* trim newline */
481 /* If we see a package header, find out if it's a package
482 * that we have processed. if so, we skip that block for
483 * now (write it at the end).
485 * we also look at packages in the status cache and update
486 * their status fields
488 if (strstr(line, "Package: ") == line) {
489 for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
490 pkg->package, strlen(line) - 9) != 0;
493 locpkg.package = line + 9;
494 statpkg = tfind(&locpkg, &status, package_compare);
496 /* note: statpkg should be non-zero, unless the status
497 * file was changed while we are processing (no locking
498 * is currently done...
501 statpkg = *(package_t **)statpkg;
507 if (strstr(line, "Status: ") == line && statpkg != 0) {
508 snprintf(line, sizeof(line), "Status: %s",
509 status_print(statpkg->status));
516 // Print out packages we processed.
517 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
518 fprintf(fout, "Package: %s\nStatus: %s\n",
519 pkg->package, status_print(pkg->status));
521 fprintf(fout, "Depends: %s\n", pkg->depends);
523 fprintf(fout, "Provides: %s\n", pkg->provides);
524 if (pkg->installer_menu_item)
525 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
526 if (pkg->description)
527 fprintf(fout, "Description: %s\n", pkg->description);
534 r = rename(statusfile, bak_statusfile);
536 r = rename(new_statusfile, statusfile);
542 static int is_file(const char *fn)
546 if (stat(fn, &statbuf) < 0) {
549 return S_ISREG(statbuf.st_mode);
552 static int dpkg_doconfigure(package_t *pkg)
558 DPRINTF("Configuring %s\n", pkg->package);
559 pkg->status &= status_statusmask;
560 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
562 if (is_file(postinst)) {
563 snprintf(buf, sizeof(buf), "%s configure", postinst);
564 if ((r = do_system(buf)) != 0) {
565 fprintf(stderr, "postinst exited with status %d\n", r);
566 pkg->status |= status_statushalfconfigured;
570 pkg->status |= status_statusinstalled;
575 static int dpkg_dounpack(package_t *pkg)
580 char *src_file = NULL;
581 char *dst_file = NULL;
582 char *lst_file = NULL;
583 char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst",
584 "/conffiles", "/md5sums", "/shlibs", "/templates" };
585 char buf[1024], buf2[1024];
587 DPRINTF("Unpacking %s\n", pkg->package);
591 deb_extract(dpkg_deb_extract, "/", pkg->file);
593 /* Installs the package scripts into the info directory */
594 for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
595 /* The full path of the current location of the admin file */
596 src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
597 strcpy(src_file, dpkgcidir);
598 strcat(src_file, pkg->package);
599 strcat(src_file, adminscripts[i]);
601 /* the full path of where we want the file to be copied to */
602 dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
603 strcpy(dst_file, infodir);
604 strcat(dst_file, pkg->package);
605 strcat(dst_file, adminscripts[i]);
607 /* copy admin file to permanent home */
608 if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) {
609 error_msg_and_die("Cannot copy %s to %s ", buf, buf2);
612 /* create the list file */
613 lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
614 strcpy(lst_file, infodir);
615 strcat(lst_file, pkg->package);
616 strcat(lst_file, ".list");
617 deb_extract(dpkg_deb_list, NULL, pkg->file);
625 pkg->status &= status_wantmask;
626 pkg->status |= status_wantinstall;
627 pkg->status &= status_flagmask;
628 pkg->status |= status_flagok;
629 pkg->status &= status_statusmask;
632 pkg->status |= status_statusunpacked;
634 pkg->status |= status_statushalfinstalled;
641 * Extract and parse the control.tar.gz from the specified package
643 static int dpkg_unpackcontrol(package_t *pkg)
649 /* clean the temp directory (dpkgcidir) be recreating it */
651 if (mkdir(dpkgcidir, S_IRWXU) != 0) {
657 * Get the package name from the file name,
658 * first remove the directories
660 if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
661 tmp_name = pkg->file;
665 /* now remove trailing version numbers etc */
666 length = strcspn(tmp_name, "_.");
667 pkg->package = (char *) xmalloc(length + 1);
668 /* store the package name */
669 strncpy(pkg->package, tmp_name, length);
671 /* work out the full extraction path */
672 tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9);
673 memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
674 strcpy(tmp_name, dpkgcidir);
675 strcat(tmp_name, pkg->package);
677 /* extract control.tar.gz to the full extraction path */
678 deb_extract(dpkg_deb_control, tmp_name, pkg->file);
680 /* parse the extracted control file */
681 strcat(tmp_name, "/control");
682 if ((file = fopen(tmp_name, "r")) == NULL) {
685 if (control_read(file, pkg) == EXIT_FAILURE) {
692 static int dpkg_unpack(package_t *pkgs, void *status)
697 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
698 if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
701 if ((r = dpkg_dounpack(pkg)) != 0 ) {
705 status_merge(status, pkgs);
711 static int dpkg_configure(package_t *pkgs, void *status)
717 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
718 found = tfind(pkg, &status, package_compare);
721 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
724 /* configure the package listed in the status file;
725 * not pkg, as we have info only for the latter
728 r = dpkg_doconfigure(*(package_t **)found);
731 status_merge(status, 0);
736 static int dpkg_install(package_t *pkgs, void *status)
738 package_t *p, *ordered = 0;
740 /* Stage 1: parse all the control information */
741 for (p = pkgs; p != 0; p = p->next) {
742 if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
748 /* Stage 2: resolve dependencies */
750 ordered = depends_resolve(pkgs, status);
755 /* Stage 3: install */
756 for (p = ordered; p != 0; p = p->next) {
757 p->status &= status_wantmask;
758 p->status |= status_wantinstall;
760 /* for now the flag is always set to ok... this is probably
763 p->status &= status_flagmask;
764 p->status |= status_flagok;
766 DPRINTF("Installing %s\n", p->package);
767 if (dpkg_dounpack(p) != 0) {
770 if (dpkg_doconfigure(p) != 0) {
776 status_merge(status, pkgs);
783 static int dpkg_remove(package_t *pkgs, void *status)
787 for (p = pkgs; p != 0; p = p->next)
790 status_merge(status, 0);
795 extern int dpkg_main(int argc, char **argv)
799 package_t *p, *packages = NULL;
800 char *cwd = getcwd(0, 0);
805 /* Nasty little hack to "parse" long options. */
812 p = (package_t *)xmalloc(sizeof(package_t));
813 memset(p, 0, sizeof(package_t));
819 p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
820 sprintf(p->file, "%s/%s", cwd, *argv);
822 p->package = strdup(*argv);
830 status = status_read();
834 return dpkg_install(packages, status);
836 return dpkg_remove(packages, status);
838 return dpkg_unpack(packages, status);
840 return dpkg_configure(packages, status);