dpkg.c 59 KB

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