dpkg.c 58 KB

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