djumbai_client_receiver done?
This commit is contained in:
parent
897c8150a5
commit
7d554c6ee2
3 changed files with 60 additions and 45 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#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 {
|
||||
|
|
|
@ -1,11 +1,39 @@
|
|||
#include "../../libs/communication/communication.h"
|
||||
#include "../../libs/protocol/protocol.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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 {
|
||||
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);
|
||||
|
||||
// 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
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue