dpkg.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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 source tree.
  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 "archive.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. ) {
  973. /* start an EDGE_OR_ list */
  974. number_of_alternatives = package_edge->version;
  975. root_of_alternatives = package_edge;
  976. continue;
  977. }
  978. if (number_of_alternatives == 0) { /* not in the middle of an EDGE_OR_ list */
  979. number_of_alternatives = 1;
  980. root_of_alternatives = NULL;
  981. }
  982. package_num = search_package_hashtable(package_edge->name, package_edge->version, package_edge->operator);
  983. if (package_edge->type == EDGE_PRE_DEPENDS
  984. || package_edge->type == EDGE_DEPENDS
  985. ) {
  986. int result=1;
  987. status_num = 0;
  988. /* If we are inside an alternative then check
  989. * this edge is the right type.
  990. *
  991. * EDGE_DEPENDS == OR_DEPENDS -1
  992. * EDGE_PRE_DEPENDS == OR_PRE_DEPENDS -1
  993. */
  994. if (root_of_alternatives && package_edge->type != root_of_alternatives->type - 1)
  995. bb_error_msg_and_die("fatal error, package dependencies corrupt: %d != %d - 1",
  996. package_edge->type, root_of_alternatives->type);
  997. if (package_hashtable[package_num] != NULL)
  998. result = !package_satisfies_dependency(package_num, package_edge->type);
  999. if (result) { /* check for other package which provide what we are looking for */
  1000. int provider = -1;
  1001. while ((provider = search_for_provides(package_edge->name, provider)) > -1) {
  1002. if (package_hashtable[provider] == NULL) {
  1003. puts("Have a provider but no package information for it");
  1004. continue;
  1005. }
  1006. result = !package_satisfies_dependency(provider, package_edge->type);
  1007. if (result == 0)
  1008. break;
  1009. }
  1010. }
  1011. /* It must be already installed, or to be installed */
  1012. number_of_alternatives--;
  1013. if (result && number_of_alternatives == 0) {
  1014. if (root_of_alternatives)
  1015. bb_error_msg_and_die(
  1016. "package %s %sdepends on %s, "
  1017. "which cannot be satisfied",
  1018. name_hashtable[package_node->name],
  1019. package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "",
  1020. name_hashtable[root_of_alternatives->name]);
  1021. bb_error_msg_and_die(
  1022. "package %s %sdepends on %s, which %s\n",
  1023. name_hashtable[package_node->name],
  1024. package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "",
  1025. name_hashtable[package_edge->name],
  1026. describe_status(status_num));
  1027. }
  1028. if (result == 0 && number_of_alternatives) {
  1029. /* we've found a package which
  1030. * satisfies the dependency,
  1031. * so skip over the rest of
  1032. * the alternatives.
  1033. */
  1034. j += number_of_alternatives;
  1035. number_of_alternatives = 0;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. free(conflicts);
  1041. return TRUE;
  1042. }
  1043. static char **create_list(const char *filename)
  1044. {
  1045. FILE *list_stream;
  1046. char **file_list;
  1047. char *line;
  1048. int count;
  1049. /* don't use [xw]fopen here, handle error ourself */
  1050. list_stream = fopen_for_read(filename);
  1051. if (list_stream == NULL) {
  1052. return NULL;
  1053. }
  1054. file_list = NULL;
  1055. count = 0;
  1056. while ((line = xmalloc_fgetline(list_stream)) != NULL) {
  1057. file_list = xrealloc_vector(file_list, 2, count);
  1058. file_list[count++] = line;
  1059. /*file_list[count] = NULL; - xrealloc_vector did it */
  1060. }
  1061. fclose(list_stream);
  1062. return file_list;
  1063. }
  1064. /* maybe i should try and hook this into remove_file.c somehow */
  1065. static int remove_file_array(char **remove_names, char **exclude_names)
  1066. {
  1067. struct stat path_stat;
  1068. int remove_flag = 1; /* not removed anything yet */
  1069. int i, j;
  1070. if (remove_names == NULL) {
  1071. return 0;
  1072. }
  1073. for (i = 0; remove_names[i] != NULL; i++) {
  1074. if (exclude_names != NULL) {
  1075. for (j = 0; exclude_names[j] != NULL; j++) {
  1076. if (strcmp(remove_names[i], exclude_names[j]) == 0) {
  1077. goto skip;
  1078. }
  1079. }
  1080. }
  1081. /* TODO: why we are checking lstat? we can just try rm/rmdir */
  1082. if (lstat(remove_names[i], &path_stat) < 0) {
  1083. continue;
  1084. }
  1085. if (S_ISDIR(path_stat.st_mode)) {
  1086. remove_flag &= rmdir(remove_names[i]); /* 0 if no error */
  1087. } else {
  1088. remove_flag &= unlink(remove_names[i]); /* 0 if no error */
  1089. }
  1090. skip:
  1091. continue;
  1092. }
  1093. return (remove_flag == 0);
  1094. }
  1095. static void run_package_script_or_die(const char *package_name, const char *script_type)
  1096. {
  1097. char *script_path;
  1098. int result;
  1099. script_path = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, script_type);
  1100. /* If the file doesnt exist is isnt fatal */
  1101. result = access(script_path, F_OK) ? EXIT_SUCCESS : system(script_path);
  1102. free(script_path);
  1103. if (result)
  1104. bb_error_msg_and_die("%s failed, exit code %d", script_type, result);
  1105. }
  1106. /*
  1107. The policy manual defines what scripts get called when and with
  1108. what arguments. I realize that busybox does not support all of
  1109. these scenarios, but it does support some of them; it does not,
  1110. however, run them with any parameters in run_package_script_or_die().
  1111. Here are the scripts:
  1112. preinst install
  1113. preinst install <old_version>
  1114. preinst upgrade <old_version>
  1115. preinst abort_upgrade <new_version>
  1116. postinst configure <most_recent_version>
  1117. postinst abort-upgade <new_version>
  1118. postinst abort-remove
  1119. postinst abort-remove in-favour <package> <version>
  1120. postinst abort-deconfigure in-favor <failed_install_package> removing <conflicting_package> <version>
  1121. prerm remove
  1122. prerm upgrade <new_version>
  1123. prerm failed-upgrade <old_version>
  1124. prerm remove in-favor <package> <new_version>
  1125. prerm deconfigure in-favour <package> <version> removing <package> <version>
  1126. postrm remove
  1127. postrm purge
  1128. postrm upgrade <new_version>
  1129. postrm failed-upgrade <old_version>
  1130. postrm abort-install
  1131. postrm abort-install <old_version>
  1132. postrm abort-upgrade <old_version>
  1133. postrm disappear <overwriter> <version>
  1134. */
  1135. static const char *const all_control_files[] = {
  1136. "preinst", "postinst", "prerm", "postrm",
  1137. "list", "md5sums", "shlibs", "conffiles",
  1138. "config", "templates"
  1139. };
  1140. static char **all_control_list(const char *package_name)
  1141. {
  1142. unsigned i = 0;
  1143. char **remove_files;
  1144. /* Create a list of all /var/lib/dpkg/info/<package> files */
  1145. remove_files = xzalloc(sizeof(all_control_files) + sizeof(char*));
  1146. while (i < ARRAY_SIZE(all_control_files)) {
  1147. remove_files[i] = xasprintf("/var/lib/dpkg/info/%s.%s",
  1148. package_name, all_control_files[i]);
  1149. i++;
  1150. }
  1151. return remove_files;
  1152. }
  1153. static void free_array(char **array)
  1154. {
  1155. if (array) {
  1156. unsigned i = 0;
  1157. while (array[i]) {
  1158. free(array[i]);
  1159. i++;
  1160. }
  1161. free(array);
  1162. }
  1163. }
  1164. /* This function lists information on the installed packages. It loops through
  1165. * the status_hashtable to retrieve the info. This results in smaller code than
  1166. * scanning the status file. The resulting list, however, is unsorted.
  1167. */
  1168. static void list_packages(const char *pattern)
  1169. {
  1170. int i;
  1171. puts(" Name Version");
  1172. puts("+++-==============-==============");
  1173. /* go through status hash, dereference package hash and finally strings */
  1174. for (i = 0; i < STATUS_HASH_PRIME+1; i++) {
  1175. if (status_hashtable[i]) {
  1176. const char *stat_str; /* status string */
  1177. const char *name_str; /* package name */
  1178. const char *vers_str; /* version */
  1179. char s1, s2; /* status abbreviations */
  1180. int spccnt; /* space count */
  1181. int j;
  1182. stat_str = name_hashtable[status_hashtable[i]->status];
  1183. name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name];
  1184. vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version];
  1185. if (pattern && fnmatch(pattern, name_str, 0) != 0)
  1186. continue;
  1187. /* get abbreviation for status field 1 */
  1188. s1 = stat_str[0] == 'i' ? 'i' : 'r';
  1189. /* get abbreviation for status field 2 */
  1190. for (j = 0, spccnt = 0; stat_str[j] && spccnt < 2; j++) {
  1191. if (stat_str[j] == ' ') spccnt++;
  1192. }
  1193. s2 = stat_str[j];
  1194. /* print out the line formatted like Debian dpkg */
  1195. printf("%c%c %-14s %s\n", s1, s2, name_str, vers_str);
  1196. }
  1197. }
  1198. }
  1199. static void remove_package(const unsigned package_num, int noisy)
  1200. {
  1201. const char *package_name = name_hashtable[package_hashtable[package_num]->name];
  1202. const char *package_version = name_hashtable[package_hashtable[package_num]->version];
  1203. const unsigned status_num = search_status_hashtable(package_name);
  1204. const int package_name_length = strlen(package_name);
  1205. char **remove_files;
  1206. char **exclude_files;
  1207. char list_name[package_name_length + 25];
  1208. char conffile_name[package_name_length + 30];
  1209. if (noisy)
  1210. printf("Removing %s (%s)...\n", package_name, package_version);
  1211. /* Run prerm script */
  1212. run_package_script_or_die(package_name, "prerm");
  1213. /* Create a list of files to remove, and a separate list of those to keep */
  1214. sprintf(list_name, "/var/lib/dpkg/info/%s.%s", package_name, "list");
  1215. remove_files = create_list(list_name);
  1216. sprintf(conffile_name, "/var/lib/dpkg/info/%s.%s", package_name, "conffiles");
  1217. exclude_files = create_list(conffile_name);
  1218. /* Some directories can't be removed straight away, so do multiple passes */
  1219. while (remove_file_array(remove_files, exclude_files))
  1220. continue;
  1221. free_array(exclude_files);
  1222. free_array(remove_files);
  1223. /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
  1224. exclude_files = xzalloc(sizeof(exclude_files[0]) * 3);
  1225. exclude_files[0] = xstrdup(conffile_name);
  1226. exclude_files[1] = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "postrm");
  1227. /* Create a list of all /var/lib/dpkg/info/<package> files */
  1228. remove_files = all_control_list(package_name);
  1229. remove_file_array(remove_files, exclude_files);
  1230. free_array(remove_files);
  1231. free_array(exclude_files);
  1232. /* rename <package>.conffiles to <package>.list
  1233. * The conffiles control file isn't required in Debian packages, so don't
  1234. * error out if it's missing. */
  1235. rename(conffile_name, list_name);
  1236. /* Change package status */
  1237. set_status(status_num, "config-files", 3);
  1238. }
  1239. static void purge_package(const unsigned package_num)
  1240. {
  1241. const char *package_name = name_hashtable[package_hashtable[package_num]->name];
  1242. const char *package_version = name_hashtable[package_hashtable[package_num]->version];
  1243. const unsigned status_num = search_status_hashtable(package_name);
  1244. char **remove_files;
  1245. char **exclude_files;
  1246. char list_name[strlen(package_name) + 25];
  1247. printf("Purging %s (%s)...\n", package_name, package_version);
  1248. /* Run prerm script */
  1249. run_package_script_or_die(package_name, "prerm");
  1250. /* Create a list of files to remove */
  1251. sprintf(list_name, "/var/lib/dpkg/info/%s.%s", package_name, "list");
  1252. remove_files = create_list(list_name);
  1253. /* Some directories cant be removed straight away, so do multiple passes */
  1254. while (remove_file_array(remove_files, NULL))
  1255. continue;
  1256. free_array(remove_files);
  1257. /* Create a list of all /var/lib/dpkg/info/<package> files */
  1258. remove_files = all_control_list(package_name);
  1259. /* Delete all of them except the postrm script */
  1260. exclude_files = xzalloc(sizeof(exclude_files[0]) * 2);
  1261. exclude_files[0] = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "postrm");
  1262. remove_file_array(remove_files, exclude_files);
  1263. free_array(exclude_files);
  1264. /* Run and remove postrm script */
  1265. run_package_script_or_die(package_name, "postrm");
  1266. remove_file_array(remove_files, NULL);
  1267. free_array(remove_files);
  1268. /* Change package status */
  1269. set_status(status_num, "not-installed", 3);
  1270. }
  1271. static archive_handle_t *init_archive_deb_ar(const char *filename)
  1272. {
  1273. archive_handle_t *ar_handle;
  1274. /* Setup an ar archive handle that refers to the gzip sub archive */
  1275. ar_handle = init_handle();
  1276. ar_handle->filter = filter_accept_list_reassign;
  1277. ar_handle->src_fd = xopen(filename, O_RDONLY);
  1278. return ar_handle;
  1279. }
  1280. static void init_archive_deb_control(archive_handle_t *ar_handle)
  1281. {
  1282. archive_handle_t *tar_handle;
  1283. /* Setup the tar archive handle */
  1284. tar_handle = init_handle();
  1285. tar_handle->src_fd = ar_handle->src_fd;
  1286. /* We don't care about data.tar.* or debian-binary, just control.tar.* */
  1287. #if ENABLE_FEATURE_SEAMLESS_GZ
  1288. llist_add_to(&(ar_handle->accept), (char*)"control.tar.gz");
  1289. #endif
  1290. #if ENABLE_FEATURE_SEAMLESS_BZ2
  1291. llist_add_to(&(ar_handle->accept), (char*)"control.tar.bz2");
  1292. #endif
  1293. /* Assign the tar handle as a subarchive of the ar handle */
  1294. ar_handle->dpkg__sub_archive = tar_handle;
  1295. }
  1296. static void init_archive_deb_data(archive_handle_t *ar_handle)
  1297. {
  1298. archive_handle_t *tar_handle;
  1299. /* Setup the tar archive handle */
  1300. tar_handle = init_handle();
  1301. tar_handle->src_fd = ar_handle->src_fd;
  1302. /* We don't care about control.tar.* or debian-binary, just data.tar.* */
  1303. #if ENABLE_FEATURE_SEAMLESS_GZ
  1304. llist_add_to(&(ar_handle->accept), (char*)"data.tar.gz");
  1305. #endif
  1306. #if ENABLE_FEATURE_SEAMLESS_BZ2
  1307. llist_add_to(&(ar_handle->accept), (char*)"data.tar.bz2");
  1308. #endif
  1309. /* Assign the tar handle as a subarchive of the ar handle */
  1310. ar_handle->dpkg__sub_archive = tar_handle;
  1311. }
  1312. static void FAST_FUNC data_extract_to_buffer(archive_handle_t *archive_handle)
  1313. {
  1314. unsigned size = archive_handle->file_header->size;
  1315. archive_handle->dpkg__buffer = xzalloc(size + 1);
  1316. xread(archive_handle->src_fd, archive_handle->dpkg__buffer, size);
  1317. }
  1318. static char *deb_extract_control_file_to_buffer(archive_handle_t *ar_handle, llist_t *myaccept)
  1319. {
  1320. ar_handle->dpkg__sub_archive->action_data = data_extract_to_buffer;
  1321. ar_handle->dpkg__sub_archive->accept = myaccept;
  1322. ar_handle->dpkg__sub_archive->filter = filter_accept_list;
  1323. unpack_ar_archive(ar_handle);
  1324. close(ar_handle->src_fd);
  1325. return ar_handle->dpkg__sub_archive->dpkg__buffer;
  1326. }
  1327. static void append_control_file_to_llist(const char *package_name, const char *control_name, llist_t **ll)
  1328. {
  1329. FILE *fp;
  1330. char *filename, *line;
  1331. filename = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, control_name);
  1332. fp = fopen_for_read(filename);
  1333. free(filename);
  1334. if (fp != NULL) {
  1335. while ((line = xmalloc_fgetline(fp)) != NULL)
  1336. llist_add_to(ll, line);
  1337. fclose(fp);
  1338. }
  1339. }
  1340. static char FAST_FUNC filter_rename_config(archive_handle_t *archive_handle)
  1341. {
  1342. int fd;
  1343. char *name_ptr = archive_handle->file_header->name + 1;
  1344. /* Is this file marked as config file? */
  1345. if (!find_list_entry(archive_handle->accept, name_ptr))
  1346. return EXIT_SUCCESS; /* no */
  1347. fd = open(name_ptr, O_RDONLY);
  1348. if (fd >= 0) {
  1349. md5_ctx_t md5;
  1350. char *md5line, *buf;
  1351. int count;
  1352. /* Calculate MD5 of existing file */
  1353. buf = xmalloc(4096);
  1354. md5_begin(&md5);
  1355. while ((count = safe_read(fd, buf, 4096)) > 0)
  1356. md5_hash(&md5, buf, count);
  1357. md5_end(&md5, buf); /* using buf as result storage */
  1358. close(fd);
  1359. md5line = xmalloc(16 * 2 + 2 + strlen(name_ptr) + 1);
  1360. sprintf(bin2hex(md5line, buf, 16), " %s", name_ptr);
  1361. free(buf);
  1362. /* Is it changed after install? */
  1363. if (find_list_entry(archive_handle->accept, md5line) == NULL) {
  1364. printf("Warning: Creating %s as %s.dpkg-new\n", name_ptr, name_ptr);
  1365. archive_handle->file_header->name = xasprintf("%s.dpkg-new", archive_handle->file_header->name);
  1366. }
  1367. free(md5line);
  1368. }
  1369. return EXIT_SUCCESS;
  1370. }
  1371. static void FAST_FUNC data_extract_all_prefix(archive_handle_t *archive_handle)
  1372. {
  1373. char *name_ptr = archive_handle->file_header->name;
  1374. /* Skip all leading "/" */
  1375. while (*name_ptr == '/')
  1376. name_ptr++;
  1377. /* Skip all leading "./" and "../" */
  1378. while (name_ptr[0] == '.') {
  1379. if (name_ptr[1] == '.')
  1380. name_ptr++;
  1381. if (name_ptr[1] != '/')
  1382. break;
  1383. name_ptr += 2;
  1384. }
  1385. if (name_ptr[0] != '\0') {
  1386. archive_handle->file_header->name = xasprintf("%s%s", archive_handle->dpkg__buffer, name_ptr);
  1387. data_extract_all(archive_handle);
  1388. if (fnmatch("*.dpkg-new", archive_handle->file_header->name, 0) == 0) {
  1389. /* remove .dpkg-new suffix */
  1390. archive_handle->file_header->name[strlen(archive_handle->file_header->name) - 9] = '\0';
  1391. }
  1392. }
  1393. }
  1394. enum {
  1395. /* Commands */
  1396. OPT_configure = (1 << 0),
  1397. OPT_install = (1 << 1),
  1398. OPT_list_installed = (1 << 2),
  1399. OPT_purge = (1 << 3),
  1400. OPT_remove = (1 << 4),
  1401. OPT_unpack = (1 << 5),
  1402. OPTMASK_cmd = (1 << 6) - 1,
  1403. /* Options */
  1404. OPT_force = (1 << 6),
  1405. OPT_force_ignore_depends = (1 << 7),
  1406. OPT_force_confnew = (1 << 8),
  1407. OPT_force_confold = (1 << 9),
  1408. };
  1409. static void unpack_package(deb_file_t *deb_file)
  1410. {
  1411. const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
  1412. const unsigned status_num = search_status_hashtable(package_name);
  1413. const unsigned status_package_num = status_hashtable[status_num]->package;
  1414. char *info_prefix;
  1415. char *list_filename;
  1416. archive_handle_t *archive_handle;
  1417. FILE *out_stream;
  1418. llist_t *accept_list;
  1419. llist_t *conffile_list;
  1420. int i;
  1421. /* If existing version, remove it first */
  1422. conffile_list = NULL;
  1423. if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) {
  1424. /* Package is already installed, remove old version first */
  1425. printf("Preparing to replace %s %s (using %s)...\n", package_name,
  1426. name_hashtable[package_hashtable[status_package_num]->version],
  1427. deb_file->filename);
  1428. /* Read md5sums from old package */
  1429. if (!(option_mask32 & OPT_force_confold))
  1430. append_control_file_to_llist(package_name, "md5sums", &conffile_list);
  1431. remove_package(status_package_num, 0);
  1432. } else {
  1433. printf("Unpacking %s (from %s)...\n", package_name, deb_file->filename);
  1434. }
  1435. /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */
  1436. info_prefix = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "");
  1437. archive_handle = init_archive_deb_ar(deb_file->filename);
  1438. init_archive_deb_control(archive_handle);
  1439. accept_list = NULL;
  1440. i = 0;
  1441. while (i < ARRAY_SIZE(all_control_files)) {
  1442. char *c = xasprintf("./%s", all_control_files[i]);
  1443. llist_add_to(&accept_list, c);
  1444. i++;
  1445. }
  1446. archive_handle->dpkg__sub_archive->accept = accept_list;
  1447. archive_handle->dpkg__sub_archive->filter = filter_accept_list;
  1448. archive_handle->dpkg__sub_archive->action_data = data_extract_all_prefix;
  1449. archive_handle->dpkg__sub_archive->dpkg__buffer = info_prefix;
  1450. archive_handle->dpkg__sub_archive->ah_flags |= ARCHIVE_UNLINK_OLD;
  1451. unpack_ar_archive(archive_handle);
  1452. /* Run the preinst prior to extracting */
  1453. run_package_script_or_die(package_name, "preinst");
  1454. /* Don't overwrite existing config files */
  1455. if (!(option_mask32 & OPT_force_confnew))
  1456. append_control_file_to_llist(package_name, "conffiles", &conffile_list);
  1457. /* Extract data.tar.gz to the root directory */
  1458. archive_handle = init_archive_deb_ar(deb_file->filename);
  1459. init_archive_deb_data(archive_handle);
  1460. archive_handle->dpkg__sub_archive->accept = conffile_list;
  1461. archive_handle->dpkg__sub_archive->filter = filter_rename_config;
  1462. archive_handle->dpkg__sub_archive->action_data = data_extract_all_prefix;
  1463. archive_handle->dpkg__sub_archive->dpkg__buffer = (char*)"/"; /* huh? */
  1464. archive_handle->dpkg__sub_archive->ah_flags |= ARCHIVE_UNLINK_OLD;
  1465. unpack_ar_archive(archive_handle);
  1466. /* Create the list file */
  1467. list_filename = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "list");
  1468. out_stream = xfopen_for_write(list_filename);
  1469. while (archive_handle->dpkg__sub_archive->passed) {
  1470. /* the leading . has been stripped by data_extract_all_prefix already */
  1471. fputs(archive_handle->dpkg__sub_archive->passed->data, out_stream);
  1472. fputc('\n', out_stream);
  1473. archive_handle->dpkg__sub_archive->passed = archive_handle->dpkg__sub_archive->passed->link;
  1474. }
  1475. fclose(out_stream);
  1476. /* change status */
  1477. set_status(status_num, "install", 1);
  1478. set_status(status_num, "unpacked", 3);
  1479. free(info_prefix);
  1480. free(list_filename);
  1481. }
  1482. static void configure_package(deb_file_t *deb_file)
  1483. {
  1484. const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
  1485. const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version];
  1486. const int status_num = search_status_hashtable(package_name);
  1487. printf("Setting up %s (%s)...\n", package_name, package_version);
  1488. /* Run the postinst script */
  1489. /* TODO: handle failure gracefully */
  1490. run_package_script_or_die(package_name, "postinst");
  1491. /* Change status to reflect success */
  1492. set_status(status_num, "install", 1);
  1493. set_status(status_num, "installed", 3);
  1494. }
  1495. int dpkg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1496. int dpkg_main(int argc UNUSED_PARAM, char **argv)
  1497. {
  1498. deb_file_t **deb_file = NULL;
  1499. status_node_t *status_node;
  1500. char *str_f;
  1501. int opt;
  1502. int package_num;
  1503. int deb_count = 0;
  1504. int state_status;
  1505. int status_num;
  1506. int i;
  1507. #if ENABLE_LONG_OPTS
  1508. static const char dpkg_longopts[] ALIGN1 =
  1509. // FIXME: we use -C non-compatibly, should be:
  1510. // "-C|--audit Check for broken package(s)"
  1511. "configure\0" No_argument "C"
  1512. "force\0" Required_argument "F"
  1513. "install\0" No_argument "i"
  1514. "list\0" No_argument "l"
  1515. "purge\0" No_argument "P"
  1516. "remove\0" No_argument "r"
  1517. "unpack\0" No_argument "u"
  1518. "force-depends\0" No_argument "\xff"
  1519. "force-confnew\0" No_argument "\xfe"
  1520. "force-confold\0" No_argument "\xfd"
  1521. ;
  1522. #endif
  1523. INIT_G();
  1524. IF_LONG_OPTS(applet_long_options = dpkg_longopts);
  1525. opt = getopt32(argv, "CilPruF:", &str_f);
  1526. argv += optind;
  1527. //if (opt & OPT_configure) ... // -C
  1528. if (opt & OPT_force) { // -F (--force in official dpkg)
  1529. if (strcmp(str_f, "depends") == 0)
  1530. opt |= OPT_force_ignore_depends;
  1531. else if (strcmp(str_f, "confnew") == 0)
  1532. opt |= OPT_force_confnew;
  1533. else if (strcmp(str_f, "confold") == 0)
  1534. opt |= OPT_force_confold;
  1535. else
  1536. bb_show_usage();
  1537. option_mask32 = opt;
  1538. }
  1539. //if (opt & OPT_install) ... // -i
  1540. //if (opt & OPT_list_installed) ... // -l
  1541. //if (opt & OPT_purge) ... // -P
  1542. //if (opt & OPT_remove) ... // -r
  1543. //if (opt & OPT_unpack) ... // -u (--unpack in official dpkg)
  1544. if (!(opt & OPTMASK_cmd) /* no cmd */
  1545. || ((opt & OPTMASK_cmd) & ((opt & OPTMASK_cmd)-1)) /* more than one cmd */
  1546. ) {
  1547. bb_show_usage();
  1548. }
  1549. /* puts("(Reading database ... xxxxx files and directories installed.)"); */
  1550. index_status_file("/var/lib/dpkg/status");
  1551. /* if the list action was given print the installed packages and exit */
  1552. if (opt & OPT_list_installed) {
  1553. list_packages(argv[0]); /* param can be NULL */
  1554. return EXIT_SUCCESS;
  1555. }
  1556. /* Read arguments and store relevant info in structs */
  1557. while (*argv) {
  1558. /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */
  1559. deb_file = xrealloc_vector(deb_file, 2, deb_count);
  1560. deb_file[deb_count] = xzalloc(sizeof(deb_file[0][0]));
  1561. if (opt & (OPT_install | OPT_unpack)) {
  1562. /* -i/-u: require filename */
  1563. archive_handle_t *archive_handle;
  1564. llist_t *control_list = NULL;
  1565. /* Extract the control file */
  1566. llist_add_to(&control_list, (char*)"./control");
  1567. archive_handle = init_archive_deb_ar(argv[0]);
  1568. init_archive_deb_control(archive_handle);
  1569. deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
  1570. if (deb_file[deb_count]->control_file == NULL) {
  1571. bb_error_msg_and_die("can't extract control file");
  1572. }
  1573. deb_file[deb_count]->filename = xstrdup(argv[0]);
  1574. package_num = fill_package_struct(deb_file[deb_count]->control_file);
  1575. if (package_num == -1) {
  1576. bb_error_msg("invalid control file in %s", argv[0]);
  1577. argv++;
  1578. continue;
  1579. }
  1580. deb_file[deb_count]->package = (unsigned) package_num;
  1581. /* Add the package to the status hashtable */
  1582. if (opt & (OPT_unpack | OPT_install)) {
  1583. /* Try and find a currently installed version of this package */
  1584. status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
  1585. /* If no previous entry was found initialise a new entry */
  1586. if (status_hashtable[status_num] == NULL
  1587. || status_hashtable[status_num]->status == 0
  1588. ) {
  1589. status_node = xmalloc(sizeof(status_node_t));
  1590. status_node->package = deb_file[deb_count]->package;
  1591. /* reinstreq isnt changed to "ok" until the package control info
  1592. * is written to the status file*/
  1593. status_node->status = search_name_hashtable("install reinstreq not-installed");
  1594. status_hashtable[status_num] = status_node;
  1595. } else {
  1596. set_status(status_num, "install", 1);
  1597. set_status(status_num, "reinstreq", 2);
  1598. }
  1599. }
  1600. } else if (opt & (OPT_configure | OPT_purge | OPT_remove)) {
  1601. /* -C/-p/-r: require package name */
  1602. deb_file[deb_count]->package = search_package_hashtable(
  1603. search_name_hashtable(argv[0]),
  1604. search_name_hashtable("ANY"), VER_ANY);
  1605. if (package_hashtable[deb_file[deb_count]->package] == NULL) {
  1606. bb_error_msg_and_die("package %s is uninstalled or unknown", argv[0]);
  1607. }
  1608. package_num = deb_file[deb_count]->package;
  1609. status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
  1610. state_status = get_status(status_num, 3);
  1611. /* check package status is "installed" */
  1612. if (opt & OPT_remove) {
  1613. if (strcmp(name_hashtable[state_status], "not-installed") == 0
  1614. || strcmp(name_hashtable[state_status], "config-files") == 0
  1615. ) {
  1616. bb_error_msg_and_die("%s is already removed", name_hashtable[package_hashtable[package_num]->name]);
  1617. }
  1618. set_status(status_num, "deinstall", 1);
  1619. } else if (opt & OPT_purge) {
  1620. /* if package status is "conf-files" then its ok */
  1621. if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
  1622. bb_error_msg_and_die("%s is already purged", name_hashtable[package_hashtable[package_num]->name]);
  1623. }
  1624. set_status(status_num, "purge", 1);
  1625. }
  1626. }
  1627. deb_count++;
  1628. argv++;
  1629. }
  1630. if (!deb_count)
  1631. bb_error_msg_and_die("no package files specified");
  1632. deb_file[deb_count] = NULL;
  1633. /* Check that the deb file arguments are installable */
  1634. if (!(opt & OPT_force_ignore_depends)) {
  1635. if (!check_deps(deb_file, 0 /*, deb_count*/)) {
  1636. bb_error_msg_and_die("dependency check failed");
  1637. }
  1638. }
  1639. /* TODO: install or remove packages in the correct dependency order */
  1640. for (i = 0; i < deb_count; i++) {
  1641. /* Remove or purge packages */
  1642. if (opt & OPT_remove) {
  1643. remove_package(deb_file[i]->package, 1);
  1644. }
  1645. else if (opt & OPT_purge) {
  1646. purge_package(deb_file[i]->package);
  1647. }
  1648. else if (opt & OPT_unpack) {
  1649. unpack_package(deb_file[i]);
  1650. }
  1651. else if (opt & OPT_install) {
  1652. unpack_package(deb_file[i]);
  1653. /* package is configured in second pass below */
  1654. }
  1655. else if (opt & OPT_configure) {
  1656. configure_package(deb_file[i]);
  1657. }
  1658. }
  1659. /* configure installed packages */
  1660. if (opt & OPT_install) {
  1661. for (i = 0; i < deb_count; i++)
  1662. configure_package(deb_file[i]);
  1663. }
  1664. write_status_file(deb_file);
  1665. if (ENABLE_FEATURE_CLEAN_UP) {
  1666. for (i = 0; i < deb_count; i++) {
  1667. free(deb_file[i]->control_file);
  1668. free(deb_file[i]->filename);
  1669. free(deb_file[i]);
  1670. }
  1671. free(deb_file);
  1672. for (i = 0; i < NAME_HASH_PRIME; i++) {
  1673. free(name_hashtable[i]);
  1674. }
  1675. for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
  1676. free_package(package_hashtable[i]);
  1677. }
  1678. for (i = 0; i < STATUS_HASH_PRIME; i++) {
  1679. free(status_hashtable[i]);
  1680. }
  1681. free(status_hashtable);
  1682. free(package_hashtable);
  1683. free(name_hashtable);
  1684. }
  1685. return EXIT_SUCCESS;
  1686. }