-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
499 lines (432 loc) · 16.7 KB
/
Copy pathclient.c
File metadata and controls
499 lines (432 loc) · 16.7 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
#include "header.h"
int main()
{
int naming_server_sock;
struct sockaddr_in client_addr;
socklen_t addr_size;
// Create a socket and connect to the naming server
connect_to_naming_server(IP_NM, &naming_server_sock, &client_addr);
int role = 2;
if (send(naming_server_sock, &role, sizeof(role), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
// recieve num zero or num one from NS
int num;
if (recv(naming_server_sock, &num, sizeof(num), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
if (num == 0) {
printf(RED "No storage servers available\nDisconnecting..\n" RESET);
exit(1);
}
while (1) {
int option = -1;
{
printf(CYAN "\nDo you want to:\n");
printf("1. Exit from program (enter 1)\n");
printf("2. Delete a File/Directory (enter 2)\n");
printf("3. Create an Empty File/Directory (enter 3)\n");
printf("4. Copy Files/Directories (enter 4)\n");
printf("5. Write To File (enter 5)\n");
printf("6. Read File (enter 6)\n");
printf("7. Get File Info (enter 7)\n" RESET);
}
scanf("%d", &option);
if (option == 1)// Exiting from the program
{
exit(0);
}
else if (option == 2)// Deletion
{
char option_client[2] = "2";
option_client[strlen(option_client)] = '\0';
// Send option to NS
if (send(naming_server_sock, option_client, sizeof(option_client), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char path[MAX_FILE_PATH];
printf(CYAN "Enter the path: " RESET);
scanf("%s", path);
char delete_option[10];
printf(CYAN "Do you want to\n");
printf("Delete a file (Enter 1)\n");
printf("Delete a directory (Enter 2)\n" RESET);
scanf("%s", delete_option);
// Send the path to NS
if (send(naming_server_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-] Send error\n" RESET);
exit(1);
}
// Send whether you want to create a file or directory to NS
if (send(naming_server_sock, delete_option, sizeof(delete_option), 0) == -1) {
printf(RED "[-]Send error\n" RESET);
exit(1);
}
char mid_ack[100];
int ind = 0;
if ((ind = recv(naming_server_sock, mid_ack, sizeof(mid_ack), 0)) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
mid_ack[ind] = '\0';
// printf("IND: %d\n",ind);
// printf("MID: %s\n",mid_ack);
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
// receive success or error message from NS
char success[100];
if (recv(naming_server_sock, success, sizeof(success), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
if (strcmp(success, "done") == 0) {
printf(GREEN "Deleted Successfully!\n" RESET);
}
else {
printf(RED "Error deleting file/directory\n" RESET);
printf(RED "%s\n" RESET, success);
}
}
else if (option == 3)// Creation
{
char option_client[2];
strcpy(option_client, "3");
// Send option to NS
if (send(naming_server_sock, option_client, sizeof(option_client), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char path[MAX_FILE_PATH];
printf(CYAN "Enter the path: " RESET);
scanf("%s", path);
char create_option[10];
printf(CYAN "Do you want to\n");
printf("Create an empty file (Enter 1)\n");
printf("Create an empty directory (Enter 2)\n" RESET);
scanf("%s", create_option);
// Send the path to NS
if (send(naming_server_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-]Send error\n" RESET);
exit(1);
}
// Send whether you want to create a file or directory to NS
if (send(naming_server_sock, create_option, sizeof(create_option), 0) == -1) {
perror(RED "[-]Send error\n" RESET);
exit(1);
}
char mid_ack[100];
int ind = 0;
if ((ind = recv(naming_server_sock, mid_ack, sizeof(mid_ack), 0)) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
// mid_ack[ind]='\0';
// printf("IND: %d\n",ind);
// printf("MID: %s\n",mid_ack);
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
// recieve success or error message from NS
char success[100];
if ((ind = recv(naming_server_sock, success, sizeof(success), 0)) == -1) {
perror(RED "[-] Receive error\n" RESET);
exit(1);
}
// success[ind]='\0';
// printf("SUCCESS: %s\n",success);
if (strcmp(success, "done") == 0) {
printf(GREEN "Created Successfully!\n" RESET);
}
else {
printf(RED "%s\n" RESET, success);
}
}
else if (option == 4)// Copying paths
{
char option_client[2] = "4";
option_client[strlen(option_client)] = '\0';
// Send option to NS
if (send(naming_server_sock, option_client, sizeof(option_client), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char source_path[MAX_FILE_PATH];
printf(CYAN "Enter the source path: " RESET);
scanf("%s", source_path);
char dest_path[MAX_FILE_PATH];
printf(CYAN "Enter the destination path: " RESET);
scanf("%s", dest_path);
char copy_option[10];
printf(CYAN "Do you want to\n");
printf("Copy a file (Enter 1)\n");
printf("Copy a directory (Enter 2)\n" RESET);
scanf("%s", copy_option);
// Send the source path to NS
if (send(naming_server_sock, source_path, sizeof(source_path), 0) == -1) {
perror(RED "[-] Send error\n" RESET);
exit(1);
}
// Send the destination path to NS
if (send(naming_server_sock, dest_path, sizeof(dest_path), 0) == -1) {
perror(RED "[-] Send error\n" RESET);
exit(1);
}
// Send whether you want to create a file or directory to NS
if (send(naming_server_sock, copy_option, sizeof(copy_option), 0) == -1) {
printf(RED "[-]Send error\n" RESET);
exit(1);
}
char mid_ack[100];
if (recv(naming_server_sock, mid_ack, sizeof(mid_ack), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
// recieve success or error message from NS
char success[100];
if (recv(naming_server_sock, success, sizeof(success), 0) == -1) {
perror(RED "[-] Receive error\n" RESET);
exit(1);
}
if (strcmp(success, "done") == 0) {
printf(GREEN "Copied Successfully!\n" RESET);
}
else {
printf(RED "%s\n" RESET, success);
}
}
else if (option == 5)// Write
{
if (send(naming_server_sock, "5", strlen("5"), 0) == -1) {
perror("[-]Send error");
exit(1);
}
char path[MAX_FILE_PATH];
printf(CYAN "Enter the path: " RESET);
scanf("%s", path);
if (send(naming_server_sock, path, sizeof(path), 0) == -1) {
perror("[-]Send error");
exit(1);
}
char ip_addr[50];
char server_addr[50];
char mid_ack1[100];
int m_ind = 0;
if ((m_ind = recv(naming_server_sock, mid_ack1, sizeof(mid_ack1), 0)) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
mid_ack1[m_ind] = '\0';
if (strcmp(mid_ack1, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack1);
continue;
}
if ((m_ind = recv(naming_server_sock, ip_addr, sizeof(ip_addr), 0)) == -1) {
perror("[-]Send error");
exit(1);
}
ip_addr[m_ind] = '\0';
if (strcmp(ip_addr, "failed") == 0) {
printf(RED "File does not exist\n" RESET);
continue;
}
if ((m_ind = recv(naming_server_sock, server_addr, sizeof(server_addr), 0)) == -1) {
perror("[-]Send error");
exit(1);
}
server_addr[m_ind] = '\0';
int ns_sock;
struct sockaddr_in ns_addr;
connect_to_SS_from_client(&ns_sock, &ns_addr, ip_addr, atoi(server_addr));
if (send(ns_sock, path, sizeof(path), 0) == -1) {
printf("[-] Send error\n");
close_socket(&ns_sock);
return 1;// Return an error code
}
char mid_ack[100];
if (recv(ns_sock, mid_ack, sizeof(mid_ack), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
char input[1024];
printf(CYAN "Start entering data: (Ctrl + D to stop)\n\n" RESET);
while (1) {
// Check for Ctrl + D or end-of-file condition
if (scanf(" %[^\n]s", input) == EOF) {
if (feof(stdin)) {
// Ctrl + D received, clear EOF state
clearerr(stdin);
printf(CYAN "\nCtrl + D received. Stopping input.\n" RESET);
break;
}
else {
// Error condition, handle it if necessary
printf("[-] Error reading input\n");
break;
}
}
if (send(ns_sock, input, sizeof(input), 0) == -1) {
printf("[-] Send error\n");
break;
}
}
printf(CYAN "\nFinished writing to file!\n" RESET);
close_socket(&ns_sock);
}
else if (option == 6)// Read
{
if (send(naming_server_sock, "6", strlen("6"), 0) == -1) {
perror("[-]Send error");
exit(1);
}
char path[MAX_FILE_PATH];
printf(CYAN "Enter the path: " RESET);
scanf("%s", path);
if (send(naming_server_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char ip_addr[50];
char server_addr[50];
char mid_ack1[100];
if (recv(naming_server_sock, mid_ack1, sizeof(mid_ack1), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
if (strcmp(mid_ack1, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack1);
continue;
}
if (recv(naming_server_sock, ip_addr, sizeof(ip_addr), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
if (strcmp(ip_addr, "failed") == 0) {
printf(RED "File does not exist\n" RESET);
continue;
}
if (recv(naming_server_sock, server_addr, sizeof(server_addr), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
strcpy(ip_addr, IP_NM);// Fix
int ss_sock;
struct sockaddr_in ss_addr;
connect_to_SS_from_client(&ss_sock, &ss_addr, ip_addr, atoi(server_addr));
if (send(ss_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char mid_ack[100];
if (recv(ss_sock, mid_ack, sizeof(mid_ack), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
// Getting file contents
printf("\n");
char buffer[1024];
int c = 0;
while (c == 0) {
recv(ss_sock, buffer, sizeof(buffer), 0);
if (strcmp("DONE", buffer) == 0 || strcmp("DONE\n", buffer) == 0) {
c = 2;// DONE WITH FILE
printf(CYAN "\nFinished reading file!\n" RESET);
break;
}
else {
printf(PINK "%s" RESET, buffer);
}
}
printf("\n");
close_socket(&ss_sock);
}
else if (option == 7)// Permissions
{
if (send(naming_server_sock, "7", strlen("7"), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char path[MAX_FILE_PATH];
printf(CYAN "Enter the path: " RESET);
scanf("%s", path);
if (send(naming_server_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char ip_addr[50];
char server_addr[50];
char mid_ack1[100];
if (recv(naming_server_sock, mid_ack1, sizeof(mid_ack1), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
if (strcmp(mid_ack1, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack1);
continue;
}
if (recv(naming_server_sock, ip_addr, sizeof(ip_addr), 0) == -1) {
perror(RED "[-]Receive error" RESET);
exit(1);
}
if (strcmp(ip_addr, "failed") == 0) {
printf(RED "File does not exist\n" RESET);
continue;
}
if (recv(naming_server_sock, server_addr, sizeof(server_addr), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
int ns_sock;
struct sockaddr_in ns_addr;
connect_to_SS_from_client(&ns_sock, &ns_addr, ip_addr, atoi(server_addr));
if (send(ns_sock, path, sizeof(path), 0) == -1) {
perror(RED "[-]Send error" RESET);
exit(1);
}
char mid_ack[100];
if (recv(ns_sock, mid_ack, sizeof(mid_ack), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
exit(1);
}
if (strcmp(mid_ack, "success") != 0) {
printf(RED "%s\n" RESET, mid_ack);
continue;
}
char permission[1024];
if (recv(ns_sock, permission, sizeof(permission), 0) == -1) {
perror(RED "Error receiving data" RESET);
exit(0);
}
if (strcmp(permission, "failed") == 0) {
printf(RED "File does not exist\n" RESET);
continue;
}
printf("\n");
printf(YELLOW "%s" RESET, permission);
close_socket(&ns_sock);
}
else {
printf(RED "Invalid option\n" RESET);
}
}
// Close the client socket when done
close(naming_server_sock);
return 0;
}