-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
1431 lines (1221 loc) · 39.8 KB
/
Copy pathfunctions.c
File metadata and controls
1431 lines (1221 loc) · 39.8 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 "header.h"
storage_servers storage_server_list;
FileMapping fileMappings[MAX_NUM_FILES];// Global array to store mappings
unsigned int counter; // Global counter for unique numbers
int redundantCounter;
int num_ss;
storage_servers redundantServers[3];
// function to check if the given ip and port belong to a redundant server
int isRedundantServer(char* ip, int port)
{
for (int i = 0; i < MAX_REDUNDANT_SERVERS; i++) {
if (redundantServers[i] == NULL) {
continue;
}
if (strcmp(redundantServers[i]->ss_send->ip_addr, ip) == 0
&& redundantServers[i]->ss_send->server_port == port) {
return 1;
}
}
return 0;
}
// Caching functions
Cache InitCache()
{
Cache cache;
cache.num_cache_entries = 0;
return cache;
}
storage_servers CheckCache(Cache cache, char* command, char* source_path, char* dest_path)
{
for (int i = 0; i < cache.num_cache_entries; i++) {
if (strcmp(cache.cache_store[i].command, command) == 0) {
if (strcmp(cache.cache_store[i].source_path, source_path) == 0) {
if (strcmp(cache.cache_store[i].dest_path, dest_path) == 0) {
return cache.cache_store[i].ss;
}
}
}
}
return NULL;
}
void InsertIntoCache(
Cache cache, char* command, char* source_path, char* dest_path, storage_servers ss)
{
storage_servers temp = CheckCache(cache, command, source_path, dest_path);
if (temp != NULL) {
return;
}
if (cache.num_cache_entries == CACHE_SIZE) {
for (int i = 0; i < CACHE_SIZE - 1; i++) {
strcpy(cache.cache_store[i].command, cache.cache_store[i + 1].command);
strcpy(cache.cache_store[i].source_path, cache.cache_store[i + 1].source_path);
strcpy(cache.cache_store[i].dest_path, cache.cache_store[i + 1].dest_path);
cache.cache_store[i].ss = cache.cache_store[i + 1].ss;
}
cache.num_cache_entries--;
}
strcpy(cache.cache_store[cache.num_cache_entries].command, command);
strcpy(cache.cache_store[cache.num_cache_entries].source_path, source_path);
strcpy(cache.cache_store[cache.num_cache_entries].dest_path, dest_path);
cache.cache_store[cache.num_cache_entries].ss = ss;
cache.num_cache_entries++;
}
// Tree and LL functions
Tree MakeNode(char* name)
{
Tree T = (Tree)malloc(sizeof(struct TreeNode));
strcpy(T->path, name);
T->first_child = NULL;
T->next_sibling = NULL;
T->prev_sibling = NULL;
T->parent = NULL;
return T;
}
Tree Insert(Tree parent, char* path)
{
Tree traveller = parent->first_child;
while (traveller != NULL && traveller->next_sibling != NULL) {
traveller = traveller->next_sibling;
}
char* token = strtok_r(path, "/", &path);
while (token != NULL) {
Tree new = MakeNode(token);
if (traveller == NULL) {
parent->first_child = new;
}
else {
traveller->next_sibling = new;
new->prev_sibling = traveller;
traveller = NULL;
}
new->parent = parent;
parent = new;
token = strtok_r(path, "/", &path);
}
return parent;
}
Tree Search_Till_Parent(Tree T, char* path, int insert)// insert == 1 => you can create a new path
{
Tree parent = T;
Tree traveller = parent->first_child;
char so_far[MAX_FILE_PATH] = {'\0'};
char* path_duplicate = (char*)malloc(MAX_FILE_PATH * sizeof(char));
char* path_duplicate2 = (char*)malloc(MAX_FILE_PATH * sizeof(char));
for (int i = 0; i < MAX_FILE_PATH; i++) {
path_duplicate[i] = '\0';
path_duplicate2[i] = '\0';
}
strcpy(path_duplicate, path);
strcpy(path_duplicate2, path);
char* token = strtok_r(path_duplicate, "/", &path_duplicate);
while (token != NULL) {
while (traveller != NULL) {
if (strcmp(traveller->path, token) == 0) {
break;
}
traveller = traveller->next_sibling;
}
if (traveller == NULL)// if we reach the end of linked list and do not reach a match
{
// break the string and send remaining part to function
// send parent also to function
int count = 0;
for (int i = strlen(so_far) + 1; i < strlen(path_duplicate2); i++) {
if (path_duplicate2[i] == '/') {
count++;
}
}
if (count >= 1) {
printf(RED "Path not found\n" RESET);
return NULL;
}
if (insert == 1) {
parent = Insert(parent, path_duplicate2 + strlen(so_far));
return T;
}
else {
return NULL;
}
}
else {
parent = traveller;
traveller = traveller->first_child;
}
strcat(so_far, token);
strcat(so_far, "/");
token = strtok_r(NULL, "/", &path_duplicate);
}
return parent;
}
void PrintTree(Tree T)
{
if (T == NULL) {
return;
}
printf("%s\n", T->path);
PrintTree(T->first_child);
PrintTree(T->next_sibling);
}
void Del_Rec(Tree T)
{
if (T == NULL) {
return;
}
Del_Rec(T->first_child);
Del_Rec(T->next_sibling);
free(T);
}
int Delete_Path(Tree T, char* path, char* ss_dir)
{
if (T == NULL) {
return -1;
}
// char line[MAX_FILE_PATH] = {'\0'};
// strcpy(line, path);
// memmove(line, line + strlen(ss_dir), strlen(line) - strlen(ss_dir) + 1);
Tree traveller = Search_Till_Parent(T, path, 0);
printf("line: %s\n", traveller->path);
if (traveller == NULL) {
printf(RED "Path not found\n" RESET);
return -1;
}
if (traveller->parent == NULL) {
printf(RED "Cannot delete root\n" RESET);
return -1;
}
if (traveller->parent->first_child == traveller) {
traveller->parent->first_child = traveller->next_sibling;
}
if (traveller->prev_sibling != NULL) {
traveller->prev_sibling->next_sibling = traveller->next_sibling;// diconnected the dir now
}
Del_Rec(traveller->first_child);
free(traveller);
return 0;
}
storage_servers check_if_path_in_ss(
char* file_path,
int insert)// NULL if not found else returns the parent depending on value of insert
{
storage_servers traveller = storage_server_list;
while (traveller != NULL) {
Tree parent = Search_Till_Parent(traveller->files_and_dirs, file_path, insert);
if (parent != NULL) {
return traveller;
}
traveller = traveller->next;
}
return NULL;
}
int isSuffix(const char* mainString, const char* suffix)
{
// Use strstr to find the suffix in the main string
char* ptr = strstr(mainString, suffix);
// Check if the pointer is not NULL and points to the end of the main string
return (ptr != NULL && *(ptr + strlen(suffix)) == '\0');
}
// Searches for ss and returns the ss_send struct
// Also adds the new child thingy
storage_servers find_ss(char* file_path)
{
// char temp[MAX_FILE_PATH] = {'\0'};
// strcpy(temp, file_path);
char path_to_add[MAX_FILE_PATH] = {'\0'};
get_full_path(file_path, path_to_add);
char* token = strtok_r(file_path, "/", &file_path);
printf("token: %s\n", token);
storage_servers traveller = storage_server_list;
while (traveller != NULL) {
if (isSuffix(traveller->files_and_dirs->path, token) == 1) {
// char *path_to_append = strtok_r(NULL, "/", &file_path);
// printf("path to append: %s\n", path_to_append);
Search_Till_Parent(traveller->files_and_dirs, path_to_add, 1);
return traveller;
}
traveller = traveller->next;
}
return NULL;
}
storage_servers MakeNode_ss(char* ip_addr, int client_port, int server_port, char* init_path)
{
storage_servers new = (storage_servers)malloc(sizeof(ss));
new->ss_send = (ss_send*)malloc(sizeof(ss_send));
strcpy(new->ss_send->ip_addr, ip_addr);
new->ss_send->client_port = client_port;
new->ss_send->server_port = server_port;
new->files_and_dirs = MakeNode(init_path);
new->next = NULL;
return new;
}
void PrintAll()
{
storage_servers traveller = storage_server_list;
while (traveller != NULL) {
printf("IP: %s\n", traveller->ss_send->ip_addr);
printf("Client Port: %d\n", traveller->ss_send->client_port);
printf("Server Port: %d\n", traveller->ss_send->server_port);
printf("Path of ss: %s\n", traveller->files_and_dirs->path);
printf("Files and Directories:\n");
PrintTree(traveller->files_and_dirs);
printf("\n");
traveller = traveller->next;
}
}
// Path file functions
int Add_to_path_file(char* file_path, char* storage_file)
{
FILE* file = fopen(storage_file, "a");
if (file == NULL) {
perror("Error opening the file");
return -1;
}
fprintf(file, "%s\n", file_path);
fclose(file);
return 0;
}
int Delete_from_path_file(char* file_path, char* storage_file)
{
FILE* file = fopen(storage_file, "r");
if (file == NULL) {
perror("Error opening the file");
return -1;
}
FILE* temp_file = fopen("dollar.txt", "w");
if (temp_file == NULL) {
perror("Error opening the file");
return -1;
}
char buffer[MAX_FILE_PATH];
char prefix_path[MAX_FILE_PATH] = {'\0'};
strcpy(prefix_path, file_path);
strcat(prefix_path, "/");
printf("path: %s\n", file_path);
while (fgets(buffer, MAX_FILE_PATH, file) != NULL) {
if (buffer[strlen(buffer) - 1] == '\n') {
buffer[strlen(buffer) - 1] = '\0';
}
if (strcmp(buffer, file_path) != 0
&& strncmp(buffer, prefix_path, strlen(prefix_path)) != 0) {
fprintf(temp_file, "%s", buffer);
fprintf(temp_file, "\n");
}
}
fclose(file);
fclose(temp_file);
remove(storage_file);
rename("dollar.txt", storage_file);
return 0;
}
void load_SS(Tree T, char* file_name, char* ss_dir)
{
char line[MAX_FILE_PATH];
FILE* file = fopen(file_name, "r");
if (file == NULL) {
perror("Error opening the file");
return;
}
while (fgets(line, sizeof(line), file) != NULL) {
// Remove the newline character (if it exists)
size_t len = strcspn(line, "\n");
if (line[len] == '\n') {
line[len] = '\0';// Replace newline with null-terminator
}
T = Search_Till_Parent(T, line, 1);
}
fclose(file);
}
// Creation and Deletion of files and folders
void get_path_details(char* path_to_go_to, char* file_name, char* file_path)
{
// Extracting the index of the start of the file name - the part after the last '\'
int i = 0;
for (i = strlen(file_path) - 1; i >= 0; i--) {
if (file_path[i] == '/') {
break;
}
}
int j;
int ind = 0;
// Getting the file name
for (j = i + 1; j < strlen(file_path); j++) {
file_name[ind] = file_path[j];
ind++;
}
file_name[ind] = '\0';
// Getting the path
for (j = 0; j < i; j++) {
path_to_go_to[j] = file_path[j];
}
path_to_go_to[j] = '\0';
}
int create_file(char* file_path)
{
// Finding the directory we need to change to
// char *path_to_go_to = (char *)malloc(sizeof(char) * MAX_FILE_PATH);
// char *file_name = (char *)malloc(sizeof(char) * MAX_FILE_NAME);
// char current_dir[MAX_FILE_PATH];
// // Getting the current directory
// if (getcwd(current_dir, sizeof(current_dir)) == NULL)
// {
// perror(RED "[-]getcwd" RESET);
// return -1;
// }
// get_path_details(path_to_go_to, file_name, file_path);
// if (chdir(path_to_go_to) == -1)
// {
// perror(RED "[-]chdir" RESET);
// return -1;
// }
// Creating a new file
FILE* file = fopen(file_path, "r");
if (file != NULL) {
printf(RED "[-]File already exists\n" RESET);
return -1;
}
file = fopen(file_path, "w");
if (file == NULL) {
perror(RED "[-]fopen" RESET);
return -1;
}
else {
// printf("File Created Successfully!\n");
}
fclose(file);
// if (chdir(current_dir) == -1)
// {
// perror(RED "[-]chdir" RESET);
// return -1;
// }
return 0;
}
int create_directory(char* file_path)
{
// Finding the directory we need to change to
char* path_to_go_to = (char*)malloc(sizeof(char) * MAX_FILE_PATH);
char* directory_name = (char*)malloc(sizeof(char) * MAX_FILE_NAME);
char current_dir[MAX_FILE_PATH];
// Getting the current directory
if (getcwd(current_dir, sizeof(current_dir)) == NULL) {
perror(RED "getcwd" RESET);
return -1;
}
get_path_details(path_to_go_to, directory_name, file_path);
if (chdir(path_to_go_to) == -1) {
perror(RED "chdir" RESET);
return -1;
}
// Creating the directory
if (mkdir(directory_name, 0777) == 0) {
// printf("Directory created successfully!\n");
}
else {
perror(RED "mkdir" RESET);
return -1;
}
if (chdir(current_dir) == -1) {
perror(RED "chdir" RESET);
return -1;
}
return 0;
}
int delete_file(char* file_path)
{
// Finding the directory we need to change to
char* path_to_go_to = (char*)malloc(sizeof(char) * MAX_FILE_PATH);
char* file_name = (char*)malloc(sizeof(char) * MAX_FILE_NAME);
char current_dir[MAX_FILE_PATH];
// Getting the current directory
// if (getcwd(current_dir, sizeof(current_dir)) == NULL)
// {
// perror(RED "getcwd" RESET);
// return -1;
// }
// get_path_details(path_to_go_to, file_name, file_path);
// if (chdir(path_to_go_to) == -1)
// {
// perror(RED "chdir" RESET);
// return -1;
// }
// Deleting the file
// printf("PATH: %s\n",file_path);
if (remove(file_path) != 0) {
perror(RED "remove" RESET);
return -1;
}
// if (chdir(current_dir) == -1)
// {
// perror(RED "chdir" RESET);
// return -1;
// }
// printf("File Deleted Successfully!\n");
}
int delete_directory(char* file_path)
{
// Finding the directory we need to change to
// char *path_to_go_to = (char *)malloc(sizeof(char) * MAX_FILE_PATH);
// char *directory_name = (char *)malloc(sizeof(char) * MAX_FILE_NAME);
// char current_dir[MAX_FILE_PATH];
// // Getting the current directory
// if (getcwd(current_dir, sizeof(current_dir)) == NULL)
// {
// perror(RED "[-]getcwd" RESET);
// return -1;
// }
// get_path_details(path_to_go_to, directory_name, file_path);
// if (chdir(path_to_go_to) == -1)
// {
// perror(RED "chdir" RESET);
// return -1;
// }
// Deleting the directory
if (rmdir(file_path) != 0) {
if (delete_non_empty_dir(file_path) == -1) {
return -1;
}
// perror(RED "rmdir" RESET);
}
// if (chdir(current_dir) == -1)
// {
// perror(RED "[-]chdir" RESET);
// return -1;
// }
// printf("Directory Deleted Successfully!\n");
return 0;
}
int delete_non_empty_dir(char* directory_name)
{
DIR* dir;
struct dirent* entry;
dir = opendir(directory_name);
if (!dir) {
perror(RED "[-]opendir" RESET);
return -1;
}
while ((entry = readdir(dir))) {
if (entry->d_type != DT_DIR) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char path[MAX_FILE_PATH];
snprintf(path, sizeof(path), "%s/%s", directory_name, entry->d_name);
if (remove(path) != 0) {
perror(RED "remove" RESET);
closedir(dir);
return -1;
}
}
}
else {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char path[MAX_FILE_PATH];
snprintf(path, sizeof(path), "%s/%s", directory_name, entry->d_name);
delete_non_empty_dir(path);
}
}
}
closedir(dir);
if (rmdir(directory_name) != 0) {
perror(RED "rmdir" RESET);
return -1;
}
return 0;
}
// socket functions
void close_socket(int* client_sock)
{
if (close(*client_sock) == -1) {
perror(RED "[-]Close error" RESET);
exit(1);
}
else {
printf("[+]Client disconnected.\n\n");
}
}
void listen_for_client(int* server_sock,
int* client_sock,
struct sockaddr_in* client_addr,
socklen_t* addr_size)
{
if (listen(*server_sock, 5) == -1) {
perror(RED "[-]Listen error" RESET);
exit(1);
}
printf("Listening...\n");
*addr_size = sizeof(*client_addr);
*client_sock = accept(*server_sock, (struct sockaddr*)client_addr, addr_size);
if (*client_sock == -1) {
perror(RED "[-]Accept error" RESET);
exit(1);
}
else {
printf("[+]Client connected.\n");
}
}
void connect_to_naming_server(char* ip, int* sock, struct sockaddr_in* addr)
{
*sock = socket(AF_INET, SOCK_STREAM, 0);
if (*sock < 0) {
perror(RED "[-]Socket error" RESET);
exit(1);
}
printf("[+]TCP server socket created.\n");
memset(addr, '\0', sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_port = PORT;
addr->sin_addr.s_addr = inet_addr(ip);// converts the string to an acceptable form
if (connect(*sock, (struct sockaddr*)addr, sizeof(*addr)) == -1) {
printf(RED "[-]Connect error" RESET);
exit(1);
}
else {
printf("[+]Connected to the naming server.\n");
}
}
void open_naming_server_port(int port_number, int* server_sock, struct sockaddr_in* server_addr)
{
char* ip = IP_NM;
int n;
*server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (*server_sock < 0) {
perror(RED "[-]Socket error" RESET);
exit(1);
}
printf("[+]TCP server socket created.\n");
// Set the SO_REUSEADDR option to avoid "Address already in use" error
int yes = 1;
if (setsockopt(*server_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
perror("setsockopt");
exit(1);
}
memset(server_addr, '\0', sizeof(*server_addr));
server_addr->sin_family = AF_INET;
server_addr->sin_port = PORT;
server_addr->sin_addr.s_addr = inet_addr(ip);
n = bind(*server_sock, (struct sockaddr*)server_addr, sizeof(*server_addr));
if (n < 0) {
perror(RED "[-]Bind error" RESET);
exit(1);
}
printf("[+]Bind to the port number: %d\n", PORT);
if (listen(*server_sock, 5) == -1) {
perror(RED "[-]Listen error" RESET);
exit(1);
}
else {
printf("[+]Listening...\n");
}
}
void connect_to_SS_from_NS(int* ns_sock, struct sockaddr_in* ns_addr, int port_num)
{
// Create the socket for the naming server
*ns_sock = socket(AF_INET, SOCK_STREAM, 0);
if (*ns_sock == -1) {
perror("[-]Socket error");
exit(1);
}
// Set up the address structure for the naming server
ns_addr->sin_family = AF_INET;
ns_addr->sin_port = htons(port_num);// Replace with your naming server's port number
ns_addr->sin_addr.s_addr = inet_addr(IP_NM);
// Connect to the storage server
if (connect(*ns_sock, (struct sockaddr*)ns_addr, sizeof(*ns_addr)) == -1) {
perror(RED "[-]Connect error" RESET);
exit(1);
}
printf("[+]Connected to Storage Server.\n");
}
void connect_to_SS_from_client(int* sock, struct sockaddr_in* addr, char* ns_ip, int ns_port)
{
// Create the socket
*sock = socket(AF_INET, SOCK_STREAM, 0);
if (*sock == -1) {
perror("[-]Socket error");
return;
}
// Set up the address structure
addr->sin_family = AF_INET;
addr->sin_port = htons(ns_port);
addr->sin_addr.s_addr = inet_addr(ns_ip);
printf("port: %d\n", ns_port);
// Connect to the naming server
if (connect(*sock, (struct sockaddr*)addr, sizeof(*addr)) == -1) {
perror(RED "[-]Connect error" RESET);
exit(1);
}
else {
printf("[+]Connected to Storage Server.\n");
}
return;
}
int initialize_SS(int* ss_sock)
{
int client_port;
int server_port;
char buffer_recv[MAX_NUM_PATHS + 20] = {'\0'};
int size;
if (recv(*ss_sock, &client_port, sizeof(client_port), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
return -1;
}
if (recv(*ss_sock, &server_port, sizeof(server_port), 0) == -1) {
printf(RED "[-]Receive error\n" RESET);
return -1;
}
if ((size = recv(*ss_sock, buffer_recv, sizeof(buffer_recv), 0)) == -1) {
perror(RED "[-]Receive error\n" RESET);
return -1;
}
buffer_recv[size] = '\0';
char path_of_ss[MAX_FILE_PATH];
char* to_tokenise = (char*)malloc(sizeof(char) * (MAX_NUM_PATHS + 20));
strcpy(to_tokenise, buffer_recv);
char buffer[MAX_NUM_PATHS] = {'\0'};
char* token = strtok_r(to_tokenise, ";", &to_tokenise);
strcpy(buffer, token);
token = strtok_r(NULL, ";", &to_tokenise);
char ip[20] = {'\0'};
strcpy(ip, token);
token = strtok_r(NULL, ";", &to_tokenise);
strcpy(path_of_ss, token);
// printf("pathhhhhh: %s\n", path_of_ss);
// path_of_ss[sizeof(token)] = '\0';
storage_servers vital_info =
MakeNode_ss(ip, client_port, server_port, path_of_ss);// Just making a temp node
// vital_info->ss_send->client_port = client_port;
// vital_info->ss_send->server_port = server_port;
// strcpy(vital_info->ss_send->ss_directory, path_of_ss);
vital_info->ss_send->ss_directory[sizeof(path_of_ss)] = '\0';
// strcpy(vital_info->ss_send->ip_addr, ip);
{
printf("Port for client: %d\n", vital_info->ss_send->client_port);
printf("Port for NM: %d\n", vital_info->ss_send->server_port);
printf("IP: %s\n", vital_info->ss_send->ip_addr);
printf("Paths: %s\n", buffer);
}
FILE* file = fopen("namethatshallnotbeused.txt", "w");
if (file == NULL) {
perror("[-] File opening error");
}
fputs(buffer, file);
fclose(file);
int num_storage_servers = 1;
load_SS(vital_info->files_and_dirs, "namethatshallnotbeused.txt", path_of_ss);
vital_info->next = storage_server_list;
storage_server_list = vital_info;
remove("namethatshallnotbeused.txt");
// assign the current server as redundant server if redundant counter is less than 3
if (redundantCounter < MAX_REDUNDANT_SERVERS) {
redundantServers[redundantCounter] = vital_info;
redundantCounter++;
}
for (int i = 0; i < MAX_REDUNDANT_SERVERS && num_ss > 2; i++) {
if (isRedundantServer(vital_info->ss_send->ip_addr, vital_info->ss_send->server_port)) {
continue;
}
// if null continue
if (redundantServers[i] == NULL) {
continue;
}
struct path_details* path_details =
readPathfile(vital_info->ss_send->ip_addr, vital_info->ss_send->server_port);
copy_files_to_SS(path_details,
redundantServers[i]->ss_send->ip_addr,
redundantServers[i]->ss_send->server_port);
}
return 0;
}
void make_socket_non_blocking(int socket)
{
int flags = fcntl(socket, F_GETFL, 0);
if (flags == -1) {
perror(RED "[-]Failed to get socket flags" RESET);
exit(1);
}
if (fcntl(socket, F_SETFL, flags | O_NONBLOCK) == -1) {
perror(RED "[-]Failed to set socket to non-blocking mode" RESET);
exit(1);
}
}
void MakeSSsend_vital(
int* naming_server_sock, char* ip, int* port_for_client, int* port_for_nm, char* paths_file)
{
FILE* file = fopen(paths_file, "r");
if (file == NULL) {
perror(RED "[-] File opening error" RESET);
exit(1);
}
char buffer[MAX_NUM_PATHS] = {'\0'};
size_t len;
if ((len = fread(buffer, sizeof(buffer), 1, file)) == -1) {
perror(RED "[-] File reading error" RESET);
exit(1);
}
printf("Paths: %s\n", buffer);
if (send(*naming_server_sock, port_for_client, sizeof(*port_for_client), 0) == -1) {
perror(RED "[-]Error sending data" RESET);
exit(1);
}
if (send(*naming_server_sock, port_for_nm, sizeof(*port_for_nm), 0) == -1) {
perror(RED "[-]Error sending data" RESET);
exit(1);
}
char current_dir[MAX_FILE_PATH];
if (getcwd(current_dir, sizeof(current_dir)) == NULL) {
perror("[-] getcwd error");
exit(1);
}
// printf("curr %s\n", current_dir);
char final_send[MAX_NUM_PATHS + 20 + MAX_FILE_PATH] = {'\0'};
strcpy(final_send, buffer);
strcat(final_send, ";");
strcat(final_send, ip);
strcat(final_send, ";");
strcat(final_send, current_dir);
strcat(final_send, "\0");
if (send(*naming_server_sock, final_send, sizeof(final_send), 0) == -1) {
perror(RED "[-]Error sending data" RESET);
exit(1);
}
}
void init_port_create_sock(int* sock, struct sockaddr_in* addr, const char* ip, int port_num)
{
// Create the socket
*sock = socket(AF_INET, SOCK_STREAM, 0);
if (*sock == -1) {
perror(RED "[-]Socket error" RESET);
return;
}
// Set up the address structure
addr->sin_family = AF_INET;
addr->sin_port = htons(port_num);
addr->sin_addr.s_addr = inet_addr(ip);// Convert IP address to the proper format
// Bind the socket to the address
if (bind(*sock, (struct sockaddr*)addr, sizeof(*addr)) == -1) {
perror(RED "[-]Bind error" RESET);
exit(1);
}
// Start listening
if (listen(*sock, 5) == -1) {
perror(RED "[-]Listen error" RESET);
exit(1);
}
else {
printf("[+]Listening...\n");
}
return;
}
int copy_file_for_dir(char* source_path, char* dest_path)
{
char ch;
FILE* sourceFile = fopen(source_path, "r");
if (sourceFile == NULL) {
perror(RED "Error opening source file" RESET);
return 0;
}
FILE* destinationFile = fopen(dest_path, "w");
if (destinationFile == NULL) {
perror(RED "Error opening destination file" RESET);
fclose(sourceFile);
return 0;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
// printf(GREEN "File copied successfully!\n" RESET);
fclose(sourceFile);
fclose(destinationFile);
return 1;
}
int copy_file(char* source_path, char* dest_path, char* buffer)
{
// printf("uysg\n");
char ch;
// getting the file name from source_path
char temp[1000];
int temp_ind = 0;
int i = 0;
char temp2[1000];
int temp_ind2 = 0;
for (i = strlen(source_path) - 1; i >= 0; i--) {
if (source_path[i] == '/') {
i++;
break;
}
}
for (int j = i; j < strlen(source_path); j++) {
temp[temp_ind] = source_path[j];
temp_ind++;
}
temp[temp_ind] = '\0';
// Getting path from dest_path
for (i = strlen(dest_path) - 1; i >= 0; i--) {
if (dest_path[i] == '/') {
i++;
break;
}
}