-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.c
374 lines (342 loc) · 11.1 KB
/
copy.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
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
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include "commands.h"
#include "utils.h"
#include "threads.h"
typedef struct
{
bool flag;
int exit_code;
pthread_mutex_t *mutex;
Arena *arena;
} WorkerArgs;
typedef struct
{
char *source;
char *destination;
} Job;
int copy_file(FILE *f_source, FILE *f_dest)
{
char buffer[BUFSIZ];
size_t bytes_read;
if (errno == EACCES)
{
fprintf(stderr, "Permission denied\n");
fclose(f_source);
fclose(f_dest);
return EXIT_FAILURE;
}
if (f_source == NULL || f_dest == NULL)
{
fprintf(stderr, "Error opening file(s)\n");
fclose(f_source);
fclose(f_dest);
return EXIT_FAILURE;
}
while ((bytes_read = fread(buffer, 1, sizeof(buffer), f_source)) > 0)
{
fwrite(buffer, 1, bytes_read, f_dest);
}
fclose(f_source);
fclose(f_dest);
return EXIT_SUCCESS;
}
bool is_parent_dir_valid(char *parent_dir)
{
struct stat st;
if (parent_dir != NULL)
{
if (lstat(parent_dir, &st) == 0)
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
{
return true;
}
}
}
return false;
}
void fail_all_tasks(WorkerArgs *worker_args)
{
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(0);
set_queue_size(0);
set_done(true);
pthread_cond_broadcast(get_not_empty_condition());
pthread_mutex_unlock(get_queue_mutex());
pthread_mutex_lock(worker_args->mutex);
worker_args->exit_code = EXIT_FAILURE;
pthread_mutex_unlock(worker_args->mutex);
}
void *work_copy(void *arg)
{
DIR *dir;
Job *job;
Tuple t = {0};
struct dirent *d;
struct stat st_source, st_dest;
bool dir_name_provided = false;
WorkerArgs *worker_args = (WorkerArgs *)arg;
Arena *arena = worker_args->arena;
while ((job = (Job *)dequeue()) != NULL)
{
char *source = job->source;
char *destination = job->destination;
if (lstat(source, &st_source) != 0)
{
fprintf(stderr, "Error: Invalid source: %s\n", source);
fail_all_tasks(worker_args);
return NULL;
}
if (lstat(destination, &st_dest) != 0)
{
parent_path_from_child(destination, strlen(destination), &t, arena);
dir_name_provided = is_parent_dir_valid(t.parent);
if (!dir_name_provided)
{
fprintf(stderr, "Error: Invalid destination: %s\n", destination);
fail_all_tasks(worker_args);
return NULL;
}
}
bool is_source_dir = (st_source.st_mode & S_IFMT) == S_IFDIR;
bool is_dest_dir = (st_dest.st_mode & S_IFMT) == S_IFDIR;
if (is_source_dir && !is_dest_dir && !dir_name_provided)
{
fprintf(stderr, "Error: Can't copy directory to a file\n");
fail_all_tasks(worker_args);
return NULL;
}
if (strcmp(source, destination) == 0)
{
fprintf(stderr, "Error: Source and destination are identical\n");
fail_all_tasks(worker_args);
return NULL;
}
if (!is_source_dir)
{
if (check_if_parent_dir(destination))
{
parent_path_from_child(source, strlen(source), &t, arena);
if (t.child != NULL)
{
is_dest_dir = false;
destination = join_paths(destination, t.child, arena);
}
}
FILE *f_source = fopen(source, "rb");
FILE *f_dest = is_dest_dir ? fopen(join_paths(destination, source, arena), "wb") : fopen(destination, "wb");
int success = copy_file(f_source, f_dest);
if (success != EXIT_SUCCESS)
{
fail_all_tasks(worker_args);
return NULL;
}
}
else
{
char *special_dir = parse_special_dir(source, strlen(source));
if (dir_name_provided)
{
int success = mkdir(destination, 0700);
if (success != 0)
{
fprintf(stderr, "Error copying directory\n");
fail_all_tasks(worker_args);
return NULL;
}
}
else
{
if (special_dir == NULL)
{
destination = !worker_args->flag
? join_paths(destination, parent_path_from_child(source, strlen(source), &t, arena)->child, arena)
: destination;
int success = mkdir(destination, 0700);
if (success != 0 && errno != EEXIST)
{
fprintf(stderr, "Error copying directory\n");
fail_all_tasks(worker_args);
return NULL;
}
}
}
dir = opendir(source);
if (dir == NULL)
{
fprintf(stderr, "Error opening directory\n");
fail_all_tasks(worker_args);
return NULL;
}
if (!worker_args->flag)
{
pthread_mutex_lock(worker_args->mutex);
worker_args->flag = true;
pthread_mutex_unlock(worker_args->mutex);
}
while ((d = readdir(dir)) != NULL)
{
if (!check_if_parent_dir(d->d_name))
{
char *new_source = join_paths(source, d->d_name, arena);
char *new_destination = join_paths(destination, d->d_name, arena);
if (d->d_type == DT_DIR)
{
Job *j = (Job *)allocate(arena, sizeof(Job));
j->source = new_source;
j->destination = new_destination;
enqueue(j);
}
else
{
FILE *f_source = fopen(new_source, "rb");
FILE *f_dest = fopen(new_destination, "wb");
int success = copy_file(f_source, f_dest);
if (success != EXIT_SUCCESS)
{
fail_all_tasks(worker_args);
return NULL;
}
}
}
}
closedir(dir);
}
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(get_active_tasks() - 1);
// If no tasks are left, signal all threads to exit
if (get_active_tasks() == 0 && get_queue_size() == 0)
{
set_done(true);
pthread_cond_broadcast(get_not_empty_condition()); // Notify all waiting threads
}
pthread_mutex_unlock(get_queue_mutex());
}
return NULL;
}
int copy_dir_or_file_recursive(char *source, char *destination, bool flag, Arena *arena)
{
struct stat st_source, st_dest;
Tuple t = {0};
struct dirent *d;
DIR *dir;
bool dir_name_provided = false;
if (lstat(source, &st_source) != 0)
{
fprintf(stderr, "Error: Invalid source: %s\n", source);
return EXIT_FAILURE;
}
if (lstat(destination, &st_dest) != 0)
{
parent_path_from_child(destination, strlen(destination), &t, arena);
dir_name_provided = is_parent_dir_valid(t.parent);
if (!dir_name_provided)
{
fprintf(stderr, "Error: Invalid destination: %s\n", destination);
return EXIT_FAILURE;
}
}
bool is_source_dir = (st_source.st_mode & S_IFMT) == S_IFDIR;
bool is_dest_dir = (st_dest.st_mode & S_IFMT) == S_IFDIR;
if (is_source_dir && !is_dest_dir && !dir_name_provided)
{
fprintf(stderr, "Error: Can't copy directory to a file\n");
return EXIT_FAILURE;
}
if (strcmp(source, destination) == 0)
{
fprintf(stderr, "Error: Source and destination are identical\n");
return EXIT_FAILURE;
}
if (!is_source_dir)
{
FILE *f_source = fopen(source, "rb");
FILE *f_dest = is_dest_dir ? fopen(join_paths(destination, source, arena), "wb") : fopen(destination, "wb");
return copy_file(f_source, f_dest);
}
else
{
char *special_dir = parse_special_dir(source, strlen(source));
if (dir_name_provided)
{
int success = mkdir(destination, 0700);
if (success != 0)
{
fprintf(stderr, "Error copying directory\n");
return EXIT_FAILURE;
}
}
else
{
if (special_dir == NULL)
{
destination = !flag
? join_paths(destination, parent_path_from_child(source, strlen(source), &t, arena)->child, arena)
: destination;
int success = mkdir(destination, 0700);
if (success != 0 && errno != EEXIST)
{
fprintf(stderr, "Error copying directory\n");
return EXIT_FAILURE;
}
}
}
dir = opendir(source);
if (dir == NULL)
{
fprintf(stderr, "Error opening directory\n");
return EXIT_FAILURE;
}
flag = true;
while ((d = readdir(dir)) != NULL)
{
if (!check_if_parent_dir(d->d_name))
{
int status = copy_dir_or_file_recursive(join_paths(source, d->d_name, arena), join_paths(destination, d->d_name, arena), flag, arena);
if (status != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
}
}
closedir(dir);
}
return EXIT_SUCCESS;
}
int copy_dir_or_file_threaded(char *source, char *destination, Arena *arena)
{
long number_of_processors = sysconf(_SC_NPROCESSORS_ONLN);
pthread_t threads[number_of_processors];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Job j;
j.source = source;
j.destination = destination;
WorkerArgs args = {.arena = arena, .exit_code = 0, .flag = false, .mutex = &mutex};
enqueue(&j);
for (int i = 0; i < number_of_processors; i++)
{
if (pthread_create(&threads[i], NULL, work_copy, &args) != 0)
{
fprintf(stderr, "Failed to create thread %d\n", i);
return EXIT_FAILURE;
}
}
for (int i = 0; i < number_of_processors; i++)
{
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(get_queue_mutex());
pthread_cond_destroy(get_not_empty_condition());
pthread_cond_destroy(get_not_full_condition());
return args.exit_code;
}
int command_copy(Cli_args *args, Arena *arena)
{
char *source = args->source;
char *destination = args->destination;
return copy_dir_or_file_threaded(source, destination, arena);
// return copy_dir_or_file_recursive(source, destination, false, arena);
}