14 //#define PACKAGE "udpkg"
15 //#define VERSION "0.1"
18 * Should we do full dependency checking?
23 * Should we do debugging?
28 #define SYSTEM(x) do_system(x)
29 #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
31 #define SYSTEM(x) system(x)
32 #define DPRINTF(fmt,args...) /* nothing */
36 #define DEPENDSMAX 64 /* maximum number of depends we can handle */
38 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
39 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
40 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
42 static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
43 static const char rm_dpkgcidir[] = "rm -rf /var/lib/dpkg/tmp.ci/";
45 static const char infodir[] = "/var/lib/dpkg/info/";
46 static const char udpkg_quiet[] = "UDPKG_QUIET";
48 //static const int status_wantstart = 0;
49 //static const int status_wantunknown = (1 << 0);
50 static const int status_wantinstall = (1 << 1);
51 //static const int status_wanthold = (1 << 2);
52 //static const int status_wantdeinstall = (1 << 3);
53 //static const int status_wantpurge = (1 << 4);
54 static const int status_wantmask = 31;
56 //static const int status_flagstart = 5;
57 static const int status_flagok = (1 << 5); /* 32 */
58 //static const int status_flagreinstreq = (1 << 6);
59 //static const int status_flaghold = (1 << 7);
60 //static const int status_flagholdreinstreq = (1 << 8);
61 static const int status_flagmask = 480;
63 //static const int status_statusstart = 9;
64 //static const int status_statusnoninstalled = (1 << 9); /* 512 */
65 static const int status_statusunpacked = (1 << 10);
66 static const int status_statushalfconfigured = (1 << 11);
67 static const int status_statusinstalled = (1 << 12);
68 static const int status_statushalfinstalled = (1 << 13);
69 //static const int status_statusconfigfiles = (1 << 14);
70 //static const int status_statuspostinstfailed = (1 << 15);
71 //static const int status_statusremovalfailed = (1 << 16);
72 static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */
74 static const char *statuswords[][10] = {
75 { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
76 { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
77 { (char *) 9, "not-installed", "unpacked", "half-configured",
78 "installed", "half-installed", "config-files",
79 "post-inst-failed", "removal-failed", 0 }
82 const int color_white = 0;
83 const int color_grey = 1;
84 const int color_black = 2;
87 typedef struct package_s {
94 int installer_menu_item;
96 char color; /* for topo-sort */
97 struct package_s *requiredfor[DEPENDSMAX];
98 unsigned short requiredcount;
99 struct package_s *next;
103 static int do_system(const char *cmd)
105 DPRINTF("cmd is %s\n", cmd);
109 #define do_system(cmd) system(cmd)
112 static int package_compare(const void *p1, const void *p2)
114 return strcmp(((package_t *)p1)->package,
115 ((package_t *)p2)->package);
121 static char **depends_split(const char *dependsstr)
123 static char *dependsvec[DEPENDSMAX];
128 if (dependsstr == 0) {
132 p = strdup(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 *)malloc(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) &&
242 (chk->status & (status_flagok | status_statusinstalled)) !=
243 (status_flagok | status_statusinstalled))) {
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 fprintf(stderr, "Too many dependencies for %s\n", chk->package);
255 chk->requiredfor[chk->requiredcount++] = pkg;
261 fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", 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 long status_parse(const char *line)
293 for (i = 0; i < 3; i++) {
294 if ((p = strchr(line, ' ')) != NULL) {
298 while (statuswords[i][j] != 0) {
299 if (strcmp(line, statuswords[i][j]) == 0) {
300 l |= (1 << ((int)statuswords[i][0] + j - 1));
306 if (statuswords[i][j] == 0) {
315 static const char *status_print(unsigned long flags)
317 /* this function returns a static buffer... */
318 static char buf[256];
322 for (i = 0; i < 3; i++) {
324 while (statuswords[i][j] != 0) {
325 if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) {
326 strcat(buf, statuswords[i][j]);
327 if (i < 2) strcat(buf, " ");
332 if (statuswords[i][j] == 0) {
333 fprintf(stderr, "corrupted status flag!!\n");
342 * Read a control file (or a stanza of a status file) and parse it,
343 * filling parsed fields into the package structure
345 static void control_read(FILE *file, package_t *p)
349 while ((line = get_line_from_file(file)) != NULL) {
350 line[strlen(line)] = 0;
352 if (strlen(line) == 0) {
355 if (strstr(line, "Package: ") == line) {
356 p->package = strdup(line + 9);
358 if (strstr(line, "Status: ") == line) {
359 p->status = status_parse(line + 8);
361 if (strstr(line, "Depends: ") == line) {
362 p->depends = strdup(line + 9);
364 if (strstr(line, "Provides: ") == line) {
365 p->provides = strdup(line + 10);
367 if (strstr(line, "Description: ") == line) {
368 p->description = strdup(line + 13);
369 /* This is specific to the Debian Installer. Ifdef? */
371 if (strstr(line, "installer-menu-item: ") == line) {
372 p->installer_menu_item = atoi(line + 21);
374 /* TODO: localized descriptions */
379 static void *status_read(void)
383 package_t *m = 0, *p = 0, *t = 0;
385 if ((f = fopen(statusfile, "r")) == NULL) {
390 if (getenv(udpkg_quiet) == NULL) {
391 printf("(Reading database...)\n");
395 m = (package_t *)xmalloc(sizeof(package_t));
396 memset(m, 0, sizeof(package_t));
400 * If there is an item in the tree by this name,
401 * it must be a virtual package; insert real
402 * package in preference.
404 tdelete(m, &status, package_compare);
405 tsearch(m, &status, package_compare);
408 * A "Provides" triggers the insertion
409 * of a pseudo package into the status
412 p = (package_t *)xmalloc(sizeof(package_t));
413 memset(p, 0, sizeof(package_t));
414 p->package = strdup(m->provides);
416 t = *(package_t **)tsearch(p, &status, package_compare);
423 * Pseudo package status is the
424 * same as the status of the
425 * package providing it
426 * FIXME: (not quite right, if 2
427 * packages of different statuses
430 t->status = m->status;
442 static int status_merge(void *status, package_t *pkgs)
446 package_t *pkg = 0, *statpkg = 0;
450 if ((fin = fopen(statusfile, "r")) == NULL) {
454 if ((fout = fopen(new_statusfile, "w")) == NULL) {
455 perror(new_statusfile);
458 if (getenv(udpkg_quiet) == NULL) {
459 printf("(Updating database...)\n");
462 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
463 line[strlen(line)] = 0; /* trim newline */
464 /* If we see a package header, find out if it's a package
465 * that we have processed. if so, we skip that block for
466 * now (write it at the end).
468 * we also look at packages in the status cache and update
469 * their status fields
471 if (strstr(line, "Package: ") == line) {
472 for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
473 pkg->package, strlen(line) - 9) != 0;
476 locpkg.package = line + 9;
477 statpkg = tfind(&locpkg, &status, package_compare);
479 /* note: statpkg should be non-zero, unless the status
480 * file was changed while we are processing (no locking
481 * is currently done...
484 statpkg = *(package_t **)statpkg;
490 if (strstr(line, "Status: ") == line && statpkg != 0) {
491 snprintf(line, sizeof(line), "Status: %s",
492 status_print(statpkg->status));
499 // Print out packages we processed.
500 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
501 fprintf(fout, "Package: %s\nStatus: %s\n",
502 pkg->package, status_print(pkg->status));
504 fprintf(fout, "Depends: %s\n", pkg->depends);
506 fprintf(fout, "Provides: %s\n", pkg->provides);
507 if (pkg->installer_menu_item)
508 fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
509 if (pkg->description)
510 fprintf(fout, "Description: %s\n", pkg->description);
517 r = rename(statusfile, bak_statusfile);
519 r = rename(new_statusfile, statusfile);
525 static int is_file(const char *fn)
529 if (stat(fn, &statbuf) < 0) {
532 return S_ISREG(statbuf.st_mode);
535 static int dpkg_doconfigure(package_t *pkg)
541 DPRINTF("Configuring %s\n", pkg->package);
542 pkg->status &= status_statusmask;
543 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
545 if (is_file(postinst)) {
546 snprintf(buf, sizeof(buf), "%s configure", postinst);
547 if ((r = do_system(buf)) != 0) {
548 fprintf(stderr, "postinst exited with status %d\n", r);
549 pkg->status |= status_statushalfconfigured;
553 pkg->status |= status_statusinstalled;
558 static int dpkg_dounpack(package_t *pkg)
563 char buf[1024], buf2[1024];
564 char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
565 "conffiles", "md5sums", "shlibs", "templates" };
567 DPRINTF("Unpacking %s\n", pkg->package);
571 snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
572 if (SYSTEM(buf) != 0) {
575 /* Installs the package scripts into the info directory */
576 for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
577 snprintf(buf, sizeof(buf), "%s%s/%s",
578 dpkgcidir, pkg->package, adminscripts[i]);
579 snprintf(buf2, sizeof(buf), "%s%s.%s",
580 infodir, pkg->package, adminscripts[i]);
581 if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0) {
582 fprintf(stderr, "Cannot copy %s to %s: %s\n",
583 buf, buf2, strerror(errno));
587 /* ugly hack to create the list file; should
588 * probably do something more elegant
590 * why oh why does dpkg create the list file
593 snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -tf -", pkg->file);
594 snprintf(buf2, sizeof(buf2), "%s%s.list", infodir, pkg->package);
595 if ((infp = popen(buf, "r")) == NULL || (outfp = fopen(buf2, "w")) == NULL) {
596 fprintf(stderr, "Cannot create %s\n", buf2);
600 while (fgets(buf, sizeof(buf), infp) && !feof(infp)) {
605 if ((*p == '/') && (*(p + 1) == '\n')) {
610 if (p[strlen(p) - 2] == '/') {
611 p[strlen(p) - 2] = '\n';
612 p[strlen(p) - 1] = 0;
620 pkg->status &= status_wantmask;
621 pkg->status |= status_wantinstall;
622 pkg->status &= status_flagmask;
623 pkg->status |= status_flagok;
624 pkg->status &= status_statusmask;
627 pkg->status |= status_statusunpacked;
629 pkg->status |= status_statushalfinstalled;
636 static int dpkg_doinstall(package_t *pkg)
638 DPRINTF("Installing %s\n", pkg->package);
639 return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
642 static int dpkg_unpackcontrol(package_t *pkg)
650 p = strrchr(pkg->file, '/');
651 if (p) p++; else p = pkg->file;
652 p = pkg->package = strdup(p);
653 while (*p != 0 && *p != '_' && *p != '.') {
659 snprintf(buf, sizeof(buf), "%s%s", dpkgcidir, pkg->package);
660 DPRINTF("dir = %s\n", buf);
662 if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0) {
663 snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -", pkg->file);
664 if (SYSTEM(buf) == 0) {
665 if ((f = fopen("control", "r")) != NULL) {
666 control_read(f, pkg);
677 static int dpkg_unpack(package_t *pkgs)
681 void *status = status_read();
683 if (SYSTEM(rm_dpkgcidir) != 0 ||
684 mkdir(dpkgcidir, S_IRWXU) != 0) {
688 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
689 dpkg_unpackcontrol(pkg);
690 r = dpkg_dounpack(pkg);
693 status_merge(status, pkgs);
694 SYSTEM(rm_dpkgcidir);
699 static int dpkg_configure(package_t *pkgs)
704 void *status = status_read();
706 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
707 found = tfind(pkg, &status, package_compare);
710 fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
713 /* configure the package listed in the status file;
714 * not pkg, as we have info only for the latter
717 r = dpkg_doconfigure(*(package_t **)found);
720 status_merge(status, 0);
725 static int dpkg_install(package_t *pkgs)
727 package_t *p, *ordered = 0;
728 void *status = status_read();
730 if (SYSTEM(rm_dpkgcidir) != 0 ||
731 mkdir(dpkgcidir, S_IRWXU) != 0) {
736 /* Stage 1: parse all the control information */
737 for (p = pkgs; p != 0; p = p->next)
738 if (dpkg_unpackcontrol(p) != 0) {
740 /* force loop break, and prevents further ops */
744 /* Stage 2: resolve dependencies */
746 ordered = depends_resolve(pkgs, status);
751 /* Stage 3: install */
752 for (p = ordered; p != 0; p = p->next) {
753 p->status &= status_wantmask;
754 p->status |= status_wantinstall;
756 /* for now the flag is always set to ok... this is probably
759 p->status &= status_flagmask;
760 p->status |= status_flagok;
762 if (dpkg_doinstall(p) != 0) {
768 status_merge(status, pkgs);
770 SYSTEM(rm_dpkgcidir);
775 static int dpkg_remove(package_t *pkgs)
778 void *status = status_read();
780 for (p = pkgs; p != 0; p = p->next)
783 status_merge(status, 0);
788 extern int dpkg_main(int argc, char **argv)
792 package_t *p, *packages = NULL;
793 char *cwd = getcwd(0, 0);
797 /* Nasty little hack to "parse" long options. */
804 p = (package_t *)xmalloc(sizeof(package_t));
805 memset(p, 0, sizeof(package_t));
811 p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
812 sprintf(p->file, "%s/%s", cwd, *argv);
814 p->package = strdup(*argv);
824 return dpkg_install(packages);
826 return dpkg_remove(packages);
828 return dpkg_unpack(packages);
830 return dpkg_configure(packages);