diff --git a/src/archives/archivator.cpp b/src/archives/archivator.cpp index ba5d48fa..c855e7d1 100644 --- a/src/archives/archivator.cpp +++ b/src/archives/archivator.cpp @@ -7,10 +7,6 @@ void archivator::compress(const std::string &dir, const std::string &destination) { - archive *a; - archive_entry *entry; - int r; - std::map files; fs::path dir_path; try { @@ -42,26 +38,26 @@ void archivator::compress(const std::string &dir, const std::string &destination throw archive_exception(e.what()); } - a = archive_write_new(); - if (a == NULL) { throw archive_exception("Cannot create destination archive."); } - if (archive_write_set_format_zip(a) != ARCHIVE_OK) { + std::unique_ptr a = {archive_write_new(), archive_write_free}; + if (a == nullptr) { throw archive_exception("Cannot create destination archive."); } + if (archive_write_set_format_zip(a.get()) != ARCHIVE_OK) { throw archive_exception("Cannot set ZIP format on destination archive."); } - if (archive_write_open_filename(a, destination.c_str()) != ARCHIVE_OK) { + if (archive_write_open_filename(a.get(), destination.c_str()) != ARCHIVE_OK) { throw archive_exception("Cannot open destination archive."); } for (auto &file : files) { - entry = archive_entry_new(); + std::unique_ptr entry = {archive_entry_new(), archive_entry_free}; - archive_entry_set_pathname(entry, (fs::path(destination).stem() / file.second).string().c_str()); - archive_entry_set_size(entry, fs::file_size(file.first)); - archive_entry_set_mtime(entry, fs::last_write_time(file.first), 0); // 0 nanoseconds - archive_entry_set_filetype(entry, AE_IFREG); - archive_entry_set_perm(entry, 0644); + archive_entry_set_pathname(entry.get(), (fs::path(destination).stem() / file.second).string().c_str()); + archive_entry_set_size(entry.get(), fs::file_size(file.first)); + archive_entry_set_mtime(entry.get(), fs::last_write_time(file.first), 0); // 0 nanoseconds + archive_entry_set_filetype(entry.get(), AE_IFREG); + archive_entry_set_perm(entry.get(), 0644); - r = archive_write_header(a, entry); - if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); } + int r = archive_write_header(a.get(), entry.get()); + if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a.get())); } std::ifstream ifs((file.first).string(), std::ios::in | std::ios::binary); if (ifs.is_open()) { @@ -78,27 +74,20 @@ void archivator::compress(const std::string &dir, const std::string &destination throw archive_exception("Error reading input file."); } - r = archive_write_data(a, buff, static_cast(ifs.gcount())); - if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); } + r = archive_write_data(a.get(), buff, static_cast(ifs.gcount())); + if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a.get())); } } } else { throw archive_exception("Cannot open file " + (file.first).string() + " for reading."); } - archive_entry_free(entry); } - archive_write_close(a); - archive_write_free(a); + + archive_write_close(a.get()); } void archivator::decompress(const std::string &filename, const std::string &destination) { - archive *a; - archive *ext; - archive_entry *entry; - int flags; - int r; - if (!fs::is_directory(destination)) { throw archive_exception("Destination '" + destination + "' is not a directory. Cannot decompress archive."); } @@ -107,35 +96,38 @@ void archivator::decompress(const std::string &filename, const std::string &dest } // Select which attributes we want to restore. + int flags; flags = ARCHIVE_EXTRACT_TIME; flags |= ARCHIVE_EXTRACT_FFLAGS; // Don't allow ".." in any path within archive flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; - a = archive_read_new(); - if (a == NULL) { throw archive_exception("Cannot create source archive."); } - if (archive_read_support_format_all(a) != ARCHIVE_OK) { + std::unique_ptr a = {archive_write_new(), archive_write_free}; + if (a == nullptr) { throw archive_exception("Cannot create source archive."); } + if (archive_read_support_format_all(a.get()) != ARCHIVE_OK) { throw archive_exception("Cannot set formats for source archive."); } - if (archive_read_support_compression_all(a) != ARCHIVE_OK) { + if (archive_read_support_filter_all(a.get()) != ARCHIVE_OK) { throw archive_exception("Cannot set compression methods for source archive."); } - ext = archive_write_disk_new(); - if (ext == NULL) { throw archive_exception("Cannot allocate archive entry."); } - if (archive_write_disk_set_options(ext, flags) != ARCHIVE_OK) { + + std::unique_ptr ext = {archive_write_disk_new(), archive_write_free}; + if (ext == nullptr) { throw archive_exception("Cannot allocate archive entry."); } + if (archive_write_disk_set_options(ext.get(), flags) != ARCHIVE_OK) { throw archive_exception("Cannot set options for writing to disk."); } - if (archive_write_disk_set_standard_lookup(ext) != ARCHIVE_OK) { + if (archive_write_disk_set_standard_lookup(ext.get()) != ARCHIVE_OK) { throw archive_exception("Cannot set lookup for writing to disk."); } - r = archive_read_open_filename(a, filename.c_str(), 10240); + int r = archive_read_open_filename(a.get(), filename.c_str(), 10240); if (r < ARCHIVE_OK) { throw archive_exception("Cannot open source archive."); } while (true) { - r = archive_read_next_header(a, &entry); + archive_entry *entry; + r = archive_read_next_header(a.get(), &entry); if (r == ARCHIVE_EOF) { break; } - if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); } + if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a.get())); } const char *current_file = archive_entry_pathname(entry); const std::string full_path = (fs::path(destination) / current_file).string(); @@ -149,25 +141,23 @@ void archivator::decompress(const std::string &filename, const std::string &dest throw archive_exception("Unsupported archive entry filetype."); } - r = archive_write_header(ext, entry); - if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext)); } + r = archive_write_header(ext.get(), entry); + if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext.get())); } - if (archive_entry_size(entry) > 0) { copy_data(a, ext); } + if (archive_entry_size(entry) > 0) { copy_data(a.get(), ext.get()); } - r = archive_write_finish_entry(ext); - if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext)); } + r = archive_write_finish_entry(ext.get()); + if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext.get())); } } - archive_read_close(a); - archive_read_free(a); - archive_write_close(ext); - archive_write_free(ext); + archive_read_close(a.get()); + archive_write_close(ext.get()); } void archivator::copy_data(archive *ar, archive *aw) { - int r; + int64_t r; const void *buff; size_t size; int64_t offset; diff --git a/src/archives/archivator.h b/src/archives/archivator.h index b524b2f1..9336326e 100644 --- a/src/archives/archivator.h +++ b/src/archives/archivator.h @@ -76,22 +76,20 @@ class archive_exception : public std::exception * Constructor with custom string. * @param what String with description of failure. */ - archive_exception(std::string what) : what_(what) + archive_exception(const std::string &what) : what_(what) { } /** * Destructor. */ - virtual ~archive_exception() - { - } + ~archive_exception() override = default; /** * Get failure description. * @return Stored string. */ - virtual const char *what() const noexcept + const char *what() const noexcept override { return what_.c_str(); } diff --git a/src/broker_connection.h b/src/broker_connection.h index facda583..51776dde 100644 --- a/src/broker_connection.h +++ b/src/broker_connection.h @@ -24,7 +24,7 @@ struct message_origin { /** * A set of origins from which there are incoming messages */ - typedef std::bitset<3> set; + using set = std::bitset<3>; }; /** diff --git a/src/commands/command_holder.h b/src/commands/command_holder.h index 49e897a1..87ea02ae 100644 --- a/src/commands/command_holder.h +++ b/src/commands/command_holder.h @@ -79,7 +79,7 @@ template class command_holder { public: /** Type of callback function for easier use. */ - typedef std::function &, const command_context &)> callback_fn; + using callback_fn = std::function &, const command_context &)>; /** * Constructor with initialization of dependent (templated) part of context and logger. diff --git a/src/config/fileman_config.h b/src/config/fileman_config.h index 3552d3fc..38aff97c 100644 --- a/src/config/fileman_config.h +++ b/src/config/fileman_config.h @@ -1,6 +1,7 @@ #ifndef RECODEX_WORKER_FILEMAN_CONFIG_H #define RECODEX_WORKER_FILEMAN_CONFIG_H +#include /** * Struct which stores informations which are usefull in file managers. diff --git a/src/config/log_config.h b/src/config/log_config.h index fe4d077e..34e6c2a1 100644 --- a/src/config/log_config.h +++ b/src/config/log_config.h @@ -2,7 +2,7 @@ #define RECODEX_WORKER_LOG_CONFIG_H #include "spdlog/spdlog.h" - +#include /** * Structure which stores all information needed to initialize logger. @@ -18,9 +18,9 @@ struct log_config { /** Level of logging. Log levels are taken from spdlog. */ std::string log_level = "debug"; /** File size of one log file. */ - int log_file_size = 1024 * 1024; + size_t log_file_size = 1024 * 1024; /** Number of rotations which will be used. */ - int log_files_count = 3; + size_t log_files_count = 3; /** * Classical equality operator on log_config structures. diff --git a/src/config/sandbox_config.h b/src/config/sandbox_config.h index 926d85bb..ace2083f 100644 --- a/src/config/sandbox_config.h +++ b/src/config/sandbox_config.h @@ -2,6 +2,7 @@ #define RECODEX_WORKER_SANDBOX_CONFIG_H #include +#include #include "sandbox_limits.h" @@ -68,9 +69,7 @@ class sandbox_config /** * Constructor with defaults. */ - sandbox_config() - { - } + sandbox_config() = default; }; #endif // RECODEX_WORKER_SANDBOX_CONFIG_H diff --git a/src/config/sandbox_limits.h b/src/config/sandbox_limits.h index 53aa663c..2749e507 100644 --- a/src/config/sandbox_limits.h +++ b/src/config/sandbox_limits.h @@ -51,6 +51,11 @@ struct sandbox_limits { * time is also in (fractional) seconds. */ float extra_time = 0; + /** + * Allow to share host computers network. Otherwise, dedicated + * local interface will be created. + */ + bool share_net = false; /** * Limit stack size. This is additional memory limit, 0 is no special limit for stack, * global memory rules will aply. Otherwise, max stack size is @a stack_size kilobytes. @@ -78,11 +83,6 @@ struct sandbox_limits { * 0 means no limit. */ size_t processes = 0; - /** - * Allow to share host computers network. Otherwise, dedicated - * local interface will be created. - */ - bool share_net = false; /** * Set environment variables before run command inside the sandbox. */ @@ -96,9 +96,7 @@ struct sandbox_limits { /** * Constructor with some defaults. */ - sandbox_limits() - { - } + sandbox_limits() = default; /** * Insert environment variables which are not present yet. diff --git a/src/config/task_metadata.h b/src/config/task_metadata.h index dede61af..2aa8344e 100644 --- a/src/config/task_metadata.h +++ b/src/config/task_metadata.h @@ -27,16 +27,16 @@ class task_metadata * @param args arguments supplied for command, default = none * @param sandbox configuration of sandbox, shared pointer, its data can be changed! default = nullptr */ - task_metadata(std::string task_id = "", + task_metadata(const std::string &task_id = "", size_t priority = 0, bool fatal = false, std::vector deps = {}, task_type type = task_type::INNER, - std::string cmd = "", + const std::string &cmd = "", std::vector args = {}, std::shared_ptr sandbox = nullptr, std::string test_id = "") - : task_id(task_id), priority(priority), fatal_failure(fatal), dependencies(deps), test_id(test_id), type(type), + : task_id(task_id), priority(priority), dependencies(deps), test_id(test_id), type(type), fatal_failure(fatal), binary(cmd), cmd_args(args), sandbox(sandbox) { } @@ -45,8 +45,6 @@ class task_metadata std::string task_id; /** Priority of task among all others. Bigger priority number == greater priority. */ size_t priority; - /** If true than failure of task will end execution of whole job. */ - bool fatal_failure; /** Dependent tasks which have to be executed before this one. */ std::vector dependencies; /** Test id for external tasks */ @@ -54,6 +52,8 @@ class task_metadata /** Type of this task. */ task_type type; + /** If true than failure of task will end execution of whole job. */ + bool fatal_failure; /** Command which will be executed within this task. */ std::string binary; diff --git a/src/config/worker_config.cpp b/src/config/worker_config.cpp index dfa2c7f0..6ab7e06a 100644 --- a/src/config/worker_config.cpp +++ b/src/config/worker_config.cpp @@ -1,9 +1,7 @@ #include "worker_config.h" #include "../helpers/config.h" -worker_config::worker_config() -{ -} +worker_config::worker_config() = default; worker_config::worker_config(const YAML::Node &config) { @@ -153,8 +151,7 @@ worker_config::worker_config(const YAML::Node &config) if (limits["environ-variable"] && limits["environ-variable"].IsMap()) { for (const auto &var : limits["environ-variable"]) { - limits_.environ_vars.push_back( - std::make_pair(var.first.as(), var.second.as())); + limits_.environ_vars.emplace_back(var.first.as(), var.second.as()); } } // no throw... can be omitted @@ -188,9 +185,7 @@ worker_config::worker_config(const YAML::Node &config) } } -worker_config::~worker_config() -{ -} +worker_config::~worker_config() = default; size_t worker_config::get_worker_id() const { diff --git a/src/config/worker_config.h b/src/config/worker_config.h index 82c7170f..139ad2da 100644 --- a/src/config/worker_config.h +++ b/src/config/worker_config.h @@ -24,7 +24,7 @@ class worker_config { public: /** Type of the header map */ - typedef std::multimap header_map_t; + using header_map_t = std::multimap; /** * The default constructor @@ -128,35 +128,35 @@ class worker_config private: /** Unique worker number in context of one machine (0-100 preferably) */ - size_t worker_id_; + size_t worker_id_ = 0; /** Human readable description of the worker for logging purposes */ - std::string worker_description_; + std::string worker_description_ = ""; /** Working directory of whole worker used as base directory for all temporary files */ - std::string working_directory_; + std::string working_directory_ = ""; /** Broker URI, address where broker is listening */ - std::string broker_uri_; + std::string broker_uri_ = ""; /** Header which are sent to broker and should specify worker abilities */ - header_map_t headers_; + header_map_t headers_ = {}; /** Hwgroup which is sent to broker and is used in job configuration to select right limits */ - std::string hwgroup_; + std::string hwgroup_ = {}; /** Maximum number of pings in a row without response before the broker is considered disconnected */ size_t max_broker_liveness_ = 4; /** How often should the worker ping the broker */ std::chrono::milliseconds broker_ping_interval_ = std::chrono::milliseconds(1000); /** The caching directory path */ - std::string cache_dir_; + std::string cache_dir_ = ""; /** Configuration of logger */ - log_config log_config_; + log_config log_config_ = {}; /** Default configuration of file managers */ - std::vector filemans_configs_; + std::vector filemans_configs_ = {}; /** Default sandbox limits */ - sandbox_limits limits_; + sandbox_limits limits_ = {}; /** Maximal length of output from sandbox which can be written to the results file, in bytes. */ - size_t max_output_length_; + size_t max_output_length_ = 0; /** Maximal lenght of output from sandbox which can be copied into results folder, in bytes */ - size_t max_carboncopy_length_; + size_t max_carboncopy_length_ = 0; /** If true then all files created during evaluation of job will be deleted at the end. */ - bool cleanup_submission_; + bool cleanup_submission_ = true; }; diff --git a/src/worker_core.cpp b/src/worker_core.cpp index 78204c88..f3a14bd2 100644 --- a/src/worker_core.cpp +++ b/src/worker_core.cpp @@ -105,7 +105,7 @@ void worker_core::load_config() return; } -void worker_core::force_exit(std::string msg) +void worker_core::force_exit(const std::string &msg) { // write to log if (msg != "") { diff --git a/src/worker_core.h b/src/worker_core.h index 10b0491c..7dc77c08 100644 --- a/src/worker_core.h +++ b/src/worker_core.h @@ -85,7 +85,7 @@ class worker_core * Exit whole application with return code 1. * @param msg string which is copied to stderr and logger if initialized. */ - void force_exit(std::string msg = ""); + void force_exit(const std::string &msg = ""); /** * Parse cmd line params given in constructor.