11 #include <sys/types.h>
17 #define DEPENDSMAX 64 /* maximum number of depends we can handle */
19 /* Should we do full dependency checking? */
22 /* Should we do debugging? */
26 #define SYSTEM(x) do_system(x)
27 #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
29 #define SYSTEM(x) system(x)
30 #define DPRINTF(fmt,args...) /* nothing */
35 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
36 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
37 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
39 static const char infodir[] = "/var/lib/dpkg/info/";
40 static const char udpkg_quiet[] = "UDPKG_QUIET";
42 //static const int status_want_unknown = 1;
43 static const int state_want_install = 2;
44 //static const int state_want_hold = 3;
45 //static const int state_want_deinstall = 4;
46 //static const int state_want_purge = 5;
48 static const int state_flag_ok = 1;
49 //static const int state_flag_reinstreq = 2;
50 //static const int state_flag_hold = 3;
51 //static const int state_flag_holdreinstreq = 4;
53 //static const int state_statusnoninstalled = 1;
54 static const int state_status_unpacked = 2;
55 static const int state_status_halfconfigured = 3;
56 static const int state_status_installed = 4;
57 static const int state_status_halfinstalled = 5;
58 //static const int state_statusconfigfiles = 6;
59 //static const int state_statuspostinstfailed = 7;
60 //static const int state_statusremovalfailed = 8;
62 static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 };
63 static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 };
64 static const char *state_words_status[] = { "not-installed", "unpacked", "half-configured", "installed",
65 "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 };
67 static const int color_white = 0;
68 static const int color_grey = 1;
69 static const int color_black = 2;
72 typedef struct package_s {
75 unsigned char state_want;
76 unsigned char state_flag;
77 unsigned char state_status;
93 char *long_description;
96 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);
120 * NOTE: this was handled by a "rm -rf" shell command
121 * Maybe theis behaviour should be integrated into the rm applet
122 * (i dont appreciate the rm applets recursive action fn)-bug1
124 static int remove_dir(const char *dirname)
127 DIR *dp = opendir(dirname);
128 while ((fp = readdir(dp)) != NULL) {
132 filename = (char *) xcalloc(1, strlen(dirname) + strlen(fp->d_name) + 2);
133 strcpy(filename, dirname);
134 strcat(filename, fp->d_name);
135 lstat(filename, &statbuf);
137 if ((strcmp(fp->d_name, ".") != 0) && (strcmp(fp->d_name, "..") != 0)) {
138 if (S_ISDIR(statbuf.st_mode)) {
139 remove_dir(strcat(filename, "/"));
141 else if (remove(filename) == -1) {
142 perror_msg(filename);
153 static char **depends_split(const char *dependsstr)
155 static char *dependsvec[DEPENDSMAX];
160 if (dependsstr == 0) {
164 p = xstrdup(dependsstr);
165 while (*p != 0 && *p != '\n') {
171 if (dependsvec[i] == 0) {
176 *p = 0; /* eat the space... */
187 /* Topological sort algorithm:
188 * ordered is the output list, pkgs is the dependency graph, pkg is
191 * recursively add all the adjacent nodes to the ordered list, marking
192 * each one as visited along the way
194 * yes, this algorithm looks a bit odd when all the params have the
197 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
202 /* mark node as processing */
203 pkg->color = color_grey;
205 /* visit each not-yet-visited node */
206 for (i = 0; i < pkg->requiredcount; i++)
207 if (pkg->requiredfor[i]->color == color_white)
208 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
211 /* add it to the list */
212 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
213 /* make a shallow copy */
215 newnode->next = *ordered;
219 pkg->next = *ordered;
222 /* mark node as done */
223 pkg->color = color_black;
226 static package_t *depends_sort(package_t *pkgs)
228 /* TODO: it needs to break cycles in the to-be-installed package
230 package_t *ordered = NULL;
233 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
234 pkg->color = color_white;
236 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
237 if (pkg->color == color_white) {
238 depends_sort_visit(&ordered, pkgs, pkg);
242 /* Leaks the old list... return the new one... */
247 /* resolve package dependencies --
248 * for each package in the list of packages to be installed, we parse its
249 * dependency info to determine if the dependent packages are either
250 * already installed, or are scheduled to be installed. If both tests fail
253 * The algorithm here is O(n^2*m) where n = number of packages to be
254 * installed and m is the # of dependencies per package. Not a terribly
255 * efficient algorithm, but given that at any one time you are unlikely
256 * to install a very large number of packages it doesn't really matter
258 static package_t *depends_resolve(package_t *pkgs, void *status)
260 package_t *pkg, *chk;
266 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
267 dependsvec = depends_split(pkg->depends);
269 while (dependsvec[i] != 0) {
270 /* Check for dependencies; first look for installed packages */
271 dependpkg.package = dependsvec[i];
272 if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
273 ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
274 (chk->state_status & state_status_installed))) {
276 /* if it fails, we look through the list of packages we are going to
278 for (chk = pkgs; chk != 0; chk = chk->next) {
279 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
280 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
281 if (chk->requiredcount >= DEPENDSMAX) {
282 error_msg("Too many dependencies for %s", chk->package);
286 chk->requiredfor[chk->requiredcount++] = pkg;
292 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
300 return depends_sort(pkgs);
304 /* Status file handling routines
306 * This is a fairly minimalistic implementation. there are two main functions
307 * that are supported:
309 * 1) reading the entire status file:
310 * the status file is read into memory as a binary-tree, with just the
311 * package and status info preserved
313 * 2) merging the status file
314 * control info from (new) packages is merged into the status file,
315 * replacing any pre-existing entries. when a merge happens, status info
316 * read using the status_read function is written back to the status file
318 static unsigned char status_parse(const char *line, const char **status_words)
320 unsigned char status_num;
323 while (status_words[i] != 0) {
324 if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
325 status_num = (char)i;
331 error_msg("Invalid status word");
336 * Read the buffered control file and parse it,
337 * filling parsed fields into the package structure
339 static int fill_package_struct(package_t *package, const char *package_buffer)
343 int field_length = 0;
345 while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
346 field_length = strlen(field);
347 field_start += (field_length + 1);
349 if (strlen(field) == 0) {
350 printf("empty line: *this shouldnt happen i dont think*\n");
354 /* these are common to both installed and uninstalled packages */
355 if (strstr(field, "Package: ") == field) {
356 package->package = strdup(field + 9);
358 else if (strstr(field, "Depends: ") == field) {
359 package->depends = strdup(field + 9);
361 else if (strstr(field, "Provides: ") == field) {
362 package->provides = strdup(field + 10);
364 /* This is specific to the Debian Installer. Ifdef? */
365 else if (strstr(field, "installer-menu-item: ") == field) {
366 package->installer_menu_item = atoi(field + 21);
368 else if (strstr(field, "Description: ") == field) {
369 package->description = strdup(field + 13);
371 else if (strstr(field, "Priority: ") == field) {
372 package->priority = strdup(field + 10);
374 else if (strstr(field, "Section: ") == field) {
375 package->section = strdup(field + 9);
377 else if (strstr(field, "Installed-Size: ") == field) {
378 package->installed_size = strdup(field + 16);
380 else if (strstr(field, "Maintainer: ") == field) {
381 package->maintainer = strdup(field + 12);
383 else if (strstr(field, "Version: ") == field) {
384 package->version = strdup(field + 9);
386 else if (strstr(field, "Suggests: ") == field) {
387 package->suggests = strdup(field + 10);
389 else if (strstr(field, "Recommends: ") == field) {
390 package->recommends = strdup(field + 12);
392 /* else if (strstr(field, "Conffiles: ") == field) {
393 package->conffiles = read_block(file);
394 package->conffiles = xcalloc(1, 1);
395 while ((field = strtok(NULL, "\n")) != NULL) {
396 package->long_description = xrealloc(package->conffiles,
397 strlen(package->conffiles) + strlen(field) + 1);
398 strcat(package->conffiles, field);
402 /* These are only in available file */
403 else if (strstr(field, "Architecture: ") == field) {
404 package->architecture = strdup(field + 14);
406 else if (strstr(field, "Filename: ") == field) {
407 package->filename = strdup(field + 10);
409 else if (strstr(field, "MD5sum ") == field) {
410 package->md5sum = strdup(field + 7);
413 /* This is only needed for status file */
414 if (strstr(field, "Status: ") == field) {
417 word_pointer = strchr(field, ' ') + 1;
418 package->state_want = status_parse(word_pointer, state_words_want);
419 word_pointer = strchr(word_pointer, ' ') + 1;
420 package->state_flag = status_parse(word_pointer, state_words_flag);
421 word_pointer = strchr(word_pointer, ' ') + 1;
422 package->state_status = status_parse(word_pointer, state_words_status);
424 package->state_want = status_parse("purge", state_words_want);
425 package->state_flag = status_parse("ok", state_words_flag);
426 package->state_status = status_parse("not-installed", state_words_status);
434 extern void write_package(FILE *out_file, package_t *pkg)
437 fprintf(out_file, "Package: %s\n", pkg->package);
439 if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
440 fprintf(out_file, "Status: %s %s %s\n",
441 state_words_want[pkg->state_want - 1],
442 state_words_flag[pkg->state_flag - 1],
443 state_words_status[pkg->state_status - 1]);
446 fprintf(out_file, "Depends: %s\n", pkg->depends);
449 fprintf(out_file, "Provides: %s\n", pkg->provides);
452 fprintf(out_file, "Priority: %s\n", pkg->priority);
455 fprintf(out_file, "Section: %s\n", pkg->section);
458 fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
460 if (pkg->maintainer) {
461 fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
464 fprintf(out_file, "Source: %s\n", pkg->source);
467 fprintf(out_file, "Version: %s\n", pkg->version);
469 if (pkg->pre_depends) {
470 fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
473 fprintf(out_file, "Replaces: %s\n", pkg->replaces);
475 if (pkg->recommends) {
476 fprintf(out_file, "Recommends: %s\n", pkg->recommends);
479 fprintf(out_file, "Suggests: %s\n", pkg->suggests);
481 if (pkg->conflicts) {
482 fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
484 if (pkg->conffiles) {
485 fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
487 if (pkg->architecture) {
488 fprintf(out_file, "Architecture: %s\n", pkg->architecture);
491 fprintf(out_file, "Filename: %s\n", pkg->filename);
494 fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
496 if (pkg->installer_menu_item) {
497 fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
499 if (pkg->description) {
500 fprintf(out_file, "Description: %s\n", pkg->description);
502 fputc('\n', out_file);
506 static void *status_read(void)
510 package_t *m = 0, *p = 0, *t = 0;
511 char *package_control_buffer = NULL;
513 if (getenv(udpkg_quiet) == NULL) {
514 printf("(Reading database...)\n");
517 if ((f = wfopen(statusfile, "r")) == NULL) {
521 while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) {
522 m = (package_t *)xcalloc(1, sizeof(package_t));
523 fill_package_struct(m, package_control_buffer);
526 * If there is an item in the tree by this name,
527 * it must be a virtual package; insert real
528 * package in preference.
530 tdelete(m, &status, package_compare);
531 tsearch(m, &status, package_compare);
534 * A "Provides" triggers the insertion
535 * of a pseudo package into the status
538 p = (package_t *)xcalloc(1, sizeof(package_t));
539 p->package = xstrdup(m->provides);
540 t = *(package_t **)tsearch(p, &status, package_compare);
546 * Pseudo package status is the
547 * same as the status of the
548 * package providing it
549 * FIXME: (not quite right, if 2
550 * packages of different statuses
553 t->state_want = m->state_want;
554 t->state_flag = m->state_flag;
555 t->state_status = m->state_status;
567 static int status_merge(void *status, package_t *pkgs)
571 package_t *pkg = 0, *statpkg = 0;
574 if ((fout = wfopen(new_statusfile, "w")) == NULL) {
577 if (getenv(udpkg_quiet) == NULL) {
578 printf("(Updating database...)\n");
582 * Dont use wfopen here, handle errors ourself
584 if ((fin = fopen(statusfile, "r")) != NULL) {
585 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
586 chomp(line); /* trim newline */
587 /* If we see a package header, find out if it's a package
588 * that we have processed. if so, we skip that block for
589 * now (write it at the end).
591 * we also look at packages in the status cache and update
592 * their status fields
594 if (strstr(line, "Package: ") == line) {
595 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
596 pkg->package) != 0; pkg = pkg->next) ;
598 locpkg.package = line + 9;
599 statpkg = tfind(&locpkg, &status, package_compare);
601 /* note: statpkg should be non-zero, unless the status
602 * file was changed while we are processing (no locking
603 * is currently done...
606 statpkg = *(package_t **)statpkg;
612 if (strstr(line, "Status: ") == line && statpkg != 0) {
613 snprintf(line, sizeof(line), "Status: %s %s %s",
614 state_words_want[statpkg->state_want - 1],
615 state_words_flag[statpkg->state_flag - 1],
616 state_words_status[statpkg->state_status - 1]);
618 fprintf(fout, "%s\n", line);
624 // Print out packages we processed.
625 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
626 write_package(fout, pkg);
631 * Its ok if renaming statusfile fails becasue it doesnt exist
633 if (rename(statusfile, bak_statusfile) == -1) {
634 struct stat stat_buf;
635 if (stat(statusfile, &stat_buf) == 0) {
636 error_msg("Couldnt create backup status file");
637 return(EXIT_FAILURE);
639 error_msg("No status file found, creating new one");
642 if (rename(new_statusfile, statusfile) == -1) {
643 error_msg("Couldnt create status file");
644 return(EXIT_FAILURE);
646 return(EXIT_SUCCESS);
649 static int is_file(const char *fn)
653 if (stat(fn, &statbuf) < 0) {
656 return S_ISREG(statbuf.st_mode);
659 static int dpkg_doconfigure(package_t *pkg)
665 DPRINTF("Configuring %s\n", pkg->package);
666 pkg->state_status = 0;
667 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
669 if (is_file(postinst)) {
670 snprintf(buf, sizeof(buf), "%s configure", postinst);
671 if ((r = do_system(buf)) != 0) {
672 error_msg("postinst exited with status %d\n", r);
673 pkg->state_status = state_status_halfconfigured;
677 pkg->state_status = state_status_installed;
682 static int dpkg_dounpack(package_t *pkg)
688 DPRINTF("Unpacking %s\n", pkg->package);
690 /* extract the data file */
691 deb_extract(pkg->filename, extract_extract, "/", NULL);
693 /* extract the control files */
694 deb_extract(pkg->filename, extract_control, infodir, pkg->package);
696 /* Create the list file */
697 lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
698 strcpy(lst_path, infodir);
699 strcat(lst_path, pkg->package);
700 strcat(lst_path, ".list");
701 deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL);
703 pkg->state_want = state_want_install;
704 pkg->state_flag = state_flag_ok;
706 if (status == TRUE) {
707 pkg->state_status = state_status_unpacked;
709 pkg->state_status = state_status_halfinstalled;
716 * Extract and parse the control file from control.tar.gz
718 static int dpkg_read_control(package_t *pkg)
721 char *control_buffer = NULL;
723 if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
726 control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL);
727 fill_package_struct(pkg, control_buffer);
731 static int dpkg_unpack(package_t *pkgs, void *status)
736 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
737 dpkg_read_control(pkg);
738 if ((r = dpkg_dounpack(pkg)) != 0 ) {
742 status_merge(status, pkgs);
747 static int dpkg_configure(package_t *pkgs, void *status)
753 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
754 found = tfind(pkg, &status, package_compare);
757 error_msg("Trying to configure %s, but it is not installed", pkg->package);
760 /* configure the package listed in the status file;
761 * not pkg, as we have info only for the latter
764 r = dpkg_doconfigure(*(package_t **)found);
767 status_merge(status, 0);
772 static int dpkg_install(package_t *pkgs, void *status)
774 package_t *p, *ordered = 0;
776 /* Stage 1: parse all the control information */
777 for (p = pkgs; p != 0; p = p->next) {
778 dpkg_read_control(p);
781 /* Stage 2: resolve dependencies */
783 ordered = depends_resolve(pkgs, status);
788 /* Stage 3: install */
789 for (p = ordered; p != 0; p = p->next) {
790 p->state_want = state_want_install;
792 /* for now the flag is always set to ok... this is probably
795 p->state_flag = state_flag_ok;
797 DPRINTF("Installing %s\n", p->package);
798 if (dpkg_dounpack(p) != 0) {
799 perror_msg(p->filename);
802 if (dpkg_doconfigure(p) != 0) {
803 perror_msg(p->filename);
808 status_merge(status, pkgs);
815 * Not implemented yet
817 static int dpkg_remove(package_t *pkgs, void *status)
821 for (p = pkgs; p != 0; p = p->next)
824 status_merge(status, 0);
830 extern int dpkg_main(int argc, char **argv)
832 const int arg_install = 1;
833 const int arg_unpack = 2;
834 const int arg_configure = 4;
836 package_t *p, *packages = NULL;
841 while ((opt = getopt(argc, argv, "iruc")) != -1) {
844 optflag |= arg_install;
847 optflag |= arg_unpack;
850 optflag |= arg_configure;
857 while (optind < argc) {
858 p = (package_t *) xcalloc(1, sizeof(package_t));
859 if (optflag & arg_configure) {
860 p->package = xstrdup(argv[optind]);
862 p->filename = xstrdup(argv[optind]);
870 create_path(infodir, S_IRWXU);
872 status = status_read();
874 if (optflag & arg_install) {
875 return dpkg_install(packages, status);
877 else if (optflag & arg_unpack) {
878 return dpkg_unpack(packages, status);
880 else if (optflag & arg_configure) {
881 return dpkg_configure(packages, status);
883 return(EXIT_FAILURE);