Test fix: clean trace files left behind by test_ctf_ir_ref
[babeltrace.git] / tests / lib / common.c
index 60e2be04244a34b821d5e93bb14a57d85530e42d..d137cb304a764f3a3680af7284fbb1c60aad6054 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <babeltrace/context.h>
 #include <babeltrace/iterator.h>
+#include <babeltrace/compat/dirent.h>
+#include <sys/stat.h>
 
 struct bt_context *create_context_with_path(const char *path)
 {
@@ -39,3 +41,35 @@ struct bt_context *create_context_with_path(const char *path)
        }
        return ctx;
 }
+
+void recursive_rmdir(const char *path)
+{
+       struct dirent *entry;
+       DIR *dir = opendir(path);
+
+       if (!dir) {
+               perror("# opendir");
+               return;
+       }
+
+       while ((entry = readdir(dir))) {
+               struct stat st;
+               char filename[PATH_MAX];
+
+               if (snprintf(filename, sizeof(filename), "%s/%s",
+                               path, entry->d_name) <= 0) {
+                       continue;
+               }
+
+               if (stat(filename, &st)) {
+                       continue;
+               }
+
+               if (S_ISREG(st.st_mode)) {
+                       unlinkat(bt_dirfd(dir), entry->d_name, 0);
+               }
+       }
+
+       rmdir(path);
+       closedir(dir);
+}
This page took 0.02388 seconds and 4 git commands to generate.