Test fix: clean trace files left behind by test_ctf_ir_ref
[babeltrace.git] / tests / lib / common.c
CommitLineData
aa968dde
YB
1/*
2 * common.c
3 *
4 * Lib BabelTrace - Common function to all tests
5 *
6 * Copyright 2012 - Yannick Brosseau <yannick.brosseau@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
aa968dde
YB
21
22#include <babeltrace/context.h>
23#include <babeltrace/iterator.h>
851299b9
JG
24#include <babeltrace/compat/dirent.h>
25#include <sys/stat.h>
aa968dde
YB
26
27struct bt_context *create_context_with_path(const char *path)
28{
29 struct bt_context *ctx;
30 int ret;
31
32 ctx = bt_context_create();
33 if (!ctx) {
34 return NULL;
35 }
36
37 ret = bt_context_add_trace(ctx, path, "ctf", NULL, NULL, NULL);
38 if (ret < 0) {
39 bt_context_put(ctx);
40 return NULL;
41 }
42 return ctx;
43}
851299b9
JG
44
45void recursive_rmdir(const char *path)
46{
47 struct dirent *entry;
48 DIR *dir = opendir(path);
49
50 if (!dir) {
51 perror("# opendir");
52 return;
53 }
54
55 while ((entry = readdir(dir))) {
56 struct stat st;
57 char filename[PATH_MAX];
58
59 if (snprintf(filename, sizeof(filename), "%s/%s",
60 path, entry->d_name) <= 0) {
61 continue;
62 }
63
64 if (stat(filename, &st)) {
65 continue;
66 }
67
68 if (S_ISREG(st.st_mode)) {
69 unlinkat(bt_dirfd(dir), entry->d_name, 0);
70 }
71 }
72
73 rmdir(path);
74 closedir(dir);
75}
This page took 0.031552 seconds and 4 git commands to generate.