diff options
| author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-13 18:35:24 +0000 |
|---|---|---|
| committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-13 18:35:24 +0000 |
| commit | ccd65c9be667d504f67e8a92bb6ab41253374ab9 (patch) | |
| tree | 022e57d140ff4e4311847e83468ab2841002013f | |
| parent | 3d46224b7866089a62a992333681c9a81030bb0b (diff) | |
| download | busybox-w32-ccd65c9be667d504f67e8a92bb6ab41253374ab9.tar.gz busybox-w32-ccd65c9be667d504f67e8a92bb6ab41253374ab9.tar.bz2 busybox-w32-ccd65c9be667d504f67e8a92bb6ab41253374ab9.zip | |
Total rewrite, uses hash tables for speed and low memory use.
| -rw-r--r-- | archival/dpkg.c | 1927 | ||||
| -rw-r--r-- | dpkg.c | 1927 |
2 files changed, 2496 insertions, 1358 deletions
diff --git a/archival/dpkg.c b/archival/dpkg.c index 020a68a90..ffe746703 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c | |||
| @@ -1,854 +1,1423 @@ | |||
| 1 | #include <ctype.h> | 1 | /* |
| 2 | #include <dirent.h> | 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 | */ | ||
| 23 | |||
| 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 | |||
| 3 | #include <getopt.h> | 40 | #include <getopt.h> |
| 4 | #include <stdio.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <stdlib.h> | 41 | #include <stdlib.h> |
| 7 | #include <search.h> | 42 | #include <string.h> |
| 8 | #include <errno.h> | ||
| 9 | #include <fcntl.h> | ||
| 10 | #include <unistd.h> | 43 | #include <unistd.h> |
| 11 | #include <utime.h> | ||
| 12 | #include <sys/types.h> | ||
| 13 | #include <sys/stat.h> | ||
| 14 | |||
| 15 | #include "busybox.h" | 44 | #include "busybox.h" |
| 16 | 45 | ||
| 17 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ | 46 | /* NOTE: If you vary HASH_PRIME sizes be aware, |
| 18 | 47 | * 1) Tweaking these will have a big effect on how much memory this program uses. | |
| 19 | /* Should we do full dependency checking? */ | 48 | * 2) For computational efficiency these hash tables should be at least 20% |
| 20 | //#define DODEPENDS 0 | 49 | * larger than the maximum number of elements stored in it. |
| 21 | 50 | * 3) All _HASH_PRIME's must be a prime number or chaos is assured, if your looking | |
| 22 | /* Should we do debugging? */ | 51 | * for a prime, try http://www.utm.edu/research/primes/lists/small/10000.txt |
| 23 | //#define DODEBUG 0 | 52 | * 4) If you go bigger than 15 bits you may get into trouble (untested) as its |
| 24 | 53 | * sometimes cast to an unsigned int, if you go to 16 bit you will overlap | |
| 25 | #ifdef DODEBUG | 54 | * int's and chaos is assured, 16381 is the max prime for 14 bit field |
| 26 | #define SYSTEM(x) do_system(x) | 55 | */ |
| 27 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) | ||
| 28 | #else | ||
| 29 | #define SYSTEM(x) system(x) | ||
| 30 | #define DPRINTF(fmt,args...) /* nothing */ | ||
| 31 | #endif | ||
| 32 | |||
| 33 | /* from dpkg-deb.c */ | ||
| 34 | |||
| 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 | |||
| 39 | static const char infodir[] = "/var/lib/dpkg/info/"; | ||
| 40 | static const char udpkg_quiet[] = "UDPKG_QUIET"; | ||
| 41 | |||
| 42 | //static const int status_want_unknown = 1; | ||
| 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; | ||
| 47 | |||
| 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; | ||
| 52 | |||
| 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; | ||
| 61 | |||
| 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", | ||
| 65 | "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 }; | ||
| 66 | |||
| 67 | static const int color_white = 0; | ||
| 68 | static const int color_grey = 1; | ||
| 69 | static const int color_black = 2; | ||
| 70 | |||
| 71 | /* data structures */ | ||
| 72 | typedef struct package_s { | ||
| 73 | char *filename; | ||
| 74 | char *package; | ||
| 75 | unsigned char state_want; | ||
| 76 | unsigned char state_flag; | ||
| 77 | unsigned char state_status; | ||
| 78 | char *depends; | ||
| 79 | char *provides; | ||
| 80 | char *description; | ||
| 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; | ||
| 96 | int installer_menu_item; | ||
| 97 | char color; /* for topo-sort */ | ||
| 98 | struct package_s *requiredfor[DEPENDSMAX]; | ||
| 99 | unsigned short requiredcount; | ||
| 100 | struct package_s *next; | ||
| 101 | } package_t; | ||
| 102 | |||
| 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 | 56 | ||
| 113 | static int package_compare(const void *p1, const void *p2) | 57 | /* NAME_HASH_PRIME, Stores package names and versions, |
| 114 | { | 58 | * I estimate it should be at least 50% bigger than PACKAGE_HASH_PRIME, |
| 115 | return strcmp(((package_t *)p1)->package, | 59 | * as there a lot of duplicate version numbers */ |
| 116 | ((package_t *)p2)->package); | 60 | #define NAME_HASH_PRIME 16381 |
| 117 | } | 61 | char *name_hashtable[NAME_HASH_PRIME + 1]; |
| 62 | |||
| 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; | ||
| 78 | |||
| 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]; | ||
| 86 | |||
| 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]; | ||
| 96 | |||
| 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 | }; | ||
| 120 | |||
| 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 | }; | ||
| 132 | |||
| 133 | typedef struct deb_file_s { | ||
| 134 | char *control_file; | ||
| 135 | char *filename; | ||
| 136 | unsigned int package:14; | ||
| 137 | } deb_file_t; | ||
| 118 | 138 | ||
| 119 | #ifdef DODEPENDS | ||
| 120 | 139 | ||
| 121 | static char **depends_split(const char *dependsstr) | 140 | void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime) |
| 122 | { | 141 | { |
| 123 | static char *dependsvec[DEPENDSMAX]; | 142 | unsigned long int hash_num = key[0]; |
| 124 | char *p; | 143 | int len = strlen(key); |
| 125 | int i = 0; | 144 | int i; |
| 126 | 145 | ||
| 127 | dependsvec[0] = 0; | 146 | /* Maybe i should have uses a "proper" hashing algorithm here instead |
| 128 | if (dependsstr == 0) { | 147 | * of making one up myself, seems to be working ok though. */ |
| 129 | goto end; | 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)); | ||
| 130 | } | 154 | } |
| 155 | *start = (unsigned int) hash_num % hash_prime; | ||
| 156 | *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1)); | ||
| 157 | } | ||
| 131 | 158 | ||
| 132 | p = xstrdup(dependsstr); | 159 | /* this adds the key to the hash table */ |
| 133 | while (*p != 0 && *p != '\n') { | 160 | int search_name_hashtable(const char *key) |
| 134 | if (*p != ' ') { | 161 | { |
| 135 | if (*p == ',') { | 162 | unsigned int probe_address = 0; |
| 136 | *p = 0; | 163 | unsigned int probe_decrement = 0; |
| 137 | dependsvec[++i] = 0; | 164 | |
| 138 | } else { | 165 | make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME); |
| 139 | if (dependsvec[i] == 0) { | 166 | while(name_hashtable[probe_address] != NULL) { |
| 140 | dependsvec[i] = p; | 167 | if (strcmp(name_hashtable[probe_address], key) == 0) { |
| 141 | } | 168 | return(probe_address); |
| 142 | } | ||
| 143 | } else { | 169 | } else { |
| 144 | *p = 0; /* eat the space... */ | 170 | probe_address -= probe_decrement; |
| 171 | if ((int)probe_address < 0) { | ||
| 172 | probe_address += NAME_HASH_PRIME; | ||
| 173 | } | ||
| 145 | } | 174 | } |
| 146 | p++; | ||
| 147 | } | 175 | } |
| 148 | *p = 0; | 176 | name_hashtable[probe_address] = xstrdup(key); |
| 149 | 177 | ||
| 150 | end: | 178 | return(probe_address); |
| 151 | dependsvec[i+1] = 0; | ||
| 152 | return dependsvec; | ||
| 153 | } | 179 | } |
| 154 | 180 | ||
| 155 | /* Topological sort algorithm: | 181 | /* this DOESNT add the key to the hashtable |
| 156 | * ordered is the output list, pkgs is the dependency graph, pkg is | 182 | * TODO make it consistent with search_name_hashtable |
| 157 | * the current node | ||
| 158 | * | ||
| 159 | * recursively add all the adjacent nodes to the ordered list, marking | ||
| 160 | * each one as visited along the way | ||
| 161 | * | ||
| 162 | * yes, this algorithm looks a bit odd when all the params have the | ||
| 163 | * same type :-) | ||
| 164 | */ | 183 | */ |
| 165 | static void depends_sort_visit(package_t **ordered, package_t *pkgs, | 184 | unsigned int search_status_hashtable(const char *key) |
| 166 | package_t *pkg) | ||
| 167 | { | 185 | { |
| 168 | unsigned short i; | 186 | unsigned int probe_address = 0; |
| 169 | 187 | unsigned int probe_decrement = 0; | |
| 170 | /* mark node as processing */ | 188 | |
| 171 | pkg->color = color_grey; | 189 | make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME); |
| 172 | 190 | while(status_hashtable[probe_address] != NULL) { | |
| 173 | /* visit each not-yet-visited node */ | 191 | if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) { |
| 174 | for (i = 0; i < pkg->requiredcount; i++) | 192 | break; |
| 175 | if (pkg->requiredfor[i]->color == color_white) | 193 | } else { |
| 176 | depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]); | 194 | probe_address -= probe_decrement; |
| 177 | 195 | if ((int)probe_address < 0) { | |
| 178 | #if 0 | 196 | probe_address += STATUS_HASH_PRIME; |
| 179 | /* add it to the list */ | 197 | } |
| 180 | newnode = (struct package_t *)xmalloc(sizeof(struct package_t)); | 198 | } |
| 181 | /* make a shallow copy */ | 199 | } |
| 182 | *newnode = *pkg; | 200 | return(probe_address); |
| 183 | newnode->next = *ordered; | ||
| 184 | *ordered = newnode; | ||
| 185 | #endif | ||
| 186 | |||
| 187 | pkg->next = *ordered; | ||
| 188 | *ordered = pkg; | ||
| 189 | |||
| 190 | /* mark node as done */ | ||
| 191 | pkg->color = color_black; | ||
| 192 | } | 201 | } |
| 193 | 202 | ||
| 194 | static package_t *depends_sort(package_t *pkgs) | 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) | ||
| 195 | { | 205 | { |
| 196 | /* TODO: it needs to break cycles in the to-be-installed package | 206 | int upstream_len1 = 0; |
| 197 | * graph... */ | 207 | int upstream_len2 = 0; |
| 198 | package_t *ordered = NULL; | 208 | char *name1_char; |
| 199 | package_t *pkg; | 209 | char *name2_char; |
| 200 | 210 | int len1 = 0; | |
| 201 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 211 | int len2 = 0; |
| 202 | pkg->color = color_white; | 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(""); | ||
| 203 | } | 221 | } |
| 204 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 222 | upstream_len1 = strlen(version1); |
| 205 | if (pkg->color == color_white) { | 223 | upstream_len2 = strlen(version2); |
| 206 | depends_sort_visit(&ordered, pkgs, pkg); | 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); | ||
| 207 | } | 253 | } |
| 254 | else if (ver_num1 > ver_num2) { | ||
| 255 | return(1); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | return(0); | ||
| 259 | } | ||
| 260 | |||
| 261 | /* if ver1 < ver2 return -1, | ||
| 262 | * if ver1 = ver2 return 0, | ||
| 263 | * if ver1 > ver2 return 1, | ||
| 264 | */ | ||
| 265 | int version_compare(const unsigned int ver1, const unsigned int ver2) | ||
| 266 | { | ||
| 267 | char *ch_ver1 = name_hashtable[ver1]; | ||
| 268 | char *ch_ver2 = name_hashtable[ver2]; | ||
| 269 | |||
| 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); | ||
| 208 | } | 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); | ||
| 209 | 318 | ||
| 210 | /* Leaks the old list... return the new one... */ | 319 | if (result != 0) { |
| 211 | return ordered; | 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); | ||
| 212 | } | 360 | } |
| 213 | 361 | ||
| 214 | 362 | ||
| 215 | /* resolve package dependencies -- | 363 | int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator) |
| 216 | * for each package in the list of packages to be installed, we parse its | ||
| 217 | * dependency info to determine if the dependent packages are either | ||
| 218 | * already installed, or are scheduled to be installed. If both tests fail | ||
| 219 | * than bail. | ||
| 220 | * | ||
| 221 | * The algorithm here is O(n^2*m) where n = number of packages to be | ||
| 222 | * installed and m is the # of dependencies per package. Not a terribly | ||
| 223 | * efficient algorithm, but given that at any one time you are unlikely | ||
| 224 | * to install a very large number of packages it doesn't really matter | ||
| 225 | */ | ||
| 226 | static package_t *depends_resolve(package_t *pkgs, void *status) | ||
| 227 | { | 364 | { |
| 228 | package_t *pkg, *chk; | 365 | unsigned int probe_address = 0; |
| 229 | package_t dependpkg; | 366 | unsigned int probe_decrement = 0; |
| 230 | char **dependsvec; | 367 | |
| 231 | int i; | 368 | make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME); |
| 232 | void *found; | 369 | while(package_hashtable[probe_address] != NULL) { |
| 233 | 370 | if (package_hashtable[probe_address]->name == name) { | |
| 234 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 371 | if (operator == VER_ANY) { |
| 235 | dependsvec = depends_split(pkg->depends); | 372 | return(probe_address); |
| 236 | i = 0; | 373 | } |
| 237 | while (dependsvec[i] != 0) { | 374 | if (test_version(package_hashtable[probe_address]->version, version, operator)) { |
| 238 | /* Check for dependencies; first look for installed packages */ | 375 | return(probe_address); |
| 239 | dependpkg.package = dependsvec[i]; | ||
| 240 | if (((found = tfind(&dependpkg, &status, package_compare)) == 0) || | ||
| 241 | ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) && | ||
| 242 | (chk->state_status & state_status_installed))) { | ||
| 243 | |||
| 244 | /* if it fails, we look through the list of packages we are going to | ||
| 245 | * install */ | ||
| 246 | for (chk = pkgs; chk != 0; chk = chk->next) { | ||
| 247 | if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && | ||
| 248 | strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { | ||
| 249 | if (chk->requiredcount >= DEPENDSMAX) { | ||
| 250 | error_msg("Too many dependencies for %s", chk->package); | ||
| 251 | return 0; | ||
| 252 | } | ||
| 253 | if (chk != pkg) { | ||
| 254 | chk->requiredfor[chk->requiredcount++] = pkg; | ||
| 255 | } | ||
| 256 | break; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | if (chk == 0) { | ||
| 260 | error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]); | ||
| 261 | return 0; | ||
| 262 | } | ||
| 263 | } | 376 | } |
| 264 | i++; | 377 | } |
| 378 | probe_address -= probe_decrement; | ||
| 379 | if ((int)probe_address < 0) { | ||
| 380 | probe_address += PACKAGE_HASH_PRIME; | ||
| 265 | } | 381 | } |
| 266 | } | 382 | } |
| 267 | 383 | return(probe_address); | |
| 268 | return depends_sort(pkgs); | ||
| 269 | } | 384 | } |
| 270 | #endif | 385 | |
| 271 | 386 | /* | |
| 272 | /* Status file handling routines | 387 | * Create one new node and one new edge for every dependency. |
| 273 | * | ||
| 274 | * This is a fairly minimalistic implementation. there are two main functions | ||
| 275 | * that are supported: | ||
| 276 | * | ||
| 277 | * 1) reading the entire status file: | ||
| 278 | * the status file is read into memory as a binary-tree, with just the | ||
| 279 | * package and status info preserved | ||
| 280 | * | ||
| 281 | * 2) merging the status file | ||
| 282 | * control info from (new) packages is merged into the status file, | ||
| 283 | * replacing any pre-existing entries. when a merge happens, status info | ||
| 284 | * read using the status_read function is written back to the status file | ||
| 285 | */ | 388 | */ |
| 286 | static unsigned char status_parse(const char *line, const char **status_words) | 389 | void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) |
| 287 | { | 390 | { |
| 288 | unsigned char status_num; | 391 | char *line = xstrdup(whole_line); |
| 289 | int i = 0; | 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); | ||
| 290 | 476 | ||
| 291 | while (status_words[i] != 0) { | 477 | return; |
| 292 | if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) { | 478 | } |
| 293 | status_num = (char)i; | 479 | |
| 294 | return(status_num); | 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); | ||
| 295 | } | 494 | } |
| 296 | i++; | ||
| 297 | } | 495 | } |
| 298 | /* parse error */ | ||
| 299 | error_msg("Invalid status word"); | ||
| 300 | return(0); | ||
| 301 | } | 496 | } |
| 302 | 497 | ||
| 303 | /* | 498 | unsigned int fill_package_struct(char *control_buffer) |
| 304 | * Read the buffered control file and parse it, | ||
| 305 | * filling parsed fields into the package structure | ||
| 306 | */ | ||
| 307 | static int fill_package_struct(package_t *package, const char *package_buffer) | ||
| 308 | { | 499 | { |
| 309 | char *field = NULL; | 500 | common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t)); |
| 501 | |||
| 502 | char *field; | ||
| 503 | char *field_name; | ||
| 504 | char *field_value; | ||
| 310 | int field_start = 0; | 505 | int field_start = 0; |
| 311 | int field_length = 0; | 506 | int field_length; |
| 507 | int seperator_offset; | ||
| 508 | int num = -1; | ||
| 509 | int buffer_length = strlen(control_buffer); | ||
| 510 | |||
| 511 | new_node->version = search_name_hashtable("unknown"); | ||
| 512 | while (field_start < buffer_length) { | ||
| 513 | field = read_package_field(&control_buffer[field_start]); | ||
| 312 | 514 | ||
| 313 | while ((field = read_package_field(&package_buffer[field_start])) != NULL) { | 515 | /* Setup start point for next field */ |
| 314 | field_length = strlen(field); | 516 | field_length = strlen(field); |
| 315 | field_start += (field_length + 1); | 517 | field_start += (field_length + 1); |
| 316 | 518 | ||
| 317 | if (strlen(field) == 0) { | 519 | seperator_offset = strcspn(field, ":"); |
| 318 | printf("empty line: *this shouldnt happen i dont think*\n"); | 520 | field_name = xstrndup(field, seperator_offset); |
| 319 | break; | 521 | field_value = field + seperator_offset + 1; |
| 320 | } | 522 | field_value += strspn(field_value, " \n\t"); |
| 321 | 523 | ||
| 322 | /* these are common to both installed and uninstalled packages */ | 524 | if (strcmp(field_name, "Package") == 0) { |
| 323 | if (strstr(field, "Package: ") == field) { | 525 | new_node->name = search_name_hashtable(field_value); |
| 324 | package->package = strdup(field + 9); | ||
| 325 | } | ||
| 326 | else if (strstr(field, "Depends: ") == field) { | ||
| 327 | package->depends = strdup(field + 9); | ||
| 328 | } | ||
| 329 | else if (strstr(field, "Provides: ") == field) { | ||
| 330 | package->provides = strdup(field + 10); | ||
| 331 | } | 526 | } |
| 332 | /* This is specific to the Debian Installer. Ifdef? */ | 527 | else if (strcmp(field_name, "Version") == 0) { |
| 333 | else if (strstr(field, "installer-menu-item: ") == field) { | 528 | new_node->version = search_name_hashtable(field_value); |
| 334 | package->installer_menu_item = atoi(field + 21); | ||
| 335 | } | 529 | } |
| 336 | else if (strstr(field, "Description: ") == field) { | 530 | else if (strcmp(field_name, "Pre-Depends") == 0) { |
| 337 | package->description = strdup(field + 13); | 531 | add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS); |
| 338 | } | 532 | } |
| 339 | else if (strstr(field, "Priority: ") == field) { | 533 | else if (strcmp(field_name, "Depends") == 0) { |
| 340 | package->priority = strdup(field + 10); | 534 | add_split_dependencies(new_node, field_value, EDGE_DEPENDS); |
| 341 | } | 535 | } |
| 342 | else if (strstr(field, "Section: ") == field) { | 536 | else if (strcmp(field_name, "Replaces") == 0) { |
| 343 | package->section = strdup(field + 9); | 537 | add_split_dependencies(new_node, field_value, EDGE_REPLACES); |
| 344 | } | 538 | } |
| 345 | else if (strstr(field, "Installed-Size: ") == field) { | 539 | else if (strcmp(field_name, "Provides") == 0) { |
| 346 | package->installed_size = strdup(field + 16); | 540 | add_split_dependencies(new_node, field_value, EDGE_PROVIDES); |
| 347 | } | 541 | } |
| 348 | else if (strstr(field, "Maintainer: ") == field) { | 542 | else if (strcmp(field_name, "Conflicts") == 0) { |
| 349 | package->maintainer = strdup(field + 12); | 543 | add_split_dependencies(new_node, field_value, EDGE_CONFLICTS); |
| 350 | } | 544 | } |
| 351 | else if (strstr(field, "Version: ") == field) { | 545 | else if (strcmp(field_name, "Suggests") == 0) { |
| 352 | package->version = strdup(field + 9); | 546 | add_split_dependencies(new_node, field_value, EDGE_SUGGESTS); |
| 353 | } | ||
| 354 | else if (strstr(field, "Suggests: ") == field) { | ||
| 355 | package->suggests = strdup(field + 10); | ||
| 356 | } | ||
| 357 | else if (strstr(field, "Recommends: ") == field) { | ||
| 358 | package->recommends = strdup(field + 12); | ||
| 359 | } | ||
| 360 | /* else if (strstr(field, "Conffiles: ") == field) { | ||
| 361 | package->conffiles = read_block(file); | ||
| 362 | package->conffiles = xcalloc(1, 1); | ||
| 363 | while ((field = strtok(NULL, "\n")) != NULL) { | ||
| 364 | package->long_description = xrealloc(package->conffiles, | ||
| 365 | strlen(package->conffiles) + strlen(field) + 1); | ||
| 366 | strcat(package->conffiles, field); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | */ | ||
| 370 | /* These are only in available file */ | ||
| 371 | else if (strstr(field, "Architecture: ") == field) { | ||
| 372 | package->architecture = strdup(field + 14); | ||
| 373 | } | 547 | } |
| 374 | else if (strstr(field, "Filename: ") == field) { | 548 | else if (strcmp(field_name, "Recommends") == 0) { |
| 375 | package->filename = strdup(field + 10); | 549 | add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS); |
| 376 | } | 550 | } |
| 377 | else if (strstr(field, "MD5sum ") == field) { | 551 | else if (strcmp(field_name, "Enhances") == 0) { |
| 378 | package->md5sum = strdup(field + 7); | 552 | add_split_dependencies(new_node, field_value, EDGE_ENHANCES); |
| 379 | } | 553 | } |
| 380 | 554 | free(field_name); | |
| 381 | /* This is only needed for status file */ | ||
| 382 | if (strstr(field, "Status: ") == field) { | ||
| 383 | char *word_pointer; | ||
| 384 | |||
| 385 | word_pointer = strchr(field, ' ') + 1; | ||
| 386 | package->state_want = status_parse(word_pointer, state_words_want); | ||
| 387 | word_pointer = strchr(word_pointer, ' ') + 1; | ||
| 388 | package->state_flag = status_parse(word_pointer, state_words_flag); | ||
| 389 | word_pointer = strchr(word_pointer, ' ') + 1; | ||
| 390 | package->state_status = status_parse(word_pointer, state_words_status); | ||
| 391 | } else { | ||
| 392 | package->state_want = status_parse("purge", state_words_want); | ||
| 393 | package->state_flag = status_parse("ok", state_words_flag); | ||
| 394 | package->state_status = status_parse("not-installed", state_words_status); | ||
| 395 | } | ||
| 396 | |||
| 397 | free(field); | 555 | free(field); |
| 398 | } | 556 | } |
| 399 | return EXIT_SUCCESS; | 557 | if (new_node->version == search_name_hashtable("unknown")) { |
| 558 | free_package(new_node); | ||
| 559 | return(-1); | ||
| 560 | } | ||
| 561 | num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL); | ||
| 562 | if (package_hashtable[num] == NULL) { | ||
| 563 | package_hashtable[num] = new_node; | ||
| 564 | } else { | ||
| 565 | free_package(new_node); | ||
| 566 | } | ||
| 567 | return(num); | ||
| 400 | } | 568 | } |
| 401 | 569 | ||
| 402 | extern void write_package(FILE *out_file, package_t *pkg) | 570 | /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */ |
| 571 | unsigned int get_status(const unsigned int status_node, const int num) | ||
| 403 | { | 572 | { |
| 404 | if (pkg->package) { | 573 | char *status_string = name_hashtable[status_hashtable[status_node]->status]; |
| 405 | fprintf(out_file, "Package: %s\n", pkg->package); | 574 | char *state_sub_string; |
| 406 | } | 575 | unsigned int state_sub_num; |
| 407 | if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) { | 576 | int len; |
| 408 | fprintf(out_file, "Status: %s %s %s\n", | 577 | int i; |
| 409 | state_words_want[pkg->state_want - 1], | 578 | |
| 410 | state_words_flag[pkg->state_flag - 1], | 579 | /* set tmp_string to point to the start of the word number */ |
| 411 | state_words_status[pkg->state_status - 1]); | 580 | for (i = 1; i < num; i++) { |
| 412 | } | 581 | /* skip past a word */ |
| 413 | if (pkg->depends) { | 582 | status_string += strcspn(status_string, " "); |
| 414 | fprintf(out_file, "Depends: %s\n", pkg->depends); | 583 | /* skip past the seperating spaces */ |
| 415 | } | 584 | status_string += strspn(status_string, " "); |
| 416 | if (pkg->provides) { | ||
| 417 | fprintf(out_file, "Provides: %s\n", pkg->provides); | ||
| 418 | } | ||
| 419 | if (pkg->priority) { | ||
| 420 | fprintf(out_file, "Priority: %s\n", pkg->priority); | ||
| 421 | } | ||
| 422 | if (pkg->section) { | ||
| 423 | fprintf(out_file, "Section: %s\n", pkg->section); | ||
| 424 | } | ||
| 425 | if (pkg->section) { | ||
| 426 | fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size); | ||
| 427 | } | ||
| 428 | if (pkg->maintainer) { | ||
| 429 | fprintf(out_file, "Maintainer: %s\n", pkg->maintainer); | ||
| 430 | } | ||
| 431 | if (pkg->source) { | ||
| 432 | fprintf(out_file, "Source: %s\n", pkg->source); | ||
| 433 | } | ||
| 434 | if (pkg->version) { | ||
| 435 | fprintf(out_file, "Version: %s\n", pkg->version); | ||
| 436 | } | ||
| 437 | if (pkg->pre_depends) { | ||
| 438 | fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends); | ||
| 439 | } | ||
| 440 | if (pkg->replaces) { | ||
| 441 | fprintf(out_file, "Replaces: %s\n", pkg->replaces); | ||
| 442 | } | ||
| 443 | if (pkg->recommends) { | ||
| 444 | fprintf(out_file, "Recommends: %s\n", pkg->recommends); | ||
| 445 | } | ||
| 446 | if (pkg->suggests) { | ||
| 447 | fprintf(out_file, "Suggests: %s\n", pkg->suggests); | ||
| 448 | } | ||
| 449 | if (pkg->conflicts) { | ||
| 450 | fprintf(out_file, "Conflicts: %s\n", pkg->conflicts); | ||
| 451 | } | ||
| 452 | if (pkg->conffiles) { | ||
| 453 | fprintf(out_file, "Conf-files: %s\n", pkg->conffiles); | ||
| 454 | } | ||
| 455 | if (pkg->architecture) { | ||
| 456 | fprintf(out_file, "Architecture: %s\n", pkg->architecture); | ||
| 457 | } | ||
| 458 | if (pkg->filename) { | ||
| 459 | fprintf(out_file, "Filename: %s\n", pkg->filename); | ||
| 460 | } | ||
| 461 | if (pkg->md5sum) { | ||
| 462 | fprintf(out_file, "MD5sum: %s\n", pkg->md5sum); | ||
| 463 | } | 585 | } |
| 464 | if (pkg->installer_menu_item) { | 586 | len = strcspn(status_string, " \n\0"); |
| 465 | fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item); | 587 | state_sub_string = xstrndup(status_string, len); |
| 588 | state_sub_num = search_name_hashtable(state_sub_string); | ||
| 589 | free(state_sub_string); | ||
| 590 | return(state_sub_num); | ||
| 591 | } | ||
| 592 | |||
| 593 | void set_status(const unsigned int status_node_num, const char *new_value, const int position) | ||
| 594 | { | ||
| 595 | const unsigned int new_value_len = strlen(new_value); | ||
| 596 | const unsigned int new_value_num = search_name_hashtable(new_value); | ||
| 597 | unsigned int want = get_status(status_node_num, 1); | ||
| 598 | unsigned int flag = get_status(status_node_num, 2); | ||
| 599 | unsigned int status = get_status(status_node_num, 3); | ||
| 600 | int want_len = strlen(name_hashtable[want]); | ||
| 601 | int flag_len = strlen(name_hashtable[flag]); | ||
| 602 | int status_len = strlen(name_hashtable[status]); | ||
| 603 | char *new_status; | ||
| 604 | |||
| 605 | switch (position) { | ||
| 606 | case (1): | ||
| 607 | want = new_value_num; | ||
| 608 | want_len = new_value_len; | ||
| 609 | break; | ||
| 610 | case (2): | ||
| 611 | flag = new_value_num; | ||
| 612 | flag_len = new_value_len; | ||
| 613 | break; | ||
| 614 | case (3): | ||
| 615 | status = new_value_num; | ||
| 616 | status_len = new_value_len; | ||
| 617 | break; | ||
| 618 | default: | ||
| 619 | error_msg_and_die("DEBUG ONLY: this shouldnt happen"); | ||
| 466 | } | 620 | } |
| 467 | if (pkg->description) { | 621 | |
| 468 | fprintf(out_file, "Description: %s\n", pkg->description); | 622 | new_status = (char *) xmalloc(want_len + flag_len + status_len + 3); |
| 623 | sprintf(new_status, "%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]); | ||
| 624 | status_hashtable[status_node_num]->status = search_name_hashtable(new_status); | ||
| 625 | return; | ||
| 626 | } | ||
| 627 | |||
| 628 | void index_status_file(const char *filename) | ||
| 629 | { | ||
| 630 | FILE *status_file; | ||
| 631 | char *control_buffer; | ||
| 632 | char *status_line; | ||
| 633 | status_node_t *status_node = NULL; | ||
| 634 | unsigned int status_num; | ||
| 635 | |||
| 636 | status_file = fopen(filename, "r"); | ||
| 637 | while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { | ||
| 638 | const unsigned int package_num = fill_package_struct(control_buffer); | ||
| 639 | if (package_num != -1) { | ||
| 640 | status_node = xmalloc(sizeof(status_node_t)); | ||
| 641 | /* fill_package_struct doesnt handle the status field */ | ||
| 642 | status_line = strstr(control_buffer, "Status:"); | ||
| 643 | if (status_line != NULL) { | ||
| 644 | status_line += 7; | ||
| 645 | status_line += strspn(status_line, " \n\t"); | ||
| 646 | status_line = xstrndup(status_line, strcspn(status_line, "\n\0")); | ||
| 647 | status_node->status = search_name_hashtable(status_line); | ||
| 648 | free(status_line); | ||
| 649 | } | ||
| 650 | status_node->package = package_num; | ||
| 651 | status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]); | ||
| 652 | status_hashtable[status_num] = status_node; | ||
| 653 | } | ||
| 654 | free(control_buffer); | ||
| 469 | } | 655 | } |
| 470 | fputc('\n', out_file); | 656 | fclose(status_file); |
| 471 | pkg = pkg->next; | 657 | return; |
| 472 | } | 658 | } |
| 473 | 659 | ||
| 474 | static void *status_read(void) | 660 | |
| 661 | char *get_depends_field(common_node_t *package, const int depends_type) | ||
| 475 | { | 662 | { |
| 476 | FILE *f; | 663 | char *depends = NULL; |
| 477 | void *status = 0; | 664 | char *old_sep = (char *)xcalloc(1, 3); |
| 478 | package_t *m = 0, *p = 0, *t = 0; | 665 | char *new_sep = (char *)xcalloc(1, 3); |
| 479 | char *package_control_buffer = NULL; | 666 | int line_size = 0; |
| 667 | int depends_size; | ||
| 480 | 668 | ||
| 481 | if (getenv(udpkg_quiet) == NULL) { | 669 | int i; |
| 482 | printf("(Reading database...)\n"); | ||
| 483 | } | ||
| 484 | 670 | ||
| 485 | if ((f = wfopen(statusfile, "r")) == NULL) { | 671 | for (i = 0; i < package->num_of_edges; i++) { |
| 486 | return(NULL); | 672 | if ((package->edge[i]->type == EDGE_OR_PRE_DEPENDS) || |
| 487 | } | 673 | (package->edge[i]->type == EDGE_OR_DEPENDS)) { |
| 674 | } | ||
| 488 | 675 | ||
| 489 | while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) { | 676 | if ((package->edge[i]->type == depends_type) || |
| 490 | m = (package_t *)xcalloc(1, sizeof(package_t)); | 677 | (package->edge[i]->type == depends_type + 1)) { |
| 491 | fill_package_struct(m, package_control_buffer); | 678 | /* Check if its the first time through */ |
| 492 | if (m->package) { | 679 | |
| 493 | /* | 680 | depends_size = 8 + strlen(name_hashtable[package->edge[i]->name]) |
| 494 | * If there is an item in the tree by this name, | 681 | + strlen(name_hashtable[package->edge[i]->version]); |
| 495 | * it must be a virtual package; insert real | 682 | line_size += depends_size; |
| 496 | * package in preference. | 683 | depends = (char *) xrealloc(depends, line_size + 1); |
| 497 | */ | 684 | |
| 498 | tdelete(m, &status, package_compare); | 685 | /* Check to see if this dependency is the type we are looking for |
| 499 | tsearch(m, &status, package_compare); | 686 | * +1 to check for 'extra' types, e.g. ored dependecies */ |
| 500 | if (m->provides) { | 687 | strcpy(old_sep, new_sep); |
| 501 | /* | 688 | if (package->edge[i]->type == depends_type) { |
| 502 | * A "Provides" triggers the insertion | 689 | strcpy(new_sep, ", "); |
| 503 | * of a pseudo package into the status | 690 | } |
| 504 | * binary-tree. | 691 | else if (package->edge[i]->type == depends_type + 1) { |
| 505 | */ | 692 | strcpy(new_sep, "| "); |
| 506 | p = (package_t *)xcalloc(1, sizeof(package_t)); | 693 | } |
| 507 | p->package = xstrdup(m->provides); | 694 | |
| 508 | t = *(package_t **)tsearch(p, &status, package_compare); | 695 | if (depends_size == line_size) { |
| 509 | if (t != p) { | 696 | strcpy(depends, ""); |
| 510 | free(p->package); | 697 | } else { |
| 511 | free(p); | 698 | if ((strcmp(old_sep, "| ") == 0) && (strcmp(new_sep, "| ") == 0)) { |
| 699 | strcat(depends, " | "); | ||
| 512 | } else { | 700 | } else { |
| 513 | /* | 701 | strcat(depends, ", "); |
| 514 | * Pseudo package status is the | ||
| 515 | * same as the status of the | ||
| 516 | * package providing it | ||
| 517 | * FIXME: (not quite right, if 2 | ||
| 518 | * packages of different statuses | ||
| 519 | * provide it). | ||
| 520 | */ | ||
| 521 | t->state_want = m->state_want; | ||
| 522 | t->state_flag = m->state_flag; | ||
| 523 | t->state_status = m->state_status; | ||
| 524 | } | 702 | } |
| 525 | } | 703 | } |
| 526 | } | 704 | |
| 527 | else { | 705 | strcat(depends, name_hashtable[package->edge[i]->name]); |
| 528 | free(m); | 706 | if (strcmp(name_hashtable[package->edge[i]->version], "NULL") != 0) { |
| 707 | if (package->edge[i]->operator == VER_EQUAL) { | ||
| 708 | strcat(depends, " (= "); | ||
| 709 | } | ||
| 710 | else if (package->edge[i]->operator == VER_LESS) { | ||
| 711 | strcat(depends, " (<< "); | ||
| 712 | } | ||
| 713 | else if (package->edge[i]->operator == VER_LESS_EQUAL) { | ||
| 714 | strcat(depends, " (<= "); | ||
| 715 | } | ||
| 716 | else if (package->edge[i]->operator == VER_MORE) { | ||
| 717 | strcat(depends, " (>> "); | ||
| 718 | } | ||
| 719 | else if (package->edge[i]->operator == VER_MORE_EQUAL) { | ||
| 720 | strcat(depends, " (>= "); | ||
| 721 | } else { | ||
| 722 | strcat(depends, " ("); | ||
| 723 | } | ||
| 724 | strcat(depends, name_hashtable[package->edge[i]->version]); | ||
| 725 | strcat(depends, ")"); | ||
| 726 | } | ||
| 529 | } | 727 | } |
| 530 | } | 728 | } |
| 531 | fclose(f); | 729 | return(depends); |
| 532 | return status; | ||
| 533 | } | 730 | } |
| 534 | 731 | ||
| 535 | static int status_merge(void *status, package_t *pkgs) | 732 | /* This could do with a cleanup */ |
| 733 | void write_status_file(deb_file_t **deb_file) | ||
| 536 | { | 734 | { |
| 537 | FILE *fin, *fout; | 735 | FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r"); |
| 538 | char *line = NULL; | 736 | FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w"); |
| 539 | package_t *pkg = 0, *statpkg = 0; | 737 | char *package_name; |
| 540 | package_t locpkg; | 738 | char *status_from_file; |
| 739 | char *control_buffer = NULL; | ||
| 740 | char *tmp_string; | ||
| 741 | char *field; | ||
| 742 | int status_num; | ||
| 743 | int field_length; | ||
| 744 | int field_start = 0; | ||
| 745 | int buffer_length; | ||
| 746 | int write_flag; | ||
| 747 | int i = 0; | ||
| 541 | 748 | ||
| 542 | if ((fout = wfopen(new_statusfile, "w")) == NULL) { | 749 | /* Update previously known packages */ |
| 543 | return 0; | 750 | while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) { |
| 544 | } | 751 | tmp_string = strstr(control_buffer, "Package:") + 8; |
| 545 | if (getenv(udpkg_quiet) == NULL) { | 752 | tmp_string += strspn(tmp_string, " \n\t"); |
| 546 | printf("(Updating database...)\n"); | 753 | package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0")); |
| 547 | } | 754 | write_flag = FALSE; |
| 755 | |||
| 756 | tmp_string = strstr(control_buffer, "Status:"); | ||
| 757 | if (tmp_string != NULL) { | ||
| 758 | /* Seperate the status value from the control buffer */ | ||
| 759 | tmp_string += 7; | ||
| 760 | tmp_string += strspn(tmp_string, " \n\t"); | ||
| 761 | status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n")); | ||
| 762 | } else { | ||
| 763 | status_from_file = NULL; | ||
| 764 | } | ||
| 548 | 765 | ||
| 549 | /* | 766 | /* Find this package in the status hashtable */ |
| 550 | * Dont use wfopen here, handle errors ourself | 767 | status_num = search_status_hashtable(package_name); |
| 551 | */ | 768 | if (status_hashtable[status_num] != NULL) { |
| 552 | if ((fin = fopen(statusfile, "r")) != NULL) { | 769 | const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status]; |
| 553 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { | 770 | if (strcmp(status_from_file, status_from_hashtable) != 0) { |
| 554 | chomp(line); /* trim newline */ | 771 | /* New status isnt exactly the same as old status */ |
| 555 | /* If we see a package header, find out if it's a package | 772 | const int state_status = get_status(status_num, 3); |
| 556 | * that we have processed. if so, we skip that block for | 773 | if ((strcmp("installed", name_hashtable[state_status]) == 0) || |
| 557 | * now (write it at the end). | 774 | (strcmp("unpacked", name_hashtable[state_status]) == 0)) { |
| 558 | * | 775 | /* We need to add the control file from the package */ |
| 559 | * we also look at packages in the status cache and update | 776 | i = 0; |
| 560 | * their status fields | 777 | while(deb_file[i] != NULL) { |
| 561 | */ | 778 | if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) { |
| 562 | if (strstr(line, "Package: ") == line) { | 779 | char *last_char; |
| 563 | for (pkg = pkgs; pkg != 0 && strcmp(line + 9, | 780 | /* Write a status file entry with a modified status */ |
| 564 | pkg->package) != 0; pkg = pkg->next) ; | 781 | /* remove trailing \n's */ |
| 565 | 782 | while(1) { | |
| 566 | locpkg.package = line + 9; | 783 | last_char = last_char_is(deb_file[i]->control_file, '\n'); |
| 567 | statpkg = tfind(&locpkg, &status, package_compare); | 784 | if (last_char) { |
| 568 | 785 | *last_char = '\0'; | |
| 569 | /* note: statpkg should be non-zero, unless the status | 786 | } else { |
| 570 | * file was changed while we are processing (no locking | 787 | break; |
| 571 | * is currently done... | 788 | } |
| 572 | */ | 789 | } |
| 573 | if (statpkg != 0) { | 790 | fputs(deb_file[i]->control_file, new_status_file); |
| 574 | statpkg = *(package_t **)statpkg; | 791 | set_status(status_num, "ok", 2); |
| 792 | fprintf(new_status_file, "\nStatus: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); | ||
| 793 | write_flag = TRUE; | ||
| 794 | break; | ||
| 795 | } | ||
| 796 | i++; | ||
| 797 | } | ||
| 798 | /* This is temperary, debugging only */ | ||
| 799 | if (deb_file[i] == NULL) { | ||
| 800 | error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | else if (strcmp("not-installed", name_hashtable[state_status]) == 0) { | ||
| 804 | /* Only write the Package, Status, Priority and Section lines */ | ||
| 805 | fprintf(new_status_file, "Package: %s\n", package_name); | ||
| 806 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); | ||
| 807 | buffer_length = strlen(control_buffer); | ||
| 808 | while (field_start < buffer_length) { | ||
| 809 | field = read_package_field(&control_buffer[field_start]); | ||
| 810 | field_length = strlen(field); | ||
| 811 | field_start += (field_length + 1); | ||
| 812 | if (strncmp(field, "Priority:", 9) == 0) { | ||
| 813 | fprintf(new_status_file, "Priority:%s\n", field + 9); | ||
| 814 | } | ||
| 815 | if (strncmp(field, "Section:", 8) == 0) { | ||
| 816 | fprintf(new_status_file, "Section:%s\n", field + 8); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | write_flag = TRUE; | ||
| 820 | fputs("\n", new_status_file); | ||
| 821 | } | ||
| 822 | else if (strcmp("config-files", name_hashtable[state_status]) == 0) { | ||
| 823 | /* only change the status line */ | ||
| 824 | buffer_length = strlen(control_buffer); | ||
| 825 | while (field_start < buffer_length) { | ||
| 826 | field = read_package_field(&control_buffer[field_start]); | ||
| 827 | /* Setup start point for next field */ | ||
| 828 | field_length = strlen(field); | ||
| 829 | field_start += (field_length + 1); | ||
| 830 | if (strncmp(field, "Status:", 7) == 0) { | ||
| 831 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); | ||
| 832 | } else { | ||
| 833 | fprintf(new_status_file, "%s\n", field); | ||
| 834 | } | ||
| 835 | free(field); | ||
| 836 | } | ||
| 837 | write_flag = TRUE; | ||
| 838 | fputs("\n", new_status_file); | ||
| 575 | } | 839 | } |
| 576 | } | 840 | } |
| 577 | if (pkg != 0) { | 841 | } |
| 578 | continue; | 842 | |
| 579 | } | 843 | /* If the package from the status file wasnt handle above, do it now*/ |
| 580 | if (strstr(line, "Status: ") == line && statpkg != 0) { | 844 | if (write_flag == FALSE) { |
| 581 | snprintf(line, sizeof(line), "Status: %s %s %s", | 845 | fprintf(new_status_file, "%s\n\n", control_buffer); |
| 582 | state_words_want[statpkg->state_want - 1], | 846 | } |
| 583 | state_words_flag[statpkg->state_flag - 1], | 847 | |
| 584 | state_words_status[statpkg->state_status - 1]); | 848 | if (status_from_file != NULL) { |
| 849 | free(status_from_file); | ||
| 850 | } | ||
| 851 | free(package_name); | ||
| 852 | free(control_buffer); | ||
| 853 | } | ||
| 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) { | ||
| 858 | char *last_char; | ||
| 859 | /* remove trailing \n's */ | ||
| 860 | while(1) { | ||
| 861 | last_char = last_char_is(deb_file[i]->control_file, '\n'); | ||
| 862 | if (last_char) { | ||
| 863 | *last_char = '\0'; | ||
| 864 | } else { | ||
| 865 | break; | ||
| 866 | } | ||
| 585 | } | 867 | } |
| 586 | fprintf(fout, "%s\n", line); | 868 | |
| 869 | fputs(deb_file[i]->control_file, new_status_file); | ||
| 870 | set_status(status_num, "ok", 2); | ||
| 871 | fprintf(new_status_file, "\nStatus: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); | ||
| 587 | } | 872 | } |
| 588 | fclose(fin); | ||
| 589 | } | 873 | } |
| 590 | free(line); | 874 | fclose(old_status_file); |
| 875 | fclose(new_status_file); | ||
| 591 | 876 | ||
| 592 | // Print out packages we processed. | ||
| 593 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | ||
| 594 | write_package(fout, pkg); | ||
| 595 | } | ||
| 596 | fclose(fout); | ||
| 597 | 877 | ||
| 598 | /* | 878 | /* Create a seperate backfile to dpkg */ |
| 599 | * Its ok if renaming statusfile fails becasue it doesnt exist | 879 | if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) { |
| 600 | */ | ||
| 601 | if (rename(statusfile, bak_statusfile) == -1) { | ||
| 602 | struct stat stat_buf; | 880 | struct stat stat_buf; |
| 603 | if (stat(statusfile, &stat_buf) == 0) { | 881 | if (stat("/var/lib/dpkg/status", &stat_buf) == 0) { |
| 604 | error_msg("Couldnt create backup status file"); | 882 | error_msg_and_die("Couldnt create backup status file"); |
| 605 | return(EXIT_FAILURE); | ||
| 606 | } | 883 | } |
| 884 | /* Its ok if renaming the status file fails becasue status | ||
| 885 | * file doesnt exist, maybe we are starting from scratch */ | ||
| 607 | error_msg("No status file found, creating new one"); | 886 | error_msg("No status file found, creating new one"); |
| 608 | } | 887 | } |
| 609 | 888 | ||
| 610 | if (rename(new_statusfile, statusfile) == -1) { | 889 | if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) { |
| 611 | error_msg("Couldnt create status file"); | 890 | error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file"); |
| 612 | return(EXIT_FAILURE); | ||
| 613 | } | 891 | } |
| 614 | return(EXIT_SUCCESS); | ||
| 615 | } | 892 | } |
| 616 | 893 | ||
| 617 | static int is_file(const char *fn) | 894 | int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count) |
| 618 | { | 895 | { |
| 619 | struct stat statbuf; | 896 | int *conflicts = NULL; |
| 897 | int conflicts_num = 0; | ||
| 898 | int state_status; | ||
| 899 | int state_flag; | ||
| 900 | int state_want; | ||
| 901 | unsigned int status_package_num; | ||
| 902 | int i = deb_start; | ||
| 903 | int j, k; | ||
| 904 | |||
| 905 | /* Check for conflicts | ||
| 906 | * TODO: TEST if conflicts with other packages to be installed | ||
| 907 | * | ||
| 908 | * Add install packages and the packages they provide | ||
| 909 | * to the list of files to check conflicts for | ||
| 910 | */ | ||
| 620 | 911 | ||
| 621 | if (stat(fn, &statbuf) < 0) { | 912 | /* Create array of package numbers to check against |
| 622 | return 0; | 913 | * installed package for conflicts*/ |
| 914 | while (deb_file[i] != NULL) { | ||
| 915 | const unsigned int package_num = deb_file[i]->package; | ||
| 916 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); | ||
| 917 | conflicts[conflicts_num] = package_num; | ||
| 918 | conflicts_num++; | ||
| 919 | /* add provides to conflicts list */ | ||
| 920 | for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) { | ||
| 921 | if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) { | ||
| 922 | const int conflicts_package_num = search_package_hashtable( | ||
| 923 | package_hashtable[package_num]->edge[j]->name, | ||
| 924 | package_hashtable[package_num]->edge[j]->version, | ||
| 925 | package_hashtable[package_num]->edge[j]->operator); | ||
| 926 | if (package_hashtable[conflicts_package_num] == NULL) { | ||
| 927 | /* create a new package */ | ||
| 928 | common_node_t *new_node = (common_node_t *) xmalloc(sizeof(common_node_t)); | ||
| 929 | new_node->name = package_hashtable[package_num]->edge[j]->name; | ||
| 930 | new_node->version = package_hashtable[package_num]->edge[j]->version; | ||
| 931 | new_node->num_of_edges = 0; | ||
| 932 | new_node->edge = NULL; | ||
| 933 | package_hashtable[conflicts_package_num] = new_node; | ||
| 934 | } | ||
| 935 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); | ||
| 936 | conflicts[conflicts_num] = conflicts_package_num; | ||
| 937 | conflicts_num++; | ||
| 938 | } | ||
| 939 | } | ||
| 940 | i++; | ||
| 623 | } | 941 | } |
| 624 | return S_ISREG(statbuf.st_mode); | 942 | |
| 943 | /* Check conflicts */ | ||
| 944 | for (i = 0; i < conflicts_num; i++) { | ||
| 945 | /* Check for conflicts */ | ||
| 946 | for (j = 0; j < STATUS_HASH_PRIME; j++) { | ||
| 947 | if (status_hashtable[j] == NULL) { | ||
| 948 | continue; | ||
| 949 | } | ||
| 950 | state_flag = get_status(j, 2); | ||
| 951 | state_status = get_status(j, 3); | ||
| 952 | if ((state_status != search_name_hashtable("installed")) | ||
| 953 | && (state_flag != search_name_hashtable("want-install"))) { | ||
| 954 | continue; | ||
| 955 | } | ||
| 956 | status_package_num = status_hashtable[j]->package; | ||
| 957 | for (k = 0; k < package_hashtable[status_package_num]->num_of_edges; k++) { | ||
| 958 | const edge_t *package_edge = package_hashtable[status_package_num]->edge[k]; | ||
| 959 | if (package_edge->type != EDGE_CONFLICTS) { | ||
| 960 | continue; | ||
| 961 | } | ||
| 962 | if (package_edge->name != package_hashtable[conflicts[i]]->name) { | ||
| 963 | continue; | ||
| 964 | } | ||
| 965 | /* There is a conflict against the package name | ||
| 966 | * check if version conflict as well */ | ||
| 967 | if (test_version(package_hashtable[deb_file[i]->package]->version, | ||
| 968 | package_edge->version, package_edge->operator)) { | ||
| 969 | error_msg_and_die("Package %s conflict with %s", | ||
| 970 | name_hashtable[package_hashtable[deb_file[i]->package]->name], | ||
| 971 | name_hashtable[package_hashtable[status_package_num]->name]); | ||
| 972 | } | ||
| 973 | } | ||
| 974 | } | ||
| 975 | } | ||
| 976 | |||
| 977 | /* Check dependendcies */ | ||
| 978 | i = 0; | ||
| 979 | while (deb_file[i] != NULL) { | ||
| 980 | const common_node_t *package_node = package_hashtable[deb_file[i]->package]; | ||
| 981 | int status_num = 0; | ||
| 982 | |||
| 983 | for (j = 0; j < package_hashtable[deb_file[i]->package]->num_of_edges; j++) { | ||
| 984 | const edge_t *package_edge = package_node->edge[j]; | ||
| 985 | const unsigned int package_num = search_package_hashtable(package_edge->name, | ||
| 986 | package_edge->version, package_edge->operator); | ||
| 987 | |||
| 988 | status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]); | ||
| 989 | state_status = get_status(status_num, 3); | ||
| 990 | state_want = get_status(status_num, 1); | ||
| 991 | switch (package_edge->type) { | ||
| 992 | case(EDGE_PRE_DEPENDS): | ||
| 993 | case(EDGE_OR_PRE_DEPENDS): | ||
| 994 | /* It must be already installed */ | ||
| 995 | /* NOTE: This is untested, nothing apropriate in my status file */ | ||
| 996 | if ((package_hashtable[package_num] == NULL) || (state_status != search_name_hashtable("installed"))) { | ||
| 997 | error_msg_and_die("Package %s pre-depends on %s, but it is not installed", | ||
| 998 | name_hashtable[package_node->name], | ||
| 999 | name_hashtable[package_edge->name]); | ||
| 1000 | } | ||
| 1001 | break; | ||
| 1002 | case(EDGE_DEPENDS): | ||
| 1003 | case(EDGE_OR_DEPENDS): | ||
| 1004 | /* It must be already installed, or to be installed */ | ||
| 1005 | if ((package_hashtable[package_num] == NULL) || | ||
| 1006 | ((state_status != search_name_hashtable("installed")) && | ||
| 1007 | (state_want != search_name_hashtable("want_install")))) { | ||
| 1008 | error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed", | ||
| 1009 | name_hashtable[package_node->name], | ||
| 1010 | name_hashtable[package_edge->name]); | ||
| 1011 | } | ||
| 1012 | break; | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | i++; | ||
| 1016 | } | ||
| 1017 | return(TRUE); | ||
| 625 | } | 1018 | } |
| 626 | 1019 | ||
| 627 | static int dpkg_doconfigure(package_t *pkg) | 1020 | char **create_list(const char *filename) |
| 628 | { | 1021 | { |
| 629 | int r; | 1022 | FILE *list_stream; |
| 630 | char postinst[1024]; | 1023 | char **file_list = xmalloc(sizeof(char *)); |
| 631 | char buf[1024]; | 1024 | char *line = NULL; |
| 632 | 1025 | char *last_char; | |
| 633 | DPRINTF("Configuring %s\n", pkg->package); | 1026 | int length = 0; |
| 634 | pkg->state_status = 0; | 1027 | int count = 0; |
| 635 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); | ||
| 636 | 1028 | ||
| 637 | if (is_file(postinst)) { | 1029 | list_stream = fopen(filename, "r"); |
| 638 | snprintf(buf, sizeof(buf), "%s configure", postinst); | 1030 | if (list_stream == NULL) { |
| 639 | if ((r = do_system(buf)) != 0) { | 1031 | return(NULL); |
| 640 | error_msg("postinst exited with status %d\n", r); | 1032 | } |
| 641 | pkg->state_status = state_status_halfconfigured; | 1033 | while (getline(&line, &length, list_stream) != -1) { |
| 642 | return 1; | 1034 | file_list = xrealloc(file_list, sizeof(char *) * (length + 1)); |
| 1035 | last_char = last_char_is(line, '\n'); | ||
| 1036 | if (last_char) { | ||
| 1037 | *last_char = '\0'; | ||
| 643 | } | 1038 | } |
| 1039 | file_list[count] = xstrdup(line); | ||
| 1040 | free(line); | ||
| 1041 | count++; | ||
| 1042 | length = 0; | ||
| 1043 | } | ||
| 1044 | fclose(list_stream); | ||
| 1045 | if (count == 0) { | ||
| 1046 | return(NULL); | ||
| 1047 | } else { | ||
| 1048 | file_list[count] = NULL; | ||
| 1049 | return(file_list); | ||
| 644 | } | 1050 | } |
| 645 | pkg->state_status = state_status_installed; | ||
| 646 | |||
| 647 | return 0; | ||
| 648 | } | 1051 | } |
| 649 | 1052 | ||
| 650 | static int dpkg_dounpack(package_t *pkg) | 1053 | /* maybe i should try and hook this into remove_file.c somehow */ |
| 1054 | int remove_file_array(char **remove_names, char **exclude_names) | ||
| 651 | { | 1055 | { |
| 652 | FILE *out_stream; | 1056 | struct stat path_stat; |
| 653 | char *info_prefix; | 1057 | int match_flag; |
| 654 | int status = TRUE; | 1058 | int remove_flag = FALSE; |
| 655 | int r = 0; | 1059 | int i,j; |
| 656 | |||
| 657 | DPRINTF("Unpacking %s\n", pkg->package); | ||
| 658 | 1060 | ||
| 659 | /* extract the data file */ | 1061 | if (remove_names == NULL) { |
| 660 | deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL); | 1062 | return(FALSE); |
| 1063 | } | ||
| 1064 | for (i = 0; remove_names[i] != NULL; i++) { | ||
| 1065 | match_flag = FALSE; | ||
| 1066 | if (exclude_names != NULL) { | ||
| 1067 | for (j = 0; exclude_names[j] != 0; j++) { | ||
| 1068 | if (strcmp(remove_names[i], exclude_names[j]) == 0) { | ||
| 1069 | match_flag = TRUE; | ||
| 1070 | break; | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | if (!match_flag) { | ||
| 1075 | if (lstat(remove_names[i], &path_stat) < 0) { | ||
| 1076 | continue; | ||
| 1077 | } | ||
| 1078 | if (S_ISDIR(path_stat.st_mode)) { | ||
| 1079 | if (rmdir(remove_names[i]) != -1) { | ||
| 1080 | remove_flag = TRUE; | ||
| 1081 | } | ||
| 1082 | } else { | ||
| 1083 | if (unlink(remove_names[i]) != -1) { | ||
| 1084 | remove_flag = TRUE; | ||
| 1085 | } | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | return(remove_flag); | ||
| 1090 | } | ||
| 661 | 1091 | ||
| 662 | /* extract the control files */ | 1092 | int run_package_script(const char *package_name, const char *script_type) |
| 663 | info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1); | 1093 | { |
| 664 | sprintf(info_prefix, "%s/%s.", infodir, pkg->package); | 1094 | struct stat path_stat; |
| 665 | deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); | 1095 | char *script_path; |
| 666 | 1096 | ||
| 667 | /* Create the list file */ | 1097 | script_path = xmalloc(strlen(package_name) + strlen(script_type) + 21); |
| 668 | strcat(info_prefix, "list"); | 1098 | sprintf(script_path, "/var/lib/dpkg/info/%s.%s", package_name, script_type); |
| 669 | out_stream = wfopen(info_prefix, "w"); | ||
| 670 | deb_extract(pkg->filename, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL); | ||
| 671 | fclose(out_stream); | ||
| 672 | 1099 | ||
| 673 | pkg->state_want = state_want_install; | 1100 | /* If the file doesnt exist is isnt a fatal */ |
| 674 | pkg->state_flag = state_flag_ok; | 1101 | if (lstat(script_path, &path_stat) < 0) { |
| 675 | 1102 | return(EXIT_SUCCESS); | |
| 676 | if (status == TRUE) { | ||
| 677 | pkg->state_status = state_status_unpacked; | ||
| 678 | } else { | 1103 | } else { |
| 679 | pkg->state_status = state_status_halfinstalled; | 1104 | return(system(script_path)); |
| 680 | } | 1105 | } |
| 681 | |||
| 682 | return r; | ||
| 683 | } | 1106 | } |
| 684 | 1107 | ||
| 685 | /* | 1108 | void all_control_list(char **remove_files, const char *package_name) |
| 686 | * Extract and parse the control file from control.tar.gz | ||
| 687 | */ | ||
| 688 | static int dpkg_read_control(package_t *pkg) | ||
| 689 | { | 1109 | { |
| 690 | FILE *pkg_file; | 1110 | const char *all_extensions[11] = {"preinst", "postinst", "prerm", "postrm", |
| 691 | char *control_buffer = NULL; | 1111 | "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL }; |
| 1112 | int i; | ||
| 692 | 1113 | ||
| 693 | if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) { | 1114 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 694 | return EXIT_FAILURE; | 1115 | for(i = 0; i < 10; i++) { |
| 1116 | remove_files[i] = xmalloc(strlen(package_name) + strlen(all_extensions[i]) + 21); | ||
| 1117 | sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, all_extensions[i]); | ||
| 695 | } | 1118 | } |
| 696 | control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); | 1119 | remove_files[10] = NULL; |
| 697 | fill_package_struct(pkg, control_buffer); | ||
| 698 | return EXIT_SUCCESS; | ||
| 699 | } | 1120 | } |
| 700 | 1121 | ||
| 701 | static int dpkg_unpack(package_t *pkgs, void *status) | 1122 | void remove_package(const unsigned int package_num) |
| 702 | { | 1123 | { |
| 703 | int r = 0; | 1124 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 704 | package_t *pkg; | 1125 | const unsigned int status_num = search_status_hashtable(package_name); |
| 705 | 1126 | const int package_name_length = strlen(package_name); | |
| 706 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 1127 | char **remove_files; |
| 707 | dpkg_read_control(pkg); | 1128 | char **exclude_files; |
| 708 | if ((r = dpkg_dounpack(pkg)) != 0 ) { | 1129 | char list_name[package_name_length + 25]; |
| 709 | break; | 1130 | char conffile_name[package_name_length + 30]; |
| 710 | } | 1131 | int return_value; |
| 1132 | |||
| 1133 | /* run prerm script */ | ||
| 1134 | return_value = run_package_script(package_name, "prem"); | ||
| 1135 | if (return_value == -1) { | ||
| 1136 | error_msg_and_die("script failed, prerm failure"); | ||
| 711 | } | 1137 | } |
| 712 | status_merge(status, pkgs); | ||
| 713 | 1138 | ||
| 714 | return r; | 1139 | /* Create a list of files to remove, and a seperate list of those to keep */ |
| 715 | } | 1140 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1141 | remove_files = create_list(list_name); | ||
| 716 | 1142 | ||
| 717 | static int dpkg_configure(package_t *pkgs, void *status) | 1143 | sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name); |
| 718 | { | 1144 | exclude_files = create_list(conffile_name); |
| 719 | int r = 0; | ||
| 720 | void *found; | ||
| 721 | package_t *pkg; | ||
| 722 | 1145 | ||
| 723 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { | 1146 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 724 | found = tfind(pkg, &status, package_compare); | 1147 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 725 | 1148 | ||
| 726 | if (found == 0) { | 1149 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 727 | error_msg("Trying to configure %s, but it is not installed", pkg->package); | 1150 | remove_files = xmalloc(11); |
| 728 | r = 1; | 1151 | all_control_list(remove_files, package_name); |
| 729 | } | ||
| 730 | /* configure the package listed in the status file; | ||
| 731 | * not pkg, as we have info only for the latter | ||
| 732 | */ | ||
| 733 | else { | ||
| 734 | r = dpkg_doconfigure(*(package_t **)found); | ||
| 735 | } | ||
| 736 | } | ||
| 737 | status_merge(status, 0); | ||
| 738 | 1152 | ||
| 739 | return r; | 1153 | /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */ |
| 1154 | exclude_files = xmalloc(sizeof(char*) * 3); | ||
| 1155 | exclude_files[0] = xstrdup(conffile_name); | ||
| 1156 | exclude_files[1] = xmalloc(package_name_length + 27); | ||
| 1157 | sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name); | ||
| 1158 | exclude_files[2] = NULL; | ||
| 1159 | |||
| 1160 | remove_file_array(remove_files, exclude_files); | ||
| 1161 | |||
| 1162 | /* rename <package>.conffile to <package>.list */ | ||
| 1163 | rename(conffile_name, list_name); | ||
| 1164 | |||
| 1165 | /* Change package status */ | ||
| 1166 | set_status(status_num, "deinstall", 1); | ||
| 1167 | set_status(status_num, "config-files", 3); | ||
| 740 | } | 1168 | } |
| 741 | 1169 | ||
| 742 | static int dpkg_install(package_t *pkgs, void *status) | 1170 | void purge_package(const unsigned int package_num) |
| 743 | { | 1171 | { |
| 744 | package_t *p, *ordered = 0; | 1172 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 745 | 1173 | const unsigned int status_num = search_status_hashtable(package_name); | |
| 746 | /* Stage 1: parse all the control information */ | 1174 | char **remove_files; |
| 747 | for (p = pkgs; p != 0; p = p->next) { | 1175 | char **exclude_files; |
| 748 | dpkg_read_control(p); | 1176 | char list_name[strlen(package_name) + 25]; |
| 1177 | |||
| 1178 | /* run prerm script */ | ||
| 1179 | if (run_package_script(package_name, "prerm") == -1) { | ||
| 1180 | error_msg_and_die("script failed, prerm failure"); | ||
| 749 | } | 1181 | } |
| 750 | 1182 | ||
| 751 | /* Stage 2: resolve dependencies */ | 1183 | /* Create a list of files to remove */ |
| 752 | #ifdef DODEPENDS | 1184 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 753 | ordered = depends_resolve(pkgs, status); | 1185 | remove_files = create_list(list_name); |
| 754 | #else | ||
| 755 | ordered = pkgs; | ||
| 756 | #endif | ||
| 757 | |||
| 758 | /* Stage 3: install */ | ||
| 759 | for (p = ordered; p != 0; p = p->next) { | ||
| 760 | p->state_want = state_want_install; | ||
| 761 | 1186 | ||
| 762 | /* for now the flag is always set to ok... this is probably | 1187 | exclude_files = xmalloc(1); |
| 763 | * not what we want | 1188 | exclude_files[0] = NULL; |
| 764 | */ | ||
| 765 | p->state_flag = state_flag_ok; | ||
| 766 | 1189 | ||
| 767 | DPRINTF("Installing %s\n", p->package); | 1190 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 768 | if (dpkg_dounpack(p) != 0) { | 1191 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 769 | perror_msg(p->filename); | ||
| 770 | } | ||
| 771 | 1192 | ||
| 772 | if (dpkg_doconfigure(p) != 0) { | 1193 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 773 | perror_msg(p->filename); | 1194 | remove_files = xmalloc(11); |
| 774 | } | 1195 | all_control_list(remove_files, package_name); |
| 1196 | remove_file_array(remove_files, exclude_files); | ||
| 1197 | |||
| 1198 | /* run postrm script */ | ||
| 1199 | if (run_package_script(package_name, "postrm") == -1) { | ||
| 1200 | error_msg_and_die("postrm fialure.. set status to what?"); | ||
| 775 | } | 1201 | } |
| 1202 | /* Change package status */ | ||
| 1203 | set_status(status_num, "purge", 1); | ||
| 1204 | set_status(status_num, "not-installed", 3); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | void unpack_package(deb_file_t *deb_file) | ||
| 1208 | { | ||
| 1209 | const unsigned int package_name_num = package_hashtable[deb_file->package]->name; | ||
| 1210 | const char *package_name = name_hashtable[package_name_num]; | ||
| 1211 | const unsigned int status_num = search_status_hashtable(package_name); | ||
| 1212 | unsigned int status_package_num; | ||
| 776 | 1213 | ||
| 777 | if (ordered != 0) { | 1214 | FILE *out_stream; |
| 778 | status_merge(status, pkgs); | 1215 | char *info_prefix; |
| 1216 | |||
| 1217 | /* If existing version, remove it first */ | ||
| 1218 | if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) { | ||
| 1219 | /* Package is already installed, remove old version first */ | ||
| 1220 | printf("Preparing to replace %s %s (using %s) ...\n", package_name, | ||
| 1221 | name_hashtable[package_hashtable[status_package_num]->version], | ||
| 1222 | deb_file->filename); | ||
| 1223 | remove_package(status_package_num); | ||
| 1224 | } else { | ||
| 1225 | printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename); | ||
| 779 | } | 1226 | } |
| 780 | 1227 | ||
| 781 | return 0; | 1228 | /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */ |
| 1229 | info_prefix = (char *) xmalloc(sizeof(package_name) + 20 + 4 + 1); | ||
| 1230 | sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name); | ||
| 1231 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); | ||
| 1232 | |||
| 1233 | /* Extract data.tar.gz to the root directory */ | ||
| 1234 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_data_tar_gz | extract_all_to_fs), "/", NULL); | ||
| 1235 | |||
| 1236 | /* Create the list file */ | ||
| 1237 | strcat(info_prefix, "list"); | ||
| 1238 | |||
| 1239 | out_stream = wfopen(info_prefix, "w"); | ||
| 1240 | deb_extract(deb_file->filename, out_stream, (extract_quiet | extract_data_tar_gz | extract_list), NULL, NULL); | ||
| 1241 | fclose(out_stream); | ||
| 1242 | |||
| 1243 | /* change status */ | ||
| 1244 | set_status(status_num, "install", 1); | ||
| 1245 | set_status(status_num, "unpacked", 3); | ||
| 782 | } | 1246 | } |
| 783 | 1247 | ||
| 784 | /* | 1248 | void configure_package(deb_file_t *deb_file) |
| 785 | * Not implemented yet | ||
| 786 | * | ||
| 787 | static int dpkg_remove(package_t *pkgs, void *status) | ||
| 788 | { | 1249 | { |
| 789 | package_t *p; | 1250 | int return_value; |
| 790 | 1251 | int status_num; | |
| 791 | for (p = pkgs; p != 0; p = p->next) | 1252 | |
| 792 | { | 1253 | /* Run the preinst prior to extracting */ |
| 1254 | return_value = run_package_script(name_hashtable[package_hashtable[deb_file->package]->name], "postinst"); | ||
| 1255 | if (return_value == -1) { | ||
| 1256 | /* TODO: handle failure gracefully */ | ||
| 1257 | error_msg_and_die("postrm fialure.. set status to what?"); | ||
| 793 | } | 1258 | } |
| 794 | status_merge(status, 0); | ||
| 795 | 1259 | ||
| 796 | return 0; | 1260 | /* Change status to reflect success */ |
| 1261 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file->package]->name]); | ||
| 1262 | set_status(status_num, "install", 1); | ||
| 1263 | set_status(status_num, "installed", 3); | ||
| 797 | } | 1264 | } |
| 798 | */ | ||
| 799 | 1265 | ||
| 800 | extern int dpkg_main(int argc, char **argv) | 1266 | extern int dpkg_main(int argc, char **argv) |
| 801 | { | 1267 | { |
| 802 | const int arg_install = 1; | 1268 | deb_file_t **deb_file = NULL; |
| 803 | const int arg_unpack = 2; | 1269 | status_node_t *status_node; |
| 804 | const int arg_configure = 4; | 1270 | char opt = 0; |
| 805 | 1271 | int package_num; | |
| 806 | package_t *p, *packages = NULL; | 1272 | int dpkg_opt = 0; |
| 807 | void *status = NULL; | 1273 | int deb_count = 0; |
| 808 | int opt = 0; | 1274 | int state_status; |
| 809 | int optflag = 0; | 1275 | int status_num; |
| 1276 | int i; | ||
| 810 | 1277 | ||
| 811 | while ((opt = getopt(argc, argv, "iruc")) != -1) { | 1278 | while ((opt = getopt(argc, argv, "cF:ilPru")) != -1) { |
| 812 | switch (opt) { | 1279 | switch (opt) { |
| 1280 | case 'c': | ||
| 1281 | dpkg_opt |= dpkg_opt_configure; | ||
| 1282 | dpkg_opt |= dpkg_opt_package_name; | ||
| 1283 | break; | ||
| 1284 | case 'F': // equivalent to --force in official dpkg | ||
| 1285 | if (strcmp(optarg, "depends") == 0) { | ||
| 1286 | dpkg_opt |= dpkg_opt_force_ignore_depends; | ||
| 1287 | } | ||
| 813 | case 'i': | 1288 | case 'i': |
| 814 | optflag |= arg_install; | 1289 | dpkg_opt |= dpkg_opt_install; |
| 1290 | dpkg_opt |= dpkg_opt_filename; | ||
| 815 | break; | 1291 | break; |
| 816 | case 'u': | 1292 | case 'l': |
| 817 | optflag |= arg_unpack; | 1293 | dpkg_opt |= dpkg_opt_list_installed; |
| 1294 | case 'P': | ||
| 1295 | dpkg_opt |= dpkg_opt_purge; | ||
| 1296 | dpkg_opt |= dpkg_opt_package_name; | ||
| 818 | break; | 1297 | break; |
| 819 | case 'c': | 1298 | case 'r': |
| 820 | optflag |= arg_configure; | 1299 | dpkg_opt |= dpkg_opt_remove; |
| 1300 | dpkg_opt |= dpkg_opt_package_name; | ||
| 1301 | break; | ||
| 1302 | case 'u': /* Equivalent to --unpack in official dpkg */ | ||
| 1303 | dpkg_opt |= dpkg_opt_unpack; | ||
| 1304 | dpkg_opt |= dpkg_opt_filename; | ||
| 821 | break; | 1305 | break; |
| 822 | default: | 1306 | default: |
| 823 | show_usage(); | 1307 | show_usage(); |
| 824 | } | 1308 | } |
| 825 | } | 1309 | } |
| 826 | 1310 | ||
| 1311 | if ((argc == optind) || (dpkg_opt == 0)) { | ||
| 1312 | show_usage(); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | puts("(Reading database ... xxxxx files and directories installed.)"); | ||
| 1316 | index_status_file("/var/lib/dpkg/status"); | ||
| 1317 | |||
| 1318 | /* Read arguments and store relevant info in structs */ | ||
| 1319 | deb_file = xmalloc(sizeof(deb_file_t)); | ||
| 827 | while (optind < argc) { | 1320 | while (optind < argc) { |
| 828 | p = (package_t *) xcalloc(1, sizeof(package_t)); | 1321 | deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t)); |
| 829 | if (optflag & arg_configure) { | 1322 | if (dpkg_opt & dpkg_opt_filename) { |
| 830 | p->package = xstrdup(argv[optind]); | 1323 | deb_file[deb_count]->filename = xstrdup(argv[optind]); |
| 831 | } else { | 1324 | deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); |
| 832 | p->filename = xstrdup(argv[optind]); | 1325 | if (deb_file[deb_count]->control_file == NULL) { |
| 1326 | error_msg_and_die("Couldnt extract control file"); | ||
| 1327 | } | ||
| 1328 | package_num = fill_package_struct(deb_file[deb_count]->control_file); | ||
| 1329 | |||
| 1330 | if (package_num == -1) { | ||
| 1331 | error_msg("Invalid control file in %s", argv[optind]); | ||
| 1332 | continue; | ||
| 1333 | } | ||
| 1334 | deb_file[deb_count]->package = (unsigned int) package_num; | ||
| 1335 | /* Add the package to the status hashtable */ | ||
| 1336 | if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) { | ||
| 1337 | status_node = (status_node_t *) xmalloc(sizeof(status_node_t)); | ||
| 1338 | status_node->package = deb_file[deb_count]->package; | ||
| 1339 | /* use reinstreq isnt changed to "ok" until the package control info | ||
| 1340 | * is written to the status file*/ | ||
| 1341 | status_node->status = search_name_hashtable("install reinstreq not-installed"); | ||
| 1342 | |||
| 1343 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1344 | status_hashtable[status_num] = status_node; | ||
| 1345 | } | ||
| 833 | } | 1346 | } |
| 834 | p->next = packages; | 1347 | else if (dpkg_opt & dpkg_opt_package_name) { |
| 835 | packages = p; | 1348 | deb_file[deb_count]->filename = NULL; |
| 1349 | deb_file[deb_count]->control_file = NULL; | ||
| 1350 | deb_file[deb_count]->package = search_package_hashtable( | ||
| 1351 | search_name_hashtable(argv[optind]), | ||
| 1352 | search_name_hashtable("ANY"), VER_ANY); | ||
| 1353 | if (package_hashtable[deb_file[deb_count]->package] == NULL) { | ||
| 1354 | error_msg_and_die("unknown package, %s\n", argv[optind]); | ||
| 1355 | } | ||
| 1356 | state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3); | ||
| 836 | 1357 | ||
| 1358 | /* check package status is "installed" */ | ||
| 1359 | if (dpkg_opt & dpkg_opt_remove) { | ||
| 1360 | if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || | ||
| 1361 | (strcmp(name_hashtable[state_status], "config-files") == 0)) { | ||
| 1362 | error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1363 | } | ||
| 1364 | } | ||
| 1365 | else if (dpkg_opt & dpkg_opt_purge) { | ||
| 1366 | /* if package status is "conf-files" then its ok */ | ||
| 1367 | if (strcmp(name_hashtable[state_status], "not-installed") == 0) { | ||
| 1368 | error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1369 | } | ||
| 1370 | } | ||
| 1371 | } | ||
| 1372 | deb_count++; | ||
| 837 | optind++; | 1373 | optind++; |
| 838 | } | 1374 | } |
| 1375 | deb_file[deb_count] = NULL; | ||
| 839 | 1376 | ||
| 840 | make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR); | 1377 | /* Check that the deb file arguments are installable */ |
| 1378 | /* TODO: check dependencies before removing */ | ||
| 1379 | if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { | ||
| 1380 | if (!check_deps(deb_file, 0, deb_count)) { | ||
| 1381 | error_msg_and_die("Dependency check fialed"); | ||
| 1382 | } | ||
| 1383 | } | ||
| 841 | 1384 | ||
| 842 | status = status_read(); | 1385 | for (i = 0; i < deb_count; i++) { |
| 1386 | /* Remove or purge packages */ | ||
| 1387 | if (dpkg_opt & dpkg_opt_remove) { | ||
| 1388 | remove_package(deb_file[i]->package); | ||
| 1389 | } | ||
| 1390 | else if (dpkg_opt & dpkg_opt_purge) { | ||
| 1391 | purge_package(deb_file[i]->package); | ||
| 1392 | } | ||
| 1393 | else if (dpkg_opt & dpkg_opt_unpack) { | ||
| 1394 | unpack_package(deb_file[i]); | ||
| 1395 | } | ||
| 1396 | else if (dpkg_opt & dpkg_opt_install) { | ||
| 1397 | unpack_package(deb_file[i]); | ||
| 1398 | configure_package(deb_file[i]); | ||
| 1399 | } | ||
| 1400 | else if (dpkg_opt & dpkg_opt_configure) { | ||
| 1401 | configure_package(deb_file[i]); | ||
| 1402 | } | ||
| 1403 | } | ||
| 843 | 1404 | ||
| 844 | if (optflag & arg_install) { | 1405 | write_status_file(deb_file); |
| 845 | return dpkg_install(packages, status); | 1406 | |
| 1407 | for (i = 0; i < NAME_HASH_PRIME; i++) { | ||
| 1408 | if (name_hashtable[i] != NULL) { | ||
| 1409 | free(name_hashtable[i]); | ||
| 1410 | } | ||
| 846 | } | 1411 | } |
| 847 | else if (optflag & arg_unpack) { | 1412 | for (i = 0; i < PACKAGE_HASH_PRIME; i++) { |
| 848 | return dpkg_unpack(packages, status); | 1413 | free_package(package_hashtable[i]); |
| 849 | } | 1414 | } |
| 850 | else if (optflag & arg_configure) { | 1415 | for (i = 0; i < STATUS_HASH_PRIME; i++) { |
| 851 | return dpkg_configure(packages, status); | 1416 | if (status_hashtable[i] != NULL) { |
| 1417 | free(status_hashtable[i]); | ||
| 1418 | } | ||
| 852 | } | 1419 | } |
| 1420 | |||
| 853 | return(EXIT_FAILURE); | 1421 | return(EXIT_FAILURE); |
| 854 | } | 1422 | } |
| 1423 | |||
| @@ -1,854 +1,1423 @@ | |||
| 1 | #include <ctype.h> | 1 | /* |
| 2 | #include <dirent.h> | 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 | */ | ||
| 23 | |||
| 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 | |||
| 3 | #include <getopt.h> | 40 | #include <getopt.h> |
| 4 | #include <stdio.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <stdlib.h> | 41 | #include <stdlib.h> |
| 7 | #include <search.h> | 42 | #include <string.h> |
| 8 | #include <errno.h> | ||
| 9 | #include <fcntl.h> | ||
| 10 | #include <unistd.h> | 43 | #include <unistd.h> |
| 11 | #include <utime.h> | ||
| 12 | #include <sys/types.h> | ||
| 13 | #include <sys/stat.h> | ||
| 14 | |||
| 15 | #include "busybox.h" | 44 | #include "busybox.h" |
| 16 | 45 | ||
| 17 | #define DEPENDSMAX 64 /* maximum number of depends we can handle */ | 46 | /* NOTE: If you vary HASH_PRIME sizes be aware, |
| 18 | 47 | * 1) Tweaking these will have a big effect on how much memory this program uses. | |
| 19 | /* Should we do full dependency checking? */ | 48 | * 2) For computational efficiency these hash tables should be at least 20% |
| 20 | //#define DODEPENDS 0 | 49 | * larger than the maximum number of elements stored in it. |
| 21 | 50 | * 3) All _HASH_PRIME's must be a prime number or chaos is assured, if your looking | |
| 22 | /* Should we do debugging? */ | 51 | * for a prime, try http://www.utm.edu/research/primes/lists/small/10000.txt |
| 23 | //#define DODEBUG 0 | 52 | * 4) If you go bigger than 15 bits you may get into trouble (untested) as its |
| 24 | 53 | * sometimes cast to an unsigned int, if you go to 16 bit you will overlap | |
| 25 | #ifdef DODEBUG | 54 | * int's and chaos is assured, 16381 is the max prime for 14 bit field |
| 26 | #define SYSTEM(x) do_system(x) | 55 | */ |
| 27 | #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args) | ||
| 28 | #else | ||
| 29 | #define SYSTEM(x) system(x) | ||
| 30 | #define DPRINTF(fmt,args...) /* nothing */ | ||
| 31 | #endif | ||
| 32 | |||
| 33 | /* from dpkg-deb.c */ | ||
| 34 | |||
| 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 | |||
| 39 | static const char infodir[] = "/var/lib/dpkg/info/"; | ||
| 40 | static const char udpkg_quiet[] = "UDPKG_QUIET"; | ||
| 41 | |||
| 42 | //static const int status_want_unknown = 1; | ||
| 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; | ||
| 47 | |||
| 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; | ||
| 52 | |||
| 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; | ||
| 61 | |||
| 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", | ||
| 65 | "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 }; | ||
| 66 | |||
| 67 | static const int color_white = 0; | ||
| 68 | static const int color_grey = 1; | ||
| 69 | static const int color_black = 2; | ||
| 70 | |||
| 71 | /* data structures */ | ||
| 72 | typedef struct package_s { | ||
| 73 | char *filename; | ||
| 74 | char *package; | ||
| 75 | unsigned char state_want; | ||
| 76 | unsigned char state_flag; | ||
| 77 | unsigned char state_status; | ||
| 78 | char *depends; | ||
| 79 | char *provides; | ||
| 80 | char *description; | ||
| 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; | ||
| 96 | int installer_menu_item; | ||
| 97 | char color; /* for topo-sort */ | ||
| 98 | struct package_s *requiredfor[DEPENDSMAX]; | ||
| 99 | unsigned short requiredcount; | ||
| 100 | struct package_s *next; | ||
| 101 | } package_t; | ||
| 102 | |||
| 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 | 56 | ||
| 113 | static int package_compare(const void *p1, const void *p2) | 57 | /* NAME_HASH_PRIME, Stores package names and versions, |
| 114 | { | 58 | * I estimate it should be at least 50% bigger than PACKAGE_HASH_PRIME, |
| 115 | return strcmp(((package_t *)p1)->package, | 59 | * as there a lot of duplicate version numbers */ |
| 116 | ((package_t *)p2)->package); | 60 | #define NAME_HASH_PRIME 16381 |
| 117 | } | 61 | char *name_hashtable[NAME_HASH_PRIME + 1]; |
| 62 | |||
| 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; | ||
| 78 | |||
| 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]; | ||
| 86 | |||
| 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]; | ||
| 96 | |||
| 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 | }; | ||
| 120 | |||
| 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 | }; | ||
| 132 | |||
| 133 | typedef struct deb_file_s { | ||
| 134 | char *control_file; | ||
| 135 | char *filename; | ||
| 136 | unsigned int package:14; | ||
| 137 | } deb_file_t; | ||
| 118 | 138 | ||
| 119 | #ifdef DODEPENDS | ||
| 120 | 139 | ||
| 121 | static char **depends_split(const char *dependsstr) | 140 | void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime) |
| 122 | { | 141 | { |
| 123 | static char *dependsvec[DEPENDSMAX]; | 142 | unsigned long int hash_num = key[0]; |
| 124 | char *p; | 143 | int len = strlen(key); |
| 125 | int i = 0; | 144 | int i; |
| 126 | 145 | ||
| 127 | dependsvec[0] = 0; | 146 | /* Maybe i should have uses a "proper" hashing algorithm here instead |
| 128 | if (dependsstr == 0) { | 147 | * of making one up myself, seems to be working ok though. */ |
| 129 | goto end; | 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)); | ||
| 130 | } | 154 | } |
| 155 | *start = (unsigned int) hash_num % hash_prime; | ||
| 156 | *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1)); | ||
| 157 | } | ||
| 131 | 158 | ||
| 132 | p = xstrdup(dependsstr); | 159 | /* this adds the key to the hash table */ |
| 133 | while (*p != 0 && *p != '\n') { | 160 | int search_name_hashtable(const char *key) |
| 134 | if (*p != ' ') { | 161 | { |
| 135 | if (*p == ',') { | 162 | unsigned int probe_address = 0; |
| 136 | *p = 0; | 163 | unsigned int probe_decrement = 0; |
| 137 | dependsvec[++i] = 0; | 164 | |
| 138 | } else { | 165 | make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME); |
| 139 | if (dependsvec[i] == 0) { | 166 | while(name_hashtable[probe_address] != NULL) { |
| 140 | dependsvec[i] = p; | 167 | if (strcmp(name_hashtable[probe_address], key) == 0) { |
| 141 | } | 168 | return(probe_address); |
| 142 | } | ||
| 143 | } else { | 169 | } else { |
| 144 | *p = 0; /* eat the space... */ | 170 | probe_address -= probe_decrement; |
| 171 | if ((int)probe_address < 0) { | ||
| 172 | probe_address += NAME_HASH_PRIME; | ||
| 173 | } | ||
| 145 | } | 174 | } |
| 146 | p++; | ||
| 147 | } | 175 | } |
| 148 | *p = 0; | 176 | name_hashtable[probe_address] = xstrdup(key); |
| 149 | 177 | ||
| 150 | end: | 178 | return(probe_address); |
| 151 | dependsvec[i+1] = 0; | ||
| 152 | return dependsvec; | ||
| 153 | } | 179 | } |
| 154 | 180 | ||
| 155 | /* Topological sort algorithm: | 181 | /* this DOESNT add the key to the hashtable |
| 156 | * ordered is the output list, pkgs is the dependency graph, pkg is | 182 | * TODO make it consistent with search_name_hashtable |
| 157 | * the current node | ||
| 158 | * | ||
| 159 | * recursively add all the adjacent nodes to the ordered list, marking | ||
| 160 | * each one as visited along the way | ||
| 161 | * | ||
| 162 | * yes, this algorithm looks a bit odd when all the params have the | ||
| 163 | * same type :-) | ||
| 164 | */ | 183 | */ |
| 165 | static void depends_sort_visit(package_t **ordered, package_t *pkgs, | 184 | unsigned int search_status_hashtable(const char *key) |
| 166 | package_t *pkg) | ||
| 167 | { | 185 | { |
| 168 | unsigned short i; | 186 | unsigned int probe_address = 0; |
| 169 | 187 | unsigned int probe_decrement = 0; | |
| 170 | /* mark node as processing */ | 188 | |
| 171 | pkg->color = color_grey; | 189 | make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME); |
| 172 | 190 | while(status_hashtable[probe_address] != NULL) { | |
| 173 | /* visit each not-yet-visited node */ | 191 | if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) { |
| 174 | for (i = 0; i < pkg->requiredcount; i++) | 192 | break; |
| 175 | if (pkg->requiredfor[i]->color == color_white) | 193 | } else { |
| 176 | depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]); | 194 | probe_address -= probe_decrement; |
| 177 | 195 | if ((int)probe_address < 0) { | |
| 178 | #if 0 | 196 | probe_address += STATUS_HASH_PRIME; |
| 179 | /* add it to the list */ | 197 | } |
| 180 | newnode = (struct package_t *)xmalloc(sizeof(struct package_t)); | 198 | } |
| 181 | /* make a shallow copy */ | 199 | } |
| 182 | *newnode = *pkg; | 200 | return(probe_address); |
| 183 | newnode->next = *ordered; | ||
| 184 | *ordered = newnode; | ||
| 185 | #endif | ||
| 186 | |||
| 187 | pkg->next = *ordered; | ||
| 188 | *ordered = pkg; | ||
| 189 | |||
| 190 | /* mark node as done */ | ||
| 191 | pkg->color = color_black; | ||
| 192 | } | 201 | } |
| 193 | 202 | ||
| 194 | static package_t *depends_sort(package_t *pkgs) | 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) | ||
| 195 | { | 205 | { |
| 196 | /* TODO: it needs to break cycles in the to-be-installed package | 206 | int upstream_len1 = 0; |
| 197 | * graph... */ | 207 | int upstream_len2 = 0; |
| 198 | package_t *ordered = NULL; | 208 | char *name1_char; |
| 199 | package_t *pkg; | 209 | char *name2_char; |
| 200 | 210 | int len1 = 0; | |
| 201 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 211 | int len2 = 0; |
| 202 | pkg->color = color_white; | 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(""); | ||
| 203 | } | 221 | } |
| 204 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 222 | upstream_len1 = strlen(version1); |
| 205 | if (pkg->color == color_white) { | 223 | upstream_len2 = strlen(version2); |
| 206 | depends_sort_visit(&ordered, pkgs, pkg); | 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); | ||
| 207 | } | 253 | } |
| 254 | else if (ver_num1 > ver_num2) { | ||
| 255 | return(1); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | return(0); | ||
| 259 | } | ||
| 260 | |||
| 261 | /* if ver1 < ver2 return -1, | ||
| 262 | * if ver1 = ver2 return 0, | ||
| 263 | * if ver1 > ver2 return 1, | ||
| 264 | */ | ||
| 265 | int version_compare(const unsigned int ver1, const unsigned int ver2) | ||
| 266 | { | ||
| 267 | char *ch_ver1 = name_hashtable[ver1]; | ||
| 268 | char *ch_ver2 = name_hashtable[ver2]; | ||
| 269 | |||
| 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); | ||
| 208 | } | 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); | ||
| 209 | 318 | ||
| 210 | /* Leaks the old list... return the new one... */ | 319 | if (result != 0) { |
| 211 | return ordered; | 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); | ||
| 212 | } | 360 | } |
| 213 | 361 | ||
| 214 | 362 | ||
| 215 | /* resolve package dependencies -- | 363 | int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator) |
| 216 | * for each package in the list of packages to be installed, we parse its | ||
| 217 | * dependency info to determine if the dependent packages are either | ||
| 218 | * already installed, or are scheduled to be installed. If both tests fail | ||
| 219 | * than bail. | ||
| 220 | * | ||
| 221 | * The algorithm here is O(n^2*m) where n = number of packages to be | ||
| 222 | * installed and m is the # of dependencies per package. Not a terribly | ||
| 223 | * efficient algorithm, but given that at any one time you are unlikely | ||
| 224 | * to install a very large number of packages it doesn't really matter | ||
| 225 | */ | ||
| 226 | static package_t *depends_resolve(package_t *pkgs, void *status) | ||
| 227 | { | 364 | { |
| 228 | package_t *pkg, *chk; | 365 | unsigned int probe_address = 0; |
| 229 | package_t dependpkg; | 366 | unsigned int probe_decrement = 0; |
| 230 | char **dependsvec; | 367 | |
| 231 | int i; | 368 | make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME); |
| 232 | void *found; | 369 | while(package_hashtable[probe_address] != NULL) { |
| 233 | 370 | if (package_hashtable[probe_address]->name == name) { | |
| 234 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 371 | if (operator == VER_ANY) { |
| 235 | dependsvec = depends_split(pkg->depends); | 372 | return(probe_address); |
| 236 | i = 0; | 373 | } |
| 237 | while (dependsvec[i] != 0) { | 374 | if (test_version(package_hashtable[probe_address]->version, version, operator)) { |
| 238 | /* Check for dependencies; first look for installed packages */ | 375 | return(probe_address); |
| 239 | dependpkg.package = dependsvec[i]; | ||
| 240 | if (((found = tfind(&dependpkg, &status, package_compare)) == 0) || | ||
| 241 | ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) && | ||
| 242 | (chk->state_status & state_status_installed))) { | ||
| 243 | |||
| 244 | /* if it fails, we look through the list of packages we are going to | ||
| 245 | * install */ | ||
| 246 | for (chk = pkgs; chk != 0; chk = chk->next) { | ||
| 247 | if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && | ||
| 248 | strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) { | ||
| 249 | if (chk->requiredcount >= DEPENDSMAX) { | ||
| 250 | error_msg("Too many dependencies for %s", chk->package); | ||
| 251 | return 0; | ||
| 252 | } | ||
| 253 | if (chk != pkg) { | ||
| 254 | chk->requiredfor[chk->requiredcount++] = pkg; | ||
| 255 | } | ||
| 256 | break; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | if (chk == 0) { | ||
| 260 | error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]); | ||
| 261 | return 0; | ||
| 262 | } | ||
| 263 | } | 376 | } |
| 264 | i++; | 377 | } |
| 378 | probe_address -= probe_decrement; | ||
| 379 | if ((int)probe_address < 0) { | ||
| 380 | probe_address += PACKAGE_HASH_PRIME; | ||
| 265 | } | 381 | } |
| 266 | } | 382 | } |
| 267 | 383 | return(probe_address); | |
| 268 | return depends_sort(pkgs); | ||
| 269 | } | 384 | } |
| 270 | #endif | 385 | |
| 271 | 386 | /* | |
| 272 | /* Status file handling routines | 387 | * Create one new node and one new edge for every dependency. |
| 273 | * | ||
| 274 | * This is a fairly minimalistic implementation. there are two main functions | ||
| 275 | * that are supported: | ||
| 276 | * | ||
| 277 | * 1) reading the entire status file: | ||
| 278 | * the status file is read into memory as a binary-tree, with just the | ||
| 279 | * package and status info preserved | ||
| 280 | * | ||
| 281 | * 2) merging the status file | ||
| 282 | * control info from (new) packages is merged into the status file, | ||
| 283 | * replacing any pre-existing entries. when a merge happens, status info | ||
| 284 | * read using the status_read function is written back to the status file | ||
| 285 | */ | 388 | */ |
| 286 | static unsigned char status_parse(const char *line, const char **status_words) | 389 | void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) |
| 287 | { | 390 | { |
| 288 | unsigned char status_num; | 391 | char *line = xstrdup(whole_line); |
| 289 | int i = 0; | 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); | ||
| 290 | 476 | ||
| 291 | while (status_words[i] != 0) { | 477 | return; |
| 292 | if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) { | 478 | } |
| 293 | status_num = (char)i; | 479 | |
| 294 | return(status_num); | 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); | ||
| 295 | } | 494 | } |
| 296 | i++; | ||
| 297 | } | 495 | } |
| 298 | /* parse error */ | ||
| 299 | error_msg("Invalid status word"); | ||
| 300 | return(0); | ||
| 301 | } | 496 | } |
| 302 | 497 | ||
| 303 | /* | 498 | unsigned int fill_package_struct(char *control_buffer) |
| 304 | * Read the buffered control file and parse it, | ||
| 305 | * filling parsed fields into the package structure | ||
| 306 | */ | ||
| 307 | static int fill_package_struct(package_t *package, const char *package_buffer) | ||
| 308 | { | 499 | { |
| 309 | char *field = NULL; | 500 | common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t)); |
| 501 | |||
| 502 | char *field; | ||
| 503 | char *field_name; | ||
| 504 | char *field_value; | ||
| 310 | int field_start = 0; | 505 | int field_start = 0; |
| 311 | int field_length = 0; | 506 | int field_length; |
| 507 | int seperator_offset; | ||
| 508 | int num = -1; | ||
| 509 | int buffer_length = strlen(control_buffer); | ||
| 510 | |||
| 511 | new_node->version = search_name_hashtable("unknown"); | ||
| 512 | while (field_start < buffer_length) { | ||
| 513 | field = read_package_field(&control_buffer[field_start]); | ||
| 312 | 514 | ||
| 313 | while ((field = read_package_field(&package_buffer[field_start])) != NULL) { | 515 | /* Setup start point for next field */ |
| 314 | field_length = strlen(field); | 516 | field_length = strlen(field); |
| 315 | field_start += (field_length + 1); | 517 | field_start += (field_length + 1); |
| 316 | 518 | ||
| 317 | if (strlen(field) == 0) { | 519 | seperator_offset = strcspn(field, ":"); |
| 318 | printf("empty line: *this shouldnt happen i dont think*\n"); | 520 | field_name = xstrndup(field, seperator_offset); |
| 319 | break; | 521 | field_value = field + seperator_offset + 1; |
| 320 | } | 522 | field_value += strspn(field_value, " \n\t"); |
| 321 | 523 | ||
| 322 | /* these are common to both installed and uninstalled packages */ | 524 | if (strcmp(field_name, "Package") == 0) { |
| 323 | if (strstr(field, "Package: ") == field) { | 525 | new_node->name = search_name_hashtable(field_value); |
| 324 | package->package = strdup(field + 9); | ||
| 325 | } | ||
| 326 | else if (strstr(field, "Depends: ") == field) { | ||
| 327 | package->depends = strdup(field + 9); | ||
| 328 | } | ||
| 329 | else if (strstr(field, "Provides: ") == field) { | ||
| 330 | package->provides = strdup(field + 10); | ||
| 331 | } | 526 | } |
| 332 | /* This is specific to the Debian Installer. Ifdef? */ | 527 | else if (strcmp(field_name, "Version") == 0) { |
| 333 | else if (strstr(field, "installer-menu-item: ") == field) { | 528 | new_node->version = search_name_hashtable(field_value); |
| 334 | package->installer_menu_item = atoi(field + 21); | ||
| 335 | } | 529 | } |
| 336 | else if (strstr(field, "Description: ") == field) { | 530 | else if (strcmp(field_name, "Pre-Depends") == 0) { |
| 337 | package->description = strdup(field + 13); | 531 | add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS); |
| 338 | } | 532 | } |
| 339 | else if (strstr(field, "Priority: ") == field) { | 533 | else if (strcmp(field_name, "Depends") == 0) { |
| 340 | package->priority = strdup(field + 10); | 534 | add_split_dependencies(new_node, field_value, EDGE_DEPENDS); |
| 341 | } | 535 | } |
| 342 | else if (strstr(field, "Section: ") == field) { | 536 | else if (strcmp(field_name, "Replaces") == 0) { |
| 343 | package->section = strdup(field + 9); | 537 | add_split_dependencies(new_node, field_value, EDGE_REPLACES); |
| 344 | } | 538 | } |
| 345 | else if (strstr(field, "Installed-Size: ") == field) { | 539 | else if (strcmp(field_name, "Provides") == 0) { |
| 346 | package->installed_size = strdup(field + 16); | 540 | add_split_dependencies(new_node, field_value, EDGE_PROVIDES); |
| 347 | } | 541 | } |
| 348 | else if (strstr(field, "Maintainer: ") == field) { | 542 | else if (strcmp(field_name, "Conflicts") == 0) { |
| 349 | package->maintainer = strdup(field + 12); | 543 | add_split_dependencies(new_node, field_value, EDGE_CONFLICTS); |
| 350 | } | 544 | } |
| 351 | else if (strstr(field, "Version: ") == field) { | 545 | else if (strcmp(field_name, "Suggests") == 0) { |
| 352 | package->version = strdup(field + 9); | 546 | add_split_dependencies(new_node, field_value, EDGE_SUGGESTS); |
| 353 | } | ||
| 354 | else if (strstr(field, "Suggests: ") == field) { | ||
| 355 | package->suggests = strdup(field + 10); | ||
| 356 | } | ||
| 357 | else if (strstr(field, "Recommends: ") == field) { | ||
| 358 | package->recommends = strdup(field + 12); | ||
| 359 | } | ||
| 360 | /* else if (strstr(field, "Conffiles: ") == field) { | ||
| 361 | package->conffiles = read_block(file); | ||
| 362 | package->conffiles = xcalloc(1, 1); | ||
| 363 | while ((field = strtok(NULL, "\n")) != NULL) { | ||
| 364 | package->long_description = xrealloc(package->conffiles, | ||
| 365 | strlen(package->conffiles) + strlen(field) + 1); | ||
| 366 | strcat(package->conffiles, field); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | */ | ||
| 370 | /* These are only in available file */ | ||
| 371 | else if (strstr(field, "Architecture: ") == field) { | ||
| 372 | package->architecture = strdup(field + 14); | ||
| 373 | } | 547 | } |
| 374 | else if (strstr(field, "Filename: ") == field) { | 548 | else if (strcmp(field_name, "Recommends") == 0) { |
| 375 | package->filename = strdup(field + 10); | 549 | add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS); |
| 376 | } | 550 | } |
| 377 | else if (strstr(field, "MD5sum ") == field) { | 551 | else if (strcmp(field_name, "Enhances") == 0) { |
| 378 | package->md5sum = strdup(field + 7); | 552 | add_split_dependencies(new_node, field_value, EDGE_ENHANCES); |
| 379 | } | 553 | } |
| 380 | 554 | free(field_name); | |
| 381 | /* This is only needed for status file */ | ||
| 382 | if (strstr(field, "Status: ") == field) { | ||
| 383 | char *word_pointer; | ||
| 384 | |||
| 385 | word_pointer = strchr(field, ' ') + 1; | ||
| 386 | package->state_want = status_parse(word_pointer, state_words_want); | ||
| 387 | word_pointer = strchr(word_pointer, ' ') + 1; | ||
| 388 | package->state_flag = status_parse(word_pointer, state_words_flag); | ||
| 389 | word_pointer = strchr(word_pointer, ' ') + 1; | ||
| 390 | package->state_status = status_parse(word_pointer, state_words_status); | ||
| 391 | } else { | ||
| 392 | package->state_want = status_parse("purge", state_words_want); | ||
| 393 | package->state_flag = status_parse("ok", state_words_flag); | ||
| 394 | package->state_status = status_parse("not-installed", state_words_status); | ||
| 395 | } | ||
| 396 | |||
| 397 | free(field); | 555 | free(field); |
| 398 | } | 556 | } |
| 399 | return EXIT_SUCCESS; | 557 | if (new_node->version == search_name_hashtable("unknown")) { |
| 558 | free_package(new_node); | ||
| 559 | return(-1); | ||
| 560 | } | ||
| 561 | num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL); | ||
| 562 | if (package_hashtable[num] == NULL) { | ||
| 563 | package_hashtable[num] = new_node; | ||
| 564 | } else { | ||
| 565 | free_package(new_node); | ||
| 566 | } | ||
| 567 | return(num); | ||
| 400 | } | 568 | } |
| 401 | 569 | ||
| 402 | extern void write_package(FILE *out_file, package_t *pkg) | 570 | /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */ |
| 571 | unsigned int get_status(const unsigned int status_node, const int num) | ||
| 403 | { | 572 | { |
| 404 | if (pkg->package) { | 573 | char *status_string = name_hashtable[status_hashtable[status_node]->status]; |
| 405 | fprintf(out_file, "Package: %s\n", pkg->package); | 574 | char *state_sub_string; |
| 406 | } | 575 | unsigned int state_sub_num; |
| 407 | if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) { | 576 | int len; |
| 408 | fprintf(out_file, "Status: %s %s %s\n", | 577 | int i; |
| 409 | state_words_want[pkg->state_want - 1], | 578 | |
| 410 | state_words_flag[pkg->state_flag - 1], | 579 | /* set tmp_string to point to the start of the word number */ |
| 411 | state_words_status[pkg->state_status - 1]); | 580 | for (i = 1; i < num; i++) { |
| 412 | } | 581 | /* skip past a word */ |
| 413 | if (pkg->depends) { | 582 | status_string += strcspn(status_string, " "); |
| 414 | fprintf(out_file, "Depends: %s\n", pkg->depends); | 583 | /* skip past the seperating spaces */ |
| 415 | } | 584 | status_string += strspn(status_string, " "); |
| 416 | if (pkg->provides) { | ||
| 417 | fprintf(out_file, "Provides: %s\n", pkg->provides); | ||
| 418 | } | ||
| 419 | if (pkg->priority) { | ||
| 420 | fprintf(out_file, "Priority: %s\n", pkg->priority); | ||
| 421 | } | ||
| 422 | if (pkg->section) { | ||
| 423 | fprintf(out_file, "Section: %s\n", pkg->section); | ||
| 424 | } | ||
| 425 | if (pkg->section) { | ||
| 426 | fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size); | ||
| 427 | } | ||
| 428 | if (pkg->maintainer) { | ||
| 429 | fprintf(out_file, "Maintainer: %s\n", pkg->maintainer); | ||
| 430 | } | ||
| 431 | if (pkg->source) { | ||
| 432 | fprintf(out_file, "Source: %s\n", pkg->source); | ||
| 433 | } | ||
| 434 | if (pkg->version) { | ||
| 435 | fprintf(out_file, "Version: %s\n", pkg->version); | ||
| 436 | } | ||
| 437 | if (pkg->pre_depends) { | ||
| 438 | fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends); | ||
| 439 | } | ||
| 440 | if (pkg->replaces) { | ||
| 441 | fprintf(out_file, "Replaces: %s\n", pkg->replaces); | ||
| 442 | } | ||
| 443 | if (pkg->recommends) { | ||
| 444 | fprintf(out_file, "Recommends: %s\n", pkg->recommends); | ||
| 445 | } | ||
| 446 | if (pkg->suggests) { | ||
| 447 | fprintf(out_file, "Suggests: %s\n", pkg->suggests); | ||
| 448 | } | ||
| 449 | if (pkg->conflicts) { | ||
| 450 | fprintf(out_file, "Conflicts: %s\n", pkg->conflicts); | ||
| 451 | } | ||
| 452 | if (pkg->conffiles) { | ||
| 453 | fprintf(out_file, "Conf-files: %s\n", pkg->conffiles); | ||
| 454 | } | ||
| 455 | if (pkg->architecture) { | ||
| 456 | fprintf(out_file, "Architecture: %s\n", pkg->architecture); | ||
| 457 | } | ||
| 458 | if (pkg->filename) { | ||
| 459 | fprintf(out_file, "Filename: %s\n", pkg->filename); | ||
| 460 | } | ||
| 461 | if (pkg->md5sum) { | ||
| 462 | fprintf(out_file, "MD5sum: %s\n", pkg->md5sum); | ||
| 463 | } | 585 | } |
| 464 | if (pkg->installer_menu_item) { | 586 | len = strcspn(status_string, " \n\0"); |
| 465 | fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item); | 587 | state_sub_string = xstrndup(status_string, len); |
| 588 | state_sub_num = search_name_hashtable(state_sub_string); | ||
| 589 | free(state_sub_string); | ||
| 590 | return(state_sub_num); | ||
| 591 | } | ||
| 592 | |||
| 593 | void set_status(const unsigned int status_node_num, const char *new_value, const int position) | ||
| 594 | { | ||
| 595 | const unsigned int new_value_len = strlen(new_value); | ||
| 596 | const unsigned int new_value_num = search_name_hashtable(new_value); | ||
| 597 | unsigned int want = get_status(status_node_num, 1); | ||
| 598 | unsigned int flag = get_status(status_node_num, 2); | ||
| 599 | unsigned int status = get_status(status_node_num, 3); | ||
| 600 | int want_len = strlen(name_hashtable[want]); | ||
| 601 | int flag_len = strlen(name_hashtable[flag]); | ||
| 602 | int status_len = strlen(name_hashtable[status]); | ||
| 603 | char *new_status; | ||
| 604 | |||
| 605 | switch (position) { | ||
| 606 | case (1): | ||
| 607 | want = new_value_num; | ||
| 608 | want_len = new_value_len; | ||
| 609 | break; | ||
| 610 | case (2): | ||
| 611 | flag = new_value_num; | ||
| 612 | flag_len = new_value_len; | ||
| 613 | break; | ||
| 614 | case (3): | ||
| 615 | status = new_value_num; | ||
| 616 | status_len = new_value_len; | ||
| 617 | break; | ||
| 618 | default: | ||
| 619 | error_msg_and_die("DEBUG ONLY: this shouldnt happen"); | ||
| 466 | } | 620 | } |
| 467 | if (pkg->description) { | 621 | |
| 468 | fprintf(out_file, "Description: %s\n", pkg->description); | 622 | new_status = (char *) xmalloc(want_len + flag_len + status_len + 3); |
| 623 | sprintf(new_status, "%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]); | ||
| 624 | status_hashtable[status_node_num]->status = search_name_hashtable(new_status); | ||
| 625 | return; | ||
| 626 | } | ||
| 627 | |||
| 628 | void index_status_file(const char *filename) | ||
| 629 | { | ||
| 630 | FILE *status_file; | ||
| 631 | char *control_buffer; | ||
| 632 | char *status_line; | ||
| 633 | status_node_t *status_node = NULL; | ||
| 634 | unsigned int status_num; | ||
| 635 | |||
| 636 | status_file = fopen(filename, "r"); | ||
| 637 | while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { | ||
| 638 | const unsigned int package_num = fill_package_struct(control_buffer); | ||
| 639 | if (package_num != -1) { | ||
| 640 | status_node = xmalloc(sizeof(status_node_t)); | ||
| 641 | /* fill_package_struct doesnt handle the status field */ | ||
| 642 | status_line = strstr(control_buffer, "Status:"); | ||
| 643 | if (status_line != NULL) { | ||
| 644 | status_line += 7; | ||
| 645 | status_line += strspn(status_line, " \n\t"); | ||
| 646 | status_line = xstrndup(status_line, strcspn(status_line, "\n\0")); | ||
| 647 | status_node->status = search_name_hashtable(status_line); | ||
| 648 | free(status_line); | ||
| 649 | } | ||
| 650 | status_node->package = package_num; | ||
| 651 | status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]); | ||
| 652 | status_hashtable[status_num] = status_node; | ||
| 653 | } | ||
| 654 | free(control_buffer); | ||
| 469 | } | 655 | } |
| 470 | fputc('\n', out_file); | 656 | fclose(status_file); |
| 471 | pkg = pkg->next; | 657 | return; |
| 472 | } | 658 | } |
| 473 | 659 | ||
| 474 | static void *status_read(void) | 660 | |
| 661 | char *get_depends_field(common_node_t *package, const int depends_type) | ||
| 475 | { | 662 | { |
| 476 | FILE *f; | 663 | char *depends = NULL; |
| 477 | void *status = 0; | 664 | char *old_sep = (char *)xcalloc(1, 3); |
| 478 | package_t *m = 0, *p = 0, *t = 0; | 665 | char *new_sep = (char *)xcalloc(1, 3); |
| 479 | char *package_control_buffer = NULL; | 666 | int line_size = 0; |
| 667 | int depends_size; | ||
| 480 | 668 | ||
| 481 | if (getenv(udpkg_quiet) == NULL) { | 669 | int i; |
| 482 | printf("(Reading database...)\n"); | ||
| 483 | } | ||
| 484 | 670 | ||
| 485 | if ((f = wfopen(statusfile, "r")) == NULL) { | 671 | for (i = 0; i < package->num_of_edges; i++) { |
| 486 | return(NULL); | 672 | if ((package->edge[i]->type == EDGE_OR_PRE_DEPENDS) || |
| 487 | } | 673 | (package->edge[i]->type == EDGE_OR_DEPENDS)) { |
| 674 | } | ||
| 488 | 675 | ||
| 489 | while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) { | 676 | if ((package->edge[i]->type == depends_type) || |
| 490 | m = (package_t *)xcalloc(1, sizeof(package_t)); | 677 | (package->edge[i]->type == depends_type + 1)) { |
| 491 | fill_package_struct(m, package_control_buffer); | 678 | /* Check if its the first time through */ |
| 492 | if (m->package) { | 679 | |
| 493 | /* | 680 | depends_size = 8 + strlen(name_hashtable[package->edge[i]->name]) |
| 494 | * If there is an item in the tree by this name, | 681 | + strlen(name_hashtable[package->edge[i]->version]); |
| 495 | * it must be a virtual package; insert real | 682 | line_size += depends_size; |
| 496 | * package in preference. | 683 | depends = (char *) xrealloc(depends, line_size + 1); |
| 497 | */ | 684 | |
| 498 | tdelete(m, &status, package_compare); | 685 | /* Check to see if this dependency is the type we are looking for |
| 499 | tsearch(m, &status, package_compare); | 686 | * +1 to check for 'extra' types, e.g. ored dependecies */ |
| 500 | if (m->provides) { | 687 | strcpy(old_sep, new_sep); |
| 501 | /* | 688 | if (package->edge[i]->type == depends_type) { |
| 502 | * A "Provides" triggers the insertion | 689 | strcpy(new_sep, ", "); |
| 503 | * of a pseudo package into the status | 690 | } |
| 504 | * binary-tree. | 691 | else if (package->edge[i]->type == depends_type + 1) { |
| 505 | */ | 692 | strcpy(new_sep, "| "); |
| 506 | p = (package_t *)xcalloc(1, sizeof(package_t)); | 693 | } |
| 507 | p->package = xstrdup(m->provides); | 694 | |
| 508 | t = *(package_t **)tsearch(p, &status, package_compare); | 695 | if (depends_size == line_size) { |
| 509 | if (t != p) { | 696 | strcpy(depends, ""); |
| 510 | free(p->package); | 697 | } else { |
| 511 | free(p); | 698 | if ((strcmp(old_sep, "| ") == 0) && (strcmp(new_sep, "| ") == 0)) { |
| 699 | strcat(depends, " | "); | ||
| 512 | } else { | 700 | } else { |
| 513 | /* | 701 | strcat(depends, ", "); |
| 514 | * Pseudo package status is the | ||
| 515 | * same as the status of the | ||
| 516 | * package providing it | ||
| 517 | * FIXME: (not quite right, if 2 | ||
| 518 | * packages of different statuses | ||
| 519 | * provide it). | ||
| 520 | */ | ||
| 521 | t->state_want = m->state_want; | ||
| 522 | t->state_flag = m->state_flag; | ||
| 523 | t->state_status = m->state_status; | ||
| 524 | } | 702 | } |
| 525 | } | 703 | } |
| 526 | } | 704 | |
| 527 | else { | 705 | strcat(depends, name_hashtable[package->edge[i]->name]); |
| 528 | free(m); | 706 | if (strcmp(name_hashtable[package->edge[i]->version], "NULL") != 0) { |
| 707 | if (package->edge[i]->operator == VER_EQUAL) { | ||
| 708 | strcat(depends, " (= "); | ||
| 709 | } | ||
| 710 | else if (package->edge[i]->operator == VER_LESS) { | ||
| 711 | strcat(depends, " (<< "); | ||
| 712 | } | ||
| 713 | else if (package->edge[i]->operator == VER_LESS_EQUAL) { | ||
| 714 | strcat(depends, " (<= "); | ||
| 715 | } | ||
| 716 | else if (package->edge[i]->operator == VER_MORE) { | ||
| 717 | strcat(depends, " (>> "); | ||
| 718 | } | ||
| 719 | else if (package->edge[i]->operator == VER_MORE_EQUAL) { | ||
| 720 | strcat(depends, " (>= "); | ||
| 721 | } else { | ||
| 722 | strcat(depends, " ("); | ||
| 723 | } | ||
| 724 | strcat(depends, name_hashtable[package->edge[i]->version]); | ||
| 725 | strcat(depends, ")"); | ||
| 726 | } | ||
| 529 | } | 727 | } |
| 530 | } | 728 | } |
| 531 | fclose(f); | 729 | return(depends); |
| 532 | return status; | ||
| 533 | } | 730 | } |
| 534 | 731 | ||
| 535 | static int status_merge(void *status, package_t *pkgs) | 732 | /* This could do with a cleanup */ |
| 733 | void write_status_file(deb_file_t **deb_file) | ||
| 536 | { | 734 | { |
| 537 | FILE *fin, *fout; | 735 | FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r"); |
| 538 | char *line = NULL; | 736 | FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w"); |
| 539 | package_t *pkg = 0, *statpkg = 0; | 737 | char *package_name; |
| 540 | package_t locpkg; | 738 | char *status_from_file; |
| 739 | char *control_buffer = NULL; | ||
| 740 | char *tmp_string; | ||
| 741 | char *field; | ||
| 742 | int status_num; | ||
| 743 | int field_length; | ||
| 744 | int field_start = 0; | ||
| 745 | int buffer_length; | ||
| 746 | int write_flag; | ||
| 747 | int i = 0; | ||
| 541 | 748 | ||
| 542 | if ((fout = wfopen(new_statusfile, "w")) == NULL) { | 749 | /* Update previously known packages */ |
| 543 | return 0; | 750 | while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) { |
| 544 | } | 751 | tmp_string = strstr(control_buffer, "Package:") + 8; |
| 545 | if (getenv(udpkg_quiet) == NULL) { | 752 | tmp_string += strspn(tmp_string, " \n\t"); |
| 546 | printf("(Updating database...)\n"); | 753 | package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0")); |
| 547 | } | 754 | write_flag = FALSE; |
| 755 | |||
| 756 | tmp_string = strstr(control_buffer, "Status:"); | ||
| 757 | if (tmp_string != NULL) { | ||
| 758 | /* Seperate the status value from the control buffer */ | ||
| 759 | tmp_string += 7; | ||
| 760 | tmp_string += strspn(tmp_string, " \n\t"); | ||
| 761 | status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n")); | ||
| 762 | } else { | ||
| 763 | status_from_file = NULL; | ||
| 764 | } | ||
| 548 | 765 | ||
| 549 | /* | 766 | /* Find this package in the status hashtable */ |
| 550 | * Dont use wfopen here, handle errors ourself | 767 | status_num = search_status_hashtable(package_name); |
| 551 | */ | 768 | if (status_hashtable[status_num] != NULL) { |
| 552 | if ((fin = fopen(statusfile, "r")) != NULL) { | 769 | const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status]; |
| 553 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { | 770 | if (strcmp(status_from_file, status_from_hashtable) != 0) { |
| 554 | chomp(line); /* trim newline */ | 771 | /* New status isnt exactly the same as old status */ |
| 555 | /* If we see a package header, find out if it's a package | 772 | const int state_status = get_status(status_num, 3); |
| 556 | * that we have processed. if so, we skip that block for | 773 | if ((strcmp("installed", name_hashtable[state_status]) == 0) || |
| 557 | * now (write it at the end). | 774 | (strcmp("unpacked", name_hashtable[state_status]) == 0)) { |
| 558 | * | 775 | /* We need to add the control file from the package */ |
| 559 | * we also look at packages in the status cache and update | 776 | i = 0; |
| 560 | * their status fields | 777 | while(deb_file[i] != NULL) { |
| 561 | */ | 778 | if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) { |
| 562 | if (strstr(line, "Package: ") == line) { | 779 | char *last_char; |
| 563 | for (pkg = pkgs; pkg != 0 && strcmp(line + 9, | 780 | /* Write a status file entry with a modified status */ |
| 564 | pkg->package) != 0; pkg = pkg->next) ; | 781 | /* remove trailing \n's */ |
| 565 | 782 | while(1) { | |
| 566 | locpkg.package = line + 9; | 783 | last_char = last_char_is(deb_file[i]->control_file, '\n'); |
| 567 | statpkg = tfind(&locpkg, &status, package_compare); | 784 | if (last_char) { |
| 568 | 785 | *last_char = '\0'; | |
| 569 | /* note: statpkg should be non-zero, unless the status | 786 | } else { |
| 570 | * file was changed while we are processing (no locking | 787 | break; |
| 571 | * is currently done... | 788 | } |
| 572 | */ | 789 | } |
| 573 | if (statpkg != 0) { | 790 | fputs(deb_file[i]->control_file, new_status_file); |
| 574 | statpkg = *(package_t **)statpkg; | 791 | set_status(status_num, "ok", 2); |
| 792 | fprintf(new_status_file, "\nStatus: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); | ||
| 793 | write_flag = TRUE; | ||
| 794 | break; | ||
| 795 | } | ||
| 796 | i++; | ||
| 797 | } | ||
| 798 | /* This is temperary, debugging only */ | ||
| 799 | if (deb_file[i] == NULL) { | ||
| 800 | error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | else if (strcmp("not-installed", name_hashtable[state_status]) == 0) { | ||
| 804 | /* Only write the Package, Status, Priority and Section lines */ | ||
| 805 | fprintf(new_status_file, "Package: %s\n", package_name); | ||
| 806 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); | ||
| 807 | buffer_length = strlen(control_buffer); | ||
| 808 | while (field_start < buffer_length) { | ||
| 809 | field = read_package_field(&control_buffer[field_start]); | ||
| 810 | field_length = strlen(field); | ||
| 811 | field_start += (field_length + 1); | ||
| 812 | if (strncmp(field, "Priority:", 9) == 0) { | ||
| 813 | fprintf(new_status_file, "Priority:%s\n", field + 9); | ||
| 814 | } | ||
| 815 | if (strncmp(field, "Section:", 8) == 0) { | ||
| 816 | fprintf(new_status_file, "Section:%s\n", field + 8); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | write_flag = TRUE; | ||
| 820 | fputs("\n", new_status_file); | ||
| 821 | } | ||
| 822 | else if (strcmp("config-files", name_hashtable[state_status]) == 0) { | ||
| 823 | /* only change the status line */ | ||
| 824 | buffer_length = strlen(control_buffer); | ||
| 825 | while (field_start < buffer_length) { | ||
| 826 | field = read_package_field(&control_buffer[field_start]); | ||
| 827 | /* Setup start point for next field */ | ||
| 828 | field_length = strlen(field); | ||
| 829 | field_start += (field_length + 1); | ||
| 830 | if (strncmp(field, "Status:", 7) == 0) { | ||
| 831 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); | ||
| 832 | } else { | ||
| 833 | fprintf(new_status_file, "%s\n", field); | ||
| 834 | } | ||
| 835 | free(field); | ||
| 836 | } | ||
| 837 | write_flag = TRUE; | ||
| 838 | fputs("\n", new_status_file); | ||
| 575 | } | 839 | } |
| 576 | } | 840 | } |
| 577 | if (pkg != 0) { | 841 | } |
| 578 | continue; | 842 | |
| 579 | } | 843 | /* If the package from the status file wasnt handle above, do it now*/ |
| 580 | if (strstr(line, "Status: ") == line && statpkg != 0) { | 844 | if (write_flag == FALSE) { |
| 581 | snprintf(line, sizeof(line), "Status: %s %s %s", | 845 | fprintf(new_status_file, "%s\n\n", control_buffer); |
| 582 | state_words_want[statpkg->state_want - 1], | 846 | } |
| 583 | state_words_flag[statpkg->state_flag - 1], | 847 | |
| 584 | state_words_status[statpkg->state_status - 1]); | 848 | if (status_from_file != NULL) { |
| 849 | free(status_from_file); | ||
| 850 | } | ||
| 851 | free(package_name); | ||
| 852 | free(control_buffer); | ||
| 853 | } | ||
| 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) { | ||
| 858 | char *last_char; | ||
| 859 | /* remove trailing \n's */ | ||
| 860 | while(1) { | ||
| 861 | last_char = last_char_is(deb_file[i]->control_file, '\n'); | ||
| 862 | if (last_char) { | ||
| 863 | *last_char = '\0'; | ||
| 864 | } else { | ||
| 865 | break; | ||
| 866 | } | ||
| 585 | } | 867 | } |
| 586 | fprintf(fout, "%s\n", line); | 868 | |
| 869 | fputs(deb_file[i]->control_file, new_status_file); | ||
| 870 | set_status(status_num, "ok", 2); | ||
| 871 | fprintf(new_status_file, "\nStatus: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); | ||
| 587 | } | 872 | } |
| 588 | fclose(fin); | ||
| 589 | } | 873 | } |
| 590 | free(line); | 874 | fclose(old_status_file); |
| 875 | fclose(new_status_file); | ||
| 591 | 876 | ||
| 592 | // Print out packages we processed. | ||
| 593 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | ||
| 594 | write_package(fout, pkg); | ||
| 595 | } | ||
| 596 | fclose(fout); | ||
| 597 | 877 | ||
| 598 | /* | 878 | /* Create a seperate backfile to dpkg */ |
| 599 | * Its ok if renaming statusfile fails becasue it doesnt exist | 879 | if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) { |
| 600 | */ | ||
| 601 | if (rename(statusfile, bak_statusfile) == -1) { | ||
| 602 | struct stat stat_buf; | 880 | struct stat stat_buf; |
| 603 | if (stat(statusfile, &stat_buf) == 0) { | 881 | if (stat("/var/lib/dpkg/status", &stat_buf) == 0) { |
| 604 | error_msg("Couldnt create backup status file"); | 882 | error_msg_and_die("Couldnt create backup status file"); |
| 605 | return(EXIT_FAILURE); | ||
| 606 | } | 883 | } |
| 884 | /* Its ok if renaming the status file fails becasue status | ||
| 885 | * file doesnt exist, maybe we are starting from scratch */ | ||
| 607 | error_msg("No status file found, creating new one"); | 886 | error_msg("No status file found, creating new one"); |
| 608 | } | 887 | } |
| 609 | 888 | ||
| 610 | if (rename(new_statusfile, statusfile) == -1) { | 889 | if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) { |
| 611 | error_msg("Couldnt create status file"); | 890 | error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file"); |
| 612 | return(EXIT_FAILURE); | ||
| 613 | } | 891 | } |
| 614 | return(EXIT_SUCCESS); | ||
| 615 | } | 892 | } |
| 616 | 893 | ||
| 617 | static int is_file(const char *fn) | 894 | int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count) |
| 618 | { | 895 | { |
| 619 | struct stat statbuf; | 896 | int *conflicts = NULL; |
| 897 | int conflicts_num = 0; | ||
| 898 | int state_status; | ||
| 899 | int state_flag; | ||
| 900 | int state_want; | ||
| 901 | unsigned int status_package_num; | ||
| 902 | int i = deb_start; | ||
| 903 | int j, k; | ||
| 904 | |||
| 905 | /* Check for conflicts | ||
| 906 | * TODO: TEST if conflicts with other packages to be installed | ||
| 907 | * | ||
| 908 | * Add install packages and the packages they provide | ||
| 909 | * to the list of files to check conflicts for | ||
| 910 | */ | ||
| 620 | 911 | ||
| 621 | if (stat(fn, &statbuf) < 0) { | 912 | /* Create array of package numbers to check against |
| 622 | return 0; | 913 | * installed package for conflicts*/ |
| 914 | while (deb_file[i] != NULL) { | ||
| 915 | const unsigned int package_num = deb_file[i]->package; | ||
| 916 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); | ||
| 917 | conflicts[conflicts_num] = package_num; | ||
| 918 | conflicts_num++; | ||
| 919 | /* add provides to conflicts list */ | ||
| 920 | for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) { | ||
| 921 | if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) { | ||
| 922 | const int conflicts_package_num = search_package_hashtable( | ||
| 923 | package_hashtable[package_num]->edge[j]->name, | ||
| 924 | package_hashtable[package_num]->edge[j]->version, | ||
| 925 | package_hashtable[package_num]->edge[j]->operator); | ||
| 926 | if (package_hashtable[conflicts_package_num] == NULL) { | ||
| 927 | /* create a new package */ | ||
| 928 | common_node_t *new_node = (common_node_t *) xmalloc(sizeof(common_node_t)); | ||
| 929 | new_node->name = package_hashtable[package_num]->edge[j]->name; | ||
| 930 | new_node->version = package_hashtable[package_num]->edge[j]->version; | ||
| 931 | new_node->num_of_edges = 0; | ||
| 932 | new_node->edge = NULL; | ||
| 933 | package_hashtable[conflicts_package_num] = new_node; | ||
| 934 | } | ||
| 935 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); | ||
| 936 | conflicts[conflicts_num] = conflicts_package_num; | ||
| 937 | conflicts_num++; | ||
| 938 | } | ||
| 939 | } | ||
| 940 | i++; | ||
| 623 | } | 941 | } |
| 624 | return S_ISREG(statbuf.st_mode); | 942 | |
| 943 | /* Check conflicts */ | ||
| 944 | for (i = 0; i < conflicts_num; i++) { | ||
| 945 | /* Check for conflicts */ | ||
| 946 | for (j = 0; j < STATUS_HASH_PRIME; j++) { | ||
| 947 | if (status_hashtable[j] == NULL) { | ||
| 948 | continue; | ||
| 949 | } | ||
| 950 | state_flag = get_status(j, 2); | ||
| 951 | state_status = get_status(j, 3); | ||
| 952 | if ((state_status != search_name_hashtable("installed")) | ||
| 953 | && (state_flag != search_name_hashtable("want-install"))) { | ||
| 954 | continue; | ||
| 955 | } | ||
| 956 | status_package_num = status_hashtable[j]->package; | ||
| 957 | for (k = 0; k < package_hashtable[status_package_num]->num_of_edges; k++) { | ||
| 958 | const edge_t *package_edge = package_hashtable[status_package_num]->edge[k]; | ||
| 959 | if (package_edge->type != EDGE_CONFLICTS) { | ||
| 960 | continue; | ||
| 961 | } | ||
| 962 | if (package_edge->name != package_hashtable[conflicts[i]]->name) { | ||
| 963 | continue; | ||
| 964 | } | ||
| 965 | /* There is a conflict against the package name | ||
| 966 | * check if version conflict as well */ | ||
| 967 | if (test_version(package_hashtable[deb_file[i]->package]->version, | ||
| 968 | package_edge->version, package_edge->operator)) { | ||
| 969 | error_msg_and_die("Package %s conflict with %s", | ||
| 970 | name_hashtable[package_hashtable[deb_file[i]->package]->name], | ||
| 971 | name_hashtable[package_hashtable[status_package_num]->name]); | ||
| 972 | } | ||
| 973 | } | ||
| 974 | } | ||
| 975 | } | ||
| 976 | |||
| 977 | /* Check dependendcies */ | ||
| 978 | i = 0; | ||
| 979 | while (deb_file[i] != NULL) { | ||
| 980 | const common_node_t *package_node = package_hashtable[deb_file[i]->package]; | ||
| 981 | int status_num = 0; | ||
| 982 | |||
| 983 | for (j = 0; j < package_hashtable[deb_file[i]->package]->num_of_edges; j++) { | ||
| 984 | const edge_t *package_edge = package_node->edge[j]; | ||
| 985 | const unsigned int package_num = search_package_hashtable(package_edge->name, | ||
| 986 | package_edge->version, package_edge->operator); | ||
| 987 | |||
| 988 | status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]); | ||
| 989 | state_status = get_status(status_num, 3); | ||
| 990 | state_want = get_status(status_num, 1); | ||
| 991 | switch (package_edge->type) { | ||
| 992 | case(EDGE_PRE_DEPENDS): | ||
| 993 | case(EDGE_OR_PRE_DEPENDS): | ||
| 994 | /* It must be already installed */ | ||
| 995 | /* NOTE: This is untested, nothing apropriate in my status file */ | ||
| 996 | if ((package_hashtable[package_num] == NULL) || (state_status != search_name_hashtable("installed"))) { | ||
| 997 | error_msg_and_die("Package %s pre-depends on %s, but it is not installed", | ||
| 998 | name_hashtable[package_node->name], | ||
| 999 | name_hashtable[package_edge->name]); | ||
| 1000 | } | ||
| 1001 | break; | ||
| 1002 | case(EDGE_DEPENDS): | ||
| 1003 | case(EDGE_OR_DEPENDS): | ||
| 1004 | /* It must be already installed, or to be installed */ | ||
| 1005 | if ((package_hashtable[package_num] == NULL) || | ||
| 1006 | ((state_status != search_name_hashtable("installed")) && | ||
| 1007 | (state_want != search_name_hashtable("want_install")))) { | ||
| 1008 | error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed", | ||
| 1009 | name_hashtable[package_node->name], | ||
| 1010 | name_hashtable[package_edge->name]); | ||
| 1011 | } | ||
| 1012 | break; | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | i++; | ||
| 1016 | } | ||
| 1017 | return(TRUE); | ||
| 625 | } | 1018 | } |
| 626 | 1019 | ||
| 627 | static int dpkg_doconfigure(package_t *pkg) | 1020 | char **create_list(const char *filename) |
| 628 | { | 1021 | { |
| 629 | int r; | 1022 | FILE *list_stream; |
| 630 | char postinst[1024]; | 1023 | char **file_list = xmalloc(sizeof(char *)); |
| 631 | char buf[1024]; | 1024 | char *line = NULL; |
| 632 | 1025 | char *last_char; | |
| 633 | DPRINTF("Configuring %s\n", pkg->package); | 1026 | int length = 0; |
| 634 | pkg->state_status = 0; | 1027 | int count = 0; |
| 635 | snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package); | ||
| 636 | 1028 | ||
| 637 | if (is_file(postinst)) { | 1029 | list_stream = fopen(filename, "r"); |
| 638 | snprintf(buf, sizeof(buf), "%s configure", postinst); | 1030 | if (list_stream == NULL) { |
| 639 | if ((r = do_system(buf)) != 0) { | 1031 | return(NULL); |
| 640 | error_msg("postinst exited with status %d\n", r); | 1032 | } |
| 641 | pkg->state_status = state_status_halfconfigured; | 1033 | while (getline(&line, &length, list_stream) != -1) { |
| 642 | return 1; | 1034 | file_list = xrealloc(file_list, sizeof(char *) * (length + 1)); |
| 1035 | last_char = last_char_is(line, '\n'); | ||
| 1036 | if (last_char) { | ||
| 1037 | *last_char = '\0'; | ||
| 643 | } | 1038 | } |
| 1039 | file_list[count] = xstrdup(line); | ||
| 1040 | free(line); | ||
| 1041 | count++; | ||
| 1042 | length = 0; | ||
| 1043 | } | ||
| 1044 | fclose(list_stream); | ||
| 1045 | if (count == 0) { | ||
| 1046 | return(NULL); | ||
| 1047 | } else { | ||
| 1048 | file_list[count] = NULL; | ||
| 1049 | return(file_list); | ||
| 644 | } | 1050 | } |
| 645 | pkg->state_status = state_status_installed; | ||
| 646 | |||
| 647 | return 0; | ||
| 648 | } | 1051 | } |
| 649 | 1052 | ||
| 650 | static int dpkg_dounpack(package_t *pkg) | 1053 | /* maybe i should try and hook this into remove_file.c somehow */ |
| 1054 | int remove_file_array(char **remove_names, char **exclude_names) | ||
| 651 | { | 1055 | { |
| 652 | FILE *out_stream; | 1056 | struct stat path_stat; |
| 653 | char *info_prefix; | 1057 | int match_flag; |
| 654 | int status = TRUE; | 1058 | int remove_flag = FALSE; |
| 655 | int r = 0; | 1059 | int i,j; |
| 656 | |||
| 657 | DPRINTF("Unpacking %s\n", pkg->package); | ||
| 658 | 1060 | ||
| 659 | /* extract the data file */ | 1061 | if (remove_names == NULL) { |
| 660 | deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL); | 1062 | return(FALSE); |
| 1063 | } | ||
| 1064 | for (i = 0; remove_names[i] != NULL; i++) { | ||
| 1065 | match_flag = FALSE; | ||
| 1066 | if (exclude_names != NULL) { | ||
| 1067 | for (j = 0; exclude_names[j] != 0; j++) { | ||
| 1068 | if (strcmp(remove_names[i], exclude_names[j]) == 0) { | ||
| 1069 | match_flag = TRUE; | ||
| 1070 | break; | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | if (!match_flag) { | ||
| 1075 | if (lstat(remove_names[i], &path_stat) < 0) { | ||
| 1076 | continue; | ||
| 1077 | } | ||
| 1078 | if (S_ISDIR(path_stat.st_mode)) { | ||
| 1079 | if (rmdir(remove_names[i]) != -1) { | ||
| 1080 | remove_flag = TRUE; | ||
| 1081 | } | ||
| 1082 | } else { | ||
| 1083 | if (unlink(remove_names[i]) != -1) { | ||
| 1084 | remove_flag = TRUE; | ||
| 1085 | } | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | return(remove_flag); | ||
| 1090 | } | ||
| 661 | 1091 | ||
| 662 | /* extract the control files */ | 1092 | int run_package_script(const char *package_name, const char *script_type) |
| 663 | info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1); | 1093 | { |
| 664 | sprintf(info_prefix, "%s/%s.", infodir, pkg->package); | 1094 | struct stat path_stat; |
| 665 | deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); | 1095 | char *script_path; |
| 666 | 1096 | ||
| 667 | /* Create the list file */ | 1097 | script_path = xmalloc(strlen(package_name) + strlen(script_type) + 21); |
| 668 | strcat(info_prefix, "list"); | 1098 | sprintf(script_path, "/var/lib/dpkg/info/%s.%s", package_name, script_type); |
| 669 | out_stream = wfopen(info_prefix, "w"); | ||
| 670 | deb_extract(pkg->filename, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL); | ||
| 671 | fclose(out_stream); | ||
| 672 | 1099 | ||
| 673 | pkg->state_want = state_want_install; | 1100 | /* If the file doesnt exist is isnt a fatal */ |
| 674 | pkg->state_flag = state_flag_ok; | 1101 | if (lstat(script_path, &path_stat) < 0) { |
| 675 | 1102 | return(EXIT_SUCCESS); | |
| 676 | if (status == TRUE) { | ||
| 677 | pkg->state_status = state_status_unpacked; | ||
| 678 | } else { | 1103 | } else { |
| 679 | pkg->state_status = state_status_halfinstalled; | 1104 | return(system(script_path)); |
| 680 | } | 1105 | } |
| 681 | |||
| 682 | return r; | ||
| 683 | } | 1106 | } |
| 684 | 1107 | ||
| 685 | /* | 1108 | void all_control_list(char **remove_files, const char *package_name) |
| 686 | * Extract and parse the control file from control.tar.gz | ||
| 687 | */ | ||
| 688 | static int dpkg_read_control(package_t *pkg) | ||
| 689 | { | 1109 | { |
| 690 | FILE *pkg_file; | 1110 | const char *all_extensions[11] = {"preinst", "postinst", "prerm", "postrm", |
| 691 | char *control_buffer = NULL; | 1111 | "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL }; |
| 1112 | int i; | ||
| 692 | 1113 | ||
| 693 | if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) { | 1114 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 694 | return EXIT_FAILURE; | 1115 | for(i = 0; i < 10; i++) { |
| 1116 | remove_files[i] = xmalloc(strlen(package_name) + strlen(all_extensions[i]) + 21); | ||
| 1117 | sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, all_extensions[i]); | ||
| 695 | } | 1118 | } |
| 696 | control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); | 1119 | remove_files[10] = NULL; |
| 697 | fill_package_struct(pkg, control_buffer); | ||
| 698 | return EXIT_SUCCESS; | ||
| 699 | } | 1120 | } |
| 700 | 1121 | ||
| 701 | static int dpkg_unpack(package_t *pkgs, void *status) | 1122 | void remove_package(const unsigned int package_num) |
| 702 | { | 1123 | { |
| 703 | int r = 0; | 1124 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 704 | package_t *pkg; | 1125 | const unsigned int status_num = search_status_hashtable(package_name); |
| 705 | 1126 | const int package_name_length = strlen(package_name); | |
| 706 | for (pkg = pkgs; pkg != 0; pkg = pkg->next) { | 1127 | char **remove_files; |
| 707 | dpkg_read_control(pkg); | 1128 | char **exclude_files; |
| 708 | if ((r = dpkg_dounpack(pkg)) != 0 ) { | 1129 | char list_name[package_name_length + 25]; |
| 709 | break; | 1130 | char conffile_name[package_name_length + 30]; |
| 710 | } | 1131 | int return_value; |
| 1132 | |||
| 1133 | /* run prerm script */ | ||
| 1134 | return_value = run_package_script(package_name, "prem"); | ||
| 1135 | if (return_value == -1) { | ||
| 1136 | error_msg_and_die("script failed, prerm failure"); | ||
| 711 | } | 1137 | } |
| 712 | status_merge(status, pkgs); | ||
| 713 | 1138 | ||
| 714 | return r; | 1139 | /* Create a list of files to remove, and a seperate list of those to keep */ |
| 715 | } | 1140 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1141 | remove_files = create_list(list_name); | ||
| 716 | 1142 | ||
| 717 | static int dpkg_configure(package_t *pkgs, void *status) | 1143 | sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name); |
| 718 | { | 1144 | exclude_files = create_list(conffile_name); |
| 719 | int r = 0; | ||
| 720 | void *found; | ||
| 721 | package_t *pkg; | ||
| 722 | 1145 | ||
| 723 | for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) { | 1146 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 724 | found = tfind(pkg, &status, package_compare); | 1147 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 725 | 1148 | ||
| 726 | if (found == 0) { | 1149 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 727 | error_msg("Trying to configure %s, but it is not installed", pkg->package); | 1150 | remove_files = xmalloc(11); |
| 728 | r = 1; | 1151 | all_control_list(remove_files, package_name); |
| 729 | } | ||
| 730 | /* configure the package listed in the status file; | ||
| 731 | * not pkg, as we have info only for the latter | ||
| 732 | */ | ||
| 733 | else { | ||
| 734 | r = dpkg_doconfigure(*(package_t **)found); | ||
| 735 | } | ||
| 736 | } | ||
| 737 | status_merge(status, 0); | ||
| 738 | 1152 | ||
| 739 | return r; | 1153 | /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */ |
| 1154 | exclude_files = xmalloc(sizeof(char*) * 3); | ||
| 1155 | exclude_files[0] = xstrdup(conffile_name); | ||
| 1156 | exclude_files[1] = xmalloc(package_name_length + 27); | ||
| 1157 | sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name); | ||
| 1158 | exclude_files[2] = NULL; | ||
| 1159 | |||
| 1160 | remove_file_array(remove_files, exclude_files); | ||
| 1161 | |||
| 1162 | /* rename <package>.conffile to <package>.list */ | ||
| 1163 | rename(conffile_name, list_name); | ||
| 1164 | |||
| 1165 | /* Change package status */ | ||
| 1166 | set_status(status_num, "deinstall", 1); | ||
| 1167 | set_status(status_num, "config-files", 3); | ||
| 740 | } | 1168 | } |
| 741 | 1169 | ||
| 742 | static int dpkg_install(package_t *pkgs, void *status) | 1170 | void purge_package(const unsigned int package_num) |
| 743 | { | 1171 | { |
| 744 | package_t *p, *ordered = 0; | 1172 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
| 745 | 1173 | const unsigned int status_num = search_status_hashtable(package_name); | |
| 746 | /* Stage 1: parse all the control information */ | 1174 | char **remove_files; |
| 747 | for (p = pkgs; p != 0; p = p->next) { | 1175 | char **exclude_files; |
| 748 | dpkg_read_control(p); | 1176 | char list_name[strlen(package_name) + 25]; |
| 1177 | |||
| 1178 | /* run prerm script */ | ||
| 1179 | if (run_package_script(package_name, "prerm") == -1) { | ||
| 1180 | error_msg_and_die("script failed, prerm failure"); | ||
| 749 | } | 1181 | } |
| 750 | 1182 | ||
| 751 | /* Stage 2: resolve dependencies */ | 1183 | /* Create a list of files to remove */ |
| 752 | #ifdef DODEPENDS | 1184 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 753 | ordered = depends_resolve(pkgs, status); | 1185 | remove_files = create_list(list_name); |
| 754 | #else | ||
| 755 | ordered = pkgs; | ||
| 756 | #endif | ||
| 757 | |||
| 758 | /* Stage 3: install */ | ||
| 759 | for (p = ordered; p != 0; p = p->next) { | ||
| 760 | p->state_want = state_want_install; | ||
| 761 | 1186 | ||
| 762 | /* for now the flag is always set to ok... this is probably | 1187 | exclude_files = xmalloc(1); |
| 763 | * not what we want | 1188 | exclude_files[0] = NULL; |
| 764 | */ | ||
| 765 | p->state_flag = state_flag_ok; | ||
| 766 | 1189 | ||
| 767 | DPRINTF("Installing %s\n", p->package); | 1190 | /* Some directories cant be removed straight away, so do multiple passes */ |
| 768 | if (dpkg_dounpack(p) != 0) { | 1191 | while (remove_file_array(remove_files, exclude_files) == TRUE); |
| 769 | perror_msg(p->filename); | ||
| 770 | } | ||
| 771 | 1192 | ||
| 772 | if (dpkg_doconfigure(p) != 0) { | 1193 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 773 | perror_msg(p->filename); | 1194 | remove_files = xmalloc(11); |
| 774 | } | 1195 | all_control_list(remove_files, package_name); |
| 1196 | remove_file_array(remove_files, exclude_files); | ||
| 1197 | |||
| 1198 | /* run postrm script */ | ||
| 1199 | if (run_package_script(package_name, "postrm") == -1) { | ||
| 1200 | error_msg_and_die("postrm fialure.. set status to what?"); | ||
| 775 | } | 1201 | } |
| 1202 | /* Change package status */ | ||
| 1203 | set_status(status_num, "purge", 1); | ||
| 1204 | set_status(status_num, "not-installed", 3); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | void unpack_package(deb_file_t *deb_file) | ||
| 1208 | { | ||
| 1209 | const unsigned int package_name_num = package_hashtable[deb_file->package]->name; | ||
| 1210 | const char *package_name = name_hashtable[package_name_num]; | ||
| 1211 | const unsigned int status_num = search_status_hashtable(package_name); | ||
| 1212 | unsigned int status_package_num; | ||
| 776 | 1213 | ||
| 777 | if (ordered != 0) { | 1214 | FILE *out_stream; |
| 778 | status_merge(status, pkgs); | 1215 | char *info_prefix; |
| 1216 | |||
| 1217 | /* If existing version, remove it first */ | ||
| 1218 | if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) { | ||
| 1219 | /* Package is already installed, remove old version first */ | ||
| 1220 | printf("Preparing to replace %s %s (using %s) ...\n", package_name, | ||
| 1221 | name_hashtable[package_hashtable[status_package_num]->version], | ||
| 1222 | deb_file->filename); | ||
| 1223 | remove_package(status_package_num); | ||
| 1224 | } else { | ||
| 1225 | printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename); | ||
| 779 | } | 1226 | } |
| 780 | 1227 | ||
| 781 | return 0; | 1228 | /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */ |
| 1229 | info_prefix = (char *) xmalloc(sizeof(package_name) + 20 + 4 + 1); | ||
| 1230 | sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name); | ||
| 1231 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL); | ||
| 1232 | |||
| 1233 | /* Extract data.tar.gz to the root directory */ | ||
| 1234 | deb_extract(deb_file->filename, stdout, (extract_quiet | extract_data_tar_gz | extract_all_to_fs), "/", NULL); | ||
| 1235 | |||
| 1236 | /* Create the list file */ | ||
| 1237 | strcat(info_prefix, "list"); | ||
| 1238 | |||
| 1239 | out_stream = wfopen(info_prefix, "w"); | ||
| 1240 | deb_extract(deb_file->filename, out_stream, (extract_quiet | extract_data_tar_gz | extract_list), NULL, NULL); | ||
| 1241 | fclose(out_stream); | ||
| 1242 | |||
| 1243 | /* change status */ | ||
| 1244 | set_status(status_num, "install", 1); | ||
| 1245 | set_status(status_num, "unpacked", 3); | ||
| 782 | } | 1246 | } |
| 783 | 1247 | ||
| 784 | /* | 1248 | void configure_package(deb_file_t *deb_file) |
| 785 | * Not implemented yet | ||
| 786 | * | ||
| 787 | static int dpkg_remove(package_t *pkgs, void *status) | ||
| 788 | { | 1249 | { |
| 789 | package_t *p; | 1250 | int return_value; |
| 790 | 1251 | int status_num; | |
| 791 | for (p = pkgs; p != 0; p = p->next) | 1252 | |
| 792 | { | 1253 | /* Run the preinst prior to extracting */ |
| 1254 | return_value = run_package_script(name_hashtable[package_hashtable[deb_file->package]->name], "postinst"); | ||
| 1255 | if (return_value == -1) { | ||
| 1256 | /* TODO: handle failure gracefully */ | ||
| 1257 | error_msg_and_die("postrm fialure.. set status to what?"); | ||
| 793 | } | 1258 | } |
| 794 | status_merge(status, 0); | ||
| 795 | 1259 | ||
| 796 | return 0; | 1260 | /* Change status to reflect success */ |
| 1261 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file->package]->name]); | ||
| 1262 | set_status(status_num, "install", 1); | ||
| 1263 | set_status(status_num, "installed", 3); | ||
| 797 | } | 1264 | } |
| 798 | */ | ||
| 799 | 1265 | ||
| 800 | extern int dpkg_main(int argc, char **argv) | 1266 | extern int dpkg_main(int argc, char **argv) |
| 801 | { | 1267 | { |
| 802 | const int arg_install = 1; | 1268 | deb_file_t **deb_file = NULL; |
| 803 | const int arg_unpack = 2; | 1269 | status_node_t *status_node; |
| 804 | const int arg_configure = 4; | 1270 | char opt = 0; |
| 805 | 1271 | int package_num; | |
| 806 | package_t *p, *packages = NULL; | 1272 | int dpkg_opt = 0; |
| 807 | void *status = NULL; | 1273 | int deb_count = 0; |
| 808 | int opt = 0; | 1274 | int state_status; |
| 809 | int optflag = 0; | 1275 | int status_num; |
| 1276 | int i; | ||
| 810 | 1277 | ||
| 811 | while ((opt = getopt(argc, argv, "iruc")) != -1) { | 1278 | while ((opt = getopt(argc, argv, "cF:ilPru")) != -1) { |
| 812 | switch (opt) { | 1279 | switch (opt) { |
| 1280 | case 'c': | ||
| 1281 | dpkg_opt |= dpkg_opt_configure; | ||
| 1282 | dpkg_opt |= dpkg_opt_package_name; | ||
| 1283 | break; | ||
| 1284 | case 'F': // equivalent to --force in official dpkg | ||
| 1285 | if (strcmp(optarg, "depends") == 0) { | ||
| 1286 | dpkg_opt |= dpkg_opt_force_ignore_depends; | ||
| 1287 | } | ||
| 813 | case 'i': | 1288 | case 'i': |
| 814 | optflag |= arg_install; | 1289 | dpkg_opt |= dpkg_opt_install; |
| 1290 | dpkg_opt |= dpkg_opt_filename; | ||
| 815 | break; | 1291 | break; |
| 816 | case 'u': | 1292 | case 'l': |
| 817 | optflag |= arg_unpack; | 1293 | dpkg_opt |= dpkg_opt_list_installed; |
| 1294 | case 'P': | ||
| 1295 | dpkg_opt |= dpkg_opt_purge; | ||
| 1296 | dpkg_opt |= dpkg_opt_package_name; | ||
| 818 | break; | 1297 | break; |
| 819 | case 'c': | 1298 | case 'r': |
| 820 | optflag |= arg_configure; | 1299 | dpkg_opt |= dpkg_opt_remove; |
| 1300 | dpkg_opt |= dpkg_opt_package_name; | ||
| 1301 | break; | ||
| 1302 | case 'u': /* Equivalent to --unpack in official dpkg */ | ||
| 1303 | dpkg_opt |= dpkg_opt_unpack; | ||
| 1304 | dpkg_opt |= dpkg_opt_filename; | ||
| 821 | break; | 1305 | break; |
| 822 | default: | 1306 | default: |
| 823 | show_usage(); | 1307 | show_usage(); |
| 824 | } | 1308 | } |
| 825 | } | 1309 | } |
| 826 | 1310 | ||
| 1311 | if ((argc == optind) || (dpkg_opt == 0)) { | ||
| 1312 | show_usage(); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | puts("(Reading database ... xxxxx files and directories installed.)"); | ||
| 1316 | index_status_file("/var/lib/dpkg/status"); | ||
| 1317 | |||
| 1318 | /* Read arguments and store relevant info in structs */ | ||
| 1319 | deb_file = xmalloc(sizeof(deb_file_t)); | ||
| 827 | while (optind < argc) { | 1320 | while (optind < argc) { |
| 828 | p = (package_t *) xcalloc(1, sizeof(package_t)); | 1321 | deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t)); |
| 829 | if (optflag & arg_configure) { | 1322 | if (dpkg_opt & dpkg_opt_filename) { |
| 830 | p->package = xstrdup(argv[optind]); | 1323 | deb_file[deb_count]->filename = xstrdup(argv[optind]); |
| 831 | } else { | 1324 | deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); |
| 832 | p->filename = xstrdup(argv[optind]); | 1325 | if (deb_file[deb_count]->control_file == NULL) { |
| 1326 | error_msg_and_die("Couldnt extract control file"); | ||
| 1327 | } | ||
| 1328 | package_num = fill_package_struct(deb_file[deb_count]->control_file); | ||
| 1329 | |||
| 1330 | if (package_num == -1) { | ||
| 1331 | error_msg("Invalid control file in %s", argv[optind]); | ||
| 1332 | continue; | ||
| 1333 | } | ||
| 1334 | deb_file[deb_count]->package = (unsigned int) package_num; | ||
| 1335 | /* Add the package to the status hashtable */ | ||
| 1336 | if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) { | ||
| 1337 | status_node = (status_node_t *) xmalloc(sizeof(status_node_t)); | ||
| 1338 | status_node->package = deb_file[deb_count]->package; | ||
| 1339 | /* use reinstreq isnt changed to "ok" until the package control info | ||
| 1340 | * is written to the status file*/ | ||
| 1341 | status_node->status = search_name_hashtable("install reinstreq not-installed"); | ||
| 1342 | |||
| 1343 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1344 | status_hashtable[status_num] = status_node; | ||
| 1345 | } | ||
| 833 | } | 1346 | } |
| 834 | p->next = packages; | 1347 | else if (dpkg_opt & dpkg_opt_package_name) { |
| 835 | packages = p; | 1348 | deb_file[deb_count]->filename = NULL; |
| 1349 | deb_file[deb_count]->control_file = NULL; | ||
| 1350 | deb_file[deb_count]->package = search_package_hashtable( | ||
| 1351 | search_name_hashtable(argv[optind]), | ||
| 1352 | search_name_hashtable("ANY"), VER_ANY); | ||
| 1353 | if (package_hashtable[deb_file[deb_count]->package] == NULL) { | ||
| 1354 | error_msg_and_die("unknown package, %s\n", argv[optind]); | ||
| 1355 | } | ||
| 1356 | state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3); | ||
| 836 | 1357 | ||
| 1358 | /* check package status is "installed" */ | ||
| 1359 | if (dpkg_opt & dpkg_opt_remove) { | ||
| 1360 | if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || | ||
| 1361 | (strcmp(name_hashtable[state_status], "config-files") == 0)) { | ||
| 1362 | error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1363 | } | ||
| 1364 | } | ||
| 1365 | else if (dpkg_opt & dpkg_opt_purge) { | ||
| 1366 | /* if package status is "conf-files" then its ok */ | ||
| 1367 | if (strcmp(name_hashtable[state_status], "not-installed") == 0) { | ||
| 1368 | error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); | ||
| 1369 | } | ||
| 1370 | } | ||
| 1371 | } | ||
| 1372 | deb_count++; | ||
| 837 | optind++; | 1373 | optind++; |
| 838 | } | 1374 | } |
| 1375 | deb_file[deb_count] = NULL; | ||
| 839 | 1376 | ||
| 840 | make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR); | 1377 | /* Check that the deb file arguments are installable */ |
| 1378 | /* TODO: check dependencies before removing */ | ||
| 1379 | if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { | ||
| 1380 | if (!check_deps(deb_file, 0, deb_count)) { | ||
| 1381 | error_msg_and_die("Dependency check fialed"); | ||
| 1382 | } | ||
| 1383 | } | ||
| 841 | 1384 | ||
| 842 | status = status_read(); | 1385 | for (i = 0; i < deb_count; i++) { |
| 1386 | /* Remove or purge packages */ | ||
| 1387 | if (dpkg_opt & dpkg_opt_remove) { | ||
| 1388 | remove_package(deb_file[i]->package); | ||
| 1389 | } | ||
| 1390 | else if (dpkg_opt & dpkg_opt_purge) { | ||
| 1391 | purge_package(deb_file[i]->package); | ||
| 1392 | } | ||
| 1393 | else if (dpkg_opt & dpkg_opt_unpack) { | ||
| 1394 | unpack_package(deb_file[i]); | ||
| 1395 | } | ||
| 1396 | else if (dpkg_opt & dpkg_opt_install) { | ||
| 1397 | unpack_package(deb_file[i]); | ||
| 1398 | configure_package(deb_file[i]); | ||
| 1399 | } | ||
| 1400 | else if (dpkg_opt & dpkg_opt_configure) { | ||
| 1401 | configure_package(deb_file[i]); | ||
| 1402 | } | ||
| 1403 | } | ||
| 843 | 1404 | ||
| 844 | if (optflag & arg_install) { | 1405 | write_status_file(deb_file); |
| 845 | return dpkg_install(packages, status); | 1406 | |
| 1407 | for (i = 0; i < NAME_HASH_PRIME; i++) { | ||
| 1408 | if (name_hashtable[i] != NULL) { | ||
| 1409 | free(name_hashtable[i]); | ||
| 1410 | } | ||
| 846 | } | 1411 | } |
| 847 | else if (optflag & arg_unpack) { | 1412 | for (i = 0; i < PACKAGE_HASH_PRIME; i++) { |
| 848 | return dpkg_unpack(packages, status); | 1413 | free_package(package_hashtable[i]); |
| 849 | } | 1414 | } |
| 850 | else if (optflag & arg_configure) { | 1415 | for (i = 0; i < STATUS_HASH_PRIME; i++) { |
| 851 | return dpkg_configure(packages, status); | 1416 | if (status_hashtable[i] != NULL) { |
| 1417 | free(status_hashtable[i]); | ||
| 1418 | } | ||
| 852 | } | 1419 | } |
| 1420 | |||
| 853 | return(EXIT_FAILURE); | 1421 | return(EXIT_FAILURE); |
| 854 | } | 1422 | } |
| 1423 | |||
