1 #include <stdio.h>
2 #include <ftw.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <fcntl.h>
6 
7 
remove_cb(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)8 static int remove_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
9     int rv = 2;
10     if (S_ISDIR(sb->st_mode)) {
11         rv = rmdir(fpath);
12     } else {
13         rv = remove(fpath);
14     }
15     printf("lol %s\n", fpath);
16     return rv;
17 }
18 
utilfuzz_rmrf(char * path)19 int utilfuzz_rmrf(char *path) {
20     return nftw(path, remove_cb, 64, FTW_DEPTH | FTW_PHYS);
21 }
22 
23 char *globalto;
24 size_t globallen = 0;
25 
26 #define CP_NAME_MAX_SIZE 512
27 #define CP_BUF_SIZE 4096
cp_cb(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)28 static int cp_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
29     char newname[CP_NAME_MAX_SIZE];
30     char buf[CP_BUF_SIZE];
31     int rv = 2;
32     snprintf(newname, CP_NAME_MAX_SIZE-1, "%s%s", globalto, fpath+globallen);
33     if (FTW_D == typeflag) {
34         rv = mkdir(newname, sb->st_mode);
35     } else {
36         int fdin = open(fpath, O_RDONLY);
37         int fdout = open(newname, O_WRONLY|O_CREAT, sb->st_mode);
38         int nb = read(fdin, buf, CP_BUF_SIZE);
39         while (nb > 0) {
40             write(fdout, buf, nb);
41             nb = read(fdin, buf, CP_BUF_SIZE);
42         }
43         rv = 0;
44     }
45     return rv;
46 }
47 
utilfuzz_cpr(char * pathfrom,char * pathto)48 int utilfuzz_cpr(char *pathfrom, char *pathto) {
49     globalto = pathto;
50     globallen = strlen(pathfrom);
51     return nftw(pathfrom, cp_cb, 64, FTW_PHYS);
52 }
53 
54