[PATCH 8/8] perf buildid-cache: Try to remove empty directories

From: Jiri Olsa
Date: Mon Dec 01 2014 - 14:07:46 EST


The cache removal routine only removes files leaving
directories untouched. Adding code that tries to
remove directories up to the 'buildid_dir' level.

Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Corey Ashford <cjashfor@xxxxxxxxxxxxxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Paul Mackerras <paulus@xxxxxxxxx>
Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Cc: Stephane Eranian <eranian@xxxxxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/util/build-id.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 0c72680a977f..c5dc066db6e9 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -329,11 +329,41 @@ static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
is_kallsyms, is_vdso);
}

+/*
+ * Tries to remove directory of the file given in
+ * the @file parameter. It might change the string
+ * pointed to by the @file pointer.
+ */
+static void try_remove_dir(char *file, const char *debugdir)
+{
+ char *dir = dirname(file);
+
+ while (dir) {
+ char *slash;
+
+ if (!strcmp(dir, debugdir))
+ return;
+
+ pr_debug("try_remove_dir %s\n", dir);
+
+ /* We dont care if it failes, we tried.. */
+ if (rmdir(dir))
+ return;
+
+ slash = strrchr(dir, '/');
+ if (!slash)
+ return;
+
+ *slash = 0;
+ }
+}
+
int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
{
const size_t size = PATH_MAX;
char *filename = zalloc(size),
- *linkname = zalloc(size);
+ *linkname = zalloc(size),
+ *build_id_link = NULL;
int err = -1;

if (filename == NULL || linkname == NULL)
@@ -351,6 +381,10 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
if (unlink(linkname))
goto out_free;

+ build_id_link = strdup(linkname);
+ if (!build_id_link)
+ goto out_free;
+
/*
* Since the link is relative, we must make it absolute:
*/
@@ -360,10 +394,18 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
if (unlink(linkname))
goto out_free;

+ /*
+ * The order matters here, because linkname path
+ * is based on build_id_link path.
+ */
+ try_remove_dir(linkname, debugdir);
+ try_remove_dir(build_id_link, debugdir);
+
err = 0;
out_free:
free(filename);
free(linkname);
+ free(build_id_link);
return err;
}

--
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/