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;
344 while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
345 field_length = strlen(field);
346 field_start += (field_length + 1);
348 if (strlen(field) == 0) {
349 printf("empty line: *this shouldnt happen i dont think*\n");
353 /* these are common to both installed and uninstalled packages */
354 if (strstr(field, "Package: ") == field) {
355 package->package = strdup(field + 9);
357 else if (strstr(field, "Depends: ") == field) {
358 package->depends = strdup(field + 9);
360 else if (strstr(field, "Provides: ") == field) {
361 package->provides = strdup(field + 10);
363 /* This is specific to the Debian Installer. Ifdef? */
364 else if (strstr(field, "installer-menu-item: ") == field) {
365 package->installer_menu_item = atoi(field + 21);
367 else if (strstr(field, "Description: ") == field) {
368 package->description = strdup(field + 13);
370 else if (strstr(field, "Priority: ") == field) {
371 package->priority = strdup(field + 10);
373 else if (strstr(field, "Section: ") == field) {
374 package->section = strdup(field + 9);
376 else if (strstr(field, "Installed-Size: ") == field) {
377 package->installed_size = strdup(field + 16);
379 else if (strstr(field, "Maintainer: ") == field) {
380 package->maintainer = strdup(field + 12);
382 else if (strstr(field, "Version: ") == field) {
383 package->version = strdup(field + 9);
385 else if (strstr(field, "Suggests: ") == field) {
386 package->suggests = strdup(field + 10);
388 else if (strstr(field, "Recommends: ") == field) {
389 package->recommends = strdup(field + 12);
391 /* else if (strstr(field, "Conffiles: ") == field) {
392 package->conffiles = read_block(file);
393 package->conffiles = xcalloc(1, 1);
394 while ((field = strtok(NULL, "\n")) != NULL) {
395 package->long_description = xrealloc(package->conffiles,
396 strlen(package->conffiles) + strlen(field) + 1);
397 strcat(package->conffiles, field);
401 /* These are only in available file */
402 else if (strstr(field, "Architecture: ") == field) {
403 package->architecture = strdup(field + 14);
405 else if (strstr(field, "Filename: ") == field) {
406 package->filename = strdup(field + 10);
408 else if (strstr(field, "MD5sum ") == field) {
409 package->md5sum = strdup(field + 7);
412 /* This is only needed for status file */
413 if (strstr(field, "Status: ") == field) {
416 word_pointer = strchr(field, ' ') + 1;
417 package->state_want = status_parse(word_pointer, state_words_want);
418 word_pointer = strchr(word_pointer, ' ') + 1;
419 package->state_flag = status_parse(word_pointer, state_words_flag);
420 word_pointer = strchr(word_pointer, ' ') + 1;
421 package->state_status = status_parse(word_pointer, state_words_status);
423 package->state_want = status_parse("purge", state_words_want);
424 package->state_flag = status_parse("ok", state_words_flag);
425 package->state_status = status_parse("not-installed", state_words_status);
433 static void *status_read(void)
437 package_t *m = 0, *p = 0, *t = 0;
438 char *package_control_buffer = NULL;
440 if (getenv(udpkg_quiet) == NULL) {
441 printf("(Reading database...)\n");
444 if ((f = wfopen(statusfile, "r")) == NULL) {
448 while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) {
449 m = (package_t *)xcalloc(1, sizeof(package_t));
450 printf("read buffer [%s]\n", package_control_buffer);
451 fill_package_struct(m, package_control_buffer);
452 printf("package is [%s]\n", m->package);
455 * If there is an item in the tree by this name,
456 * it must be a virtual package; insert real
457 * package in preference.
459 tdelete(m, &status, package_compare);
460 tsearch(m, &status, package_compare);
463 * A "Provides" triggers the insertion
464 * of a pseudo package into the status
467 p = (package_t *)xcalloc(1, sizeof(package_t));
468 p->package = xstrdup(m->provides);
469 t = *(package_t **)tsearch(p, &status, package_compare);
475 * Pseudo package status is the
476 * same as the status of the
477 * package providing it
478 * FIXME: (not quite right, if 2
479 * packages of different statuses
482 t->state_want = m->state_want;
483 t->state_flag = m->state_flag;
484 t->state_status = m->state_status;
497 static int status_merge(void *status, package_t *pkgs)
501 package_t *pkg = 0, *statpkg = 0;
504 if ((fout = wfopen(new_statusfile, "w")) == NULL) {
507 if (getenv(udpkg_quiet) == NULL) {
508 printf("(Updating database...)\n");
512 * Dont use wfopen here, handle errors ourself
514 if ((fin = fopen(statusfile, "r")) != NULL) {
515 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
516 line[strlen(line) - 1] = '\0'; /* trim newline */
517 /* If we see a package header, find out if it's a package
518 * that we have processed. if so, we skip that block for
519 * now (write it at the end).
521 * we also look at packages in the status cache and update
522 * their status fields
524 if (strstr(line, "Package: ") == line) {
525 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
526 pkg->package) != 0; pkg = pkg->next) ;
528 locpkg.package = line + 9;
529 statpkg = tfind(&locpkg, &status, package_compare);
531 /* note: statpkg should be non-zero, unless the status
532 * file was changed while we are processing (no locking
533 * is currently done...
536 statpkg = *(package_t **)statpkg;
542 if (strstr(line, "Status: ") == line && statpkg != 0) {
543 snprintf(line, sizeof(line), "Status: %s %s %s",
544 state_words_want[statpkg->state_want - 1],
545 state_words_flag[statpkg->state_flag - 1],
546 state_words_status[statpkg->state_status - 1]);
548 fprintf(fout, "%s\n", line);
554 // Print out packages we processed.
555 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
556 fprintf(fout, "Package: %s\nStatus: %s %s %s\n",
557 pkg->package, state_words_want[pkg->state_want - 1],
558 state_words_flag[pkg->state_flag - 1],
559 state_words_status[pkg->state_status - 1]);
562 fprintf(fout, "Depends: %s\n", pkg->depends);
564 fprintf(fout, "Provides: %s\n", pkg->provides);
565 if (pkg->installer_menu_item)
566 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
567 if (pkg->description)
568 fprintf(fout, "Description: %s\n", pkg->description);
574 * Its ok if renaming statusfile fails becasue it doesnt exist
576 if (rename(statusfile, bak_statusfile) == -1) {
577 struct stat stat_buf;
578 if (stat(statusfile, &stat_buf) == 0) {
579 error_msg("Couldnt create backup status file");
580 return(EXIT_FAILURE);
582 error_msg("No status file found, creating new one");
585 if (rename(new_statusfile, statusfile) == -1) {
586 error_msg("Couldnt create status file");
587 return(EXIT_FAILURE);
589 return(EXIT_SUCCESS);
592 static int is_file(const char *fn)
596 if (stat(fn, &statbuf) < 0) {
599 return S_ISREG(statbuf.st_mode);
602 static int dpkg_doconfigure(package_t *pkg)
608 DPRINTF("Configuring %s\n", pkg->package);
609 pkg->state_status = 0;
610 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
612 if (is_file(postinst)) {
613 snprintf(buf, sizeof(buf), "%s configure", postinst);
614 if ((r = do_system(buf)) != 0) {
615 error_msg("postinst exited with status %d\n", r);
616 pkg->state_status = state_status_halfconfigured;
620 pkg->state_status = state_status_installed;
625 static int dpkg_dounpack(package_t *pkg)
631 DPRINTF("Unpacking %s\n", pkg->package);
633 /* extract the data file */
634 deb_extract(pkg->filename, extract_extract, "/", NULL);
636 /* extract the control files */
637 deb_extract(pkg->filename, extract_control, infodir, pkg->package);
639 /* Create the list file */
640 lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
641 strcpy(lst_path, infodir);
642 strcat(lst_path, pkg->package);
643 strcat(lst_path, ".list");
644 deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL);
646 pkg->state_want = state_want_install;
647 pkg->state_flag = state_flag_ok;
649 if (status == TRUE) {
650 pkg->state_status = state_status_unpacked;
652 pkg->state_status = state_status_halfinstalled;
659 * Extract and parse the control file from control.tar.gz
661 static int dpkg_read_control(package_t *pkg)
664 char *control_buffer = NULL;
666 if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
669 control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL);
670 fill_package_struct(pkg, control_buffer);
674 static int dpkg_unpack(package_t *pkgs, void *status)
679 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
680 dpkg_read_control(pkg);
681 if ((r = dpkg_dounpack(pkg)) != 0 ) {
685 status_merge(status, pkgs);
690 static int dpkg_configure(package_t *pkgs, void *status)
696 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
697 found = tfind(pkg, &status, package_compare);
700 error_msg("Trying to configure %s, but it is not installed", pkg->package);
703 /* configure the package listed in the status file;
704 * not pkg, as we have info only for the latter
707 r = dpkg_doconfigure(*(package_t **)found);
710 status_merge(status, 0);
715 static int dpkg_install(package_t *pkgs, void *status)
717 package_t *p, *ordered = 0;
719 /* Stage 1: parse all the control information */
720 for (p = pkgs; p != 0; p = p->next) {
721 dpkg_read_control(p);
724 /* Stage 2: resolve dependencies */
726 ordered = depends_resolve(pkgs, status);
731 /* Stage 3: install */
732 for (p = ordered; p != 0; p = p->next) {
733 p->state_want = state_want_install;
735 /* for now the flag is always set to ok... this is probably
738 p->state_flag = state_flag_ok;
740 DPRINTF("Installing %s\n", p->package);
741 if (dpkg_dounpack(p) != 0) {
742 perror_msg(p->filename);
745 if (dpkg_doconfigure(p) != 0) {
746 perror_msg(p->filename);
751 status_merge(status, pkgs);
758 * Not implemented yet
760 static int dpkg_remove(package_t *pkgs, void *status)
764 for (p = pkgs; p != 0; p = p->next)
767 status_merge(status, 0);
773 extern int dpkg_main(int argc, char **argv)
775 const int arg_install = 1;
776 const int arg_unpack = 2;
777 const int arg_configure = 4;
779 package_t *p, *packages = NULL;
784 while ((opt = getopt(argc, argv, "iruc")) != -1) {
787 optflag |= arg_install;
790 optflag |= arg_unpack;
793 optflag |= arg_configure;
800 while (optind < argc) {
801 p = (package_t *) xcalloc(1, sizeof(package_t));
802 if (optflag & arg_configure) {
803 p->package = xstrdup(argv[optind]);
805 p->filename = xstrdup(argv[optind]);
813 create_path(infodir, S_IRWXU);
815 status = status_read();
817 if (optflag & arg_install) {
818 return dpkg_install(packages, status);
820 else if (optflag & arg_unpack) {
821 return dpkg_unpack(packages, status);
823 else if (optflag & arg_configure) {
824 return dpkg_configure(packages, status);
826 return(EXIT_FAILURE);