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 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 16 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 18 | /* Should we do full dependency checking? */ |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 19 | //#define DODEPENDS 0 |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 20 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 21 | /* Should we do debugging? */ |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 22 | //#define DODEBUG 0 |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 23 | |
| 24 | #ifdef DODEBUG |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 25 | #define SYSTEM(x) do_system(x) |
| 26 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) |
| 27 | #else |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 28 | #define SYSTEM(x) system(x) |
| 29 | #define DPRINTF(fmt,args...) /* nothing */ |
| 30 | #endif |
| 31 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 32 | /* from dpkg-deb.c */ |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 33 | |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 34 | static const char statusfile[] = "/var/lib/dpkg/status.udeb"; |
| 35 | static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new"; |
| 36 | static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak"; |
| 37 | |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 38 | static const char infodir[] = "/var/lib/dpkg/info/"; |
| 39 | static const char udpkg_quiet[] = "UDPKG_QUIET"; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 40 | |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 41 | //static const int status_want_unknown = 1; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 42 | static const int state_want_install = 2; |
| 43 | //static const int state_want_hold = 3; |
| 44 | //static const int state_want_deinstall = 4; |
| 45 | //static const int state_want_purge = 5; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 46 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 47 | static const int state_flag_ok = 1; |
| 48 | //static const int state_flag_reinstreq = 2; |
| 49 | //static const int state_flag_hold = 3; |
| 50 | //static const int state_flag_holdreinstreq = 4; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 51 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 52 | //static const int state_statusnoninstalled = 1; |
| 53 | static const int state_status_unpacked = 2; |
| 54 | static const int state_status_halfconfigured = 3; |
| 55 | static const int state_status_installed = 4; |
| 56 | static const int state_status_halfinstalled = 5; |
| 57 | //static const int state_statusconfigfiles = 6; |
| 58 | //static const int state_statuspostinstfailed = 7; |
| 59 | //static const int state_statusremovalfailed = 8; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 61 | static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 }; |
| 62 | static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 }; |
| 63 | 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] | 64 | "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 }; |
Glenn L McGrath | af8c65d | 2001-02-10 03:19:51 +0000 | [diff] [blame] | 65 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 66 | static const int color_white = 0; |
| 67 | static const int color_grey = 1; |
| 68 | static const int color_black = 2; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 69 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 70 | /* data structures */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 71 | typedef struct package_s { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 72 | char *filename; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 73 | char *package; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 74 | unsigned char state_want; |
| 75 | unsigned char state_flag; |
| 76 | unsigned char state_status; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 77 | char *depends; |
| 78 | char *provides; |
| 79 | char *description; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 80 | char *priority; |
| 81 | char *section; |
| 82 | char *installed_size; |
| 83 | char *maintainer; |
| 84 | char *source; |
| 85 | char *version; |
| 86 | char *pre_depends; |
| 87 | char *replaces; |
| 88 | char *recommends; |
| 89 | char *suggests; |
| 90 | char *conflicts; |
| 91 | char *conffiles; |
| 92 | char *long_description; |
| 93 | char *architecture; |
| 94 | char *md5sum; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 95 | int installer_menu_item; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 132 | p = xstrdup(dependsstr); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 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 */ |
Eric Andersen | b50d707 | 2001-02-15 19:50:11 +0000 | [diff] [blame] | 180 | newnode = (struct package_t *)xmalloc(sizeof(struct package_t)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 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]; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 240 | if (((found = tfind(&dependpkg, &status, package_compare)) == 0) || |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 241 | ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) && |
| 242 | (chk->state_status & state_status_installed))) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 243 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 244 | /* if it fails, we look through the list of packages we are going to |
| 245 | * install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 246 | for (chk = pkgs; chk != 0; chk = chk->next) { |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 247 | if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 248 | strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { |
| 249 | if (chk->requiredcount >= DEPENDSMAX) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 250 | error_msg("Too many dependencies for %s", chk->package); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 251 | return 0; |
| 252 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 253 | if (chk != pkg) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 254 | chk->requiredfor[chk->requiredcount++] = pkg; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 255 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 259 | if (chk == 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 260 | 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] | 261 | return 0; |
| 262 | } |
| 263 | } |
| 264 | i++; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return depends_sort(pkgs); |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | /* Status file handling routines |
| 273 | * |
| 274 | * This is a fairly minimalistic implementation. there are two main functions |
| 275 | * that are supported: |
| 276 | * |
| 277 | * 1) reading the entire status file: |
| 278 | * the status file is read into memory as a binary-tree, with just the |
| 279 | * package and status info preserved |
| 280 | * |
| 281 | * 2) merging the status file |
| 282 | * control info from (new) packages is merged into the status file, |
| 283 | * replacing any pre-existing entries. when a merge happens, status info |
| 284 | * read using the status_read function is written back to the status file |
| 285 | */ |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 286 | 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] | 287 | { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 288 | unsigned char status_num; |
| 289 | int i = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 290 | |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 291 | while (status_words[i] != 0) { |
| 292 | if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) { |
| 293 | status_num = (char)i; |
| 294 | return(status_num); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 295 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 296 | i++; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 297 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 298 | /* parse error */ |
| 299 | error_msg("Invalid status word"); |
| 300 | return(0); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 304 | * Read the buffered control file and parse it, |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 305 | * filling parsed fields into the package structure |
| 306 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 307 | 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] | 308 | { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 309 | char *field = NULL; |
| 310 | int field_start = 0; |
| 311 | int field_length = 0; |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 312 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 313 | while ((field = read_package_field(&package_buffer[field_start])) != NULL) { |
| 314 | field_length = strlen(field); |
| 315 | field_start += (field_length + 1); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 316 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 317 | if (strlen(field) == 0) { |
| 318 | printf("empty line: *this shouldnt happen i dont think*\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 319 | break; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 320 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 321 | |
| 322 | /* these are common to both installed and uninstalled packages */ |
| 323 | if (strstr(field, "Package: ") == field) { |
| 324 | package->package = strdup(field + 9); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 325 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 326 | else if (strstr(field, "Depends: ") == field) { |
| 327 | package->depends = strdup(field + 9); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 328 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 329 | else if (strstr(field, "Provides: ") == field) { |
| 330 | package->provides = strdup(field + 10); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 331 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 332 | /* This is specific to the Debian Installer. Ifdef? */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 333 | else if (strstr(field, "installer-menu-item: ") == field) { |
| 334 | package->installer_menu_item = atoi(field + 21); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 335 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 336 | else if (strstr(field, "Description: ") == field) { |
| 337 | package->description = strdup(field + 13); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 338 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 339 | else if (strstr(field, "Priority: ") == field) { |
| 340 | package->priority = strdup(field + 10); |
| 341 | } |
| 342 | else if (strstr(field, "Section: ") == field) { |
| 343 | package->section = strdup(field + 9); |
| 344 | } |
| 345 | else if (strstr(field, "Installed-Size: ") == field) { |
| 346 | package->installed_size = strdup(field + 16); |
| 347 | } |
| 348 | else if (strstr(field, "Maintainer: ") == field) { |
| 349 | package->maintainer = strdup(field + 12); |
| 350 | } |
| 351 | else if (strstr(field, "Version: ") == field) { |
| 352 | package->version = strdup(field + 9); |
| 353 | } |
| 354 | else if (strstr(field, "Suggests: ") == field) { |
| 355 | package->suggests = strdup(field + 10); |
| 356 | } |
| 357 | else if (strstr(field, "Recommends: ") == field) { |
| 358 | package->recommends = strdup(field + 12); |
| 359 | } |
| 360 | /* else if (strstr(field, "Conffiles: ") == field) { |
| 361 | package->conffiles = read_block(file); |
| 362 | package->conffiles = xcalloc(1, 1); |
| 363 | while ((field = strtok(NULL, "\n")) != NULL) { |
| 364 | package->long_description = xrealloc(package->conffiles, |
| 365 | strlen(package->conffiles) + strlen(field) + 1); |
| 366 | strcat(package->conffiles, field); |
| 367 | } |
| 368 | } |
| 369 | */ |
| 370 | /* These are only in available file */ |
| 371 | else if (strstr(field, "Architecture: ") == field) { |
| 372 | package->architecture = strdup(field + 14); |
| 373 | } |
| 374 | else if (strstr(field, "Filename: ") == field) { |
| 375 | package->filename = strdup(field + 10); |
| 376 | } |
| 377 | else if (strstr(field, "MD5sum ") == field) { |
| 378 | package->md5sum = strdup(field + 7); |
| 379 | } |
| 380 | |
| 381 | /* This is only needed for status file */ |
| 382 | if (strstr(field, "Status: ") == field) { |
| 383 | char *word_pointer; |
| 384 | |
| 385 | word_pointer = strchr(field, ' ') + 1; |
| 386 | package->state_want = status_parse(word_pointer, state_words_want); |
| 387 | word_pointer = strchr(word_pointer, ' ') + 1; |
| 388 | package->state_flag = status_parse(word_pointer, state_words_flag); |
| 389 | word_pointer = strchr(word_pointer, ' ') + 1; |
| 390 | package->state_status = status_parse(word_pointer, state_words_status); |
| 391 | } else { |
| 392 | package->state_want = status_parse("purge", state_words_want); |
| 393 | package->state_flag = status_parse("ok", state_words_flag); |
| 394 | package->state_status = status_parse("not-installed", state_words_status); |
| 395 | } |
| 396 | |
| 397 | free(field); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 398 | } |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 399 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 402 | extern void write_package(FILE *out_file, package_t *pkg) |
| 403 | { |
| 404 | if (pkg->package) { |
| 405 | fprintf(out_file, "Package: %s\n", pkg->package); |
| 406 | } |
| 407 | if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) { |
| 408 | fprintf(out_file, "Status: %s %s %s\n", |
| 409 | state_words_want[pkg->state_want - 1], |
| 410 | state_words_flag[pkg->state_flag - 1], |
| 411 | state_words_status[pkg->state_status - 1]); |
| 412 | } |
| 413 | if (pkg->depends) { |
| 414 | fprintf(out_file, "Depends: %s\n", pkg->depends); |
| 415 | } |
| 416 | if (pkg->provides) { |
| 417 | fprintf(out_file, "Provides: %s\n", pkg->provides); |
| 418 | } |
| 419 | if (pkg->priority) { |
| 420 | fprintf(out_file, "Priority: %s\n", pkg->priority); |
| 421 | } |
| 422 | if (pkg->section) { |
| 423 | fprintf(out_file, "Section: %s\n", pkg->section); |
| 424 | } |
| 425 | if (pkg->section) { |
| 426 | fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size); |
| 427 | } |
| 428 | if (pkg->maintainer) { |
| 429 | fprintf(out_file, "Maintainer: %s\n", pkg->maintainer); |
| 430 | } |
| 431 | if (pkg->source) { |
| 432 | fprintf(out_file, "Source: %s\n", pkg->source); |
| 433 | } |
| 434 | if (pkg->version) { |
| 435 | fprintf(out_file, "Version: %s\n", pkg->version); |
| 436 | } |
| 437 | if (pkg->pre_depends) { |
| 438 | fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends); |
| 439 | } |
| 440 | if (pkg->replaces) { |
| 441 | fprintf(out_file, "Replaces: %s\n", pkg->replaces); |
| 442 | } |
| 443 | if (pkg->recommends) { |
| 444 | fprintf(out_file, "Recommends: %s\n", pkg->recommends); |
| 445 | } |
| 446 | if (pkg->suggests) { |
| 447 | fprintf(out_file, "Suggests: %s\n", pkg->suggests); |
| 448 | } |
| 449 | if (pkg->conflicts) { |
| 450 | fprintf(out_file, "Conflicts: %s\n", pkg->conflicts); |
| 451 | } |
| 452 | if (pkg->conffiles) { |
| 453 | fprintf(out_file, "Conf-files: %s\n", pkg->conffiles); |
| 454 | } |
| 455 | if (pkg->architecture) { |
| 456 | fprintf(out_file, "Architecture: %s\n", pkg->architecture); |
| 457 | } |
| 458 | if (pkg->filename) { |
| 459 | fprintf(out_file, "Filename: %s\n", pkg->filename); |
| 460 | } |
| 461 | if (pkg->md5sum) { |
| 462 | fprintf(out_file, "MD5sum: %s\n", pkg->md5sum); |
| 463 | } |
| 464 | if (pkg->installer_menu_item) { |
| 465 | fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item); |
| 466 | } |
| 467 | if (pkg->description) { |
| 468 | fprintf(out_file, "Description: %s\n", pkg->description); |
| 469 | } |
| 470 | fputc('\n', out_file); |
| 471 | pkg = pkg->next; |
| 472 | } |
| 473 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 474 | static void *status_read(void) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 475 | { |
| 476 | FILE *f; |
| 477 | void *status = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 478 | package_t *m = 0, *p = 0, *t = 0; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 479 | char *package_control_buffer = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 480 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 481 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 482 | printf("(Reading database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 483 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 484 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 485 | if ((f = wfopen(statusfile, "r")) == NULL) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 486 | return(NULL); |
| 487 | } |
| 488 | |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 489 | while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 490 | m = (package_t *)xcalloc(1, sizeof(package_t)); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 491 | fill_package_struct(m, package_control_buffer); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 492 | if (m->package) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 493 | /* |
| 494 | * If there is an item in the tree by this name, |
| 495 | * it must be a virtual package; insert real |
| 496 | * package in preference. |
| 497 | */ |
| 498 | tdelete(m, &status, package_compare); |
| 499 | tsearch(m, &status, package_compare); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 500 | if (m->provides) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 501 | /* |
| 502 | * A "Provides" triggers the insertion |
| 503 | * of a pseudo package into the status |
| 504 | * binary-tree. |
| 505 | */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 506 | p = (package_t *)xcalloc(1, sizeof(package_t)); |
| 507 | p->package = xstrdup(m->provides); |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 508 | t = *(package_t **)tsearch(p, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 509 | if (t != p) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 510 | free(p->package); |
| 511 | free(p); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 512 | } else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 513 | /* |
| 514 | * Pseudo package status is the |
| 515 | * same as the status of the |
| 516 | * package providing it |
| 517 | * FIXME: (not quite right, if 2 |
| 518 | * packages of different statuses |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 519 | * provide it). |
| 520 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 521 | t->state_want = m->state_want; |
| 522 | t->state_flag = m->state_flag; |
| 523 | t->state_status = m->state_status; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 527 | else { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 528 | free(m); |
| 529 | } |
| 530 | } |
| 531 | fclose(f); |
| 532 | return status; |
| 533 | } |
| 534 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 535 | static int status_merge(void *status, package_t *pkgs) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 536 | { |
| 537 | FILE *fin, *fout; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 538 | char *line = NULL; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 539 | package_t *pkg = 0, *statpkg = 0; |
| 540 | package_t locpkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 541 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 542 | if ((fout = wfopen(new_statusfile, "w")) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 545 | if (getenv(udpkg_quiet) == NULL) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 546 | printf("(Updating database...)\n"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 547 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 548 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 549 | /* |
| 550 | * Dont use wfopen here, handle errors ourself |
| 551 | */ |
| 552 | if ((fin = fopen(statusfile, "r")) != NULL) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 553 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
Eric Andersen | c1bdffe | 2001-04-26 15:56:47 +0000 | [diff] [blame] | 554 | chomp(line); /* trim newline */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 555 | /* If we see a package header, find out if it's a package |
| 556 | * that we have processed. if so, we skip that block for |
| 557 | * now (write it at the end). |
| 558 | * |
| 559 | * we also look at packages in the status cache and update |
| 560 | * their status fields |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 561 | */ |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 562 | if (strstr(line, "Package: ") == line) { |
| 563 | for (pkg = pkgs; pkg != 0 && strcmp(line + 9, |
| 564 | pkg->package) != 0; pkg = pkg->next) ; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 565 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 566 | locpkg.package = line + 9; |
| 567 | statpkg = tfind(&locpkg, &status, package_compare); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 568 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 569 | /* note: statpkg should be non-zero, unless the status |
| 570 | * file was changed while we are processing (no locking |
| 571 | * is currently done... |
| 572 | */ |
| 573 | if (statpkg != 0) { |
| 574 | statpkg = *(package_t **)statpkg; |
| 575 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 576 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 577 | if (pkg != 0) { |
| 578 | continue; |
| 579 | } |
| 580 | if (strstr(line, "Status: ") == line && statpkg != 0) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 581 | snprintf(line, sizeof(line), "Status: %s %s %s", |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 582 | state_words_want[statpkg->state_want - 1], |
| 583 | state_words_flag[statpkg->state_flag - 1], |
| 584 | state_words_status[statpkg->state_status - 1]); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 585 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 586 | fprintf(fout, "%s\n", line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 587 | } |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 588 | fclose(fin); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 589 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 590 | free(line); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 591 | |
| 592 | // Print out packages we processed. |
| 593 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 594 | write_package(fout, pkg); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 595 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 596 | fclose(fout); |
| 597 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 598 | /* |
| 599 | * Its ok if renaming statusfile fails becasue it doesnt exist |
| 600 | */ |
| 601 | if (rename(statusfile, bak_statusfile) == -1) { |
| 602 | struct stat stat_buf; |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 603 | if (stat(statusfile, &stat_buf) == 0) { |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 604 | error_msg("Couldnt create backup status file"); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 605 | return(EXIT_FAILURE); |
| 606 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 607 | error_msg("No status file found, creating new one"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 608 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 609 | |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 610 | if (rename(new_statusfile, statusfile) == -1) { |
| 611 | error_msg("Couldnt create status file"); |
| 612 | return(EXIT_FAILURE); |
| 613 | } |
| 614 | return(EXIT_SUCCESS); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 617 | static int is_file(const char *fn) |
| 618 | { |
| 619 | struct stat statbuf; |
| 620 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 621 | if (stat(fn, &statbuf) < 0) { |
| 622 | return 0; |
| 623 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 624 | return S_ISREG(statbuf.st_mode); |
| 625 | } |
| 626 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 627 | static int dpkg_doconfigure(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 628 | { |
| 629 | int r; |
| 630 | char postinst[1024]; |
| 631 | char buf[1024]; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 632 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 633 | DPRINTF("Configuring %s\n", pkg->package); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 634 | pkg->state_status = 0; |
Glenn L McGrath | 510f0dd | 2001-02-10 14:53:08 +0000 | [diff] [blame] | 635 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 636 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 637 | if (is_file(postinst)) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 638 | snprintf(buf, sizeof(buf), "%s configure", postinst); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 639 | if ((r = do_system(buf)) != 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 640 | error_msg("postinst exited with status %d\n", r); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 641 | pkg->state_status = state_status_halfconfigured; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 642 | return 1; |
| 643 | } |
| 644 | } |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 645 | pkg->state_status = state_status_installed; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 650 | static int dpkg_dounpack(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 651 | { |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 652 | FILE *out_stream; |
| 653 | char *info_prefix; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 654 | int status = TRUE; |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 655 | int r = 0; |
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 */ |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 660 | deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", 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 */ |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 663 | info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1); |
| 664 | sprintf(info_prefix, "%s/%s.", infodir, pkg->package); |
| 665 | deb_extract(pkg->package, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 666 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 667 | /* Create the list file */ |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 668 | strcat(info_prefix, "list"); |
| 669 | out_stream = wfopen(info_prefix, "w"); |
| 670 | deb_extract(pkg->package, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL); |
| 671 | fclose(out_stream); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 672 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 673 | pkg->state_want = state_want_install; |
| 674 | pkg->state_flag = state_flag_ok; |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 675 | |
| 676 | if (status == TRUE) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 677 | pkg->state_status = state_status_unpacked; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 678 | } else { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 679 | pkg->state_status = state_status_halfinstalled; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 680 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 681 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 682 | return r; |
| 683 | } |
| 684 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 685 | /* |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 686 | * Extract and parse the control file from control.tar.gz |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 687 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 688 | static int dpkg_read_control(package_t *pkg) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 689 | { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 690 | FILE *pkg_file; |
| 691 | char *control_buffer = NULL; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 692 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 693 | if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) { |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 694 | return EXIT_FAILURE; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 695 | } |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 696 | control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 697 | fill_package_struct(pkg, control_buffer); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 698 | return EXIT_SUCCESS; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 701 | static int dpkg_unpack(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 702 | { |
| 703 | int r = 0; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 704 | package_t *pkg; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 705 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 706 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 707 | dpkg_read_control(pkg); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 708 | if ((r = dpkg_dounpack(pkg)) != 0 ) { |
| 709 | break; |
| 710 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 711 | } |
| 712 | status_merge(status, pkgs); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 713 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 714 | return r; |
| 715 | } |
| 716 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 717 | static int dpkg_configure(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 718 | { |
| 719 | int r = 0; |
| 720 | void *found; |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 721 | package_t *pkg; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 722 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 723 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 724 | found = tfind(pkg, &status, package_compare); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 725 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 726 | if (found == 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 727 | 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] | 728 | r = 1; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 729 | } |
| 730 | /* configure the package listed in the status file; |
| 731 | * not pkg, as we have info only for the latter |
| 732 | */ |
| 733 | else { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 734 | r = dpkg_doconfigure(*(package_t **)found); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 738 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 739 | return r; |
| 740 | } |
| 741 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 742 | static int dpkg_install(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 743 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 744 | package_t *p, *ordered = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 745 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 746 | /* Stage 1: parse all the control information */ |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 747 | for (p = pkgs; p != 0; p = p->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 748 | dpkg_read_control(p); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 751 | /* Stage 2: resolve dependencies */ |
| 752 | #ifdef DODEPENDS |
| 753 | ordered = depends_resolve(pkgs, status); |
| 754 | #else |
| 755 | ordered = pkgs; |
| 756 | #endif |
| 757 | |
| 758 | /* Stage 3: install */ |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 759 | for (p = ordered; p != 0; p = p->next) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 760 | p->state_want = state_want_install; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 761 | |
| 762 | /* for now the flag is always set to ok... this is probably |
| 763 | * not what we want |
| 764 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 765 | p->state_flag = state_flag_ok; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 766 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 767 | DPRINTF("Installing %s\n", p->package); |
| 768 | if (dpkg_dounpack(p) != 0) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 769 | perror_msg(p->filename); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 770 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 771 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 772 | if (dpkg_doconfigure(p) != 0) { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 773 | perror_msg(p->filename); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 776 | |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 777 | if (ordered != 0) { |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 778 | status_merge(status, pkgs); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 779 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 780 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Not implemented yet |
| 786 | * |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 787 | static int dpkg_remove(package_t *pkgs, void *status) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 788 | { |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 789 | package_t *p; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 790 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 791 | for (p = pkgs; p != 0; p = p->next) |
| 792 | { |
| 793 | } |
| 794 | status_merge(status, 0); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 795 | |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 796 | return 0; |
| 797 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 798 | */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 799 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 800 | extern int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 801 | { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 802 | const int arg_install = 1; |
| 803 | const int arg_unpack = 2; |
| 804 | const int arg_configure = 4; |
| 805 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 806 | package_t *p, *packages = NULL; |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 807 | void *status = NULL; |
Eric Andersen | 5a9d441 | 2001-05-24 14:16:28 +0000 | [diff] [blame] | 808 | int opt = 0; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 809 | int optflag = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 810 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 811 | while ((opt = getopt(argc, argv, "iruc")) != -1) { |
| 812 | switch (opt) { |
| 813 | case 'i': |
| 814 | optflag |= arg_install; |
| 815 | break; |
| 816 | case 'u': |
| 817 | optflag |= arg_unpack; |
| 818 | break; |
| 819 | case 'c': |
| 820 | optflag |= arg_configure; |
| 821 | break; |
| 822 | default: |
| 823 | show_usage(); |
| 824 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 825 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 826 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 827 | while (optind < argc) { |
| 828 | p = (package_t *) xcalloc(1, sizeof(package_t)); |
| 829 | if (optflag & arg_configure) { |
| 830 | p->package = xstrdup(argv[optind]); |
| 831 | } else { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 832 | p->filename = xstrdup(argv[optind]); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 833 | } |
| 834 | p->next = packages; |
| 835 | packages = p; |
| 836 | |
| 837 | optind++; |
| 838 | } |
| 839 | |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame^] | 840 | make_directory(infodir, S_IRWXU, FILEUTILS_RECUR); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 841 | |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 842 | status = status_read(); |
| 843 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 844 | if (optflag & arg_install) { |
| 845 | return dpkg_install(packages, status); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 846 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 847 | else if (optflag & arg_unpack) { |
| 848 | return dpkg_unpack(packages, status); |
| 849 | } |
| 850 | else if (optflag & arg_configure) { |
| 851 | return dpkg_configure(packages, status); |
| 852 | } |
| 853 | return(EXIT_FAILURE); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 854 | } |