* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <babeltrace/context.h>
-#include <babeltrace/iterator.h>
-#include <babeltrace/compat/dirent.h>
-#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <ftw.h>
-struct bt_context *create_context_with_path(const char *path)
-{
- struct bt_context *ctx;
- int ret;
+#define RMDIR_NFDOPEN 8
- ctx = bt_context_create();
- if (!ctx) {
- return NULL;
+static
+int nftw_recursive_rmdir(const char *file, const struct stat *sb, int flag,
+ struct FTW *s)
+{
+ switch (flag) {
+ case FTW_F:
+ unlink(file);
+ break;
+ case FTW_DP:
+ rmdir(file);
+ break;
}
- ret = bt_context_add_trace(ctx, path, "ctf", NULL, NULL, NULL);
- if (ret < 0) {
- bt_context_put(ctx);
- return NULL;
- }
- return ctx;
+ return 0;
}
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);
- }
- }
+ int nftw_flags = FTW_PHYS | FTW_DEPTH;
- rmdir(path);
- closedir(dir);
+ nftw(path, nftw_recursive_rmdir, RMDIR_NFDOPEN, nftw_flags);
}