[PATCH] debuginfod: Try to remove empty cache dirs.

Mark Wielaard mark@klomp.org
Sat Nov 28 01:25:52 GMT 2020


When unlinking a max_unused_age file also try to rmdir the parent
directory in case it is now empty.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 debuginfod/ChangeLog           |  5 +++++
 debuginfod/debuginfod-client.c | 16 ++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index aaffac7c..4c5e4947 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,8 @@
+2020-11-27  Mark Wielaard  <mark@klomp.org>
+
+	* debuginfod.cxx (debuginfod_clean_cache): Try to rmdir the
+	parent directory when unlinking a max_unused_age file.
+
 2020-11-25  Frank Ch. Eigler  <fche@redhat.com>
 
 	* debuginfod.cxx (step_ok_done): Correct typo in prom metric label.
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index a99f3c14..63dcc719 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -69,7 +69,7 @@ void debuginfod_end (debuginfod_client *c) { }
 #include <assert.h>
 #include <dirent.h>
 #include <stdio.h>
-#include <errno.h>
+#include <libgen.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <fts.h>
@@ -328,12 +328,20 @@ debuginfod_clean_cache(debuginfod_client *c,
           /* delete file if max_unused_age has been met or exceeded.  */
           /* XXX consider extra effort to clean up old tmp files */
           if (time(NULL) - f->fts_statp->st_atime >= max_unused_age)
-            unlink (f->fts_path);
+            {
+              unlink (f->fts_path);
+              /* Remove parent dir if now empty.  Note that we need a
+                 copy of fts_path since dirname will change it.  */
+              char *fts_path = strdup (f->fts_path);
+              if (fts_path != NULL)
+                (void) rmdir (dirname (fts_path));
+              free (fts_path);
+            }
           break;
 
         case FTS_DP:
-          /* Remove if empty. */
-          (void) rmdir (f->fts_path);
+          /* Should never be reached because the regex only matches
+	     files, so if it is a dir it isn't ours. */
           break;
 
         default:
-- 
2.18.4



More information about the Elfutils-devel mailing list