From 7d554c6ee296b3d9d6b56b150b1da251ec33a678 Mon Sep 17 00:00:00 2001 From: afonso Date: Sun, 12 May 2024 11:57:10 +0100 Subject: [PATCH] djumbai_client_receiver done? --- libs/protocol/protocol.c | 2 +- libs/protocol/protocol.h | 4 +- .../djumbai_client_receive.c | 99 +++++++++++-------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/libs/protocol/protocol.c b/libs/protocol/protocol.c index 420a495..8b8b287 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; - time(&m->header.timestamp); + gettimeofday(&m->header.timestamp,NULL); strncpy(m->content, content, MAX_CONTENT_SIZE); return 0; } diff --git a/libs/protocol/protocol.h b/libs/protocol/protocol.h index 27f8b35..66435f3 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 - time_t timestamp; + struct timeval 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 7513880..818fe01 100644 --- a/src/djumbai_client_receive/djumbai_client_receive.c +++ b/src/djumbai_client_receive/djumbai_client_receive.c @@ -1,11 +1,39 @@ #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]; @@ -14,49 +42,36 @@ int main() { deserialize_message(buffer, MESSAGE_SIZE, &msg); if (msg.header.isgroup) { - 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/"); + 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); - 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 { - - // 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 + 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); } }