From: Mike Brady Date: Thu, 18 Jan 2018 15:19:04 +0000 (+0000) Subject: Delete previous cover art files but keep the current one. X-Git-Tag: 3.2d29~106 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e3f82f765671e95613795c7efc3d39e8e01c91f;p=thirdparty%2Fshairport-sync.git Delete previous cover art files but keep the current one. --- diff --git a/metadata_hub.c b/metadata_hub.c index 174aca43..7a03888b 100644 --- a/metadata_hub.c +++ b/metadata_hub.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "config.h" @@ -84,6 +85,8 @@ void run_metadata_watchers(void) { void metadata_write_image_file(const char *buf, int len) { +// warning -- this removes all files from the directory apart from this one, if it exists + uint8_t img_md5[16]; // uint8_t ap_md5[16]; @@ -136,6 +139,7 @@ void metadata_write_image_file(const char *buf, int len) { size_t pl = strlen(config.cover_art_cache_dir) + 1 + strlen(prefix) + strlen(img_md5_str) + 1 + strlen(ext); + char *path = malloc(pl + 1); snprintf(path, pl + 1, "%s/%s%s.%s", config.cover_art_cache_dir, prefix, img_md5_str, ext); int cover_fd = open(path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); @@ -146,6 +150,38 @@ void metadata_write_image_file(const char *buf, int len) { } close(cover_fd); free(path); + + // now delete all other files, if there are any + DIR *d; + struct dirent *dir; + d = opendir(config.cover_art_cache_dir); + if (d) { + int fnl = strlen(prefix)+strlen(img_md5_str)+1+strlen(ext)+1; + + char *full_filename = malloc(fnl); + if (full_filename==NULL) + die("Can't allocate memory at metadata_write_image_file."); + memset(full_filename,0,fnl); + snprintf(full_filename, fnl, "%s%s.%s", prefix, img_md5_str, ext); + debug(1,"Full filename is \"%s\".",full_filename); + int dir_fd = open(config.cover_art_cache_dir, O_DIRECTORY); + if (dir_fd>0) { + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_REG) { + if(strcmp(full_filename,dir->d_name)!=0) { + debug(1,"Deleting file \"%s\".",dir->d_name); + if (unlinkat(dir_fd, dir->d_name, 0)!=0) { + debug(1,"Error %d deleting cover art file \"%s\".",errno,dir->d_name); + } + } + } + } + } else { + debug(1,"Can't open the directory for deletion."); + } + free(full_filename); + closedir(d); + } } else { if (errno == EEXIST) debug(1, "Cover art file \"%s\" already exists!", path);