1 /* opkg.c - the opkg package management system
3 Thomas Wood <thomas@openedhand.com>
5 Copyright (C) 2008 OpenMoko Inc
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
22 #include "opkg_conf.h"
25 #include "opkg_install.h"
26 #include "opkg_configure.h"
27 #include "opkg_download.h"
28 #include "opkg_remove.h"
29 #include "opkg_upgrade.h"
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
34 #include <libbb/libbb.h>
38 #define opkg_assert(expr) if (!(expr)) { \
39 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
40 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
42 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
44 /** Private Functions ***/
47 opkg_configure_packages(char *pkg_name)
54 all = pkg_vec_alloc ();
55 pkg_hash_fetch_available (all);
57 for (i = 0; i < all->len; i++)
61 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
64 if (pkg->state_status == SS_UNPACKED)
66 r = opkg_configure (pkg);
69 pkg->state_status = SS_INSTALLED;
70 pkg->parent->state_status = SS_INSTALLED;
71 pkg->state_flag &= ~SF_PREFER;
87 opkg_progress_callback_t cb;
88 opkg_progress_data_t *progress_data;
95 curl_progress_cb (struct _curl_cb_data *cb_data,
96 double t, /* dltotal */
101 int p = (t) ? d * 100 / t : 0;
102 static int prev = -1;
105 /* prevent the same value being sent twice (can occur due to rounding) */
113 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
114 cb_data->progress_data->percentage = progress;
116 (cb_data->cb)(cb_data->progress_data,
130 args = xcalloc(1, sizeof (args_t));
133 err = opkg_conf_init (args);
155 opkg_re_read_config_files (void)
157 /* Unfortunately, the easiest way to re-read the config files right now is to
158 * throw away conf and start again */
160 memset(conf, '\0', sizeof(opkg_conf_t));
166 opkg_get_option (char *option, void **value)
169 extern opkg_option_t options[];
171 /* look up the option
172 * TODO: this would be much better as a hash table
174 while (options[i].name)
176 if (strcmp (options[i].name, option) != 0)
184 switch (options[i].type)
186 case OPKG_OPT_TYPE_BOOL:
187 *((int *) value) = *((int *) options[i].value);
190 case OPKG_OPT_TYPE_INT:
191 *((int *) value) = *((int *) options[i].value);
194 case OPKG_OPT_TYPE_STRING:
195 *((char **)value) = xstrdup(options[i].value);
202 opkg_set_option (char *option, void *value)
204 int i = 0, found = 0;
205 extern opkg_option_t options[];
207 opkg_assert (option != NULL);
208 opkg_assert (value != NULL);
210 /* look up the option
211 * TODO: this would be much better as a hash table
213 while (options[i].name)
215 if (strcmp (options[i].name, option) == 0)
225 /* XXX: Warning: Option not found */
230 switch (options[i].type)
232 case OPKG_OPT_TYPE_BOOL:
233 if (*((int *) value) == 0)
234 *((int *)options[i].value) = 0;
236 *((int *)options[i].value) = 1;
239 case OPKG_OPT_TYPE_INT:
240 *((int *) options[i].value) = *((int *) value);
243 case OPKG_OPT_TYPE_STRING:
244 *((char **)options[i].value) = xstrdup(value);
251 * @brief libopkg API: Install package
252 * @param package_name The name of package in which is going to install
253 * @param progress_callback The callback function that report the status to caller.
256 opkg_install_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
259 char *stripped_filename;
260 opkg_progress_data_t pdata;
262 pkg_vec_t *deps, *all;
264 char **unresolved = NULL;
266 opkg_assert (package_name != NULL);
269 pkg_info_preinstall_check ();
272 /* check to ensure package is not already installed */
273 old = pkg_hash_fetch_installed_by_name(package_name);
276 /* XXX: Error: Package is already installed. */
277 return OPKG_PACKAGE_ALREADY_INSTALLED;
280 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
283 /* XXX: Error: Could not find package to install */
284 return OPKG_PACKAGE_NOT_FOUND;
287 new->state_flag |= SF_USER;
289 pdata.action = OPKG_INSTALL;
294 /* find dependancies and download them */
295 deps = pkg_vec_alloc ();
296 /* this function does not return the original package, so we insert it later */
297 ndepends = pkg_hash_fetch_unsatisfied_dependencies (new, deps, &unresolved);
300 /* XXX: Error: Could not satisfy dependencies */
302 return OPKG_DEPENDENCIES_FAILED;
305 /* insert the package we are installing so that we download it */
306 pkg_vec_insert (deps, new);
308 /* download package and dependancies */
309 for (i = 0; i < deps->len; i++)
312 struct _curl_cb_data cb_data;
316 if (pkg->local_filename)
320 pdata.action = OPKG_DOWNLOAD;
322 if (pkg->src == NULL)
324 /* XXX: Error: Package not available from any configured src */
325 return OPKG_PACKAGE_NOT_AVAILABLE;
328 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
330 /* Get the filename part, without any directory */
331 stripped_filename = strrchr(pkg->filename, '/');
332 if ( ! stripped_filename )
333 stripped_filename = pkg->filename;
335 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir, stripped_filename);
337 cb_data.cb = progress_callback;
338 cb_data.progress_data = &pdata;
339 cb_data.user_data = user_data;
340 /* 75% of "install" progress is for downloading */
341 cb_data.start_range = 75 * i / deps->len;
342 cb_data.finish_range = 75 * (i + 1) / deps->len;
344 err = opkg_download(url, pkg->local_filename,
345 (curl_progress_func) curl_progress_cb, &cb_data);
351 return OPKG_DOWNLOAD_FAILED;
357 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
358 all = pkg_vec_alloc ();
359 pkg_hash_fetch_available (all);
360 for (i = 0; i < all->len; i++)
362 all->pkgs[i]->parent->dependencies_checked = 0;
367 /* 75% of "install" progress is for downloading */
369 pdata.action = OPKG_INSTALL;
370 progress (pdata, 75);
372 /* unpack the package */
373 err = opkg_install_pkg(new, 0);
377 return OPKG_UNKNOWN_ERROR;
380 progress (pdata, 75);
382 /* run configure scripts, etc. */
383 err = opkg_configure_packages (NULL);
386 return OPKG_UNKNOWN_ERROR;
389 /* write out status files and file lists */
390 opkg_conf_write_status_files ();
391 pkg_write_changed_filelists ();
393 progress (pdata, 100);
398 opkg_remove_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
402 pkg_t *pkg_to_remove;
403 opkg_progress_data_t pdata;
405 opkg_assert (package_name != NULL);
407 pkg_info_preinstall_check ();
409 pkg = pkg_hash_fetch_installed_by_name (package_name);
413 /* XXX: Error: Package not installed. */
414 return OPKG_PACKAGE_NOT_INSTALLED;
417 pdata.action = OPKG_REMOVE;
422 if (pkg->state_status == SS_NOT_INSTALLED)
424 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
425 return OPKG_PACKAGE_NOT_INSTALLED;
427 progress (pdata, 25);
429 if (conf->restrict_to_default_dest)
431 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (pkg->name,
436 pkg_to_remove = pkg_hash_fetch_installed_by_name (pkg->name);
440 progress (pdata, 75);
442 err = opkg_remove_pkg (pkg_to_remove, 0);
444 /* write out status files and file lists */
445 opkg_conf_write_status_files ();
446 pkg_write_changed_filelists ();
449 progress (pdata, 100);
450 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
454 opkg_upgrade_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
458 opkg_progress_data_t pdata;
460 opkg_assert (package_name != NULL);
462 pkg_info_preinstall_check ();
464 if (conf->restrict_to_default_dest)
466 pkg = pkg_hash_fetch_installed_by_name_dest (package_name,
470 /* XXX: Error: Package not installed in default_dest */
471 return OPKG_PACKAGE_NOT_INSTALLED;
476 pkg = pkg_hash_fetch_installed_by_name (package_name);
481 /* XXX: Error: Package not installed */
482 return OPKG_PACKAGE_NOT_INSTALLED;
485 pdata.action = OPKG_INSTALL;
489 err = opkg_upgrade_pkg (pkg);
490 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
493 return OPKG_UNKNOWN_ERROR;
495 progress (pdata, 75);
497 err = opkg_configure_packages (NULL);
499 return OPKG_UNKNOWN_ERROR;
502 /* write out status files and file lists */
503 opkg_conf_write_status_files ();
504 pkg_write_changed_filelists ();
506 progress (pdata, 100);
511 opkg_upgrade_all (opkg_progress_callback_t progress_callback, void *user_data)
513 pkg_vec_t *installed;
517 opkg_progress_data_t pdata;
519 pdata.action = OPKG_INSTALL;
524 installed = pkg_vec_alloc ();
525 pkg_info_preinstall_check ();
527 pkg_hash_fetch_all_installed (installed);
528 for (i = 0; i < installed->len; i++)
530 pkg = installed->pkgs[i];
533 progress (pdata, 99 * i / installed->len);
535 err += opkg_upgrade_pkg (pkg);
537 pkg_vec_free (installed);
542 err = opkg_configure_packages (NULL);
547 progress (pdata, 100);
552 opkg_update_package_lists (opkg_progress_callback_t progress_callback, void *user_data)
557 pkg_src_list_elt_t *iter;
559 int sources_list_count, sources_done;
560 opkg_progress_data_t pdata;
562 pdata.action = OPKG_DOWNLOAD;
566 sprintf_alloc (&lists_dir, "%s",
567 (conf->restrict_to_default_dest)
568 ? conf->default_dest->lists_dir
571 if (!file_is_dir (lists_dir))
573 if (file_exists (lists_dir))
575 /* XXX: Error: file exists but is not a directory */
580 err = file_mkdir_hier (lists_dir, 0755);
583 /* XXX: Error: failed to create directory */
589 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
590 if (mkdtemp (tmp) == NULL) {
591 /* XXX: Error: could not create temporary file name */
597 /* count the number of sources so we can give some progress updates */
598 sources_list_count = 0;
600 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
602 sources_list_count++;
605 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
607 char *url, *list_file_name = NULL;
609 src = (pkg_src_t *)iter->data;
611 if (src->extra_data) /* debian style? */
612 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
613 src->gzip ? "Packages.gz" : "Packages");
615 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
617 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
621 struct _curl_cb_data cb_data;
622 char *tmp_file_name = NULL;
624 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
626 /* XXX: Note: downloading url */
628 cb_data.cb = progress_callback;
629 cb_data.progress_data = &pdata;
630 cb_data.user_data = user_data;
631 cb_data.start_range = 100 * sources_done / sources_list_count;
632 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
634 err = opkg_download (url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
638 /* XXX: Note: Inflating downloaded file */
639 in = fopen (tmp_file_name, "r");
640 out = fopen (list_file_name, "w");
649 unlink (tmp_file_name);
651 free (tmp_file_name);
654 err = opkg_download (url, list_file_name, NULL, NULL);
658 /* XXX: Error: download error */
659 result = OPKG_DOWNLOAD_FAILED;
663 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
664 if ( conf->check_signature ) {
666 /* download detached signitures to verify the package lists */
667 /* get the url for the sig file */
668 if (src->extra_data) /* debian style? */
669 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
672 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
674 /* create filename for signature */
675 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
677 /* make sure there is no existing signature file */
678 unlink (sig_file_name);
680 err = opkg_download (url, sig_file_name, NULL, NULL);
683 /* XXX: Warning: Download failed */
688 err = opkg_verify_file (list_file_name, sig_file_name);
691 /* XXX: Notice: Signature check passed */
695 /* XXX: Warning: Signature check failed */
698 free (sig_file_name);
699 free (list_file_name);
703 /* XXX: Note: Signature check for %s skipped because GPG support was not
704 * enabled in this build
709 progress (pdata, 100 * sources_done / sources_list_count);
716 /* Now re-read the package lists to update package hash tables. */
717 opkg_re_read_config_files ();
724 opkg_list_packages (opkg_package_callback_t callback, void *user_data)
729 opkg_assert (callback);
731 all = pkg_vec_alloc ();
732 pkg_hash_fetch_available (all);
733 for (i = 0; i < all->len; i++)
739 callback (pkg, user_data);
748 opkg_list_upgradable_packages (opkg_package_callback_t callback, void *user_data)
750 struct active_list *head;
751 struct active_list *node;
752 pkg_t *old=NULL, *new = NULL;
754 opkg_assert (callback);
756 /* ensure all data is valid */
757 pkg_info_preinstall_check ();
759 head = prepare_upgrade_list();
760 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
761 old = list_entry(node, pkg_t, list);
762 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
765 callback (new, user_data);
767 active_list_head_delete(head);
772 opkg_find_package (const char *name, const char *ver, const char *arch, const char *repo)
777 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
779 all = pkg_vec_alloc ();
780 pkg_hash_fetch_available (all);
781 for (i = 0; i < all->len; i++)
788 if (sstrcmp (pkg->name, name))
792 pkgv = pkg_version_str_alloc (pkg);
793 if (sstrcmp (pkgv, ver))
800 /* check architecture */
803 if (sstrcmp (pkg->architecture, arch))
807 /* check repository */
810 if (sstrcmp (pkg->src->name, repo))
824 #include <curl/curl.h>
827 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
828 * @return return how many repositories cannot access. 0 means all okay.
830 int opkg_repository_accessibility_check(void)
832 pkg_src_list_elt_t *iter;
833 str_list_elt_t *iter1;
841 src = str_list_alloc();
843 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
845 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
846 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
847 stmp = xstrndup(((pkg_src_t *)iter->data)->value,
848 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
851 stmp = xstrdup(((pkg_src_t *)iter->data)->value);
853 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
855 if (strstr(iter1->data, stmp))
861 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
864 str_list_append(src, repo_ptr);
868 while (repositories > 0)
870 iter1 = str_list_pop(src);
873 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
875 if (!(err == CURLE_OK ||
876 err == CURLE_HTTP_RETURNED_ERROR ||
877 err == CURLE_FILE_COULDNT_READ_FILE ||
878 err == CURLE_REMOTE_FILE_NOT_FOUND ||
879 err == CURLE_TFTP_NOTFOUND
886 str_list_elt_deinit(iter1);