-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnm_functions.c
More file actions
1313 lines (1165 loc) · 45.2 KB
/
Copy pathnm_functions.c
File metadata and controls
1313 lines (1165 loc) · 45.2 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "headers.h"
#include "nm.h"
extern trie *root;
extern lru_head *head;
extern ss_info *array_of_ss_info;
char result_strings[MAX_STRINGS][MAX_STRING_LENGTH];
int result_count;
// receive action from client
// for read: read filename
// for write: write filename and then after receiving ack write data
// for retrieve information: retrieve filename
// for create: create file filepath
// for delete: delete filename with path
// for copy file: copy filepath to newfilepath
// for create folder: create folder folderpath
// for delete folder: delete folder folderpath
// for copy folder: copy folder folderpath to newfolderpath
int what_to_do(char *input, int nm_sock_for_client)
{
printf("task of nm server: %s\n", input);
if ((strncmp(input, "read", strlen("read")) == 0) || (strncmp(input, "retrieve", strlen("retrieve")) == 0))
{
read_or_retrieve_file(input, nm_sock_for_client);
return 1;
}
else if (strncmp(input, "write", strlen("write")) == 0)
{
// command: write filepath
char temp[1024];
strcpy(temp, input);
char *file_path = strtok(temp, " ");
file_path = strtok(NULL, " ");
// Check if the file is in the LRU cache
lru_node *node_in_cache = find_and_return(file_path, head);
// File not found in cache
if (node_in_cache == NULL)
{
// printf("not in cache\n");
// Search in trie
int ss_num = search(root, file_path);
// printf("ss_num: %d\n", ss_num);
if (ss_num == 0)
{
// If file not found in trie, send -1 as acknowledgment to the client
printf("file not found!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else
{
// printf("found in trie\n");
// File found in trie, send storage server details to the client
char send_details_to_client[BUF_SIZE];
int port = array_of_ss_info[ss_num].ss_client_port;
strcpy(send_details_to_client, array_of_ss_info[ss_num].ss_ip);
// Concatenate IP and port as a string separated by space
char port_as_string[100];
sprintf(port_as_string, " %d", port);
strcat(send_details_to_client, port_as_string);
rwlock_t *rwlock_t = find_rwlock(root, file_path);
acquire_writelock(rwlock_t);
printf("write lock acquired\n");
printf("ss details sent to the client: %s#\n", send_details_to_client);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
// receive ack from client
char ack[BUF_SIZE];
if (recv(nm_sock_for_client, ack, sizeof(ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
release_writelock(rwlock_t);
printf("write lock released\n");
}
}
// File found in the LRU cache
else
{
// Send storage server details to the client
char send_details_to_client[BUF_SIZE];
int port = node_in_cache->storage_server_port_for_client;
char ss_ip[21];
strcpy(ss_ip, node_in_cache->storage_server_ip);
// Concatenate IP and port as a string separated by space
sprintf(send_details_to_client, "%s %d", ss_ip, port);
printf("ss details sent to the client: %s#\n", send_details_to_client);
rwlock_t *rwlock_t = find_rwlock(root, file_path);
acquire_writelock(rwlock_t);
printf("write lock acquired\n");
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
// receive ack from client
char ack[BUF_SIZE];
if (recv(nm_sock_for_client, ack, sizeof(ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
release_writelock(rwlock_t);
printf("write lock released\n");
}
return 1;
}
else if (strncmp(input, "create_file", strlen("create_file")) == 0)
{
// if file already exists send ack as -1
// choose ss number by comparing string by removing file name
// make connection with storage server and send action
// receive ack from storage server
// add to trie the file and storage server number
// add to lru cache
// command = create_file filepath
// Get the file path from the command
char temp[1024];
strcpy(temp, input);
char *file_path = strtok(temp, " ");
file_path = strtok(NULL, " ");
// Checking if the file already exists in the trie
if (search(root, file_path) > 0)
{
// Send -1 acknowledgment to the client
printf("file already exists!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else
{
// Extract directory path
char dir_path[1024];
strcpy(dir_path, file_path);
char *remove_file_name = strrchr(dir_path, '/');
if (remove_file_name != NULL)
*remove_file_name = '\0';
// Find Storage Server number
int ss_num = search(root, dir_path);
// Handle directory not found
if (ss_num == 0)
{
printf("directory not found!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
// Retrieve Storage Server information
int ss_client_port = array_of_ss_info[ss_num].ss_client_port;
int ss_nm_port = array_of_ss_info[ss_num].ss_nm_port;
char *ss_ip = array_of_ss_info[ss_num].ss_ip;
// Make a connection with the Storage Server
int sock2;
struct sockaddr_in serv_addr2;
char buffer[1024];
// Create socket
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 == -1)
{
perror("socket() error");
exit(1);
}
memset(&serv_addr2, 0, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET;
serv_addr2.sin_addr.s_addr = inet_addr(ss_ip);
serv_addr2.sin_port = htons(ss_nm_port);
// Connect to Storage Server
if (connect(sock2, (struct sockaddr *)&serv_addr2, sizeof(serv_addr2)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Connected to Storage Server %d\n", ss_num);
// Send input to Storage Server
if (send(sock2, input, strlen(input), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive Ack from Storage Server
int ack;
char buffer_ack[BUF_SIZE];
if (recv(sock2, buffer_ack, sizeof(buffer_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Convert received acknowledgment to an integer
sscanf(buffer_ack, "%d", &ack);
close(sock2);
// Send the same acknowledgment to the client
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", ack);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
if (ack >= 0)
{
// Insert into trie
insert(root, file_path, ss_num);
// Insert into LRU
// lru_node *new_lru_node = make_lru_node(file_path, ss_num, ss_client_port, ss_ip);
// insert_at_front(new_lru_node, head);
printf("Create File done\n");
}
else
{
perror("[-]File create error");
// exit(1);
close(sock2);
return -1;
}
// Send Ack to client that opeartion is successfull
// close(sock2);
do_backup_file(ss_num,input);
}
return 1;
}
else if (strncmp(input, "delete_file", strlen("delete_file")) == 0)
{
// search filepath in the trie
// retireve ss_num and delete from ss_info and receive ack from ss
// if found delete from trie
// if present in lru delete from there
// send ack to client
// command = delete_file filepath
// Get the file path from the command
char temp[1024];
strcpy(temp, input);
char *file_path = strtok(temp, " ");
file_path = strtok(NULL, " ");
int ss_num = search(root, file_path);
if (ss_num <= 0)
{
printf("file not found!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else if (ss_num > 0)
{
// Retrieve Storage Server information
int ss_client_port = array_of_ss_info[ss_num].ss_client_port;
int ss_nm_port = array_of_ss_info[ss_num].ss_nm_port;
char *ss_ip = array_of_ss_info[ss_num].ss_ip;
// Make a connection with the Storage Server
int sock2;
struct sockaddr_in serv_addr2;
char buffer[1024];
// Create socket
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 == -1)
{
perror("socket() error");
exit(1);
}
memset(&serv_addr2, 0, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET;
serv_addr2.sin_addr.s_addr = inet_addr(ss_ip);
serv_addr2.sin_port = htons(ss_nm_port);
// Connect to Storage Server
if (connect(sock2, (struct sockaddr *)&serv_addr2, sizeof(serv_addr2)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Connected to Storage Server %d\n", ss_num);
// Send input to Storage Server
if (send(sock2, input, strlen(input), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive Ack from Storage Server
int ack;
char buffer_ack[BUF_SIZE];
if (recv(sock2, buffer_ack, sizeof(buffer_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Convert received acknowledgment to an integer
sscanf(buffer_ack, "%d", &ack);
close(sock2);
// Send the same acknowledgment to the client
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", ack);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
if (ack >= 0)
{
printf("Deleted File successfully!\n");
// Acknowledgment received successfully: delete the file path from the trie
delete_node(root, file_path);
// Search and delete from the LRU cache
lru_node *deleted_node = delete_lru_node(file_path, head);
free(deleted_node);
}
else
{
printf("file not found!!!\n");
// perror("[-]File delete error");
// exit(1);
}
do_backup_file(ss_num,input);
close(sock2);
}
return 1;
}
else if (strncmp(input, "copy_file", strlen("copy_file")) == 0)
{
// command: copy_file old_filepath new_folderpath
char temp[1024];
strcpy(temp, input);
char *old_filepath;
char *new_folderpath;
// Tokenize the input to extract old_filepath and new_folderpath
old_filepath = strtok(temp, " ");
old_filepath = strtok(NULL, " ");
new_folderpath = strtok(NULL, " ");
char *filename = strrchr(old_filepath, '/');
if (filename == NULL)
{
// Handle the case when there is no '/'
filename = old_filepath;
}
else
{
// Move to the next character after '/'
filename++;
}
// Concatenate filename with new_folderpath to get the new file path
char new_filepath[1024];
snprintf(new_filepath, sizeof(new_filepath), "%s/%s", new_folderpath, filename);
// Check if the file is in the LRU cache
lru_node *node_in_cache_1 = find_and_return(old_filepath, head);
lru_node *node_in_cache_2 = find_and_return(new_folderpath, head);
int ss_num_1;
int ss_num_2;
int ss_client_port_1, ss_client_port_2;
int ss_nm_port_1, ss_nm_port_2;
char *ss_ip_1;
char *ss_ip_2;
int port_1, port_2;
// char ss_ip_3[21];
// char ss_ip_4[21];
if (node_in_cache_1 == NULL)
{
// printf("not in cache\n");
// Search in trie
ss_num_1 = search(root, old_filepath);
// printf("ss_num_1: %d\n", ss_num_1);
if (ss_num_1 == 0)
{
// If file not found in trie, send -1 as acknowledgment to the client
printf("File path not found\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else
{
// printf("found in trie\n");
// File found in trie
// Retrieve Storage Server information
ss_client_port_1 = array_of_ss_info[ss_num_1].ss_client_port;
ss_nm_port_1 = array_of_ss_info[ss_num_1].ss_nm_port;
ss_ip_1 = array_of_ss_info[ss_num_1].ss_ip;
}
}
// File found in the LRU cache
else
{
port_1 = node_in_cache_1->storage_server_port_for_client;
strcpy(ss_ip_1, node_in_cache_1->storage_server_ip);
}
if (node_in_cache_2 == NULL)
{
// printf("not in cache\n");
// Search in trie
ss_num_2 = search(root, new_folderpath);
// printf("ss_num_2: %d\n", ss_num_2);
if (ss_num_2 == 0)
{
// If file not found in trie, send -1 as acknowledgment to the client
printf("Folder path (Destination) not found\n");
char send_details_to_client_1[BUF_SIZE];
sprintf(send_details_to_client_1, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client_1, strlen(send_details_to_client_1), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else
{
// printf("found in trie\n");
// File found in trie
// Retrieve Storage Server information
ss_client_port_2 = array_of_ss_info[ss_num_2].ss_client_port;
ss_nm_port_2 = array_of_ss_info[ss_num_2].ss_nm_port;
ss_ip_2 = array_of_ss_info[ss_num_2].ss_ip;
}
}
// File found in the LRU cache
else
{
// Retrieve Storage Server information
port_2 = node_in_cache_2->storage_server_port_for_client;
strcpy(ss_ip_2, node_in_cache_2->storage_server_ip);
}
if (ss_num_1 == ss_num_2)
{
// Make a connection with the Storage Server
int sock1;
struct sockaddr_in serv_addr1;
char buffer1[BUF_SIZE];
// Create socket
sock1 = socket(AF_INET, SOCK_STREAM, 0);
if (sock1 == -1)
{
perror("socket() error");
exit(1);
}
memset(&serv_addr1, 0, sizeof(serv_addr1));
serv_addr1.sin_family = AF_INET;
serv_addr1.sin_addr.s_addr = inet_addr(ss_ip_1);
serv_addr1.sin_port = htons(ss_nm_port_1);
// Connect to Storage Server
if (connect(sock1, (struct sockaddr *)&serv_addr1, sizeof(serv_addr1)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Storage Server %d connected\n", ss_num_1);
// Send input and "same" to Storage Server
char combined_input[BUF_SIZE];
sprintf(combined_input, "%s same", input);
if (send(sock1, combined_input, strlen(combined_input), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive acknowledgment from Storage Server
int ack;
char buffer_ack[BUF_SIZE];
if (recv(sock1, buffer_ack, sizeof(buffer_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Close the connection to Storage Server
close(sock1);
// Send the same acknowledgment to the client
if (send(nm_sock_for_client, buffer_ack, strlen(buffer_ack), 0) < 0)
{
perror("send() error");
exit(1);
}
// Insert into trie
insert(root, new_filepath, ss_num_2);
// Insert into LRU
// lru_node *new_lru_node = make_lru_node(new_filepath, ss_num_2, ss_client_port_2, ss_ip_2);
// insert_at_front(new_lru_node, head);
}
else
{
printf("reached here!");
int sock_ss1, sock_ss2;
struct sockaddr_in serv_addr_ss1, serv_addr_ss2;
char buffer_ss1[BUF_SIZE], buffer_ss2[BUF_SIZE];
// Create sockets
sock_ss1 = socket(AF_INET, SOCK_STREAM, 0);
sock_ss2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock_ss1 == -1 || sock_ss2 == -1)
{
perror("socket() error");
exit(1);
}
// Setup connection details for Storage Server 1
memset(&serv_addr_ss1, 0, sizeof(serv_addr_ss1));
serv_addr_ss1.sin_family = AF_INET;
serv_addr_ss1.sin_addr.s_addr = inet_addr(ss_ip_1);
serv_addr_ss1.sin_port = htons(ss_nm_port_1);
// Setup connection details for Storage Server 2
memset(&serv_addr_ss2, 0, sizeof(serv_addr_ss2));
serv_addr_ss2.sin_family = AF_INET;
serv_addr_ss2.sin_addr.s_addr = inet_addr(ss_ip_2);
serv_addr_ss2.sin_port = htons(ss_nm_port_2);
// Connect to Storage Server 1
if (connect(sock_ss1, (struct sockaddr *)&serv_addr_ss1, sizeof(serv_addr_ss1)) == -1)
{
perror("connect() error");
exit(1);
}
// Connect to Storage Server 2
if (connect(sock_ss2, (struct sockaddr *)&serv_addr_ss2, sizeof(serv_addr_ss2)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Connected to Storage Server %d and Storage Server %d\n", ss_num_1, ss_num_2);
char input_to_ss1[BUF_SIZE];
sprintf(input_to_ss1, "%s send", input);
// Send input to Storage Server 1
if (send(sock_ss1, input_to_ss1, strlen(input_to_ss1), 0) < 0)
{
perror("send() error");
exit(1);
}
char input_to_ss2[BUF_SIZE];
sprintf(input_to_ss2, "%s receive", input);
// Send input to Storage Server 1
if (send(sock_ss2, input_to_ss2, strlen(input_to_ss2), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive "create_file filepath" command from Storage Server 1
char create_file_command_ss1[BUF_SIZE];
bzero(create_file_command_ss1, BUF_SIZE);
if (recv(sock_ss1, create_file_command_ss1, sizeof(create_file_command_ss1), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Tokenize the "create_file filepath" command and store filepath
char temp[BUF_SIZE];
strcpy(temp, create_file_command_ss1);
char *token_ss1 = strtok(temp, " ");
token_ss1 = strtok(NULL, " ");
char filepath_ss1[BUF_SIZE];
strcpy(filepath_ss1, token_ss1);
printf("filepath: %s\n", filepath_ss1);
// Insert into trie
insert(root, filepath_ss1, ss_num_2);
// Insert into LRU
// lru_node *new_lru_node = make_lru_node(filepath_ss1, ss_num_2, ss_client_port_2, ss_ip_2);
// insert_at_front(new_lru_node, head);
// Send "create_file filepath" command to Storage Server 2
// printf("NM %s\n", create_file_command_ss1);
if (send(sock_ss2, create_file_command_ss1, strlen(create_file_command_ss1), 0) < 0)
{
perror("send() error");
exit(1);
}
char acknowledge[BUF_SIZE];
bzero(acknowledge, BUF_SIZE);
if (recv(sock_ss2, acknowledge, sizeof(acknowledge), 0) < 0)
{
perror("recv() error");
exit(1);
}
if (send(sock_ss1, acknowledge, strlen(acknowledge), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive "NUM_packets" from Storage Server 1
int num_packets;
char num_packets_ss1[BUF_SIZE];
if (recv(sock_ss1, num_packets_ss1, sizeof(num_packets_ss1), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Send the received "NUM_packets" to Storage Server 2
if (send(sock_ss2, num_packets_ss1, strlen(num_packets_ss1), 0) < 0)
{
perror("send() error");
exit(1);
}
// Recv ack from SS2 and send to SS1
char num_packets_ack[BUF_SIZE];
if (recv(sock_ss2, num_packets_ack, sizeof(num_packets_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Send ack to SS1
if (send(sock_ss1, num_packets_ack, strlen(num_packets_ack), 0) < 0)
{
perror("send() error");
exit(1);
}
// printf("num_packets: %s\n", num_packets_ss1);
// Convert NUM_packets to an integer
sscanf(num_packets_ss1, "%d", &num_packets);
// printf("num_packets int: %d\n", num_packets);
// While loop to receive and send data packets
while (num_packets--)
{
// Receive data packets from Storage Server 1
char data_packet_ss1[BUF_SIZE];
bzero(data_packet_ss1, BUF_SIZE);
if (recv(sock_ss1, data_packet_ss1, sizeof(data_packet_ss1), 0) < 0)
{
perror("recv() error");
exit(1);
}
// printf("data received: %s", data_packet_ss1);
// Send data packets to Storage Server 2
if (send(sock_ss2, data_packet_ss1, strlen(data_packet_ss1), 0) < 0)
{
perror("send() error");
exit(1);
}
}
// Recv ack from SS2 and send to SS1 and client
char ack_ss2_full[BUF_SIZE];
if (recv(sock_ss2, ack_ss2_full, sizeof(ack_ss2_full), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Send ack to SS1
if (send(sock_ss1, ack_ss2_full, strlen(ack_ss2_full), 0) < 0)
{
perror("send() error");
exit(1);
}
// Send ack to the client
if (send(nm_sock_for_client, ack_ss2_full, strlen(ack_ss2_full), 0) < 0)
{
perror("send() error");
exit(1);
}
printf("Copy File Done\n");
// Close connections
// close(sock_ss1);
// close(sock_ss2);
}
// return 1;
}
else if (strncmp(input, "create_folder", strlen("create_folder")) == 0)
{
// command: create_folder folderpath
char temp[1024];
strcpy(temp, input);
char *file_path = strtok(temp, " ");
file_path = strtok(NULL, " ");
// Checking if the folder already exists in the trie
if (search(root, file_path) > 0)
{
// Send -1 acknowledgment to the client
printf("file already exists!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else
{
// Extract parent directory path
char dir_path[1024];
strcpy(dir_path, file_path);
char *remove_file_name = strrchr(dir_path, '/');
if (remove_file_name != NULL)
*remove_file_name = '\0';
// Find Storage Server number
int ss_num = search(root, dir_path);
if (ss_num == 0)
{
printf("directory not found!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
// Retrieve Storage Server information
int ss_client_port = array_of_ss_info[ss_num].ss_client_port;
int ss_nm_port = array_of_ss_info[ss_num].ss_nm_port;
char *ss_ip = array_of_ss_info[ss_num].ss_ip;
// Make a connection with the Storage Server
int sock2;
struct sockaddr_in serv_addr2;
char buffer[1024];
// Create socket
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 == -1)
{
perror("socket() error");
exit(1);
}
memset(&serv_addr2, 0, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET;
serv_addr2.sin_addr.s_addr = inet_addr(ss_ip);
serv_addr2.sin_port = htons(ss_nm_port);
// Connect to Storage Server
if (connect(sock2, (struct sockaddr *)&serv_addr2, sizeof(serv_addr2)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Connected to Storage Server %d\n", ss_num);
// Send input to Storage Server
if (send(sock2, input, strlen(input), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive Ack from Storage Server
int ack;
char buffer_ack[BUF_SIZE];
if (recv(sock2, buffer_ack, sizeof(buffer_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Convert received acknowledgment to an integer
sscanf(buffer_ack, "%d", &ack);
close(sock2);
// Send the same acknowledgment to the client
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", ack);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
if (ack >= 0)
{
// Insert into trie
insert(root, file_path, ss_num);
// Insert into LRU
// lru_node *new_lru_node = make_lru_node(file_path, ss_num, ss_client_port, ss_ip);
// insert_at_front(new_lru_node, head);
printf("Create Folder done\n");
}
else
{
perror("[-]File creation error");
exit(1);
}
do_backup_file(ss_num,input);
close(sock2);
}
return 1;
}
else if (strncmp(input, "delete_folder", strlen("delete_folder")) == 0)
{
// command: delete_folder folderpath
char temp[1024];
strcpy(temp, input);
char *file_path = strtok(temp, " ");
file_path = strtok(NULL, " ");
int ss_num = search(root, file_path);
if (ss_num <= 0)
{
printf("file not found!!!\n");
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", -1);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
return 1;
}
else if (ss_num > 0)
{
// Retrieve Storage Server information
int ss_client_port = array_of_ss_info[ss_num].ss_client_port;
int ss_nm_port = array_of_ss_info[ss_num].ss_nm_port;
char *ss_ip = array_of_ss_info[ss_num].ss_ip;
// Make a connection with the Storage Server
int sock2;
struct sockaddr_in serv_addr2;
char buffer[1024];
// Create socket
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 == -1)
{
perror("socket() error");
exit(1);
}
memset(&serv_addr2, 0, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET;
serv_addr2.sin_addr.s_addr = inet_addr(ss_ip);
serv_addr2.sin_port = htons(ss_nm_port);
// Connect to Storage Server
if (connect(sock2, (struct sockaddr *)&serv_addr2, sizeof(serv_addr2)) == -1)
{
perror("connect() error");
exit(1);
}
printf("Connected to Storage Server %d\n", ss_num);
// Send input to Storage Server
if (send(sock2, input, strlen(input), 0) < 0)
{
perror("send() error");
exit(1);
}
// Receive Ack from Storage Server
int ack;
char buffer_ack[BUF_SIZE];
if (recv(sock2, buffer_ack, sizeof(buffer_ack), 0) < 0)
{
perror("recv() error");
exit(1);
}
// Convert received acknowledgment to an integer
sscanf(buffer_ack, "%d", &ack);
close(sock2);
// Send the same acknowledgment to the client
char send_details_to_client[BUF_SIZE];
sprintf(send_details_to_client, "%d", ack);
if (send(nm_sock_for_client, send_details_to_client, strlen(send_details_to_client), 0) < 0)
{
perror("send() error");
exit(1);
}
if (ack >= 0)
{
// Acknowledgment received successfully: delete the file path from the trie
delete_node(root, file_path);
// Search and delete from the LRU cache
lru_node *deleted_node = delete_lru_node(file_path, head);
free(deleted_node);
printf("Deleted Folder successfully!\n");
}
else
{
perror("[-]File delete error");
exit(1);
}
do_backup_file(ss_num,input);
close(sock2);
}
return 1;
}
else if (strncmp(input, "copy_folder", strlen("copy_folder")) == 0)
{
// command copy_folder folderpath new_folderpath
char temp[1024];
strcpy(temp, input);
char *old_folderpath;
char *new_folderpath;
// Tokenize the input to extract old_folderpath and new_folderpath
old_folderpath = strtok(temp, " ");