Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame^] | 1 | From 61636f15718edc7ea17b91f22f1d97b905eaf951 Mon Sep 17 00:00:00 2001 |
| 2 | From: Paul Barker <paul@paulbarker.me.uk> |
| 3 | Date: Sat, 7 Nov 2015 10:23:52 +0000 |
| 4 | Subject: [PATCH 4/4] opkg_download: Use short cache file name |
| 5 | |
| 6 | Source URIs can be very long. The cache directory itself may already have a very |
| 7 | long path, especially if we're installing packages into an offline rootfs. |
| 8 | Therefore it's not a good idea to simply tag the source URI onto the cache |
| 9 | directory path to create a cache file name. |
| 10 | |
| 11 | To create shorter cache file names which are deterministic and very likely to be |
| 12 | unique, we use the md5sum of the source URI along with the basename of the |
| 13 | source URI. The basename is length limited to ensure that it the resulting |
| 14 | filename length is always reasonable. |
| 15 | |
| 16 | Signed-off-by: Paul Barker <paul@paulbarker.me.uk> |
| 17 | Signed-off-by: Alejandro del Castillo <alejandro.delcastillo@ni.com> |
| 18 | |
| 19 | Upstream-Status: Accepted |
| 20 | --- |
| 21 | libopkg/opkg_download.c | 35 ++++++++++++++++++++++++++++------- |
| 22 | 1 file changed, 28 insertions(+), 7 deletions(-) |
| 23 | |
| 24 | diff --git a/libopkg/opkg_download.c b/libopkg/opkg_download.c |
| 25 | index e9b86a5..a37b10d 100644 |
| 26 | --- a/libopkg/opkg_download.c |
| 27 | +++ b/libopkg/opkg_download.c |
| 28 | @@ -29,10 +29,18 @@ |
| 29 | #include "opkg_verify.h" |
| 30 | #include "opkg_utils.h" |
| 31 | |
| 32 | +#include "md5.h" |
| 33 | #include "sprintf_alloc.h" |
| 34 | #include "file_util.h" |
| 35 | #include "xfuncs.h" |
| 36 | |
| 37 | +/* Limit the short file name used to generate cache file names to 90 characters |
| 38 | + * so that when added to the md5sum (32 characters) and an underscore, the |
| 39 | + * resulting length is below 128 characters. The maximum file name length |
| 40 | + * differs between plaforms but 128 characters should be reasonable. |
| 41 | + */ |
| 42 | +#define MAX_SHORT_FILE_NAME_LENGTH 90 |
| 43 | + |
| 44 | static int opkg_download_set_env() |
| 45 | { |
| 46 | int r; |
| 47 | @@ -135,15 +143,28 @@ int opkg_download_internal(const char *src, const char *dest, |
| 48 | */ |
| 49 | char *get_cache_location(const char *src) |
| 50 | { |
| 51 | - char *cache_name = xstrdup(src); |
| 52 | - char *cache_location, *p; |
| 53 | + unsigned char md5sum_bin[16]; |
| 54 | + char *md5sum_hex; |
| 55 | + char *cache_location; |
| 56 | + char *short_file_name; |
| 57 | + char *tmp = xstrdup(src); |
| 58 | |
| 59 | - for (p = cache_name; *p; p++) |
| 60 | - if (*p == '/') |
| 61 | - *p = '_'; |
| 62 | + md5_buffer(src, strlen(src), md5sum_bin); |
| 63 | + md5sum_hex = md5_to_string(md5sum_bin); |
| 64 | |
| 65 | - sprintf_alloc(&cache_location, "%s/%s", opkg_config->cache_dir, cache_name); |
| 66 | - free(cache_name); |
| 67 | + /* Generate a short file name which will be used along with an md5sum of the |
| 68 | + * full src URI in the cache file name. This short file name is limited to |
| 69 | + * MAX_SHORT_FILE_NAME_LENGTH to ensure that the total cache file name |
| 70 | + * length is reasonable. |
| 71 | + */ |
| 72 | + short_file_name = basename(tmp); |
| 73 | + if (strlen(short_file_name) > MAX_SHORT_FILE_NAME_LENGTH) |
| 74 | + short_file_name[MAX_SHORT_FILE_NAME_LENGTH] = '\0'; |
| 75 | + |
| 76 | + sprintf_alloc(&cache_location, "%s/%s_%s", opkg_config->cache_dir, |
| 77 | + md5sum_hex, short_file_name); |
| 78 | + free(md5sum_hex); |
| 79 | + free(tmp); |
| 80 | return cache_location; |
| 81 | } |
| 82 | |
| 83 | -- |
| 84 | 1.9.1 |
| 85 | |