a8f85a95fb21f86e4c1518091737b68fc6d446da
[oweals/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg  package management system
2
3    Thomas Wood <thomas@openedhand.com>
4
5    Copyright (C) 2008 OpenMoko Inc
6
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.
11
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.
16  */
17
18 #include <config.h>
19 #include <fnmatch.h>
20
21 #include "opkg.h"
22 #include "opkg_conf.h"
23
24 #include "opkg_install.h"
25 #include "opkg_configure.h"
26 #include "opkg_download.h"
27 #include "opkg_remove.h"
28 #include "opkg_upgrade.h"
29
30 #include "sprintf_alloc.h"
31 #include "file_util.h"
32
33 #include <libbb/libbb.h>
34
35 #define opkg_assert(expr) if (!(expr)) { \
36     printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
37             __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
38
39 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
40
41 /** Private Functions ***/
42
43 static int
44 opkg_configure_packages(char *pkg_name)
45 {
46         pkg_vec_t *all;
47         int i;
48         pkg_t *pkg;
49         int r, err = 0;
50
51         all = pkg_vec_alloc();
52         pkg_hash_fetch_available(all);
53
54         for (i = 0; i < all->len; i++) {
55                 pkg = all->pkgs[i];
56
57                 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
58                         continue;
59
60                 if (pkg->state_status == SS_UNPACKED) {
61                         r = opkg_configure(pkg);
62                         if (r == 0) {
63                                 pkg->state_status = SS_INSTALLED;
64                                 pkg->parent->state_status = SS_INSTALLED;
65                                 pkg->state_flag &= ~SF_PREFER;
66                         } else {
67                                 if (!err)
68                                         err = r;
69                         }
70                 }
71         }
72
73         pkg_vec_free(all);
74         return err;
75 }
76
77 struct _curl_cb_data {
78         opkg_progress_callback_t cb;
79         opkg_progress_data_t *progress_data;
80         void *user_data;
81         int start_range;
82         int finish_range;
83 };
84
85 int
86 curl_progress_cb(struct _curl_cb_data *cb_data, double t,       /* dltotal */
87                 double d,       /* dlnow */
88                 double ultotal, double ulnow)
89 {
90         int p = (t) ? d * 100 / t : 0;
91         static int prev = -1;
92         int progress = 0;
93
94         /* prevent the same value being sent twice (can occur due to rounding) */
95         if (p == prev)
96                 return 0;
97         prev = p;
98
99         if (t < 1)
100                 return 0;
101
102         progress = cb_data->start_range +
103             (d / t * ((cb_data->finish_range - cb_data->start_range)));
104         cb_data->progress_data->percentage = progress;
105
106         (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
107
108         return 0;
109 }
110
111
112 /*** Public API ***/
113
114 int
115 opkg_new()
116 {
117         if (opkg_conf_init())
118                 goto err0;
119
120         if (pkg_hash_load_feeds())
121                 goto err1;
122
123         if (pkg_hash_load_status_files())
124                 goto err1;
125
126         return 0;
127
128 err1:
129         pkg_hash_deinit();
130 err0:
131         opkg_conf_deinit();
132         return -1;
133 }
134
135 void
136 opkg_free(void)
137 {
138 #ifdef HAVE_CURL
139         opkg_curl_cleanup();
140 #endif
141         opkg_conf_deinit();
142 }
143
144 int
145 opkg_re_read_config_files(void)
146 {
147         pkg_hash_deinit();
148         pkg_hash_init();
149
150         if (pkg_hash_load_feeds())
151                 goto err;
152
153         if (pkg_hash_load_status_files())
154                 goto err;
155
156         return 0;
157
158 err:
159         pkg_hash_deinit();
160         return -1;
161 }
162
163 void
164 opkg_get_option(char *option, void **value)
165 {
166         int i = 0;
167         extern opkg_option_t options[];
168
169         /* look up the option
170          * TODO: this would be much better as a hash table
171          */
172         while (options[i].name) {
173                 if (strcmp(options[i].name, option) != 0) {
174                         i++;
175                         continue;
176                 }
177         }
178
179         /* get the option */
180         switch (options[i].type) {
181         case OPKG_OPT_TYPE_BOOL:
182                 *((int *) value) = *((int *) options[i].value);
183                 return;
184
185         case OPKG_OPT_TYPE_INT:
186                 *((int *) value) = *((int *) options[i].value);
187                 return;
188
189         case OPKG_OPT_TYPE_STRING:
190                 *((char **) value) = xstrdup(options[i].value);
191                 return;
192         }
193
194 }
195
196 void
197 opkg_set_option(char *option, void *value)
198 {
199         int i = 0, found = 0;
200         extern opkg_option_t options[];
201
202         opkg_assert(option != NULL);
203         opkg_assert(value != NULL);
204
205         /* look up the option
206          * TODO: this would be much better as a hash table
207          */
208         while (options[i].name) {
209                 if (strcmp(options[i].name, option) == 0) {
210                         found = 1;
211                         break;
212                 }
213                 i++;
214         }
215
216         if (!found) {
217                 opkg_msg(ERROR, "Invalid option: %s\n", option);
218                 return;
219         }
220
221         /* set the option */
222         switch (options[i].type) {
223         case OPKG_OPT_TYPE_BOOL:
224                 if (*((int *) value) == 0)
225                         *((int *) options[i].value) = 0;
226                 else
227                         *((int *) options[i].value) = 1;
228                 return;
229
230         case OPKG_OPT_TYPE_INT:
231                 *((int *) options[i].value) = *((int *) value);
232                 return;
233
234         case OPKG_OPT_TYPE_STRING:
235                 *((char **) options[i].value) = xstrdup(value);
236                 return;
237         }
238
239 }
240
241 /**
242  * @brief libopkg API: Install package
243  * @param package_name The name of package in which is going to install
244  * @param progress_callback The callback function that report the status to caller. 
245  */
246 int
247 opkg_install_package(const char *package_name,
248                 opkg_progress_callback_t progress_callback,
249                 void *user_data)
250 {
251         int err;
252         char *stripped_filename;
253         opkg_progress_data_t pdata;
254         pkg_t *old, *new;
255         pkg_vec_t *deps, *all;
256         int i, ndepends;
257         char **unresolved = NULL;
258
259         opkg_assert(package_name != NULL);
260
261         /* ... */
262         pkg_info_preinstall_check();
263
264
265         /* check to ensure package is not already installed */
266         old = pkg_hash_fetch_installed_by_name(package_name);
267         if (old) {
268                 opkg_msg(ERROR, "Package %s is already installed\n",
269                                 package_name);
270                 return -1;
271         }
272
273         new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
274         if (!new) {
275                 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
276                 return -1;
277         }
278
279         new->state_flag |= SF_USER;
280
281         pdata.action = -1;
282         pdata.pkg = new;
283
284         progress(pdata, 0);
285
286         /* find dependancies and download them */
287         deps = pkg_vec_alloc();
288         /* this function does not return the original package, so we insert it later */
289         ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
290                         &unresolved);
291         if (unresolved) {
292                 char **tmp = unresolved;
293                 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
294                                " for %s:\n", package_name);
295                 while (*tmp) {
296                         opkg_msg(ERROR, "\t%s", *tmp);
297                         free(*tmp);
298                         tmp++;
299                 }
300                 free(unresolved);
301                 pkg_vec_free(deps);
302                 opkg_message(ERROR, "\n");
303                 return -1;
304         }
305
306         /* insert the package we are installing so that we download it */
307         pkg_vec_insert(deps, new);
308
309         /* download package and dependencies */
310         for (i = 0; i < deps->len; i++) {
311                 pkg_t *pkg;
312                 struct _curl_cb_data cb_data;
313                 char *url;
314
315                 pkg = deps->pkgs[i];
316                 if (pkg->local_filename)
317                         continue;
318
319                 pdata.pkg = pkg;
320                 pdata.action = OPKG_DOWNLOAD;
321
322                 if (pkg->src == NULL) {
323                         opkg_msg(ERROR, "Package %s not available from any "
324                                         "configured src\n", package_name);
325                         return -1;
326                 }
327
328                 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
329
330                 /* Get the filename part, without any directory */
331                 stripped_filename = strrchr(pkg->filename, '/');
332                 if (!stripped_filename)
333                         stripped_filename = pkg->filename;
334
335                 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
336                               stripped_filename);
337
338                 cb_data.cb = progress_callback;
339                 cb_data.progress_data = &pdata;
340                 cb_data.user_data = user_data;
341                 /* 75% of "install" progress is for downloading */
342                 cb_data.start_range = 75 * i / deps->len;
343                 cb_data.finish_range = 75 * (i + 1) / deps->len;
344
345                 err = opkg_download(url, pkg->local_filename,
346                                     (curl_progress_func) curl_progress_cb,
347                                     &cb_data);
348                 free(url);
349
350                 if (err) {
351                         pkg_vec_free(deps);
352                         return -1;
353                 }
354
355         }
356         pkg_vec_free(deps);
357
358         /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
359         all = pkg_vec_alloc();
360         pkg_hash_fetch_available(all);
361         for (i = 0; i < all->len; i++) {
362                 all->pkgs[i]->parent->dependencies_checked = 0;
363         }
364         pkg_vec_free(all);
365
366
367         /* 75% of "install" progress is for downloading */
368         pdata.pkg = new;
369         pdata.action = OPKG_INSTALL;
370         progress(pdata, 75);
371
372         /* unpack the package */
373         err = opkg_install_pkg(new, 0);
374
375         if (err) {
376                 return -1;
377         }
378
379         progress(pdata, 75);
380
381         /* run configure scripts, etc. */
382         err = opkg_configure_packages(NULL);
383         if (err) {
384                 return -1;
385         }
386
387         /* write out status files and file lists */
388         opkg_conf_write_status_files();
389         pkg_write_changed_filelists();
390
391         progress(pdata, 100);
392         return 0;
393 }
394
395 int
396 opkg_remove_package(const char *package_name,
397                 opkg_progress_callback_t progress_callback, void *user_data)
398 {
399         int err;
400         pkg_t *pkg = NULL;
401         pkg_t *pkg_to_remove;
402         opkg_progress_data_t pdata;
403
404         opkg_assert(package_name != NULL);
405
406         pkg_info_preinstall_check();
407
408         pkg = pkg_hash_fetch_installed_by_name(package_name);
409
410         if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
411                 opkg_msg(ERROR, "Package %s not installed\n", package_name);
412                 return -1;
413         }
414
415         pdata.action = OPKG_REMOVE;
416         pdata.pkg = pkg;
417         progress(pdata, 0);
418
419         if (conf->restrict_to_default_dest) {
420                 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
421                                                       conf->default_dest);
422         } else {
423                 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
424         }
425
426
427         progress(pdata, 75);
428
429         err = opkg_remove_pkg(pkg_to_remove, 0);
430
431         /* write out status files and file lists */
432         opkg_conf_write_status_files();
433         pkg_write_changed_filelists();
434
435
436         progress(pdata, 100);
437         return (err) ? -1 : 0;
438 }
439
440 int
441 opkg_upgrade_package(const char *package_name,
442                 opkg_progress_callback_t progress_callback,
443                 void *user_data)
444 {
445         int err;
446         pkg_t *pkg;
447         opkg_progress_data_t pdata;
448
449         opkg_assert(package_name != NULL);
450
451         pkg_info_preinstall_check();
452
453         if (conf->restrict_to_default_dest) {
454                 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
455                                                             conf->default_dest);
456         } else {
457                 pkg = pkg_hash_fetch_installed_by_name(package_name);
458         }
459
460         if (!pkg) {
461                 opkg_msg(ERROR, "Package %s not installed\n", package_name);
462                 return -1;
463         }
464
465         pdata.action = OPKG_INSTALL;
466         pdata.pkg = pkg;
467         progress(pdata, 0);
468
469         err = opkg_upgrade_pkg(pkg);
470         if (err) {
471                 return -1;
472         }
473         progress(pdata, 75);
474
475         err = opkg_configure_packages(NULL);
476         if (err) {
477                 return -1;
478         }
479
480         /* write out status files and file lists */
481         opkg_conf_write_status_files();
482         pkg_write_changed_filelists();
483
484         progress(pdata, 100);
485         return 0;
486 }
487
488 int
489 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
490 {
491         pkg_vec_t *installed;
492         int err = 0;
493         int i;
494         pkg_t *pkg;
495         opkg_progress_data_t pdata;
496
497         pdata.action = OPKG_INSTALL;
498         pdata.pkg = NULL;
499
500         progress(pdata, 0);
501
502         installed = pkg_vec_alloc();
503         pkg_info_preinstall_check();
504
505         pkg_hash_fetch_all_installed(installed);
506         for (i = 0; i < installed->len; i++) {
507                 pkg = installed->pkgs[i];
508
509                 pdata.pkg = pkg;
510                 progress(pdata, 99 * i / installed->len);
511
512                 err += opkg_upgrade_pkg(pkg);
513         }
514         pkg_vec_free(installed);
515
516         if (err)
517                 return 1;
518
519         err = opkg_configure_packages(NULL);
520         if (err)
521                 return 1;
522
523         pdata.pkg = NULL;
524         progress(pdata, 100);
525         return 0;
526 }
527
528 int
529 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
530                         void *user_data)
531 {
532         char *tmp;
533         int err, result = 0;
534         char *lists_dir;
535         pkg_src_list_elt_t *iter;
536         pkg_src_t *src;
537         int sources_list_count, sources_done;
538         opkg_progress_data_t pdata;
539
540         pdata.action = OPKG_DOWNLOAD;
541         pdata.pkg = NULL;
542         progress(pdata, 0);
543
544         sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
545                 ? conf->default_dest->lists_dir : conf->lists_dir);
546
547         if (!file_is_dir(lists_dir)) {
548                 if (file_exists(lists_dir)) {
549                         opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
550                         free(lists_dir);
551                         return 1;
552                 }
553
554                 err = file_mkdir_hier(lists_dir, 0755);
555                 if (err) {
556                         opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
557                                         lists_dir);
558                         free(lists_dir);
559                         return 1;
560                 }
561         }
562
563         sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
564         if (mkdtemp(tmp) == NULL) {
565                 opkg_perror(ERROR, "Coundn't create temporary directory %s",
566                                 tmp);
567                 free(lists_dir);
568                 free(tmp);
569                 return 1;
570         }
571
572         /* count the number of sources so we can give some progress updates */
573         sources_list_count = 0;
574         sources_done = 0;
575         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
576                 sources_list_count++;
577         }
578
579         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
580                 char *url, *list_file_name = NULL;
581
582                 src = (pkg_src_t *) iter->data;
583
584                 if (src->extra_data)    /* debian style? */
585                         sprintf_alloc(&url, "%s/%s/%s", src->value,
586                                       src->extra_data,
587                                       src->gzip ? "Packages.gz" : "Packages");
588                 else
589                         sprintf_alloc(&url, "%s/%s", src->value,
590                                       src->gzip ? "Packages.gz" : "Packages");
591
592                 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
593                 if (src->gzip) {
594                         FILE *in, *out;
595                         struct _curl_cb_data cb_data;
596                         char *tmp_file_name = NULL;
597
598                         sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
599                                       src->name);
600
601                         opkg_msg(INFO, "Downloading %s to %s...\n", url,
602                                         tmp_file_name);
603
604                         cb_data.cb = progress_callback;
605                         cb_data.progress_data = &pdata;
606                         cb_data.user_data = user_data;
607                         cb_data.start_range =
608                             100 * sources_done / sources_list_count;
609                         cb_data.finish_range =
610                             100 * (sources_done + 1) / sources_list_count;
611
612                         err = opkg_download(url, tmp_file_name,
613                                           (curl_progress_func) curl_progress_cb,
614                                           &cb_data);
615
616                         if (err == 0) {
617                                 opkg_msg(INFO, "Inflating %s...\n",
618                                                 tmp_file_name);
619                                 in = fopen(tmp_file_name, "r");
620                                 out = fopen(list_file_name, "w");
621                                 if (in && out)
622                                         unzip(in, out);
623                                 else
624                                         err = 1;
625                                 if (in)
626                                         fclose(in);
627                                 if (out)
628                                         fclose(out);
629                                 unlink(tmp_file_name);
630                         }
631                         free(tmp_file_name);
632                 } else
633                         err = opkg_download(url, list_file_name, NULL, NULL);
634
635                 if (err) {
636                         opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
637                         result = -1;
638                 }
639                 free(url);
640
641 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
642                 if (conf->check_signature) {
643                         char *sig_file_name;
644                         /* download detached signitures to verify the package lists */
645                         /* get the url for the sig file */
646                         if (src->extra_data)    /* debian style? */
647                                 sprintf_alloc(&url, "%s/%s/%s", src->value,
648                                               src->extra_data, "Packages.sig");
649                         else
650                                 sprintf_alloc(&url, "%s/%s", src->value,
651                                               "Packages.sig");
652
653                         /* create filename for signature */
654                         sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
655                                       src->name);
656
657                         /* make sure there is no existing signature file */
658                         unlink(sig_file_name);
659
660                         err = opkg_download(url, sig_file_name, NULL, NULL);
661                         if (err) {
662                                 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
663                         } else {
664                                 int err;
665                                 err = opkg_verify_file(list_file_name,
666                                                      sig_file_name);
667                                 if (err == 0) {
668                                         opkg_msg(INFO, "Signature check "
669                                                         "passed for %s",
670                                                         list_file_name);
671                                 } else {
672                                         opkg_msg(ERROR, "Signature check "
673                                                         "failed for %s",
674                                                         list_file_name);
675                                 }
676                         }
677                         free(sig_file_name);
678                         free(list_file_name);
679                         free(url);
680                 }
681 #else
682                 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
683                                 " has not been enabled in this build\n",
684                                 list_file_name);
685 #endif
686
687                 sources_done++;
688                 progress(pdata, 100 * sources_done / sources_list_count);
689         }
690
691         rmdir(tmp);
692         free(tmp);
693         free(lists_dir);
694
695         /* Now re-read the package lists to update package hash tables. */
696         opkg_re_read_config_files();
697
698         return result;
699 }
700
701 static int
702 pkg_compare_names_and_version(const void *a0, const void *b0)
703 {
704         const pkg_t *a = *(const pkg_t **)a0;
705         const pkg_t *b = *(const pkg_t **)b0;
706         int ret;
707
708         ret = strcmp(a->name, b->name);
709
710         if (ret == 0)
711                 ret = pkg_compare_versions(a, b);
712
713         return ret;
714 }
715
716 int
717 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
718 {
719         pkg_vec_t *all;
720         int i;
721
722         opkg_assert(callback);
723
724         all = pkg_vec_alloc();
725         pkg_hash_fetch_available(all);
726
727         pkg_vec_sort(all, pkg_compare_names_and_version);
728
729         for (i = 0; i < all->len; i++) {
730                 pkg_t *pkg;
731
732                 pkg = all->pkgs[i];
733
734                 callback(pkg, user_data);
735         }
736
737         pkg_vec_free(all);
738
739         return 0;
740 }
741
742 int
743 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
744 {
745         struct active_list *head;
746         struct active_list *node;
747         pkg_t *old = NULL, *new = NULL;
748
749         opkg_assert(callback);
750
751         /* ensure all data is valid */
752         pkg_info_preinstall_check();
753
754         head = prepare_upgrade_list();
755         for (node = active_list_next(head, head); node;
756              active_list_next(head, node)) {
757                 old = list_entry(node, pkg_t, list);
758                 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
759                 if (new == NULL)
760                         continue;
761                 callback(new, user_data);
762         }
763         active_list_head_delete(head);
764         return 0;
765 }
766
767 pkg_t *
768 opkg_find_package(const char *name, const char *ver, const char *arch,
769                 const char *repo)
770 {
771         pkg_t *pkg = NULL;
772         pkg_vec_t *all;
773         int i;
774 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
775
776         all = pkg_vec_alloc();
777         pkg_hash_fetch_available(all);
778         for (i = 0; i < all->len; i++) {
779                 char *pkgv;
780
781                 pkg = all->pkgs[i];
782
783                 /* check name */
784                 if (sstrcmp(pkg->name, name))
785                         continue;
786
787                 /* check version */
788                 pkgv = pkg_version_str_alloc(pkg);
789                 if (sstrcmp(pkgv, ver)) {
790                         free(pkgv);
791                         continue;
792                 }
793                 free(pkgv);
794
795                 /* check architecture */
796                 if (arch) {
797                         if (sstrcmp(pkg->architecture, arch))
798                                 continue;
799                 }
800
801                 /* check repository */
802                 if (repo) {
803                         if (sstrcmp(pkg->src->name, repo))
804                                 continue;
805                 }
806
807                 /* match found */
808                 break;
809         }
810
811         pkg_vec_free(all);
812
813         return pkg;
814 }
815
816 #ifdef HAVE_CURL
817 #include <curl/curl.h>
818 #endif
819 /**
820  * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
821  * @return return how many repositories cannot access. 0 means all okay. 
822  */
823 int
824 opkg_repository_accessibility_check(void)
825 {
826         pkg_src_list_elt_t *iter;
827         str_list_elt_t *iter1;
828         str_list_t *src;
829         int repositories = 0;
830         int ret = 0;
831         int err;
832         char *repo_ptr;
833         char *stmp;
834         char *host, *end;
835
836         src = str_list_alloc();
837
838         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
839                 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
840                 end = index(host, '/');
841                 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
842                         stmp = xstrndup(((pkg_src_t *) iter->data)->value,
843                                      end - ((pkg_src_t *) iter->data)->value);
844                 else
845                         stmp = xstrdup(((pkg_src_t *) iter->data)->value);
846
847                 for (iter1 = str_list_first(src); iter1;
848                      iter1 = str_list_next(src, iter1)) {
849                         if (strstr(iter1->data, stmp))
850                                 break;
851                 }
852                 if (iter1)
853                         continue;
854
855                 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
856                 free(stmp);
857
858                 str_list_append(src, repo_ptr);
859                 free(repo_ptr);
860                 repositories++;
861         }
862         while (repositories > 0) {
863                 iter1 = str_list_pop(src);
864                 repositories--;
865
866                 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
867 #ifdef HAVE_CURL
868                 if (!(err == CURLE_OK ||
869                       err == CURLE_HTTP_RETURNED_ERROR ||
870                       err == CURLE_FILE_COULDNT_READ_FILE ||
871                       err == CURLE_REMOTE_FILE_NOT_FOUND ||
872                       err == CURLE_TFTP_NOTFOUND)) {
873 #else
874                 if (!(err == 0)) {
875 #endif
876                         ret++;
877                 }
878                 str_list_elt_deinit(iter1);
879         }
880         free(src);
881         return ret;
882 }