Compare commits

..
6 Commits
10 changed files with 125 additions and 70 deletions
+4 -25
View File
@@ -1,36 +1,15 @@
## Tasks
# Testing
- [ ] Tests for the server module
# Development plans
- [ ] make print_log to write a file log
- [ ] Remove file when error while saving the data in DB
- [ ] Colorize server messages
- [x] make print_log to write a file log
- [x] Colorize server messages
- [ ] Unified list of exit codes
## Features
- [x] Database integration
- [x] Paging in the file list
- [x] Get files in FS by their hash
- [x] Registration
- [x] Encrypt passwords in DB
- [x] Registration: Error when username or email is already used
- [ ] Sorting in the file list
- [ ] Check correctness of email
- [ ] Message to admin
- [x] Advanced folder structure in the storage
- [ ] File encryption
- [ ] Connection encryption
- [ ] Advanced rights for files
- [ ] Multithreading
## Done
- [x] Switch to SQLite
- [x] Close the DB correctly in any quit case (also by SIGTERM)
- [x] Get rid of unsigned integers
- [x] Check registration
- [x] Auto create a database when one is missing
- [x] Show date in server log
- [ ] Multithreading
+1 -1
View File
@@ -65,7 +65,7 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) {
if (!strcmp(arg_2, "search")) {
char *s_type = arg_1; /* since we don't need the contents of arg_1 and
arg_2 variables */
char *s_val = NULL;
char s_val[256];
ret = sscanf(line, "%*s %*s %s %s %u %u", s_type, s_val, &(fl_args.limit),
&(fl_args.page));
+16 -18
View File
@@ -108,7 +108,7 @@ void file_list(session *sess, i_file_list_t *f_args) {
fl_current->size, fl_current->owner);
int d_len = strlen(fl_current->description);
for (int i = h_len; i < d_len; i++) {
for (int i = 0; i < d_len; i++) {
if (fl_current->description[i] == '\n') {
fl_current->description[i] = '\a';
}
@@ -172,8 +172,7 @@ int32_t file_send_prepare(session *sess, char *line, server_data_t *s_d) {
}
int name_len = name_end - name_begin;
if (name_len == 0) {
session_send_string(
sess, "File name should not be empty");
session_send_string(sess, "File name should not be empty");
clear_file_from_sess(sess);
return -6;
}
@@ -238,8 +237,7 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
}
int name_len = name_end - name_begin;
if (name_len == 0) {
session_send_string(
sess, "File name should not be empty");
session_send_string(sess, "File name should not be empty");
clear_file_from_sess(sess);
return -7;
}
@@ -367,7 +365,7 @@ void file_download(session *sess) {
int rlen = read(source_d, buf, read_len);
if (rlen == 0) {
if (sess->file->rest) {
print_log(stdout, pl_error, "Error downloading file %s!\n",
print_log(stdout, pl_fail, "Fail while downloading file %s!\n",
sess->file->name);
clear_file_from_sess(sess);
}
@@ -377,7 +375,7 @@ void file_download(session *sess) {
int ret = write(dest_d, buf, rlen);
if (ret == -1) {
const char *err_mes = strerror(errno);
print_log(stdout, pl_error, "Error downloading file %s! %s\n",
print_log(stdout, pl_fail, "Fail while downloading file %s! %s\n",
sess->file->name, err_mes);
sess->state = ERR;
clear_file_from_sess(sess);
@@ -386,7 +384,7 @@ void file_download(session *sess) {
sess->file->rest -= rlen;
sess->file->package_rest -= rlen;
if (!sess->file->rest) {
print_log(stdout, pl_info, "File %s is downloaded from the server\n",
print_log(stdout, pl_success, "File %s is downloaded from the server\n",
sess->file->name);
clear_file_from_sess(sess);
sess->state = OP_WAIT;
@@ -402,10 +400,10 @@ void file_download(session *sess) {
{ \
int res = write(dest_d, buf, len); \
if (res == -1) { \
session_send_string( \
sess, "There is an error sending the file named \"%s\"\n"); \
session_send_string(sess, \
"There is an error sending the file name \"%s\"\n"); \
char *err_mes = strerror(errno); \
print_log(stdout, pl_error, "Error uploading the file \"%s\": %s\n", \
print_log(stdout, pl_fail, "Fail while uploading the file \"%s\": %s\n", \
sess->file->name, err_mes); \
close(dest_d); \
unlink(sess->file->path); /* remove file */ \
@@ -425,7 +423,7 @@ void file_upload(session *sess) {
if (rlen == 0) {
if (sess->file->rest) {
print_log(stdout, pl_error, "Error uploading file %s!\n",
print_log(stdout, pl_fail, "Fail while uploading file %s!\n",
sess->file->name);
unlink(sess->file->path); /* remove file */
clear_file_from_sess(sess);
@@ -460,7 +458,7 @@ void file_upload(session *sess) {
sess->file->package_rest = fpd->package_size - rlen;
break;
case sig_cancel:
print_log(stdout, pl_error, "Upload of %s is cancelled!\n",
print_log(stdout, pl_fail, "Upload of %s is cancelled!\n",
sess->file->name);
unlink(sess->file->path); /* remove file */
clear_file_from_sess(sess);
@@ -479,13 +477,13 @@ void file_upload(session *sess) {
}
fin:
if (!sess->file->rest) {
print_log(stdout, pl_info, "File %s is uploaded to the server\n",
print_log(stdout, pl_success, "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);
}
sess->state = OP_WAIT;
sess->state = OP_UPLOAD_DESCRIPTION;
// if (db_save_file(sess)) {
// clear_file_from_sess(sess);
// }
}
}
+11 -9
View File
@@ -2,14 +2,12 @@
/* Copyright (c) 2026 Oleksandr Zhylin */
#include "session.h"
#include <client.h>
#include <file_p.h>
#include <main.h>
#include <user.h>
#include <utils.h>
#include <arpa/inet.h>
#include <client.h>
#include <db.h>
#include <fcntl.h>
#include <file_p.h>
#include <main.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stddef.h>
@@ -19,6 +17,8 @@
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <user.h>
#include <utils.h>
session *make_new_session(int fd, struct sockaddr_in *from, char *wm);
@@ -206,14 +206,16 @@ void perform_session_action(session *sess, char *line, server_data_t *s_d) {
case OP_UPLOAD_DESCRIPTION:
res = file_upload_description(sess, line, s_d);
if (res) {
if (db_save_file(sess)) {
if (db_save_file(sess->file, sess->uid)) {
session_send_string(sess, "File \"%s\" is saved!\04\n",
sess->file->name);
clear_file_from_sess(sess);
sess->state = OP_WAIT;
} else {
// TODO: error case
session_send_string(sess, "Error while saving the file %s!\04\n",
sess->file->name);
unlink(sess->file->path);
}
clear_file_from_sess(sess);
sess->state = OP_WAIT;
}
break;
case OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE:
+78 -6
View File
@@ -1,9 +1,63 @@
#include "utils.h"
#include <asm-generic/errno-base.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define LOGFILE_NAME "bbs-server.log"
/* COLORS */
#define INFO_COLOR "\033[0m"
#define WARN_COLOR "\033[0m\033[33m"
#define ERR_COLOR "\033[0m\033[31m"
#define FATAL_COLOR "\033[0m\033[31m\033[1m"
#define FATAL_COLOR "\033[0m\033[31m\033[1m"
#define SUCCESS_COLOR "\033[0m\033[32m"
#define FAIL_COLOR "\033[0m\033[35m"
static struct {
int fd;
} file;
int open_log_file(char *path, int *_fd) {
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd == -1) {
return errno;
}
*_fd = fd;
return 0;
}
void close_log_file() { close(file.fd); }
/* TODO: create recursive backups 5 times */
int init_log_file() {
int fd;
int res = open_log_file(LOGFILE_NAME, &fd);
if (res == EEXIST) {
int res = open_log_file(LOGFILE_NAME ".old", &fd);
if (res == EEXIST) {
unlink(LOGFILE_NAME ".old");
} else {
close(fd);
}
rename(LOGFILE_NAME, LOGFILE_NAME ".old");
res = open_log_file(LOGFILE_NAME, &fd);
if (res != 0) {
return -1;
}
}
file.fd = fd;
return fd;
}
int write_into_log_file(char *fmt, va_list args) {
return vdprintf(file.fd, fmt, args);
}
int print_log(FILE *output, enum pl_type type, const char *format_message,
...) {
@@ -15,24 +69,42 @@ int print_log(FILE *output, enum pl_type type, const char *format_message,
va_list args;
sprintf(date_preffix, "[%s] ", ctime(&t));
strftime(date_preffix, 128, "[%d.%m.%y %T]", tm);
const char *tcolor;
switch (type) {
case pl_info:
uff_str = "%s %s";
uff_str = "%s%s %s";
tcolor = INFO_COLOR;
break;
case pl_warning:
uff_str = "%s Warning: %s";
uff_str = "%s%s Warning: %s";
tcolor = WARN_COLOR;
break;
case pl_error:
uff_str = "%s Error: %s";
uff_str = "%s%s Error: %s";
tcolor = ERR_COLOR;
break;
case pl_fatal:
uff_str = "%s Fatal: %s";
uff_str = "%s%s Fatal: %s";
tcolor = FATAL_COLOR;
break;
case pl_success:
uff_str = "%s%s %s";
tcolor = SUCCESS_COLOR;
break;
case pl_fail:
uff_str = "%s%s Fail: %s";
tcolor = FAIL_COLOR;
break;
}
char *uf_str = malloc(strlen(date_preffix) + 16 + strlen(format_message) + 4);
sprintf(uf_str, uff_str, date_preffix, format_message);
char *uf_str = malloc(strlen(date_preffix) + 32 + strlen(format_message) + 4);
sprintf(uf_str, uff_str, tcolor, date_preffix, format_message);
va_start(args, format_message);
ret = vfprintf(output, uf_str, args);
va_end(args);
va_start(args, format_message);
write_into_log_file(uf_str + strlen(tcolor), args);
va_end(args);
free(uf_str);
va_end(args);
return ret;
}
+4
View File
@@ -4,11 +4,15 @@
#include <stdio.h>
enum pl_type {
pl_info,
pl_success,
pl_fail,
pl_warning,
pl_error,
pl_fatal,
};
int print_log(FILE * output, enum pl_type type, const char *format_message, ...);
int init_log_file();
void close_log_file();
#endif
+1 -1
View File
@@ -47,7 +47,7 @@ typedef struct {
enum db_cb_resp { db_no_result, db_success, db_err, db_fail }; /* fail: the problem is on the user's side; no error */
int32_t init_db_connection();
int32_t db_save_file(session *s);
int32_t db_save_file(s_file_t *sfP, int uid);
int32_t db_user_auth(i_auth_t *credentials, o_auth_t *response);
s_file_t *db_get_file(i_get_file_db *arg);
int32_t db_get_files_data(i_get_files_db *arg, fl_t **fl_start,
+5 -6
View File
@@ -3,9 +3,7 @@
/* TODO: variadic function for preparing parameters */
#include "../main.h"
#include "../utils.h"
#include "db_common.h"
#include <main.h>
#include <db.h>
#include <endian.h>
#include <fcntl.h>
@@ -18,6 +16,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
#include "db_common.h"
#define UNUSED(x) (void)(x)
#define Q_LEN 128
@@ -198,15 +198,14 @@ int32_t db_user_create(i_db_user_create *args) {
return ret_value;
}
int32_t db_save_file(session *s) {
int32_t db_save_file(s_file_t *sf, int uid) {
const char *paramValues[6];
int paramFormats[6];
int paramLengths[6];
/* int collision_id = 0; */
s_file_t *sf = s->file;
char empty_str[] = "";
int32_t uid_n = htonl(s->uid);
int32_t uid_n = htonl(uid);
size_t size_n = htobe64(sf->size);
int32_t hash_n = htonl(sf->hash);
int32_t perm_n = htonl(sf->permissions);
+3 -4
View File
@@ -3,7 +3,6 @@
#include <main.h>
#include <utils.h>
#include "db_common.h"
#include <db.h>
#include <endian.h>
#include <openssl/sha.h>
@@ -16,6 +15,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "db_common.h"
#define UNUSED(x) (void)(x)
#define Q_LEN 128
@@ -284,8 +284,7 @@ enum db_cb_resp db_save_file_cb(sqlite3_stmt *stmt, void *resp) {
return res;
};
int32_t db_save_file(session *s) {
s_file_t *sfP = s->file;
int32_t db_save_file(s_file_t *sfP, int uid) {
struct db_save_file_data resp_data = {.id = 0};
const char empty_str[] = "";
enum db_cb_resp res = db_no_result;
@@ -296,7 +295,7 @@ int32_t db_save_file(session *s) {
"hash, description, permissions) "
"VALUES ($1, $2, $3, date(), $4, $5, $6) "
"RETURNING id",
db_save_file_cb, &resp_data, arg_types, s->uid, sfP->name,
db_save_file_cb, &resp_data, arg_types, uid, sfP->name,
sfP->size, sfP->hash,
sfP->description != NULL ? sfP->description : empty_str,
sfP->permissions);
+2
View File
@@ -14,8 +14,10 @@ int main(int argc, char *argv[]) {
init_db_connection();
server_data.ls = start_server();
/* TODO: display server port */
init_log_file();
print_log(stdout, pl_info, "Server is started!\n");
server_main_loop(&server_data);
db_close_connection();
close_log_file();
return 0;
}