/src/clib/deps/rimraf/rimraf.c
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | // |
3 | | // rimraf.c |
4 | | // |
5 | | // Copyright (c) 2013 Stephen Mathieson |
6 | | // MIT licensed |
7 | | // |
8 | | |
9 | | #define _POSIX_C_SOURCE 200809L |
10 | | |
11 | | // hack |
12 | | #ifndef S_IFDIR |
13 | 0 | #define S_IFDIR 0040000 |
14 | | #endif |
15 | | |
16 | | #include <dirent.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #include <sys/stat.h> |
20 | | #include <unistd.h> |
21 | | #include "path-join/path-join.h" |
22 | | #include "rimraf.h" |
23 | | |
24 | | /* |
25 | | * rm -rf $path |
26 | | */ |
27 | | |
28 | | int |
29 | 0 | rimraf(const char *path) { |
30 | 0 | DIR *dir = opendir(path); |
31 | 0 | if (NULL == dir) return -1; |
32 | | |
33 | 0 | struct dirent *dp = NULL; |
34 | 0 | while (NULL != (dp = readdir(dir))) { |
35 | 0 | if (0 == strcmp(".", dp->d_name) |
36 | 0 | || 0 == strcmp("..", dp->d_name)) continue; |
37 | | |
38 | 0 | char *f = path_join(path, dp->d_name); |
39 | 0 | if (NULL == f) return -1; |
40 | | |
41 | 0 | struct stat s; |
42 | 0 | if (0 != stat(f, &s)) return -1; |
43 | 0 | if (s.st_mode & S_IFDIR) { |
44 | | // rimraf dirs |
45 | 0 | if (-1 == rimraf(f)) return -1; |
46 | 0 | } else { |
47 | | // unlink files |
48 | 0 | if (-1 == unlink(f)) return -1; |
49 | 0 | } |
50 | 0 | free(f); |
51 | 0 | } |
52 | 0 | free(dp); |
53 | 0 | closedir(dir); |
54 | |
|
55 | 0 | return rmdir(path); |
56 | 0 | } |