]> git.ipfire.org Git - thirdparty/shairport-sync.git/commitdiff
Delete previous cover art files but keep the current one.
authorMike Brady <mikebrady@eircom.net>
Thu, 18 Jan 2018 15:19:04 +0000 (15:19 +0000)
committerMike Brady <mikebrady@eircom.net>
Thu, 18 Jan 2018 15:19:04 +0000 (15:19 +0000)
metadata_hub.c

index 174aca43033965b859cc381ee404a67d572ffd4e..7a03888b2fe61632952ab50cc7a9d99b0be2bf87 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <dirent.h>
 
 #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);