diff --git a/libs/protocol/protocol.c b/libs/protocol/protocol.c index 8b8b287..420a495 100644 --- a/libs/protocol/protocol.c +++ b/libs/protocol/protocol.c @@ -8,7 +8,7 @@ int new_message(message *m, unsigned int sender, unsigned int isgroup, unsigned m->header.sender = sender; m->header.isgroup = isgroup; m->header.receiver = receiver; - gettimeofday(&m->header.timestamp,NULL); + time(&m->header.timestamp); strncpy(m->content, content, MAX_CONTENT_SIZE); return 0; } diff --git a/libs/protocol/protocol.h b/libs/protocol/protocol.h index 66435f3..27f8b35 100644 --- a/libs/protocol/protocol.h +++ b/libs/protocol/protocol.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #define VERSION 1 #define MAX_CONTENT_SIZE (PIPE_BUF - sizeof(struct MessageHeader)) @@ -20,7 +20,7 @@ typedef struct MessageHeader { // The receiver is either a user or a group unsigned int receiver; // The message was sent at this timestamp - struct timeval timestamp; + time_t timestamp; } message_header; typedef struct Message { diff --git a/src/djumbai_client_receive/djumbai_client_receive.c b/src/djumbai_client_receive/djumbai_client_receive.c index 818fe01..7513880 100644 --- a/src/djumbai_client_receive/djumbai_client_receive.c +++ b/src/djumbai_client_receive/djumbai_client_receive.c @@ -1,39 +1,11 @@ #include "../../libs/communication/communication.h" #include "../../libs/protocol/protocol.h" #include -#include #include #include #include -#include #include -void create_directory(const char *path, mode_t mode) { - struct stat st = {0}; - if (stat(path, &st) == -1) { - if (mkdir(path, mode) == -1) { - fprintf(stderr, "Error creating directory %s: %s\n", path, strerror(errno)); - exit(EXIT_FAILURE); - } - } -} - -void write_message_to_file(const char *directory, const char *filename, mode_t mode, - unsigned char *msg) { - char filepath[PATH_MAX]; - snprintf(filepath, sizeof(filepath), "%s/%s", directory, filename); - int fd = open(filepath, O_WRONLY | O_CREAT | O_EXCL, mode); - if (fd == -1) { - fprintf(stderr, "Error creating file %s: %s\n", filepath, strerror(errno)); - exit(EXIT_FAILURE); - } - if (write(fd, msg, MESSAGE_SIZE) == -1) { - fprintf(stderr, "Error writing to file %s: %s\n", filepath, strerror(errno)); - exit(EXIT_FAILURE); - } - close(fd); -} - int main() { unsigned char buffer[MESSAGE_SIZE]; @@ -42,36 +14,49 @@ int main() { deserialize_message(buffer, MESSAGE_SIZE, &msg); if (msg.header.isgroup) { - const char *directory_format = "/opt/djumbai/group/%d/message_box"; - char directory[PATH_MAX]; - snprintf(directory, sizeof(directory), directory_format, msg.header.receiver); - - // Ensure that the directory structure exists - create_directory(directory, 0070); - - // Write the message to a file inside the sender's folder - char timestamp_str[21]; - unsigned long long timestamp = - msg.header.timestamp.tv_sec * 1000 + msg.header.timestamp.tv_usec / 1000; - snprintf(timestamp_str, sizeof(timestamp_str), "%lld", timestamp); - - write_message_to_file(directory, timestamp_str, 0020, buffer); + const char *directory_format = "/opt/djumbai/group/%d/"; + char group_path[50]; + sprintf(group_path, directory_format, msg.header.receiver); + char message_box_path[70]; + sprintf(message_box_path, group_path, "message_box/"); + struct stat st; + if (stat(group_path, &st) == -1) { + // Directory doesn't exist, so create it + if (mkdir(group_path, 0070) == -1) { + perror("mkdir"); + return EXIT_FAILURE; + } + } + if (stat(message_box_path, &st) == -1) { + // Directory doesn't exist, so create it + if (mkdir(message_box_path, 0070) == -1) { + perror("mkdir"); + return EXIT_FAILURE; + } + } + chroot(message_box_path); + //Do the rest } else { - const char *directory_format = "/opt/djumbai/user/%d/message_box/%d"; - char directory[PATH_MAX]; - snprintf(directory, sizeof(directory), directory_format, msg.header.receiver, - msg.header.sender); - - // Ensure that the directory structure exists - create_directory(directory, 0700); - - // Write the message to a file inside the sender's folder - char timestamp_str[21]; - unsigned long long timestamp = - msg.header.timestamp.tv_sec * 1000 + msg.header.timestamp.tv_usec / 1000; - snprintf(timestamp_str, sizeof(timestamp_str), "%lld", timestamp); - - write_message_to_file(directory, timestamp_str, 0200, buffer); + + // struct stat st; + // if (stat(user_path, &st) == -1) { + // // Directory doesn't exist, so create it + // if (mkdir(user_path, 0700) == -1) { + // perror("mkdir"); + // return EXIT_FAILURE; + // } + // } + // if (stat(message_box_path, &st) == -1) { + // // Directory doesn't exist, so create it + // if (mkdir(message_box_path, 0700) == -1) { + // perror("mkdir"); + // return EXIT_FAILURE; + // } + // } + // chroot(message_box_path); + // + // Message receiver is a user + // Write to the user's mailbox } }