SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / common / compat / directory-handle.c
index d6fb7037b663396a190615772e7860f319eabaff..e29d6540b56c648242afafc5c065e1ca0fe7ec9c 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <common/compat/directory-handle.h>
@@ -36,9 +26,6 @@
  * of the internal API below.
  */
 static
-int lttng_directory_handle_stat(const struct lttng_directory_handle *handle,
-               const char *path, struct stat *st);
-static
 int lttng_directory_handle_mkdir(
                const struct lttng_directory_handle *handle,
                const char *path, mode_t mode);
@@ -85,65 +72,120 @@ int _run_as_rmdir(const struct lttng_directory_handle *handle,
 static
 int _run_as_rmdir_recursive(
                const struct lttng_directory_handle *handle, const char *name,
-               uid_t uid, gid_t gid);
+               uid_t uid, gid_t gid, int flags);
 static
 void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle);
+static
+void lttng_directory_handle_release(struct urcu_ref *ref);
 
 #ifdef COMPAT_DIRFD
 
+/*
+ * Special inode number reserved to represent the "current working directory".
+ * ino_t is spec'ed as being an unsigned integral type.
+ */
+#define RESERVED_AT_FDCWD_INO                      \
+       ({                                         \
+               uint64_t reserved_val;             \
+               switch (sizeof(ino_t)) {           \
+               case 4:                            \
+                       reserved_val = UINT32_MAX; \
+                       break;                     \
+               case 8:                            \
+                       reserved_val = UINT64_MAX; \
+                       break;                     \
+               default:                           \
+                       abort();                   \
+               }                                  \
+               (ino_t) reserved_val;              \
+       })
+
 LTTNG_HIDDEN
-int lttng_directory_handle_init(struct lttng_directory_handle *new_handle,
-               const char *path)
+struct lttng_directory_handle *lttng_directory_handle_create(const char *path)
 {
        const struct lttng_directory_handle cwd_handle = {
                .dirfd = AT_FDCWD,
        };
 
        /* Open a handle to the CWD if NULL is passed. */
-       return lttng_directory_handle_init_from_handle(new_handle,
-                       path,
-                       &cwd_handle);
+       return lttng_directory_handle_create_from_handle(path, &cwd_handle);
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_init_from_handle(
-               struct lttng_directory_handle *new_handle, const char *path,
-               const struct lttng_directory_handle *handle)
+struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
+               const char *path,
+               const struct lttng_directory_handle *ref_handle)
 {
-       int ret;
+       int dirfd;
+       struct lttng_directory_handle *handle = NULL;
 
        if (!path) {
-               ret = lttng_directory_handle_copy(handle, new_handle);
+               handle = lttng_directory_handle_copy(ref_handle);
                goto end;
        }
        if (!*path) {
                ERR("Failed to initialize directory handle: provided path is an empty string");
-               ret = -1;
                goto end;
        }
-       ret = openat(handle->dirfd, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
-       if (ret == -1) {
+
+       dirfd = openat(ref_handle->dirfd, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+       if (dirfd == -1) {
                PERROR("Failed to initialize directory handle to \"%s\"", path);
                goto end;
        }
-       new_handle->dirfd = ret;
-       ret = 0;
+
+       handle = lttng_directory_handle_create_from_dirfd(dirfd);
+       if (!handle) {
+               goto error_close;
+       }
 end:
-       return ret;
+       return handle;
+error_close:
+       if (close(dirfd)) {
+               PERROR("Failed to close directory file descriptor");
+       }
+       return NULL;
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_init_from_dirfd(
-               struct lttng_directory_handle *handle, int dirfd)
+struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
+               int dirfd)
 {
+       int ret;
+       struct lttng_directory_handle *handle = zmalloc(sizeof(*handle));
+       struct stat stat_buf;
+
+       if (!handle) {
+               goto end;
+       }
+
+       if (dirfd != AT_FDCWD) {
+               ret = fstat(dirfd, &stat_buf);
+               if (ret) {
+                       PERROR("Failed to fstat directory file descriptor %i", dirfd);
+                       lttng_directory_handle_release(&handle->ref);
+                       handle = NULL;
+                       goto end;
+               }
+       } else {
+               handle->directory_inode = RESERVED_AT_FDCWD_INO;
+       }
        handle->dirfd = dirfd;
-       return 0;
+       urcu_ref_init(&handle->ref);
+end:
+       return handle;
 }
 
-LTTNG_HIDDEN
-void lttng_directory_handle_fini(struct lttng_directory_handle *handle)
+static
+void lttng_directory_handle_release(struct urcu_ref *ref)
 {
        int ret;
+       struct lttng_directory_handle *handle =
+                       container_of(ref, struct lttng_directory_handle, ref);
+
+       if (handle->destroy_cb) {
+               handle->destroy_cb(handle, handle->destroy_cb_data);
+       }
 
        if (handle->dirfd == AT_FDCWD || handle->dirfd == -1) {
                goto end;
@@ -151,28 +193,42 @@ void lttng_directory_handle_fini(struct lttng_directory_handle *handle)
        ret = close(handle->dirfd);
        if (ret == -1) {
                PERROR("Failed to close directory file descriptor of directory handle");
-               abort();
        }
 end:
        lttng_directory_handle_invalidate(handle);
+       free(handle);
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_copy(const struct lttng_directory_handle *handle,
-               struct lttng_directory_handle *new_copy)
+struct lttng_directory_handle *lttng_directory_handle_copy(
+               const struct lttng_directory_handle *handle)
 {
-       int ret = 0;
+       struct lttng_directory_handle *new_handle = NULL;
 
        if (handle->dirfd == AT_FDCWD) {
-               new_copy->dirfd = handle->dirfd;
+               new_handle = lttng_directory_handle_create_from_dirfd(AT_FDCWD);
        } else {
-               new_copy->dirfd = dup(handle->dirfd);
-               if (new_copy->dirfd == -1) {
-                       PERROR("Failed to duplicate directory fd of directory handle");
-                       ret = -1;
+               const int new_dirfd = dup(handle->dirfd);
+
+               if (new_dirfd == -1) {
+                       PERROR("Failed to duplicate directory file descriptor of directory handle");
+                       goto end;
+               }
+               new_handle = lttng_directory_handle_create_from_dirfd(
+                               new_dirfd);
+               if (!new_handle && close(new_dirfd)) {
+                       PERROR("Failed to close directory file descriptor of directory handle");
                }
        }
-       return ret;
+end:
+       return new_handle;
+}
+
+LTTNG_HIDDEN
+bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
+               const struct lttng_directory_handle *rhs)
+{
+       return lhs->directory_inode == rhs->directory_inode;
 }
 
 static
@@ -181,13 +237,20 @@ void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle)
        handle->dirfd = -1;
 }
 
-static
+LTTNG_HIDDEN
 int lttng_directory_handle_stat(const struct lttng_directory_handle *handle,
                const char *path, struct stat *st)
 {
        return fstatat(handle->dirfd, path, st, 0);
 }
 
+LTTNG_HIDDEN
+bool lttng_directory_handle_uses_fd(
+               const struct lttng_directory_handle *handle)
+{
+       return handle->dirfd != AT_FDCWD;
+}
+
 static
 int lttng_directory_handle_mkdir(
                const struct lttng_directory_handle *handle,
@@ -292,7 +355,12 @@ static
 int lttng_directory_handle_rmdir(
                const struct lttng_directory_handle *handle, const char *name)
 {
-       return unlinkat(handle->dirfd, name, AT_REMOVEDIR);
+       int ret = unlinkat(handle->dirfd, name, AT_REMOVEDIR);
+       if (ret) {
+               PERROR("Failed to remove directory `%s`", name);
+       }
+
+       return ret;
 }
 
 static
@@ -305,9 +373,9 @@ int _run_as_rmdir(const struct lttng_directory_handle *handle,
 static
 int _run_as_rmdir_recursive(
                const struct lttng_directory_handle *handle, const char *name,
-               uid_t uid, gid_t gid)
+               uid_t uid, gid_t gid, int flags)
 {
-       return run_as_rmdirat_recursive(handle->dirfd, name, uid, gid);
+       return run_as_rmdirat_recursive(handle->dirfd, name, uid, gid, flags);
 }
 
 #else /* COMPAT_DIRFD */
@@ -343,8 +411,22 @@ end:
        return ret;
 }
 
+static
+struct lttng_directory_handle *_lttng_directory_handle_create(char *path)
+{
+       struct lttng_directory_handle *handle = zmalloc(sizeof(*handle));
+
+       if (!handle) {
+               goto end;
+       }
+       urcu_ref_init(&handle->ref);
+       handle->base_path = path;
+end:
+       return handle;
+}
+
 LTTNG_HIDDEN
-int lttng_directory_handle_init(struct lttng_directory_handle *handle,
+struct lttng_directory_handle *lttng_directory_handle_create(
                const char *path)
 {
        int ret;
@@ -352,6 +434,7 @@ int lttng_directory_handle_init(struct lttng_directory_handle *handle,
        size_t cwd_len, path_len;
        char cwd_buf[LTTNG_PATH_MAX] = {};
        char handle_buf[LTTNG_PATH_MAX] = {};
+       struct lttng_directory_handle *new_handle = NULL;
        bool add_cwd_slash = false, add_trailing_slash = false;
        const struct lttng_directory_handle cwd_handle = {
                .base_path = handle_buf,
@@ -385,25 +468,26 @@ int lttng_directory_handle_init(struct lttng_directory_handle *handle,
                goto end;
        }
 
-       ret = lttng_directory_handle_init_from_handle(handle, path,
-                       &cwd_handle);
+       new_handle = lttng_directory_handle_create_from_handle(path, &cwd_handle);
 end:
-       return ret;
+       return new_handle;
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_init_from_handle(
-               struct lttng_directory_handle *new_handle, const char *path,
-               const struct lttng_directory_handle *handle)
+struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
+               const char *path,
+               const struct lttng_directory_handle *ref_handle)
 {
        int ret;
        size_t path_len, handle_path_len;
        bool add_trailing_slash;
        struct stat stat_buf;
+       struct lttng_directory_handle *new_handle = NULL;
+       char *new_path = NULL;
 
-       assert(handle && handle->base_path);
+       assert(ref_handle && ref_handle->base_path);
 
-       ret = lttng_directory_handle_stat(handle, path, &stat_buf);
+       ret = lttng_directory_handle_stat(ref_handle, path, &stat_buf);
        if (ret == -1) {
                PERROR("Failed to create directory handle");
                goto end;
@@ -411,7 +495,7 @@ int lttng_directory_handle_init_from_handle(
                char full_path[LTTNG_PATH_MAX];
 
                /* Best effort for logging purposes. */
-               ret = get_full_path(handle, path, full_path,
+               ret = get_full_path(ref_handle, path, full_path,
                                sizeof(full_path));
                if (ret) {
                        full_path[0] = '\0';
@@ -419,11 +503,10 @@ int lttng_directory_handle_init_from_handle(
 
                ERR("Failed to initialize directory handle to \"%s\": not a directory",
                                full_path);
-               ret = -1;
                goto end;
        }
        if (!path) {
-               ret = lttng_directory_handle_copy(handle, new_handle);
+               new_handle = lttng_directory_handle_copy(ref_handle);
                goto end;
        }
 
@@ -434,62 +517,88 @@ int lttng_directory_handle_init_from_handle(
                goto end;
        }
        if (*path == '/') {
-               new_handle->base_path = strdup(path);
-               ret = new_handle->base_path ? 0 : -1;
+               new_path = strdup(path);
+               if (!new_path) {
+                       goto end;
+               }
+               /* Takes ownership of new_path. */
+               new_handle = _lttng_directory_handle_create(new_path);
+               new_path = NULL;
                goto end;
        }
 
        add_trailing_slash = path[path_len - 1] != '/';
 
-       handle_path_len = strlen(handle->base_path) + path_len +
+       handle_path_len = strlen(ref_handle->base_path) + path_len +
                        !!add_trailing_slash;
        if (handle_path_len >= LTTNG_PATH_MAX) {
                ERR("Failed to initialize directory handle as the resulting path's length (%zu bytes) exceeds the maximal allowed length (%d bytes)",
                                handle_path_len, LTTNG_PATH_MAX);
-               ret = -1;
                goto end;
        }
-       new_handle->base_path = zmalloc(handle_path_len);
-       if (!new_handle->base_path) {
+       new_path = zmalloc(handle_path_len);
+       if (!new_path) {
                PERROR("Failed to initialize directory handle");
-               ret = -1;
                goto end;
        }
 
        ret = sprintf(new_handle->base_path, "%s%s%s",
-                       handle->base_path,
+                       ref_handle->base_path,
                        path,
                        add_trailing_slash ? "/" : "");
        if (ret == -1 || ret >= handle_path_len) {
                ERR("Failed to initialize directory handle: path formatting failed");
-               ret = -1;
                goto end;
        }
+       new_handle = _lttng_directory_handle_create(new_path);
+       new_path = NULL;
 end:
-       return ret;
+       free(new_path);
+       return new_handle;
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_init_from_dirfd(
-               struct lttng_directory_handle *handle, int dirfd)
+struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
+               int dirfd)
 {
        assert(dirfd == AT_FDCWD);
-       return lttng_directory_handle_init(handle, NULL);
+       return lttng_directory_handle_create(NULL);
 }
 
-LTTNG_HIDDEN
-void lttng_directory_handle_fini(struct lttng_directory_handle *handle)
+static
+void lttng_directory_handle_release(struct urcu_ref *ref)
 {
+       struct lttng_directory_handle *handle =
+                       container_of(ref, struct lttng_directory_handle, ref);
+
        free(handle->base_path);
        lttng_directory_handle_invalidate(handle);
+       free(handle);
 }
 
 LTTNG_HIDDEN
-int lttng_directory_handle_copy(const struct lttng_directory_handle *handle,
-               struct lttng_directory_handle *new_copy)
+struct lttng_directory_handle *lttng_directory_handle_copy(
+               const struct lttng_directory_handle *handle)
 {
-       new_copy->base_path = strdup(handle->base_path);
-       return new_copy->base_path ? 0 : -1;
+       struct lttng_directory_handle *new_handle = NULL;
+       char *new_path = NULL;
+
+       if (handle->base_path) {
+               new_path = strdup(handle->base_path);
+               if (!new_path) {
+                       goto end;
+               }
+       }
+       new_handle = _lttng_directory_handle_create(new_path);
+end:
+       return new_handle;
+}
+
+LTTNG_HIDDEN
+bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
+               const struct lttng_directory_handle *rhs)
+{
+       return strcmp(lhs->base_path, rhs->base_path) == 0;
 }
 
 static
@@ -498,7 +607,7 @@ void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle)
        handle->base_path = NULL;
 }
 
-static
+LTTNG_HIDDEN
 int lttng_directory_handle_stat(const struct lttng_directory_handle *handle,
                const char *subdirectory, struct stat *st)
 {
@@ -516,6 +625,13 @@ end:
        return ret;
 }
 
+LTTNG_HIDDEN
+bool lttng_directory_handle_uses_fd(
+               const struct lttng_directory_handle *handle)
+{
+       return false;
+}
+
 static
 int lttng_directory_handle_mkdir(const struct lttng_directory_handle *handle,
                const char *subdirectory, mode_t mode)
@@ -759,7 +875,7 @@ end:
 static
 int _run_as_rmdir_recursive(
                const struct lttng_directory_handle *handle, const char *name,
-               uid_t uid, gid_t gid)
+               uid_t uid, gid_t gid, int flags)
 {
        int ret;
        char fullpath[LTTNG_PATH_MAX];
@@ -770,7 +886,7 @@ int _run_as_rmdir_recursive(
                goto end;
        }
 
-       ret = run_as_rmdir_recursive(fullpath, uid, gid);
+       ret = run_as_rmdir_recursive(fullpath, uid, gid, flags);
 end:
        return ret;
 }
@@ -819,16 +935,6 @@ end:
        return ret;
 }
 
-LTTNG_HIDDEN
-struct lttng_directory_handle
-lttng_directory_handle_move(struct lttng_directory_handle *original)
-{
-       const struct lttng_directory_handle tmp = *original;
-
-       lttng_directory_handle_invalidate(original);
-       return tmp;
-}
-
 static
 int create_directory_recursive(const struct lttng_directory_handle *handle,
                const char *path, mode_t mode)
@@ -884,6 +990,22 @@ error:
        return ret;
 }
 
+LTTNG_HIDDEN
+bool lttng_directory_handle_get(struct lttng_directory_handle *handle)
+{
+       return urcu_ref_get_unless_zero(&handle->ref);
+}
+
+LTTNG_HIDDEN
+void lttng_directory_handle_put(struct lttng_directory_handle *handle)
+{
+       if (!handle) {
+               return;
+       }
+       assert(handle->ref.refcount);
+       urcu_ref_put(&handle->ref, lttng_directory_handle_release);
+}
+
 LTTNG_HIDDEN
 int lttng_directory_handle_create_subdirectory_as_user(
                const struct lttng_directory_handle *handle,
@@ -897,8 +1019,9 @@ int lttng_directory_handle_create_subdirectory_as_user(
                ret = create_directory_check_exists(handle,
                                subdirectory, mode);
        } else {
-               ret = _run_as_mkdir(handle, subdirectory,
-                               mode, creds->uid, creds->gid);
+               ret = _run_as_mkdir(handle, subdirectory, mode,
+                               lttng_credentials_get_uid(creds),
+                               lttng_credentials_get_gid(creds));
        }
 
        return ret;
@@ -918,7 +1041,7 @@ int lttng_directory_handle_create_subdirectory_recursive_as_user(
                                subdirectory_path, mode);
        } else {
                ret = _run_as_mkdir_recursive(handle, subdirectory_path,
-                               mode, creds->uid, creds->gid);
+                               mode, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
        }
 
        return ret;
@@ -959,7 +1082,7 @@ int lttng_directory_handle_open_file_as_user(
                                mode);
        } else {
                ret = _run_as_open(handle, filename, flags, mode,
-                               creds->uid, creds->gid);
+                               lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
        }
        return ret;
 }
@@ -986,7 +1109,7 @@ int lttng_directory_handle_unlink_file_as_user(
                /* Run as current user. */
                ret = lttng_directory_handle_unlink(handle, filename);
        } else {
-               ret = _run_as_unlink(handle, filename, creds->uid, creds->gid);
+               ret = _run_as_unlink(handle, filename, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
        }
        return ret;
 }
@@ -1027,7 +1150,7 @@ int lttng_directory_handle_rename_as_user(
                                old_name, new_handle, new_name);
        } else {
                ret = _run_as_rename(old_handle, old_name, new_handle,
-                               new_name, creds->uid, creds->gid);
+                               new_name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
        }
        return ret;
 }
@@ -1053,13 +1176,15 @@ int lttng_directory_handle_remove_subdirectory_as_user(
                /* Run as current user. */
                ret = lttng_directory_handle_rmdir(handle, name);
        } else {
-               ret = _run_as_rmdir(handle, name, creds->uid, creds->gid);
+               ret = _run_as_rmdir(handle, name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
        }
        return ret;
 }
 
 struct rmdir_frame {
+       ssize_t parent_frame_idx;
        DIR *dir;
+       bool empty;
        /* Size including '\0'. */
        size_t path_size;
 };
@@ -1067,20 +1192,26 @@ struct rmdir_frame {
 static
 void rmdir_frame_fini(void *data)
 {
+       int ret;
        struct rmdir_frame *frame = data;
 
-       closedir(frame->dir);
+       ret = closedir(frame->dir);
+       if (ret == -1) {
+               PERROR("Failed to close directory stream");
+       }
 }
 
 static
 int remove_directory_recursive(const struct lttng_directory_handle *handle,
-               const char *path)
+               const char *path, int flags)
 {
        int ret;
        struct lttng_dynamic_array frames;
        size_t current_frame_idx = 0;
        struct rmdir_frame initial_frame = {
+               .parent_frame_idx = -1,
                .dir = lttng_directory_handle_opendir(handle, path),
+               .empty = true,
                .path_size = strlen(path) + 1,
        };
        struct lttng_dynamic_buffer current_path;
@@ -1088,7 +1219,27 @@ int remove_directory_recursive(const struct lttng_directory_handle *handle,
 
        lttng_dynamic_buffer_init(&current_path);
        lttng_dynamic_array_init(&frames, sizeof(struct rmdir_frame),
-                       rmdir_frame_fini);
+                       rmdir_frame_fini);
+
+       if (flags & ~(LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG |
+                                   LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG)) {
+               ERR("Unknown flags %d", flags);
+               ret = -1;
+               goto end;
+       }
+
+       if (!initial_frame.dir) {
+               if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG &&
+                               errno == ENOENT) {
+                       DBG("Cannot rmdir \"%s\": root does not exist", path);
+                       ret = 0;
+                       goto end;
+               } else {
+                       PERROR("Failed to rmdir \"%s\"", path);
+                       ret = -1;
+                       goto end;
+               }
+       }
 
        ret = lttng_dynamic_array_add_element(&frames, &initial_frame);
        if (ret) {
@@ -1097,42 +1248,37 @@ int remove_directory_recursive(const struct lttng_directory_handle *handle,
                goto end;
        }
 
-       ret = lttng_dynamic_buffer_append(&current_path, path,
-                       initial_frame.path_size);
-        if (ret) {
+       ret = lttng_dynamic_buffer_append(
+                       &current_path, path, initial_frame.path_size);
+       if (ret) {
                ERR("Failed to set initial path during recursive directory removal");
                ret = -1;
                goto end;
-        }
+       }
 
-        while (lttng_dynamic_array_get_count(&frames) > 0) {
+       while (lttng_dynamic_array_get_count(&frames) > 0) {
                struct dirent *entry;
                struct rmdir_frame *current_frame =
-                               lttng_dynamic_array_get_element(&frames,
-                                               current_frame_idx);
+                               lttng_dynamic_array_get_element(
+                                               &frames, current_frame_idx);
 
-                if (!current_frame->dir) {
-                       PERROR("Failed to open directory stream during recursive directory removal");
-                       ret = -1;
-                       goto end;
-               }
-               ret = lttng_dynamic_buffer_set_size(&current_path,
-                               current_frame->path_size);
+               assert(current_frame->dir);
+               ret = lttng_dynamic_buffer_set_size(
+                               &current_path, current_frame->path_size);
                assert(!ret);
                current_path.data[current_path.size - 1] = '\0';
 
                while ((entry = readdir(current_frame->dir))) {
                        struct stat st;
-                       struct rmdir_frame new_frame;
 
-                       if (!strcmp(entry->d_name, ".")
-                                       || !strcmp(entry->d_name, "..")) {
+                       if (!strcmp(entry->d_name, ".") ||
+                                       !strcmp(entry->d_name, "..")) {
                                continue;
                        }
 
                        /* Set current_path to the entry's path. */
-                       ret = lttng_dynamic_buffer_set_size(&current_path,
-                                       current_path.size - 1);
+                       ret = lttng_dynamic_buffer_set_size(
+                                       &current_path, current_path.size - 1);
                        assert(!ret);
                        ret = lttng_dynamic_buffer_append(&current_path,
                                        &separator, sizeof(separator));
@@ -1146,8 +1292,12 @@ int remove_directory_recursive(const struct lttng_directory_handle *handle,
                                goto end;
                        }
 
-                       if (lttng_directory_handle_stat(handle,
-                                       current_path.data, &st)) {
+                       if (lttng_directory_handle_stat(
+                                           handle, current_path.data, &st)) {
+                               if ((flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG) &&
+                                               errno == ENOENT) {
+                                       break;
+                               }
                                PERROR("Failed to stat \"%s\"",
                                                current_path.data);
                                ret = -1;
@@ -1155,42 +1305,81 @@ int remove_directory_recursive(const struct lttng_directory_handle *handle,
                        }
 
                        if (!S_ISDIR(st.st_mode)) {
-                               /* Not empty, abort. */
-                               DBG("Directory \"%s\" is not empty; refusing to remove directory",
-                                               current_path.data);
-                               ret = -1;
-                               goto end;
+                               if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG) {
+                                       current_frame->empty = false;
+                                       break;
+                               } else {
+                                       /* Not empty, abort. */
+                                       DBG("Directory \"%s\" is not empty; refusing to remove directory",
+                                                       current_path.data);
+                                       ret = -1;
+                                       goto end;
+                               }
+                       } else {
+                               struct rmdir_frame new_frame = {
+                                       .path_size = current_path.size,
+                                       .dir = lttng_directory_handle_opendir(
+                                                       handle,
+                                                       current_path.data),
+                                       .empty = true,
+                                       .parent_frame_idx = current_frame_idx,
+                               };
+
+                               if (!new_frame.dir) {
+                                       if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG &&
+                                                       errno == ENOENT) {
+                                               DBG("Non-existing directory stream during recursive directory removal");
+                                               break;
+                                       } else {
+                                               PERROR("Failed to open directory stream during recursive directory removal");
+                                               ret = -1;
+                                               goto end;
+                                       }
+                               }
+                               ret = lttng_dynamic_array_add_element(
+                                               &frames, &new_frame);
+                               if (ret) {
+                                       ERR("Failed to push context frame during recursive directory removal");
+                                       rmdir_frame_fini(&new_frame);
+                                       goto end;
+                               }
+                               current_frame_idx++;
+                               /* We break iteration on readdir. */
+                               break;
                        }
+               }
+               if (entry) {
+                       continue;
+               }
 
-                       new_frame.path_size = current_path.size;
-                       new_frame.dir = lttng_directory_handle_opendir(handle,
-                                       current_path.data);
-                       ret = lttng_dynamic_array_add_element(&frames,
-                                       &new_frame);
-                        if (ret) {
-                               ERR("Failed to push context frame during recursive directory removal");
-                               rmdir_frame_fini(&new_frame);
-                               goto end;
-                        }
-                       current_frame_idx++;
-                       break;
-                }
-               if (!entry) {
-                       ret = lttng_directory_handle_rmdir(handle,
-                                       current_path.data);
-                        if (ret) {
-                               PERROR("Failed to remove \"%s\" during recursive directory removal",
-                                               current_path.data);
-                               goto end;
-                        }
-                       ret = lttng_dynamic_array_remove_element(&frames,
-                                       current_frame_idx);
+               /* Pop rmdir frame. */
+               if (current_frame->empty) {
+                       ret = lttng_directory_handle_rmdir(
+                                       handle, current_path.data);
                        if (ret) {
-                               ERR("Failed to pop context frame during recursive directory removal");
-                               goto end;
+                               if ((flags & LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG) ||
+                                               errno != ENOENT) {
+                                       PERROR("Failed to remove \"%s\" during recursive directory removal",
+                                                       current_path.data);
+                                       goto end;
+                               }
+                               DBG("Non-existing directory stream during recursive directory removal");
                        }
-                       current_frame_idx--;
-                }
+               } else if (current_frame->parent_frame_idx >= 0) {
+                       struct rmdir_frame *parent_frame;
+
+                       parent_frame = lttng_dynamic_array_get_element(&frames,
+                                       current_frame->parent_frame_idx);
+                       assert(parent_frame);
+                       parent_frame->empty = false;
+               }
+               ret = lttng_dynamic_array_remove_element(
+                               &frames, current_frame_idx);
+               if (ret) {
+                       ERR("Failed to pop context frame during recursive directory removal");
+                       goto end;
+               }
+               current_frame_idx--;
        }
 end:
        lttng_dynamic_array_reset(&frames);
@@ -1201,26 +1390,28 @@ end:
 LTTNG_HIDDEN
 int lttng_directory_handle_remove_subdirectory_recursive(
                const struct lttng_directory_handle *handle,
-               const char *name)
+               const char *name,
+               int flags)
 {
        return lttng_directory_handle_remove_subdirectory_recursive_as_user(
-                       handle, name, NULL);
+                       handle, name, NULL, flags);
 }
 
 LTTNG_HIDDEN
 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
                const struct lttng_directory_handle *handle,
                const char *name,
-               const struct lttng_credentials *creds)
+               const struct lttng_credentials *creds,
+               int flags)
 {
        int ret;
 
        if (!creds) {
                /* Run as current user. */
-               ret = remove_directory_recursive(handle, name);
+               ret = remove_directory_recursive(handle, name, flags);
        } else {
-               ret = _run_as_rmdir_recursive(handle, name, creds->uid,
-                               creds->gid);
+               ret = _run_as_rmdir_recursive(handle, name, lttng_credentials_get_uid(creds),
+                               lttng_credentials_get_gid(creds), flags);
        }
        return ret;
 }
This page took 0.035875 seconds and 5 git commands to generate.