Remove file when error while saving the data in DB
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
## Tasks
|
## Tasks
|
||||||
|
|
||||||
# Testing
|
|
||||||
|
|
||||||
- [ ] Tests for the server module
|
|
||||||
|
|
||||||
# Development plans
|
# Development plans
|
||||||
- [ ] make print_log to write a file log
|
- [ ] make print_log to write a file log
|
||||||
- [ ] Remove file when error while saving the data in DB
|
- [ ] Remove file when error while saving the data in DB
|
||||||
@@ -11,26 +7,10 @@
|
|||||||
- [ ] Unified list of exit codes
|
- [ ] Unified list of exit codes
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
- [ ] Sorting in the file list
|
||||||
- [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
|
|
||||||
- [ ] Check correctness of email
|
- [ ] Check correctness of email
|
||||||
- [ ] Message to admin
|
- [ ] Message to admin
|
||||||
- [x] Advanced folder structure in the storage
|
|
||||||
- [ ] File encryption
|
- [ ] File encryption
|
||||||
- [ ] Connection encryption
|
- [ ] Connection encryption
|
||||||
- [ ] Advanced rights for files
|
- [ ] Advanced rights for files
|
||||||
- [ ] Multithreading
|
- [ ] 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
|
|
||||||
@@ -2,14 +2,12 @@
|
|||||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||||
|
|
||||||
#include "session.h"
|
#include "session.h"
|
||||||
#include <client.h>
|
|
||||||
#include <file_p.h>
|
|
||||||
#include <main.h>
|
|
||||||
#include <user.h>
|
|
||||||
#include <utils.h>
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <client.h>
|
||||||
#include <db.h>
|
#include <db.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <file_p.h>
|
||||||
|
#include <main.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -19,6 +17,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <user.h>
|
||||||
|
#include <utils.h>
|
||||||
|
|
||||||
session *make_new_session(int fd, struct sockaddr_in *from, char *wm);
|
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:
|
case OP_UPLOAD_DESCRIPTION:
|
||||||
res = file_upload_description(sess, line, s_d);
|
res = file_upload_description(sess, line, s_d);
|
||||||
if (res) {
|
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",
|
session_send_string(sess, "File \"%s\" is saved!\04\n",
|
||||||
sess->file->name);
|
sess->file->name);
|
||||||
|
} else {
|
||||||
|
session_send_string(sess, "Error while saving the file %s!\04\n",
|
||||||
|
sess->file->name);
|
||||||
|
unlink(sess->file->path);
|
||||||
|
}
|
||||||
clear_file_from_sess(sess);
|
clear_file_from_sess(sess);
|
||||||
sess->state = OP_WAIT;
|
sess->state = OP_WAIT;
|
||||||
} else {
|
|
||||||
// TODO: error case
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE:
|
case OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE:
|
||||||
|
|||||||
+1
-1
@@ -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 */
|
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 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);
|
int32_t db_user_auth(i_auth_t *credentials, o_auth_t *response);
|
||||||
s_file_t *db_get_file(i_get_file_db *arg);
|
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,
|
int32_t db_get_files_data(i_get_files_db *arg, fl_t **fl_start,
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
/* TODO: variadic function for preparing parameters */
|
/* TODO: variadic function for preparing parameters */
|
||||||
|
|
||||||
#include "../main.h"
|
#include <main.h>
|
||||||
#include "../utils.h"
|
|
||||||
#include "db_common.h"
|
|
||||||
#include <db.h>
|
#include <db.h>
|
||||||
#include <endian.h>
|
#include <endian.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -18,6 +16,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include "db_common.h"
|
||||||
|
|
||||||
#define UNUSED(x) (void)(x)
|
#define UNUSED(x) (void)(x)
|
||||||
#define Q_LEN 128
|
#define Q_LEN 128
|
||||||
@@ -198,15 +198,14 @@ int32_t db_user_create(i_db_user_create *args) {
|
|||||||
return ret_value;
|
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];
|
const char *paramValues[6];
|
||||||
int paramFormats[6];
|
int paramFormats[6];
|
||||||
int paramLengths[6];
|
int paramLengths[6];
|
||||||
/* int collision_id = 0; */
|
/* int collision_id = 0; */
|
||||||
s_file_t *sf = s->file;
|
|
||||||
char empty_str[] = "";
|
char empty_str[] = "";
|
||||||
|
|
||||||
int32_t uid_n = htonl(s->uid);
|
int32_t uid_n = htonl(uid);
|
||||||
size_t size_n = htobe64(sf->size);
|
size_t size_n = htobe64(sf->size);
|
||||||
int32_t hash_n = htonl(sf->hash);
|
int32_t hash_n = htonl(sf->hash);
|
||||||
int32_t perm_n = htonl(sf->permissions);
|
int32_t perm_n = htonl(sf->permissions);
|
||||||
|
|||||||
+3
-4
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
#include "db_common.h"
|
|
||||||
#include <db.h>
|
#include <db.h>
|
||||||
#include <endian.h>
|
#include <endian.h>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
@@ -16,6 +15,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "db_common.h"
|
||||||
|
|
||||||
#define UNUSED(x) (void)(x)
|
#define UNUSED(x) (void)(x)
|
||||||
#define Q_LEN 128
|
#define Q_LEN 128
|
||||||
@@ -284,8 +284,7 @@ enum db_cb_resp db_save_file_cb(sqlite3_stmt *stmt, void *resp) {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
int32_t db_save_file(session *s) {
|
int32_t db_save_file(s_file_t *sfP, int uid) {
|
||||||
s_file_t *sfP = s->file;
|
|
||||||
struct db_save_file_data resp_data = {.id = 0};
|
struct db_save_file_data resp_data = {.id = 0};
|
||||||
const char empty_str[] = "";
|
const char empty_str[] = "";
|
||||||
enum db_cb_resp res = db_no_result;
|
enum db_cb_resp res = db_no_result;
|
||||||
@@ -296,7 +295,7 @@ int32_t db_save_file(session *s) {
|
|||||||
"hash, description, permissions) "
|
"hash, description, permissions) "
|
||||||
"VALUES ($1, $2, $3, date(), $4, $5, $6) "
|
"VALUES ($1, $2, $3, date(), $4, $5, $6) "
|
||||||
"RETURNING id",
|
"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->size, sfP->hash,
|
||||||
sfP->description != NULL ? sfP->description : empty_str,
|
sfP->description != NULL ? sfP->description : empty_str,
|
||||||
sfP->permissions);
|
sfP->permissions);
|
||||||
|
|||||||
Reference in New Issue
Block a user