Fix: PATH_MAX requires limits.h on OS X and Solaris
[babeltrace.git] / tests / lib / common.c
index 25bf735eb81ad6a3442f549d8edd43edff0120e9..0b8ebdf1206296691a84f963feb0cb44f5c2cd07 100644 (file)
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
-#define _GNU_SOURCE
 
 #include <babeltrace/context.h>
 #include <babeltrace/iterator.h>
+#include <babeltrace/compat/dirent.h>
+#include <babeltrace/compat/limits.h>
+#include <sys/stat.h>
 
 struct bt_context *create_context_with_path(const char *path)
 {
@@ -40,3 +42,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.023593 seconds and 4 git commands to generate.