-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpdir.c
104 lines (83 loc) · 2.58 KB
/
cpdir.c
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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
void *copy_directory(void *args);
void copy_file(void *paths);
typedef struct {
char source[2048];
char destination[2048];
} PathFiles;
void *copy_dir(void *args){
PathFiles *paths = (PathFiles *)args;
DIR *dir = opendir(paths->source);
if (dir == NULL) {
perror("Error opening directory");
return NULL;
}
mkdir(paths->destination, 0755);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
char source_path[2048];
char destination_path[2048];
if (snprintf(source_path, sizeof(source_path), "%s/%s", paths->source, entry->d_name) >= sizeof(source_path) ||
snprintf(destination_path, sizeof(destination_path), "%s/%s", paths->destination, entry->d_name) >= sizeof(destination_path)) {
fprintf(stderr, "Path too long, skipping: %s\n", entry->d_name);
continue;
}
PathFiles *new_paths = malloc(sizeof(PathFiles));
strcpy(new_paths->source, source_path);
strcpy(new_paths->destination, destination_path);
if (entry->d_type == DT_DIR) {
pthread_t thread;
pthread_create(&thread, NULL, copy_dir, new_paths);
pthread_detach(thread);
}
else {
copy_file(new_paths);
}
}
closedir(dir);
free(paths);
return NULL;
}
void copy_file(void *paths){
PathFiles *path = (PathFiles *)paths;
int src_file = open(path->source, O_RDONLY);
if (src_file == -1) {
perror("Error opening source file");
return;
}
int dest_file = open(path->destination, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_file == -1) {
perror("Error creating destination file");
close(src_file);
return;
}
char buffer[4096];
ssize_t bytes;
while ((bytes = read(src_file, buffer, sizeof(buffer))) > 0) {
write(dest_file, buffer, bytes);
}
close(src_file);
close(dest_file);
}
int main(int argc, char *argv[]){
if (argc!=3){
printf("Usage: %s <source_directory> <destination_directory>\n", argv[0]);
return 1;
}
PathFiles *paths = malloc(sizeof(PathFiles));
strcpy(paths->source, argv[1]);
strcpy(paths->destination, argv[2]);
copy_dir(paths);
sleep(1);
return 0;
}