dpkg.c 60 KB

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