Skip to content

Commit

Permalink
Modernization vol 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed Jun 21, 2018
1 parent 3569305 commit 34e3d06
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 98 deletions.
86 changes: 38 additions & 48 deletions src/archives/archivator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

void archivator::compress(const std::string &dir, const std::string &destination)
{
archive *a;
archive_entry *entry;
int r;

std::map<fs::path, fs::path> files;
fs::path dir_path;
try {
Expand Down Expand Up @@ -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<archive, decltype(&archive_write_free)> 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<archive_entry, decltype(&archive_entry_free)> 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()) {
Expand All @@ -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<size_t>(ifs.gcount()));
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); }
r = archive_write_data(a.get(), buff, static_cast<size_t>(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.");
}
Expand All @@ -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<archive, decltype(&archive_write_free)> 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<archive, decltype(&archive_write_free)> 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();
Expand All @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/archives/archivator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/broker_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/commands/command_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template <typename context_t> class command_holder
{
public:
/** Type of callback function for easier use. */
typedef std::function<void(const std::vector<std::string> &, const command_context<context_t> &)> callback_fn;
using callback_fn = std::function<void(const std::vector<std::string> &, const command_context<context_t> &)>;

/**
* Constructor with initialization of dependent (templated) part of context and logger.
Expand Down
1 change: 1 addition & 0 deletions src/config/fileman_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RECODEX_WORKER_FILEMAN_CONFIG_H
#define RECODEX_WORKER_FILEMAN_CONFIG_H

#include <string>

/**
* Struct which stores informations which are usefull in file managers.
Expand Down
6 changes: 3 additions & 3 deletions src/config/log_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define RECODEX_WORKER_LOG_CONFIG_H

#include "spdlog/spdlog.h"

#include <string>

/**
* Structure which stores all information needed to initialize logger.
Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/config/sandbox_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RECODEX_WORKER_SANDBOX_CONFIG_H

#include <map>
#include <memory>
#include "sandbox_limits.h"


Expand Down Expand Up @@ -68,9 +69,7 @@ class sandbox_config
/**
* Constructor with defaults.
*/
sandbox_config()
{
}
sandbox_config() = default;
};

#endif // RECODEX_WORKER_SANDBOX_CONFIG_H
14 changes: 6 additions & 8 deletions src/config/sandbox_limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand All @@ -96,9 +96,7 @@ struct sandbox_limits {
/**
* Constructor with some defaults.
*/
sandbox_limits()
{
}
sandbox_limits() = default;

/**
* Insert environment variables which are not present yet.
Expand Down
10 changes: 5 additions & 5 deletions src/config/task_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> deps = {},
task_type type = task_type::INNER,
std::string cmd = "",
const std::string &cmd = "",
std::vector<std::string> args = {},
std::shared_ptr<sandbox_config> 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)
{
}
Expand All @@ -45,15 +45,15 @@ 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<std::string> dependencies;
/** Test id for external tasks */
std::string test_id;

/** 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;
Expand Down
11 changes: 3 additions & 8 deletions src/config/worker_config.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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<std::string>(), var.second.as<std::string>()));
limits_.environ_vars.emplace_back(var.first.as<std::string>(), var.second.as<std::string>());
}
} // no throw... can be omitted

Expand Down Expand Up @@ -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
{
Expand Down
Loading

0 comments on commit 34e3d06

Please sign in to comment.