File hashing (not finished)

This commit is contained in:
2025-10-27 17:33:39 +02:00
parent 24c4e75943
commit 76dff7dafb
9 changed files with 421 additions and 86 deletions
+60 -17
View File
@@ -1,4 +1,5 @@
#include "file_p.h"
#include "libs/murmur3/murmur3.h"
#include "main.h"
#include "session.h"
#include <arpa/inet.h>
@@ -12,6 +13,7 @@
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/syslog.h>
#include <sys/types.h>
@@ -44,7 +46,7 @@ void file_list(session *sess, server_data_t *s_d) {
full_str[i] = '\a';
}
}
write(sess->sd, full_str, f_len-1); // -1 : do not include \0
write(sess->sd, full_str, f_len - 1); // -1 : do not include \0
free(full_str);
} while ((current = current->next) != NULL);
write(sess->sd, list_end, sizeof(list_end));
@@ -84,6 +86,25 @@ int file_send_prepare(session *sess, char *line, server_data_t *s_d) {
return 0;
}
int directory_exists(const char *path) {
struct stat info;
if (stat(path, &info) != 0) {
// stat() failed, probably doesn't exist
return 0;
}
return S_ISDIR(info.st_mode); // true if it's a directory
}
void extract_names_from_hash(uint32_t file_hash, char *dirname, char *fname) {
char hashed_full_name[9];
sprintf(hashed_full_name, "%08x", file_hash);
strncpy(dirname, hashed_full_name, 2);
strncpy(fname, hashed_full_name + 2, 6);
dirname[2] = 0;
fname[6] = 0;
}
int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
int sd = sess->sd;
char fname[64];
@@ -93,8 +114,6 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
sess->fname = malloc(sizeof(char) * strlen(fname));
sess->f_perm = (char)perm;
strncpy(sess->fname, fname, strlen(fname) - 1); // remove the last \"
sess->fpath = malloc(strlen(sess->fname) + sizeof(STORAGE_FOLDER) + 2);
sprintf(sess->fpath, "%s/%s", STORAGE_FOLDER, sess->fname);
char mes[256];
char mes_len = 0;
@@ -114,19 +133,43 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
clear_file_from_sess(sess);
return -1;
}
int file_d = open(sess->fpath, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (file_d == -1) {
if (errno == EEXIST) {
mes_len =
sprintf(mes, "File with name \"%s\" is already exist\n", sess->fname);
char hashed_dir_name[3];
char hashed_name[7];
int seed = FILE_HASH_SEED;
int file_d;
for (;;) {
MurmurHash3_x86_32(sess->fname, strlen(sess->fname), FILE_HASH_SEED,
&(sess->hash));
extract_names_from_hash(sess->hash, hashed_dir_name, hashed_name);
sess->fpath = malloc(sizeof(STORAGE_FOLDER) + 2 + 9);
sprintf(sess->fpath, "%s/%s/%s", STORAGE_FOLDER, hashed_dir_name,
hashed_name);
if (!directory_exists(hashed_dir_name)) {
mkdir(hashed_dir_name, 0700);
}
chdir(hashed_dir_name);
file_d = open(sess->fpath, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (file_d == -1) {
if (errno == EEXIST) {
seed++;
free(sess->fpath);
sess->fpath = NULL;
continue;
}
mes_len = sprintf(mes, "Can't create file with such name: \"%s\"\n",
sess->fname);
write(sd, mes, mes_len);
clear_file_from_sess(sess);
return -1;
}
mes_len =
sprintf(mes, "Can't create file with such name: \"%s\"\n", sess->fname);
write(sd, mes, mes_len);
clear_file_from_sess(sess);
return -1;
}
mes_len = sprintf(mes, "accept");
sess->fd = file_d;
sess->fsize = fsize;
@@ -257,19 +300,19 @@ void get_files_descriptions(server_data_t *s_d) {
/* file name */
sscanf(buf_pos, "%s\n", tmp);
buf_pos += strlen(tmp)+1;
buf_pos += strlen(tmp) + 1;
fl_item->name = malloc(sizeof(char) * (strlen(tmp) + 1));
strcpy(fl_item->name,tmp);
strcpy(fl_item->name, tmp);
/* file owner */
sscanf(buf_pos, "%s\n", tmp);
buf_pos += strlen(tmp)+1;
buf_pos += strlen(tmp) + 1;
fl_item->owner = malloc(sizeof(char) * (strlen(tmp) + 1));
strcpy(fl_item->owner, tmp);
/* file permissions */
sscanf(buf_pos, "%s\n", tmp);
buf_pos += strlen(tmp)+1;
buf_pos += strlen(tmp) + 1;
fl_item->permissions = atoi(tmp);
/* file size */