pkg_hash.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* opkg_hash.c - the opkg package management system
  2. Steven M. Ayer
  3. Copyright (C) 2002 Compaq Computer Corporation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <stdio.h>
  14. #include "hash_table.h"
  15. #include "pkg.h"
  16. #include "opkg_message.h"
  17. #include "pkg_vec.h"
  18. #include "pkg_hash.h"
  19. #include "parse_util.h"
  20. #include "pkg_parse.h"
  21. #include "opkg_utils.h"
  22. #include "sprintf_alloc.h"
  23. #include "file_util.h"
  24. #include "libbb/libbb.h"
  25. #include "libbb/gzip.h"
  26. void pkg_hash_init(void)
  27. {
  28. hash_table_init("pkg-hash", &conf->pkg_hash,
  29. OPKG_CONF_DEFAULT_HASH_LEN);
  30. }
  31. static void free_pkgs(const char *key, void *entry, void *data)
  32. {
  33. int i;
  34. abstract_pkg_t *ab_pkg;
  35. /* Each entry in the hash table is an abstract package, which contains
  36. * a list of packages that provide the abstract package.
  37. */
  38. ab_pkg = (abstract_pkg_t *) entry;
  39. if (ab_pkg->pkgs) {
  40. for (i = 0; i < ab_pkg->pkgs->len; i++) {
  41. pkg_deinit(ab_pkg->pkgs->pkgs[i]);
  42. free(ab_pkg->pkgs->pkgs[i]);
  43. }
  44. }
  45. abstract_pkg_vec_free(ab_pkg->provided_by);
  46. abstract_pkg_vec_free(ab_pkg->replaced_by);
  47. pkg_vec_free(ab_pkg->pkgs);
  48. free(ab_pkg->depended_upon_by);
  49. free(ab_pkg->name);
  50. free(ab_pkg);
  51. }
  52. void pkg_hash_deinit(void)
  53. {
  54. hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
  55. hash_table_deinit(&conf->pkg_hash);
  56. }
  57. int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
  58. {
  59. nv_pair_list_elt_t *l;
  60. char *list_file, *subname;
  61. list_for_each_entry(l, &conf->arch_list.head, node) {
  62. nv_pair_t *nv = (nv_pair_t *) l->data;
  63. sprintf_alloc(&subname, "%s-%s", dist->name, nv->name);
  64. sprintf_alloc(&list_file, "%s/%s", lists_dir, subname);
  65. if (file_exists(list_file)) {
  66. if (pkg_hash_add_from_file(list_file, dist, NULL, 0, 0)) {
  67. free(list_file);
  68. return -1;
  69. }
  70. pkg_src_list_append(&conf->pkg_src_list, subname,
  71. dist->value, "__dummy__", 0);
  72. }
  73. free(list_file);
  74. }
  75. return 0;
  76. }
  77. int
  78. pkg_hash_add_from_file(const char *file_name,
  79. pkg_src_t * src, pkg_dest_t * dest, int is_status_file, int state_flags)
  80. {
  81. pkg_t *pkg;
  82. FILE *fp;
  83. char *buf;
  84. const size_t len = 4096;
  85. int ret = 0;
  86. struct gzip_handle zh;
  87. if (src && src->gzip) {
  88. fp = gzip_fdopen(&zh, file_name);
  89. } else {
  90. fp = fopen(file_name, "r");
  91. }
  92. if (fp == NULL) {
  93. opkg_perror(ERROR, "Failed to open %s", file_name);
  94. return -1;
  95. }
  96. buf = xmalloc(len);
  97. do {
  98. pkg = pkg_new();
  99. pkg->src = src;
  100. pkg->dest = dest;
  101. pkg->state_flag |= state_flags;
  102. ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
  103. &buf, len);
  104. if (pkg->name == NULL) {
  105. /* probably just a blank line */
  106. ret = 1;
  107. }
  108. if (ret) {
  109. pkg_deinit(pkg);
  110. free(pkg);
  111. if (ret == -1)
  112. break;
  113. if (ret == 1)
  114. /* Probably a blank line, continue parsing. */
  115. ret = 0;
  116. continue;
  117. }
  118. if (!(pkg->state_flag & SF_NEED_DETAIL)) {
  119. //opkg_msg(DEBUG, "Package %s is unrelated, ignoring.\n", pkg->name);
  120. pkg_deinit(pkg);
  121. free(pkg);
  122. continue;
  123. }
  124. if (!pkg_get_architecture(pkg) || !pkg_get_arch_priority(pkg)) {
  125. char *version_str = pkg_version_str_alloc(pkg);
  126. opkg_msg(NOTICE, "Package %s version %s has no "
  127. "valid architecture, ignoring.\n",
  128. pkg->name, version_str);
  129. free(version_str);
  130. continue;
  131. }
  132. hash_insert_pkg(pkg, is_status_file);
  133. } while (!feof(fp));
  134. free(buf);
  135. fclose(fp);
  136. if (src && src->gzip)
  137. gzip_close(&zh);
  138. return ret;
  139. }
  140. /*
  141. * Load in feed files from the cached "src" and/or "src/gz" locations.
  142. */
  143. int pkg_hash_load_feeds(int state_flags)
  144. {
  145. pkg_src_list_elt_t *iter;
  146. pkg_src_t *src, *subdist;
  147. char *list_file, *lists_dir;
  148. opkg_msg(INFO, "\n");
  149. lists_dir = conf->restrict_to_default_dest ?
  150. conf->default_dest->lists_dir : conf->lists_dir;
  151. for (iter = void_list_first(&conf->pkg_src_list); iter;
  152. iter = void_list_next(&conf->pkg_src_list, iter)) {
  153. src = (pkg_src_t *) iter->data;
  154. sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
  155. if (file_exists(list_file)) {
  156. if (pkg_hash_add_from_file(list_file, src, NULL, 0, state_flags)) {
  157. free(list_file);
  158. return -1;
  159. }
  160. }
  161. free(list_file);
  162. }
  163. return 0;
  164. }
  165. /*
  166. * Load in status files from the configured "dest"s.
  167. */
  168. int pkg_hash_load_status_files(void)
  169. {
  170. pkg_dest_list_elt_t *iter;
  171. pkg_dest_t *dest;
  172. opkg_msg(INFO, "\n");
  173. for (iter = void_list_first(&conf->pkg_dest_list); iter;
  174. iter = void_list_next(&conf->pkg_dest_list, iter)) {
  175. dest = (pkg_dest_t *) iter->data;
  176. if (file_exists(dest->status_file_name)) {
  177. if (pkg_hash_add_from_file
  178. (dest->status_file_name, NULL, dest, 1, SF_NEED_DETAIL))
  179. return -1;
  180. }
  181. }
  182. return 0;
  183. }
  184. static void
  185. pkg_hash_load_package_details_helper(const char *pkg_name, void *entry, void *data)
  186. {
  187. int *count = data;
  188. abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
  189. if (ab_pkg->state_flag & SF_NEED_DETAIL) {
  190. if (ab_pkg->state_flag & SF_MARKED) {
  191. opkg_msg(DEBUG, "skipping already seen flagged abpkg %s\n",
  192. ab_pkg->name);
  193. return;
  194. }
  195. opkg_msg(DEBUG, "found yet incomplete flagged abpkg %s\n",
  196. ab_pkg->name);
  197. (*count)++;
  198. ab_pkg->state_flag |= SF_MARKED;
  199. }
  200. }
  201. int pkg_hash_load_package_details(void)
  202. {
  203. int n_need_detail;
  204. while (1) {
  205. pkg_hash_load_feeds(0);
  206. n_need_detail = 0;
  207. hash_table_foreach(&conf->pkg_hash, pkg_hash_load_package_details_helper, &n_need_detail);
  208. if (n_need_detail > 0)
  209. opkg_msg(DEBUG, "Found %d packages requiring details, reloading feeds\n", n_need_detail);
  210. else
  211. break;
  212. }
  213. return 0;
  214. }
  215. pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
  216. int (*constraint_fcn) (pkg_t *
  217. pkg,
  218. void
  219. *cdata),
  220. void *cdata, int quiet)
  221. {
  222. int i, j;
  223. int nprovides = 0;
  224. int nmatching = 0;
  225. int wrong_arch_found = 0;
  226. int arch_priority;
  227. pkg_vec_t *matching_pkgs;
  228. abstract_pkg_vec_t *matching_apkgs;
  229. abstract_pkg_vec_t *provided_apkg_vec;
  230. abstract_pkg_t **provided_apkgs;
  231. abstract_pkg_vec_t *providers;
  232. pkg_t *latest_installed_parent = NULL;
  233. pkg_t *latest_matching = NULL;
  234. pkg_t *priorized_matching = NULL;
  235. pkg_t *held_pkg = NULL;
  236. pkg_t *good_pkg_by_name = NULL;
  237. if (apkg == NULL || apkg->provided_by == NULL
  238. || (apkg->provided_by->len == 0))
  239. return NULL;
  240. matching_pkgs = pkg_vec_alloc();
  241. matching_apkgs = abstract_pkg_vec_alloc();
  242. providers = abstract_pkg_vec_alloc();
  243. opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
  244. provided_apkg_vec = apkg->provided_by;
  245. nprovides = provided_apkg_vec->len;
  246. provided_apkgs = provided_apkg_vec->pkgs;
  247. if (nprovides > 1)
  248. opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name,
  249. nprovides);
  250. /* accumulate all the providers */
  251. for (i = 0; i < nprovides; i++) {
  252. abstract_pkg_t *provider_apkg = provided_apkgs[i];
  253. opkg_msg(DEBUG, "Adding %s to providers.\n",
  254. provider_apkg->name);
  255. abstract_pkg_vec_insert(providers, provider_apkg);
  256. }
  257. nprovides = providers->len;
  258. for (i = 0; i < nprovides; i++) {
  259. abstract_pkg_t *provider_apkg =
  260. abstract_pkg_vec_get(providers, i);
  261. abstract_pkg_t *replacement_apkg = NULL;
  262. pkg_vec_t *vec;
  263. if (provider_apkg->replaced_by
  264. && provider_apkg->replaced_by->len) {
  265. replacement_apkg = provider_apkg->replaced_by->pkgs[0];
  266. if (provider_apkg->replaced_by->len > 1) {
  267. opkg_msg(NOTICE, "Multiple replacers for %s, "
  268. "using first one (%s).\n",
  269. provider_apkg->name,
  270. replacement_apkg->name);
  271. }
  272. }
  273. if (replacement_apkg)
  274. opkg_msg(DEBUG,
  275. "replacement_apkg=%s for provider_apkg=%s.\n",
  276. replacement_apkg->name, provider_apkg->name);
  277. if (replacement_apkg && (replacement_apkg != provider_apkg)) {
  278. if (abstract_pkg_vec_contains
  279. (providers, replacement_apkg))
  280. continue;
  281. else
  282. provider_apkg = replacement_apkg;
  283. }
  284. if (!(vec = provider_apkg->pkgs)) {
  285. opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
  286. provider_apkg->name);
  287. continue;
  288. }
  289. /* now check for supported architecture */
  290. {
  291. int max_count = 0;
  292. /* count packages matching max arch priority and keep track of last one */
  293. for (j = 0; j < vec->len; j++) {
  294. pkg_t *maybe = vec->pkgs[j];
  295. arch_priority = pkg_get_arch_priority(maybe);
  296. opkg_msg(DEBUG,
  297. "%s arch=%s arch_priority=%d version=%s.\n",
  298. maybe->name, pkg_get_architecture(maybe),
  299. arch_priority, pkg_get_string(maybe, PKG_VERSION));
  300. /* We make sure not to add the same package twice. Need to search for the reason why
  301. they show up twice sometimes. */
  302. if ((arch_priority > 0)
  303. &&
  304. (!pkg_vec_contains(matching_pkgs, maybe))) {
  305. max_count++;
  306. abstract_pkg_vec_insert(matching_apkgs,
  307. maybe->parent);
  308. pkg_vec_insert(matching_pkgs, maybe);
  309. }
  310. }
  311. if (vec->len > 0 && matching_pkgs->len < 1)
  312. wrong_arch_found = 1;
  313. }
  314. }
  315. if (matching_pkgs->len < 1) {
  316. if (wrong_arch_found)
  317. opkg_msg(ERROR, "Packages for %s found, but"
  318. " incompatible with the architectures configured\n",
  319. apkg->name);
  320. pkg_vec_free(matching_pkgs);
  321. abstract_pkg_vec_free(matching_apkgs);
  322. abstract_pkg_vec_free(providers);
  323. return NULL;
  324. }
  325. if (matching_pkgs->len > 1)
  326. pkg_vec_sort(matching_pkgs,
  327. pkg_name_version_and_architecture_compare);
  328. if (matching_apkgs->len > 1)
  329. abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
  330. for (i = 0; i < matching_pkgs->len; i++) {
  331. pkg_t *matching = matching_pkgs->pkgs[i];
  332. if (constraint_fcn(matching, cdata)) {
  333. opkg_msg(DEBUG, "Candidate: %s %s.\n",
  334. matching->name, pkg_get_string(matching, PKG_VERSION));
  335. good_pkg_by_name = matching;
  336. /* It has been provided by hand, so it is what user want */
  337. if (matching->provided_by_hand == 1)
  338. break;
  339. }
  340. }
  341. for (i = 0; i < matching_pkgs->len; i++) {
  342. pkg_t *matching = matching_pkgs->pkgs[i];
  343. latest_matching = matching;
  344. if (matching->parent->state_status == SS_INSTALLED
  345. || matching->parent->state_status == SS_UNPACKED)
  346. latest_installed_parent = matching;
  347. if (matching->state_flag & (SF_HOLD | SF_PREFER)) {
  348. if (held_pkg)
  349. opkg_msg(NOTICE,
  350. "Multiple packages (%s and %s) providing"
  351. " same name marked HOLD or PREFER. "
  352. "Using latest.\n", held_pkg->name,
  353. matching->name);
  354. held_pkg = matching;
  355. }
  356. }
  357. if (!good_pkg_by_name && !held_pkg && !latest_installed_parent
  358. && matching_apkgs->len > 1 && !quiet) {
  359. int prio = 0;
  360. for (i = 0; i < matching_pkgs->len; i++) {
  361. pkg_t *matching = matching_pkgs->pkgs[i];
  362. arch_priority = pkg_get_arch_priority(matching);
  363. if (arch_priority > prio) {
  364. priorized_matching = matching;
  365. prio = arch_priority;
  366. opkg_msg(DEBUG, "Match %s with priority %i.\n",
  367. matching->name, prio);
  368. }
  369. }
  370. }
  371. if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
  372. opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
  373. matching_pkgs->len, apkg->name);
  374. for (i = 0; i < matching_pkgs->len; i++) {
  375. pkg_t *matching = matching_pkgs->pkgs[i];
  376. opkg_msg(INFO, "%s %s %s\n",
  377. matching->name, pkg_get_string(matching, PKG_VERSION),
  378. pkg_get_architecture(matching));
  379. }
  380. }
  381. nmatching = matching_apkgs->len;
  382. pkg_vec_free(matching_pkgs);
  383. abstract_pkg_vec_free(matching_apkgs);
  384. abstract_pkg_vec_free(providers);
  385. if (good_pkg_by_name) { /* We found a good candidate, we will install it */
  386. return good_pkg_by_name;
  387. }
  388. if (held_pkg) {
  389. opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
  390. return held_pkg;
  391. }
  392. if (latest_installed_parent) {
  393. opkg_msg(INFO,
  394. "Using latest version of installed package %s.\n",
  395. latest_installed_parent->name);
  396. return latest_installed_parent;
  397. }
  398. if (priorized_matching) {
  399. opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
  400. priorized_matching->name, pkg_get_string(priorized_matching, PKG_VERSION),
  401. pkg_get_architecture(priorized_matching));
  402. return priorized_matching;
  403. }
  404. if (nmatching > 1) {
  405. opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
  406. nmatching);
  407. return NULL;
  408. }
  409. if (latest_matching) {
  410. opkg_msg(INFO, "Using latest matching %s %s %s.\n",
  411. latest_matching->name, pkg_get_string(latest_matching, PKG_VERSION),
  412. pkg_get_architecture(latest_matching));
  413. return latest_matching;
  414. }
  415. return NULL;
  416. }
  417. static int pkg_name_constraint_fcn(pkg_t * pkg, void *cdata)
  418. {
  419. const char *name = (const char *)cdata;
  420. if (strcmp(pkg->name, name) == 0)
  421. return 1;
  422. else
  423. return 0;
  424. }
  425. static pkg_vec_t *pkg_vec_fetch_by_name(const char *pkg_name)
  426. {
  427. abstract_pkg_t *ab_pkg;
  428. if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
  429. return NULL;
  430. if (ab_pkg->pkgs)
  431. return ab_pkg->pkgs;
  432. if (ab_pkg->provided_by) {
  433. abstract_pkg_t *abpkg =
  434. abstract_pkg_vec_get(ab_pkg->provided_by, 0);
  435. if (abpkg != NULL)
  436. return abpkg->pkgs;
  437. else
  438. return ab_pkg->pkgs;
  439. }
  440. return NULL;
  441. }
  442. pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
  443. {
  444. abstract_pkg_t *apkg = NULL;
  445. if (!(apkg = abstract_pkg_fetch_by_name(name)))
  446. return NULL;
  447. return pkg_hash_fetch_best_installation_candidate(apkg,
  448. pkg_name_constraint_fcn,
  449. apkg->name, 0);
  450. }
  451. pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, const char *version)
  452. {
  453. pkg_vec_t *vec;
  454. int i;
  455. char *version_str = NULL;
  456. if (!(vec = pkg_vec_fetch_by_name(pkg_name)))
  457. return NULL;
  458. for (i = 0; i < vec->len; i++) {
  459. version_str = pkg_version_str_alloc(vec->pkgs[i]);
  460. if (!strcmp(version_str, version)) {
  461. free(version_str);
  462. break;
  463. }
  464. free(version_str);
  465. }
  466. if (i == vec->len)
  467. return NULL;
  468. return vec->pkgs[i];
  469. }
  470. pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name,
  471. pkg_dest_t * dest)
  472. {
  473. pkg_vec_t *vec;
  474. int i;
  475. if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
  476. return NULL;
  477. }
  478. for (i = 0; i < vec->len; i++)
  479. if ((vec->pkgs[i]->state_status == SS_INSTALLED
  480. || vec->pkgs[i]->state_status == SS_UNPACKED)
  481. && vec->pkgs[i]->dest == dest) {
  482. return vec->pkgs[i];
  483. }
  484. return NULL;
  485. }
  486. pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name)
  487. {
  488. pkg_vec_t *vec;
  489. int i;
  490. if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
  491. return NULL;
  492. }
  493. for (i = 0; i < vec->len; i++) {
  494. if (vec->pkgs[i]->state_status == SS_INSTALLED
  495. || vec->pkgs[i]->state_status == SS_UNPACKED) {
  496. return vec->pkgs[i];
  497. }
  498. }
  499. return NULL;
  500. }
  501. static void
  502. pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
  503. {
  504. int j;
  505. abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
  506. pkg_vec_t *all = (pkg_vec_t *) data;
  507. pkg_vec_t *pkg_vec = ab_pkg->pkgs;
  508. if (!pkg_vec)
  509. return;
  510. for (j = 0; j < pkg_vec->len; j++) {
  511. pkg_t *pkg = pkg_vec->pkgs[j];
  512. pkg_vec_insert(all, pkg);
  513. }
  514. }
  515. void pkg_hash_fetch_available(pkg_vec_t * all)
  516. {
  517. hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
  518. all);
  519. }
  520. static void
  521. pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry,
  522. void *data)
  523. {
  524. abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
  525. pkg_vec_t *all = (pkg_vec_t *) data;
  526. pkg_vec_t *pkg_vec = ab_pkg->pkgs;
  527. int j;
  528. if (!pkg_vec)
  529. return;
  530. for (j = 0; j < pkg_vec->len; j++) {
  531. pkg_t *pkg = pkg_vec->pkgs[j];
  532. if (pkg->state_status == SS_INSTALLED
  533. || pkg->state_status == SS_UNPACKED)
  534. pkg_vec_insert(all, pkg);
  535. }
  536. }
  537. void pkg_hash_fetch_all_installed(pkg_vec_t * all)
  538. {
  539. hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
  540. all);
  541. }
  542. /*
  543. * This assumes that the abstract pkg doesn't exist.
  544. */
  545. static abstract_pkg_t *add_new_abstract_pkg_by_name(const char *pkg_name)
  546. {
  547. abstract_pkg_t *ab_pkg;
  548. ab_pkg = abstract_pkg_new();
  549. ab_pkg->name = xstrdup(pkg_name);
  550. hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
  551. return ab_pkg;
  552. }
  553. abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name)
  554. {
  555. abstract_pkg_t *ab_pkg;
  556. if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
  557. ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
  558. return ab_pkg;
  559. }
  560. void hash_insert_pkg(pkg_t * pkg, int set_status)
  561. {
  562. abstract_pkg_t *ab_pkg;
  563. ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
  564. if (!ab_pkg->pkgs)
  565. ab_pkg->pkgs = pkg_vec_alloc();
  566. if (pkg->state_status == SS_INSTALLED) {
  567. ab_pkg->state_status = SS_INSTALLED;
  568. } else if (pkg->state_status == SS_UNPACKED) {
  569. ab_pkg->state_status = SS_UNPACKED;
  570. }
  571. buildDepends(pkg);
  572. buildProvides(ab_pkg, pkg);
  573. init_providelist(pkg, NULL);
  574. /* Need to build the conflicts graph before replaces for correct
  575. * calculation of replaced_by relation.
  576. */
  577. buildConflicts(pkg);
  578. buildReplaces(ab_pkg, pkg);
  579. buildDependedUponBy(pkg, ab_pkg);
  580. pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
  581. pkg->parent = ab_pkg;
  582. }
  583. static const char *strip_offline_root(const char *file_name)
  584. {
  585. unsigned int len;
  586. if (conf->offline_root) {
  587. len = strlen(conf->offline_root);
  588. if (strncmp(file_name, conf->offline_root, len) == 0)
  589. file_name += len;
  590. }
  591. return file_name;
  592. }
  593. void file_hash_remove(const char *file_name)
  594. {
  595. file_name = strip_offline_root(file_name);
  596. hash_table_remove(&conf->file_hash, file_name);
  597. }
  598. pkg_t *file_hash_get_file_owner(const char *file_name)
  599. {
  600. file_name = strip_offline_root(file_name);
  601. return hash_table_get(&conf->file_hash, file_name);
  602. }
  603. void file_hash_set_file_owner(const char *file_name, pkg_t * owning_pkg)
  604. {
  605. pkg_t *old_owning_pkg;
  606. int file_name_len = strlen(file_name);
  607. if (file_name[file_name_len - 1] == '/')
  608. return;
  609. file_name = strip_offline_root(file_name);
  610. old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
  611. hash_table_insert(&conf->file_hash, file_name, owning_pkg);
  612. if (old_owning_pkg) {
  613. pkg_get_installed_files(old_owning_pkg);
  614. str_list_remove_elt(old_owning_pkg->installed_files, file_name);
  615. pkg_free_installed_files(old_owning_pkg);
  616. /* mark this package to have its filelist written */
  617. old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
  618. owning_pkg->state_flag |= SF_FILELIST_CHANGED;
  619. }
  620. }