-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
547 lines (327 loc) · 12 KB
/
Copy pathutil.c
File metadata and controls
547 lines (327 loc) · 12 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "Types.h"
void configure(int argc, char **argv, char **origindir, char **destdir) { // Function for -i argument
if(argc > 1) {
/*
argv choices:
-v verbose
-d deleted
-l links
*/
// Go through all arguments
for (int i = 1 ; i < argc ; i++) {
// Check if we have -c as argument
if(!strcmp("-v", argv[i])) { /* if we have -c */
verboseFlag = 1;
} else if(!strcmp("-d", argv[i])) { /* if we have -c */
deleteFlag = 1;
} else if(!strcmp("-l", argv[i])) { /* if we have -c */
linksFlag = 1;
} else {
if(*origindir == NULL) {
*origindir = (char *) malloc(sizeof(strlen(argv[i]) + 1));
strcpy(*origindir, argv[i]);
} else {
*destdir = (char *) malloc(sizeof(strlen(argv[i]) + 1));
strcpy(*destdir, argv[i]);
}
}
}
}
}
statistics parseDir(char *srcPath, char *destPath, clock_t programStart, iList *list) {
DIR *dir_ptr;
struct dirent *direntp;
statistics stats = {0, 0, 0};
if((dir_ptr = opendir(srcPath)) == NULL) {
fprintf(stderr, "Error while opening %s.", srcPath);
exit(-1);
}
while((direntp = readdir(dir_ptr)) != NULL) {
if(direntp->d_name[0] == '.') // dont mind these
continue;
// up total entities counter
stats.entityCount++;
// build path
char path[1024]; // path is source path
strcpy(path, srcPath);
strcat(path, "/");
strcat(path, direntp->d_name);
char path2[1024]; // path2 is destionation path
strcpy(path2, destPath);
strcat(path2, "/");
strcat(path2, direntp->d_name);
struct stat st, st2;
lstat(path, &st);
// Loop
if(st.st_mtime > programStart)// if modification time is after program start means we have a cycle, so skip to prevent infinite looping
continue;
// Directories
if(lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) { // src is dir
if(lstat(path2, &st2) == 0) { // directory already exists in dest
if(!S_ISDIR(st2.st_mode)) { // entity in destination folder is not dir
remove(path2); // remove the false type entity
if(verboseFlag) { // log removal if needed
if(S_ISREG(st.st_mode))
printf("Removed file %s.\n", path2);
else if(S_ISLNK(st.st_mode))
printf("Removed soft link %s.\n", path2);
else
printf("Removed soft link %s.\n", path2);
}
}
}
if(lstat(path2, &st2) == -1) {
if(errno == ENOENT) { // dir in destination folder does not exist
mkdir(path2, S_IRWXU); // make the directory
struct stat st;
lstat(path, &st);
chmod(path2, st.st_mode);
stats.copiedCount++; // up respective counter
if(verboseFlag) // log if needed
printf("Created directory %s\n", path2);
} else {
exit(-1); // error occured
}
}
statistics temp = parseDir(path, path2, programStart, list); // call recursively for next depth
// Increment counters based on values returned
stats.entityCount += temp.entityCount;
stats.copiedCount += temp.copiedCount;
stats.bytesCopied += temp.bytesCopied;
} else if(lstat(path, &st) == 0 && S_ISREG(st.st_mode)) { // src is file / hard link
// Hard Links
if(linksFlag) {
if(manageHardLinks(path, path2, list)) { // if we ended up creating a hard link for this entity
lstat(path2, &st2);
stats.bytesCopied += st2.st_size; // add site of link to bytes copied counter
stats.copiedCount++; // up the entities copied counter
continue; // skip rest of loop
}
}
if(lstat(path2, &st2) == 0) { // file in destination folder exists
if(!S_ISREG(st2.st_mode)) { // file in destination folder is not file
remove(path2); // so we remove false type entity
if(verboseFlag) { // if needed log removal
if(S_ISDIR(st2.st_mode))
printf("Removed directory %s.\n", path2);
else if(S_ISLNK(st2.st_mode))
printf("Removed soft link %s.\n", path2);
else
printf("Removed entity %s.\n", path2);
}
} else if((st.st_size != st2.st_size) || (st.st_mtime > st2.st_mtime)) { // size of src and dest is not the same so we overwrite
// OR dest has more recent modification time
remove(path2); // remove old dest file
if(verboseFlag) // log if needed
printf("Removed file %s.\n", path2);
stats.bytesCopied += copy(path, path2); // copy src to dest path
stats.copiedCount++; // up copied entities counter
if(verboseFlag) // log if needed
printf("Created file %s\n", path2);
}
}
if(lstat(path2, &st2) == -1) {
if(errno == ENOENT) { // file in destination folder does not exist
stats.bytesCopied += copy(path, path2); // copy src to dest path
stats.copiedCount++; // up copied entities counter
if(verboseFlag) // log if needed
printf("Created file %s\n", path2);
} else {
exit(-1); // error
}
}
} else if(linksFlag) { // if we are managing links
if(lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) { // src is soft link
if(lstat(path2, &st2) == 0) { // entity in destination folder exists
if(!S_ISLNK(st2.st_mode)) { // entity in destination folder is not link
remove(path2); // remove false entity
if(verboseFlag) { // log if needed
if(!S_ISREG(st2.st_mode))
printf("Removed file %s.\n", path2);
else if(!S_ISDIR(st2.st_mode))
printf("Removed directory %s.\n", path2);
else
printf("Removed entity %s.\n", path2);
}
} else if((st.st_size != st2.st_size) || (st.st_mtime > st2.st_mtime)) { // size of src and dest is not the same so we overwrite
// OR dest has more recent modification time
remove(path2); // remove old file
if(verboseFlag) // log if needed
printf("Removed soft link %s.\n", path2);
char buf[PATH_MAX];
ssize_t len = readlink(path, buf, sizeof(buf) - 1); // get value of soft link
buf[len] = '\0';
if(len == -1)
exit(-1); // error
symlink(buf, path2); // create soft link
lstat(path2, &st2);
stats.bytesCopied += st2.st_size; // add site of link to bytes copied counter
stats.copiedCount++; // up entity creation counter
if(verboseFlag) // log if needed
printf("Created link %s\n", path2);
}
}
if(lstat(path2, &st2) == -1) {
if(errno == ENOENT) { // file in destination folder does not exist
char buf[PATH_MAX];
ssize_t len = readlink(path, buf, sizeof(buf) - 1); // get value of soft link
buf[len] = '\0';
if(len == -1)
exit(-1); // bad :(
symlink(buf, path2); // create soft link
lstat(path2, &st2);
stats.bytesCopied += st2.st_size; // add site of link to bytes copied counter
stats.copiedCount++; // up entity creation counter
if(verboseFlag) // log if needed
printf("Created link %s\n", path2);
} else {
exit(-1); // error
}
}
}
}
}
if(closedir(dir_ptr) == - 1) { // close dir we parsed -- error handle
fprintf(stderr, "Error while closing directory %s\n", srcPath);
exit(-1);
}
return stats; // return statistics
}
int copy(char *src, char *dest) {
int n, from, to, bytesCopied = 0;
char buf[BUFFSIZE];
mode_t fdmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if((from = open(src, O_RDONLY)) < 0) { // open src location -- error handling
fprintf(stderr, "Error while opening entity %s\n", src);
return -1;
}
if((to = open(dest, O_WRONLY | O_CREAT, fdmode )) < 0 ) { // open dest location -- error handling
fprintf(stderr, "Error while opening entity %s\n", dest);
return -1;
}
while((n = read(from, buf, sizeof(buf))) > 0) // copy until there is no more to copy
bytesCopied += write(to, buf, n);
struct stat st;
lstat(src, &st);
chmod(dest, st.st_mode); // copy over permissions
close(from);
close(to);
return bytesCopied;
}
int manageHardLinks(char *path, char *path2, iList *list) {
struct stat st, st2;
if(lstat(path, &st) == 0 && S_ISREG(st.st_mode)) { // if src entity is reg file
char *inodeExists = NULL;
if(*list == NULL) { // if this is the first entry, create the list
*list = iListCreate(st.st_ino, path2);
} else {
inodeExists = iListInsert(*list, st.st_ino, path2);
}
if(lstat(path2, &st2) == 0) { // entity in destination folder exists
if(!S_ISREG(st2.st_mode) || (inodeExists != NULL)) { // entity in destination folder is not reg -- or entity exists but is hard copy
remove(path2); // remove false entity
if(verboseFlag) { // log if needed
if(S_ISLNK(st2.st_mode))
printf("Removed soft link %s.\n", path2);
else if(S_ISDIR(st2.st_mode))
printf("Removed directory %s.\n", path2);
else if(S_ISREG(st2.st_mode))
printf("Removed file %s.\n", path2);
else
printf("Removed entity %s.\n", path2);
}
} else if((st.st_size != st2.st_size) || (st.st_mtime > st2.st_mtime)) { // size of src and dest is not the same so we overwrite
// OR dest has more recent modification time
if(inodeExists != NULL) { // if i node exists already in map this means, our file can be made a hard link
if(lstat(path2, &st2) == 0 && S_ISREG(st2.st_mode)) { // if deep copy exists from before -- remove before makign hard link
remove(path2);
if(verboseFlag)
printf("Removed file %s.\n", path2);
}
link(inodeExists, path2);
if(verboseFlag)
printf("Created hard link %s.\n", path2);
return 1; // Skip rest
}
}
}
if(lstat(path2, &st2) == -1) {
if(errno == ENOENT) { // file in destination folder does not exist
if(inodeExists != NULL) { // if i node exists already in map this means, our file can be made a hard link
if(lstat(path2, &st2) == 0 && S_ISREG(st2.st_mode)) { // if deep copy exists from before -- remove before makign hard link
remove(path2);
if(verboseFlag)
printf("Removed file %s.\n", path2);
}
link(inodeExists, path2);
if(verboseFlag)
printf("Created hard link %s.\n", path2);
return 1; // Skip rest
}
} else {
exit(-1); //error
}
}
}
return 0;
}
int cleanup(char *srcPath, char *destPath) {
DIR *dir_ptr;
struct dirent *direntp;
int removals = 0;
if((dir_ptr = opendir(destPath)) == NULL) {
fprintf(stderr, "Error while opening %s.", srcPath);
exit(-1);
}
while((direntp = readdir(dir_ptr)) != NULL) {
// dont mind these
if(direntp->d_name[0] == '.')
continue;
// build path
char path[1024];
strcpy(path, srcPath);
strcat(path, "/");
strcat(path, direntp->d_name);
char path2[1024];
strcpy(path2, destPath);
strcat(path2, "/");
strcat(path2, direntp->d_name);
struct stat st, st2;
if(lstat(path2, &st2) == 0 && S_ISDIR(st2.st_mode)) { // if current entity is a dir
if(lstat(path, &st) == 0) {
removals += cleanup(path, path2);
} else if(lstat(path, &st) == -1) {
if(errno == ENOENT) { // and it does not exist in the relative path of source
rmdir(path2); // remove it
removals++;
if(verboseFlag)
printf("Removed directory %s.\n", path2);
} else {
exit(-1);
}
}
} else if(lstat(path2, &st2) == 0 && (S_ISREG(st2.st_mode) || S_ISLNK(st2.st_mode))) { // if current entity is a file
if(lstat(path, &st) == -1) {
if(errno == ENOENT) { // file in destination folder does not exist
remove(path2);
removals++;
if(verboseFlag) {
if(S_ISLNK(st2.st_mode))
printf("Removed soft link %s.\n", path2);
else if S_ISREG(st2.st_mode)
printf("Removed file %s.\n", path2);
else
printf("Removed entity %s.\n", path2);
}
} else {
exit(-1);
}
}
}
}
if(closedir(dir_ptr) == - 1) {
fprintf(stderr, "Error while closing directory %s\n", srcPath);
exit(-1);
}
return removals;
}