dpkg.c 53 KB

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