3
0

dpkg.c 56 KB

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