+ char *tmp;
+ int err, result = 0;
+ char *lists_dir;
+ pkg_src_list_elt_t *iter;
+ pkg_src_t *src;
+ int sources_list_count, sources_done;
+ opkg_progress_data_t pdata;
+
+ pdata.action = OPKG_DOWNLOAD;
+ pdata.pkg = NULL;
+ progress(pdata, 0);
+
+ sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
+ ? conf->default_dest->lists_dir : conf->lists_dir);
+
+ if (!file_is_dir(lists_dir)) {
+ if (file_exists(lists_dir)) {
+ opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
+ free(lists_dir);
+ return 1;
+ }
+
+ err = file_mkdir_hier(lists_dir, 0755);
+ if (err) {
+ opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
+ lists_dir);
+ free(lists_dir);
+ return 1;
+ }
+ }
+
+ sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
+ if (mkdtemp(tmp) == NULL) {
+ opkg_perror(ERROR, "Coundn't create temporary directory %s",
+ tmp);
+ free(lists_dir);
+ free(tmp);
+ return 1;
+ }
+
+ /* count the number of sources so we can give some progress updates */
+ sources_list_count = 0;
+ sources_done = 0;
+ list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
+ sources_list_count++;
+ }
+
+ list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
+ char *url, *list_file_name = NULL;
+
+ src = (pkg_src_t *) iter->data;
+
+ if (src->extra_data) /* debian style? */
+ sprintf_alloc(&url, "%s/%s/%s", src->value,
+ src->extra_data,
+ src->gzip ? "Packages.gz" : "Packages");
+ else
+ sprintf_alloc(&url, "%s/%s", src->value,
+ src->gzip ? "Packages.gz" : "Packages");
+
+ sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
+ if (src->gzip) {
+ FILE *in, *out;
+ struct _curl_cb_data cb_data;
+ char *tmp_file_name = NULL;
+
+ sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
+ src->name);
+
+ opkg_msg(INFO, "Downloading %s to %s...\n", url,
+ tmp_file_name);
+
+ cb_data.cb = progress_callback;
+ cb_data.progress_data = &pdata;
+ cb_data.user_data = user_data;
+ cb_data.start_range =
+ 100 * sources_done / sources_list_count;
+ cb_data.finish_range =
+ 100 * (sources_done + 1) / sources_list_count;
+
+ err = opkg_download(url, tmp_file_name,
+ (curl_progress_func) curl_progress_cb,
+ &cb_data);
+
+ if (err == 0) {
+ opkg_msg(INFO, "Inflating %s...\n",
+ tmp_file_name);
+ in = fopen(tmp_file_name, "r");
+ out = fopen(list_file_name, "w");
+ if (in && out)
+ unzip(in, out);
+ else
+ err = 1;
+ if (in)
+ fclose(in);
+ if (out)
+ fclose(out);
+ unlink(tmp_file_name);
+ }
+ free(tmp_file_name);
+ } else
+ err = opkg_download(url, list_file_name, NULL, NULL);
+
+ if (err) {
+ opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
+ result = -1;
+ }
+ free(url);
+
+#if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
+ if (conf->check_signature) {
+ char *sig_file_name;
+ /* download detached signitures to verify the package lists */
+ /* get the url for the sig file */
+ if (src->extra_data) /* debian style? */
+ sprintf_alloc(&url, "%s/%s/%s", src->value,
+ src->extra_data, "Packages.sig");
+ else
+ sprintf_alloc(&url, "%s/%s", src->value,
+ "Packages.sig");
+
+ /* create filename for signature */
+ sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
+ src->name);
+
+ /* make sure there is no existing signature file */
+ unlink(sig_file_name);
+
+ err = opkg_download(url, sig_file_name, NULL, NULL);
+ if (err) {
+ opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
+ } else {
+ int err;
+ err = opkg_verify_file(list_file_name,
+ sig_file_name);
+ if (err == 0) {
+ opkg_msg(INFO, "Signature check "
+ "passed for %s",
+ list_file_name);
+ } else {
+ opkg_msg(ERROR, "Signature check "
+ "failed for %s",
+ list_file_name);
+ }
+ }
+ free(sig_file_name);
+ free(list_file_name);
+ free(url);
+ }
+#else
+ opkg_msg(INFO, "Signature check skipped for %s as GPG support"
+ " has not been enabled in this build\n",
+ list_file_name);
+#endif