download: factor out the logic for building cache filenames
authorBaptiste Jonglez <[email protected]>
Mon, 24 Aug 2020 23:00:30 +0000 (01:00 +0200)
committerBaptiste Jonglez <[email protected]>
Sun, 31 Jan 2021 09:56:12 +0000 (10:56 +0100)
If we want to access files in the cache from multiple functions, it is
necessary to have a single source of truth regarding the naming of files
in the cache.

Signed-off-by: Baptiste Jonglez <[email protected]>
(cherry picked from commit 1c1480e573fc6b4c5f9c51bf225c32456672e5f8)

libopkg/opkg_download.c

index f7c2f88e4294e6790f5126cf91cffd873ec2184a..175282c28aa1439d5f941fb5dc3b0d06fb25b618 100644 (file)
@@ -206,6 +206,17 @@ opkg_download(const char *src, const char *dest_file_name,
        return err;
 }
 
+static char* get_cache_filename(const char *dest_file_name)
+{
+       char *cache_name;
+       char *filename = strrchr(dest_file_name, '/');
+       if (filename)
+               cache_name = xstrdup(filename + 1);     // strip leading '/'
+       else
+               cache_name = xstrdup(dest_file_name);
+       return cache_name;
+}
+
 static int
 opkg_download_cache(const char *src, const char *dest_file_name)
 {
@@ -223,11 +234,7 @@ opkg_download_cache(const char *src, const char *dest_file_name)
                goto out1;
        }
 
-       char *filename = strrchr(dest_file_name, '/');
-       if (filename)
-               cache_name = xstrdup(filename + 1);     // strip leading '/'
-       else
-               cache_name = xstrdup(dest_file_name);
+       cache_name = get_cache_filename(dest_file_name);
        sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
        if (file_exists(cache_location))
                opkg_msg(NOTICE, "Copying %s.\n", cache_location);