-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
837 lines (713 loc) · 24.6 KB
/
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
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
/**
* @file server.c
* @brief Implementation of server-side communication functions.
*
* This file contains the main function and various utility functions to handle
* server-side communication. It supports handling client requests such as GET,
* PUT, LIST, and DELETE, and provides necessary error handling and data processing.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "dictionary.h"
#include "vector.h"
#include "common.h"
#include <ctype.h>
#include <netdb.h>
#include "format.h"
#include <errno.h>
#define MAX_CLIENTS 1000
// Function declarations
int handle_put(int client_file_descriptor);
int handle_get(int client_file_descriptor);
int handle_delete(int client_file_descriptor);
int handle_list(int client_file_descriptor);
int handle_client(int client_file_descriptor);
void signal_handler(int signal);
int init_server_socket(char* port);
void quit_server();
void create_server(char* port);
ssize_t read_all_from_socket(int socket, char *buffer, size_t count, int is_header);
ssize_t write_all_to_socket(int socket, const char *buffer, size_t count);
// Global variables
static int epollfd;
static char* dir;
static vector* file_vector;
static dictionary* clientfd_to_cinfo;
typedef struct cinfo_t {
int fd;
verb command_sent;
size_t file_size;
char filename[256];
char header[1024];
} cinfo_t;
/**
* @brief Main function to run the server program.
*
* This function initializes the server program, sets up signal handling, creates
* a temporary directory for file storage, and starts the server to listen for
* client connections.
*
* @param argc Number of command-line arguments.
* @param argv Array of command-line arguments.
* @return int Exit status.
*/
int main(int argc, char **argv) {
if (argc != 2) {
print_server_usage();
exit(1);
}
signal(SIGPIPE, signal_handler);
struct sigaction sigint_act;
memset(&sigint_act, '\0', sizeof(sigint_act));
sigint_act.sa_handler = quit_server;
if (sigaction(SIGINT, &sigint_act, NULL) != 0) {
perror("sigaction");
exit(1);
}
file_vector = string_vector_create();
clientfd_to_cinfo = int_to_shallow_dictionary_create();
char dirname[] = "XXXXXX";
dir = mkdtemp(dirname);
if (dir == NULL) {
perror("mkdtemp");
exit(1);
}
print_temp_directory(dir);
create_server(argv[1]);
}
/**
* @brief Handles the GET command.
*
* This function processes a GET request from a client, retrieves the requested file,
* and sends it to the client.
*
* @param client_file_descriptor Client's file descriptor.
* @return int Status code.
*/
int handle_get(int client_file_descriptor) {
printf("Starting GET\n");
// Retrieve client information
struct cinfo_t* current_client = dictionary_get(clientfd_to_cinfo, &client_file_descriptor);
if (!current_client) {
fprintf(stderr, "Failed to get client info.\n");
return -1;
}
sanitize_filename(current_client->filename);
// Check if the file is in the vector and construct the path
char *path = NULL;
int found = 0;
VECTOR_FOR_EACH(file_vector, elem, {
if (strcmp(elem, current_client->filename) == 0) {
asprintf(&path, "%s/%s", dir, (char*)elem);
found = 1;
break;
}
});
if (!found) {
printf("File not found: %s\n", current_client->filename);
return -1;
}
// Get file stats
struct stat stats;
if (stat(path, &stats) != 0) {
perror("Failed to get file stats");
free(path);
return -1;
}
current_client->file_size = stats.st_size;
// Open the file
int file_fd = open(path, O_RDONLY);
if (file_fd < 0) {
perror("Failed to open file");
free(path);
return -1;
}
printf("File size: %ld bytes\n", stats.st_size); // Output the file size
write_all_to_socket(client_file_descriptor, "OK\n", strlen("OK\n"));
write_all_to_socket(client_file_descriptor, (char*) &(current_client->file_size), sizeof(size_t));
// Send the file contents
char buf[1024];
ssize_t bytes_read;
while ((bytes_read = read(file_fd, buf, sizeof(buf))) > 0) {
if (write_all_to_socket(client_file_descriptor, buf, bytes_read) != bytes_read) {
fprintf(stderr, "Failed to send all data.\n");
close(file_fd);
free(path);
return -1;
}
}
if (bytes_read < 0) {
perror("Failed to read file");
close(file_fd);
free(path);
return -1;
}
// Cleanup and close file
close(file_fd);
free(path);
printf("File sent successfully.\n");
return 0; // Indicate success
}
/**
* @brief Handles the PUT command.
*
* This function processes a PUT request from a client, receives the file data, and
* stores it on the server.
*
* @param client_file_descriptor Client's file descriptor.
* @return int Status code.
*/
int handle_put(int client_file_descriptor) {
printf("Starting PUT\n");
struct cinfo_t* current_client = dictionary_get(clientfd_to_cinfo, &client_file_descriptor);
if (!current_client) {
fprintf(stderr, "Client info not found\n");
return -1;
}
sanitize_filename(current_client->filename);
char *path;
if (asprintf(&path, "%s/%s", dir, current_client->filename) == -1) {
fprintf(stderr, "Memory allocation failed\n");
return -1;
}
printf("Path: %s\n", path);
int file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (file_fd < 0) {
perror("open");
free(path);
return -1;
}
char buf[1024] = {0};
ssize_t bytes_read;
size_t bytes_left = current_client->file_size;
size_t total_bytes_received = 0;
// int while_loop_count = 0;
while (bytes_left > 0) {
size_t to_read = sizeof(buf) < bytes_left ? sizeof(buf) : bytes_left;
bytes_read = read_all_from_socket_put(client_file_descriptor, buf, to_read);
// printf("While loop count: %d\n and bytes_read: %ld\n, bytes_left: %ld\n", while_loop_count++, bytes_read, bytes_left);
if (bytes_read == -1) {
printf("Total bytes received: %zu\n", total_bytes_received);
printf("File size: %zu\n", current_client->file_size);
fprintf(stderr, "Failed to read from socket or connection closed prematurely.\n");
close(file_fd);
unlink(path);
free(path);
return -1;
}
if (bytes_read == 0) {
if (total_bytes_received < current_client->file_size) {
print_too_little_data();
write_all_to_socket(client_file_descriptor, "ERROR\n", 6);
write_all_to_socket(client_file_descriptor, err_bad_file_size, strlen(err_bad_file_size));
close(file_fd);
unlink(path);
free(path);
return -1;
}
if (total_bytes_received > current_client->file_size) {
print_received_too_much_data();
write_all_to_socket(client_file_descriptor, "ERROR\n", 6);
write_all_to_socket(client_file_descriptor, err_bad_file_size, strlen(err_bad_file_size));
close(file_fd);
unlink(path);
free(path);
return -1;
}
}
total_bytes_received += bytes_read;
if (total_bytes_received > current_client->file_size) {
print_received_too_much_data();
write_all_to_socket(client_file_descriptor, "ERROR\n", 6);
write_all_to_socket(client_file_descriptor, err_bad_file_size, strlen(err_bad_file_size));
close(file_fd);
unlink(path);
free(path);
return -1;
}
if (write(file_fd, buf, bytes_read) != bytes_read) {
perror("write");
close(file_fd);
unlink(path);
free(path);
return -1;
}
bytes_left -= bytes_read;
}
printf("Total bytes received: %zu\n", total_bytes_received);
printf("File size: %zu\n", current_client->file_size);
if (total_bytes_received < current_client->file_size) {
print_too_little_data();
write_all_to_socket(client_file_descriptor, "ERROR\n", 6);
write_all_to_socket(client_file_descriptor, err_bad_file_size, strlen(err_bad_file_size));
close(file_fd);
unlink(path);
free(path);
return -1;
}
printf("Successfully received and wrote %zu bytes.\n", total_bytes_received);
vector_push_back(file_vector, strdup(current_client->filename));
close(file_fd);
free(path);
return 0;
}
/**
* @brief Handles the LIST command.
*
* This function processes a LIST request from a client and sends a list of files
* stored on the server to the client.
*
* @param client_file_descriptor Client's file descriptor.
* @return int Status code.
*/
int handle_list(int client_file_descriptor) {
printf("Starting LIST\n");
// Construct the list of filenames
char *list = NULL;
size_t total_size = 0;
write_all_to_socket(client_file_descriptor, "OK\n", strlen("OK\n"));
VECTOR_FOR_EACH(file_vector, filename, {
size_t name_len = strlen(filename);
char *new_list = realloc(list, total_size + name_len + 1); // +1 for '\n'
if (!new_list) {
perror("Failed to allocate memory for file list");
free(list);
return -1;
}
list = new_list;
memcpy(list + total_size, filename, name_len);
list[total_size + name_len] = '\n'; // Append newline after each filename
total_size += name_len + 1;
});
// Ensure list is null-terminated
if (list) {
list[total_size] = '\0';
} else {
// No files to list
list = strdup("");
total_size = 1;
}
write_all_to_socket(client_file_descriptor, (char*) &total_size, sizeof(size_t));
// Write the list to the socket
if (write_all_to_socket(client_file_descriptor, list, total_size) != (ssize_t)(total_size - 1)) {
fprintf(stderr, "Failed to send file list.\n");
free(list);
return -1;
}
free(list);
printf("File list sent successfully.\n");
return 0;
}
/**
* @brief Handles the DELETE command.
*
* This function processes a DELETE request from a client and deletes the specified
* file from the server.
*
* @param client_file_descriptor Client's file descriptor.
* @return int Status code.
*/
int handle_delete(int client_file_descriptor) {
printf("Starting DELETE\n");
// Retrieve client information
struct cinfo_t* current_client = dictionary_get(clientfd_to_cinfo, &client_file_descriptor);
if (!current_client) {
fprintf(stderr, "Failed to get client info.\n");
return -1;
}
sanitize_filename(current_client->filename);
// Check if the file exists in the vector
int found = 0;
size_t index = 0;
VECTOR_FOR_EACH(file_vector, elem, {
if (strcmp(elem, current_client->filename) == 0) {
found = 1;
break;
}
index++;
});
if (!found) {
printf("File not found: %s\n", current_client->filename);
// Optionally send an error message to the client here
return -1;
}
// Construct the file path
char *path = NULL;
asprintf(&path, "%s/%s", dir, current_client->filename);
// Attempt to delete the file
if (unlink(path) != 0) {
perror("Failed to delete file");
free(path);
return -1;
}
free(path);
vector_erase(file_vector, index);
printf("File %s deleted successfully.\n", current_client->filename);
return 0;
}
/**
* @brief Handles a client connection.
*
* This function reads the client's request, determines the command, and dispatches
* the appropriate handler function (GET, PUT, LIST, DELETE).
*
* @param client_file_descriptor Client's file descriptor.
* @return int Status code.
*/
int handle_client(int client_file_descriptor) {
printf("Handling client %d\n", client_file_descriptor);
cinfo_t* curr_client_info = dictionary_get(clientfd_to_cinfo, &client_file_descriptor);
read_all_from_socket(client_file_descriptor, curr_client_info->header, 1024, 1);
printf("Header received: %s\n", curr_client_info->header);
if (!strncmp(curr_client_info->header, "PUT", 3)) {
curr_client_info->command_sent = PUT;
strcpy(curr_client_info->filename, curr_client_info->header + 4);
curr_client_info->file_size = get_message_size(client_file_descriptor);
//free(curr_client_info->header);
} else if (!strncmp(curr_client_info->header, "GET", 3)) {
curr_client_info->command_sent = GET;
strcpy(curr_client_info->filename, curr_client_info->header + 4);
curr_client_info->file_size = -1;
//free(curr_client_info->header);
} else if (!strncmp(curr_client_info->header, "DELETE", 6)) {
curr_client_info->command_sent = DELETE;
strcpy(curr_client_info->filename, curr_client_info->header + 7);
curr_client_info->file_size = -1;
//free(curr_client_info->header);
} else if (!strncmp(curr_client_info->header, "LIST", 4)) {
curr_client_info->command_sent = LIST;
strcpy(curr_client_info->filename, "Nothing");
curr_client_info->file_size = -1;
//free(curr_client_info->header);
} else {
print_invalid_response();
struct epoll_event eve_temp;
eve_temp.events = EPOLLOUT;
eve_temp.data.fd = client_file_descriptor;
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, &eve_temp);
write_all_to_socket(client_file_descriptor, "ERROR\n", 6);
printf("sending Bad request\n");
write_all_to_socket(client_file_descriptor, err_bad_request, strlen(err_bad_request));
printf("Bad request\n");
return 1;
}
if (curr_client_info->command_sent == PUT) {
if (curr_client_info->filename[0] == '\0') {
struct epoll_event eve_temp;
eve_temp.events = EPOLLOUT;
eve_temp.data.fd = client_file_descriptor;
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, &eve_temp);
write_all_to_socket(client_file_descriptor, err_bad_request, strlen(err_bad_request));
}
handle_put(client_file_descriptor);
close(client_file_descriptor);
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, NULL);
} else if (curr_client_info->command_sent == GET) {
if (curr_client_info->filename[0] == '\0') {
struct epoll_event eve_temp;
eve_temp.events = EPOLLOUT;
eve_temp.data.fd = client_file_descriptor;
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, &eve_temp);
write_all_to_socket(client_file_descriptor, err_bad_request, strlen(err_bad_request));
}
handle_get(client_file_descriptor);
close(client_file_descriptor);
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, NULL);
} else if (curr_client_info->command_sent == DELETE) {
if (curr_client_info->filename[0] == '\0') {
struct epoll_event eve_temp;
eve_temp.events = EPOLLOUT;
eve_temp.data.fd = client_file_descriptor;
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, &eve_temp);
write_all_to_socket(client_file_descriptor, err_bad_request, strlen(err_bad_request));
}
handle_delete(client_file_descriptor);
close(client_file_descriptor);
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, NULL);
} else if (curr_client_info->command_sent == LIST) {
handle_list(client_file_descriptor);
close(client_file_descriptor);
epoll_ctl(epollfd, EPOLL_CTL_DEL, client_file_descriptor, NULL);
} else {
perror("Invalid command");
}
return 0;
}
/**
* @brief Initializes the server socket.
*
* This function sets up the server socket, binds it to the specified port, and
* configures it to listen for incoming connections.
*
* @param port The port to bind the server socket to.
* @return int Server socket file descriptor.
*/
int init_server_socket(char* port) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
perror("socket");
exit(1);
}
// Set the socket to non-blocking
int flags = fcntl(sock_fd, F_GETFL, 0);
if (flags < 0) {
perror("fcntl - F_GETFL");
exit(1);
}
flags |= O_NONBLOCK;
if (fcntl(sock_fd, F_SETFL, flags) < 0) {
perror("fcntl - F_SETFL");
exit(1);
}
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int s = getaddrinfo(NULL, port, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
// quit_server();
exit(1);
}
int optval = 1;
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
perror("setsockopt");
// quit_server();
exit(1);
}
if (bind(sock_fd, result->ai_addr, result->ai_addrlen) < 0) {
perror("bind");
// quit_server();
exit(1);
}
if (listen(sock_fd, MAX_CLIENTS) < 0) {
perror("listen");
// quit_server();
exit(1);
}
freeaddrinfo(result);
return sock_fd;
}
/**
* @brief Creates the server and starts listening for connections.
*
* This function initializes the server, sets up the epoll instance, and enters the
* main loop to handle client connections and requests.
*
* @param port The port to bind the server socket to.
*/
void create_server(char* port) {
int sock_fd = init_server_socket(port);
epollfd = epoll_create1(0);
if (epollfd < 0) {
perror("epoll_create1");
// quit_server();
exit(1);
}
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = EPOLLIN;
event.data.fd = sock_fd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock_fd, &event) < 0) {
perror("epoll_ctl");
// quit_server();
exit(1);
}
struct epoll_event events[MAX_CLIENTS];
while (true) {
int num_events = epoll_wait(epollfd, events, MAX_CLIENTS, -1);
if (num_events < 0) {
perror("epoll_wait");
// quit_server();
exit(1);
} else if (num_events == 0) {
continue;
}
for (int i = 0; i < num_events; i++) {
if (events[i].data.fd == sock_fd) {
int client_fd = accept(sock_fd, NULL, NULL);
if (client_fd < 0) {
perror("accept");
// quit_server();
exit(1);
}
struct epoll_event client_event;
memset(&client_event, 0, sizeof(client_event));
client_event.events = EPOLLIN;
client_event.data.fd = client_fd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client_fd, &client_event) < 0) {
perror("epoll_ctl");
// quit_server();
exit(1);
}
cinfo_t* cinfo = calloc(1, sizeof(cinfo_t));
memset(cinfo->filename, 0, 256);
memset(cinfo->header, 0, 1024);
dictionary_set(clientfd_to_cinfo, &client_fd, cinfo);
} else {
printf("Here");
handle_client(events[i].data.fd);
}
}
}
}
/**
* @brief Retrieves the size of an incoming message.
*
* This function reads the size of an incoming message from a client.
*
* @param client_file_descriptor Client's file descriptor.
* @return ssize_t Size of the incoming message, or -1 on error.
*/
ssize_t get_message_size(int client_file_descriptor) {
size_t message_size;
char *buffer = (char *)&message_size;
memset(buffer, 0, sizeof(size_t));
ssize_t status = read_all_from_socket(client_file_descriptor, (char *)&message_size, sizeof(size_t), 0);
if (status <= 0)
return status;
return message_size;
}
/**
* @brief Reads data from a socket for a PUT request.
*
* This function reads a specified number of bytes from a socket into a buffer,
* handling interruptions and ensuring all data is read for a PUT request.
*
* @param socket The socket to read from.
* @param buffer The buffer to store the read data.
* @param count The number of bytes to read.
* @return size_t Number of bytes read, or -1 on error.
*/
size_t read_all_from_socket_put(int socket, char *buffer, size_t count) {
size_t total_bytes_read = 0;
while (total_bytes_read < count) {
ssize_t result = read(socket, buffer + total_bytes_read, count - total_bytes_read);
if (result == 0) { // No more data, normal termination
break;
}
if (result < 0) { // Handle possible errors
if (errno == EINTR) continue; // Interrupted, retry read
return -1; // An error occurred, return -1
}
total_bytes_read += result; // Increment the count of bytes read
}
return total_bytes_read == 0 ? 0 : total_bytes_read;
}
/**
* @brief Reads data from a socket.
*
* This function reads a specified number of bytes from a socket into a buffer,
* handling interruptions and ensuring all data is read.
*
* @param socket The socket to read from.
* @param buffer The buffer to store the read data.
* @param count The number of bytes to read.
* @param is_header Flag indicating if the read is for a header.
* @return ssize_t Number of bytes read, or -1 on error.
*/
ssize_t read_all_from_socket(int socket, char *buffer, size_t count, int is_header) {
size_t bytes_read = 0;
while (bytes_read < count) {
int read_retval = read(socket, (void*) (buffer + bytes_read), 1);
if (is_header) {
if (read_retval == 0 || buffer[strlen(buffer) - 1] == '\n') {
return 0;
} else if (read_retval == -1 && errno == EINTR) {
continue;
} else if (read_retval > 0) {
bytes_read += read_retval;
} else {
return -1;
}
} else {
if (read_retval == 0) {
return 0;
} else if (read_retval == -1 && errno == EINTR) {
continue;
} else if (read_retval > 0) {
bytes_read += read_retval;
} else {
return -1;
}
}
}
return bytes_read;
}
/**
* @brief Writes data to a socket.
*
* This function writes a specified number of bytes from a buffer to a socket,
* handling interruptions and ensuring all data is written.
*
* @param socket The socket to write to.
* @param buffer The buffer containing the data to write.
* @param count The number of bytes to write.
* @return ssize_t Number of bytes written, or -1 on error.
*/
ssize_t write_all_to_socket(int socket, const char *buffer, size_t count) {
size_t total = 0;
ssize_t bytes_written;
while (total < count) {
bytes_written = write(socket, buffer + total, count - total);
if (bytes_written == -1) {
if (errno == EINTR) continue;
return -1;
}
total += bytes_written;
}
return total;
}
/**
* @brief Signal handler for server shutdown.
*
* This function handles signals such as SIGINT to gracefully shut down the server.
*
* @param signal The received signal.
*/
void signal_handler(int signal) {
}
/**
* @brief Quits the server and cleans up resources.
*
* This function shuts down the server, cleans up allocated resources, and exits
* the program.
*/
void quit_server() {
printf("Quitting server\n");
VECTOR_FOR_EACH(file_vector, file_name, {
char* path = NULL;
asprintf(&path, "%s/%s", dir, (char*)file_name);
unlink(path);
free(path);
});
vector_destroy(file_vector);
rmdir(dir);
close(epollfd);
exit(0);
}
/**
* @brief Cleans up a filename.
*
* This function removes any newline or carriage return characters from a filename.
*/
void sanitize_filename(char *filename) {
if (filename == NULL) return;
char *p = filename;
while (*p) {
if (*p == '\n' || *p == '\r') {
*p = '\0'; // Replace newline or carriage return with null terminator
break;
}
p++;
}
}