| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2019 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 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 |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <dirent.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "common/common.h" |
| 33 | #include "lib/util/dir.h" |
| 34 | #include "lib/util/file.h" |
| 35 | #include "lib/util/string_array.h" |
| 36 | |
| 37 | static int |
| 38 | compare_timespec(struct timespec *a, struct timespec *b) |
| 39 | { |
| 40 | if (a->tv_sec == b->tv_sec) { |
| 41 | if (a->tv_nsec < b->tv_nsec) { |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | if (a->tv_nsec > b->tv_nsec) { |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | if (a->tv_sec < b->tv_sec) { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | if (a->tv_sec > b->tv_sec) { |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | sort_by_ctime(const void *a, const void *b, void *arg) |
| 65 | { |
| 66 | const char *name_a = *(const char **)a; |
| 67 | const char *name_b = *(const char **)b; |
| 68 | int dirfd = *(int *)arg; |
| 69 | const char *base_a; |
| 70 | const char *base_b; |
| 71 | struct stat stat_a; |
| 72 | struct stat stat_b; |
| 73 | errno_t ret; |
| 74 | |
| 75 | base_a = file_get_basename(name_a); |
| 76 | if (base_a == NULL) { |
| 77 | ERROR("Unable to get basename of [%s]", name_a); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | base_b = file_get_basename(name_b); |
| 82 | if (base_b == NULL) { |
| 83 | ERROR("Unable to get basename of [%s]", name_b); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | ret = fstatat(dirfd, base_a, &stat_a, 0); |
| 88 | if (ret != 0) { |
| 89 | ret = errno; |
| 90 | ERROR("Unable to stat [%s] [%d]: %s", name_a, ret, strerror(ret)); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | ret = fstatat(dirfd, base_b, &stat_b, 0); |
| 95 | if (ret != 0) { |
| 96 | ret = errno; |
| 97 | ERROR("Unable to stat [%s] [%d]: %s", name_b, ret, strerror(ret)); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | return compare_timespec(&stat_a.st_ctim, &stat_b.st_ctim); |
| 102 | } |
| 103 | |
| 104 | static bool |
| 105 | is_dot_dir(const char *name) |
| 106 | { |
| 107 | return strcmp(name, ".") == 0 || strcmp(name, "..") == 0; |
| 108 | } |
| 109 | |
| 110 | static bool |
| 111 | is_directory(struct dirent *entry, int dirfd) |
| 112 | { |
| 113 | struct stat statres; |
| 114 | errno_t ret; |
| 115 | |
| 116 | #ifdef _DIRENT_HAVE_D_TYPE |
| 117 | if (entry->d_type == DT_DIR) { |
| 118 | return true; |
| 119 | } else if (entry->d_type != DT_UNKNOWN) { |
| 120 | return false; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | /* We must use stat() if d_type is not available or it couldn't determine |
| 125 | * the type (which may happen on some filesystems). */ |
| 126 | |
| 127 | ret = fstatat(dirfd, entry->d_name, &statres, 0); |
| 128 | if (ret != 0) { |
| 129 | ret = errno; |
| 130 | ERROR("Unable to stat directory [%d]: %s", ret, strerror(ret)); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (S_ISDIR(statres.st_mode)) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | static errno_t |
| 142 | dir_open(const char *path, |
| 143 | DIR **_dirstream, |
| 144 | int *_descriptor) |
| 145 | { |
| 146 | DIR *dirstream; |
| 147 | int descriptor; |
| 148 | errno_t ret; |
| 149 | |
| 150 | if (path == NULL) { |
| 151 | return EINVAL; |
| 152 | } |
| 153 | |
| 154 | dirstream = opendir(path); |
| 155 | if (dirstream == NULL) { |
| 156 | ret = errno; |
| 157 | /* To silence static analyzers that assumes that errno can be 0 here. */ |
| 158 | if (ret == EOK) { |
| 159 | ret = EINVAL; |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | /* Descriptor is closed when closedir() is called. */ |
| 166 | descriptor = dirfd(dirstream); |
| 167 | if (descriptor == -1) { |
| 168 | ret = errno; |
| 169 | /* To silence static analyzers that assumes that errno can be 0 here. */ |
| 170 | if (ret == EOK) { |
| 171 | ret = EINVAL; |
| 172 | } |
| 173 | |
| 174 | closedir(dirstream); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | *_dirstream = dirstream; |
| 179 | *_descriptor = descriptor; |
| 180 | |
| 181 | return EOK; |
| 182 | } |
| 183 | |
| 184 | errno_t |
| 185 | dir_list(const char *path, |
| 186 | uint32_t flags, |
| 187 | char ***_items, |
| 188 | int *_dirfd) |
| 189 | { |
| 190 | struct dirent *entry; |
| 191 | DIR *dirstream; |
| 192 | char *fullpath; |
| 193 | char **items; |
| 194 | int dirfd; |
| 195 | int dupfd; |
| 196 | errno_t ret; |
| 197 | |
| 198 | ret = dir_open(path, &dirstream, &dirfd); |
| 199 | if (ret != EOK) { |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | items = string_array_create(1); |
| 204 | if (items == NULL) { |
| 205 | ret = ENOMEM; |
| 206 | goto done; |
| 207 | } |
| 208 | |
| 209 | errno = 0; |
| 210 | while ((entry = readdir(dirstream)) != NULL) { |
| 211 | if (is_dot_dir(entry->d_name)) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | if (is_directory(entry, dirfd)) { |
| 216 | if (!(flags & DIR_LIST_DIRS)) { |
| 217 | continue; |
| 218 | } |
| 219 | } else { |
| 220 | if (!(flags & DIR_LIST_FILES)) { |
| 221 | continue; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (flags & DIR_LIST_FULL_PATH) { |
| 226 | fullpath = format("%s/%s", path, entry->d_name); |
| 227 | if (fullpath == NULL) { |
| 228 | ret = ENOMEM; |
| 229 | goto done; |
| 230 | } |
| 231 | |
| 232 | items = string_array_add_value(items, fullpath, true); |
| 233 | free(fullpath); |
| 234 | } else { |
| 235 | items = string_array_add_value(items, entry->d_name, true); |
| 236 | } |
| 237 | |
| 238 | if (items == NULL) { |
| 239 | ret = ENOMEM; |
| 240 | goto done; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (flags & DIR_LIST_SORT_BY_CTIME) { |
| 245 | qsort_r(items, string_array_count(items), sizeof(char *), |
| 246 | sort_by_ctime, &dirfd); |
| 247 | } |
| 248 | |
| 249 | if (_dirfd != NULL) { |
| 250 | dupfd = dup(dirfd); |
| 251 | if (dupfd == -1) { |
| 252 | ret = errno; |
| 253 | /* To silence static analyzers that assumes that errno |
| 254 | * can be 0 here. */ |
| 255 | if (ret == EOK) { |
| 256 | ret = EINVAL; |
| 257 | } |
| 258 | goto done; |
| 259 | } |
| 260 | |
| 261 | *_dirfd = dupfd; |
| 262 | } |
| 263 | |
| 264 | *_items = items; |
| 265 | |
| 266 | ret = EOK; |
| 267 | |
| 268 | done: |
| 269 | if (ret == EOK && _dirfd != NULL) { |
| 270 | closedir(dirstream); |
| 271 | } |
| 272 | |
| 273 | if (ret != EOK) { |
| 274 | string_array_free(items); |
| 275 | } |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | errno_t |
| 281 | dir_remove(const char *path) |
| 282 | { |
| 283 | char **subdirs = NULL; |
| 284 | char **files = NULL; |
| 285 | int dirfd = -1; |
| 286 | errno_t ret; |
| 287 | int i; |
| 288 | |
| 289 | ret = dir_list(path, DIR_LIST_DIRS | DIR_LIST_FULL_PATH, &subdirs, NULL); |
| 290 | if (ret == ENOENT) { |
| 291 | ret = EOK; |
| 292 | goto done; |
| 293 | } else if (ret != EOK) { |
| 294 | goto done; |
| 295 | } |
| 296 | |
| 297 | ret = dir_list(path, DIR_LIST_FILES, &files, &dirfd); |
| 298 | if (ret == ENOENT) { |
| 299 | ret = EOK; |
| 300 | goto done; |
| 301 | } else if (ret != EOK) { |
| 302 | goto done; |
| 303 | } |
| 304 | |
| 305 | for (i = 0; subdirs[i] != NULL; i++) { |
| 306 | ret = dir_remove(subdirs[i]); |
| 307 | if (ret != EOK) { |
| 308 | goto done; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | for (i = 0; files[i] != NULL; i++) { |
| 313 | INFO("Removing file [%s/%s]", path, files[i]); |
| 314 | ret = unlinkat(dirfd, files[i], 0); |
| 315 | if (ret != 0) { |
| 316 | ret = errno; |
| 317 | goto done; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | INFO("Removing directory [%s]", path); |
| 322 | ret = rmdir(path); |
| 323 | if (ret != 0) { |
| 324 | ret = errno; |
| 325 | goto done; |
| 326 | } |
| 327 | |
| 328 | ret = EOK; |
| 329 | |
| 330 | done: |
| 331 | string_array_free(subdirs); |
| 332 | string_array_free(files); |
| 333 | |
| 334 | if (dirfd != -1) { |
| 335 | close(dirfd); |
| 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |