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);
122 static char **depends_split(const char *dependsstr)
124 static char *dependsvec[DEPENDSMAX];
129 if (dependsstr == 0) {
133 p = xstrdup(dependsstr);
134 while (*p != 0 && *p != '\n') {
140 if (dependsvec[i] == 0) {
145 *p = 0; /* eat the space... */
156 /* Topological sort algorithm:
157 * ordered is the output list, pkgs is the dependency graph, pkg is
160 * recursively add all the adjacent nodes to the ordered list, marking
161 * each one as visited along the way
163 * yes, this algorithm looks a bit odd when all the params have the
166 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
171 /* mark node as processing */
172 pkg->color = color_grey;
174 /* visit each not-yet-visited node */
175 for (i = 0; i < pkg->requiredcount; i++)
176 if (pkg->requiredfor[i]->color == color_white)
177 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
180 /* add it to the list */
181 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
182 /* make a shallow copy */
184 newnode->next = *ordered;
188 pkg->next = *ordered;
191 /* mark node as done */
192 pkg->color = color_black;
195 static package_t *depends_sort(package_t *pkgs)
197 /* TODO: it needs to break cycles in the to-be-installed package
199 package_t *ordered = NULL;
202 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
203 pkg->color = color_white;
205 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
206 if (pkg->color == color_white) {
207 depends_sort_visit(&ordered, pkgs, pkg);
211 /* Leaks the old list... return the new one... */
216 /* resolve package dependencies --
217 * for each package in the list of packages to be installed, we parse its
218 * dependency info to determine if the dependent packages are either
219 * already installed, or are scheduled to be installed. If both tests fail
222 * The algorithm here is O(n^2*m) where n = number of packages to be
223 * installed and m is the # of dependencies per package. Not a terribly
224 * efficient algorithm, but given that at any one time you are unlikely
225 * to install a very large number of packages it doesn't really matter
227 static package_t *depends_resolve(package_t *pkgs, void *status)
229 package_t *pkg, *chk;
235 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
236 dependsvec = depends_split(pkg->depends);
238 while (dependsvec[i] != 0) {
239 /* Check for dependencies; first look for installed packages */
240 dependpkg.package = dependsvec[i];
241 if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
242 ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
243 (chk->state_status & state_status_installed))) {
245 /* if it fails, we look through the list of packages we are going to
247 for (chk = pkgs; chk != 0; chk = chk->next) {
248 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
249 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
250 if (chk->requiredcount >= DEPENDSMAX) {
251 error_msg("Too many dependencies for %s", chk->package);
255 chk->requiredfor[chk->requiredcount++] = pkg;
261 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
269 return depends_sort(pkgs);
273 /* Status file handling routines
275 * This is a fairly minimalistic implementation. there are two main functions
276 * that are supported:
278 * 1) reading the entire status file:
279 * the status file is read into memory as a binary-tree, with just the
280 * package and status info preserved
282 * 2) merging the status file
283 * control info from (new) packages is merged into the status file,
284 * replacing any pre-existing entries. when a merge happens, status info
285 * read using the status_read function is written back to the status file
287 static unsigned char status_parse(const char *line, const char **status_words)
289 unsigned char status_num;
292 while (status_words[i] != 0) {
293 if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
294 status_num = (char)i;
300 error_msg("Invalid status word");
305 * Read the buffered control file and parse it,
306 * filling parsed fields into the package structure
308 static int fill_package_struct(package_t *package, const char *package_buffer)
312 int field_length = 0;
314 while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
315 field_length = strlen(field);
316 field_start += (field_length + 1);
318 if (strlen(field) == 0) {
319 printf("empty line: *this shouldnt happen i dont think*\n");
323 /* these are common to both installed and uninstalled packages */
324 if (strstr(field, "Package: ") == field) {
325 package->package = strdup(field + 9);
327 else if (strstr(field, "Depends: ") == field) {
328 package->depends = strdup(field + 9);
330 else if (strstr(field, "Provides: ") == field) {
331 package->provides = strdup(field + 10);
333 /* This is specific to the Debian Installer. Ifdef? */
334 else if (strstr(field, "installer-menu-item: ") == field) {
335 package->installer_menu_item = atoi(field + 21);
337 else if (strstr(field, "Description: ") == field) {
338 package->description = strdup(field + 13);
340 else if (strstr(field, "Priority: ") == field) {
341 package->priority = strdup(field + 10);
343 else if (strstr(field, "Section: ") == field) {
344 package->section = strdup(field + 9);
346 else if (strstr(field, "Installed-Size: ") == field) {
347 package->installed_size = strdup(field + 16);
349 else if (strstr(field, "Maintainer: ") == field) {
350 package->maintainer = strdup(field + 12);
352 else if (strstr(field, "Version: ") == field) {
353 package->version = strdup(field + 9);
355 else if (strstr(field, "Suggests: ") == field) {
356 package->suggests = strdup(field + 10);
358 else if (strstr(field, "Recommends: ") == field) {
359 package->recommends = strdup(field + 12);
361 /* else if (strstr(field, "Conffiles: ") == field) {
362 package->conffiles = read_block(file);
363 package->conffiles = xcalloc(1, 1);
364 while ((field = strtok(NULL, "\n")) != NULL) {
365 package->long_description = xrealloc(package->conffiles,
366 strlen(package->conffiles) + strlen(field) + 1);
367 strcat(package->conffiles, field);
371 /* These are only in available file */
372 else if (strstr(field, "Architecture: ") == field) {
373 package->architecture = strdup(field + 14);
375 else if (strstr(field, "Filename: ") == field) {
376 package->filename = strdup(field + 10);
378 else if (strstr(field, "MD5sum ") == field) {
379 package->md5sum = strdup(field + 7);
382 /* This is only needed for status file */
383 if (strstr(field, "Status: ") == field) {
386 word_pointer = strchr(field, ' ') + 1;
387 package->state_want = status_parse(word_pointer, state_words_want);
388 word_pointer = strchr(word_pointer, ' ') + 1;
389 package->state_flag = status_parse(word_pointer, state_words_flag);
390 word_pointer = strchr(word_pointer, ' ') + 1;
391 package->state_status = status_parse(word_pointer, state_words_status);
393 package->state_want = status_parse("purge", state_words_want);
394 package->state_flag = status_parse("ok", state_words_flag);
395 package->state_status = status_parse("not-installed", state_words_status);
403 extern void write_package(FILE *out_file, package_t *pkg)
406 fprintf(out_file, "Package: %s\n", pkg->package);
408 if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
409 fprintf(out_file, "Status: %s %s %s\n",
410 state_words_want[pkg->state_want - 1],
411 state_words_flag[pkg->state_flag - 1],
412 state_words_status[pkg->state_status - 1]);
415 fprintf(out_file, "Depends: %s\n", pkg->depends);
418 fprintf(out_file, "Provides: %s\n", pkg->provides);
421 fprintf(out_file, "Priority: %s\n", pkg->priority);
424 fprintf(out_file, "Section: %s\n", pkg->section);
427 fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
429 if (pkg->maintainer) {
430 fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
433 fprintf(out_file, "Source: %s\n", pkg->source);
436 fprintf(out_file, "Version: %s\n", pkg->version);
438 if (pkg->pre_depends) {
439 fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
442 fprintf(out_file, "Replaces: %s\n", pkg->replaces);
444 if (pkg->recommends) {
445 fprintf(out_file, "Recommends: %s\n", pkg->recommends);
448 fprintf(out_file, "Suggests: %s\n", pkg->suggests);
450 if (pkg->conflicts) {
451 fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
453 if (pkg->conffiles) {
454 fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
456 if (pkg->architecture) {
457 fprintf(out_file, "Architecture: %s\n", pkg->architecture);
460 fprintf(out_file, "Filename: %s\n", pkg->filename);
463 fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
465 if (pkg->installer_menu_item) {
466 fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
468 if (pkg->description) {
469 fprintf(out_file, "Description: %s\n", pkg->description);
471 fputc('\n', out_file);
475 static void *status_read(void)
479 package_t *m = 0, *p = 0, *t = 0;
480 char *package_control_buffer = NULL;
482 if (getenv(udpkg_quiet) == NULL) {
483 printf("(Reading database...)\n");
486 if ((f = wfopen(statusfile, "r")) == NULL) {
490 while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) {
491 m = (package_t *)xcalloc(1, sizeof(package_t));
492 fill_package_struct(m, package_control_buffer);
495 * If there is an item in the tree by this name,
496 * it must be a virtual package; insert real
497 * package in preference.
499 tdelete(m, &status, package_compare);
500 tsearch(m, &status, package_compare);
503 * A "Provides" triggers the insertion
504 * of a pseudo package into the status
507 p = (package_t *)xcalloc(1, sizeof(package_t));
508 p->package = xstrdup(m->provides);
509 t = *(package_t **)tsearch(p, &status, package_compare);
515 * Pseudo package status is the
516 * same as the status of the
517 * package providing it
518 * FIXME: (not quite right, if 2
519 * packages of different statuses
522 t->state_want = m->state_want;
523 t->state_flag = m->state_flag;
524 t->state_status = m->state_status;
536 static int status_merge(void *status, package_t *pkgs)
540 package_t *pkg = 0, *statpkg = 0;
543 if ((fout = wfopen(new_statusfile, "w")) == NULL) {
546 if (getenv(udpkg_quiet) == NULL) {
547 printf("(Updating database...)\n");
551 * Dont use wfopen here, handle errors ourself
553 if ((fin = fopen(statusfile, "r")) != NULL) {
554 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
555 chomp(line); /* trim newline */
556 /* If we see a package header, find out if it's a package
557 * that we have processed. if so, we skip that block for
558 * now (write it at the end).
560 * we also look at packages in the status cache and update
561 * their status fields
563 if (strstr(line, "Package: ") == line) {
564 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
565 pkg->package) != 0; pkg = pkg->next) ;
567 locpkg.package = line + 9;
568 statpkg = tfind(&locpkg, &status, package_compare);
570 /* note: statpkg should be non-zero, unless the status
571 * file was changed while we are processing (no locking
572 * is currently done...
575 statpkg = *(package_t **)statpkg;
581 if (strstr(line, "Status: ") == line && statpkg != 0) {
582 snprintf(line, sizeof(line), "Status: %s %s %s",
583 state_words_want[statpkg->state_want - 1],
584 state_words_flag[statpkg->state_flag - 1],
585 state_words_status[statpkg->state_status - 1]);
587 fprintf(fout, "%s\n", line);
593 // Print out packages we processed.
594 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
595 write_package(fout, pkg);
600 * Its ok if renaming statusfile fails becasue it doesnt exist
602 if (rename(statusfile, bak_statusfile) == -1) {
603 struct stat stat_buf;
604 if (stat(statusfile, &stat_buf) == 0) {
605 error_msg("Couldnt create backup status file");
606 return(EXIT_FAILURE);
608 error_msg("No status file found, creating new one");
611 if (rename(new_statusfile, statusfile) == -1) {
612 error_msg("Couldnt create status file");
613 return(EXIT_FAILURE);
615 return(EXIT_SUCCESS);
618 static int is_file(const char *fn)
622 if (stat(fn, &statbuf) < 0) {
625 return S_ISREG(statbuf.st_mode);
628 static int dpkg_doconfigure(package_t *pkg)
634 DPRINTF("Configuring %s\n", pkg->package);
635 pkg->state_status = 0;
636 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
638 if (is_file(postinst)) {
639 snprintf(buf, sizeof(buf), "%s configure", postinst);
640 if ((r = do_system(buf)) != 0) {
641 error_msg("postinst exited with status %d\n", r);
642 pkg->state_status = state_status_halfconfigured;
646 pkg->state_status = state_status_installed;
651 static int dpkg_dounpack(package_t *pkg)
657 DPRINTF("Unpacking %s\n", pkg->package);
659 /* extract the data file */
660 deb_extract(pkg->filename, extract_extract, "/", NULL);
662 /* extract the control files */
663 deb_extract(pkg->filename, extract_control, infodir, pkg->package);
665 /* Create the list file */
666 lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
667 strcpy(lst_path, infodir);
668 strcat(lst_path, pkg->package);
669 strcat(lst_path, ".list");
670 deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL);
672 pkg->state_want = state_want_install;
673 pkg->state_flag = state_flag_ok;
675 if (status == TRUE) {
676 pkg->state_status = state_status_unpacked;
678 pkg->state_status = state_status_halfinstalled;
685 * Extract and parse the control file from control.tar.gz
687 static int dpkg_read_control(package_t *pkg)
690 char *control_buffer = NULL;
692 if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
695 control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL);
696 fill_package_struct(pkg, control_buffer);
700 static int dpkg_unpack(package_t *pkgs, void *status)
705 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
706 dpkg_read_control(pkg);
707 if ((r = dpkg_dounpack(pkg)) != 0 ) {
711 status_merge(status, pkgs);
716 static int dpkg_configure(package_t *pkgs, void *status)
722 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
723 found = tfind(pkg, &status, package_compare);
726 error_msg("Trying to configure %s, but it is not installed", pkg->package);
729 /* configure the package listed in the status file;
730 * not pkg, as we have info only for the latter
733 r = dpkg_doconfigure(*(package_t **)found);
736 status_merge(status, 0);
741 static int dpkg_install(package_t *pkgs, void *status)
743 package_t *p, *ordered = 0;
745 /* Stage 1: parse all the control information */
746 for (p = pkgs; p != 0; p = p->next) {
747 dpkg_read_control(p);
750 /* Stage 2: resolve dependencies */
752 ordered = depends_resolve(pkgs, status);
757 /* Stage 3: install */
758 for (p = ordered; p != 0; p = p->next) {
759 p->state_want = state_want_install;
761 /* for now the flag is always set to ok... this is probably
764 p->state_flag = state_flag_ok;
766 DPRINTF("Installing %s\n", p->package);
767 if (dpkg_dounpack(p) != 0) {
768 perror_msg(p->filename);
771 if (dpkg_doconfigure(p) != 0) {
772 perror_msg(p->filename);
777 status_merge(status, pkgs);
784 * Not implemented yet
786 static int dpkg_remove(package_t *pkgs, void *status)
790 for (p = pkgs; p != 0; p = p->next)
793 status_merge(status, 0);
799 extern int dpkg_main(int argc, char **argv)
801 const int arg_install = 1;
802 const int arg_unpack = 2;
803 const int arg_configure = 4;
805 package_t *p, *packages = NULL;
810 while ((opt = getopt(argc, argv, "iruc")) != -1) {
813 optflag |= arg_install;
816 optflag |= arg_unpack;
819 optflag |= arg_configure;
826 while (optind < argc) {
827 p = (package_t *) xcalloc(1, sizeof(package_t));
828 if (optflag & arg_configure) {
829 p->package = xstrdup(argv[optind]);
831 p->filename = xstrdup(argv[optind]);
839 create_path(infodir, S_IRWXU);
841 status = status_read();
843 if (optflag & arg_install) {
844 return dpkg_install(packages, status);
846 else if (optflag & arg_unpack) {
847 return dpkg_unpack(packages, status);
849 else if (optflag & arg_configure) {
850 return dpkg_configure(packages, status);
852 return(EXIT_FAILURE);