opkg: adding the hash_table_remove API, not using yet.
[oweals/opkg-lede.git] / libopkg / opkg.c
index 9f081cf3937ad5b953d0989d98fd96e9e4a286c7..35ddb89bcebee341774dc75aac8cce4e67e409cc 100644 (file)
@@ -34,6 +34,8 @@
 
 #include <libbb/libbb.h>
 
+struct errlist* error_list;
+
 struct _opkg_t
 {
   args_t *args;
@@ -161,8 +163,7 @@ opkg_package_new ()
 
   opkg_package_t *p;
 
-  p = malloc (sizeof (opkg_package_t));
-  memset (p, 0, sizeof (opkg_package_t));
+  p = calloc (1, sizeof (opkg_package_t));
 
   return p;
 }
@@ -187,9 +188,9 @@ opkg_new ()
   opkg_t *opkg;
   int err;
 
-  opkg = malloc (sizeof (opkg_t));
+  opkg = calloc (1, sizeof (opkg_t));
 
-  opkg->args = malloc (sizeof (args_t));
+  opkg->args = calloc (1, sizeof (args_t));
   err = args_init (opkg->args);
   if (err)
   {
@@ -198,7 +199,7 @@ opkg_new ()
     return NULL;
   }
 
-  opkg->conf = malloc (sizeof (opkg_conf_t));
+  opkg->conf = calloc (1, sizeof (opkg_conf_t));
   err = opkg_conf_init (opkg->conf, opkg->args);
   if (err)
   {
@@ -271,6 +272,12 @@ opkg_re_read_config_files (opkg_t *opkg)
     a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
   }
 
+  if (c->cache) {
+    if (a->cache)
+       free (a->cache);
+    a->cache = strdup(c->cache);
+  }
+
   /* throw away old opkg_conf and start again */
   opkg_conf_deinit (opkg->conf);
   opkg_conf_init (opkg->conf, opkg->args);
@@ -564,6 +571,7 @@ opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callb
   if (pkg->state_status == SS_NOT_INSTALLED)
   {
     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
+    opkg_package_free (pdata.package);
     return OPKG_PACKAGE_NOT_INSTALLED;
   }
   progress (pdata, 25);
@@ -639,6 +647,8 @@ opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_call
   /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
   if (err)
   {
+
+    opkg_package_free (pdata.package);
     switch (err)
     {
       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
@@ -654,8 +664,15 @@ opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_call
   progress (pdata, 75);
 
   err = opkg_configure_packages (opkg->conf, NULL);
-  if (err)
+  if (err) {
+    opkg_package_free (pdata.package);  
     return OPKG_UNKNOWN_ERROR;
+  }
+
+  /* write out status files and file lists */
+  opkg_conf_write_status_files (opkg->conf);
+  pkg_write_changed_filelists (opkg->conf);
+
   progress (pdata, 100);
   opkg_package_free (pdata.package);
   return 0;
@@ -861,7 +878,7 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
     free (list_file_name);
     free (url);
 #else
-    /* XXX: Note: Signiture check for %s skipped because GPG support was not
+    /* XXX: Note: Signature check for %s skipped because GPG support was not
      * enabled in this build
      */
 #endif
@@ -901,6 +918,7 @@ opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_d
 
     package = old_pkg_to_new (pkg);
     callback (opkg, package, user_data);
+    opkg_package_free (package);
   }
 
   pkg_vec_free (all);
@@ -945,6 +963,7 @@ opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, v
     {
       package = old_pkg_to_new (new);
       callback (opkg, package, user_data);
+      opkg_package_free (package);
     }
   }
 
@@ -1008,3 +1027,68 @@ opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *
 
   return package;
 }
+
+#include <curl/curl.h>
+/**
+ * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
+ * @param opkg The opkg_t
+ * @return return how many repositories cannot access. 0 means all okay. 
+ */ 
+int opkg_repository_accessibility_check(opkg_t *opkg) 
+{
+  pkg_src_list_elt_t *iter;
+  str_list_elt_t *iter1;
+  str_list_t *src;
+  int repositories=0;
+  int ret=0;
+  int err;
+  char *repo_ptr;
+  char *stmp;
+  opkg_assert(opkg != NULL);
+
+  src = str_list_alloc();
+
+  for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next) 
+  {
+    if (strstr(iter->data->value, "://") && 
+                   index(strstr(iter->data->value, "://") + 3, '/')) 
+      stmp = strndup(iter->data->value, 
+                     (index(strstr(iter->data->value, "://") + 3, '/') - iter->data->value)*sizeof(char));
+
+    else
+      stmp = strdup(iter->data->value);
+
+    for (iter1 = src->head; iter1; iter1 = iter1->next)
+    {
+      if (strstr(iter1->data, stmp)) 
+        break;
+    }
+    if (iter1)
+      continue;
+
+    sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
+    free(stmp);
+
+    str_list_append(src, repo_ptr);
+    repositories++;
+  }
+  while (repositories > 0) 
+  {
+    iter1 = str_list_pop(src);
+    repositories--;
+
+    err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
+    if (!(err == CURLE_OK || 
+               err == CURLE_HTTP_RETURNED_ERROR || 
+               err == CURLE_FILE_COULDNT_READ_FILE ||
+               err == CURLE_REMOTE_FILE_NOT_FOUND || 
+               err == CURLE_TFTP_NOTFOUND
+               )) {
+           ret++;
+    }
+    str_list_elt_deinit(iter1);
+    free(iter1);
+  }
+  free(src);
+  return ret;
+}