3
0

dpkg.c 54 KB

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