| 1 | /* |
| 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/compat/directory-handle.hpp> |
| 9 | #include <common/compat/errno.hpp> |
| 10 | #include <common/error.hpp> |
| 11 | |
| 12 | #include <fcntl.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <tap/tap.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #define TEST_COUNT 9 |
| 23 | |
| 24 | /* For error.h */ |
| 25 | int lttng_opt_quiet = 1; |
| 26 | int lttng_opt_verbose = 3; |
| 27 | int lttng_opt_mi; |
| 28 | |
| 29 | #define DIR_CREATION_MODE (S_IRWXU | S_IRWXG) |
| 30 | |
| 31 | /* |
| 32 | * Returns the number of tests that ran (irrespective of the result) or a |
| 33 | * negative value on error (will abort all tests). |
| 34 | */ |
| 35 | using test_func = int(const char *); |
| 36 | |
| 37 | static test_func test_rmdir_fail_non_empty; |
| 38 | static test_func test_rmdir_skip_non_empty; |
| 39 | |
| 40 | static test_func *const test_funcs[] = { |
| 41 | &test_rmdir_fail_non_empty, |
| 42 | &test_rmdir_skip_non_empty, |
| 43 | }; |
| 44 | |
| 45 | static bool dir_exists(const char *path) |
| 46 | { |
| 47 | int ret; |
| 48 | struct stat st; |
| 49 | |
| 50 | ret = stat(path, &st); |
| 51 | return ret == 0 && S_ISDIR(st.st_mode); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Create a non-empty folder hierarchy from a directory handle: |
| 56 | * |
| 57 | * test_root_name |
| 58 | * └── a |
| 59 | * └── b |
| 60 | * ├── c |
| 61 | * │ └── d |
| 62 | * └── e |
| 63 | * ├── f |
| 64 | * └── file1 |
| 65 | */ |
| 66 | static int create_non_empty_hierarchy_with_root(struct lttng_directory_handle *test_dir_handle, |
| 67 | const char *test_root_name) |
| 68 | { |
| 69 | int ret; |
| 70 | const int file_flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 71 | const mode_t file_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 72 | char *branch_name = nullptr; |
| 73 | |
| 74 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/c/d"); |
| 75 | if (ret < 0) { |
| 76 | diag("Failed to format folder path"); |
| 77 | goto end; |
| 78 | } |
| 79 | ret = lttng_directory_handle_create_subdirectory_recursive( |
| 80 | test_dir_handle, branch_name, DIR_CREATION_MODE); |
| 81 | if (ret) { |
| 82 | diag("Failed to create test folder hierarchy %s", branch_name); |
| 83 | goto end; |
| 84 | } |
| 85 | |
| 86 | free(branch_name); |
| 87 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/f"); |
| 88 | if (ret < 0) { |
| 89 | diag("Failed to format folder path"); |
| 90 | goto end; |
| 91 | } |
| 92 | ret = lttng_directory_handle_create_subdirectory_recursive( |
| 93 | test_dir_handle, branch_name, DIR_CREATION_MODE); |
| 94 | if (ret) { |
| 95 | diag("Failed to create test folder hierarchy %s", branch_name); |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | free(branch_name); |
| 100 | ret = asprintf(&branch_name, "%s/%s", test_root_name, "a/b/e/file1"); |
| 101 | if (ret < 0) { |
| 102 | diag("Failed to format file path"); |
| 103 | goto end; |
| 104 | } |
| 105 | ret = lttng_directory_handle_open_file(test_dir_handle, branch_name, file_flags, file_mode); |
| 106 | if (ret < 0) { |
| 107 | diag("Failed to create file %s", branch_name); |
| 108 | goto end; |
| 109 | } |
| 110 | ret = close(ret); |
| 111 | if (ret) { |
| 112 | PERROR("Failed to close fd to newly created file %s", branch_name); |
| 113 | goto end; |
| 114 | } |
| 115 | end: |
| 116 | free(branch_name); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* Remove "file1" from the test folder hierarchy. */ |
| 121 | static int remove_file_from_hierarchy(struct lttng_directory_handle *test_dir_handle, |
| 122 | const char *test_root_name) |
| 123 | { |
| 124 | int ret; |
| 125 | char *file_name = nullptr; |
| 126 | |
| 127 | ret = asprintf(&file_name, "%s/%s", test_root_name, "a/b/e/file1"); |
| 128 | if (ret < 0) { |
| 129 | diag("Failed to format file path"); |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | ret = lttng_directory_handle_unlink_file(test_dir_handle, file_name); |
| 134 | if (ret) { |
| 135 | PERROR("Failed to unlink file %s", file_name); |
| 136 | goto end; |
| 137 | } |
| 138 | end: |
| 139 | free(file_name); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static int test_rmdir_fail_non_empty(const char *test_dir) |
| 144 | { |
| 145 | int ret, tests_ran = 0; |
| 146 | struct lttng_directory_handle *test_dir_handle; |
| 147 | char *created_dir = nullptr; |
| 148 | const char test_root_name[] = "fail_non_empty"; |
| 149 | char *test_dir_path = nullptr; |
| 150 | |
| 151 | diag("rmdir (fail if non-empty)"); |
| 152 | |
| 153 | test_dir_handle = lttng_directory_handle_create(test_dir); |
| 154 | ok(test_dir_handle, "Initialized directory handle from the test directory"); |
| 155 | tests_ran++; |
| 156 | if (!test_dir_handle) { |
| 157 | ret = -1; |
| 158 | goto end; |
| 159 | } |
| 160 | |
| 161 | ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name); |
| 162 | if (ret) { |
| 163 | diag("Failed to setup folder/file hierarchy to run test"); |
| 164 | goto end; |
| 165 | } |
| 166 | |
| 167 | ret = lttng_directory_handle_remove_subdirectory_recursive( |
| 168 | test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG); |
| 169 | ok(ret == -1, |
| 170 | "Error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG"); |
| 171 | tests_ran++; |
| 172 | |
| 173 | ret = remove_file_from_hierarchy(test_dir_handle, test_root_name); |
| 174 | if (ret) { |
| 175 | diag("Failed to remove file from test folder hierarchy"); |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | ret = lttng_directory_handle_remove_subdirectory_recursive( |
| 180 | test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG); |
| 181 | ok(ret == 0, |
| 182 | "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG"); |
| 183 | tests_ran++; |
| 184 | |
| 185 | ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name); |
| 186 | if (ret < 0) { |
| 187 | diag("Failed to format test directory path"); |
| 188 | goto end; |
| 189 | } |
| 190 | ok(!dir_exists(test_dir_path) && errno == ENOENT, |
| 191 | "Folder hierarchy %s successfully removed", |
| 192 | test_dir_path); |
| 193 | tests_ran++; |
| 194 | ret = 0; |
| 195 | end: |
| 196 | lttng_directory_handle_put(test_dir_handle); |
| 197 | free(created_dir); |
| 198 | free(test_dir_path); |
| 199 | return ret == 0 ? tests_ran : ret; |
| 200 | } |
| 201 | |
| 202 | static int test_rmdir_skip_non_empty(const char *test_dir) |
| 203 | { |
| 204 | int ret, tests_ran = 0; |
| 205 | struct lttng_directory_handle *test_dir_handle; |
| 206 | char *created_dir = nullptr; |
| 207 | const char test_root_name[] = "skip_non_empty"; |
| 208 | char *test_dir_path = nullptr; |
| 209 | |
| 210 | diag("rmdir (skip if non-empty)"); |
| 211 | |
| 212 | test_dir_handle = lttng_directory_handle_create(test_dir); |
| 213 | ok(test_dir_handle, "Initialized directory handle from the test directory"); |
| 214 | tests_ran++; |
| 215 | if (!test_dir_handle) { |
| 216 | ret = -1; |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | ret = create_non_empty_hierarchy_with_root(test_dir_handle, test_root_name); |
| 221 | if (ret) { |
| 222 | diag("Failed to setup folder/file hierarchy to run test"); |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | ret = lttng_directory_handle_remove_subdirectory_recursive( |
| 227 | test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
| 228 | ok(ret == 0, |
| 229 | "No error returned when attempting to recursively remove non-empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG"); |
| 230 | tests_ran++; |
| 231 | |
| 232 | ret = asprintf(&test_dir_path, "%s/%s", test_dir, test_root_name); |
| 233 | if (ret < 0) { |
| 234 | diag("Failed to format test directory path"); |
| 235 | goto end; |
| 236 | } |
| 237 | ok(dir_exists(test_dir_path), "Test directory still exists after skip"); |
| 238 | tests_ran++; |
| 239 | |
| 240 | ret = remove_file_from_hierarchy(test_dir_handle, test_root_name); |
| 241 | if (ret) { |
| 242 | diag("Failed to remove file from test folder hierarchy"); |
| 243 | goto end; |
| 244 | } |
| 245 | |
| 246 | ret = lttng_directory_handle_remove_subdirectory_recursive( |
| 247 | test_dir_handle, test_root_name, LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
| 248 | ok(ret == 0, |
| 249 | "No error returned when recursively removing empty hierarchy with LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG"); |
| 250 | tests_ran++; |
| 251 | |
| 252 | ok(!dir_exists(test_dir_path) && errno == ENOENT, |
| 253 | "Folder hierarchy %s successfully removed", |
| 254 | test_dir_path); |
| 255 | tests_ran++; |
| 256 | ret = 0; |
| 257 | end: |
| 258 | lttng_directory_handle_put(test_dir_handle); |
| 259 | free(created_dir); |
| 260 | free(test_dir_path); |
| 261 | return ret == 0 ? tests_ran : ret; |
| 262 | } |
| 263 | |
| 264 | int main() |
| 265 | { |
| 266 | int ret; |
| 267 | char test_dir[] = "/tmp/lttng-XXXXXX"; |
| 268 | int tests_left = TEST_COUNT; |
| 269 | size_t func_idx; |
| 270 | |
| 271 | plan_tests(TEST_COUNT); |
| 272 | |
| 273 | diag("lttng_directory_handle tests"); |
| 274 | |
| 275 | if (!mkdtemp(test_dir)) { |
| 276 | diag("Failed to generate temporary test directory"); |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | for (func_idx = 0; func_idx < sizeof(test_funcs) / sizeof(*test_funcs); func_idx++) { |
| 281 | tests_left -= test_funcs[func_idx](test_dir); |
| 282 | } |
| 283 | if (tests_left) { |
| 284 | diag("Skipping %d tests that could not be executed due to a prior error", |
| 285 | tests_left); |
| 286 | skip(tests_left, "test due to an error"); |
| 287 | } |
| 288 | end: |
| 289 | ret = rmdir(test_dir); |
| 290 | if (ret) { |
| 291 | diag("Failed to clean-up test directory: %s", strerror(errno)); |
| 292 | } |
| 293 | return exit_status(); |
| 294 | } |