2 * Copyright (C) - 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
28 #include <common/compat/directory-handle.h>
33 int lttng_opt_quiet
= 1;
34 int lttng_opt_verbose
= 3;
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)
40 bool dir_exists(const char *path
)
45 ret
= stat(path
, &st
);
46 return ret
== 0 && S_ISDIR(st
.st_mode
);
49 int main(int argc
, char **argv
)
52 struct lttng_directory_handle test_dir_handle
;
53 char test_dir
[] = "/tmp/lttng-XXXXXX";
54 char *created_dir
= NULL
;
56 plan_tests(TEST_COUNT
);
58 diag("directory handle tests");
60 if (!mkdtemp(test_dir
)) {
61 diag("Failed to generate temporary test directory");
65 ret
= lttng_directory_handle_init(&test_dir_handle
, test_dir
);
66 ok(ret
== 0, "Initialized directory handle from the test directory");
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
);
77 diag("Failed to allocate created directory path buffer");
80 ok(dir_exists(created_dir
), "Folder %s exists", created_dir
);
82 ret
= lttng_directory_handle_remove_subdirectory_recursive(
83 &test_dir_handle
, "a", LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG
);
84 ok(ret
== 0, "Recursively removed directory hierarchy %s by removing %s",
87 ret
= rmdir(test_dir
);
89 diag("Failed to clean-up test directory: %s", strerror(errno
));
91 ok(ret
== 0, "Cleaned-up test directory");
92 lttng_directory_handle_fini(&test_dir_handle
);