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 ***/
49 static opkg_package_t*
50 pkg_t_to_opkg_package_t (pkg_t *old)
54 new = opkg_package_new ();
56 new->name = xstrdup(old->name);
57 new->version = pkg_version_str_alloc (old);
58 new->architecture = xstrdup(old->architecture);
60 new->repository = xstrdup(old->src->name);
61 new->description = xstrdup(old->description);
62 new->tags = xstrdup(old->tags);
64 new->size = old->size;
65 new->installed = (old->state_status == SS_INSTALLED);
71 opkg_configure_packages(char *pkg_name)
78 all = pkg_vec_alloc ();
79 pkg_hash_fetch_available (all);
81 for (i = 0; i < all->len; i++)
85 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
88 if (pkg->state_status == SS_UNPACKED)
90 r = opkg_configure (pkg);
93 pkg->state_status = SS_INSTALLED;
94 pkg->parent->state_status = SS_INSTALLED;
95 pkg->state_flag &= ~SF_PREFER;
111 opkg_progress_callback_t cb;
112 opkg_progress_data_t *progress_data;
119 curl_progress_cb (struct _curl_cb_data *cb_data,
120 double t, /* dltotal */
121 double d, /* dlnow */
125 int p = (t) ? d * 100 / t : 0;
126 static int prev = -1;
129 /* prevent the same value being sent twice (can occur due to rounding) */
137 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
138 cb_data->progress_data->percentage = progress;
140 (cb_data->cb)(cb_data->progress_data,
155 p = xcalloc(1, sizeof (opkg_package_t));
161 opkg_package_free (opkg_package_t *p)
165 free (p->architecture);
166 free (p->description);
168 free (p->repository);
178 args = xcalloc(1, sizeof (args_t));
181 err = opkg_conf_init (args);
204 opkg_re_read_config_files (void)
206 /* Unfortunately, the easiest way to re-read the config files right now is to
207 * throw away conf and start again */
209 memset(conf, '\0', sizeof(opkg_conf_t));
214 opkg_get_option (char *option, void **value)
217 extern opkg_option_t options[];
219 /* look up the option
220 * TODO: this would be much better as a hash table
222 while (options[i].name)
224 if (strcmp (options[i].name, option) != 0)
232 switch (options[i].type)
234 case OPKG_OPT_TYPE_BOOL:
235 *((int *) value) = *((int *) options[i].value);
238 case OPKG_OPT_TYPE_INT:
239 *((int *) value) = *((int *) options[i].value);
242 case OPKG_OPT_TYPE_STRING:
243 *((char **)value) = xstrdup(options[i].value);
250 opkg_set_option (char *option, void *value)
252 int i = 0, found = 0;
253 extern opkg_option_t options[];
255 opkg_assert (option != NULL);
256 opkg_assert (value != NULL);
258 /* look up the option
259 * TODO: this would be much better as a hash table
261 while (options[i].name)
263 if (strcmp (options[i].name, option) == 0)
273 /* XXX: Warning: Option not found */
278 switch (options[i].type)
280 case OPKG_OPT_TYPE_BOOL:
281 if (*((int *) value) == 0)
282 *((int *)options[i].value) = 0;
284 *((int *)options[i].value) = 1;
287 case OPKG_OPT_TYPE_INT:
288 *((int *) options[i].value) = *((int *) value);
291 case OPKG_OPT_TYPE_STRING:
292 *((char **)options[i].value) = xstrdup(value);
299 * @brief libopkg API: Install package
300 * @param package_name The name of package in which is going to install
301 * @param progress_callback The callback function that report the status to caller.
304 opkg_install_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
307 char *stripped_filename;
308 opkg_progress_data_t pdata;
310 pkg_vec_t *deps, *all;
312 char **unresolved = NULL;
314 opkg_assert (package_name != NULL);
317 pkg_info_preinstall_check ();
320 /* check to ensure package is not already installed */
321 old = pkg_hash_fetch_installed_by_name(package_name);
324 /* XXX: Error: Package is already installed. */
325 return OPKG_PACKAGE_ALREADY_INSTALLED;
328 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
331 /* XXX: Error: Could not find package to install */
332 return OPKG_PACKAGE_NOT_FOUND;
335 new->state_flag |= SF_USER;
337 pdata.action = OPKG_INSTALL;
338 pdata.package = pkg_t_to_opkg_package_t (new);
342 /* find dependancies and download them */
343 deps = pkg_vec_alloc ();
344 /* this function does not return the original package, so we insert it later */
345 ndepends = pkg_hash_fetch_unsatisfied_dependencies (new, deps, &unresolved);
348 /* XXX: Error: Could not satisfy dependencies */
350 return OPKG_DEPENDENCIES_FAILED;
353 /* insert the package we are installing so that we download it */
354 pkg_vec_insert (deps, new);
356 /* download package and dependancies */
357 for (i = 0; i < deps->len; i++)
360 struct _curl_cb_data cb_data;
364 if (pkg->local_filename)
367 opkg_package_free (pdata.package);
368 pdata.package = pkg_t_to_opkg_package_t (pkg);
369 pdata.action = OPKG_DOWNLOAD;
371 if (pkg->src == NULL)
373 /* XXX: Error: Package not available from any configured src */
374 return OPKG_PACKAGE_NOT_AVAILABLE;
377 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
379 /* Get the filename part, without any directory */
380 stripped_filename = strrchr(pkg->filename, '/');
381 if ( ! stripped_filename )
382 stripped_filename = pkg->filename;
384 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir, stripped_filename);
386 cb_data.cb = progress_callback;
387 cb_data.progress_data = &pdata;
388 cb_data.user_data = user_data;
389 /* 75% of "install" progress is for downloading */
390 cb_data.start_range = 75 * i / deps->len;
391 cb_data.finish_range = 75 * (i + 1) / deps->len;
393 err = opkg_download(url, pkg->local_filename,
394 (curl_progress_func) curl_progress_cb, &cb_data);
400 opkg_package_free (pdata.package);
401 return OPKG_DOWNLOAD_FAILED;
407 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
408 all = pkg_vec_alloc ();
409 pkg_hash_fetch_available (all);
410 for (i = 0; i < all->len; i++)
412 all->pkgs[i]->parent->dependencies_checked = 0;
417 /* 75% of "install" progress is for downloading */
418 opkg_package_free (pdata.package);
419 pdata.package = pkg_t_to_opkg_package_t (new);
420 pdata.action = OPKG_INSTALL;
421 progress (pdata, 75);
423 /* unpack the package */
424 err = opkg_install_pkg(new, 0);
428 opkg_package_free (pdata.package);
429 return OPKG_UNKNOWN_ERROR;
432 progress (pdata, 75);
434 /* run configure scripts, etc. */
435 err = opkg_configure_packages (NULL);
438 opkg_package_free (pdata.package);
439 return OPKG_UNKNOWN_ERROR;
442 /* write out status files and file lists */
443 opkg_conf_write_status_files ();
444 pkg_write_changed_filelists ();
446 progress (pdata, 100);
447 opkg_package_free (pdata.package);
452 opkg_remove_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
456 pkg_t *pkg_to_remove;
457 opkg_progress_data_t pdata;
459 opkg_assert (package_name != NULL);
461 pkg_info_preinstall_check ();
463 pkg = pkg_hash_fetch_installed_by_name (package_name);
467 /* XXX: Error: Package not installed. */
468 return OPKG_PACKAGE_NOT_INSTALLED;
471 pdata.action = OPKG_REMOVE;
472 pdata.package = pkg_t_to_opkg_package_t (pkg);
476 if (pkg->state_status == SS_NOT_INSTALLED)
478 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
479 opkg_package_free (pdata.package);
480 return OPKG_PACKAGE_NOT_INSTALLED;
482 progress (pdata, 25);
484 if (conf->restrict_to_default_dest)
486 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (pkg->name,
491 pkg_to_remove = pkg_hash_fetch_installed_by_name (pkg->name);
495 progress (pdata, 75);
497 err = opkg_remove_pkg (pkg_to_remove, 0);
499 /* write out status files and file lists */
500 opkg_conf_write_status_files ();
501 pkg_write_changed_filelists ();
504 progress (pdata, 100);
505 opkg_package_free (pdata.package);
506 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
510 opkg_upgrade_package (const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
514 opkg_progress_data_t pdata;
516 opkg_assert (package_name != NULL);
518 pkg_info_preinstall_check ();
520 if (conf->restrict_to_default_dest)
522 pkg = pkg_hash_fetch_installed_by_name_dest (package_name,
526 /* XXX: Error: Package not installed in default_dest */
527 return OPKG_PACKAGE_NOT_INSTALLED;
532 pkg = pkg_hash_fetch_installed_by_name (package_name);
537 /* XXX: Error: Package not installed */
538 return OPKG_PACKAGE_NOT_INSTALLED;
541 pdata.action = OPKG_INSTALL;
542 pdata.package = pkg_t_to_opkg_package_t (pkg);
545 err = opkg_upgrade_pkg (pkg);
546 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
549 opkg_package_free (pdata.package);
550 return OPKG_UNKNOWN_ERROR;
552 progress (pdata, 75);
554 err = opkg_configure_packages (NULL);
556 opkg_package_free (pdata.package);
557 return OPKG_UNKNOWN_ERROR;
560 /* write out status files and file lists */
561 opkg_conf_write_status_files ();
562 pkg_write_changed_filelists ();
564 progress (pdata, 100);
565 opkg_package_free (pdata.package);
570 opkg_upgrade_all (opkg_progress_callback_t progress_callback, void *user_data)
572 pkg_vec_t *installed;
576 opkg_progress_data_t pdata;
578 pdata.action = OPKG_INSTALL;
579 pdata.package = NULL;
583 installed = pkg_vec_alloc ();
584 pkg_info_preinstall_check ();
586 pkg_hash_fetch_all_installed (installed);
587 for (i = 0; i < installed->len; i++)
589 pkg = installed->pkgs[i];
591 pdata.package = pkg_t_to_opkg_package_t (pkg);
592 progress (pdata, 99 * i / installed->len);
593 opkg_package_free (pdata.package);
595 err += opkg_upgrade_pkg (pkg);
597 pkg_vec_free (installed);
602 err = opkg_configure_packages (NULL);
606 pdata.package = NULL;
607 progress (pdata, 100);
612 opkg_update_package_lists (opkg_progress_callback_t progress_callback, void *user_data)
617 pkg_src_list_elt_t *iter;
619 int sources_list_count, sources_done;
620 opkg_progress_data_t pdata;
622 pdata.action = OPKG_DOWNLOAD;
623 pdata.package = NULL;
626 sprintf_alloc (&lists_dir, "%s",
627 (conf->restrict_to_default_dest)
628 ? conf->default_dest->lists_dir
631 if (!file_is_dir (lists_dir))
633 if (file_exists (lists_dir))
635 /* XXX: Error: file exists but is not a directory */
640 err = file_mkdir_hier (lists_dir, 0755);
643 /* XXX: Error: failed to create directory */
649 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
650 if (mkdtemp (tmp) == NULL) {
651 /* XXX: Error: could not create temporary file name */
657 /* count the number of sources so we can give some progress updates */
658 sources_list_count = 0;
660 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
662 sources_list_count++;
665 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
667 char *url, *list_file_name = NULL;
669 src = (pkg_src_t *)iter->data;
671 if (src->extra_data) /* debian style? */
672 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
673 src->gzip ? "Packages.gz" : "Packages");
675 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
677 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
681 struct _curl_cb_data cb_data;
682 char *tmp_file_name = NULL;
684 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
686 /* XXX: Note: downloading url */
688 cb_data.cb = progress_callback;
689 cb_data.progress_data = &pdata;
690 cb_data.user_data = user_data;
691 cb_data.start_range = 100 * sources_done / sources_list_count;
692 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
694 err = opkg_download (url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
698 /* XXX: Note: Inflating downloaded file */
699 in = fopen (tmp_file_name, "r");
700 out = fopen (list_file_name, "w");
709 unlink (tmp_file_name);
711 free (tmp_file_name);
714 err = opkg_download (url, list_file_name, NULL, NULL);
718 /* XXX: Error: download error */
719 result = OPKG_DOWNLOAD_FAILED;
723 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
724 if ( conf->check_signature ) {
726 /* download detached signitures to verify the package lists */
727 /* get the url for the sig file */
728 if (src->extra_data) /* debian style? */
729 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
732 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
734 /* create filename for signature */
735 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
737 /* make sure there is no existing signature file */
738 unlink (sig_file_name);
740 err = opkg_download (url, sig_file_name, NULL, NULL);
743 /* XXX: Warning: Download failed */
748 err = opkg_verify_file (list_file_name, sig_file_name);
751 /* XXX: Notice: Signature check passed */
755 /* XXX: Warning: Signature check failed */
758 free (sig_file_name);
759 free (list_file_name);
763 /* XXX: Note: Signature check for %s skipped because GPG support was not
764 * enabled in this build
769 progress (pdata, 100 * sources_done / sources_list_count);
776 /* Now re-read the package lists to update package hash tables. */
777 opkg_re_read_config_files ();
784 opkg_list_packages (opkg_package_callback_t callback, void *user_data)
789 opkg_assert (callback);
791 all = pkg_vec_alloc ();
792 pkg_hash_fetch_available (all);
793 for (i = 0; i < all->len; i++)
796 opkg_package_t *package;
800 package = pkg_t_to_opkg_package_t (pkg);
801 callback (package, user_data);
802 opkg_package_free (package);
811 opkg_list_upgradable_packages (opkg_package_callback_t callback, void *user_data)
813 struct active_list *head;
814 struct active_list *node;
815 pkg_t *old=NULL, *new = NULL;
816 static opkg_package_t* package=NULL;
818 opkg_assert (callback);
820 /* ensure all data is valid */
821 pkg_info_preinstall_check ();
823 head = prepare_upgrade_list();
824 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
825 old = list_entry(node, pkg_t, list);
826 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
829 package = pkg_t_to_opkg_package_t (new);
830 callback (package, user_data);
831 opkg_package_free (package);
833 active_list_head_delete(head);
838 opkg_find_package (const char *name, const char *ver, const char *arch, const char *repo)
841 opkg_package_t *package = NULL;
843 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
845 all = pkg_vec_alloc ();
846 pkg_hash_fetch_available (all);
847 for (i = 0; i < all->len; i++)
855 if (sstrcmp (pkg->name, name))
859 pkgv = pkg_version_str_alloc (pkg);
860 if (sstrcmp (pkgv, ver))
867 /* check architecture */
870 if (sstrcmp (pkg->architecture, arch))
874 /* check repository */
877 if (sstrcmp (pkg->src->name, repo))
882 package = pkg_t_to_opkg_package_t (pkg);
892 #include <curl/curl.h>
895 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
896 * @return return how many repositories cannot access. 0 means all okay.
898 int opkg_repository_accessibility_check(void)
900 pkg_src_list_elt_t *iter;
901 str_list_elt_t *iter1;
909 src = str_list_alloc();
911 list_for_each_entry(iter, &conf->pkg_src_list.head, node)
913 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
914 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
915 stmp = xstrndup(((pkg_src_t *)iter->data)->value,
916 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
919 stmp = xstrdup(((pkg_src_t *)iter->data)->value);
921 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
923 if (strstr(iter1->data, stmp))
929 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
932 str_list_append(src, repo_ptr);
936 while (repositories > 0)
938 iter1 = str_list_pop(src);
941 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
943 if (!(err == CURLE_OK ||
944 err == CURLE_HTTP_RETURNED_ERROR ||
945 err == CURLE_FILE_COULDNT_READ_FILE ||
946 err == CURLE_REMOTE_FILE_NOT_FOUND ||
947 err == CURLE_TFTP_NOTFOUND
954 str_list_elt_deinit(iter1);