12 #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);
121 static char **depends_split(const char *dependsstr)
123 static char *dependsvec[DEPENDSMAX];
128 if (dependsstr == 0) {
132 p = xstrdup(dependsstr);
133 while (*p != 0 && *p != '\n') {
139 if (dependsvec[i] == 0) {
144 *p = 0; /* eat the space... */
155 /* Topological sort algorithm:
156 * ordered is the output list, pkgs is the dependency graph, pkg is
159 * recursively add all the adjacent nodes to the ordered list, marking
160 * each one as visited along the way
162 * yes, this algorithm looks a bit odd when all the params have the
165 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
170 /* mark node as processing */
171 pkg->color = color_grey;
173 /* visit each not-yet-visited node */
174 for (i = 0; i < pkg->requiredcount; i++)
175 if (pkg->requiredfor[i]->color == color_white)
176 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
179 /* add it to the list */
180 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
181 /* make a shallow copy */
183 newnode->next = *ordered;
187 pkg->next = *ordered;
190 /* mark node as done */
191 pkg->color = color_black;
194 static package_t *depends_sort(package_t *pkgs)
196 /* TODO: it needs to break cycles in the to-be-installed package
198 package_t *ordered = NULL;
201 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
202 pkg->color = color_white;
204 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
205 if (pkg->color == color_white) {
206 depends_sort_visit(&ordered, pkgs, pkg);
210 /* Leaks the old list... return the new one... */
215 /* resolve package dependencies --
216 * for each package in the list of packages to be installed, we parse its
217 * dependency info to determine if the dependent packages are either
218 * already installed, or are scheduled to be installed. If both tests fail
221 * The algorithm here is O(n^2*m) where n = number of packages to be
222 * installed and m is the # of dependencies per package. Not a terribly
223 * efficient algorithm, but given that at any one time you are unlikely
224 * to install a very large number of packages it doesn't really matter
226 static package_t *depends_resolve(package_t *pkgs, void *status)
228 package_t *pkg, *chk;
234 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
235 dependsvec = depends_split(pkg->depends);
237 while (dependsvec[i] != 0) {
238 /* Check for dependencies; first look for installed packages */
239 dependpkg.package = dependsvec[i];
240 if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
241 ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
242 (chk->state_status & state_status_installed))) {
244 /* if it fails, we look through the list of packages we are going to
246 for (chk = pkgs; chk != 0; chk = chk->next) {
247 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
248 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
249 if (chk->requiredcount >= DEPENDSMAX) {
250 error_msg("Too many dependencies for %s", chk->package);
254 chk->requiredfor[chk->requiredcount++] = pkg;
260 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
268 return depends_sort(pkgs);
272 /* Status file handling routines
274 * This is a fairly minimalistic implementation. there are two main functions
275 * that are supported:
277 * 1) reading the entire status file:
278 * the status file is read into memory as a binary-tree, with just the
279 * package and status info preserved
281 * 2) merging the status file
282 * control info from (new) packages is merged into the status file,
283 * replacing any pre-existing entries. when a merge happens, status info
284 * read using the status_read function is written back to the status file
286 static unsigned char status_parse(const char *line, const char **status_words)
288 unsigned char status_num;
291 while (status_words[i] != 0) {
292 if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
293 status_num = (char)i;
299 error_msg("Invalid status word");
304 * Read the buffered control file and parse it,
305 * filling parsed fields into the package structure
307 static int fill_package_struct(package_t *package, const char *package_buffer)
311 int field_length = 0;
313 while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
314 field_length = strlen(field);
315 field_start += (field_length + 1);
317 if (strlen(field) == 0) {
318 printf("empty line: *this shouldnt happen i dont think*\n");
322 /* these are common to both installed and uninstalled packages */
323 if (strstr(field, "Package: ") == field) {
324 package->package = strdup(field + 9);
326 else if (strstr(field, "Depends: ") == field) {
327 package->depends = strdup(field + 9);
329 else if (strstr(field, "Provides: ") == field) {
330 package->provides = strdup(field + 10);
332 /* This is specific to the Debian Installer. Ifdef? */
333 else if (strstr(field, "installer-menu-item: ") == field) {
334 package->installer_menu_item = atoi(field + 21);
336 else if (strstr(field, "Description: ") == field) {
337 package->description = strdup(field + 13);
339 else if (strstr(field, "Priority: ") == field) {
340 package->priority = strdup(field + 10);
342 else if (strstr(field, "Section: ") == field) {
343 package->section = strdup(field + 9);
345 else if (strstr(field, "Installed-Size: ") == field) {
346 package->installed_size = strdup(field + 16);
348 else if (strstr(field, "Maintainer: ") == field) {
349 package->maintainer = strdup(field + 12);
351 else if (strstr(field, "Version: ") == field) {
352 package->version = strdup(field + 9);
354 else if (strstr(field, "Suggests: ") == field) {
355 package->suggests = strdup(field + 10);
357 else if (strstr(field, "Recommends: ") == field) {
358 package->recommends = strdup(field + 12);
360 /* else if (strstr(field, "Conffiles: ") == field) {
361 package->conffiles = read_block(file);
362 package->conffiles = xcalloc(1, 1);
363 while ((field = strtok(NULL, "\n")) != NULL) {
364 package->long_description = xrealloc(package->conffiles,
365 strlen(package->conffiles) + strlen(field) + 1);
366 strcat(package->conffiles, field);
370 /* These are only in available file */
371 else if (strstr(field, "Architecture: ") == field) {
372 package->architecture = strdup(field + 14);
374 else if (strstr(field, "Filename: ") == field) {
375 package->filename = strdup(field + 10);
377 else if (strstr(field, "MD5sum ") == field) {
378 package->md5sum = strdup(field + 7);
381 /* This is only needed for status file */
382 if (strstr(field, "Status: ") == field) {
385 word_pointer = strchr(field, ' ') + 1;
386 package->state_want = status_parse(word_pointer, state_words_want);
387 word_pointer = strchr(word_pointer, ' ') + 1;
388 package->state_flag = status_parse(word_pointer, state_words_flag);
389 word_pointer = strchr(word_pointer, ' ') + 1;
390 package->state_status = status_parse(word_pointer, state_words_status);
392 package->state_want = status_parse("purge", state_words_want);
393 package->state_flag = status_parse("ok", state_words_flag);
394 package->state_status = status_parse("not-installed", state_words_status);
402 extern void write_package(FILE *out_file, package_t *pkg)
405 fprintf(out_file, "Package: %s\n", pkg->package);
407 if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
408 fprintf(out_file, "Status: %s %s %s\n",
409 state_words_want[pkg->state_want - 1],
410 state_words_flag[pkg->state_flag - 1],
411 state_words_status[pkg->state_status - 1]);
414 fprintf(out_file, "Depends: %s\n", pkg->depends);
417 fprintf(out_file, "Provides: %s\n", pkg->provides);
420 fprintf(out_file, "Priority: %s\n", pkg->priority);
423 fprintf(out_file, "Section: %s\n", pkg->section);
426 fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
428 if (pkg->maintainer) {
429 fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
432 fprintf(out_file, "Source: %s\n", pkg->source);
435 fprintf(out_file, "Version: %s\n", pkg->version);
437 if (pkg->pre_depends) {
438 fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
441 fprintf(out_file, "Replaces: %s\n", pkg->replaces);
443 if (pkg->recommends) {
444 fprintf(out_file, "Recommends: %s\n", pkg->recommends);
447 fprintf(out_file, "Suggests: %s\n", pkg->suggests);
449 if (pkg->conflicts) {
450 fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
452 if (pkg->conffiles) {
453 fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
455 if (pkg->architecture) {
456 fprintf(out_file, "Architecture: %s\n", pkg->architecture);
459 fprintf(out_file, "Filename: %s\n", pkg->filename);
462 fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
464 if (pkg->installer_menu_item) {
465 fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
467 if (pkg->description) {
468 fprintf(out_file, "Description: %s\n", pkg->description);
470 fputc('\n', out_file);
474 static void *status_read(void)
478 package_t *m = 0, *p = 0, *t = 0;
479 char *package_control_buffer = NULL;
481 if (getenv(udpkg_quiet) == NULL) {
482 printf("(Reading database...)\n");
485 if ((f = wfopen(statusfile, "r")) == NULL) {
489 while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) {
490 m = (package_t *)xcalloc(1, sizeof(package_t));
491 fill_package_struct(m, package_control_buffer);
494 * If there is an item in the tree by this name,
495 * it must be a virtual package; insert real
496 * package in preference.
498 tdelete(m, &status, package_compare);
499 tsearch(m, &status, package_compare);
502 * A "Provides" triggers the insertion
503 * of a pseudo package into the status
506 p = (package_t *)xcalloc(1, sizeof(package_t));
507 p->package = xstrdup(m->provides);
508 t = *(package_t **)tsearch(p, &status, package_compare);
514 * Pseudo package status is the
515 * same as the status of the
516 * package providing it
517 * FIXME: (not quite right, if 2
518 * packages of different statuses
521 t->state_want = m->state_want;
522 t->state_flag = m->state_flag;
523 t->state_status = m->state_status;
535 static int status_merge(void *status, package_t *pkgs)
539 package_t *pkg = 0, *statpkg = 0;
542 if ((fout = wfopen(new_statusfile, "w")) == NULL) {
545 if (getenv(udpkg_quiet) == NULL) {
546 printf("(Updating database...)\n");
550 * Dont use wfopen here, handle errors ourself
552 if ((fin = fopen(statusfile, "r")) != NULL) {
553 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
554 chomp(line); /* trim newline */
555 /* If we see a package header, find out if it's a package
556 * that we have processed. if so, we skip that block for
557 * now (write it at the end).
559 * we also look at packages in the status cache and update
560 * their status fields
562 if (strstr(line, "Package: ") == line) {
563 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
564 pkg->package) != 0; pkg = pkg->next) ;
566 locpkg.package = line + 9;
567 statpkg = tfind(&locpkg, &status, package_compare);
569 /* note: statpkg should be non-zero, unless the status
570 * file was changed while we are processing (no locking
571 * is currently done...
574 statpkg = *(package_t **)statpkg;
580 if (strstr(line, "Status: ") == line && statpkg != 0) {
581 snprintf(line, sizeof(line), "Status: %s %s %s",
582 state_words_want[statpkg->state_want - 1],
583 state_words_flag[statpkg->state_flag - 1],
584 state_words_status[statpkg->state_status - 1]);
586 fprintf(fout, "%s\n", line);
592 // Print out packages we processed.
593 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
594 write_package(fout, pkg);
599 * Its ok if renaming statusfile fails becasue it doesnt exist
601 if (rename(statusfile, bak_statusfile) == -1) {
602 struct stat stat_buf;
603 if (stat(statusfile, &stat_buf) == 0) {
604 error_msg("Couldnt create backup status file");
605 return(EXIT_FAILURE);
607 error_msg("No status file found, creating new one");
610 if (rename(new_statusfile, statusfile) == -1) {
611 error_msg("Couldnt create status file");
612 return(EXIT_FAILURE);
614 return(EXIT_SUCCESS);
617 static int is_file(const char *fn)
621 if (stat(fn, &statbuf) < 0) {
624 return S_ISREG(statbuf.st_mode);
627 static int dpkg_doconfigure(package_t *pkg)
633 DPRINTF("Configuring %s\n", pkg->package);
634 pkg->state_status = 0;
635 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
637 if (is_file(postinst)) {
638 snprintf(buf, sizeof(buf), "%s configure", postinst);
639 if ((r = do_system(buf)) != 0) {
640 error_msg("postinst exited with status %d\n", r);
641 pkg->state_status = state_status_halfconfigured;
645 pkg->state_status = state_status_installed;
650 static int dpkg_dounpack(package_t *pkg)
657 DPRINTF("Unpacking %s\n", pkg->package);
659 /* extract the data file */
660 deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL);
662 /* extract the control files */
663 info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1);
664 sprintf(info_prefix, "%s/%s.", infodir, pkg->package);
665 deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL);
667 /* Create the list file */
668 strcat(info_prefix, "list");
669 out_stream = wfopen(info_prefix, "w");
670 deb_extract(pkg->filename, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL);
673 pkg->state_want = state_want_install;
674 pkg->state_flag = state_flag_ok;
676 if (status == TRUE) {
677 pkg->state_status = state_status_unpacked;
679 pkg->state_status = state_status_halfinstalled;
686 * Extract and parse the control file from control.tar.gz
688 static int dpkg_read_control(package_t *pkg)
691 char *control_buffer = NULL;
693 if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
696 control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
697 fill_package_struct(pkg, control_buffer);
701 static int dpkg_unpack(package_t *pkgs, void *status)
706 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
707 dpkg_read_control(pkg);
708 if ((r = dpkg_dounpack(pkg)) != 0 ) {
712 status_merge(status, pkgs);
717 static int dpkg_configure(package_t *pkgs, void *status)
723 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
724 found = tfind(pkg, &status, package_compare);
727 error_msg("Trying to configure %s, but it is not installed", pkg->package);
730 /* configure the package listed in the status file;
731 * not pkg, as we have info only for the latter
734 r = dpkg_doconfigure(*(package_t **)found);
737 status_merge(status, 0);
742 static int dpkg_install(package_t *pkgs, void *status)
744 package_t *p, *ordered = 0;
746 /* Stage 1: parse all the control information */
747 for (p = pkgs; p != 0; p = p->next) {
748 dpkg_read_control(p);
751 /* Stage 2: resolve dependencies */
753 ordered = depends_resolve(pkgs, status);
758 /* Stage 3: install */
759 for (p = ordered; p != 0; p = p->next) {
760 p->state_want = state_want_install;
762 /* for now the flag is always set to ok... this is probably
765 p->state_flag = state_flag_ok;
767 DPRINTF("Installing %s\n", p->package);
768 if (dpkg_dounpack(p) != 0) {
769 perror_msg(p->filename);
772 if (dpkg_doconfigure(p) != 0) {
773 perror_msg(p->filename);
778 status_merge(status, pkgs);
785 * Not implemented yet
787 static int dpkg_remove(package_t *pkgs, void *status)
791 for (p = pkgs; p != 0; p = p->next)
794 status_merge(status, 0);
800 extern int dpkg_main(int argc, char **argv)
802 const int arg_install = 1;
803 const int arg_unpack = 2;
804 const int arg_configure = 4;
806 package_t *p, *packages = NULL;
811 while ((opt = getopt(argc, argv, "iruc")) != -1) {
814 optflag |= arg_install;
817 optflag |= arg_unpack;
820 optflag |= arg_configure;
827 while (optind < argc) {
828 p = (package_t *) xcalloc(1, sizeof(package_t));
829 if (optflag & arg_configure) {
830 p->package = xstrdup(argv[optind]);
832 p->filename = xstrdup(argv[optind]);
840 make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR);
842 status = status_read();
844 if (optflag & arg_install) {
845 return dpkg_install(packages, status);
847 else if (optflag & arg_unpack) {
848 return dpkg_unpack(packages, status);
850 else if (optflag & arg_configure) {
851 return dpkg_configure(packages, status);
853 return(EXIT_FAILURE);