-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.h
158 lines (132 loc) · 3.36 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <dirent.h>
bool is_empty(const char* path){
DIR* dir = opendir(path);
struct dirent* ent;
int count = 0;
while ((ent = readdir(dir)) != NULL) {
count++;
}
if(count == 2){
return YES;
}else{
return NO;
}
}
static void NSPrint(NSString *format, ...)
{
va_list args;
va_start(args, format);
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
fprintf(stdout, "%s\n", [string UTF8String]);
#if !__has_feature(objc_arc)
[string release];
#endif
}
/*
int copy_dir(const char *from, const char *to) {
NSError *copyError = nil;
[[NSFileManager defaultManager]
copyItemAtPath:[NSString stringWithUTF8String:from]
toPath:[NSString stringWithUTF8String:to] error:©Error];
if (copyError != nil) {
NSPrint(@"%@", [copyError localizedDescription]);
return 1;
}
return 0;
}*/
int copy_dir(const char *from, const char *to);
#include <sys/stat.h>
#define BUFFERSIZE 0x4000
#define COPYMORE 0644
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;
}