Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini dpkg implementation for busybox. |
| 3 | * This is not meant as a replacemnt for dpkg |
| 4 | * |
| 5 | * Copyright (C) 2001 by Glenn McGrath |
| 6 | * |
| 7 | * Started life as a busybox implementation of udpkg |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Library General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 23 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 24 | /* |
| 25 | * Known difference between busybox dpkg and the official dpkg that i dont |
| 26 | * consider important, its worth keeping a note of differences anyway, just to |
| 27 | * make it easier to maintain. |
| 28 | * - The first value for the Confflile: field isnt placed on a new line. |
| 29 | * - The <package>.control file is extracted and kept in the info dir. |
| 30 | * - When installing a package the Status: field is placed at the end of the |
| 31 | * section, rather than just after the Package: field. |
| 32 | * - Packages with previously unknown status are inserted at the begining of |
| 33 | * the status file |
| 34 | * |
| 35 | * Bugs that need to be fixed |
| 36 | * - (unknown, please let me know when you find any) |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | #include <getopt.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 44 | #include "busybox.h" |
| 45 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 46 | /* NOTE: If you vary HASH_PRIME sizes be aware, |
| 47 | * 1) Tweaking these will have a big effect on how much memory this program uses. |
| 48 | * 2) For computational efficiency these hash tables should be at least 20% |
| 49 | * larger than the maximum number of elements stored in it. |
| 50 | * 3) All _HASH_PRIME's must be a prime number or chaos is assured, if your looking |
| 51 | * for a prime, try http://www.utm.edu/research/primes/lists/small/10000.txt |
| 52 | * 4) If you go bigger than 15 bits you may get into trouble (untested) as its |
| 53 | * sometimes cast to an unsigned int, if you go to 16 bit you will overlap |
| 54 | * int's and chaos is assured, 16381 is the max prime for 14 bit field |
| 55 | */ |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 56 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 57 | /* NAME_HASH_PRIME, Stores package names and versions, |
| 58 | * I estimate it should be at least 50% bigger than PACKAGE_HASH_PRIME, |
| 59 | * as there a lot of duplicate version numbers */ |
| 60 | #define NAME_HASH_PRIME 16381 |
| 61 | char *name_hashtable[NAME_HASH_PRIME + 1]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 62 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 63 | /* PACKAGE_HASH_PRIME, Maximum number of unique packages, |
| 64 | * It must not be smaller than STATUS_HASH_PRIME, |
| 65 | * Currently only packages from status_hashtable are stored in here, but in |
| 66 | * future this may be used to store packages not only from a status file, |
| 67 | * but an available_hashtable, and even multiple packages files. |
| 68 | * Package can be stored more than once if they have different versions. |
| 69 | * e.g. The same package may have different versions in the status file |
| 70 | * and available file */ |
| 71 | #define PACKAGE_HASH_PRIME 10007 |
| 72 | typedef struct edge_s { |
| 73 | unsigned int operator:3; |
| 74 | unsigned int type:4; |
| 75 | unsigned int name:14; |
| 76 | unsigned int version:14; |
| 77 | } edge_t; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 78 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 79 | typedef struct common_node_s { |
| 80 | unsigned int name:14; |
| 81 | unsigned int version:14; |
| 82 | unsigned int num_of_edges:14; |
| 83 | edge_t **edge; |
| 84 | } common_node_t; |
| 85 | common_node_t *package_hashtable[PACKAGE_HASH_PRIME + 1]; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 86 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 87 | /* Currently it doesnt store packages that have state-status of not-installed |
| 88 | * So it only really has to be the size of the maximum number of packages |
| 89 | * likely to be installed at any one time, so there is a bit of leaway here */ |
| 90 | #define STATUS_HASH_PRIME 8191 |
| 91 | typedef struct status_node_s { |
| 92 | unsigned int package:14; /* has to fit PACKAGE_HASH_PRIME */ |
| 93 | unsigned int status:14; /* has to fit STATUS_HASH_PRIME */ |
| 94 | } status_node_t; |
| 95 | status_node_t *status_hashtable[STATUS_HASH_PRIME + 1]; |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 96 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 97 | /* Even numbers are for 'extras', like ored dependecies or null */ |
| 98 | enum edge_type_e { |
| 99 | EDGE_NULL = 0, |
| 100 | EDGE_PRE_DEPENDS = 1, |
| 101 | EDGE_OR_PRE_DEPENDS = 2, |
| 102 | EDGE_DEPENDS = 3, |
| 103 | EDGE_OR_DEPENDS = 4, |
| 104 | EDGE_REPLACES = 5, |
| 105 | EDGE_PROVIDES = 7, |
| 106 | EDGE_CONFLICTS = 9, |
| 107 | EDGE_SUGGESTS = 11, |
| 108 | EDGE_RECOMMENDS = 13, |
| 109 | EDGE_ENHANCES = 15 |
| 110 | }; |
| 111 | enum operator_e { |
| 112 | VER_NULL = 0, |
| 113 | VER_EQUAL = 1, |
| 114 | VER_LESS = 2, |
| 115 | VER_LESS_EQUAL = 3, |
| 116 | VER_MORE = 4, |
| 117 | VER_MORE_EQUAL = 5, |
| 118 | VER_ANY = 6 |
| 119 | }; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 120 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 121 | enum dpkg_opt_e { |
| 122 | dpkg_opt_purge = 1, |
| 123 | dpkg_opt_remove = 2, |
| 124 | dpkg_opt_unpack = 4, |
| 125 | dpkg_opt_configure = 8, |
| 126 | dpkg_opt_install = 16, |
| 127 | dpkg_opt_package_name = 32, |
| 128 | dpkg_opt_filename = 64, |
| 129 | dpkg_opt_list_installed = 128, |
| 130 | dpkg_opt_force_ignore_depends = 256 |
| 131 | }; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 132 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 133 | typedef struct deb_file_s { |
| 134 | char *control_file; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 135 | char *filename; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 136 | unsigned int package:14; |
| 137 | } deb_file_t; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 138 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 139 | |
| 140 | void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime) |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 141 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 142 | unsigned long int hash_num = key[0]; |
| 143 | int len = strlen(key); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 144 | int i; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 145 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 146 | /* Maybe i should have uses a "proper" hashing algorithm here instead |
| 147 | * of making one up myself, seems to be working ok though. */ |
| 148 | for(i = 1; i < len; i++) { |
| 149 | /* shifts the ascii based value and adds it to previous value |
| 150 | * shift amount is mod 24 because long int is 32 bit and data |
| 151 | * to be shifted is 8, dont want to shift data to where it has |
| 152 | * no effect*/ |
| 153 | hash_num += ((key[i] + key[i-1]) << ((key[i] * i) % 24)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 155 | *start = (unsigned int) hash_num % hash_prime; |
| 156 | *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1)); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 157 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 158 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 159 | /* this adds the key to the hash table */ |
| 160 | int search_name_hashtable(const char *key) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 161 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 162 | unsigned int probe_address = 0; |
| 163 | unsigned int probe_decrement = 0; |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 164 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 165 | make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME); |
| 166 | while(name_hashtable[probe_address] != NULL) { |
| 167 | if (strcmp(name_hashtable[probe_address], key) == 0) { |
| 168 | return(probe_address); |
| 169 | } else { |
| 170 | probe_address -= probe_decrement; |
| 171 | if ((int)probe_address < 0) { |
| 172 | probe_address += NAME_HASH_PRIME; |
| 173 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 174 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 175 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 176 | name_hashtable[probe_address] = xstrdup(key); |
| 177 | |
| 178 | return(probe_address); |
| 179 | } |
| 180 | |
| 181 | /* this DOESNT add the key to the hashtable |
| 182 | * TODO make it consistent with search_name_hashtable |
| 183 | */ |
| 184 | unsigned int search_status_hashtable(const char *key) |
| 185 | { |
| 186 | unsigned int probe_address = 0; |
| 187 | unsigned int probe_decrement = 0; |
| 188 | |
| 189 | make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME); |
| 190 | while(status_hashtable[probe_address] != NULL) { |
| 191 | if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) { |
| 192 | break; |
| 193 | } else { |
| 194 | probe_address -= probe_decrement; |
| 195 | if ((int)probe_address < 0) { |
| 196 | probe_address += STATUS_HASH_PRIME; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | return(probe_address); |
| 201 | } |
| 202 | |
| 203 | /* Need to rethink version comparison, maybe the official dpkg has something i can use ? */ |
| 204 | int version_compare_part(const char *version1, const char *version2) |
| 205 | { |
| 206 | int upstream_len1 = 0; |
| 207 | int upstream_len2 = 0; |
| 208 | char *name1_char; |
| 209 | char *name2_char; |
| 210 | int len1 = 0; |
| 211 | int len2 = 0; |
| 212 | int tmp_int; |
| 213 | int ver_num1; |
| 214 | int ver_num2; |
| 215 | |
| 216 | if (version1 == NULL) { |
| 217 | version1 = xstrdup(""); |
| 218 | } |
| 219 | if (version2 != NULL) { |
| 220 | version2 = xstrdup(""); |
| 221 | } |
| 222 | upstream_len1 = strlen(version1); |
| 223 | upstream_len2 = strlen(version2); |
| 224 | |
| 225 | while ((len1 < upstream_len1) || (len2 < upstream_len2)) { |
| 226 | /* Compare non-digit section */ |
| 227 | tmp_int = strcspn(&version1[len1], "0123456789"); |
| 228 | name1_char = xstrndup(&version1[len1], tmp_int); |
| 229 | len1 += tmp_int; |
| 230 | tmp_int = strcspn(&version2[len2], "0123456789"); |
| 231 | name2_char = xstrndup(&version2[len2], tmp_int); |
| 232 | len2 += tmp_int; |
| 233 | tmp_int = strcmp(name1_char, name2_char); |
| 234 | free(name1_char); |
| 235 | free(name2_char); |
| 236 | if (tmp_int != 0) { |
| 237 | return(tmp_int); |
| 238 | } |
| 239 | |
| 240 | /* Compare digits */ |
| 241 | tmp_int = strspn(&version1[len1], "0123456789"); |
| 242 | name1_char = xstrndup(&version1[len1], tmp_int); |
| 243 | len1 += tmp_int; |
| 244 | tmp_int = strspn(&version2[len2], "0123456789"); |
| 245 | name2_char = xstrndup(&version2[len2], tmp_int); |
| 246 | len2 += tmp_int; |
| 247 | ver_num1 = atoi(name1_char); |
| 248 | ver_num2 = atoi(name2_char); |
| 249 | free(name1_char); |
| 250 | free(name2_char); |
| 251 | if (ver_num1 < ver_num2) { |
| 252 | return(-1); |
| 253 | } |
| 254 | else if (ver_num1 > ver_num2) { |
| 255 | return(1); |
| 256 | } |
| 257 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 258 | return(0); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 261 | /* if ver1 < ver2 return -1, |
| 262 | * if ver1 = ver2 return 0, |
| 263 | * if ver1 > ver2 return 1, |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 264 | */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 265 | int version_compare(const unsigned int ver1, const unsigned int ver2) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 266 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 267 | char *ch_ver1 = name_hashtable[ver1]; |
| 268 | char *ch_ver2 = name_hashtable[ver2]; |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 269 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 270 | char epoch1, epoch2; |
| 271 | char *deb_ver1, *deb_ver2; |
| 272 | char *ver1_ptr, *ver2_ptr; |
| 273 | char *upstream_ver1; |
| 274 | char *upstream_ver2; |
| 275 | int result; |
| 276 | |
| 277 | /* Compare epoch */ |
| 278 | if (ch_ver1[1] == ':') { |
| 279 | epoch1 = ch_ver1[0]; |
| 280 | ver1_ptr = strchr(ch_ver1, ':') + 1; |
| 281 | } else { |
| 282 | epoch1 = '0'; |
| 283 | ver1_ptr = ch_ver1; |
| 284 | } |
| 285 | if (ch_ver2[1] == ':') { |
| 286 | epoch2 = ch_ver2[0]; |
| 287 | ver2_ptr = strchr(ch_ver2, ':') + 1; |
| 288 | } else { |
| 289 | epoch2 = '0'; |
| 290 | ver2_ptr = ch_ver2; |
| 291 | } |
| 292 | if (epoch1 < epoch2) { |
| 293 | return(-1); |
| 294 | } |
| 295 | else if (epoch1 > epoch2) { |
| 296 | return(1); |
| 297 | } |
| 298 | |
| 299 | /* Compare upstream version */ |
| 300 | upstream_ver1 = xstrdup(ver1_ptr); |
| 301 | upstream_ver2 = xstrdup(ver2_ptr); |
| 302 | |
| 303 | /* Chop off debian version, and store for later use */ |
| 304 | deb_ver1 = strrchr(upstream_ver1, '-'); |
| 305 | deb_ver2 = strrchr(upstream_ver2, '-'); |
| 306 | if (deb_ver1) { |
| 307 | deb_ver1[0] = '\0'; |
| 308 | deb_ver1++; |
| 309 | } |
| 310 | if (deb_ver2) { |
| 311 | deb_ver2[0] = '\0'; |
| 312 | deb_ver2++; |
| 313 | } |
| 314 | result = version_compare_part(upstream_ver1, upstream_ver2); |
| 315 | |
| 316 | free(upstream_ver1); |
| 317 | free(upstream_ver2); |
| 318 | |
| 319 | if (result != 0) { |
| 320 | return(result); |
| 321 | } |
| 322 | |
| 323 | /* Compare debian versions */ |
| 324 | return(version_compare_part(deb_ver1, deb_ver2)); |
| 325 | } |
| 326 | |
| 327 | int test_version(const unsigned int version1, const unsigned int version2, const unsigned int operator) |
| 328 | { |
| 329 | const int version_result = version_compare(version1, version2); |
| 330 | switch(operator) { |
| 331 | case (VER_ANY): |
| 332 | return(TRUE); |
| 333 | case (VER_EQUAL): |
| 334 | if (version_result == 0) { |
| 335 | return(TRUE); |
| 336 | } |
| 337 | break; |
| 338 | case (VER_LESS): |
| 339 | if (version_result < 0) { |
| 340 | return(TRUE); |
| 341 | } |
| 342 | break; |
| 343 | case (VER_LESS_EQUAL): |
| 344 | if (version_result <= 0) { |
| 345 | return(TRUE); |
| 346 | } |
| 347 | break; |
| 348 | case (VER_MORE): |
| 349 | if (version_result > 0) { |
| 350 | return(TRUE); |
| 351 | } |
| 352 | break; |
| 353 | case (VER_MORE_EQUAL): |
| 354 | if (version_result >= 0) { |
| 355 | return(TRUE); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | return(FALSE); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator) |
| 364 | { |
| 365 | unsigned int probe_address = 0; |
| 366 | unsigned int probe_decrement = 0; |
| 367 | |
| 368 | make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME); |
| 369 | while(package_hashtable[probe_address] != NULL) { |
| 370 | if (package_hashtable[probe_address]->name == name) { |
| 371 | if (operator == VER_ANY) { |
| 372 | return(probe_address); |
| 373 | } |
| 374 | if (test_version(package_hashtable[probe_address]->version, version, operator)) { |
| 375 | return(probe_address); |
| 376 | } |
| 377 | } |
| 378 | probe_address -= probe_decrement; |
| 379 | if ((int)probe_address < 0) { |
| 380 | probe_address += PACKAGE_HASH_PRIME; |
| 381 | } |
| 382 | } |
| 383 | return(probe_address); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Create one new node and one new edge for every dependency. |
| 388 | */ |
| 389 | void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) |
| 390 | { |
| 391 | char *line = xstrdup(whole_line); |
| 392 | char *line2; |
| 393 | char *line_ptr1 = NULL; |
| 394 | char *line_ptr2 = NULL; |
| 395 | char *field; |
| 396 | char *field2; |
| 397 | char *version; |
| 398 | edge_t *edge; |
| 399 | int offset_ch; |
| 400 | int type; |
| 401 | |
| 402 | field = strtok_r(line, ",", &line_ptr1); |
| 403 | do { |
| 404 | line2 = xstrdup(field); |
| 405 | field2 = strtok_r(line2, "|", &line_ptr2); |
| 406 | if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) { |
| 407 | type = EDGE_OR_DEPENDS; |
| 408 | } |
| 409 | else if ((edge_type == EDGE_PRE_DEPENDS) && (strcmp(field, field2) != 0)) { |
| 410 | type = EDGE_OR_PRE_DEPENDS; |
| 411 | } else { |
| 412 | type = edge_type; |
| 413 | } |
| 414 | |
| 415 | do { |
| 416 | edge = (edge_t *) xmalloc(sizeof(edge_t)); |
| 417 | edge->type = type; |
| 418 | |
| 419 | /* Skip any extra leading spaces */ |
| 420 | field2 += strspn(field2, " "); |
| 421 | |
| 422 | /* Get dependency version info */ |
| 423 | version = strchr(field2, '('); |
| 424 | if (version == NULL) { |
| 425 | edge->operator = VER_ANY; |
| 426 | /* Get the versions hash number, adding it if the number isnt already in there */ |
| 427 | edge->version = search_name_hashtable("ANY"); |
| 428 | } else { |
| 429 | /* Skip leading ' ' or '(' */ |
| 430 | version += strspn(field2, " "); |
| 431 | version += strspn(version, "("); |
| 432 | /* Calculate length of any operator charactors */ |
| 433 | offset_ch = strspn(version, "<=>"); |
| 434 | /* Determine operator */ |
| 435 | if (offset_ch > 0) { |
| 436 | if (strncmp(version, "=", offset_ch) == 0) { |
| 437 | edge->operator = VER_EQUAL; |
| 438 | } |
| 439 | else if (strncmp(version, "<<", offset_ch) == 0) { |
| 440 | edge->operator = VER_LESS; |
| 441 | } |
| 442 | else if (strncmp(version, "<=", offset_ch) == 0) { |
| 443 | edge->operator = VER_LESS_EQUAL; |
| 444 | } |
| 445 | else if (strncmp(version, ">>", offset_ch) == 0) { |
| 446 | edge->operator = VER_MORE; |
| 447 | } |
| 448 | else if (strncmp(version, ">=", offset_ch) == 0) { |
| 449 | edge->operator = VER_MORE_EQUAL; |
| 450 | } else { |
| 451 | error_msg_and_die("Illegal operator\n"); |
| 452 | } |
| 453 | } |
| 454 | /* skip to start of version numbers */ |
| 455 | version += offset_ch; |
| 456 | version += strspn(version, " "); |
| 457 | |
| 458 | /* Truncate version at trailing ' ' or ')' */ |
| 459 | version[strcspn(version, " )")] = '\0'; |
| 460 | /* Get the versions hash number, adding it if the number isnt already in there */ |
| 461 | edge->version = search_name_hashtable(version); |
| 462 | } |
| 463 | |
| 464 | /* Get the dependency name */ |
| 465 | field2[strcspn(field2, " (")] = '\0'; |
| 466 | edge->name = search_name_hashtable(field2); |
| 467 | |
| 468 | /* link the new edge to the current node */ |
| 469 | parent_node->num_of_edges++; |
| 470 | parent_node->edge = xrealloc(parent_node->edge, sizeof(edge_t) * (parent_node->num_of_edges + 1)); |
| 471 | parent_node->edge[parent_node->num_of_edges - 1] = edge; |
| 472 | } while ((field2 = strtok_r(NULL, "|", &line_ptr2)) != NULL); |
| 473 | free(line2); |
| 474 | } while ((field = strtok_r(NULL, ",", &line_ptr1)) != NULL); |
| 475 | free(line); |
| 476 | |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | void free_package(common_node_t *node) |
| 481 | { |
| 482 | int i; |
| 483 | if (node != NULL) { |
| 484 | for (i = 0; i < node->num_of_edges; i++) { |
| 485 | if (node->edge[i] != NULL) { |
| 486 | free(node->edge[i]); |
| 487 | } |
| 488 | } |
| 489 | if (node->edge != NULL) { |
| 490 | free(node->edge); |
| 491 | } |
| 492 | if (node != NULL) { |
| 493 | free(node); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | unsigned int fill_package_struct(char *control_buffer) |
| 499 | { |
| 500 | common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t)); |
| 501 | |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 502 | char *field_name = xmalloc(sizeof(char *)); |
| 503 | char *field_value = xmalloc(sizeof(char *)); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 504 | int field_start = 0; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 505 | int num = -1; |
| 506 | int buffer_length = strlen(control_buffer); |
| 507 | |
| 508 | new_node->version = search_name_hashtable("unknown"); |
| 509 | while (field_start < buffer_length) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 510 | field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 511 | |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 512 | if (field_name == NULL) { |
Glenn L McGrath | 58a5bd1 | 2001-07-14 06:25:54 +0000 | [diff] [blame] | 513 | goto fill_package_struct_cleanup; // Oh no, the dreaded goto statement !! |
| 514 | } |
| 515 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 516 | if (strcmp(field_name, "Package") == 0) { |
| 517 | new_node->name = search_name_hashtable(field_value); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 518 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 519 | else if (strcmp(field_name, "Version") == 0) { |
| 520 | new_node->version = search_name_hashtable(field_value); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 521 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 522 | else if (strcmp(field_name, "Pre-Depends") == 0) { |
| 523 | add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 524 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 525 | else if (strcmp(field_name, "Depends") == 0) { |
| 526 | add_split_dependencies(new_node, field_value, EDGE_DEPENDS); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 527 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 528 | else if (strcmp(field_name, "Replaces") == 0) { |
| 529 | add_split_dependencies(new_node, field_value, EDGE_REPLACES); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 530 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 531 | else if (strcmp(field_name, "Provides") == 0) { |
| 532 | add_split_dependencies(new_node, field_value, EDGE_PROVIDES); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 533 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 534 | else if (strcmp(field_name, "Conflicts") == 0) { |
| 535 | add_split_dependencies(new_node, field_value, EDGE_CONFLICTS); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 536 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 537 | else if (strcmp(field_name, "Suggests") == 0) { |
| 538 | add_split_dependencies(new_node, field_value, EDGE_SUGGESTS); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 539 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 540 | else if (strcmp(field_name, "Recommends") == 0) { |
| 541 | add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 542 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 543 | else if (strcmp(field_name, "Enhances") == 0) { |
| 544 | add_split_dependencies(new_node, field_value, EDGE_ENHANCES); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 545 | } |
Glenn L McGrath | 58a5bd1 | 2001-07-14 06:25:54 +0000 | [diff] [blame] | 546 | fill_package_struct_cleanup: |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 547 | free(field_name); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 548 | free(field_value); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 549 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 550 | if (new_node->version == search_name_hashtable("unknown")) { |
| 551 | free_package(new_node); |
| 552 | return(-1); |
| 553 | } |
| 554 | num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL); |
| 555 | if (package_hashtable[num] == NULL) { |
| 556 | package_hashtable[num] = new_node; |
| 557 | } else { |
| 558 | free_package(new_node); |
| 559 | } |
| 560 | return(num); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 563 | /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */ |
| 564 | unsigned int get_status(const unsigned int status_node, const int num) |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 565 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 566 | char *status_string = name_hashtable[status_hashtable[status_node]->status]; |
| 567 | char *state_sub_string; |
| 568 | unsigned int state_sub_num; |
| 569 | int len; |
| 570 | int i; |
| 571 | |
| 572 | /* set tmp_string to point to the start of the word number */ |
| 573 | for (i = 1; i < num; i++) { |
| 574 | /* skip past a word */ |
| 575 | status_string += strcspn(status_string, " "); |
| 576 | /* skip past the seperating spaces */ |
| 577 | status_string += strspn(status_string, " "); |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 578 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 579 | len = strcspn(status_string, " \n\0"); |
| 580 | state_sub_string = xstrndup(status_string, len); |
| 581 | state_sub_num = search_name_hashtable(state_sub_string); |
| 582 | free(state_sub_string); |
| 583 | return(state_sub_num); |
Glenn L McGrath | ae1c704 | 2001-04-16 10:26:46 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 586 | void set_status(const unsigned int status_node_num, const char *new_value, const int position) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 587 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 588 | const unsigned int new_value_len = strlen(new_value); |
| 589 | const unsigned int new_value_num = search_name_hashtable(new_value); |
| 590 | unsigned int want = get_status(status_node_num, 1); |
| 591 | unsigned int flag = get_status(status_node_num, 2); |
| 592 | unsigned int status = get_status(status_node_num, 3); |
| 593 | int want_len = strlen(name_hashtable[want]); |
| 594 | int flag_len = strlen(name_hashtable[flag]); |
| 595 | int status_len = strlen(name_hashtable[status]); |
| 596 | char *new_status; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 597 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 598 | switch (position) { |
| 599 | case (1): |
| 600 | want = new_value_num; |
| 601 | want_len = new_value_len; |
| 602 | break; |
| 603 | case (2): |
| 604 | flag = new_value_num; |
| 605 | flag_len = new_value_len; |
| 606 | break; |
| 607 | case (3): |
| 608 | status = new_value_num; |
| 609 | status_len = new_value_len; |
| 610 | break; |
| 611 | default: |
| 612 | error_msg_and_die("DEBUG ONLY: this shouldnt happen"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 613 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 614 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 615 | new_status = (char *) xmalloc(want_len + flag_len + status_len + 3); |
| 616 | sprintf(new_status, "%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]); |
| 617 | status_hashtable[status_node_num]->status = search_name_hashtable(new_status); |
| 618 | return; |
| 619 | } |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 620 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 621 | void index_status_file(const char *filename) |
| 622 | { |
| 623 | FILE *status_file; |
| 624 | char *control_buffer; |
| 625 | char *status_line; |
| 626 | status_node_t *status_node = NULL; |
| 627 | unsigned int status_num; |
| 628 | |
Glenn L McGrath | 4cdc607 | 2001-07-18 03:13:49 +0000 | [diff] [blame] | 629 | status_file = xfopen(filename, "r"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 630 | while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { |
| 631 | const unsigned int package_num = fill_package_struct(control_buffer); |
| 632 | if (package_num != -1) { |
| 633 | status_node = xmalloc(sizeof(status_node_t)); |
| 634 | /* fill_package_struct doesnt handle the status field */ |
| 635 | status_line = strstr(control_buffer, "Status:"); |
| 636 | if (status_line != NULL) { |
| 637 | status_line += 7; |
| 638 | status_line += strspn(status_line, " \n\t"); |
| 639 | status_line = xstrndup(status_line, strcspn(status_line, "\n\0")); |
| 640 | status_node->status = search_name_hashtable(status_line); |
| 641 | free(status_line); |
| 642 | } |
| 643 | status_node->package = package_num; |
| 644 | status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]); |
| 645 | status_hashtable[status_num] = status_node; |
| 646 | } |
| 647 | free(control_buffer); |
| 648 | } |
| 649 | fclose(status_file); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | char *get_depends_field(common_node_t *package, const int depends_type) |
| 655 | { |
| 656 | char *depends = NULL; |
| 657 | char *old_sep = (char *)xcalloc(1, 3); |
| 658 | char *new_sep = (char *)xcalloc(1, 3); |
| 659 | int line_size = 0; |
| 660 | int depends_size; |
| 661 | |
| 662 | int i; |
| 663 | |
| 664 | for (i = 0; i < package->num_of_edges; i++) { |
| 665 | if ((package->edge[i]->type == EDGE_OR_PRE_DEPENDS) || |
| 666 | (package->edge[i]->type == EDGE_OR_DEPENDS)) { |
| 667 | } |
| 668 | |
| 669 | if ((package->edge[i]->type == depends_type) || |
| 670 | (package->edge[i]->type == depends_type + 1)) { |
| 671 | /* Check if its the first time through */ |
| 672 | |
| 673 | depends_size = 8 + strlen(name_hashtable[package->edge[i]->name]) |
| 674 | + strlen(name_hashtable[package->edge[i]->version]); |
| 675 | line_size += depends_size; |
| 676 | depends = (char *) xrealloc(depends, line_size + 1); |
| 677 | |
| 678 | /* Check to see if this dependency is the type we are looking for |
| 679 | * +1 to check for 'extra' types, e.g. ored dependecies */ |
| 680 | strcpy(old_sep, new_sep); |
| 681 | if (package->edge[i]->type == depends_type) { |
| 682 | strcpy(new_sep, ", "); |
| 683 | } |
| 684 | else if (package->edge[i]->type == depends_type + 1) { |
| 685 | strcpy(new_sep, "| "); |
| 686 | } |
| 687 | |
| 688 | if (depends_size == line_size) { |
| 689 | strcpy(depends, ""); |
| 690 | } else { |
| 691 | if ((strcmp(old_sep, "| ") == 0) && (strcmp(new_sep, "| ") == 0)) { |
| 692 | strcat(depends, " | "); |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 693 | } else { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 694 | strcat(depends, ", "); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 697 | |
| 698 | strcat(depends, name_hashtable[package->edge[i]->name]); |
| 699 | if (strcmp(name_hashtable[package->edge[i]->version], "NULL") != 0) { |
| 700 | if (package->edge[i]->operator == VER_EQUAL) { |
| 701 | strcat(depends, " (= "); |
| 702 | } |
| 703 | else if (package->edge[i]->operator == VER_LESS) { |
| 704 | strcat(depends, " (<< "); |
| 705 | } |
| 706 | else if (package->edge[i]->operator == VER_LESS_EQUAL) { |
| 707 | strcat(depends, " (<= "); |
| 708 | } |
| 709 | else if (package->edge[i]->operator == VER_MORE) { |
| 710 | strcat(depends, " (>> "); |
| 711 | } |
| 712 | else if (package->edge[i]->operator == VER_MORE_EQUAL) { |
| 713 | strcat(depends, " (>= "); |
| 714 | } else { |
| 715 | strcat(depends, " ("); |
| 716 | } |
| 717 | strcat(depends, name_hashtable[package->edge[i]->version]); |
| 718 | strcat(depends, ")"); |
| 719 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 722 | return(depends); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 725 | void write_buffer_no_status(FILE *new_status_file, const char *control_buffer) |
| 726 | { |
| 727 | char *name; |
| 728 | char *value; |
| 729 | int start = 0; |
| 730 | while (1) { |
| 731 | start += read_package_field(&control_buffer[start], &name, &value); |
| 732 | if (name == NULL) { |
| 733 | break; |
| 734 | } |
| 735 | if (strcmp(name, "Status") != 0) { |
| 736 | fprintf(new_status_file, "%s: %s\n", name, value); |
| 737 | } |
| 738 | } |
| 739 | return; |
| 740 | } |
| 741 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 742 | /* This could do with a cleanup */ |
| 743 | void write_status_file(deb_file_t **deb_file) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 744 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 745 | FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r"); |
| 746 | FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w"); |
| 747 | char *package_name; |
| 748 | char *status_from_file; |
| 749 | char *control_buffer = NULL; |
| 750 | char *tmp_string; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 751 | int status_num; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 752 | int field_start = 0; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 753 | int write_flag; |
| 754 | int i = 0; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 755 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 756 | /* Update previously known packages */ |
| 757 | while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) { |
| 758 | tmp_string = strstr(control_buffer, "Package:") + 8; |
| 759 | tmp_string += strspn(tmp_string, " \n\t"); |
| 760 | package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0")); |
| 761 | write_flag = FALSE; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 762 | tmp_string = strstr(control_buffer, "Status:"); |
| 763 | if (tmp_string != NULL) { |
| 764 | /* Seperate the status value from the control buffer */ |
| 765 | tmp_string += 7; |
| 766 | tmp_string += strspn(tmp_string, " \n\t"); |
| 767 | status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n")); |
| 768 | } else { |
| 769 | status_from_file = NULL; |
| 770 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 771 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 772 | /* Find this package in the status hashtable */ |
| 773 | status_num = search_status_hashtable(package_name); |
| 774 | if (status_hashtable[status_num] != NULL) { |
| 775 | const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status]; |
| 776 | if (strcmp(status_from_file, status_from_hashtable) != 0) { |
| 777 | /* New status isnt exactly the same as old status */ |
| 778 | const int state_status = get_status(status_num, 3); |
| 779 | if ((strcmp("installed", name_hashtable[state_status]) == 0) || |
| 780 | (strcmp("unpacked", name_hashtable[state_status]) == 0)) { |
| 781 | /* We need to add the control file from the package */ |
| 782 | i = 0; |
| 783 | while(deb_file[i] != NULL) { |
| 784 | if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 785 | /* Write a status file entry with a modified status */ |
| 786 | /* remove trailing \n's */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 787 | write_buffer_no_status(new_status_file, deb_file[i]->control_file); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 788 | set_status(status_num, "ok", 2); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 789 | fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 790 | write_flag = TRUE; |
| 791 | break; |
| 792 | } |
| 793 | i++; |
| 794 | } |
| 795 | /* This is temperary, debugging only */ |
| 796 | if (deb_file[i] == NULL) { |
| 797 | error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name); |
| 798 | } |
| 799 | } |
| 800 | else if (strcmp("not-installed", name_hashtable[state_status]) == 0) { |
| 801 | /* Only write the Package, Status, Priority and Section lines */ |
| 802 | fprintf(new_status_file, "Package: %s\n", package_name); |
| 803 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 804 | |
| 805 | while (1) { |
| 806 | char *field_name; |
| 807 | char *field_value; |
| 808 | field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value); |
| 809 | if (field_name == NULL) { |
| 810 | break; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 811 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 812 | if ((strcmp(field_name, "Priority") == 0) || |
| 813 | (strcmp(field_name, "Section") == 0)) { |
| 814 | fprintf(new_status_file, "%s: %s\n", field_name, field_value); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | write_flag = TRUE; |
| 818 | fputs("\n", new_status_file); |
| 819 | } |
| 820 | else if (strcmp("config-files", name_hashtable[state_status]) == 0) { |
| 821 | /* only change the status line */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 822 | // buffer_length = strlen(control_buffer); |
| 823 | while (1) { |
| 824 | char *field_name; |
| 825 | char *field_value; |
| 826 | field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value); |
| 827 | if (field_name == NULL) { |
| 828 | break; |
| 829 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 830 | /* Setup start point for next field */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 831 | if (strcmp(field_name, "Status") == 0) { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 832 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); |
| 833 | } else { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 834 | fprintf(new_status_file, "%s: %s\n", field_name, field_value); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 835 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 836 | } |
| 837 | write_flag = TRUE; |
| 838 | fputs("\n", new_status_file); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 839 | } |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 840 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 841 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 842 | /* If the package from the status file wasnt handle above, do it now*/ |
| 843 | if (write_flag == FALSE) { |
| 844 | fprintf(new_status_file, "%s\n\n", control_buffer); |
| 845 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 846 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 847 | if (status_from_file != NULL) { |
| 848 | free(status_from_file); |
| 849 | } |
| 850 | free(package_name); |
| 851 | free(control_buffer); |
| 852 | } |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 853 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 854 | /* Write any new packages */ |
| 855 | for(i = 0; deb_file[i] != NULL; i++) { |
| 856 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[i]->package]->name]); |
| 857 | if (strcmp("reinstreq", name_hashtable[get_status(status_num, 2)]) == 0) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 858 | write_buffer_no_status(new_status_file, deb_file[i]->control_file); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 859 | set_status(status_num, "ok", 2); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 860 | fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | fclose(old_status_file); |
| 864 | fclose(new_status_file); |
| 865 | |
| 866 | |
| 867 | /* Create a seperate backfile to dpkg */ |
| 868 | if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) { |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 869 | struct stat stat_buf; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 870 | if (stat("/var/lib/dpkg/status", &stat_buf) == 0) { |
| 871 | error_msg_and_die("Couldnt create backup status file"); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 872 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 873 | /* Its ok if renaming the status file fails becasue status |
| 874 | * file doesnt exist, maybe we are starting from scratch */ |
Glenn L McGrath | 305fdfa | 2001-04-08 13:27:39 +0000 | [diff] [blame] | 875 | error_msg("No status file found, creating new one"); |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 876 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 877 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 878 | if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) { |
| 879 | error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file"); |
Glenn L McGrath | 13e9c7a | 2001-04-08 07:18:08 +0000 | [diff] [blame] | 880 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 883 | int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 884 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 885 | int *conflicts = NULL; |
| 886 | int conflicts_num = 0; |
| 887 | int state_status; |
| 888 | int state_flag; |
| 889 | int state_want; |
| 890 | unsigned int status_package_num; |
| 891 | int i = deb_start; |
| 892 | int j, k; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 893 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 894 | /* Check for conflicts |
| 895 | * TODO: TEST if conflicts with other packages to be installed |
| 896 | * |
| 897 | * Add install packages and the packages they provide |
| 898 | * to the list of files to check conflicts for |
| 899 | */ |
| 900 | |
| 901 | /* Create array of package numbers to check against |
| 902 | * installed package for conflicts*/ |
| 903 | while (deb_file[i] != NULL) { |
| 904 | const unsigned int package_num = deb_file[i]->package; |
| 905 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); |
| 906 | conflicts[conflicts_num] = package_num; |
| 907 | conflicts_num++; |
| 908 | /* add provides to conflicts list */ |
| 909 | for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) { |
| 910 | if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) { |
| 911 | const int conflicts_package_num = search_package_hashtable( |
| 912 | package_hashtable[package_num]->edge[j]->name, |
| 913 | package_hashtable[package_num]->edge[j]->version, |
| 914 | package_hashtable[package_num]->edge[j]->operator); |
| 915 | if (package_hashtable[conflicts_package_num] == NULL) { |
| 916 | /* create a new package */ |
| 917 | common_node_t *new_node = (common_node_t *) xmalloc(sizeof(common_node_t)); |
| 918 | new_node->name = package_hashtable[package_num]->edge[j]->name; |
| 919 | new_node->version = package_hashtable[package_num]->edge[j]->version; |
| 920 | new_node->num_of_edges = 0; |
| 921 | new_node->edge = NULL; |
| 922 | package_hashtable[conflicts_package_num] = new_node; |
| 923 | } |
| 924 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); |
| 925 | conflicts[conflicts_num] = conflicts_package_num; |
| 926 | conflicts_num++; |
| 927 | } |
| 928 | } |
| 929 | i++; |
Glenn L McGrath | 0c9d77c | 2001-02-11 00:17:22 +0000 | [diff] [blame] | 930 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 931 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 932 | /* Check conflicts */ |
| 933 | for (i = 0; i < conflicts_num; i++) { |
| 934 | /* Check for conflicts */ |
| 935 | for (j = 0; j < STATUS_HASH_PRIME; j++) { |
| 936 | if (status_hashtable[j] == NULL) { |
| 937 | continue; |
| 938 | } |
| 939 | state_flag = get_status(j, 2); |
| 940 | state_status = get_status(j, 3); |
| 941 | if ((state_status != search_name_hashtable("installed")) |
| 942 | && (state_flag != search_name_hashtable("want-install"))) { |
| 943 | continue; |
| 944 | } |
| 945 | status_package_num = status_hashtable[j]->package; |
| 946 | for (k = 0; k < package_hashtable[status_package_num]->num_of_edges; k++) { |
| 947 | const edge_t *package_edge = package_hashtable[status_package_num]->edge[k]; |
| 948 | if (package_edge->type != EDGE_CONFLICTS) { |
| 949 | continue; |
| 950 | } |
| 951 | if (package_edge->name != package_hashtable[conflicts[i]]->name) { |
| 952 | continue; |
| 953 | } |
| 954 | /* There is a conflict against the package name |
| 955 | * check if version conflict as well */ |
| 956 | if (test_version(package_hashtable[deb_file[i]->package]->version, |
| 957 | package_edge->version, package_edge->operator)) { |
| 958 | error_msg_and_die("Package %s conflict with %s", |
| 959 | name_hashtable[package_hashtable[deb_file[i]->package]->name], |
| 960 | name_hashtable[package_hashtable[status_package_num]->name]); |
| 961 | } |
| 962 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 965 | |
| 966 | /* Check dependendcies */ |
| 967 | i = 0; |
| 968 | while (deb_file[i] != NULL) { |
| 969 | const common_node_t *package_node = package_hashtable[deb_file[i]->package]; |
| 970 | int status_num = 0; |
| 971 | |
| 972 | for (j = 0; j < package_hashtable[deb_file[i]->package]->num_of_edges; j++) { |
| 973 | const edge_t *package_edge = package_node->edge[j]; |
| 974 | const unsigned int package_num = search_package_hashtable(package_edge->name, |
| 975 | package_edge->version, package_edge->operator); |
| 976 | |
| 977 | status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]); |
| 978 | state_status = get_status(status_num, 3); |
| 979 | state_want = get_status(status_num, 1); |
| 980 | switch (package_edge->type) { |
| 981 | case(EDGE_PRE_DEPENDS): |
| 982 | case(EDGE_OR_PRE_DEPENDS): |
| 983 | /* It must be already installed */ |
| 984 | /* NOTE: This is untested, nothing apropriate in my status file */ |
| 985 | if ((package_hashtable[package_num] == NULL) || (state_status != search_name_hashtable("installed"))) { |
| 986 | error_msg_and_die("Package %s pre-depends on %s, but it is not installed", |
| 987 | name_hashtable[package_node->name], |
| 988 | name_hashtable[package_edge->name]); |
| 989 | } |
| 990 | break; |
| 991 | case(EDGE_DEPENDS): |
| 992 | case(EDGE_OR_DEPENDS): |
| 993 | /* It must be already installed, or to be installed */ |
| 994 | if ((package_hashtable[package_num] == NULL) || |
| 995 | ((state_status != search_name_hashtable("installed")) && |
| 996 | (state_want != search_name_hashtable("want_install")))) { |
| 997 | error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed", |
| 998 | name_hashtable[package_node->name], |
| 999 | name_hashtable[package_edge->name]); |
| 1000 | } |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | i++; |
| 1005 | } |
| 1006 | return(TRUE); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1009 | char **create_list(const char *filename) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1010 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1011 | FILE *list_stream; |
| 1012 | char **file_list = xmalloc(sizeof(char *)); |
| 1013 | char *line = NULL; |
| 1014 | char *last_char; |
| 1015 | int length = 0; |
| 1016 | int count = 0; |
| 1017 | |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1018 | /* dont use [xw]fopen here, handle error ourself */ |
| 1019 | list_stream = fopen(filename, "r"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1020 | if (list_stream == NULL) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1021 | *file_list = NULL; |
| 1022 | return(file_list); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1023 | } |
| 1024 | while (getline(&line, &length, list_stream) != -1) { |
| 1025 | file_list = xrealloc(file_list, sizeof(char *) * (length + 1)); |
| 1026 | last_char = last_char_is(line, '\n'); |
| 1027 | if (last_char) { |
| 1028 | *last_char = '\0'; |
| 1029 | } |
| 1030 | file_list[count] = xstrdup(line); |
| 1031 | free(line); |
| 1032 | count++; |
| 1033 | length = 0; |
| 1034 | } |
| 1035 | fclose(list_stream); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1036 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1037 | if (count == 0) { |
| 1038 | return(NULL); |
| 1039 | } else { |
| 1040 | file_list[count] = NULL; |
| 1041 | return(file_list); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /* maybe i should try and hook this into remove_file.c somehow */ |
| 1046 | int remove_file_array(char **remove_names, char **exclude_names) |
| 1047 | { |
| 1048 | struct stat path_stat; |
| 1049 | int match_flag; |
| 1050 | int remove_flag = FALSE; |
| 1051 | int i,j; |
| 1052 | |
| 1053 | if (remove_names == NULL) { |
| 1054 | return(FALSE); |
| 1055 | } |
| 1056 | for (i = 0; remove_names[i] != NULL; i++) { |
| 1057 | match_flag = FALSE; |
| 1058 | if (exclude_names != NULL) { |
| 1059 | for (j = 0; exclude_names[j] != 0; j++) { |
| 1060 | if (strcmp(remove_names[i], exclude_names[j]) == 0) { |
| 1061 | match_flag = TRUE; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | if (!match_flag) { |
| 1067 | if (lstat(remove_names[i], &path_stat) < 0) { |
| 1068 | continue; |
| 1069 | } |
| 1070 | if (S_ISDIR(path_stat.st_mode)) { |
| 1071 | if (rmdir(remove_names[i]) != -1) { |
| 1072 | remove_flag = TRUE; |
| 1073 | } |
| 1074 | } else { |
| 1075 | if (unlink(remove_names[i]) != -1) { |
| 1076 | remove_flag = TRUE; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | return(remove_flag); |
| 1082 | } |
| 1083 | |
| 1084 | int run_package_script(const char *package_name, const char *script_type) |
| 1085 | { |
| 1086 | struct stat path_stat; |
| 1087 | char *script_path; |
| 1088 | |
| 1089 | script_path = xmalloc(strlen(package_name) + strlen(script_type) + 21); |
| 1090 | sprintf(script_path, "/var/lib/dpkg/info/%s.%s", package_name, script_type); |
| 1091 | |
| 1092 | /* If the file doesnt exist is isnt a fatal */ |
| 1093 | if (lstat(script_path, &path_stat) < 0) { |
| 1094 | return(EXIT_SUCCESS); |
| 1095 | } else { |
| 1096 | return(system(script_path)); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | void all_control_list(char **remove_files, const char *package_name) |
| 1101 | { |
| 1102 | const char *all_extensions[11] = {"preinst", "postinst", "prerm", "postrm", |
| 1103 | "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL }; |
| 1104 | int i; |
| 1105 | |
| 1106 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 1107 | for(i = 0; i < 10; i++) { |
| 1108 | remove_files[i] = xmalloc(strlen(package_name) + strlen(all_extensions[i]) + 21); |
| 1109 | sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, all_extensions[i]); |
| 1110 | } |
| 1111 | remove_files[10] = NULL; |
| 1112 | } |
| 1113 | |
| 1114 | void remove_package(const unsigned int package_num) |
| 1115 | { |
| 1116 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 1117 | const unsigned int status_num = search_status_hashtable(package_name); |
| 1118 | const int package_name_length = strlen(package_name); |
| 1119 | char **remove_files; |
| 1120 | char **exclude_files; |
| 1121 | char list_name[package_name_length + 25]; |
| 1122 | char conffile_name[package_name_length + 30]; |
| 1123 | int return_value; |
| 1124 | |
Glenn L McGrath | ed4492a | 2001-07-18 05:03:49 +0000 | [diff] [blame] | 1125 | printf("Removing %s ...\n", package_name); |
| 1126 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1127 | /* run prerm script */ |
| 1128 | return_value = run_package_script(package_name, "prem"); |
| 1129 | if (return_value == -1) { |
| 1130 | error_msg_and_die("script failed, prerm failure"); |
| 1131 | } |
| 1132 | |
| 1133 | /* Create a list of files to remove, and a seperate list of those to keep */ |
| 1134 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1135 | remove_files = create_list(list_name); |
| 1136 | |
| 1137 | sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name); |
| 1138 | exclude_files = create_list(conffile_name); |
| 1139 | |
| 1140 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 1141 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 1142 | |
| 1143 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 1144 | remove_files = xmalloc(11); |
| 1145 | all_control_list(remove_files, package_name); |
| 1146 | |
| 1147 | /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */ |
| 1148 | exclude_files = xmalloc(sizeof(char*) * 3); |
| 1149 | exclude_files[0] = xstrdup(conffile_name); |
| 1150 | exclude_files[1] = xmalloc(package_name_length + 27); |
| 1151 | sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name); |
| 1152 | exclude_files[2] = NULL; |
| 1153 | |
| 1154 | remove_file_array(remove_files, exclude_files); |
| 1155 | |
| 1156 | /* rename <package>.conffile to <package>.list */ |
| 1157 | rename(conffile_name, list_name); |
| 1158 | |
| 1159 | /* Change package status */ |
| 1160 | set_status(status_num, "deinstall", 1); |
| 1161 | set_status(status_num, "config-files", 3); |
| 1162 | } |
| 1163 | |
| 1164 | void purge_package(const unsigned int package_num) |
| 1165 | { |
| 1166 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 1167 | const unsigned int status_num = search_status_hashtable(package_name); |
| 1168 | char **remove_files; |
| 1169 | char **exclude_files; |
| 1170 | char list_name[strlen(package_name) + 25]; |
| 1171 | |
| 1172 | /* run prerm script */ |
| 1173 | if (run_package_script(package_name, "prerm") == -1) { |
| 1174 | error_msg_and_die("script failed, prerm failure"); |
| 1175 | } |
| 1176 | |
| 1177 | /* Create a list of files to remove */ |
| 1178 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1179 | remove_files = create_list(list_name); |
| 1180 | |
| 1181 | exclude_files = xmalloc(1); |
| 1182 | exclude_files[0] = NULL; |
| 1183 | |
| 1184 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 1185 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 1186 | |
| 1187 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 1188 | remove_files = xmalloc(11); |
| 1189 | all_control_list(remove_files, package_name); |
| 1190 | remove_file_array(remove_files, exclude_files); |
| 1191 | |
| 1192 | /* run postrm script */ |
| 1193 | if (run_package_script(package_name, "postrm") == -1) { |
| 1194 | error_msg_and_die("postrm fialure.. set status to what?"); |
| 1195 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1196 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1197 | /* Change package status */ |
| 1198 | set_status(status_num, "purge", 1); |
| 1199 | set_status(status_num, "not-installed", 3); |
| 1200 | } |
| 1201 | |
| 1202 | void unpack_package(deb_file_t *deb_file) |
| 1203 | { |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1204 | const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name]; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1205 | const unsigned int status_num = search_status_hashtable(package_name); |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1206 | const unsigned int status_package_num = status_hashtable[status_num]->status; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1207 | |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 1208 | FILE *out_stream; |
| 1209 | char *info_prefix; |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1210 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1211 | /* If existing version, remove it first */ |
| 1212 | if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) { |
| 1213 | /* Package is already installed, remove old version first */ |
| 1214 | printf("Preparing to replace %s %s (using %s) ...\n", package_name, |
| 1215 | name_hashtable[package_hashtable[status_package_num]->version], |
| 1216 | deb_file->filename); |
| 1217 | remove_package(status_package_num); |
| 1218 | } else { |
| 1219 | printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename); |
| 1220 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1221 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1222 | /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */ |
| 1223 | info_prefix = (char *) xmalloc(sizeof(package_name) + 20 + 4 + 1); |
| 1224 | sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name); |
| 1225 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 1226 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1227 | /* Extract data.tar.gz to the root directory */ |
| 1228 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_data_tar_gz | extract_all_to_fs), "/", NULL); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1229 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 1230 | /* Create the list file */ |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 1231 | strcat(info_prefix, "list"); |
Glenn L McGrath | 4cdc607 | 2001-07-18 03:13:49 +0000 | [diff] [blame] | 1232 | out_stream = xfopen(info_prefix, "w"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1233 | deb_extract(deb_file->filename, out_stream, (extract_quiet | extract_data_tar_gz | extract_list), NULL, NULL); |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame] | 1234 | fclose(out_stream); |
Glenn L McGrath | 3af1f88 | 2001-02-12 11:33:09 +0000 | [diff] [blame] | 1235 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1236 | /* change status */ |
| 1237 | set_status(status_num, "install", 1); |
| 1238 | set_status(status_num, "unpacked", 3); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1241 | void configure_package(deb_file_t *deb_file) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1242 | { |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1243 | const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name]; |
| 1244 | const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version]; |
| 1245 | const int status_num = search_status_hashtable(package_name); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1246 | int return_value; |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1247 | |
| 1248 | printf("Setting up %s (%s)\n", package_name, package_version); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1249 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1250 | /* Run the preinst prior to extracting */ |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1251 | return_value = run_package_script(package_name, "postinst"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1252 | if (return_value == -1) { |
| 1253 | /* TODO: handle failure gracefully */ |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1254 | error_msg_and_die("postrm failure.. set status to what?"); |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 1255 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1256 | /* Change status to reflect success */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1257 | set_status(status_num, "install", 1); |
| 1258 | set_status(status_num, "installed", 3); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 1261 | extern int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1262 | { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1263 | deb_file_t **deb_file = NULL; |
| 1264 | status_node_t *status_node; |
| 1265 | char opt = 0; |
| 1266 | int package_num; |
| 1267 | int dpkg_opt = 0; |
| 1268 | int deb_count = 0; |
| 1269 | int state_status; |
| 1270 | int status_num; |
| 1271 | int i; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1272 | |
Glenn L McGrath | 778041f | 2001-07-18 05:17:39 +0000 | [diff] [blame] | 1273 | while ((opt = getopt(argc, argv, "CF:ilPru")) != -1) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1274 | switch (opt) { |
Glenn L McGrath | 778041f | 2001-07-18 05:17:39 +0000 | [diff] [blame] | 1275 | case 'C': // equivalent to --configure in official dpkg |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1276 | dpkg_opt |= dpkg_opt_configure; |
| 1277 | dpkg_opt |= dpkg_opt_package_name; |
| 1278 | break; |
| 1279 | case 'F': // equivalent to --force in official dpkg |
| 1280 | if (strcmp(optarg, "depends") == 0) { |
| 1281 | dpkg_opt |= dpkg_opt_force_ignore_depends; |
| 1282 | } |
| 1283 | case 'i': |
| 1284 | dpkg_opt |= dpkg_opt_install; |
| 1285 | dpkg_opt |= dpkg_opt_filename; |
| 1286 | break; |
| 1287 | case 'l': |
| 1288 | dpkg_opt |= dpkg_opt_list_installed; |
| 1289 | case 'P': |
| 1290 | dpkg_opt |= dpkg_opt_purge; |
| 1291 | dpkg_opt |= dpkg_opt_package_name; |
| 1292 | break; |
| 1293 | case 'r': |
| 1294 | dpkg_opt |= dpkg_opt_remove; |
| 1295 | dpkg_opt |= dpkg_opt_package_name; |
| 1296 | break; |
| 1297 | case 'u': /* Equivalent to --unpack in official dpkg */ |
| 1298 | dpkg_opt |= dpkg_opt_unpack; |
| 1299 | dpkg_opt |= dpkg_opt_filename; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
| 1302 | show_usage(); |
| 1303 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1304 | } |
Glenn L McGrath | 6310646 | 2001-02-11 01:40:23 +0000 | [diff] [blame] | 1305 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1306 | if ((argc == optind) || (dpkg_opt == 0)) { |
| 1307 | show_usage(); |
| 1308 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1309 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1310 | puts("(Reading database ... xxxxx files and directories installed.)"); |
| 1311 | index_status_file("/var/lib/dpkg/status"); |
| 1312 | |
| 1313 | /* Read arguments and store relevant info in structs */ |
| 1314 | deb_file = xmalloc(sizeof(deb_file_t)); |
| 1315 | while (optind < argc) { |
| 1316 | deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t)); |
| 1317 | if (dpkg_opt & dpkg_opt_filename) { |
| 1318 | deb_file[deb_count]->filename = xstrdup(argv[optind]); |
| 1319 | deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); |
| 1320 | if (deb_file[deb_count]->control_file == NULL) { |
| 1321 | error_msg_and_die("Couldnt extract control file"); |
| 1322 | } |
| 1323 | package_num = fill_package_struct(deb_file[deb_count]->control_file); |
| 1324 | |
| 1325 | if (package_num == -1) { |
| 1326 | error_msg("Invalid control file in %s", argv[optind]); |
| 1327 | continue; |
| 1328 | } |
| 1329 | deb_file[deb_count]->package = (unsigned int) package_num; |
| 1330 | /* Add the package to the status hashtable */ |
| 1331 | if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) { |
| 1332 | status_node = (status_node_t *) xmalloc(sizeof(status_node_t)); |
| 1333 | status_node->package = deb_file[deb_count]->package; |
| 1334 | /* use reinstreq isnt changed to "ok" until the package control info |
| 1335 | * is written to the status file*/ |
| 1336 | status_node->status = search_name_hashtable("install reinstreq not-installed"); |
| 1337 | |
| 1338 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); |
| 1339 | status_hashtable[status_num] = status_node; |
| 1340 | } |
| 1341 | } |
| 1342 | else if (dpkg_opt & dpkg_opt_package_name) { |
| 1343 | deb_file[deb_count]->filename = NULL; |
| 1344 | deb_file[deb_count]->control_file = NULL; |
| 1345 | deb_file[deb_count]->package = search_package_hashtable( |
| 1346 | search_name_hashtable(argv[optind]), |
| 1347 | search_name_hashtable("ANY"), VER_ANY); |
| 1348 | if (package_hashtable[deb_file[deb_count]->package] == NULL) { |
Glenn L McGrath | ed4492a | 2001-07-18 05:03:49 +0000 | [diff] [blame] | 1349 | error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1350 | } |
| 1351 | state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3); |
| 1352 | |
| 1353 | /* check package status is "installed" */ |
| 1354 | if (dpkg_opt & dpkg_opt_remove) { |
| 1355 | if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || |
| 1356 | (strcmp(name_hashtable[state_status], "config-files") == 0)) { |
| 1357 | error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); |
| 1358 | } |
| 1359 | } |
| 1360 | else if (dpkg_opt & dpkg_opt_purge) { |
| 1361 | /* if package status is "conf-files" then its ok */ |
| 1362 | if (strcmp(name_hashtable[state_status], "not-installed") == 0) { |
| 1363 | error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | deb_count++; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1368 | optind++; |
| 1369 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1370 | deb_file[deb_count] = NULL; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1371 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1372 | /* Check that the deb file arguments are installable */ |
| 1373 | /* TODO: check dependencies before removing */ |
| 1374 | if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { |
| 1375 | if (!check_deps(deb_file, 0, deb_count)) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1376 | error_msg_and_die("Dependency check failed"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1379 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1380 | for (i = 0; i < deb_count; i++) { |
| 1381 | /* Remove or purge packages */ |
| 1382 | if (dpkg_opt & dpkg_opt_remove) { |
| 1383 | remove_package(deb_file[i]->package); |
| 1384 | } |
| 1385 | else if (dpkg_opt & dpkg_opt_purge) { |
| 1386 | purge_package(deb_file[i]->package); |
| 1387 | } |
| 1388 | else if (dpkg_opt & dpkg_opt_unpack) { |
| 1389 | unpack_package(deb_file[i]); |
| 1390 | } |
| 1391 | else if (dpkg_opt & dpkg_opt_install) { |
| 1392 | unpack_package(deb_file[i]); |
| 1393 | configure_package(deb_file[i]); |
| 1394 | } |
| 1395 | else if (dpkg_opt & dpkg_opt_configure) { |
| 1396 | configure_package(deb_file[i]); |
| 1397 | } |
| 1398 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame^] | 1399 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1400 | write_status_file(deb_file); |
| 1401 | |
| 1402 | for (i = 0; i < NAME_HASH_PRIME; i++) { |
| 1403 | if (name_hashtable[i] != NULL) { |
| 1404 | free(name_hashtable[i]); |
| 1405 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1406 | } |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1407 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1408 | for (i = 0; i < PACKAGE_HASH_PRIME; i++) { |
| 1409 | free_package(package_hashtable[i]); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1410 | } |
Glenn L McGrath | 7b02415 | 2001-07-18 04:33:31 +0000 | [diff] [blame] | 1411 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1412 | for (i = 0; i < STATUS_HASH_PRIME; i++) { |
| 1413 | if (status_hashtable[i] != NULL) { |
| 1414 | free(status_hashtable[i]); |
| 1415 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1416 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1417 | |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1418 | return(EXIT_FAILURE); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 1419 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1420 | |