opkg: trivial, clean up obsolete code, and some typo
[oweals/opkg-lede.git] / libopkg / opkg_download.c
index 1875195c12118aba4e2578ec8253d0efa609aad3..04073cf593f3659acd6749d8c76cd6ac888b2fd7 100644 (file)
@@ -33,7 +33,8 @@
 #include "str_util.h"
 #include "opkg_defines.h"
 
-int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data)
+int opkg_download(opkg_conf_t *conf, const char *src,
+  const char *dest_file_name, curl_progress_func cb, void *data)
 {
     int err = 0;
 
@@ -135,6 +136,43 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
     return 0;
 }
 
+static int opkg_download_cache(opkg_conf_t *conf, const char *src,
+  const char *dest_file_name, curl_progress_func cb, void *data)
+{
+    char *cache_name = strdup(src);
+    char *cache_location, *p;
+    int err = 0;
+
+    if (!conf->cache || str_starts_with(src, "file:")) {
+       err = opkg_download(conf, src, dest_file_name, cb, data);
+       goto out1;
+    }
+
+    for (p = cache_name; *p; p++)
+       if (*p == '/')
+           *p = ',';   /* looks nicer than | or # */
+
+    sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
+    if (file_exists(cache_location))
+       opkg_message(conf, OPKG_NOTICE, "Copying %s\n", cache_location);
+    else {
+       err = opkg_download(conf, src, cache_location, cb, data);
+       if (err) {
+           (void) unlink(cache_location);
+           goto out2;
+       }
+    }
+
+    err = file_copy(cache_location, dest_file_name);
+
+
+out2:
+    free(cache_location);
+out1:
+    free(cache_name);
+    return err;
+}
+
 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
 {
     int err;
@@ -165,7 +203,7 @@ int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
 
     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
 
-    err = opkg_download(conf, url, pkg->local_filename, NULL, NULL);
+    err = opkg_download_cache(conf, url, pkg->local_filename, NULL, NULL);
     free(url);
 
     opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);