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 | |
| 14 | //#define PACKAGE "udpkg" |
| 15 | //#define VERSION "0.1" |
| 16 | |
| 17 | /* |
| 18 | * Should we do full dependency checking? |
| 19 | */ |
| 20 | #define DODEPENDS 1 |
| 21 | |
| 22 | /* |
| 23 | * Should we do debugging? |
| 24 | */ |
| 25 | #define DODEBUG 0 |
| 26 | |
| 27 | #ifdef DODEBUG |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 28 | #define SYSTEM(x) do_system(x) |
| 29 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) |
| 30 | #else |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 31 | #define SYSTEM(x) system(x) |
| 32 | #define DPRINTF(fmt,args...) /* nothing */ |
| 33 | #endif |
| 34 | |
| 35 | #define BUFSIZE 4096 |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 36 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ |
| 37 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 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"; |
| 41 | |
| 42 | static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/"; |
| 43 | static const char rm_dpkgcidir[] = "rm -rf /var/lib/dpkg/tmp.ci/"; |
| 44 | |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 45 | static const char infodir[] = "/var/lib/dpkg/info/"; |
| 46 | static const char udpkg_quiet[] = "UDPKG_QUIET"; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 55 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 62 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 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 */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 73 | |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 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 } |
| 80 | }; |
| 81 | |
| 82 | const int color_white = 0; |
| 83 | const int color_grey = 1; |
| 84 | const int color_black = 2; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 85 | |
| 86 | /* data structures */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 87 | typedef struct package_s { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 88 | char *file; |
| 89 | char *package; |
| 90 | char *version; |
| 91 | char *depends; |
| 92 | char *provides; |
| 93 | char *description; |
| 94 | int installer_menu_item; |
| 95 | unsigned long status; |
| 96 | char color; /* for topo-sort */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 97 | struct package_s *requiredfor[DEPENDSMAX]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 98 | unsigned short requiredcount; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 99 | struct package_s *next; |
| 100 | } package_t; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 101 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 102 | #ifdef DODEBUG |
| 103 | static int do_system(const char *cmd) |
| 104 | { |
| 105 | DPRINTF("cmd is %s\n", cmd); |
| 106 | return system(cmd); |
| 107 | } |
| 108 | #else |
| 109 | #define do_system(cmd) system(cmd) |
| 110 | #endif |
| 111 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 112 | static int package_compare(const void *p1, const void *p2) |
| 113 | { |
| 114 | return strcmp(((package_t *)p1)->package, |
| 115 | ((package_t *)p2)->package); |
| 116 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 117 | |
| 118 | #ifdef DODEPENDS |
| 119 | #include <ctype.h> |
| 120 | |
| 121 | static char **depends_split(const char *dependsstr) |
| 122 | { |
| 123 | static char *dependsvec[DEPENDSMAX]; |
| 124 | char *p; |
| 125 | int i = 0; |
| 126 | |
| 127 | dependsvec[0] = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 128 | if (dependsstr == 0) { |
| 129 | goto end; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 130 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 131 | |
| 132 | p = strdup(dependsstr); |
| 133 | while (*p != 0 && *p != '\n') { |
| 134 | if (*p != ' ') { |
| 135 | if (*p == ',') { |
| 136 | *p = 0; |
| 137 | dependsvec[++i] = 0; |
| 138 | } else { |
| 139 | if (dependsvec[i] == 0) { |
| 140 | dependsvec[i] = p; |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | *p = 0; /* eat the space... */ |
| 145 | } |
| 146 | p++; |
| 147 | } |
| 148 | *p = 0; |
| 149 | |
| 150 | end: |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 151 | dependsvec[i+1] = 0; |
| 152 | return dependsvec; |
| 153 | } |
| 154 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 155 | /* Topological sort algorithm: |
| 156 | * ordered is the output list, pkgs is the dependency graph, pkg is |
| 157 | * the current node |
| 158 | * |
| 159 | * recursively add all the adjacent nodes to the ordered list, marking |
| 160 | * each one as visited along the way |
| 161 | * |
| 162 | * yes, this algorithm looks a bit odd when all the params have the |
| 163 | * same type :-) |
| 164 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 165 | static void depends_sort_visit(package_t **ordered, package_t *pkgs, |
| 166 | package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 167 | { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 168 | unsigned short i; |
| 169 | |
| 170 | /* mark node as processing */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 171 | pkg->color = color_grey; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 172 | |
| 173 | /* visit each not-yet-visited node */ |
| 174 | for (i = 0; i < pkg->requiredcount; i++) |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 175 | if (pkg->requiredfor[i]->color == color_white) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 176 | depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]); |
| 177 | |
| 178 | #if 0 |
| 179 | /* add it to the list */ |
| 180 | newnode = (struct package_t *)malloc(sizeof(struct package_t)); |
| 181 | /* make a shallow copy */ |
| 182 | *newnode = *pkg; |
| 183 | newnode->next = *ordered; |
| 184 | *ordered = newnode; |
| 185 | #endif |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 186 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 187 | pkg->next = *ordered; |
| 188 | *ordered = pkg; |
| 189 | |
| 190 | /* mark node as done */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 191 | pkg->color = color_black; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 194 | static package_t *depends_sort(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 195 | { |
| 196 | /* TODO: it needs to break cycles in the to-be-installed package |
| 197 | * graph... */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 198 | package_t *ordered = NULL; |
| 199 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 200 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 201 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 202 | pkg->color = color_white; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 203 | } |
| 204 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
| 205 | if (pkg->color == color_white) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 206 | depends_sort_visit(&ordered, pkgs, pkg); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 209 | |
| 210 | /* Leaks the old list... return the new one... */ |
| 211 | return ordered; |
| 212 | } |
| 213 | |
| 214 | |
| 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 |
| 219 | * than bail. |
| 220 | * |
| 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 |
| 225 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 226 | static package_t *depends_resolve(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 227 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 228 | package_t *pkg, *chk; |
| 229 | package_t dependpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 230 | char **dependsvec; |
| 231 | int i; |
| 232 | void *found; |
| 233 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 234 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 235 | dependsvec = depends_split(pkg->depends); |
| 236 | i = 0; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 237 | while (dependsvec[i] != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 238 | /* Check for dependencies; first look for installed packages */ |
| 239 | dependpkg.package = dependsvec[i]; |
| 240 | if ((found = tfind(&dependpkg, &status, package_compare)) == 0 || |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 241 | ((chk = *(package_t **)found) && |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 242 | (chk->status & (status_flagok | status_statusinstalled)) != |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 243 | (status_flagok | status_statusinstalled))) { |
| 244 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 245 | /* if it fails, we look through the list of packages we are going to |
| 246 | * install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 247 | for (chk = pkgs; chk != 0; chk = chk->next) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 248 | if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 249 | strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { |
| 250 | if (chk->requiredcount >= DEPENDSMAX) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 251 | fprintf(stderr, "Too many dependencies for %s\n", chk->package); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 254 | if (chk != pkg) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 255 | chk->requiredfor[chk->requiredcount++] = pkg; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 256 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 260 | if (chk == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 261 | fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]); |
| 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | i++; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return depends_sort(pkgs); |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | /* Status file handling routines |
| 274 | * |
| 275 | * This is a fairly minimalistic implementation. there are two main functions |
| 276 | * that are supported: |
| 277 | * |
| 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 |
| 281 | * |
| 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 |
| 286 | */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 287 | static unsigned long status_parse(const char *line) |
| 288 | { |
| 289 | char *p; |
| 290 | int i, j; |
| 291 | unsigned long l = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 292 | |
| 293 | for (i = 0; i < 3; i++) { |
| 294 | if ((p = strchr(line, ' ')) != NULL) { |
| 295 | *p = 0; |
| 296 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 297 | j = 1; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 298 | while (statuswords[i][j] != 0) { |
| 299 | if (strcmp(line, statuswords[i][j]) == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 300 | l |= (1 << ((int)statuswords[i][0] + j - 1)); |
| 301 | break; |
| 302 | } |
| 303 | j++; |
| 304 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 305 | /* parse error */ |
| 306 | if (statuswords[i][j] == 0) { |
| 307 | return 0; |
| 308 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 309 | line = p+1; |
| 310 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 311 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 312 | return l; |
| 313 | } |
| 314 | |
| 315 | static const char *status_print(unsigned long flags) |
| 316 | { |
| 317 | /* this function returns a static buffer... */ |
| 318 | static char buf[256]; |
| 319 | int i, j; |
| 320 | |
| 321 | buf[0] = 0; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 322 | for (i = 0; i < 3; i++) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 323 | j = 1; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 324 | while (statuswords[i][j] != 0) { |
| 325 | if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 326 | strcat(buf, statuswords[i][j]); |
| 327 | if (i < 2) strcat(buf, " "); |
| 328 | break; |
| 329 | } |
| 330 | j++; |
| 331 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 332 | if (statuswords[i][j] == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 333 | fprintf(stderr, "corrupted status flag!!\n"); |
| 334 | return NULL; |
| 335 | } |
| 336 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 337 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 338 | return buf; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Read a control file (or a stanza of a status file) and parse it, |
| 343 | * filling parsed fields into the package structure |
| 344 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 345 | static void control_read(FILE *file, package_t *p) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 346 | { |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 347 | char *line; |
| 348 | |
| 349 | while ((line = get_line_from_file(file)) != NULL) { |
| 350 | line[strlen(line)] = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 351 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 352 | if (strlen(line) == 0) { |
| 353 | break; |
| 354 | } else |
| 355 | if (strstr(line, "Package: ") == line) { |
| 356 | p->package = strdup(line + 9); |
| 357 | } else |
| 358 | if (strstr(line, "Status: ") == line) { |
| 359 | p->status = status_parse(line + 8); |
| 360 | } else |
| 361 | if (strstr(line, "Depends: ") == line) { |
| 362 | p->depends = strdup(line + 9); |
| 363 | } else |
| 364 | if (strstr(line, "Provides: ") == line) { |
| 365 | p->provides = strdup(line + 10); |
| 366 | } else |
| 367 | if (strstr(line, "Description: ") == line) { |
| 368 | p->description = strdup(line + 13); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 369 | /* This is specific to the Debian Installer. Ifdef? */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 370 | } else |
| 371 | if (strstr(line, "installer-menu-item: ") == line) { |
| 372 | p->installer_menu_item = atoi(line + 21); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 373 | } |
| 374 | /* TODO: localized descriptions */ |
| 375 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 376 | free(line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 379 | static void *status_read(void) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 380 | { |
| 381 | FILE *f; |
| 382 | void *status = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 383 | package_t *m = 0, *p = 0, *t = 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 384 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 385 | if ((f = fopen(statusfile, "r")) == NULL) { |
| 386 | perror(statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 387 | return 0; |
| 388 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 389 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 390 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 391 | printf("(Reading database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 392 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 393 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 394 | while (!feof(f)) { |
| 395 | m = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 396 | memset(m, 0, sizeof(package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 397 | control_read(f, m); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 398 | if (m->package) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 399 | /* |
| 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. |
| 403 | */ |
| 404 | tdelete(m, &status, package_compare); |
| 405 | tsearch(m, &status, package_compare); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 406 | if (m->provides) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 407 | /* |
| 408 | * A "Provides" triggers the insertion |
| 409 | * of a pseudo package into the status |
| 410 | * binary-tree. |
| 411 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 412 | p = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 413 | memset(p, 0, sizeof(package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 414 | p->package = strdup(m->provides); |
| 415 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 416 | t = *(package_t **)tsearch(p, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 417 | if (t != p) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 418 | free(p->package); |
| 419 | free(p); |
| 420 | } |
| 421 | else { |
| 422 | /* |
| 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 |
| 428 | * provide it). |
| 429 | */ |
| 430 | t->status = m->status; |
| 431 | } |
| 432 | } |
| 433 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 434 | else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 435 | free(m); |
| 436 | } |
| 437 | } |
| 438 | fclose(f); |
| 439 | return status; |
| 440 | } |
| 441 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 442 | static int status_merge(void *status, package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 443 | { |
| 444 | FILE *fin, *fout; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 445 | char *line; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 446 | package_t *pkg = 0, *statpkg = 0; |
| 447 | package_t locpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 448 | int r = 0; |
| 449 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 450 | if ((fin = fopen(statusfile, "r")) == NULL) { |
| 451 | perror(statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 452 | return 0; |
| 453 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 454 | if ((fout = fopen(new_statusfile, "w")) == NULL) { |
| 455 | perror(new_statusfile); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 456 | return 0; |
| 457 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 458 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 459 | printf("(Updating database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 460 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 461 | |
| 462 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 463 | line[strlen(line)] = 0; /* trim newline */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 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). |
| 467 | * |
| 468 | * we also look at packages in the status cache and update |
| 469 | * their status fields |
| 470 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 471 | if (strstr(line, "Package: ") == line) { |
| 472 | for (pkg = pkgs; pkg != 0 && strncmp(line + 9, |
| 473 | pkg->package, strlen(line) - 9) != 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 474 | pkg = pkg->next) ; |
| 475 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 476 | locpkg.package = line + 9; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 477 | statpkg = tfind(&locpkg, &status, package_compare); |
| 478 | |
| 479 | /* note: statpkg should be non-zero, unless the status |
| 480 | * file was changed while we are processing (no locking |
| 481 | * is currently done... |
| 482 | */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 483 | if (statpkg != 0) { |
| 484 | statpkg = *(package_t **)statpkg; |
| 485 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 486 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 487 | if (pkg != 0) { |
| 488 | continue; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 489 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 490 | if (strstr(line, "Status: ") == line && statpkg != 0) { |
| 491 | snprintf(line, sizeof(line), "Status: %s", |
| 492 | status_print(statpkg->status)); |
| 493 | } |
| 494 | fputs(line, fout); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 495 | fputc('\n', fout); |
| 496 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 497 | free(line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 498 | |
| 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)); |
| 503 | if (pkg->depends) |
| 504 | fprintf(fout, "Depends: %s\n", pkg->depends); |
| 505 | if (pkg->provides) |
| 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); |
| 511 | fputc('\n', fout); |
| 512 | } |
| 513 | |
| 514 | fclose(fin); |
| 515 | fclose(fout); |
| 516 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 517 | r = rename(statusfile, bak_statusfile); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 518 | if (r == 0) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 519 | r = rename(new_statusfile, statusfile); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 520 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 521 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 525 | static int is_file(const char *fn) |
| 526 | { |
| 527 | struct stat statbuf; |
| 528 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 529 | if (stat(fn, &statbuf) < 0) { |
| 530 | return 0; |
| 531 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 532 | return S_ISREG(statbuf.st_mode); |
| 533 | } |
| 534 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 535 | static int dpkg_doconfigure(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 536 | { |
| 537 | int r; |
| 538 | char postinst[1024]; |
| 539 | char buf[1024]; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 540 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 541 | DPRINTF("Configuring %s\n", pkg->package); |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 542 | pkg->status &= status_statusmask; |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 543 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 544 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 545 | if (is_file(postinst)) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 546 | snprintf(buf, sizeof(buf), "%s configure", postinst); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 547 | if ((r = do_system(buf)) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 548 | fprintf(stderr, "postinst exited with status %d\n", r); |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 549 | pkg->status |= status_statushalfconfigured; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 550 | return 1; |
| 551 | } |
| 552 | } |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 553 | pkg->status |= status_statusinstalled; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 558 | static int dpkg_dounpack(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 559 | { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 560 | int r = 0, i; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 561 | char *cwd, *p; |
| 562 | FILE *infp, *outfp; |
| 563 | char buf[1024], buf2[1024]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 564 | char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst", |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 565 | "conffiles", "md5sums", "shlibs", "templates" }; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 566 | |
| 567 | DPRINTF("Unpacking %s\n", pkg->package); |
| 568 | |
| 569 | cwd = getcwd(0, 0); |
| 570 | chdir("/"); |
| 571 | snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 572 | if (SYSTEM(buf) != 0) { |
| 573 | goto end; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 574 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 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)); |
| 584 | r = 1; |
| 585 | break; |
| 586 | } |
| 587 | /* ugly hack to create the list file; should |
| 588 | * probably do something more elegant |
| 589 | * |
| 590 | * why oh why does dpkg create the list file |
| 591 | * so oddly... |
| 592 | */ |
| 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); |
| 597 | r = 1; |
| 598 | break; |
| 599 | } |
| 600 | while (fgets(buf, sizeof(buf), infp) && !feof(infp)) { |
| 601 | p = buf; |
| 602 | if (*p == '.') { |
| 603 | p++; |
| 604 | } |
| 605 | if ((*p == '/') && (*(p + 1) == '\n')) { |
| 606 | *(p + 1) = '.'; |
| 607 | *(p + 2) = '\n'; |
| 608 | *(p + 3) = 0; |
| 609 | } |
| 610 | if (p[strlen(p) - 2] == '/') { |
| 611 | p[strlen(p) - 2] = '\n'; |
| 612 | p[strlen(p) - 1] = 0; |
| 613 | } |
| 614 | fputs(p, outfp); |
| 615 | } |
| 616 | fclose(infp); |
| 617 | fclose(outfp); |
| 618 | } |
| 619 | |
| 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; |
| 625 | |
| 626 | if (r == 0) { |
| 627 | pkg->status |= status_statusunpacked; |
| 628 | } else { |
| 629 | pkg->status |= status_statushalfinstalled; |
| 630 | } |
| 631 | end: |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 632 | chdir(cwd); |
| 633 | return r; |
| 634 | } |
| 635 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 636 | static int dpkg_doinstall(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 637 | { |
| 638 | DPRINTF("Installing %s\n", pkg->package); |
| 639 | return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg)); |
| 640 | } |
| 641 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 642 | static int dpkg_unpackcontrol(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 643 | { |
| 644 | int r = 1; |
| 645 | char *cwd = 0; |
| 646 | char *p; |
| 647 | char buf[1024]; |
| 648 | FILE *f; |
| 649 | |
| 650 | p = strrchr(pkg->file, '/'); |
| 651 | if (p) p++; else p = pkg->file; |
| 652 | p = pkg->package = strdup(p); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 653 | while (*p != 0 && *p != '_' && *p != '.') { |
| 654 | p++; |
| 655 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 656 | *p = 0; |
| 657 | |
| 658 | cwd = getcwd(0, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 659 | snprintf(buf, sizeof(buf), "%s%s", dpkgcidir, pkg->package); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 660 | DPRINTF("dir = %s\n", buf); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 661 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 662 | if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 663 | snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -", pkg->file); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 664 | if (SYSTEM(buf) == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 665 | if ((f = fopen("control", "r")) != NULL) { |
| 666 | control_read(f, pkg); |
| 667 | r = 0; |
| 668 | } |
| 669 | } |
| 670 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 671 | chdir(cwd); |
| 672 | free(cwd); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 673 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 674 | return r; |
| 675 | } |
| 676 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 677 | static int dpkg_unpack(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 678 | { |
| 679 | int r = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 680 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 681 | void *status = status_read(); |
| 682 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 683 | if (SYSTEM(rm_dpkgcidir) != 0 || |
| 684 | mkdir(dpkgcidir, S_IRWXU) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 685 | perror("mkdir"); |
| 686 | return 1; |
| 687 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 688 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 689 | dpkg_unpackcontrol(pkg); |
| 690 | r = dpkg_dounpack(pkg); |
| 691 | if (r != 0) break; |
| 692 | } |
| 693 | status_merge(status, pkgs); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 694 | SYSTEM(rm_dpkgcidir); |
| 695 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 696 | return r; |
| 697 | } |
| 698 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 699 | static int dpkg_configure(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 700 | { |
| 701 | int r = 0; |
| 702 | void *found; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 703 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 704 | void *status = status_read(); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 705 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 706 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 707 | found = tfind(pkg, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 708 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 709 | if (found == 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 710 | fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package); |
| 711 | r = 1; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 712 | } |
| 713 | /* configure the package listed in the status file; |
| 714 | * not pkg, as we have info only for the latter |
| 715 | */ |
| 716 | else { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 717 | r = dpkg_doconfigure(*(package_t **)found); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 721 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 722 | return r; |
| 723 | } |
| 724 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 725 | static int dpkg_install(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 726 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 727 | package_t *p, *ordered = 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 728 | void *status = status_read(); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 729 | |
| 730 | if (SYSTEM(rm_dpkgcidir) != 0 || |
| 731 | mkdir(dpkgcidir, S_IRWXU) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 732 | perror("mkdir"); |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | /* Stage 1: parse all the control information */ |
| 737 | for (p = pkgs; p != 0; p = p->next) |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 738 | if (dpkg_unpackcontrol(p) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 739 | perror(p->file); |
| 740 | /* force loop break, and prevents further ops */ |
| 741 | pkgs = 0; |
| 742 | } |
| 743 | |
| 744 | /* Stage 2: resolve dependencies */ |
| 745 | #ifdef DODEPENDS |
| 746 | ordered = depends_resolve(pkgs, status); |
| 747 | #else |
| 748 | ordered = pkgs; |
| 749 | #endif |
| 750 | |
| 751 | /* Stage 3: install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 752 | for (p = ordered; p != 0; p = p->next) { |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 753 | p->status &= status_wantmask; |
| 754 | p->status |= status_wantinstall; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 755 | |
| 756 | /* for now the flag is always set to ok... this is probably |
| 757 | * not what we want |
| 758 | */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 759 | p->status &= status_flagmask; |
| 760 | p->status |= status_flagok; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 761 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 762 | if (dpkg_doinstall(p) != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 763 | perror(p->file); |
| 764 | } |
| 765 | } |
| 766 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 767 | if (ordered != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 768 | status_merge(status, pkgs); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 769 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 770 | SYSTEM(rm_dpkgcidir); |
| 771 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 775 | static int dpkg_remove(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 776 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 777 | package_t *p; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 778 | void *status = status_read(); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 779 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 780 | for (p = pkgs; p != 0; p = p->next) |
| 781 | { |
| 782 | } |
| 783 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 784 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 788 | extern int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 789 | { |
| 790 | char opt = 0; |
| 791 | char *s; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 792 | package_t *p, *packages = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 793 | char *cwd = getcwd(0, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 794 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 795 | while (*++argv) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 796 | if (**argv == '-') { |
| 797 | /* Nasty little hack to "parse" long options. */ |
| 798 | s = *argv; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 799 | while (*s == '-') { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 800 | s++; |
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 | opt=s[0]; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 803 | } else { |
| 804 | p = (package_t *)xmalloc(sizeof(package_t)); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 805 | memset(p, 0, sizeof(package_t)); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 806 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 807 | if (**argv == '/') { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 808 | p->file = *argv; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 809 | } else |
| 810 | if (opt != 'c') { |
| 811 | p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2); |
| 812 | sprintf(p->file, "%s/%s", cwd, *argv); |
| 813 | } else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 814 | p->package = strdup(*argv); |
| 815 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 816 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 817 | p->next = packages; |
| 818 | packages = p; |
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 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 821 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 822 | switch (opt) { |
| 823 | case 'i': |
| 824 | return dpkg_install(packages); |
| 825 | case 'r': |
| 826 | return dpkg_remove(packages); |
| 827 | case 'u': |
| 828 | return dpkg_unpack(packages); |
| 829 | case 'c': |
| 830 | return dpkg_configure(packages); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 831 | default : |
| 832 | usage(dpkg_usage); |
| 833 | return 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 834 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 835 | } |