-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_server.c
576 lines (496 loc) · 19.6 KB
/
storage_server.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
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
#include "headers.h"
#define NM_IP "127.0.0.1"
#define NM_PORT 5000
int cnt;
char current_directory[500];
typedef struct SS {
char ip[50];
int nm_port;
int client_port;
int num; // denotes the number of paths in this storage server
char path[100000];
} SS;
char* perms(const char* filepath) {
struct stat file_stat;
if (stat(filepath, &file_stat) != 0) {
perror("Error getting file information");
exit(EXIT_FAILURE);
}
static char permissions[11];
snprintf(permissions, sizeof(permissions),
"%s%s%s%s%s%s%s%s%s%s",
(S_ISDIR(file_stat.st_mode)) ? "d" : "-",
(file_stat.st_mode & S_IRUSR) ? "r" : "-",
(file_stat.st_mode & S_IWUSR) ? "w" : "-",
(file_stat.st_mode & S_IXUSR) ? "x" : "-",
(file_stat.st_mode & S_IRGRP) ? "r" : "-",
(file_stat.st_mode & S_IWGRP) ? "w" : "-",
(file_stat.st_mode & S_IXGRP) ? "x" : "-",
(file_stat.st_mode & S_IROTH) ? "r" : "-",
(file_stat.st_mode & S_IWOTH) ? "w" : "-",
(file_stat.st_mode & S_IXOTH) ? "x" : "-");
return permissions;
}
// gpt prompt: C code to get all files in a given directory, recursively of all folders.
// line 24-61
void traverse_directory(const char *dirname, const char *base, SS *here, int *path_count) {
DIR *dir;
struct dirent *ent;
struct stat st;
if ((dir = opendir(dirname)) != NULL) {
while ((ent = readdir(dir)) != NULL && *path_count < 1024) {
if (ent->d_name[0] == '.') {
continue;
}
char path[500];
snprintf(path, sizeof(path), "%s/%s", dirname, ent->d_name);
if (stat(path, &st) == 0) {
char relative_path[1024];
if (strcmp(base, ".") == 0) {
// If the base is ".", just use the file/folder name
snprintf(relative_path, sizeof(relative_path), "%s", ent->d_name);
} else {
// Otherwise, construct the relative path
snprintf(relative_path, sizeof(relative_path), "%s/%s", base, ent->d_name);
}
if (S_ISDIR(st.st_mode)) {
// Recursive call for subdirectories
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
traverse_directory(path, relative_path, here, path_count);
}
}
// Store the relative path
strcat(here->path, relative_path);
strcat(here->path, "\n");
(*path_count)++;
}
}
closedir(dir);
} else {
perror("Error reading directory");
}
}
void fill_values(SS* here){
FILE *fp = fopen("/home/sumit_kk10/Desktop/Network-File-System/count.txt", "r");
fscanf(fp, "%d", &cnt);
fclose(fp);
fp = fopen("/home/sumit_kk10/Desktop/Network-File-System/count.txt", "w"); // REPLACE THIS WITH THE ABSOULTE PATH OF THE FILE COUNT
fprintf(fp, "%d", cnt + 2);
fclose(fp);
strcpy(here->ip, NM_IP);
here->nm_port = NM_PORT + cnt;
here->client_port = NM_PORT + cnt + 1;
if (getcwd(current_directory, sizeof(current_directory)) != NULL) {
int path_count = 0;
strcat(here->path, "\n");
strcat(here->path, ".");
strcat(here->path, "\n");
traverse_directory(current_directory, ".", here, &path_count);
here->num = path_count;
printf("%d\n", path_count);
printf("%s\n", here->path);
} else
perror("Error getting current directory");
}
void connect_with_nm(SS* here){
struct sockaddr_in add;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
printf("Error creating a socket\n");
return;
}
add.sin_family = AF_INET;
add.sin_port = htons(NM_PORT);
add.sin_addr.s_addr = INADDR_ANY;
if(connect(sock, (struct sockaddr*) &add, sizeof(add)) == -1) {
printf("Error connecting to naming server\n");
close(sock);
return;
}
size_t herre = sizeof(SS);
printf("%ld\n", herre);
if (send(sock, &herre, sizeof(size_t), 0) == -1) {
perror("Error sending struct size");
close(sock);
return;
}
if(send(sock, here, herre, 0) == -1){
printf("Error in sending SS data to NM\n");
close(sock);
return;
}
char ack[100];
recv(sock, ack, 100, 0);
if(!strcmp(ack, "success"))
printf("Storage server initialized\n");
else
printf("Some error in initializing storage server\n");
close(sock);
}
void* handle_nm(void* arg){
SS* ss = (SS*) arg;
struct sockaddr_in add;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
printf("Error creating a socket\n");
return NULL;
}
add.sin_family = AF_INET;
add.sin_port = htons(ss->nm_port);
add.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr*)&add, sizeof(add)) == -1) {
perror("Error binding to naming server");
exit(1);
}
if (listen(sock, 100) == -1) {
perror("Listening failed");
exit(1);
}
// stores file contents to copy
char buffer[100000];
char source[1000];
while(1){
// now we need to recieve a request from naming server.
struct sockaddr_in temp;
// Accept incoming connection from the storage server
size_t pp = sizeof(temp);
int ss_data = accept(sock, (struct sockaddr*)&temp, (socklen_t*) &pp);
if (ss_data == -1) {
// perror("Operation couldn't get completed: Accept failed");
// exit(1);
continue;
}
int x;
if(recv(ss_data, &x, sizeof(x), 0) > 0){
// 0 -> create a file
// 1 -> create a directory
// 2 -> delete a file
// 3 -> delete a directory
// 4 -> copy a file
// 5 -> copy a directory
// 6 -> read
// 7 -> write
// 8 -> get info
printf("%d\n", x);
if(x <= 1){
char mess[200];
if(recv(ss_data, mess, 200, 0) > 0){
printf("%s\n", mess);
char* token = strtok(mess, "|");
char path[200];
strcpy(path, token);
char name[100];
token = strtok(NULL, "|");
strcpy(name, token);
char fullpath[5000];
snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", current_directory, path, name);
if(!x){
FILE *file = fopen(fullpath, "w");
if(file == NULL){
perror("Error creating file");
continue;
}
fclose(file);
printf("Empty file created: %s\n", fullpath);
}
else{
if(mkdir(fullpath, 0777) == 0)
printf("Empty directory created: %s\n", fullpath);
else{
perror("Error creating directory\n");
continue;
}
}
int y = 1;
if (send(ss_data, &y, sizeof(y), 0) == -1) {
perror("Error sending ack to NM");
continue;
}
}
else{
perror("Operation couldn't get completed: Accept failed\n");
continue;
}
}
else if(x <= 3){
char mess[200];
if(recv(ss_data, mess, 200, 0) > 0){
char path[5000];
snprintf(path, sizeof(path), "%s/%s", current_directory, mess);
if(x == 2){
if(remove(path) == 0)
printf("File deleted: %s\n", path);
else{
perror("Error deleting file\n");
continue;
}
}
else{
if(rmdir(path) == 0)
printf("Directory deleted: %s\n", path);
else{
perror("Error deleting directory\n");
continue;
}
}
int y = 1;
if (send(ss_data, &y, sizeof(y), 0) == -1) {
perror("Error sending ack to NM");
continue;
}
}
}
else if(x == 4) {
if(recv(ss_data, source, 200, 0) > 0){
char fullpath[5000];
snprintf(fullpath, sizeof(fullpath), "%s/%s", current_directory, source);
printf("fullpath: %s\n", fullpath);
FILE* file = fopen(fullpath, "r");
if (file == NULL) {
perror("Error opening file for reading");
continue;
}
size_t read_size;
// Read and send the file content in chunks
while ((read_size = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// Send the chunk size
if (send(ss_data, &read_size, sizeof(read_size), 0) == -1) {
perror("Error sending chunk size to client");
fclose(file);
break;
}
printf("chunks: %zu\n", read_size);
// Send the buffer content
if (send(ss_data, buffer, read_size, 0) == -1) {
perror("Error sending file content chunk to client");
fclose(file);
break;
}
printf("file content: %s", buffer);
// memset(buffer, 0, sizeof(buffer)); // Clear the buffer
}
// Signal end of file by sending a zero-sized chunk
read_size = 0;
if (send(ss_data, &read_size, sizeof(read_size), 0) == -1) {
perror("Error sending end of file to client");
}
fclose(file);
}
}
else if (x == 5) {
char path[1000];
if (recv(ss_data, path, 200, 0) > 0) {
// destination path
char fullpath[5000];
// Assuming 'current_directory' contains the destination path
snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", current_directory, path, source);
printf("fullpath: %s\n", fullpath);
FILE *file = fopen(fullpath, "w");
if (file == NULL) {
perror("Error creating file");
continue;
}
fseek(file, 0, SEEK_SET);
printf("Empty file created: %s\n", fullpath);
// Receive the new file contents
char new_content[10000];
memset(new_content, 0, sizeof(new_content));
size_t received_size;
// Receive and write the file content in chunks
while ((received_size = recv(ss_data, new_content, sizeof(new_content), 0)) > 0) {
// if(new_content == NULL) break;
fprintf(file, "%s", new_content);
memset(new_content, 0, sizeof(new_content));
}
if (received_size < 0) {
perror("Error receiving data");
}
fflush(file);
fclose(file);
printf("File content replaced successfully.\n");
}
}
}
}
close(sock);
return NULL;
}
// concurrency for client connections
void* processClient(void* client_data_ptr) {
int client_data = *((int*)client_data_ptr);
while(1){
// printf("Client socket connection accepted\n");
int operation;
char path[1024];
if(recv(client_data, &operation, sizeof(operation), 0) > 0){
printf("operation: %d\n", operation);
// 6 --> read a file
// 7 --> write a file
// 8 --> get file size and permissions
// Receive the file path
if (recv(client_data, path, sizeof(path), 0) == -1) {
perror("Error receiving file path from client");
continue;
}
printf("path: %s\n", path);
char fullpath[10000];
snprintf(fullpath, sizeof(fullpath), "%s/%s", current_directory, path);
printf("fullpath: %s\n", fullpath);
if (operation == 6) {
printf("OPERATION: READ\n");
// read a file
FILE* file = fopen(fullpath, "r");
if (file == NULL) {
perror("Error opening file for reading");
continue;
}
char buffer[10000];
size_t read_size;
// Read and send the file content in chunks
while ((read_size = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// Send the chunk size
if (send(client_data, &read_size, sizeof(read_size), 0) == -1) {
perror("Error sending chunk size to client");
fclose(file);
break;
}
// Send the buffer content
if (send(client_data, buffer, read_size, 0) == -1) {
perror("Error sending file content chunk to client");
fclose(file);
break;
}
memset(buffer, 0, sizeof(buffer)); // Clear the buffer
}
printf("%s\n", buffer);
// Signal end of file by sending a zero-sized chunk
read_size = 0;
if (send(client_data, &read_size, sizeof(read_size), 0) == -1) {
perror("Error sending end of file to client");
}
fclose(file);
}
else if (operation == 7){
// write a file
// Receive the new file contents
char new_content[10000];
memset(new_content, 0, sizeof (new_content));
size_t received_size;
FILE* file = fopen(fullpath, "w");
if (file == NULL) {
perror("Error opening file for writing");
continue;
}
fseek(file, 0, SEEK_SET);
// Receive and write the file content in chunks
if(recv(client_data, new_content, sizeof(new_content), 0) == 1){
fflush(file);
fclose(file);
perror("Error in reciving the data");
continue;
}
fprintf(file, "%s", new_content);
printf("\n%s", new_content);
fflush(file);
fclose(file);
printf("File content replaced successfully.\n");
}
else if (operation == 8){
// get file size
printf("GET INFO OPERATION\n");
FILE* file = fopen(fullpath, "r");
if (file == NULL) {
printf("File not found");
exit(1);
}
fseek(file, 0L, SEEK_END);
long int file_size = ftell(file);
fclose(file);
if (send(client_data, &file_size, sizeof(file_size) + 1, 0) == -1) {
perror("Error sending file size to Storage Server");
continue;
}
const char* permission_str = perms(fullpath);
size_t ack_length = strlen(permission_str) + 1;
if (send(client_data, &ack_length, sizeof(ack_length), 0) == -1) {
perror("Error sending ack length to client");
continue;
}
printf("%s\n", permission_str);
if (send(client_data, permission_str, ack_length, 0) == -1) {
perror("Error sending acknowledgement to client");
continue;
}
printf("%s\n", permission_str);
// Get file permissions
// struct stat file_stat;
// if (stat(fullpath, &file_stat) == -1) {
// perror("Error getting file information");
// continue;
// }
// uint16_t pp = (uint16_t) file_stat.st_mode;
// // Send file permissions as an integer to Storage Server
// printf("permissions: %u", pp);
// if (send(client_data, &pp, sizeof(pp), 0) == -1) {
// perror("Error sending file permissions to Storage Server");
// continue;
// }
char stop_packet[] = "STOP";
if (send(client_data, stop_packet, strlen(stop_packet) + 1, 0) == -1) {
perror("Did not send STOP packet to client\n");
continue;
}
}
}
}
close(client_data);
return NULL;
}
void* handle_client(void* arg){
SS* ss = (SS*) arg;
struct sockaddr_in add;
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if(client_socket == -1){
printf("Error creating a socket\n");
return NULL;
}
add.sin_family = AF_INET;
add.sin_port = htons(ss->client_port);
add.sin_addr.s_addr = INADDR_ANY;
if (bind(client_socket, (struct sockaddr*)&add, sizeof(add)) == -1) {
perror("Error binding to client");
exit(1);
}
if (listen(client_socket, 100) == -1) {
perror("Listening failed");
exit(1);
}
while(1){
struct sockaddr_in temp;
// Accept incoming connection from the storage server
int client_data = accept(client_socket, (struct sockaddr*)&temp, (socklen_t*)&temp);
if (client_data == -1) {
perror("Accept failed");
continue;
}
pthread_t client_thread;
pthread_create(&client_thread, NULL, (void*)processClient, (void*)&client_data);
pthread_detach(client_thread);
}
close(client_socket);
return NULL;
}
int main(){
SS ss;
fill_values(&ss);
connect_with_nm(&ss);
// we need to make two threads
// one which will listen from ss.nm_port
// one which will listen from ss.client_port
pthread_t threads[2];
pthread_create(&threads[0], NULL, handle_nm, (void*) &ss);
pthread_create(&threads[1], NULL, handle_client, (void*) &ss);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
return 0;
}