diff mbox series

[opkg,3/5] download: factor out the logic for building cache filenames

Message ID 20200824230032.492951-4-baptiste@bitsofnetworks.org
State Accepted
Headers show
Series Purge packages from cache when they have incorrect checksum | expand

Commit Message

Baptiste Jonglez Aug. 24, 2020, 11 p.m. UTC
From: Baptiste Jonglez <git@bitsofnetworks.org>

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 <git@bitsofnetworks.org>
---
 libopkg/opkg_download.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/libopkg/opkg_download.c b/libopkg/opkg_download.c
index f7c2f88..175282c 100644
--- a/libopkg/opkg_download.c
+++ b/libopkg/opkg_download.c
@@ -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);