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