Fix: rmdir recursive: skip non-empty directories with flag
[lttng-tools.git] / tests / unit / test_directory_handle.c
CommitLineData
93bed9fe
JG
1/*
2 * Copyright (C) - 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#include <assert.h>
19#include <string.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <errno.h>
24#include <stdbool.h>
25#include <sys/stat.h>
26
27#include <tap/tap.h>
28#include <common/compat/directory-handle.h>
29
30#define TEST_COUNT 5
31
32/* For error.h */
33int lttng_opt_quiet = 1;
34int lttng_opt_verbose = 3;
35int lttng_opt_mi;
36
37#define DIR_HIERARCHY "a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p"
38#define DIR_CREATION_MODE (S_IRWXU | S_IRWXG)
39
40bool dir_exists(const char *path)
41{
42 int ret;
43 struct stat st;
44
45 ret = stat(path, &st);
46 return ret == 0 && S_ISDIR(st.st_mode);
47}
48
49int main(int argc, char **argv)
50{
51 int ret;
52 struct lttng_directory_handle test_dir_handle;
53 char test_dir[] = "/tmp/lttng-XXXXXX";
54 char *created_dir = NULL;
55
56 plan_tests(TEST_COUNT);
57
58 diag("directory handle tests");
59
60 if (!mkdtemp(test_dir)) {
61 diag("Failed to generate temporary test directory");
62 goto end;
63 }
64
65 ret = lttng_directory_handle_init(&test_dir_handle, test_dir);
66 ok(ret == 0, "Initialized directory handle from the test directory");
67 if (ret) {
68 goto end;
69 }
70
71 ret = lttng_directory_handle_create_subdirectory_recursive(
72 &test_dir_handle, DIR_HIERARCHY, DIR_CREATION_MODE);
73 ok(ret == 0, "Create folder hierarchy %s from handle to %s",
74 DIR_HIERARCHY, test_dir);
75 ret = asprintf(&created_dir, "%s/%s", test_dir, DIR_HIERARCHY);
76 if (ret < 0) {
77 diag("Failed to allocate created directory path buffer");
78 goto end;
79 }
80 ok(dir_exists(created_dir), "Folder %s exists", created_dir);
81
82 ret = lttng_directory_handle_remove_subdirectory_recursive(
f75c5439 83 &test_dir_handle, "a", LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG);
93bed9fe
JG
84 ok(ret == 0, "Recursively removed directory hierarchy %s by removing %s",
85 DIR_HIERARCHY, "a");
86end:
87 ret = rmdir(test_dir);
88 if (ret) {
89 diag("Failed to clean-up test directory: %s", strerror(errno));
90 }
91 ok(ret == 0, "Cleaned-up test directory");
92 lttng_directory_handle_fini(&test_dir_handle);
93 free(created_dir);
94 return exit_status();
95}
This page took 0.036294 seconds and 4 git commands to generate.