fixing bugs, changing some code from char arrays to c++ std::string
This commit is contained in:
@@ -1,106 +1,98 @@
|
||||
#ifndef UPDATE_CPP
|
||||
#define UPDATE_CPP
|
||||
|
||||
#include "update.h"
|
||||
#include "log.h"
|
||||
#include "connect.h"
|
||||
#include "settings.h"
|
||||
#include "security.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
int update_db(const std::string& folder_path) {
|
||||
// Download the databases from the server
|
||||
for (char firstChar = '0'; firstChar <= 'f'; ++firstChar) {
|
||||
for (char secondChar = '0'; secondChar <= 'f'; ++secondChar) {
|
||||
// Ensure that the characters are valid hexadecimal digits
|
||||
if (!std::isxdigit(firstChar) || !std::isxdigit(secondChar) || std::isupper(firstChar) || std::isupper(secondChar)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int update_db(const char* folder_path) {
|
||||
//download the databases from the server
|
||||
for (char firstChar = '0'; firstChar <= 'f'; ++firstChar) {
|
||||
for (char secondChar = '0'; secondChar <= 'f'; ++secondChar) {
|
||||
// Ensure that the characters are valid hexadecimal digits
|
||||
if (!std::isxdigit(firstChar) || !std::isxdigit(secondChar) or std::isupper(firstChar) or std::isupper(secondChar)) {
|
||||
continue;
|
||||
}
|
||||
// Create the filename based on the naming convention
|
||||
std::string file_name = std::string(1, firstChar) + secondChar + ".jdbf";
|
||||
|
||||
// Create the filename based on the naming convention
|
||||
// Create the strings to download the files
|
||||
std::string url = get_setting_string("server:server_url");
|
||||
if (url.empty() || url == "nan") {
|
||||
return 2; // Invalid server URL
|
||||
}
|
||||
url += "/database_srv/" + file_name;
|
||||
|
||||
char file_name[] = { firstChar, secondChar ,'.','j','d','b','f','\0' };
|
||||
//create the strings to download the files
|
||||
char* output_path = new char[300];
|
||||
char* url = new char[1000];
|
||||
if (get_setting("server:server_url", url) == 0 or strcmp(url, "nan") == 0) {
|
||||
strcat_s(url, 295, "/database_srv/");
|
||||
strcat_s(url, 295, file_name);
|
||||
strcpy_s(output_path, 295, folder_path);
|
||||
strcat_s(output_path, 295, "\\");
|
||||
strcat_s(output_path, 295, file_name);
|
||||
printf("%s\n", url);
|
||||
int res = download_file_from_srv(url, output_path,get_setting("communication:unsafe_tls"));
|
||||
if (res != 0) {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return 2;
|
||||
}
|
||||
std::string output_path = folder_path + "\\" + file_name;
|
||||
|
||||
delete[] url;
|
||||
delete[] output_path;
|
||||
std::cout << url << std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
int res = download_file_from_srv(url, output_path, get_setting("communication:unsafe_tls"));
|
||||
if (res != 0) {
|
||||
return 10; // Error downloading file
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int update_settings(const char*settings_type) {
|
||||
//create the strings to download the files
|
||||
char* url = new char[1000];
|
||||
if (get_setting("server:server_url", url) == 0 or strcmp(url,"nan")==0) {
|
||||
strcat_s(url, 1000, "/api/php/settings/get_settings.php?");//need to add machine_id and apikey
|
||||
strcat_s(url, 1000, settings_type);
|
||||
strcat_s(url, 1000, "&machine_id=");
|
||||
strcat_s(url, 1000, get_machineid(SECRETS));
|
||||
strcat_s(url, 1000, "&apikey=");
|
||||
strcat_s(url, 1000, get_apikey(SECRETS));
|
||||
int res = 1;
|
||||
if (strcmp(settings_type, "settings") == 0)
|
||||
res = download_file_from_srv(url, SETTINGS_DB, get_setting("communication:unsafe_tls"));
|
||||
else if (strcmp(settings_type, "rtp_included") == 0)
|
||||
res = download_file_from_srv(url, INCLUDED_FOLDERS, get_setting("communication:unsafe_tls"));
|
||||
else if (strcmp(settings_type, "rtp_excluded") == 0)
|
||||
res = download_file_from_srv(url, EXCLUDED_FOLDERS, get_setting("communication:unsafe_tls"));
|
||||
else if (strcmp(settings_type, "sched") == 0)
|
||||
res = download_file_from_srv(url, SCHED_PATH, get_setting("communication:unsafe_tls"));
|
||||
//int res = 0;
|
||||
if (res != 0) {
|
||||
log(LOGLEVEL::ERR, "[update_settings()]: Error downloading settings database file from server", " ERROR:", res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
delete[] url;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
delete[] url;
|
||||
return 2;
|
||||
}
|
||||
return 2;
|
||||
int update_settings(const std::string& settings_type) {
|
||||
// Create the strings to download the files
|
||||
std::string url = get_setting_string("server:server_url");
|
||||
if (url.empty() || url == "nan") {
|
||||
return 2; // Invalid server URL
|
||||
}
|
||||
url += "/api/php/settings/get_settings.php?";
|
||||
url += settings_type + "&machine_id=" + get_machineid(SECRETS) + "&apikey=" + get_apikey(SECRETS);
|
||||
|
||||
int res = 1;
|
||||
if (settings_type == "settings")
|
||||
res = download_file_from_srv(url, SETTINGS_DB, get_setting("communication:unsafe_tls"));
|
||||
else if (settings_type == "rtp_included")
|
||||
res = download_file_from_srv(url, INCLUDED_FOLDERS, get_setting("communication:unsafe_tls"));
|
||||
else if (settings_type == "rtp_excluded")
|
||||
res = download_file_from_srv(url, EXCLUDED_FOLDERS, get_setting("communication:unsafe_tls"));
|
||||
else if (settings_type == "sched")
|
||||
res = download_file_from_srv(url, SCHED_PATH, get_setting("communication:unsafe_tls"));
|
||||
|
||||
if (res != 0) {
|
||||
log(LOGLEVEL::ERR, "[update_settings()]: Error downloading settings database file from server. ERROR:", res);
|
||||
return 1; // Error downloading file
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int action_update_settings() {
|
||||
//update the settings
|
||||
int err = 0;
|
||||
if (update_settings("settings") != 0) {
|
||||
err= 1;
|
||||
}
|
||||
//update the included folders
|
||||
if (update_settings("rtp_included") != 0) {
|
||||
err= 2;
|
||||
}
|
||||
//update the excluded folders
|
||||
if (update_settings("rtp_excluded") != 0) {
|
||||
err= 3;
|
||||
}
|
||||
//update the schedule
|
||||
if (update_settings("sched") != 0) {
|
||||
err= 4;
|
||||
}
|
||||
return err;
|
||||
// Update the settings
|
||||
int err = 0;
|
||||
if (update_settings("settings") != 0)
|
||||
err = 1;
|
||||
|
||||
// Update the included folders
|
||||
if (update_settings("rtp_included") != 0)
|
||||
err = 2;
|
||||
|
||||
// Update the excluded folders
|
||||
if (update_settings("rtp_excluded") != 0)
|
||||
err = 3;
|
||||
|
||||
// Update the schedule
|
||||
if (update_settings("sched") != 0)
|
||||
err = 4;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int action_update_db() {
|
||||
//update the databases
|
||||
return update_db(DB_DIR);
|
||||
// Update the databases
|
||||
return update_db(DB_DIR);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // UPDATE_CPP
|
||||
|
||||
Reference in New Issue
Block a user