From 8598dc1a7582f82827cc08fefa89a0e73979d53a Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Wed, 5 Aug 2020 14:46:42 +1000 Subject: [PATCH] disk_cache: add new OS specific helper disk_cache_evict_item() Reviewed-by: Bas Nieuwenhuizen Part-of: --- src/util/disk_cache.c | 13 +------------ src/util/disk_cache_os.c | 16 ++++++++++++++++ src/util/disk_cache_os.h | 3 +++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index 8d6817f8164..c2d5db4f588 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -227,23 +227,12 @@ disk_cache_wait_for_idle(struct disk_cache *cache) void disk_cache_remove(struct disk_cache *cache, const cache_key key) { - struct stat sb; - char *filename = disk_cache_get_cache_filename(cache, key); if (filename == NULL) { return; } - if (stat(filename, &sb) == -1) { - free(filename); - return; - } - - unlink(filename); - free(filename); - - if (sb.st_blocks) - p_atomic_add(cache->size, - (uint64_t)sb.st_blocks * 512); + disk_cache_evict_item(cache, filename); } static ssize_t diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c index 28885820bf1..d23b2f7d82f 100644 --- a/src/util/disk_cache_os.c +++ b/src/util/disk_cache_os.c @@ -438,6 +438,22 @@ disk_cache_evict_lru_item(struct disk_cache *cache) p_atomic_add(cache->size, - (uint64_t)size); } +void +disk_cache_evict_item(struct disk_cache *cache, char *filename) +{ + struct stat sb; + if (stat(filename, &sb) == -1) { + free(filename); + return; + } + + unlink(filename); + free(filename); + + if (sb.st_blocks) + p_atomic_add(cache->size, - (uint64_t)sb.st_blocks * 512); +} + /* Return a filename within the cache's directory corresponding to 'key'. The * returned filename is ralloced with 'cache' as the parent context. * diff --git a/src/util/disk_cache_os.h b/src/util/disk_cache_os.h index 04102545aa7..e753d08bd99 100644 --- a/src/util/disk_cache_os.h +++ b/src/util/disk_cache_os.h @@ -100,6 +100,9 @@ disk_cache_generate_cache_dir(void *mem_ctx); void disk_cache_evict_lru_item(struct disk_cache *cache); +void +disk_cache_evict_item(struct disk_cache *cache, char *filename); + char * disk_cache_get_cache_filename(struct disk_cache *cache, const cache_key key);