Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <search.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | #include <utime.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 11 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
| 13 | |
Eric Andersen | 3c0b425 | 2001-03-14 01:31:11 +0000 | [diff] [blame] | 14 | |
| 15 | /* Stupid libc doesn't have a reliable way for use to know |
| 16 | * that libc5 is being used. Assume this is good enough */ |
Mark Whitley | 3bf60aa | 2001-03-15 17:39:29 +0000 | [diff] [blame] | 17 | #if !defined __GLIBC__ && !defined __UCLIBC__ |
Eric Andersen | 82d8502 | 2001-03-14 17:15:45 +0000 | [diff] [blame] | 18 | #error It looks like you are using libc5, which does not support |
Eric Andersen | 3c0b425 | 2001-03-14 01:31:11 +0000 | [diff] [blame] | 19 | #error tfind(). tfind() is used by busybox dpkg. |
| 20 | #error Please disable BB_DPKG. Sorry. |
| 21 | #endif |
| 22 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 23 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 24 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 25 | /* Should we do full dependency checking? */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 26 | #define DODEPENDS 1 |
| 27 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 28 | /* Should we do debugging? */ |
| 29 | #define DODEBUG 1 |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef DODEBUG |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 32 | #define SYSTEM(x) do_system(x) |
| 33 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) |
| 34 | #else |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 35 | #define SYSTEM(x) system(x) |
| 36 | #define DPRINTF(fmt,args...) /* nothing */ |
| 37 | #endif |
| 38 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 39 | /* from dpkg-deb.c */ |
| 40 | extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename); |
| 41 | static const int dpkg_deb_contents = 1; |
| 42 | static const int dpkg_deb_control = 2; |
| 43 | // const int dpkg_deb_info = 4; |
| 44 | static const int dpkg_deb_extract = 8; |
| 45 | static const int dpkg_deb_verbose_extract = 16; |
| 46 | static const int dpkg_deb_list = 32; |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 48 | static const char statusfile[] = "/var/lib/dpkg/status.udeb"; |
| 49 | static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new"; |
| 50 | static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak"; |
| 51 | |
| 52 | static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/"; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 53 | |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 54 | static const char infodir[] = "/var/lib/dpkg/info/"; |
| 55 | static const char udpkg_quiet[] = "UDPKG_QUIET"; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 56 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 57 | //static const int status_wantstart = 0; |
| 58 | //static const int status_wantunknown = (1 << 0); |
| 59 | static const int status_wantinstall = (1 << 1); |
| 60 | //static const int status_wanthold = (1 << 2); |
| 61 | //static const int status_wantdeinstall = (1 << 3); |
| 62 | //static const int status_wantpurge = (1 << 4); |
| 63 | static const int status_wantmask = 31; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 64 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 65 | //static const int status_flagstart = 5; |
| 66 | static const int status_flagok = (1 << 5); /* 32 */ |
| 67 | //static const int status_flagreinstreq = (1 << 6); |
| 68 | //static const int status_flaghold = (1 << 7); |
| 69 | //static const int status_flagholdreinstreq = (1 << 8); |
| 70 | static const int status_flagmask = 480; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 72 | //static const int status_statusstart = 9; |
| 73 | //static const int status_statusnoninstalled = (1 << 9); /* 512 */ |
| 74 | static const int status_statusunpacked = (1 << 10); |
| 75 | static const int status_statushalfconfigured = (1 << 11); |
| 76 | static const int status_statusinstalled = (1 << 12); |
| 77 | static const int status_statushalfinstalled = (1 << 13); |
| 78 | //static const int status_statusconfigfiles = (1 << 14); |
| 79 | //static const int status_statuspostinstfailed = (1 << 15); |
| 80 | //static const int status_statusremovalfailed = (1 << 16); |
| 81 | static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 83 | static const char *statuswords[][10] = { |
| 84 | { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 }, |
| 85 | { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 }, |
| 86 | { (char *) 9, "not-installed", "unpacked", "half-configured", |
| 87 | "installed", "half-installed", "config-files", |
| 88 | "post-inst-failed", "removal-failed", 0 } |
| 89 | }; |
| 90 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 91 | static const int color_white = 0; |
| 92 | static const int color_grey = 1; |
| 93 | static const int color_black = 2; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 94 | |
| 95 | /* data structures */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 96 | typedef struct package_s { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 97 | char *file; |
| 98 | char *package; |
| 99 | char *version; |
| 100 | char *depends; |
| 101 | char *provides; |
| 102 | char *description; |
| 103 | int installer_menu_item; |
| 104 | unsigned long status; |
| 105 | char color; /* for topo-sort */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 106 | struct package_s *requiredfor[DEPENDSMAX]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 107 | unsigned short requiredcount; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 108 | struct package_s *next; |
| 109 | } package_t; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 110 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 111 | #ifdef DODEBUG |
| 112 | static int do_system(const char *cmd) |
| 113 | { |
| 114 | DPRINTF("cmd is %s\n", cmd); |
| 115 | return system(cmd); |
| 116 | } |
| 117 | #else |
| 118 | #define do_system(cmd) system(cmd) |
| 119 | #endif |
| 120 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 121 | static int package_compare(const void *p1, const void *p2) |
| 122 | { |
| 123 | return strcmp(((package_t *)p1)->package, |
| 124 | ((package_t *)p2)->package); |
| 125 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 126 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 127 | static int remove_dpkgcidir() |
| 128 | { |
| 129 | char *rm_dpkgcidir = NULL; |
| 130 | |
| 131 | rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8); |
| 132 | strcpy(rm_dpkgcidir, "rm -rf "); |
| 133 | strcat(rm_dpkgcidir, dpkgcidir); |
| 134 | |
| 135 | if (SYSTEM(rm_dpkgcidir) != 0) { |
| 136 | perror("mkdir "); |
| 137 | return EXIT_FAILURE; |
| 138 | } |
| 139 | return EXIT_SUCCESS; |
| 140 | } |
| 141 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 142 | #ifdef DODEPENDS |
| 143 | #include <ctype.h> |
| 144 | |
| 145 | static char **depends_split(const char *dependsstr) |
| 146 | { |
| 147 | static char *dependsvec[DEPENDSMAX]; |
| 148 | char *p; |
| 149 | int i = 0; |
| 150 | |
| 151 | dependsvec[0] = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 152 | if (dependsstr == 0) { |
| 153 | goto end; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 155 | |
| 156 | p = strdup(dependsstr); |
| 157 | while (*p != 0 && *p != '\n') { |
| 158 | if (*p != ' ') { |
| 159 | if (*p == ',') { |
| 160 | *p = 0; |
| 161 | dependsvec[++i] = 0; |
| 162 | } else { |
| 163 | if (dependsvec[i] == 0) { |
| 164 | dependsvec[i] = p; |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | *p = 0; /* eat the space... */ |
| 169 | } |
| 170 | p++; |
| 171 | } |
| 172 | *p = 0; |
| 173 | |
| 174 | end: |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 175 | dependsvec[i+1] = 0; |
| 176 | return dependsvec; |
| 177 | } |
| 178 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 179 | /* Topological sort algorithm: |
| 180 | * ordered is the output list, pkgs is the dependency graph, pkg is |
| 181 | * the current node |
| 182 | * |
| 183 | * recursively add all the adjacent nodes to the ordered list, marking |
| 184 | * each one as visited along the way |
| 185 | * |
| 186 | * yes, this algorithm looks a bit odd when all the params have the |
| 187 | * same type :-) |
| 188 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 189 | static void depends_sort_visit(package_t **ordered, package_t *pkgs, |
| 190 | package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 191 | { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 192 | unsigned short i; |
| 193 | |
| 194 | /* mark node as processing */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 195 | pkg->color = color_grey; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 196 | |
| 197 | /* visit each not-yet-visited node */ |
| 198 | for (i = 0; i < pkg->requiredcount; i++) |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 199 | if (pkg->requiredfor[i]->color == color_white) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 200 | depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]); |
| 201 | |
| 202 | #if 0 |
| 203 | /* add it to the list */ |
Eric Andersen | b50d707 | 2001-02-15 19:50:11 +0000 | [diff] [blame] | 204 | newnode = (struct package_t *)xmalloc(sizeof(struct package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 205 | /* make a shallow copy */ |
| 206 | *newnode = *pkg; |
| 207 | newnode->next = *ordered; |
| 208 | *ordered = newnode; |
| 209 | #endif |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 210 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 211 | pkg->next = *ordered; |
| 212 | *ordered = pkg; |
| 213 | |
| 214 | /* mark node as done */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 215 | pkg->color = color_black; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 218 | static package_t *depends_sort(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 219 | { |
| 220 | /* TODO: it needs to break cycles in the to-be-installed package |
| 221 | * graph... */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 222 | package_t *ordered = NULL; |
| 223 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 224 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 225 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 226 | pkg->color = color_white; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 227 | } |
| 228 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
| 229 | if (pkg->color == color_white) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 230 | depends_sort_visit(&ordered, pkgs, pkg); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 233 | |
| 234 | /* Leaks the old list... return the new one... */ |
| 235 | return ordered; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* resolve package dependencies -- |
| 240 | * for each package in the list of packages to be installed, we parse its |
| 241 | * dependency info to determine if the dependent packages are either |
| 242 | * already installed, or are scheduled to be installed. If both tests fail |
| 243 | * than bail. |
| 244 | * |
| 245 | * The algorithm here is O(n^2*m) where n = number of packages to be |
| 246 | * installed and m is the # of dependencies per package. Not a terribly |
| 247 | * efficient algorithm, but given that at any one time you are unlikely |
| 248 | * to install a very large number of packages it doesn't really matter |
| 249 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 250 | static package_t *depends_resolve(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 251 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 252 | package_t *pkg, *chk; |
| 253 | package_t dependpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 254 | char **dependsvec; |
| 255 | int i; |
| 256 | void *found; |
| 257 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 258 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 259 | dependsvec = depends_split(pkg->depends); |
| 260 | i = 0; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 261 | while (dependsvec[i] != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 262 | /* Check for dependencies; first look for installed packages */ |
| 263 | dependpkg.package = dependsvec[i]; |
| 264 | if ((found = tfind(&dependpkg, &status, package_compare)) == 0 || |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 265 | ((chk = *(package_t **)found) && |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 266 | (chk->status & (status_flagok | status_statusinstalled)) != |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 267 | (status_flagok | status_statusinstalled))) { |
| 268 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 269 | /* if it fails, we look through the list of packages we are going to |
| 270 | * install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 271 | for (chk = pkgs; chk != 0; chk = chk->next) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 272 | if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 273 | strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { |
| 274 | if (chk->requiredcount >= DEPENDSMAX) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 275 | fprintf(stderr, "Too many dependencies for %s\n", chk->package); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 278 | if (chk != pkg) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 279 | chk->requiredfor[chk->requiredcount++] = pkg; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 280 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 284 | if (chk == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 285 | fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]); |
| 286 | return 0; |
| 287 | } |
| 288 | } |
| 289 | i++; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return depends_sort(pkgs); |
| 294 | } |
| 295 | #endif |
| 296 | |
| 297 | /* Status file handling routines |
| 298 | * |
| 299 | * This is a fairly minimalistic implementation. there are two main functions |
| 300 | * that are supported: |
| 301 | * |
| 302 | * 1) reading the entire status file: |
| 303 | * the status file is read into memory as a binary-tree, with just the |
| 304 | * package and status info preserved |
| 305 | * |
| 306 | * 2) merging the status file |
| 307 | * control info from (new) packages is merged into the status file, |
| 308 | * replacing any pre-existing entries. when a merge happens, status info |
| 309 | * read using the status_read function is written back to the status file |
| 310 | */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 311 | static unsigned long status_parse(const char *line) |
| 312 | { |
| 313 | char *p; |
| 314 | int i, j; |
| 315 | unsigned long l = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 316 | |
| 317 | for (i = 0; i < 3; i++) { |
| 318 | if ((p = strchr(line, ' ')) != NULL) { |
| 319 | *p = 0; |
| 320 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 321 | j = 1; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 322 | while (statuswords[i][j] != 0) { |
| 323 | if (strcmp(line, statuswords[i][j]) == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 324 | l |= (1 << ((int)statuswords[i][0] + j - 1)); |
| 325 | break; |
| 326 | } |
| 327 | j++; |
| 328 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 329 | /* parse error */ |
| 330 | if (statuswords[i][j] == 0) { |
| 331 | return 0; |
| 332 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 333 | line = p+1; |
| 334 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 335 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 336 | return l; |
| 337 | } |
| 338 | |
| 339 | static const char *status_print(unsigned long flags) |
| 340 | { |
| 341 | /* this function returns a static buffer... */ |
| 342 | static char buf[256]; |
| 343 | int i, j; |
| 344 | |
| 345 | buf[0] = 0; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 346 | for (i = 0; i < 3; i++) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 347 | j = 1; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 348 | while (statuswords[i][j] != 0) { |
| 349 | if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 350 | strcat(buf, statuswords[i][j]); |
| 351 | if (i < 2) strcat(buf, " "); |
| 352 | break; |
| 353 | } |
| 354 | j++; |
| 355 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 356 | if (statuswords[i][j] == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 357 | fprintf(stderr, "corrupted status flag!!\n"); |
| 358 | return NULL; |
| 359 | } |
| 360 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 361 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 362 | return buf; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Read a control file (or a stanza of a status file) and parse it, |
| 367 | * filling parsed fields into the package structure |
| 368 | */ |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 369 | static int control_read(FILE *file, package_t *p) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 370 | { |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 371 | char *line; |
| 372 | |
| 373 | while ((line = get_line_from_file(file)) != NULL) { |
| 374 | line[strlen(line)] = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 375 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 376 | if (strlen(line) == 0) { |
| 377 | break; |
| 378 | } else |
| 379 | if (strstr(line, "Package: ") == line) { |
| 380 | p->package = strdup(line + 9); |
| 381 | } else |
| 382 | if (strstr(line, "Status: ") == line) { |
| 383 | p->status = status_parse(line + 8); |
| 384 | } else |
| 385 | if (strstr(line, "Depends: ") == line) { |
| 386 | p->depends = strdup(line + 9); |
| 387 | } else |
| 388 | if (strstr(line, "Provides: ") == line) { |
| 389 | p->provides = strdup(line + 10); |
| 390 | } else |
| 391 | if (strstr(line, "Description: ") == line) { |
| 392 | p->description = strdup(line + 13); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 393 | /* This is specific to the Debian Installer. Ifdef? */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 394 | } else |
| 395 | if (strstr(line, "installer-menu-item: ") == line) { |
| 396 | p->installer_menu_item = atoi(line + 21); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 397 | } |
| 398 | /* TODO: localized descriptions */ |
| 399 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 400 | free(line); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 401 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 404 | static void *status_read(void) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 405 | { |
| 406 | FILE *f; |
| 407 | void *status = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 408 | package_t *m = 0, *p = 0, *t = 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 409 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 410 | if ((f = fopen(statusfile, "r")) == NULL) { |
| 411 | perror(statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 414 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 415 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 416 | printf("(Reading database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 417 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 418 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 419 | while (!feof(f)) { |
| 420 | m = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 421 | memset(m, 0, sizeof(package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 422 | control_read(f, m); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 423 | if (m->package) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 424 | /* |
| 425 | * If there is an item in the tree by this name, |
| 426 | * it must be a virtual package; insert real |
| 427 | * package in preference. |
| 428 | */ |
| 429 | tdelete(m, &status, package_compare); |
| 430 | tsearch(m, &status, package_compare); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 431 | if (m->provides) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 432 | /* |
| 433 | * A "Provides" triggers the insertion |
| 434 | * of a pseudo package into the status |
| 435 | * binary-tree. |
| 436 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 437 | p = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 438 | memset(p, 0, sizeof(package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 439 | p->package = strdup(m->provides); |
| 440 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 441 | t = *(package_t **)tsearch(p, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 442 | if (t != p) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 443 | free(p->package); |
| 444 | free(p); |
| 445 | } |
| 446 | else { |
| 447 | /* |
| 448 | * Pseudo package status is the |
| 449 | * same as the status of the |
| 450 | * package providing it |
| 451 | * FIXME: (not quite right, if 2 |
| 452 | * packages of different statuses |
| 453 | * provide it). |
| 454 | */ |
| 455 | t->status = m->status; |
| 456 | } |
| 457 | } |
| 458 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 459 | else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 460 | free(m); |
| 461 | } |
| 462 | } |
| 463 | fclose(f); |
| 464 | return status; |
| 465 | } |
| 466 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 467 | static int status_merge(void *status, package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 468 | { |
| 469 | FILE *fin, *fout; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 470 | char *line; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 471 | package_t *pkg = 0, *statpkg = 0; |
| 472 | package_t locpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 473 | int r = 0; |
| 474 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 475 | if ((fin = fopen(statusfile, "r")) == NULL) { |
| 476 | perror(statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 477 | return 0; |
| 478 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 479 | if ((fout = fopen(new_statusfile, "w")) == NULL) { |
| 480 | perror(new_statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 483 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 484 | printf("(Updating database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 485 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 486 | |
| 487 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 488 | line[strlen(line)] = 0; /* trim newline */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 489 | /* If we see a package header, find out if it's a package |
| 490 | * that we have processed. if so, we skip that block for |
| 491 | * now (write it at the end). |
| 492 | * |
| 493 | * we also look at packages in the status cache and update |
| 494 | * their status fields |
| 495 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 496 | if (strstr(line, "Package: ") == line) { |
| 497 | for (pkg = pkgs; pkg != 0 && strncmp(line + 9, |
| 498 | pkg->package, strlen(line) - 9) != 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 499 | pkg = pkg->next) ; |
| 500 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 501 | locpkg.package = line + 9; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 502 | statpkg = tfind(&locpkg, &status, package_compare); |
| 503 | |
| 504 | /* note: statpkg should be non-zero, unless the status |
| 505 | * file was changed while we are processing (no locking |
| 506 | * is currently done... |
| 507 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 508 | if (statpkg != 0) { |
| 509 | statpkg = *(package_t **)statpkg; |
| 510 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 511 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 512 | if (pkg != 0) { |
| 513 | continue; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 514 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 515 | if (strstr(line, "Status: ") == line && statpkg != 0) { |
| 516 | snprintf(line, sizeof(line), "Status: %s", |
| 517 | status_print(statpkg->status)); |
| 518 | } |
| 519 | fputs(line, fout); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 520 | fputc('\n', fout); |
| 521 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 522 | free(line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 523 | |
| 524 | // Print out packages we processed. |
| 525 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
| 526 | fprintf(fout, "Package: %s\nStatus: %s\n", |
| 527 | pkg->package, status_print(pkg->status)); |
| 528 | if (pkg->depends) |
| 529 | fprintf(fout, "Depends: %s\n", pkg->depends); |
| 530 | if (pkg->provides) |
| 531 | fprintf(fout, "Provides: %s\n", pkg->provides); |
| 532 | if (pkg->installer_menu_item) |
| 533 | fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item); |
| 534 | if (pkg->description) |
| 535 | fprintf(fout, "Description: %s\n", pkg->description); |
| 536 | fputc('\n', fout); |
| 537 | } |
| 538 | |
| 539 | fclose(fin); |
| 540 | fclose(fout); |
| 541 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 542 | r = rename(statusfile, bak_statusfile); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 543 | if (r == 0) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 544 | r = rename(new_statusfile, statusfile); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 545 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 546 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 550 | static int is_file(const char *fn) |
| 551 | { |
| 552 | struct stat statbuf; |
| 553 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 554 | if (stat(fn, &statbuf) < 0) { |
| 555 | return 0; |
| 556 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 557 | return S_ISREG(statbuf.st_mode); |
| 558 | } |
| 559 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 560 | static int dpkg_doconfigure(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 561 | { |
| 562 | int r; |
| 563 | char postinst[1024]; |
| 564 | char buf[1024]; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 565 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 566 | DPRINTF("Configuring %s\n", pkg->package); |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 567 | pkg->status &= status_statusmask; |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 568 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 569 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 570 | if (is_file(postinst)) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 571 | snprintf(buf, sizeof(buf), "%s configure", postinst); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 572 | if ((r = do_system(buf)) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 573 | fprintf(stderr, "postinst exited with status %d\n", r); |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 574 | pkg->status |= status_statushalfconfigured; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 575 | return 1; |
| 576 | } |
| 577 | } |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 578 | pkg->status |= status_statusinstalled; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 583 | static int dpkg_dounpack(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 584 | { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 585 | int r = 0, i; |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 586 | char *cwd; |
| 587 | FILE *outfp; |
| 588 | char *src_file = NULL; |
| 589 | char *dst_file = NULL; |
| 590 | char *lst_file = NULL; |
| 591 | char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst", |
| 592 | "/conffiles", "/md5sums", "/shlibs", "/templates" }; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 593 | char buf[1024], buf2[1024]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 594 | |
| 595 | DPRINTF("Unpacking %s\n", pkg->package); |
| 596 | |
| 597 | cwd = getcwd(0, 0); |
| 598 | chdir("/"); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 599 | deb_extract(dpkg_deb_extract, "/", pkg->file); |
| 600 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 601 | /* Installs the package scripts into the info directory */ |
| 602 | for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) { |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 603 | /* The full path of the current location of the admin file */ |
| 604 | src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); |
| 605 | strcpy(src_file, dpkgcidir); |
| 606 | strcat(src_file, pkg->package); |
| 607 | strcat(src_file, adminscripts[i]); |
| 608 | |
| 609 | /* the full path of where we want the file to be copied to */ |
| 610 | dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); |
| 611 | strcpy(dst_file, infodir); |
| 612 | strcat(dst_file, pkg->package); |
| 613 | strcat(dst_file, adminscripts[i]); |
| 614 | |
| 615 | /* copy admin file to permanent home */ |
| 616 | if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) { |
| 617 | error_msg_and_die("Cannot copy %s to %s ", buf, buf2); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 618 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 619 | |
| 620 | /* create the list file */ |
Eric Andersen | b50d707 | 2001-02-15 19:50:11 +0000 | [diff] [blame] | 621 | lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 622 | strcpy(lst_file, infodir); |
| 623 | strcat(lst_file, pkg->package); |
| 624 | strcat(lst_file, ".list"); |
| 625 | outfp = freopen(lst_file, "w", stdout); |
| 626 | deb_extract(dpkg_deb_list, NULL, pkg->file); |
| 627 | stdout = freopen(NULL, "w", outfp); |
| 628 | |
| 629 | printf("done\n"); |
| 630 | getchar(); |
| 631 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 632 | fclose(outfp); |
| 633 | } |
| 634 | |
| 635 | pkg->status &= status_wantmask; |
| 636 | pkg->status |= status_wantinstall; |
| 637 | pkg->status &= status_flagmask; |
| 638 | pkg->status |= status_flagok; |
| 639 | pkg->status &= status_statusmask; |
| 640 | |
| 641 | if (r == 0) { |
| 642 | pkg->status |= status_statusunpacked; |
| 643 | } else { |
| 644 | pkg->status |= status_statushalfinstalled; |
| 645 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 646 | chdir(cwd); |
| 647 | return r; |
| 648 | } |
| 649 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 650 | /* |
| 651 | * Extract and parse the control.tar.gz from the specified package |
| 652 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 653 | static int dpkg_unpackcontrol(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 654 | { |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 655 | char *tmp_name; |
| 656 | FILE *file; |
| 657 | int length; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 658 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 659 | /* clean the temp directory (dpkgcidir) be recreating it */ |
| 660 | remove_dpkgcidir(); |
| 661 | if (mkdir(dpkgcidir, S_IRWXU) != 0) { |
| 662 | perror("mkdir"); |
| 663 | return EXIT_FAILURE; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 664 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 665 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 666 | /* |
| 667 | * Get the package name from the file name, |
| 668 | * first remove the directories |
| 669 | */ |
| 670 | if ((tmp_name = strrchr(pkg->file, '/')) == NULL) { |
| 671 | tmp_name = pkg->file; |
| 672 | } else { |
| 673 | tmp_name++; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 674 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 675 | /* now remove trailing version numbers etc */ |
| 676 | length = strcspn(tmp_name, "_."); |
| 677 | pkg->package = (char *) xmalloc(length + 1); |
| 678 | /* store the package name */ |
| 679 | strncpy(pkg->package, tmp_name, length); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 681 | /* work out the full extraction path */ |
| 682 | tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9); |
| 683 | memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9); |
| 684 | strcpy(tmp_name, dpkgcidir); |
| 685 | strcat(tmp_name, pkg->package); |
| 686 | |
| 687 | /* extract control.tar.gz to the full extraction path */ |
| 688 | deb_extract(dpkg_deb_control, tmp_name, pkg->file); |
| 689 | |
| 690 | /* parse the extracted control file */ |
| 691 | strcat(tmp_name, "/control"); |
| 692 | if ((file = fopen(tmp_name, "r")) == NULL) { |
| 693 | return EXIT_FAILURE; |
| 694 | } |
| 695 | if (control_read(file, pkg) == EXIT_FAILURE) { |
| 696 | return EXIT_FAILURE; |
| 697 | } |
| 698 | |
| 699 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 702 | static int dpkg_unpack(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 703 | { |
| 704 | int r = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 705 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 706 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 707 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 708 | if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) { |
| 709 | return EXIT_FAILURE; |
| 710 | } |
| 711 | if ((r = dpkg_dounpack(pkg)) != 0 ) { |
| 712 | break; |
| 713 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 714 | } |
| 715 | status_merge(status, pkgs); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 716 | remove_dpkgcidir(); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 717 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 718 | return r; |
| 719 | } |
| 720 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 721 | static int dpkg_configure(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 722 | { |
| 723 | int r = 0; |
| 724 | void *found; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 725 | package_t *pkg; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 726 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 727 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 728 | found = tfind(pkg, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 729 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 730 | if (found == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 731 | fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package); |
| 732 | r = 1; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 733 | } |
| 734 | /* configure the package listed in the status file; |
| 735 | * not pkg, as we have info only for the latter |
| 736 | */ |
| 737 | else { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 738 | r = dpkg_doconfigure(*(package_t **)found); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 742 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 743 | return r; |
| 744 | } |
| 745 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 746 | static int dpkg_install(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 747 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 748 | package_t *p, *ordered = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 749 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 750 | /* Stage 1: parse all the control information */ |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 751 | for (p = pkgs; p != 0; p = p->next) { |
| 752 | if (dpkg_unpackcontrol(p) == EXIT_FAILURE) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 753 | perror(p->file); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 754 | return EXIT_FAILURE; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 755 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 758 | /* Stage 2: resolve dependencies */ |
| 759 | #ifdef DODEPENDS |
| 760 | ordered = depends_resolve(pkgs, status); |
| 761 | #else |
| 762 | ordered = pkgs; |
| 763 | #endif |
| 764 | |
| 765 | /* Stage 3: install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 766 | for (p = ordered; p != 0; p = p->next) { |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 767 | p->status &= status_wantmask; |
| 768 | p->status |= status_wantinstall; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 769 | |
| 770 | /* for now the flag is always set to ok... this is probably |
| 771 | * not what we want |
| 772 | */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 773 | p->status &= status_flagmask; |
| 774 | p->status |= status_flagok; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 775 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 776 | DPRINTF("Installing %s\n", p->package); |
| 777 | if (dpkg_dounpack(p) != 0) { |
| 778 | perror(p->file); |
| 779 | } |
| 780 | if (dpkg_doconfigure(p) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 781 | perror(p->file); |
| 782 | } |
| 783 | } |
| 784 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 785 | if (ordered != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 786 | status_merge(status, pkgs); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 787 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 788 | remove_dpkgcidir(); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 789 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 793 | static int dpkg_remove(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 794 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 795 | package_t *p; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 796 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 797 | for (p = pkgs; p != 0; p = p->next) |
| 798 | { |
| 799 | } |
| 800 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 801 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 805 | extern int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 806 | { |
| 807 | char opt = 0; |
| 808 | char *s; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 809 | package_t *p, *packages = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 810 | char *cwd = getcwd(0, 0); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 811 | void *status = NULL; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 812 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 813 | while (*++argv) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 814 | if (**argv == '-') { |
| 815 | /* Nasty little hack to "parse" long options. */ |
| 816 | s = *argv; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 817 | while (*s == '-') { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 818 | s++; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 819 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 820 | opt=s[0]; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 821 | } else { |
| 822 | p = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 823 | memset(p, 0, sizeof(package_t)); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 824 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 825 | if (**argv == '/') { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 826 | p->file = *argv; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 827 | } else |
| 828 | if (opt != 'c') { |
| 829 | p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2); |
| 830 | sprintf(p->file, "%s/%s", cwd, *argv); |
| 831 | } else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 832 | p->package = strdup(*argv); |
| 833 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 834 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 835 | p->next = packages; |
| 836 | packages = p; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 837 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 838 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 839 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 840 | status = status_read(); |
| 841 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 842 | switch (opt) { |
| 843 | case 'i': |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 844 | return dpkg_install(packages, status); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 845 | case 'r': |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 846 | return dpkg_remove(packages, status); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 847 | case 'u': |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 848 | return dpkg_unpack(packages, status); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 849 | case 'c': |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 850 | return dpkg_configure(packages, status); |
| 851 | default : |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 852 | show_usage(); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 853 | return EXIT_FAILURE; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 854 | } |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 855 | } |