3
0

dpkg.c 54 KB

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