changes file upload methods; improves session_send_string function.
This commit is contained in:
@@ -205,8 +205,9 @@ int32_t db_save_file(session *s) {
|
||||
const char *paramValues[6];
|
||||
int paramFormats[6];
|
||||
int paramLengths[6];
|
||||
int collision_id = 0;
|
||||
/* int collision_id = 0; */
|
||||
s_file_t *sf = s->file;
|
||||
char empty_str[] = "";
|
||||
|
||||
int32_t uid_n = htonl(s->uid);
|
||||
uint64_t size_n = htobe64(sf->size);
|
||||
@@ -217,14 +218,22 @@ int32_t db_save_file(session *s) {
|
||||
paramValues[1] = sf->name;
|
||||
paramValues[2] = (char *)&size_n;
|
||||
paramValues[3] = (char *)&hash_n;
|
||||
paramValues[4] = sf->description;
|
||||
if (sf->description != NULL) {
|
||||
paramValues[4] = sf->description;
|
||||
} else {
|
||||
paramValues[4] = empty_str;
|
||||
}
|
||||
paramValues[5] = (char *)&perm_n;
|
||||
|
||||
paramLengths[0] = sizeof(int32_t);
|
||||
paramLengths[1] = strlen(sf->name);
|
||||
paramLengths[2] = sizeof(uint64_t);
|
||||
paramLengths[3] = sizeof(int32_t);
|
||||
paramLengths[4] = strlen(sf->description);
|
||||
if (sf->description != NULL) {
|
||||
paramLengths[4] = strlen(sf->description);
|
||||
} else {
|
||||
paramLengths[4] = 0;
|
||||
}
|
||||
paramLengths[5] = sizeof(int32_t);
|
||||
|
||||
paramFormats[0] = BIN;
|
||||
|
||||
+13
-12
@@ -95,15 +95,16 @@ void file_list(session *sess, server_data_t *s_d, i_file_list_t *f_args) {
|
||||
int h_len = sprintf(item_h, "%s %zu %s", fl_current->name, fl_current->size,
|
||||
fl_current->owner);
|
||||
int d_len = strlen(fl_current->description);
|
||||
int f_len = h_len + d_len + 2;
|
||||
char *full_str = malloc(sizeof(char) * f_len);
|
||||
sprintf(full_str, "%s %s", item_h, fl_current->description);
|
||||
int i;
|
||||
for (i = h_len; i < h_len + d_len; i++) {
|
||||
if (full_str[i] == '\n') {
|
||||
full_str[i] = '\a';
|
||||
|
||||
for (int i = h_len; i < d_len; i++) {
|
||||
if (fl_current->description[i] == '\n') {
|
||||
fl_current->description[i] = '\a';
|
||||
}
|
||||
}
|
||||
|
||||
int f_len = h_len + d_len + 3;
|
||||
char *full_str = malloc(sizeof(char) * f_len);
|
||||
sprintf(full_str, "%s %s\n", item_h, fl_current->description);
|
||||
write(sess->sd, full_str, f_len - 1); // -1 : do not include \0
|
||||
free(full_str);
|
||||
sess->fl_current = fl_current;
|
||||
@@ -304,12 +305,12 @@ void file_load(session *sess, enum f_actions f_action) {
|
||||
clear_file_from_sess(sess);
|
||||
} else {
|
||||
printf("File %s is uploaded to the server\n", sess->file->name);
|
||||
session_send_string(sess, "finished\n");
|
||||
if (db_save_file(sess)) {
|
||||
clear_file_from_sess(sess);
|
||||
}
|
||||
}
|
||||
if (f_action == F_UPLOAD) {
|
||||
sess->state = OP_UPLOAD_DESCRIPTION;
|
||||
} else {
|
||||
sess->state = OP_WAIT;
|
||||
}
|
||||
sess->state = OP_WAIT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -3,7 +3,6 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "session.h"
|
||||
#include "user.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@@ -88,7 +87,7 @@ void server_main_loop(server_data_t *s_d) {
|
||||
}
|
||||
}
|
||||
|
||||
char *get_welcome_mes() {
|
||||
char *get_welcome_mes(void) {
|
||||
int fd = open(WELCOME_FILE_NAME, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror(WELCOME_FILE_NAME);
|
||||
|
||||
+10
-6
@@ -10,6 +10,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -54,8 +55,14 @@ session *make_new_session(int fd, struct sockaddr_in *from, char *wm) {
|
||||
return sess;
|
||||
}
|
||||
|
||||
void session_send_string(struct session *sess, const char *str) {
|
||||
write(sess->sd, str, strlen(str));
|
||||
void session_send_string(struct session *sess, const char *fmt, ...) {
|
||||
char buf[INBUFSIZE];
|
||||
size_t b_len = 0;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
b_len = vsprintf(buf, fmt, args);
|
||||
write(sess->sd, buf, b_len);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
int session_do_read(session *sess, char **read_str) {
|
||||
@@ -130,8 +137,6 @@ int query_extract_from_buf(session *sess, char **output_line) {
|
||||
void perform_session_action(session *sess, char *line, server_data_t *s_d) {
|
||||
int state = sess->state;
|
||||
int res;
|
||||
char response[INBUFSIZE];
|
||||
uint32_t response_len = 0;
|
||||
/* while (sess->buf_used) { */
|
||||
switch (state) {
|
||||
case OP_LOGIN_USR:
|
||||
@@ -153,8 +158,7 @@ void perform_session_action(session *sess, char *line, server_data_t *s_d) {
|
||||
res = file_upload_description(sess, line, s_d);
|
||||
if (res) {
|
||||
if (db_save_file(sess)) {
|
||||
response_len = sprintf(response, "File \"%s\" is saved!\04\n", sess->file->name);
|
||||
write(sess->sd, response, response_len+1);
|
||||
session_send_string(sess, "File \"%s\" is saved!\04\n", sess->file->name);
|
||||
clear_file_from_sess(sess);
|
||||
sess->state = OP_WAIT;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
#define SESSION_H
|
||||
#include "main.h"
|
||||
int accept_client(int ls, session *connections[], char *wm);
|
||||
void session_send_string(struct session *sess, const char *str);
|
||||
void session_send_string(struct session *sess, const char *str, ...);
|
||||
int query_extract_from_buf(session *sess, char **output_line);
|
||||
int session_do_read(session *sess, char **read_str);
|
||||
void perform_session_action(session *sess, char *line, server_data_t *s_d);
|
||||
|
||||
Reference in New Issue
Block a user