Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1 | #include <dirent.h> |
| 2 | #include <getopt.h> |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <search.h> |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <unistd.h> |
| 10 | #include <utime.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 13 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 14 | #include "busybox.h" |
| 15 | |
Eric Andersen | 3c0b425 | 2001-03-14 01:31:11 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 17 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 18 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 19 | /* Should we do full dependency checking? */ |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 20 | //#define DODEPENDS 0 |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 21 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 22 | /* Should we do debugging? */ |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 23 | //#define DODEBUG 0 |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef DODEBUG |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 26 | #define SYSTEM(x) do_system(x) |
| 27 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) |
| 28 | #else |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 29 | #define SYSTEM(x) system(x) |
| 30 | #define DPRINTF(fmt,args...) /* nothing */ |
| 31 | #endif |
| 32 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 33 | /* from dpkg-deb.c */ |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 34 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 35 | static const char statusfile[] = "/var/lib/dpkg/status.udeb"; |
| 36 | static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new"; |
| 37 | static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak"; |
| 38 | |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 39 | static const char infodir[] = "/var/lib/dpkg/info/"; |
| 40 | static const char udpkg_quiet[] = "UDPKG_QUIET"; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 42 | //static const int status_want_unknown = 1; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 43 | static const int state_want_install = 2; |
| 44 | //static const int state_want_hold = 3; |
| 45 | //static const int state_want_deinstall = 4; |
| 46 | //static const int state_want_purge = 5; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 48 | static const int state_flag_ok = 1; |
| 49 | //static const int state_flag_reinstreq = 2; |
| 50 | //static const int state_flag_hold = 3; |
| 51 | //static const int state_flag_holdreinstreq = 4; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 52 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 53 | //static const int state_statusnoninstalled = 1; |
| 54 | static const int state_status_unpacked = 2; |
| 55 | static const int state_status_halfconfigured = 3; |
| 56 | static const int state_status_installed = 4; |
| 57 | static const int state_status_halfinstalled = 5; |
| 58 | //static const int state_statusconfigfiles = 6; |
| 59 | //static const int state_statuspostinstfailed = 7; |
| 60 | //static const int state_statusremovalfailed = 8; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 61 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 62 | static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 }; |
| 63 | static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 }; |
| 64 | static const char *state_words_status[] = { "not-installed", "unpacked", "half-configured", "installed", |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 65 | "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 }; |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 66 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 67 | static const int color_white = 0; |
| 68 | static const int color_grey = 1; |
| 69 | static const int color_black = 2; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 70 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 71 | /* data structures */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 72 | typedef struct package_s { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 73 | char *filename; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 74 | char *package; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 75 | unsigned char state_want; |
| 76 | unsigned char state_flag; |
| 77 | unsigned char state_status; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 78 | char *depends; |
| 79 | char *provides; |
| 80 | char *description; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 81 | char *priority; |
| 82 | char *section; |
| 83 | char *installed_size; |
| 84 | char *maintainer; |
| 85 | char *source; |
| 86 | char *version; |
| 87 | char *pre_depends; |
| 88 | char *replaces; |
| 89 | char *recommends; |
| 90 | char *suggests; |
| 91 | char *conflicts; |
| 92 | char *conffiles; |
| 93 | char *long_description; |
| 94 | char *architecture; |
| 95 | char *md5sum; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 96 | int installer_menu_item; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 97 | char color; /* for topo-sort */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 98 | struct package_s *requiredfor[DEPENDSMAX]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 99 | unsigned short requiredcount; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 100 | struct package_s *next; |
| 101 | } package_t; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 102 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 103 | #ifdef DODEBUG |
| 104 | static int do_system(const char *cmd) |
| 105 | { |
| 106 | DPRINTF("cmd is %s\n", cmd); |
| 107 | return system(cmd); |
| 108 | } |
| 109 | #else |
| 110 | #define do_system(cmd) system(cmd) |
| 111 | #endif |
| 112 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 113 | static int package_compare(const void *p1, const void *p2) |
| 114 | { |
| 115 | return strcmp(((package_t *)p1)->package, |
| 116 | ((package_t *)p2)->package); |
| 117 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 118 | |
| 119 | #ifdef DODEPENDS |
| 120 | #include <ctype.h> |
| 121 | |
| 122 | static char **depends_split(const char *dependsstr) |
| 123 | { |
| 124 | static char *dependsvec[DEPENDSMAX]; |
| 125 | char *p; |
| 126 | int i = 0; |
| 127 | |
| 128 | dependsvec[0] = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 129 | if (dependsstr == 0) { |
| 130 | goto end; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 131 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 132 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 133 | p = xstrdup(dependsstr); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 134 | while (*p != 0 && *p != '\n') { |
| 135 | if (*p != ' ') { |
| 136 | if (*p == ',') { |
| 137 | *p = 0; |
| 138 | dependsvec[++i] = 0; |
| 139 | } else { |
| 140 | if (dependsvec[i] == 0) { |
| 141 | dependsvec[i] = p; |
| 142 | } |
| 143 | } |
| 144 | } else { |
| 145 | *p = 0; /* eat the space... */ |
| 146 | } |
| 147 | p++; |
| 148 | } |
| 149 | *p = 0; |
| 150 | |
| 151 | end: |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 152 | dependsvec[i+1] = 0; |
| 153 | return dependsvec; |
| 154 | } |
| 155 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 156 | /* Topological sort algorithm: |
| 157 | * ordered is the output list, pkgs is the dependency graph, pkg is |
| 158 | * the current node |
| 159 | * |
| 160 | * recursively add all the adjacent nodes to the ordered list, marking |
| 161 | * each one as visited along the way |
| 162 | * |
| 163 | * yes, this algorithm looks a bit odd when all the params have the |
| 164 | * same type :-) |
| 165 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 166 | static void depends_sort_visit(package_t **ordered, package_t *pkgs, |
| 167 | package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 168 | { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 169 | unsigned short i; |
| 170 | |
| 171 | /* mark node as processing */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 172 | pkg->color = color_grey; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 173 | |
| 174 | /* visit each not-yet-visited node */ |
| 175 | for (i = 0; i < pkg->requiredcount; i++) |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 176 | if (pkg->requiredfor[i]->color == color_white) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 177 | depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]); |
| 178 | |
| 179 | #if 0 |
| 180 | /* add it to the list */ |
Eric Andersen | b50d707 | 2001-02-15 19:50:11 +0000 | [diff] [blame] | 181 | newnode = (struct package_t *)xmalloc(sizeof(struct package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 182 | /* make a shallow copy */ |
| 183 | *newnode = *pkg; |
| 184 | newnode->next = *ordered; |
| 185 | *ordered = newnode; |
| 186 | #endif |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 187 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 188 | pkg->next = *ordered; |
| 189 | *ordered = pkg; |
| 190 | |
| 191 | /* mark node as done */ |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 192 | pkg->color = color_black; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 195 | static package_t *depends_sort(package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 196 | { |
| 197 | /* TODO: it needs to break cycles in the to-be-installed package |
| 198 | * graph... */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 199 | package_t *ordered = NULL; |
| 200 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 201 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 202 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 203 | pkg->color = color_white; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 204 | } |
| 205 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
| 206 | if (pkg->color == color_white) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 207 | depends_sort_visit(&ordered, pkgs, pkg); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 210 | |
| 211 | /* Leaks the old list... return the new one... */ |
| 212 | return ordered; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* resolve package dependencies -- |
| 217 | * for each package in the list of packages to be installed, we parse its |
| 218 | * dependency info to determine if the dependent packages are either |
| 219 | * already installed, or are scheduled to be installed. If both tests fail |
| 220 | * than bail. |
| 221 | * |
| 222 | * The algorithm here is O(n^2*m) where n = number of packages to be |
| 223 | * installed and m is the # of dependencies per package. Not a terribly |
| 224 | * efficient algorithm, but given that at any one time you are unlikely |
| 225 | * to install a very large number of packages it doesn't really matter |
| 226 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 227 | static package_t *depends_resolve(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 228 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 229 | package_t *pkg, *chk; |
| 230 | package_t dependpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 231 | char **dependsvec; |
| 232 | int i; |
| 233 | void *found; |
| 234 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 235 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 236 | dependsvec = depends_split(pkg->depends); |
| 237 | i = 0; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 238 | while (dependsvec[i] != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 239 | /* Check for dependencies; first look for installed packages */ |
| 240 | dependpkg.package = dependsvec[i]; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 241 | if (((found = tfind(&dependpkg, &status, package_compare)) == 0) || |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 242 | ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) && |
| 243 | (chk->state_status & state_status_installed))) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 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) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 251 | error_msg("Too many dependencies for %s", 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) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 261 | error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 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 | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 287 | static unsigned char status_parse(const char *line, const char **status_words) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 288 | { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 289 | unsigned char status_num; |
| 290 | int i = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 291 | |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 292 | while (status_words[i] != 0) { |
| 293 | if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) { |
| 294 | status_num = (char)i; |
| 295 | return(status_num); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 296 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 297 | i++; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 298 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 299 | /* parse error */ |
| 300 | error_msg("Invalid status word"); |
| 301 | return(0); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 305 | * Read the buffered control file and parse it, |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 306 | * filling parsed fields into the package structure |
| 307 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 308 | static int fill_package_struct(package_t *package, const char *package_buffer) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 309 | { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 310 | char *field = NULL; |
| 311 | int field_start = 0; |
| 312 | int field_length = 0; |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 313 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 314 | while ((field = read_package_field(&package_buffer[field_start])) != NULL) { |
| 315 | field_length = strlen(field); |
| 316 | field_start += (field_length + 1); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 317 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 318 | if (strlen(field) == 0) { |
| 319 | printf("empty line: *this shouldnt happen i dont think*\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 320 | break; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 321 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 322 | |
| 323 | /* these are common to both installed and uninstalled packages */ |
| 324 | if (strstr(field, "Package: ") == field) { |
| 325 | package->package = strdup(field + 9); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 326 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 327 | else if (strstr(field, "Depends: ") == field) { |
| 328 | package->depends = strdup(field + 9); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 329 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 330 | else if (strstr(field, "Provides: ") == field) { |
| 331 | package->provides = strdup(field + 10); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 332 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 333 | /* This is specific to the Debian Installer. Ifdef? */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 334 | else if (strstr(field, "installer-menu-item: ") == field) { |
| 335 | package->installer_menu_item = atoi(field + 21); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 336 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 337 | else if (strstr(field, "Description: ") == field) { |
| 338 | package->description = strdup(field + 13); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 339 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 340 | else if (strstr(field, "Priority: ") == field) { |
| 341 | package->priority = strdup(field + 10); |
| 342 | } |
| 343 | else if (strstr(field, "Section: ") == field) { |
| 344 | package->section = strdup(field + 9); |
| 345 | } |
| 346 | else if (strstr(field, "Installed-Size: ") == field) { |
| 347 | package->installed_size = strdup(field + 16); |
| 348 | } |
| 349 | else if (strstr(field, "Maintainer: ") == field) { |
| 350 | package->maintainer = strdup(field + 12); |
| 351 | } |
| 352 | else if (strstr(field, "Version: ") == field) { |
| 353 | package->version = strdup(field + 9); |
| 354 | } |
| 355 | else if (strstr(field, "Suggests: ") == field) { |
| 356 | package->suggests = strdup(field + 10); |
| 357 | } |
| 358 | else if (strstr(field, "Recommends: ") == field) { |
| 359 | package->recommends = strdup(field + 12); |
| 360 | } |
| 361 | /* else if (strstr(field, "Conffiles: ") == field) { |
| 362 | package->conffiles = read_block(file); |
| 363 | package->conffiles = xcalloc(1, 1); |
| 364 | while ((field = strtok(NULL, "\n")) != NULL) { |
| 365 | package->long_description = xrealloc(package->conffiles, |
| 366 | strlen(package->conffiles) + strlen(field) + 1); |
| 367 | strcat(package->conffiles, field); |
| 368 | } |
| 369 | } |
| 370 | */ |
| 371 | /* These are only in available file */ |
| 372 | else if (strstr(field, "Architecture: ") == field) { |
| 373 | package->architecture = strdup(field + 14); |
| 374 | } |
| 375 | else if (strstr(field, "Filename: ") == field) { |
| 376 | package->filename = strdup(field + 10); |
| 377 | } |
| 378 | else if (strstr(field, "MD5sum ") == field) { |
| 379 | package->md5sum = strdup(field + 7); |
| 380 | } |
| 381 | |
| 382 | /* This is only needed for status file */ |
| 383 | if (strstr(field, "Status: ") == field) { |
| 384 | char *word_pointer; |
| 385 | |
| 386 | word_pointer = strchr(field, ' ') + 1; |
| 387 | package->state_want = status_parse(word_pointer, state_words_want); |
| 388 | word_pointer = strchr(word_pointer, ' ') + 1; |
| 389 | package->state_flag = status_parse(word_pointer, state_words_flag); |
| 390 | word_pointer = strchr(word_pointer, ' ') + 1; |
| 391 | package->state_status = status_parse(word_pointer, state_words_status); |
| 392 | } else { |
| 393 | package->state_want = status_parse("purge", state_words_want); |
| 394 | package->state_flag = status_parse("ok", state_words_flag); |
| 395 | package->state_status = status_parse("not-installed", state_words_status); |
| 396 | } |
| 397 | |
| 398 | free(field); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 399 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 400 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 403 | extern void write_package(FILE *out_file, package_t *pkg) |
| 404 | { |
| 405 | if (pkg->package) { |
| 406 | fprintf(out_file, "Package: %s\n", pkg->package); |
| 407 | } |
| 408 | if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) { |
| 409 | fprintf(out_file, "Status: %s %s %s\n", |
| 410 | state_words_want[pkg->state_want - 1], |
| 411 | state_words_flag[pkg->state_flag - 1], |
| 412 | state_words_status[pkg->state_status - 1]); |
| 413 | } |
| 414 | if (pkg->depends) { |
| 415 | fprintf(out_file, "Depends: %s\n", pkg->depends); |
| 416 | } |
| 417 | if (pkg->provides) { |
| 418 | fprintf(out_file, "Provides: %s\n", pkg->provides); |
| 419 | } |
| 420 | if (pkg->priority) { |
| 421 | fprintf(out_file, "Priority: %s\n", pkg->priority); |
| 422 | } |
| 423 | if (pkg->section) { |
| 424 | fprintf(out_file, "Section: %s\n", pkg->section); |
| 425 | } |
| 426 | if (pkg->section) { |
| 427 | fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size); |
| 428 | } |
| 429 | if (pkg->maintainer) { |
| 430 | fprintf(out_file, "Maintainer: %s\n", pkg->maintainer); |
| 431 | } |
| 432 | if (pkg->source) { |
| 433 | fprintf(out_file, "Source: %s\n", pkg->source); |
| 434 | } |
| 435 | if (pkg->version) { |
| 436 | fprintf(out_file, "Version: %s\n", pkg->version); |
| 437 | } |
| 438 | if (pkg->pre_depends) { |
| 439 | fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends); |
| 440 | } |
| 441 | if (pkg->replaces) { |
| 442 | fprintf(out_file, "Replaces: %s\n", pkg->replaces); |
| 443 | } |
| 444 | if (pkg->recommends) { |
| 445 | fprintf(out_file, "Recommends: %s\n", pkg->recommends); |
| 446 | } |
| 447 | if (pkg->suggests) { |
| 448 | fprintf(out_file, "Suggests: %s\n", pkg->suggests); |
| 449 | } |
| 450 | if (pkg->conflicts) { |
| 451 | fprintf(out_file, "Conflicts: %s\n", pkg->conflicts); |
| 452 | } |
| 453 | if (pkg->conffiles) { |
| 454 | fprintf(out_file, "Conf-files: %s\n", pkg->conffiles); |
| 455 | } |
| 456 | if (pkg->architecture) { |
| 457 | fprintf(out_file, "Architecture: %s\n", pkg->architecture); |
| 458 | } |
| 459 | if (pkg->filename) { |
| 460 | fprintf(out_file, "Filename: %s\n", pkg->filename); |
| 461 | } |
| 462 | if (pkg->md5sum) { |
| 463 | fprintf(out_file, "MD5sum: %s\n", pkg->md5sum); |
| 464 | } |
| 465 | if (pkg->installer_menu_item) { |
| 466 | fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item); |
| 467 | } |
| 468 | if (pkg->description) { |
| 469 | fprintf(out_file, "Description: %s\n", pkg->description); |
| 470 | } |
| 471 | fputc('\n', out_file); |
| 472 | pkg = pkg->next; |
| 473 | } |
| 474 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 475 | static void *status_read(void) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 476 | { |
| 477 | FILE *f; |
| 478 | void *status = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 479 | package_t *m = 0, *p = 0, *t = 0; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 480 | char *package_control_buffer = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 481 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 482 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 483 | printf("(Reading database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 484 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 485 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 486 | if ((f = wfopen(statusfile, "r")) == NULL) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 487 | return(NULL); |
| 488 | } |
| 489 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 490 | while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 491 | m = (package_t *)xcalloc(1, sizeof(package_t)); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 492 | fill_package_struct(m, package_control_buffer); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 493 | if (m->package) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 494 | /* |
| 495 | * If there is an item in the tree by this name, |
| 496 | * it must be a virtual package; insert real |
| 497 | * package in preference. |
| 498 | */ |
| 499 | tdelete(m, &status, package_compare); |
| 500 | tsearch(m, &status, package_compare); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 501 | if (m->provides) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 502 | /* |
| 503 | * A "Provides" triggers the insertion |
| 504 | * of a pseudo package into the status |
| 505 | * binary-tree. |
| 506 | */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 507 | p = (package_t *)xcalloc(1, sizeof(package_t)); |
| 508 | p->package = xstrdup(m->provides); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 509 | t = *(package_t **)tsearch(p, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 510 | if (t != p) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 511 | free(p->package); |
| 512 | free(p); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 513 | } else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 514 | /* |
| 515 | * Pseudo package status is the |
| 516 | * same as the status of the |
| 517 | * package providing it |
| 518 | * FIXME: (not quite right, if 2 |
| 519 | * packages of different statuses |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 520 | * provide it). |
| 521 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 522 | t->state_want = m->state_want; |
| 523 | t->state_flag = m->state_flag; |
| 524 | t->state_status = m->state_status; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 528 | else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 529 | free(m); |
| 530 | } |
| 531 | } |
| 532 | fclose(f); |
| 533 | return status; |
| 534 | } |
| 535 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 536 | static int status_merge(void *status, package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 537 | { |
| 538 | FILE *fin, *fout; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 539 | char *line = NULL; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 540 | package_t *pkg = 0, *statpkg = 0; |
| 541 | package_t locpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 542 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 543 | if ((fout = wfopen(new_statusfile, "w")) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 544 | return 0; |
| 545 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 546 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 547 | printf("(Updating database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 548 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 549 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 550 | /* |
| 551 | * Dont use wfopen here, handle errors ourself |
| 552 | */ |
| 553 | if ((fin = fopen(statusfile, "r")) != NULL) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 554 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
Eric Andersen | c1bdffe | 2001-04-26 15:56:47 +0000 | [diff] [blame] | 555 | chomp(line); /* trim newline */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 556 | /* If we see a package header, find out if it's a package |
| 557 | * that we have processed. if so, we skip that block for |
| 558 | * now (write it at the end). |
| 559 | * |
| 560 | * we also look at packages in the status cache and update |
| 561 | * their status fields |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 562 | */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 563 | if (strstr(line, "Package: ") == line) { |
| 564 | for (pkg = pkgs; pkg != 0 && strcmp(line + 9, |
| 565 | pkg->package) != 0; pkg = pkg->next) ; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 566 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 567 | locpkg.package = line + 9; |
| 568 | statpkg = tfind(&locpkg, &status, package_compare); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 569 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 570 | /* note: statpkg should be non-zero, unless the status |
| 571 | * file was changed while we are processing (no locking |
| 572 | * is currently done... |
| 573 | */ |
| 574 | if (statpkg != 0) { |
| 575 | statpkg = *(package_t **)statpkg; |
| 576 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 577 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 578 | if (pkg != 0) { |
| 579 | continue; |
| 580 | } |
| 581 | if (strstr(line, "Status: ") == line && statpkg != 0) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 582 | snprintf(line, sizeof(line), "Status: %s %s %s", |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 583 | state_words_want[statpkg->state_want - 1], |
| 584 | state_words_flag[statpkg->state_flag - 1], |
| 585 | state_words_status[statpkg->state_status - 1]); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 586 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 587 | fprintf(fout, "%s\n", line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 588 | } |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 589 | fclose(fin); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 590 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 591 | free(line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 592 | |
| 593 | // Print out packages we processed. |
| 594 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 595 | write_package(fout, pkg); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 596 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 597 | fclose(fout); |
| 598 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 599 | /* |
| 600 | * Its ok if renaming statusfile fails becasue it doesnt exist |
| 601 | */ |
| 602 | if (rename(statusfile, bak_statusfile) == -1) { |
| 603 | struct stat stat_buf; |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 604 | if (stat(statusfile, &stat_buf) == 0) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 605 | error_msg("Couldnt create backup status file"); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 606 | return(EXIT_FAILURE); |
| 607 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 608 | error_msg("No status file found, creating new one"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 609 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 610 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 611 | if (rename(new_statusfile, statusfile) == -1) { |
| 612 | error_msg("Couldnt create status file"); |
| 613 | return(EXIT_FAILURE); |
| 614 | } |
| 615 | return(EXIT_SUCCESS); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 618 | static int is_file(const char *fn) |
| 619 | { |
| 620 | struct stat statbuf; |
| 621 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 622 | if (stat(fn, &statbuf) < 0) { |
| 623 | return 0; |
| 624 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 625 | return S_ISREG(statbuf.st_mode); |
| 626 | } |
| 627 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 628 | static int dpkg_doconfigure(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 629 | { |
| 630 | int r; |
| 631 | char postinst[1024]; |
| 632 | char buf[1024]; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 633 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 634 | DPRINTF("Configuring %s\n", pkg->package); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 635 | pkg->state_status = 0; |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 636 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 637 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 638 | if (is_file(postinst)) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 639 | snprintf(buf, sizeof(buf), "%s configure", postinst); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 640 | if ((r = do_system(buf)) != 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 641 | error_msg("postinst exited with status %d\n", r); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 642 | pkg->state_status = state_status_halfconfigured; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 643 | return 1; |
| 644 | } |
| 645 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 646 | pkg->state_status = state_status_installed; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 651 | static int dpkg_dounpack(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 652 | { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 653 | int r = 0; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 654 | int status = TRUE; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 655 | char *lst_path; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 656 | |
| 657 | DPRINTF("Unpacking %s\n", pkg->package); |
| 658 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 659 | /* extract the data file */ |
| 660 | deb_extract(pkg->filename, extract_extract, "/", NULL); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 661 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 662 | /* extract the control files */ |
| 663 | deb_extract(pkg->filename, extract_control, infodir, pkg->package); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 664 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 665 | /* Create the list file */ |
| 666 | lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6); |
| 667 | strcpy(lst_path, infodir); |
| 668 | strcat(lst_path, pkg->package); |
| 669 | strcat(lst_path, ".list"); |
| 670 | deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 671 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 672 | pkg->state_want = state_want_install; |
| 673 | pkg->state_flag = state_flag_ok; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 674 | |
| 675 | if (status == TRUE) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 676 | pkg->state_status = state_status_unpacked; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 677 | } else { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 678 | pkg->state_status = state_status_halfinstalled; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 679 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 681 | return r; |
| 682 | } |
| 683 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 684 | /* |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 685 | * Extract and parse the control file from control.tar.gz |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 686 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 687 | static int dpkg_read_control(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 688 | { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 689 | FILE *pkg_file; |
| 690 | char *control_buffer = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 691 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 692 | if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) { |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 693 | return EXIT_FAILURE; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 694 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 695 | control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL); |
| 696 | fill_package_struct(pkg, control_buffer); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 697 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 700 | static int dpkg_unpack(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 701 | { |
| 702 | int r = 0; |
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 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 705 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 706 | dpkg_read_control(pkg); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 707 | if ((r = dpkg_dounpack(pkg)) != 0 ) { |
| 708 | break; |
| 709 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 710 | } |
| 711 | status_merge(status, pkgs); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 712 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 713 | return r; |
| 714 | } |
| 715 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 716 | static int dpkg_configure(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 717 | { |
| 718 | int r = 0; |
| 719 | void *found; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 720 | package_t *pkg; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 721 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 722 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 723 | found = tfind(pkg, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 724 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 725 | if (found == 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 726 | error_msg("Trying to configure %s, but it is not installed", pkg->package); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 727 | r = 1; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 728 | } |
| 729 | /* configure the package listed in the status file; |
| 730 | * not pkg, as we have info only for the latter |
| 731 | */ |
| 732 | else { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 733 | r = dpkg_doconfigure(*(package_t **)found); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 737 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 738 | return r; |
| 739 | } |
| 740 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 741 | static int dpkg_install(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 742 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 743 | package_t *p, *ordered = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 744 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 745 | /* Stage 1: parse all the control information */ |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 746 | for (p = pkgs; p != 0; p = p->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 747 | dpkg_read_control(p); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 750 | /* Stage 2: resolve dependencies */ |
| 751 | #ifdef DODEPENDS |
| 752 | ordered = depends_resolve(pkgs, status); |
| 753 | #else |
| 754 | ordered = pkgs; |
| 755 | #endif |
| 756 | |
| 757 | /* Stage 3: install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 758 | for (p = ordered; p != 0; p = p->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 759 | p->state_want = state_want_install; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 760 | |
| 761 | /* for now the flag is always set to ok... this is probably |
| 762 | * not what we want |
| 763 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 764 | p->state_flag = state_flag_ok; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 765 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 766 | DPRINTF("Installing %s\n", p->package); |
| 767 | if (dpkg_dounpack(p) != 0) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 768 | perror_msg(p->filename); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 769 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 770 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 771 | if (dpkg_doconfigure(p) != 0) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 772 | perror_msg(p->filename); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 775 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 776 | if (ordered != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 777 | status_merge(status, pkgs); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 778 | } |
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 | return 0; |
| 781 | } |
| 782 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 783 | /* |
| 784 | * Not implemented yet |
| 785 | * |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 786 | static int dpkg_remove(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 787 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 788 | package_t *p; |
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 | for (p = pkgs; p != 0; p = p->next) |
| 791 | { |
| 792 | } |
| 793 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 794 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 795 | return 0; |
| 796 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 797 | */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 798 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 799 | extern int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 800 | { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 801 | const int arg_install = 1; |
| 802 | const int arg_unpack = 2; |
| 803 | const int arg_configure = 4; |
| 804 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 805 | package_t *p, *packages = NULL; |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 806 | void *status = NULL; |
Eric Andersen | 5a9d441 | 2001-05-24 14:16:28 +0000 | [diff] [blame] | 807 | int opt = 0; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 808 | int optflag = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 809 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 810 | while ((opt = getopt(argc, argv, "iruc")) != -1) { |
| 811 | switch (opt) { |
| 812 | case 'i': |
| 813 | optflag |= arg_install; |
| 814 | break; |
| 815 | case 'u': |
| 816 | optflag |= arg_unpack; |
| 817 | break; |
| 818 | case 'c': |
| 819 | optflag |= arg_configure; |
| 820 | break; |
| 821 | default: |
| 822 | show_usage(); |
| 823 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 824 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 825 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 826 | while (optind < argc) { |
| 827 | p = (package_t *) xcalloc(1, sizeof(package_t)); |
| 828 | if (optflag & arg_configure) { |
| 829 | p->package = xstrdup(argv[optind]); |
| 830 | } else { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 831 | p->filename = xstrdup(argv[optind]); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 832 | } |
| 833 | p->next = packages; |
| 834 | packages = p; |
| 835 | |
| 836 | optind++; |
| 837 | } |
| 838 | |
Glenn L McGrath | 37849f3 | 2001-04-08 07:23:53 +0000 | [diff] [blame] | 839 | create_path(infodir, S_IRWXU); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 840 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 841 | status = status_read(); |
| 842 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 843 | if (optflag & arg_install) { |
| 844 | return dpkg_install(packages, status); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 845 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 846 | else if (optflag & arg_unpack) { |
| 847 | return dpkg_unpack(packages, status); |
| 848 | } |
| 849 | else if (optflag & arg_configure) { |
| 850 | return dpkg_configure(packages, status); |
| 851 | } |
| 852 | return(EXIT_FAILURE); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 853 | } |