| 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/compat/dirent.h> |
| 23 | #include <babeltrace/compat/limits.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
| 26 | void recursive_rmdir(const char *path) |
| 27 | { |
| 28 | struct dirent *entry; |
| 29 | DIR *dir = opendir(path); |
| 30 | |
| 31 | if (!dir) { |
| 32 | perror("# opendir"); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | while ((entry = readdir(dir))) { |
| 37 | struct stat st; |
| 38 | char filename[PATH_MAX]; |
| 39 | |
| 40 | if (snprintf(filename, sizeof(filename), "%s/%s", |
| 41 | path, entry->d_name) <= 0) { |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | if (stat(filename, &st)) { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if (S_ISREG(st.st_mode)) { |
| 50 | unlinkat(bt_dirfd(dir), entry->d_name, 0); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | rmdir(path); |
| 55 | closedir(dir); |
| 56 | } |