| 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 | */ |
| 21 | |
| 22 | #include <babeltrace/context.h> |
| 23 | #include <babeltrace/iterator.h> |
| 24 | #include <babeltrace/compat/dirent.h> |
| 25 | #include <babeltrace/compat/limits.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | struct bt_context *create_context_with_path(const char *path) |
| 29 | { |
| 30 | struct bt_context *ctx; |
| 31 | int ret; |
| 32 | |
| 33 | ctx = bt_context_create(); |
| 34 | if (!ctx) { |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | ret = bt_context_add_trace(ctx, path, "ctf", NULL, NULL, NULL); |
| 39 | if (ret < 0) { |
| 40 | bt_context_put(ctx); |
| 41 | return NULL; |
| 42 | } |
| 43 | return ctx; |
| 44 | } |
| 45 | |
| 46 | void recursive_rmdir(const char *path) |
| 47 | { |
| 48 | struct dirent *entry; |
| 49 | DIR *dir = opendir(path); |
| 50 | |
| 51 | if (!dir) { |
| 52 | perror("# opendir"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | while ((entry = readdir(dir))) { |
| 57 | struct stat st; |
| 58 | char filename[PATH_MAX]; |
| 59 | |
| 60 | if (snprintf(filename, sizeof(filename), "%s/%s", |
| 61 | path, entry->d_name) <= 0) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | if (stat(filename, &st)) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if (S_ISREG(st.st_mode)) { |
| 70 | unlinkat(bt_dirfd(dir), entry->d_name, 0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | rmdir(path); |
| 75 | closedir(dir); |
| 76 | } |