save file into db
This commit is contained in:
@@ -115,20 +115,15 @@ int db_save_file(session *s) {
|
||||
int collision_id = 0;
|
||||
session_file *sf = s->file;
|
||||
|
||||
if (sf->seed != FILE_HASH_SEED) {
|
||||
// TODO: сиды обрабатываются неправильно
|
||||
collision_id = db_save_collision(sf->hash);
|
||||
}
|
||||
|
||||
int32_t uid_n = htonl(s->uid);
|
||||
int64_t size_n = htonl(sf->size);
|
||||
int32_t coll_n = htonl(collision_id);
|
||||
int32_t hash_n = htonl(sf->hash);
|
||||
int32_t perm_n = htonl(sf->permissions);
|
||||
|
||||
paramValues[0] = (char *)&uid_n;
|
||||
paramValues[1] = sf->name;
|
||||
paramValues[2] = (char *)&size_n;
|
||||
paramValues[3] = (char *)&coll_n;
|
||||
paramValues[3] = (char *)&hash_n;
|
||||
paramValues[4] = sf->description;
|
||||
paramValues[5] = (char *)&perm_n;
|
||||
|
||||
@@ -148,8 +143,8 @@ int db_save_file(session *s) {
|
||||
|
||||
res = PQexecParams(conn,
|
||||
"INSERT INTO files(user_id, name, size, created_at, "
|
||||
"collision_id, description, permissions) "
|
||||
"VALUES ($1, $2, $3, NOW(), NULLIF($4, 0), $5, $6) "
|
||||
"hash, description, permissions) "
|
||||
"VALUES ($1, $2, $3, NOW(), $4, $5, $6) "
|
||||
"RETURNING id",
|
||||
6, NULL, paramValues, paramLengths, paramFormats, TEXT);
|
||||
|
||||
@@ -161,31 +156,6 @@ int db_save_file(session *s) {
|
||||
return id;
|
||||
}
|
||||
|
||||
int db_save_collision(uint32_t hash) {
|
||||
const char *paramValues[1];
|
||||
int paramLengths[1];
|
||||
int paramFormats[1];
|
||||
|
||||
uint32_t hash_n = htonl(hash);
|
||||
|
||||
paramValues[0] = (char *)&hash_n;
|
||||
paramLengths[0] = sizeof(uint32_t);
|
||||
paramFormats[0] = BIN;
|
||||
|
||||
res = PQexecParams(conn,
|
||||
"INSERT INTO file_collisions(hash) "
|
||||
"VALUES ($1) "
|
||||
"RETURNING id",
|
||||
1, NULL, paramValues, paramLengths, paramFormats, TEXT);
|
||||
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res))
|
||||
return exit_query_2(0);
|
||||
|
||||
int id = atoi(PQgetvalue(res, 0, 0));
|
||||
clearRes();
|
||||
return id;
|
||||
}
|
||||
|
||||
// TODO: get file
|
||||
// int db_get_file(i_get_file_db *arg) {
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
client.o: src/client.c src/client.h src/main.h src/file_p.h src/session.h
|
||||
db.o: src/db.c src/db.h src/main.h
|
||||
file_p.o: src/file_p.c src/file_p.h src/main.h src/libs/murmur3/murmur3.h
|
||||
main.o: src/main.c src/main.h src/file_p.h src/server.h src/db.h
|
||||
server.o: src/server.c src/main.h src/session.h src/user.h
|
||||
session.o: src/session.c src/session.h src/main.h src/client.h \
|
||||
src/file_p.h src/user.h src/db.h
|
||||
user.o: src/user.c src/main.h src/session.h src/db.h
|
||||
|
||||
+32
-32
@@ -1,6 +1,7 @@
|
||||
#include "file_p.h"
|
||||
#include "libs/murmur3/murmur3.h"
|
||||
#include "main.h"
|
||||
#include "session.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@@ -112,10 +113,10 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
|
||||
sscanf(line, "file upload \"%s %zd %d", fname, &fsize, &perm);
|
||||
sess->file = malloc(sizeof(session_file));
|
||||
sess->file->name = malloc(sizeof(char) * strlen(fname));
|
||||
// TODO: в конце гуляет \n и \"
|
||||
sess->file->permissions = (char)perm;
|
||||
sess->file->description = NULL;
|
||||
strncpy(sess->file->name, fname, strlen(fname) - 1); // remove the last \"
|
||||
strncpy(sess->file->name, fname, strlen(fname) - 1);
|
||||
sess->file->name[strlen(fname) - 1] = 0;
|
||||
|
||||
char mes[256];
|
||||
char mes_len = 0;
|
||||
@@ -138,12 +139,12 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
|
||||
|
||||
char hashed_dir_name[3];
|
||||
char hashed_name[7];
|
||||
sess->file->seed = FILE_HASH_SEED;
|
||||
int seed = FILE_HASH_SEED;
|
||||
int file_d;
|
||||
|
||||
for (;;) {
|
||||
MurmurHash3_x86_32(sess->file->name, strlen(sess->file->name), sess->file->seed,
|
||||
&sess->file->hash);
|
||||
MurmurHash3_x86_32(sess->file->name, strlen(sess->file->name),
|
||||
seed, &sess->file->hash);
|
||||
|
||||
extract_names_from_hash(sess->file->hash, hashed_dir_name, hashed_name);
|
||||
|
||||
@@ -161,7 +162,7 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
|
||||
file_d = open(sess->file->path, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
||||
if (file_d == -1) {
|
||||
if (errno == EEXIST) {
|
||||
sess->file->seed++;
|
||||
seed++;
|
||||
free(sess->file->path);
|
||||
sess->file->path = NULL;
|
||||
continue;
|
||||
@@ -185,7 +186,7 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
|
||||
}
|
||||
|
||||
void clear_file_from_sess(session *s) {
|
||||
session_file * sf = s->file;
|
||||
session_file *sf = s->file;
|
||||
if (sf->name != NULL) {
|
||||
free(sf->name);
|
||||
sf->name = NULL;
|
||||
@@ -262,31 +263,30 @@ size_t get_file_size(char *dir_n, char *file_n) {
|
||||
|
||||
/* returns 1 if there is :END: ; 0 if opposite */
|
||||
int file_upload_description(session *sess, char *line, server_data_t *s_d) {
|
||||
// for (;;) {
|
||||
// if (line != NULL && sess->file->description == NULL) { /* first query */
|
||||
// sess->file->description = malloc(strlen(line) + 1);
|
||||
// strcpy(sess->file->description, line);
|
||||
// } else { /* subsequent queries */
|
||||
// if (line == NULL) {
|
||||
// if (sess->buf_used > 0) {
|
||||
// query_extract_from_buf(sess, &line);
|
||||
// } else {
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
// sess->file->description =
|
||||
// realloc(sess->file->description, strlen(sess->file->description) + strlen(line) + 1);
|
||||
// strcat(sess->file->description, line);
|
||||
// }
|
||||
// char *desc_end = strstr(line, ":END:");
|
||||
// if (desc_end != NULL) {
|
||||
// return 1;
|
||||
// }
|
||||
// }
|
||||
char _msg[] = "This is a test \n Description!\n";
|
||||
sess->file->description = malloc(sizeof(_msg) + 1);
|
||||
strcpy(sess->file->description, _msg);
|
||||
return 1;
|
||||
for (;;) {
|
||||
if (line != NULL && sess->file->description == NULL) { /* first query */
|
||||
sess->file->description = malloc(strlen(line) + 1);
|
||||
strcpy(sess->file->description, line);
|
||||
} else { /* subsequent queries */
|
||||
if (line == NULL) {
|
||||
if (sess->buf_used > 0) {
|
||||
query_extract_from_buf(sess, &line);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
sess->file->description =
|
||||
realloc(sess->file->description,
|
||||
strlen(sess->file->description) + strlen(line) + 1);
|
||||
strcat(sess->file->description, line);
|
||||
}
|
||||
char *desc_end = strstr(line, ":END:");
|
||||
if (desc_end != NULL) {
|
||||
sess->file->description[strlen(sess->file->description) - 6] = 0; // cut the :END:
|
||||
return 1;
|
||||
}
|
||||
line = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: синхронизовать c C++
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/* Murmur3 example: hash the first command line argument. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "murmur3.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
uint32_t hash[4]; /* Output for the hash */
|
||||
uint32_t seed = 420; /* Seed value for hash */
|
||||
|
||||
if (argc != 2) {
|
||||
printf("usage: %s \"string to hash\"\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Input: \"%s\"\n", argv[1]);
|
||||
|
||||
MurmurHash3_x86_32(argv[1], strlen(argv[1]), seed, hash);
|
||||
printf("x86_32: %08x\n", hash[0]);
|
||||
|
||||
MurmurHash3_x86_128(argv[1], strlen(argv[1]), seed, hash);
|
||||
printf("x86_128: %08x %08x %08x %08x\n",
|
||||
hash[0], hash[1], hash[2], hash[3]);
|
||||
|
||||
MurmurHash3_x64_128(argv[1], strlen(argv[1]), seed, hash);
|
||||
printf("x64_128: %08x %08x %08x %08x\n",
|
||||
hash[0], hash[1], hash[2], hash[3]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -28,7 +28,6 @@ enum sess_states {
|
||||
FIN
|
||||
};
|
||||
|
||||
|
||||
enum user_privileges {
|
||||
ADD_FILE = 01,
|
||||
EDIT_FILES = 02,
|
||||
@@ -50,7 +49,6 @@ typedef struct {
|
||||
size_t size;
|
||||
size_t rest;
|
||||
uint32_t hash;
|
||||
int seed;
|
||||
int fd;
|
||||
} session_file;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user