forked from akusio/KernBypass-Public
-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.h
122 lines (102 loc) · 2.76 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <dirent.h>
#include <sys/stat.h>
#define BUFFERSIZE 0x4000
#define COPYMORE 0644
bool is_empty(const char *path) {
DIR *dir = opendir(path);
struct dirent *ent;
int count = 0;
if ((dir = opendir(path)) == NULL) {
printf("ERROR: Couldn't open directory: %s\n",path);
exit(1);
}
while ((ent = readdir(dir)) != NULL) {
count++;
}
closedir(dir);
if (count == 2) {
return YES;
} else {
return NO;
}
}
int copy_dir(const char *from, const char *to);
int file_exists(const char* filename) {
struct stat buffer;
int exist = stat(filename,&buffer);
if (exist == 0) {
return 1;
} else { // -1
return 0;
}
}
void oops(const char *msg, char *s2) {
fprintf(stderr, "%s", msg);
perror(s2);
}
int copyFiles(char *source, char *destination) {
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
if ((in_fd = open(source, O_RDONLY)) == -1) {
oops("Cannot open ", source);
return 1;
}
if ((out_fd = creat(destination, COPYMORE)) == -1) {
oops("Cannot creat ", destination);
return 1;
}
while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0) {
if (write(out_fd, buf, n_chars) != n_chars) {
oops("Write error to ", destination);
return 1;
}
if (n_chars == -1) {
oops("Read error from ", source);
return 1;
}
}
if (close(in_fd) == -1 || close(out_fd) == -1) {
oops("Error closing files", "");
return 1;
}
return 0;
}
int copy_dir(const char *name, const char *target) {
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name))) return 1;
mkdir(target, 0755);
char path[1024];
char targetpath[1024];
int child = 0;
while ((entry = readdir(dir)) != NULL) {
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
snprintf(targetpath, sizeof(targetpath), "%s/%s", target, entry->d_name);
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
}
printf("Copying: %s...", path);
child++;
if (entry->d_type == DT_DIR) {
if (mkdir(targetpath, 0755)) {
perror("create folder");
}
if (!file_exists(targetpath)) {
printf("Failed\n");
continue;
}
printf("\n");
copy_dir(path, targetpath);
} else {
if (copyFiles(path, targetpath) != 0) {
printf("Failed\n");
}
printf("\n");
}
}
closedir(dir);
if (child == 0) {
return 1;
}
return 0;
}