-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysync.c
More file actions
238 lines (188 loc) · 6.55 KB
/
mysync.c
File metadata and controls
238 lines (188 loc) · 6.55 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "mysync.h"
OPTION *option = NULL;
File *master = NULL;
// returns new string containing concatenated strings
char *concat(const char *str1, const char *str2)
{
size_t len = strlen(str1) + strlen(str2) + 1;
char *result = (char *)malloc(len);
if (result) {
strcpy(result, str1);
strcat(result, str2);
}
return result;
}
// initialises option struct
void initialise_option(void)
{
option->allFiles = false;
option->dryRun = false;
option->iPattern = malloc(sizeof(PatternInfo));
CHECK_ALLOC(option->iPattern);
option->iPattern->npatterns = 0;
option->iPattern->flag = false;
option->iPattern->patterns = NULL;
option->oPattern = malloc(sizeof(PatternInfo));
CHECK_ALLOC(option->oPattern)
option->oPattern->npatterns = 0;
option->oPattern->flag = false;
option->oPattern->patterns = NULL;
option->preserveAttributes = false;
option->recursive = false;
option->verbose = false;
}
// Function takes a path and checks if it corresponds to a valid directory
bool is_valid_dir(const char *path)
{
struct stat info;
if (stat(path, &info) != 0) {
return false;
}
return S_ISDIR(info.st_mode);
}
// Function processes all command line arguments, checks which options are enabled, processes the remaining directories and add the last directory to a directory list
Dir *process_args(int count, char *args[], Dir *dirlist)
{
if (count < 3) {
fprintf(stderr, "Usage: %s [options] directory1 directory2 [directory3 ...]\n", args[0]);
exit(EXIT_FAILURE);
}
int opt;
opterr = 0;
char *regexPattern;
int status;
// process command line options
while((opt = getopt(count, args, OPTLIST)) != -1) {
switch(opt) {
case 'a':
option->allFiles = true;
break;
case 'i':
regexPattern = strdup(optarg);
CHECK_ALLOC(regexPattern);
regexPattern = glob2regex(regexPattern);
if (option->iPattern->patterns == NULL) {
option->iPattern->patterns = calloc(1, sizeof(regex_t *));
CHECK_ALLOC(option->iPattern->patterns);
}
option->iPattern->patterns[option->iPattern->npatterns] = malloc(sizeof(regex_t));
CHECK_ALLOC(option->iPattern->patterns[option->iPattern->npatterns]);
status = regcomp(option->iPattern->patterns[option->iPattern->npatterns], regexPattern, 0);
if (status != 0) {
perror("couldn't compile regex expression");
exit(EXIT_FAILURE);
}
option->iPattern->npatterns++;
option->iPattern->flag = true;
PRINT_IF_V("include pattern found: %s\n", regexPattern);
free(regexPattern);
break;
case 'n':
option->dryRun = true;
option->verbose = true;
break;
case 'o':
regexPattern = strdup(optarg);
CHECK_ALLOC(regexPattern);
regexPattern = glob2regex(regexPattern);
if (option->oPattern->patterns == NULL) {
option->oPattern->patterns = calloc(1, sizeof(regex_t));
CHECK_ALLOC(option->oPattern->patterns);
}
option->oPattern->patterns[option->oPattern->npatterns] = malloc(sizeof(regex_t));
CHECK_ALLOC(option->oPattern->patterns[option->oPattern->npatterns]);
status = regcomp(option->oPattern->patterns[option->oPattern->npatterns], regexPattern, 0);
if (status != 0) {
perror("couldn't compile regex expression");
exit(EXIT_FAILURE);
}
option->oPattern->npatterns++;
option->oPattern->flag = true;
PRINT_IF_V("only pattern found: %s\n", regexPattern);
free(regexPattern);
break;
case 'p':
option->preserveAttributes = true;
break;
case 'r':
option->recursive = true;
break;
case 'v':
option->verbose = true;
break;
default:
count = -1;
}
}
if (count < 0) {
perror("cannot read command line arguments");
exit(EXIT_FAILURE);
}
count -= optind;
args += optind;
if (count < 2) {
perror("Insufficient directories supplied");
exit(EXIT_FAILURE);
}
// find the top level directory paths
while (count > 0) {
if (is_valid_dir(*args)) {
Dir *newdir = (Dir *)malloc(sizeof(Dir));
newdir->next = NULL;
newdir->dirpath = strdup(*args);
CHECK_ALLOC(newdir->dirpath);
if (dirlist == NULL) {
dirlist = newdir;
} else {
Dir *current = dirlist;
while (current->next != NULL) {
current = current->next;
}
current->next = newdir;
}
count--;
args++;
} else {
perror("Invalid directory path supplied");
exit(EXIT_FAILURE);
}
}
return dirlist;
}
int main(int argc, char *argv[])
{
Dir *dirlist = NULL;
option = malloc(sizeof(OPTION));
CHECK_ALLOC(option);
initialise_option();
dirlist = process_args(argc, argv, dirlist);
if (argc < 2) {
perror("Insufficient directories supplied");
exit(EXIT_FAILURE);
}
Dir *currdir = dirlist;
while (currdir != NULL) {
if (is_valid_dir(currdir->dirpath)) {
initialise_master(currdir->dirpath, strlen(currdir->dirpath));
currdir = currdir->next;
} else {
perror("Invalid directory path supplied");
exit(EXIT_FAILURE);
}
}
// provide the representation of the master directory contents
if (option->verbose) {
printf("printing the master blueprint that will be applied to all directories:\n");
File *current = master;
while (current != NULL) {
printf("master file root path: %s\n", current->root);
current = current->next;
}
}
currdir = dirlist;
while (currdir != NULL) {
updatedir(currdir->dirpath);
currdir = currdir->next;
}
PRINT_IF_V("\nsyncing complete\n");
}