From f4b8d1656a4433d1af9243fdb597ee72045f4b16 Mon Sep 17 00:00:00 2001 From: Philipp Storz Date: Thu, 22 Apr 2021 10:57:57 +0200 Subject: [PATCH 1/6] core: make "alist" and "dlist" type safe templates The two data structures "alist" and "dlist" that are used widely in the code are not type safe as they only handle void* pointers that are cast-ed from and to the required datatype manually. This is both error-prone and leads to code that is hard to understand. This commit changes the two types to templates that are type safe. As the usage of dlist in the chunked-device backend was totally misused so that the change to the template dlist was not possible, the old dlist still exists there as "old_dlist" which needs to be removed in a later step. --- core/src/cats/bdb_mysql.h | 1 + core/src/cats/bdb_postgresql.h | 1 + core/src/cats/bdb_sqlite.h | 1 + core/src/cats/bvfs.cc | 6 +- core/src/cats/cats.h | 8 +- core/src/cats/cats_backends.cc | 11 +- core/src/cats/mysql.cc | 16 +- core/src/cats/postgresql.cc | 12 +- core/src/cats/sqlite.cc | 10 +- core/src/console/console.cc | 10 +- core/src/dird/dir_plugins.cc | 16 +- core/src/dird/dir_plugins.h | 3 +- core/src/dird/dird.cc | 10 +- core/src/dird/dird.h | 16 +- core/src/dird/dird_conf.cc | 98 +++-- core/src/dird/dird_conf.h | 87 ++--- core/src/dird/fd_cmds.cc | 5 +- core/src/dird/jcr_private.h | 6 +- core/src/dird/job.cc | 5 +- core/src/dird/jobq.cc | 8 +- core/src/dird/jobq.h | 28 +- core/src/dird/migrate.cc | 20 +- core/src/dird/msgchan.cc | 6 +- core/src/dird/msgchan.h | 7 +- core/src/dird/ndmp_dma_storage.cc | 26 +- core/src/dird/ndmp_dma_storage.h | 12 +- core/src/dird/sd_cmds.cc | 14 +- core/src/dird/sd_cmds.h | 12 +- core/src/dird/socket_server.cc | 15 +- core/src/dird/socket_server.h | 6 +- core/src/dird/storage.cc | 44 ++- core/src/dird/storage.h | 17 +- core/src/dird/ua_acl.cc | 4 +- core/src/dird/ua_cmds.cc | 8 +- core/src/dird/ua_impexp.cc | 6 +- core/src/dird/ua_prune.cc | 6 +- core/src/dird/ua_run.cc | 4 +- core/src/dird/ua_select.cc | 9 +- core/src/dird/ua_select.h | 4 +- core/src/dird/ua_status.cc | 13 +- core/src/filed/dir_cmd.cc | 12 +- core/src/filed/fd_plugins.cc | 32 +- core/src/filed/fd_plugins.h | 3 +- core/src/filed/filed.cc | 8 +- core/src/filed/filed_conf.h | 42 ++- core/src/filed/jcr_private.h | 6 +- core/src/filed/restore.cc | 4 +- core/src/filed/restore.h | 4 +- core/src/filed/socket_server.cc | 8 +- core/src/filed/socket_server.h | 4 +- core/src/findlib/find.h | 38 +- core/src/findlib/xattr.cc | 59 +-- core/src/findlib/xattr.h | 9 +- core/src/include/jcr.h | 17 +- core/src/lib/address_conf.cc | 41 ++- core/src/lib/address_conf.h | 17 +- core/src/lib/alist.cc | 123 +------ core/src/lib/alist.h | 206 +++++++++-- core/src/lib/bnet.cc | 10 +- core/src/lib/bnet.h | 9 +- core/src/lib/bnet_server_tcp.cc | 31 +- core/src/lib/bnet_server_tcp.h | 12 +- core/src/lib/breg.cc | 12 +- core/src/lib/breg.h | 12 +- core/src/lib/bsock.cc | 6 +- core/src/lib/bsock.h | 5 +- core/src/lib/bsock_tcp.cc | 10 +- core/src/lib/connection_pool.cc | 8 +- core/src/lib/connection_pool.h | 6 +- core/src/lib/crypto.h | 10 +- core/src/lib/crypto_cache.cc | 10 +- core/src/lib/crypto_cache.h | 2 +- core/src/lib/crypto_none.cc | 7 +- core/src/lib/crypto_nss.cc | 7 +- core/src/lib/crypto_openssl.cc | 11 +- core/src/lib/dlink.h | 5 +- core/src/lib/dlist.cc | 285 +-------------- core/src/lib/dlist.h | 396 +++++++++++++++++++-- core/src/lib/guid_to_name.cc | 22 +- core/src/lib/guid_to_name.h | 8 +- core/src/lib/ini.cc | 6 +- core/src/lib/ini.h | 8 +- core/src/lib/jcr.cc | 11 +- core/src/lib/message_queue_item.h | 4 +- core/src/lib/mntent_cache.cc | 13 +- core/src/lib/mntent_cache.h | 2 +- core/src/lib/output_formatter.cc | 10 +- core/src/lib/output_formatter.h | 4 +- core/src/lib/output_formatter_resource.cc | 26 +- core/src/lib/output_formatter_resource.h | 8 +- core/src/lib/parse_conf_init_resource.cc | 19 +- core/src/lib/plugins.cc | 16 +- core/src/lib/plugins.h | 13 +- core/src/lib/res.cc | 50 ++- core/src/lib/resource_item.h | 20 +- core/src/lib/runscript.cc | 10 +- core/src/lib/runscript.h | 8 +- core/src/lib/watchdog.cc | 18 +- core/src/lib/watchdog.h | 2 +- core/src/plugins/filed/cephfs/cephfs-fd.cc | 12 +- core/src/plugins/filed/gfapi/gfapi-fd.cc | 20 +- core/src/stored/acquire.cc | 2 +- core/src/stored/autochanger_resource.cc | 7 +- core/src/stored/autochanger_resource.h | 11 +- core/src/stored/backends/CMakeLists.txt | 6 +- core/src/stored/backends/chunked_device.cc | 4 +- core/src/stored/backends/chunked_device.h | 3 +- core/src/stored/backends/old_dlink.h | 36 ++ core/src/stored/backends/old_dlist.cc | 336 +++++++++++++++++ core/src/stored/backends/old_dlist.h | 187 ++++++++++ core/src/stored/backends/ordered_cbuf.cc | 4 +- core/src/stored/backends/ordered_cbuf.h | 8 +- core/src/stored/bextract.cc | 8 +- core/src/stored/bsr.h | 6 +- core/src/stored/dev.h | 2 +- core/src/stored/dir_cmd.cc | 6 +- core/src/stored/jcr_private.h | 13 +- core/src/stored/ndmp_tape.cc | 15 +- core/src/stored/ndmp_tape.h | 5 +- core/src/stored/read_ctx.h | 2 +- core/src/stored/read_record.cc | 4 +- core/src/stored/record.h | 3 +- core/src/stored/reserve.cc | 24 +- core/src/stored/reserve.h | 3 +- core/src/stored/sd_plugins.cc | 16 +- core/src/stored/sd_plugins.h | 3 +- core/src/stored/sd_stats.cc | 162 ++++----- core/src/stored/socket_server.cc | 8 +- core/src/stored/socket_server.h | 6 +- core/src/stored/status.cc | 2 +- core/src/stored/stored_conf.h | 13 +- core/src/stored/vol_mgr.cc | 37 +- core/src/stored/vol_mgr.h | 7 +- core/src/tests/alist_test.cc | 12 +- core/src/tests/dlist_test.cc | 37 +- core/src/tests/test_config_parser_dir.cc | 5 +- core/src/win32/findlib/win32.cc | 7 +- 137 files changed, 2079 insertions(+), 1319 deletions(-) create mode 100644 core/src/stored/backends/old_dlink.h create mode 100644 core/src/stored/backends/old_dlist.cc create mode 100644 core/src/stored/backends/old_dlist.h diff --git a/core/src/cats/bdb_mysql.h b/core/src/cats/bdb_mysql.h index 9705a1b0248..9648de8a4d4 100644 --- a/core/src/cats/bdb_mysql.h +++ b/core/src/cats/bdb_mysql.h @@ -41,6 +41,7 @@ class JobControlRecord; class BareosDbMysql : public BareosDbPrivateInterface { private: + dlink link; /**< Queue control */ MYSQL* db_handle_; MYSQL instance_; MYSQL_RES* result_; diff --git a/core/src/cats/bdb_postgresql.h b/core/src/cats/bdb_postgresql.h index e5275a8cf4b..8a9e97bd66e 100644 --- a/core/src/cats/bdb_postgresql.h +++ b/core/src/cats/bdb_postgresql.h @@ -34,6 +34,7 @@ class JobControlRecord; class BareosDbPostgresql : public BareosDbPrivateInterface { private: + dlink link; /**< Queue control */ PGconn* db_handle_; PGresult* result_; POOLMEM* buf_; /**< Buffer to manipulate queries */ diff --git a/core/src/cats/bdb_sqlite.h b/core/src/cats/bdb_sqlite.h index df34b2cd606..a4e46c3012a 100644 --- a/core/src/cats/bdb_sqlite.h +++ b/core/src/cats/bdb_sqlite.h @@ -34,6 +34,7 @@ class JobControlRecord; class BareosDbSqlite : public BareosDbPrivateInterface { private: + dlink link; /**< Queue control */ struct sqlite3* db_handle_; char** result_; /**< sql_store_results() and SqlQueryWithoutHandler() */ char** diff --git a/core/src/cats/bvfs.cc b/core/src/cats/bvfs.cc index 5462ce90b5a..782fd9c0f8f 100644 --- a/core/src/cats/bvfs.cc +++ b/core/src/cats/bvfs.cc @@ -3,7 +3,7 @@ Copyright (C) 2009-2010 Free Software Foundation Europe e.V. Copyright (C) 2016-2016 Planets Communications B.V. - Copyright (C) 2016-2020 Bareos GmbH & Co. KG + Copyright (C) 2016-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -43,7 +43,7 @@ class pathid_cache { hlink* nodes; int nb_node; int max_node; - alist* table_node; + alist* table_node; htable* cache_ppathid; public: @@ -55,7 +55,7 @@ class pathid_cache { max_node = NITEMS; nodes = (hlink*)malloc(max_node * sizeof(hlink)); nb_node = 0; - table_node = new alist(5, owned_by_alist); + table_node = new alist(5, owned_by_alist); table_node->append(nodes); } diff --git a/core/src/cats/cats.h b/core/src/cats/cats.h index de5ca2cdbd0..d1b0d45aea9 100644 --- a/core/src/cats/cats.h +++ b/core/src/cats/cats.h @@ -42,7 +42,7 @@ #include #include - +template class dlist; /* import automatically generated SQL_QUERY */ @@ -492,7 +492,6 @@ typedef struct sql_field { class BareosDb : public BareosDbQueryEnum { protected: brwlock_t lock_; /**< Transaction lock */ - dlink link_; /**< Queue control */ SQL_INTERFACETYPE db_interface_type_ = SQL_INTERFACE_TYPE_UNKNOWN; /**< Type of backend used */ SQL_DBTYPE db_type_ = SQL_TYPE_UNKNOWN; /**< Database type */ @@ -1018,12 +1017,11 @@ struct SqlPoolEntry { time_t last_update = 0; /**< When was this connection last updated either used or put back on the pool */ BareosDb* db_handle = nullptr; /**< Connection handle to the database */ - dlink link; /**< list management */ + dlink link; /**< list management */ }; // Pooled backend list descriptor (one defined per backend defined in config) struct SqlPoolDescriptor { - dlist* pool_entries = nullptr; /**< Linked list of all pool entries */ bool active = false; /**< Is this an active pool, after a config reload an pool is made inactive */ time_t last_update = 0; /**< When was this pool last updated */ @@ -1040,7 +1038,7 @@ struct SqlPoolDescriptor { int validate_timeout = 0; /**< Number of seconds after which an idle connection should be validated */ int nr_connections = 0; /**< Number of active connections in the pool */ - dlink link; /**< list management */ + dlink link; /**< list management */ }; #include "include/jcr.h" diff --git a/core/src/cats/cats_backends.cc b/core/src/cats/cats_backends.cc index 1449094a4a3..243cd2a695e 100644 --- a/core/src/cats/cats_backends.cc +++ b/core/src/cats/cats_backends.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -52,7 +52,7 @@ static struct backend_interface_mapping_t { # define RTLD_NOW 2 # endif -static alist* loaded_backends = NULL; +static alist* loaded_backends = NULL; static std::vector backend_dirs; void DbSetBackendDirs(std::vector& new_backend_dirs) @@ -233,7 +233,8 @@ BareosDb* db_init_database(JobControlRecord* jcr, backend_shared_library->flush_backend = flush_backend; if (loaded_backends == NULL) { - loaded_backends = new alist(10, not_owned_by_alist); + loaded_backends + = new alist(10, not_owned_by_alist); } loaded_backends->append(backend_shared_library); @@ -294,5 +295,5 @@ BareosDb* db_init_database(JobControlRecord* jcr, void DbFlushBackends(void) {} # endif /* HAVE_DYNAMIC_CATS_BACKENDS */ -#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \ - HAVE_DBI */ +#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \ + HAVE_DBI */ diff --git a/core/src/cats/mysql.cc b/core/src/cats/mysql.cc index 257ae290dd7..f9eafaa96f6 100644 --- a/core/src/cats/mysql.cc +++ b/core/src/cats/mysql.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -53,7 +53,7 @@ */ // List of open databases -static dlist* db_list = NULL; +static dlist* db_list = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -120,7 +120,9 @@ BareosDbMysql::BareosDbMysql(JobControlRecord* jcr, result_ = NULL; // Put the db in the list. - if (db_list == NULL) { db_list = new dlist(this, &this->link_); } + if (db_list == NULL) { + db_list = new dlist(this, &this->link); + } db_list->append(this); /* make the queries available using the queries variable from the parent class @@ -397,9 +399,7 @@ bool BareosDbMysql::SqlQueryWithHandler(const char* query, break; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - if (exit_on_fatal_) { - Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); - } + if (exit_on_fatal_) { Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); } if (try_reconnect_ && !transaction_) { /* @@ -505,9 +505,7 @@ bool BareosDbMysql::SqlQueryWithoutHandler(const char* query, int flags) break; case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: - if (exit_on_fatal_) { - Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); - } + if (exit_on_fatal_) { Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); } if (try_reconnect_ && !transaction_) { /* diff --git a/core/src/cats/postgresql.cc b/core/src/cats/postgresql.cc index bcc1d0f5056..ae041dc0f72 100644 --- a/core/src/cats/postgresql.cc +++ b/core/src/cats/postgresql.cc @@ -3,7 +3,7 @@ Copyright (C) 2003-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -54,7 +54,7 @@ * ----------------------------------------------------------------------- */ -static dlist* db_list = NULL; +static dlist* db_list = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -126,7 +126,9 @@ BareosDbPostgresql::BareosDbPostgresql(JobControlRecord* jcr, result_ = NULL; // Put the db in the list. - if (db_list == NULL) { db_list = new dlist(this, &this->link_); } + if (db_list == NULL) { + db_list = new dlist(this, &this->link); + } db_list->append(this); /* make the queries available using the queries variable from the parent class @@ -633,9 +635,7 @@ bool BareosDbPostgresql::SqlQueryWithoutHandler(const char* query, int flags) break; case PGRES_FATAL_ERROR: Dmsg1(50, "Result status fatal: %s\n", query); - if (exit_on_fatal_) { - Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); - } + if (exit_on_fatal_) { Emsg0(M_ERROR_TERM, 0, "Fatal database error\n"); } if (try_reconnect_ && !transaction_) { /* diff --git a/core/src/cats/sqlite.cc b/core/src/cats/sqlite.cc index 3d316a40d18..cc5b03d5a75 100644 --- a/core/src/cats/sqlite.cc +++ b/core/src/cats/sqlite.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -50,7 +50,7 @@ * ----------------------------------------------------------------------- */ -static dlist* db_list = NULL; +static dlist* db_list = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -120,7 +120,9 @@ BareosDbSqlite::BareosDbSqlite(JobControlRecord* jcr, lowlevel_errmsg_ = NULL; // Put the db in the list. - if (db_list == NULL) { db_list = new dlist(this, &this->link_); } + if (db_list == NULL) { + db_list = new dlist(this, &this->link); + } db_list->append(this); /* make the queries available using the queries variable from the parent class @@ -664,7 +666,7 @@ BareosDb* db_init_database(JobControlRecord* jcr, bool need_private) # endif { - BareosDb* mdb = NULL; + BareosDbSqlite* mdb = NULL; P(mutex); /* lock DB queue */ diff --git a/core/src/console/console.cc b/core/src/console/console.cc index 63c88bec3da..2bb173cb90e 100644 --- a/core/src/console/console.cc +++ b/core/src/console/console.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -380,7 +380,7 @@ static char* get_previous_keyword(int current_point, int nb) } struct ItemList { - alist list; /* holds the completion list */ + alist list; /* holds the completion list */ }; static ItemList* items = NULL; @@ -464,8 +464,8 @@ static char* item_generator(const char* text, const char* item, cpl_item_t type) { - static int list_index, len; - char* name; + static std::size_t list_index, len; + const char* name; if (!state) { list_index = 0; @@ -1075,7 +1075,7 @@ int main(int argc, char* argv[]) TerminateConsole(0); return 1; #endif /* HAVE_PAM */ - } /* kMessageIdPamRequired */ + } /* kMessageIdPamRequired */ if (response_id == kMessageIdOk) { ConsoleOutput(response_args.JoinReadable().c_str()); diff --git a/core/src/dird/dir_plugins.cc b/core/src/dird/dir_plugins.cc index 0b79f78330a..06a62c822e6 100644 --- a/core/src/dird/dir_plugins.cc +++ b/core/src/dird/dir_plugins.cc @@ -3,7 +3,7 @@ Copyright (C) 2007-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -40,7 +40,7 @@ const char* plugin_type = "-dir.dll"; const char* plugin_type = "-dir.so"; #endif -static alist* dird_plugin_list; +static alist* dird_plugin_list; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* Forward referenced functions */ @@ -123,7 +123,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, bDirEvent* event, PluginContext* ctx, void* value, - alist* plugin_ctx_list, + alist* plugin_ctx_list, int* index, bRC* rc) { @@ -159,7 +159,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, * that moved back a position in the alist. */ if (index) { - UnloadPlugin(plugin_ctx_list, ctx->plugin, *index); + UnloadPlugin(dird_plugin_list, ctx->plugin, *index); *index = ((*index) - 1); } break; @@ -191,7 +191,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, { int i; bDirEvent event; - alist* plugin_ctx_list; + alist* plugin_ctx_list; bRC rc = bRC_OK; if (!dird_plugin_list) { @@ -269,7 +269,7 @@ static void DumpDirPlugins(FILE* fp) { DumpPlugins(dird_plugin_list, fp); } * This entry point is called internally by BAREOS to ensure * that the plugin IO calls come into this code. */ -void LoadDirPlugins(const char* plugin_dir, alist* plugin_names) +void LoadDirPlugins(const char* plugin_dir, alist* plugin_names) { Plugin* plugin; int i; @@ -280,7 +280,7 @@ void LoadDirPlugins(const char* plugin_dir, alist* plugin_names) return; } - dird_plugin_list = new alist(10, not_owned_by_alist); + dird_plugin_list = new alist(10, not_owned_by_alist); if (!LoadPlugins((void*)&bareos_plugin_interface_version, (void*)&bareos_core_functions, dird_plugin_list, plugin_dir, plugin_names, plugin_type, IsPluginCompatible)) { @@ -501,7 +501,7 @@ void NewPlugins(JobControlRecord* jcr) Dmsg1(debuglevel, "dir-plugin-list size=%d\n", num); if (num == 0) { return; } - jcr->plugin_ctx_list = new alist(10, owned_by_alist); + jcr->plugin_ctx_list = new alist(10, owned_by_alist); foreach_alist_index (i, plugin, dird_plugin_list) { // Start a new instance of each plugin instantiate_plugin(jcr, plugin, 0); diff --git a/core/src/dird/dir_plugins.h b/core/src/dird/dir_plugins.h index 4953781f82d..e78d769a644 100644 --- a/core/src/dird/dir_plugins.h +++ b/core/src/dird/dir_plugins.h @@ -47,6 +47,7 @@ #include "include/bc_types.h" #include "lib/plugins.h" +template class alist; namespace directordaemon { @@ -151,7 +152,7 @@ typedef struct s_dirbareosFuncs { // Bareos Core Routines -- not used within a plugin #ifdef DIRECTOR_DAEMON -void LoadDirPlugins(const char* plugin_dir, alist* plugin_names); +void LoadDirPlugins(const char* plugin_dir, alist* plugin_names); void UnloadDirPlugins(void); int ListDirPlugins(PoolMem& msg); void DispatchNewPluginOptions(JobControlRecord* jcr); diff --git a/core/src/dird/dird.cc b/core/src/dird/dird.cc index 8cc8f64d049..f20066dfc5e 100644 --- a/core/src/dird/dird.cc +++ b/core/src/dird/dird.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -92,7 +92,8 @@ void InitDeviceResources(); static char* runjob = NULL; static bool background = true; static bool test_config = false; -static alist* reload_table = NULL; +struct resource_table_reference; +static alist* reload_table = NULL; /* Globals Imported */ extern ResourceItem job_items[]; @@ -628,7 +629,10 @@ bool DoReloadConfig() Dmsg0(10, "Director's configuration file reread.\n"); if (num_running_jobs > 0) { - if (!reload_table) { reload_table = new alist(10, not_owned_by_alist); } + if (!reload_table) { + reload_table + = new alist(10, not_owned_by_alist); + } reload_table->push(new_table); } else { // no jobs running FreeSavedResources(&prev_config); diff --git a/core/src/dird/dird.h b/core/src/dird/dird.h index 8c95ff8fa72..aebfc994ca4 100644 --- a/core/src/dird/dird.h +++ b/core/src/dird/dird.h @@ -3,7 +3,7 @@ Copyright (C) 2000-2008 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -43,7 +43,7 @@ #include "include/jcr.h" #include "jobq.h" #include "ua.h" - +template class dlist; namespace directordaemon { @@ -139,7 +139,7 @@ enum s_mapping_type // Slot list definition /* clang-format off */ struct vol_list_t { - dlink link; /**< Link for list */ + dlink link; /**< Link for list */ slot_number_t element_address = kInvalidSlotNumber; /**< scsi element address */ slot_flags_t flags = 0; /**< Slot specific flags see e_slot_flag enum */ slot_type_t slot_type = slot_type_t::kSlotTypeUnknown; @@ -153,15 +153,15 @@ struct vol_list_t { /* clang-format on */ struct changer_vol_list_t { - int16_t reference_count{}; /**< Number of references to this vol_list */ - vol_list_type type{}; /**< Type of vol_list see vol_list_type enum */ - utime_t timestamp{}; /**< When was this vol_list created */ - dlist* contents{}; /**< Contents of autochanger */ + int16_t reference_count{}; /**< Number of references to this vol_list */ + vol_list_type type{}; /**< Type of vol_list see vol_list_type enum */ + utime_t timestamp{}; /**< When was this vol_list created */ + dlist* contents{}; /**< Contents of autochanger */ }; // Mapping from logical to physical storage address struct storage_mapping_t { - dlink link{}; /**< Link for list */ + dlink link{}; /**< Link for list */ slot_type_t slot_type{slot_type_t::kSlotTypeUnknown}; /**< See slot_type_* */ slot_number_t element_address{}; /**< scsi element address */ slot_number_t Slot{}; /**< Drive number when kSlotTypeDrive diff --git a/core/src/dird/dird_conf.cc b/core/src/dird/dird_conf.cc index f186d8f9690..8574f6d1590 100644 --- a/core/src/dird/dird_conf.cc +++ b/core/src/dird/dird_conf.cc @@ -1033,7 +1033,7 @@ static void PropagateResource(ResourceItem* items, for (int i = 0; items[i].name; i++) { if (!BitIsSet(i, dest->item_present_) && BitIsSet(i, source->item_present_)) { - offset = items[i].offset; // Ueb + offset = items[i].offset; switch (items[i].type) { case CFG_TYPE_STR: case CFG_TYPE_DIR: @@ -1065,16 +1065,18 @@ static void PropagateResource(ResourceItem* items, } case CFG_TYPE_ALIST_STR: { const char* str = nullptr; - alist *orig_list, **new_list; + alist*orig_list, **new_list; // Handle alist strings - orig_list = *(alist**)((char*)(source) + offset); + orig_list = *(alist**)((char*)(source) + offset); // See if there is anything on the list. if (orig_list && orig_list->size()) { - new_list = (alist**)((char*)(dest) + offset); + new_list = (alist**)((char*)(dest) + offset); - if (!*new_list) { *new_list = new alist(10, owned_by_alist); } + if (!*new_list) { + *new_list = new alist(10, owned_by_alist); + } foreach_alist (str, orig_list) { (*new_list)->append(strdup(str)); @@ -1087,16 +1089,18 @@ static void PropagateResource(ResourceItem* items, } case CFG_TYPE_ALIST_RES: { BareosResource* res = nullptr; - alist *orig_list, **new_list; + alist*orig_list, **new_list; // Handle alist resources - orig_list = *(alist**)((char*)(source) + offset); + orig_list = *(alist**)((char*)(source) + offset); // See if there is anything on the list. if (orig_list && orig_list->size()) { - new_list = (alist**)((char*)(dest) + offset); + new_list = (alist**)((char*)(dest) + offset); - if (!*new_list) { *new_list = new alist(10, not_owned_by_alist); } + if (!*new_list) { + *new_list = new alist(10, not_owned_by_alist); + } foreach_alist (res, orig_list) { (*new_list)->append(res); @@ -1109,16 +1113,20 @@ static void PropagateResource(ResourceItem* items, } case CFG_TYPE_ACL: { const char* str = nullptr; - alist *orig_list, **new_list; + alist*orig_list, **new_list; // Handle ACL lists. - orig_list = ((alist**)((char*)(source) + offset))[items[i].code]; + orig_list = ((alist**)((char*)(source) + + offset))[items[i].code]; // See if there is anything on the list. if (orig_list && orig_list->size()) { - new_list = &(((alist**)((char*)(dest) + offset))[items[i].code]); + new_list = &(( + (alist**)((char*)(dest) + offset))[items[i].code]); - if (!*new_list) { *new_list = new alist(10, owned_by_alist); } + if (!*new_list) { + *new_list = new alist(10, owned_by_alist); + } foreach_alist (str, orig_list) { (*new_list)->append(strdup(str)); @@ -1305,7 +1313,7 @@ static void PrintConfigRunscript(OutputFormatterResource& send, { if (!Bstrcasecmp(item.name, "runscript")) { return; } - alist* list = GetItemVariable(item); + alist* list = GetItemVariable*>(item); if ((!list) or (list->empty())) { return; } send.ArrayStart(item.name, inherited, ""); @@ -2438,7 +2446,7 @@ bool PropagateJobdefs(int res_type, JobResource* res) // Handle RunScripts alists specifically if (jobdefs->RunScripts) { if (!res->RunScripts) { - res->RunScripts = new alist(10, not_owned_by_alist); + res->RunScripts = new alist(10, not_owned_by_alist); } RunScript* rs = nullptr; @@ -2806,14 +2814,15 @@ static void StoreAutopassword(LEX* lc, ResourceItem* item, int index, int pass) static void StoreAcl(LEX* lc, ResourceItem* item, int index, int pass) { - alist** alistvalue = GetItemVariablePointer(*item); + alist** alistvalue + = GetItemVariablePointer**>(*item); if (pass == 1) { if (!alistvalue[item->code]) { - alistvalue[item->code] = new alist(10, owned_by_alist); + alistvalue[item->code] = new alist(10, owned_by_alist); Dmsg1(900, "Defined new ACL alist at %d\n", item->code); } } - alist* list = alistvalue[item->code]; + alist* list = alistvalue[item->code]; std::vector msg(256); int token = BCT_COMMA; while (token == BCT_COMMA) { @@ -2835,12 +2844,15 @@ static void StoreAcl(LEX* lc, ResourceItem* item, int index, int pass) static void StoreAudit(LEX* lc, ResourceItem* item, int index, int pass) { int token; - alist* list; + alist* list; - alist** alistvalue = GetItemVariablePointer(*item); + alist** alistvalue + = GetItemVariablePointer**>(*item); if (pass == 1) { - if (!*alistvalue) { *alistvalue = new alist(10, owned_by_alist); } + if (!*alistvalue) { + *alistvalue = new alist(10, owned_by_alist); + } } list = *alistvalue; @@ -2925,7 +2937,8 @@ static void StoreShortRunscript(LEX* lc, int pass) { LexGetToken(lc, BCT_STRING); - alist** runscripts = GetItemVariablePointer(*item); + alist** runscripts + = GetItemVariablePointer**>(*item); if (pass == 2) { Dmsg0(500, "runscript: creating new RunScript object\n"); @@ -2963,7 +2976,9 @@ static void StoreShortRunscript(LEX* lc, // Remember that the entry was configured in the short runscript form. script->short_form = true; - if (!*runscripts) { *runscripts = new alist(10, not_owned_by_alist); } + if (!*runscripts) { + *runscripts = new alist(10, not_owned_by_alist); + } (*runscripts)->append(script); script->Debug(); @@ -3062,8 +3077,11 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass) } if (pass == 2) { - alist** runscripts = GetItemVariablePointer(*item); - if (!*runscripts) { *runscripts = new alist(10, not_owned_by_alist); } + alist** runscripts + = GetItemVariablePointer**>(*item); + if (!*runscripts) { + *runscripts = new alist(10, not_owned_by_alist); + } res_runscript->SetJobCodeCallback(job_code_callback_director); @@ -3299,8 +3317,8 @@ static bool HasDefaultValue(ResourceItem& item, s_jl* keywords) return is_default; } - -static bool HasDefaultValue(ResourceItem& item, alist* values) +template +static bool HasDefaultValue(ResourceItem& item, alist* values) { if (item.flags & CFG_ITEM_DEFAULT) { if ((values->size() == 1) @@ -3316,7 +3334,7 @@ static bool HasDefaultValue(ResourceItem& item, alist* values) static bool HasDefaultValueAlistConstChar(ResourceItem& item) { - alist* values = GetItemVariable(item); + alist* values = GetItemVariable*>(item); return HasDefaultValue(item, values); } @@ -3335,7 +3353,7 @@ static bool HasDefaultValue(ResourceItem& item) /* this should not happen */ is_default = false; } else { - is_default = (GetItemVariable(item) == NULL); + is_default = (GetItemVariable*>(item) == NULL); } break; } @@ -3346,8 +3364,9 @@ static bool HasDefaultValue(ResourceItem& item) */ break; case CFG_TYPE_ACL: { - alist** alistvalue = GetItemVariablePointer(item); - alist* list = alistvalue[item.code]; + alist** alistvalue + = GetItemVariablePointer**>(item); + alist* list = alistvalue[item.code]; is_default = HasDefaultValue(item, list); break; } @@ -3393,7 +3412,8 @@ static bool HasDefaultValue(ResourceItem& item) break; } case CFG_TYPE_AUDIT: { - is_default = HasDefaultValue(item, GetItemVariable(item)); + is_default + = HasDefaultValue(item, GetItemVariable*>(item)); break; } case CFG_TYPE_POOLTYPE: @@ -3441,8 +3461,9 @@ static void PrintConfigCb(ResourceItem& item, switch (item.type) { case CFG_TYPE_DEVICE: { // Each member of the list is comma-separated - send.KeyMultipleStringsInOneLine(item.name, GetItemVariable(item), - GetResourceName, false, true); + send.KeyMultipleStringsInOneLine( + item.name, GetItemVariable*>(item), + GetResourceName, false, true); break; } case CFG_TYPE_RUNSCRIPT: @@ -3456,8 +3477,9 @@ static void PrintConfigCb(ResourceItem& item, */ break; case CFG_TYPE_ACL: { - alist** alistvalue = GetItemVariablePointer(item); - alist* list = alistvalue[item.code]; + alist** alistvalue + = GetItemVariablePointer**>(item); + alist* list = alistvalue[item.code]; send.KeyMultipleStringsInOneLine(item.name, list, inherited); break; } @@ -3527,8 +3549,8 @@ static void PrintConfigCb(ResourceItem& item, } case CFG_TYPE_AUDIT: { // Each member of the list is comma-separated - send.KeyMultipleStringsInOneLine(item.name, GetItemVariable(item), - inherited); + send.KeyMultipleStringsInOneLine( + item.name, GetItemVariable*>(item), inherited); break; } case CFG_TYPE_POOLTYPE: diff --git a/core/src/dird/dird_conf.h b/core/src/dird/dird_conf.h index eb898be1cac..508338767e2 100644 --- a/core/src/dird/dird_conf.h +++ b/core/src/dird/dird_conf.h @@ -35,8 +35,11 @@ #include "lib/resource_item.h" #include "lib/tls_conf.h" +template class dlist; struct json_t; +class RunScript; +class IPADDR; namespace directordaemon { @@ -108,16 +111,15 @@ class DirectorResource public: DirectorResource() = default; virtual ~DirectorResource() = default; - - dlist* DIRaddrs = nullptr; - dlist* DIRsrc_addr = nullptr; /* Address to source connections from */ - char* query_file = nullptr; /* SQL query file */ - char* working_directory = nullptr; /* WorkingDirectory */ - char* scripts_directory = nullptr; /* ScriptsDirectory */ - char* plugin_directory = nullptr; /* Plugin Directory */ - alist* plugin_names = nullptr; /* Plugin names to load */ - char* pid_directory = nullptr; /* PidDirectory */ - char* subsys_directory = nullptr; /* SubsysDirectory */ + dlist* DIRaddrs = nullptr; + dlist* DIRsrc_addr = nullptr; /* Address to source connections from */ + char* query_file = nullptr; /* SQL query file */ + char* working_directory = nullptr; /* WorkingDirectory */ + char* scripts_directory = nullptr; /* ScriptsDirectory */ + char* plugin_directory = nullptr; /* Plugin Directory */ + alist* plugin_names = nullptr; /* Plugin names to load */ + char* pid_directory = nullptr; /* PidDirectory */ + char* subsys_directory = nullptr; /* SubsysDirectory */ std::vector backend_directories; MessagesResource* messages = nullptr; /* Daemon message handler */ uint32_t MaxConcurrentJobs = 0; /* Max concurrent jobs for whole director */ @@ -134,7 +136,8 @@ class DirectorResource dumping the config */ bool ndmp_snooping = false; /* NDMP Protocol specific snooping enabled */ bool auditing = false; /* Auditing enabled */ - alist* audit_events = nullptr; /* Specific audit events to enable */ + alist* audit_events + = nullptr; /* Specific audit events to enable */ uint32_t ndmp_loglevel = 0; /* NDMP Protocol specific loglevel to use */ uint32_t subscriptions = 0; /* Number of subscribtions available */ uint32_t subscriptions_used = 0; /* Number of subscribtions used */ @@ -204,13 +207,13 @@ class ProfileResource : public BareosResource { ProfileResource() = default; virtual ~ProfileResource() = default; - alist* ACL_lists[Num_ACL] = {0}; /**< Pointers to ACLs */ + alist* ACL_lists[Num_ACL] = {0}; /**< Pointers to ACLs */ }; struct UserAcl { BareosResource* corresponding_resource = nullptr; - alist* ACL_lists[Num_ACL] = {0}; /**< Pointers to ACLs */ - alist* profiles = nullptr; /**< Pointers to profile resources */ + alist* ACL_lists[Num_ACL] = {0}; /**< Pointers to ACLs */ + alist* profiles = nullptr; /**< Pointers to profile resources */ }; // Console Resource @@ -341,7 +344,8 @@ class StorageResource char* media_type = nullptr; /**< Media Type provided by this Storage */ char* ndmp_changer_device = nullptr; /**< If DIR controls storage directly (NDMP_NATIVE) changer device used */ - alist* device = nullptr; /**< Alternate devices for this Storage */ + alist* device + = nullptr; /**< Alternate devices for this Storage */ int32_t MaxConcurrentJobs = 0; /**< Maximum concurrent jobs */ int32_t MaxConcurrentReadJobs = 0; /**< Maximum concurrent jobs reading */ bool enabled = false; /**< Set if device is enabled */ @@ -460,7 +464,7 @@ class JobResource : public BareosResource { ClientResource* client = nullptr; /**< Who to backup */ FilesetResource* fileset = nullptr; /**< What to backup -- Fileset */ CatalogResource* catalog = nullptr; /**< Which Catalog to use */ - alist* storage = nullptr; /**< Where is device -- list of Storage to be used */ + alist* storage = nullptr; /**< Where is device -- list of Storage to be used */ PoolResource* pool = nullptr; /**< Where is media -- Media Pool */ PoolResource* full_pool = nullptr; /**< Pool for Full backups */ PoolResource* vfull_pool = nullptr; /**< Pool for Virtual Full backups */ @@ -470,12 +474,12 @@ class JobResource : public BareosResource { char* selection_pattern = nullptr; JobResource* verify_job = nullptr; /**< Job name to verify */ JobResource* jobdefs = nullptr; /**< Job defaults */ - alist* run_cmds = nullptr; /**< Run commands */ - alist* RunScripts = nullptr; /**< Run {client} program {after|before} Job */ - alist* FdPluginOptions = nullptr; /**< Generic FD plugin options used by this Job */ - alist* SdPluginOptions = nullptr; /**< Generic SD plugin options used by this Job */ - alist* DirPluginOptions = nullptr; /**< Generic DIR plugin options used by this Job */ - alist* base = nullptr; /**< Base jobs */ + alist* run_cmds = nullptr; /**< Run commands */ + alist* RunScripts = nullptr; /**< Run {client} program {after|before} Job */ + alist* FdPluginOptions = nullptr; /**< Generic FD plugin options used by this Job */ + alist* SdPluginOptions = nullptr; /**< Generic SD plugin options used by this Job */ + alist* DirPluginOptions = nullptr; /**< Generic DIR plugin options used by this Job */ + alist* base = nullptr; /**< Base jobs */ bool allow_mixed_priority = false; /**< Allow jobs with higher priority concurrently with this */ bool where_use_regexp = false; /**< true if RestoreWhere is a BareosRegex */ @@ -517,21 +521,21 @@ struct FileOptions { FileOptions() = default; virtual ~FileOptions() = default; - char opts[MAX_FOPTS] = {0}; /**< Options string */ - alist regex; /**< Regex string(s) */ - alist regexdir; /**< Regex string(s) for directories */ - alist regexfile; /**< Regex string(s) for files */ - alist wild; /**< Wild card strings */ - alist wilddir; /**< Wild card strings for directories */ - alist wildfile; /**< Wild card strings for files */ - alist wildbase; /**< Wild card strings for files without '/' */ - alist base; /**< List of base names */ - alist fstype; /**< File system type limitation */ - alist Drivetype; /**< Drive type limitation */ - alist meta; /**< Backup meta information */ - char* reader = nullptr; /**< Reader program */ - char* writer = nullptr; /**< Writer program */ - char* plugin = nullptr; /**< Plugin program */ + char opts[MAX_FOPTS] = {0}; /**< Options string */ + alist regex; /**< Regex string(s) */ + alist regexdir; /**< Regex string(s) for directories */ + alist regexfile; /**< Regex string(s) for files */ + alist wild; /**< Wild card strings */ + alist wilddir; /**< Wild card strings for directories */ + alist wildfile; /**< Wild card strings for files */ + alist wildbase; /**< Wild card strings for files without '/' */ + alist base; /**< List of base names */ + alist fstype; /**< File system type limitation */ + alist Drivetype; /**< Drive type limitation */ + alist meta; /**< Backup meta information */ + char* reader = nullptr; /**< Reader program */ + char* writer = nullptr; /**< Writer program */ + char* plugin = nullptr; /**< Plugin program */ }; // This is either an include item or an exclude item @@ -542,9 +546,9 @@ class IncludeExcludeItem { FileOptions* current_opts = nullptr; std::vector file_options_list; - alist name_list; /**< Filename list -- holds char * */ - alist plugin_list; /**< Filename list for plugins */ - alist ignoredir; /**< Ignoredir string */ + alist name_list; /**< Filename list -- holds char * */ + alist plugin_list; /**< Filename list for plugins */ + alist ignoredir; /**< Ignoredir string */ }; // FileSet Resource @@ -617,7 +621,8 @@ class PoolResource : public BareosResource { uint64_t MigrationHighBytes = 0; /* When migration starts */ uint64_t MigrationLowBytes = 0; /* When migration stops */ PoolResource* NextPool = nullptr; /* Next pool for migration */ - alist* storage = nullptr; /* Where is device -- list of Storage to be used */ + alist* storage + = nullptr; /* Where is device -- list of Storage to be used */ bool use_catalog = false; /* Maintain catalog for media */ bool catalog_files = false; /* Maintain file entries in catalog */ bool use_volume_once = false; /* Write on volume only once */ diff --git a/core/src/dird/fd_cmds.cc b/core/src/dird/fd_cmds.cc index 3ccb34ff8dc..9dfc549345a 100644 --- a/core/src/dird/fd_cmds.cc +++ b/core/src/dird/fd_cmds.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -49,6 +49,7 @@ #include "lib/bnet.h" #include "lib/edit.h" #include "lib/parse_conf.h" +#include "lib/runscript.h" #include "lib/util.h" #include "lib/watchdog.h" @@ -787,7 +788,7 @@ bool SendIncludeList(JobControlRecord* jcr) bool SendExcludeList(JobControlRecord* jcr) { return true; } // This checks to see if there are any non local runscripts for this job. -static bool HaveClientRunscripts(alist* RunScripts) +static bool HaveClientRunscripts(alist* RunScripts) { RunScript* cmd = nullptr; bool retval = false; diff --git a/core/src/dird/jcr_private.h b/core/src/dird/jcr_private.h index d5d6d801490..f736155eeb0 100644 --- a/core/src/dird/jcr_private.h +++ b/core/src/dird/jcr_private.h @@ -81,9 +81,9 @@ struct Resources { POOLMEM* rstore_source{}; /**< Where read storage came from */ POOLMEM* wstore_source{}; /**< Where write storage came from */ POOLMEM* catalog_source{}; /**< Where catalog came from */ - alist* read_storage_list{}; /**< Read storage possibilities */ - alist* write_storage_list{}; /**< Write storage possibilities */ - alist* paired_read_write_storage_list{}; /**< Paired storage possibilities (saved write_storage_list or read_storage_list) */ + alist* read_storage_list{}; /**< Read storage possibilities */ + alist* write_storage_list{}; /**< Write storage possibilities */ + alist* paired_read_write_storage_list{}; /**< Paired storage possibilities (saved write_storage_list or read_storage_list) */ bool run_pool_override{}; /**< Pool override was given on run cmdline */ bool run_full_pool_override{}; /**< Full pool override was given on run cmdline */ bool run_vfull_pool_override{}; /**< Virtual Full pool override was given on run cmdline */ diff --git a/core/src/dird/job.cc b/core/src/dird/job.cc index e4e5060b092..784184089d6 100644 --- a/core/src/dird/job.cc +++ b/core/src/dird/job.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -466,7 +466,8 @@ static void* job_thread(void* arg) // TODO : check if it is used somewhere if (jcr->impl->res.job->RunScripts == NULL) { Dmsg0(200, "Warning, job->RunScripts is empty\n"); - jcr->impl->res.job->RunScripts = new alist(10, not_owned_by_alist); + jcr->impl->res.job->RunScripts + = new alist(10, not_owned_by_alist); } if (!jcr->db->UpdateJobStartRecord(jcr, &jcr->impl->jr)) { diff --git a/core/src/dird/jobq.cc b/core/src/dird/jobq.cc index 2fea074b4bd..0ef18ed9a4a 100644 --- a/core/src/dird/jobq.cc +++ b/core/src/dird/jobq.cc @@ -3,7 +3,7 @@ Copyright (C) 2003-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -102,9 +102,9 @@ int JobqInit(jobq_t* jq, int max_workers, void* (*engine)(void* arg)) jq->valid = JOBQ_VALID; // Initialize the job queues - jq->waiting_jobs = new dlist(item, &item->link); - jq->running_jobs = new dlist(item, &item->link); - jq->ready_jobs = new dlist(item, &item->link); + jq->waiting_jobs = new dlist(item, &item->link); + jq->running_jobs = new dlist(item, &item->link); + jq->ready_jobs = new dlist(item, &item->link); return 0; } diff --git a/core/src/dird/jobq.h b/core/src/dird/jobq.h index 914f1d5718c..e1923563231 100644 --- a/core/src/dird/jobq.h +++ b/core/src/dird/jobq.h @@ -33,29 +33,33 @@ #ifndef BAREOS_DIRD_JOBQ_H_ #define BAREOS_DIRD_JOBQ_H_ +#include "lib/dlink.h" +#include "include/jcr.h" + +template class dlist; namespace directordaemon { // Structure to keep track of job queue request struct jobq_item_t { - dlink link; + dlink link; JobControlRecord* jcr; }; // Structure describing a work queue struct jobq_t { - pthread_mutex_t mutex; /* queue access control */ - pthread_cond_t work; /* wait for work */ - pthread_attr_t attr; /* create detached threads */ - dlist* waiting_jobs; /* list of jobs waiting */ - dlist* running_jobs; /* jobs running */ - dlist* ready_jobs; /* jobs ready to run */ - int valid; /* queue initialized */ - bool quit; /* jobq should quit */ - int max_workers; /* max threads */ - int num_workers; /* current threads */ - void* (*engine)(void* arg); /* user engine */ + pthread_mutex_t mutex; /* queue access control */ + pthread_cond_t work; /* wait for work */ + pthread_attr_t attr; /* create detached threads */ + dlist* waiting_jobs; /* list of jobs waiting */ + dlist* running_jobs; /* jobs running */ + dlist* ready_jobs; /* jobs ready to run */ + int valid; /* queue initialized */ + bool quit; /* jobq should quit */ + int max_workers; /* max threads */ + int num_workers; /* current threads */ + void* (*engine)(void* arg); /* user engine */ }; #define JOBQ_VALID 0xdec1993 diff --git a/core/src/dird/migrate.cc b/core/src/dird/migrate.cc index f3f82cde443..6c42f7e30c7 100644 --- a/core/src/dird/migrate.cc +++ b/core/src/dird/migrate.cc @@ -479,21 +479,21 @@ static int UniqueDbidHandler(void* ctx, int num_fields, char** row) } struct uitem { - dlink link; + dlink link; char* item; }; -static int ItemCompare(void* item1, void* item2) +static int ItemCompare(uitem* item1, uitem* item2) { - uitem* i1 = (uitem*)item1; - uitem* i2 = (uitem*)item2; + uitem* i1 = item1; + uitem* i2 = item2; return strcmp(i1->item, i2->item); } static int UniqueNameHandler(void* ctx, int num_fields, char** row) { - dlist* list = (dlist*)ctx; + dlist* list = (dlist*)ctx; uitem* new_item = (uitem*)malloc(sizeof(uitem)); new (new_item) uitem(); @@ -502,7 +502,7 @@ static int UniqueNameHandler(void* ctx, int num_fields, char** row) new_item->item = strdup(row[0]); Dmsg1(dbglevel, "Unique_name_hdlr Item=%s\n", row[0]); - item = (uitem*)list->binary_insert((void*)new_item, ItemCompare); + item = list->binary_insert(new_item, ItemCompare); if (item != new_item) { /* already in list */ free(new_item->item); free((char*)new_item); @@ -618,7 +618,7 @@ static bool regex_find_jobids(JobControlRecord* jcr, const char* query2, const char* type) { - dlist* item_chain; + dlist* item_chain; uitem* item = NULL; uitem* last_item = NULL; regex_t preg{}; @@ -627,7 +627,7 @@ static bool regex_find_jobids(JobControlRecord* jcr, bool ok = false; PoolMem query(PM_MESSAGE); - item_chain = new dlist(item, &item->link); + item_chain = new dlist(item, &item->link); if (!jcr->impl->res.job->selection_pattern) { Jmsg(jcr, M_FATAL, 0, _("No %s %s selection pattern specified.\n"), jcr->get_OperationName(), type); @@ -1301,7 +1301,7 @@ static inline bool DoActualMigration(JobControlRecord* jcr) ->resource_name_); if (jcr->impl->remote_replicate) { - alist* write_storage_list; + alist* write_storage_list; /* * See if we need to apply any bandwidth limiting. @@ -1509,7 +1509,7 @@ static inline bool DoActualMigration(JobControlRecord* jcr) bail_out: if (jcr->impl->remote_replicate && mig_jcr) { - alist* write_storage_list; + alist* write_storage_list; // Swap the write_storage_list between the jcr and the mig_jcr. write_storage_list = mig_jcr->impl->res.write_storage_list; diff --git a/core/src/dird/msgchan.cc b/core/src/dird/msgchan.cc index 138491447e8..7a0fdd1067a 100644 --- a/core/src/dird/msgchan.cc +++ b/core/src/dird/msgchan.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2009 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -120,8 +120,8 @@ static inline bool SendBootstrapFileToSd(JobControlRecord* jcr, /** Start a job with the Storage daemon */ bool StartStorageDaemonJob(JobControlRecord* jcr, - alist* read_storage, - alist* write_storage, + alist* read_storage, + alist* write_storage, bool send_bsr) { bool ok = true; diff --git a/core/src/dird/msgchan.h b/core/src/dird/msgchan.h index 8fc4f41d018..642cb089d3b 100644 --- a/core/src/dird/msgchan.h +++ b/core/src/dird/msgchan.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -22,13 +22,14 @@ #ifndef BAREOS_DIRD_MSGCHAN_H_ #define BAREOS_DIRD_MSGCHAN_H_ +template class alist; namespace directordaemon { bool StartStorageDaemonJob(JobControlRecord* jcr, - alist* read_storage, - alist* write_storage, + alist* read_storage, + alist* write_storage, bool send_bsr = false); bool StartStorageDaemonMessageThread(JobControlRecord* jcr); int BgetDirmsg(BareosSocket* bs, bool allow_any_msg = false); diff --git a/core/src/dird/ndmp_dma_storage.cc b/core/src/dird/ndmp_dma_storage.cc index c02bde04939..f48430c8848 100644 --- a/core/src/dird/ndmp_dma_storage.cc +++ b/core/src/dird/ndmp_dma_storage.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2015 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -418,20 +418,20 @@ static void NdmpFillStorageMappings(StorageResource* store, } // Get the current content of the autochanger as a generic vol_list dlist. -dlist* ndmp_get_vol_list(UaContext* ua, - StorageResource* store, - bool listall, - bool scan) +dlist* ndmp_get_vol_list(UaContext* ua, + StorageResource* store, + bool listall, + bool scan) { struct ndm_session* ndmp_sess; struct smc_ctrl_block* smc; struct smc_element_descriptor* edp; vol_list_t* vl = NULL; - dlist* vol_list = NULL; + dlist* vol_list = NULL; ua->WarningMsg(_("get ndmp_vol_list...\n")); if (!GetRobotElementStatus(ua->jcr, store, &ndmp_sess)) { - return (dlist*)NULL; + return (dlist*)NULL; } /* @@ -441,7 +441,7 @@ dlist* ndmp_get_vol_list(UaContext* ua, NdmpFillStorageMappings(store, ndmp_sess); // Start with an empty dlist(). - vol_list = new dlist(vl, &vl->link); + vol_list = new dlist(vl, &vl->link); // Process the robot element status retrieved. smc = ndmp_sess->control_acb->smc_cb; @@ -1093,13 +1093,13 @@ void DoNdmpStorageStatus(UaContext* ua, StorageResource* store, char* cmd) Jmsg(ua->jcr, M_FATAL, 0, _("NDMP protocol not supported\n")); } -dlist* ndmp_get_vol_list(UaContext* ua, - StorageResource* store, - bool listall, - bool scan) +dlist* ndmp_get_vol_list(UaContext* ua, + StorageResource* store, + bool listall, + bool scan) { Jmsg(ua->jcr, M_FATAL, 0, _("NDMP protocol not supported\n")); - return (dlist*)NULL; + return (dlist*)NULL; } slot_number_t NdmpGetNumSlots(UaContext* ua, StorageResource* store) diff --git a/core/src/dird/ndmp_dma_storage.h b/core/src/dird/ndmp_dma_storage.h index 3ba5e05ebe4..5b51cafdef7 100644 --- a/core/src/dird/ndmp_dma_storage.h +++ b/core/src/dird/ndmp_dma_storage.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -23,15 +23,17 @@ #define BAREOS_DIRD_NDMP_DMA_STORAGE_H_ struct ndm_job_param; +template class dlist; +struct vol_list_t; namespace directordaemon { void DoNdmpStorageStatus(UaContext* ua, StorageResource* store, char* cmd); -dlist* ndmp_get_vol_list(UaContext* ua, - StorageResource* store, - bool listall, - bool scan); +dlist* ndmp_get_vol_list(UaContext* ua, + StorageResource* store, + bool listall, + bool scan); slot_number_t NdmpGetNumSlots(UaContext* ua, StorageResource* store); drive_number_t NdmpGetNumDrives(UaContext* ua, StorageResource* store); bool NdmpTransferVolume(UaContext* ua, diff --git a/core/src/dird/sd_cmds.cc b/core/src/dird/sd_cmds.cc index ef10386aa94..72339d8c49e 100644 --- a/core/src/dird/sd_cmds.cc +++ b/core/src/dird/sd_cmds.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -252,17 +252,17 @@ char* get_volume_name_from_SD(UaContext* ua, * * If a drive is loaded, the slot *should* be empty */ -dlist* native_get_vol_list(UaContext* ua, - StorageResource* store, - bool listall, - bool scan) +dlist* native_get_vol_list(UaContext* ua, + StorageResource* store, + bool listall, + bool scan) { int nr_fields; char* bp; char dev_name[MAX_NAME_LENGTH]; char *field1, *field2, *field3, *field4, *field5; vol_list_t* vl = nullptr; - dlist* vol_list; + dlist* vol_list; BareosSocket* sd = nullptr; ua->jcr->impl->res.write_storage = store; @@ -278,7 +278,7 @@ dlist* native_get_vol_list(UaContext* ua, sd->fsend(changerlistcmd, dev_name); } - vol_list = new dlist(vl, &vl->link); + vol_list = new dlist(vl, &vl->link); // Read and organize list of Volumes while (BnetRecv(sd) >= 0) { diff --git a/core/src/dird/sd_cmds.h b/core/src/dird/sd_cmds.h index b75ab336a78..b81da26c9f5 100644 --- a/core/src/dird/sd_cmds.h +++ b/core/src/dird/sd_cmds.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -23,12 +23,14 @@ #define BAREOS_DIRD_SD_CMDS_H_ class JobControlRecord; +template class dlist; namespace directordaemon { class UaContext; class StorageResource; +struct vol_list_t; bool ConnectToStorageDaemon(JobControlRecord* jcr, int retry_interval, @@ -39,10 +41,10 @@ void CloseSdBsock(UaContext* ua); char* get_volume_name_from_SD(UaContext* ua, slot_number_t Slot, drive_number_t drive); -dlist* native_get_vol_list(UaContext* ua, - StorageResource* store, - bool listall, - bool scan); +dlist* native_get_vol_list(UaContext* ua, + StorageResource* store, + bool listall, + bool scan); slot_number_t NativeGetNumSlots(UaContext* ua, StorageResource* store); drive_number_t NativeGetNumDrives(UaContext* ua, StorageResource* store); bool CancelStorageDaemonJob(UaContext* ua, StorageResource* store, char* JobId); diff --git a/core/src/dird/socket_server.cc b/core/src/dird/socket_server.cc index a66e0bebe7f..6764c76f965 100644 --- a/core/src/dird/socket_server.cc +++ b/core/src/dird/socket_server.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2007 Free Software Foundation Europe e.V. - Copyright (C) 2014-2020 Bareos GmbH & Co. KG + Copyright (C) 2014-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -50,7 +50,7 @@ static char hello_client[] = "Hello Client %127s calling"; /* Global variables */ static ThreadList thread_list; -static alist* sock_fds = NULL; +static alist* sock_fds = NULL; static pthread_t tcp_server_tid; static ConnectionPool* client_connections = NULL; @@ -135,10 +135,11 @@ extern "C" void* connect_thread(void* arg) SetJcrInThreadSpecificData(nullptr); // Permit MaxConnections connections. - sock_fds = new alist(10, not_owned_by_alist); - BnetThreadServerTcp((dlist*)arg, me->MaxConnections, sock_fds, thread_list, - HandleConnectionRequest, my_config, &server_state, - UserAgentShutdownCallback, CleanupConnectionPool); + sock_fds = new alist(10, not_owned_by_alist); + BnetThreadServerTcp((dlist*)arg, me->MaxConnections, sock_fds, + thread_list, HandleConnectionRequest, my_config, + &server_state, UserAgentShutdownCallback, + CleanupConnectionPool); return NULL; } @@ -148,7 +149,7 @@ extern "C" void* connect_thread(void* arg) * command thread. This routine creates the thread and then * returns. */ -bool StartSocketServer(dlist* addrs) +bool StartSocketServer(dlist* addrs) { int status; diff --git a/core/src/dird/socket_server.h b/core/src/dird/socket_server.h index 6ffa9f7488b..1efccf4a11e 100644 --- a/core/src/dird/socket_server.h +++ b/core/src/dird/socket_server.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -22,11 +22,13 @@ #ifndef BAREOS_DIRD_SOCKET_SERVER_H_ #define BAREOS_DIRD_SOCKET_SERVER_H_ +template class dlist; +class IPADDR; namespace directordaemon { -bool StartSocketServer(dlist* addrs); +bool StartSocketServer(dlist* addrs); void StopSocketServer(); } /* namespace directordaemon */ diff --git a/core/src/dird/storage.cc b/core/src/dird/storage.cc index da8034514be..34a046cefdf 100644 --- a/core/src/dird/storage.cc +++ b/core/src/dird/storage.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -50,7 +50,9 @@ namespace directordaemon { /* Forward referenced functions */ // Copy the storage definitions from an alist to the JobControlRecord -void CopyRwstorage(JobControlRecord* jcr, alist* storage, const char* where) +void CopyRwstorage(JobControlRecord* jcr, + alist* storage, + const char* where) { if (jcr->JobReads()) { CopyRstorage(jcr, storage, where); } CopyWstorage(jcr, storage, where); @@ -77,14 +79,17 @@ void FreeRwstorage(JobControlRecord* jcr) } // Copy the storage definitions from an alist to the JobControlRecord -void CopyRstorage(JobControlRecord* jcr, alist* storage, const char* where) +void CopyRstorage(JobControlRecord* jcr, + alist* storage, + const char* where) { if (storage) { StorageResource* store = nullptr; if (jcr->impl->res.read_storage_list) { delete jcr->impl->res.read_storage_list; } - jcr->impl->res.read_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.read_storage_list + = new alist(10, not_owned_by_alist); foreach_alist (store, storage) { jcr->impl->res.read_storage_list->append(store); } @@ -110,7 +115,8 @@ void SetRstorage(JobControlRecord* jcr, UnifiedStorageResource* store) if (!store->store) { return; } if (jcr->impl->res.read_storage_list) { FreeRstorage(jcr); } if (!jcr->impl->res.read_storage_list) { - jcr->impl->res.read_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.read_storage_list + = new alist(10, not_owned_by_alist); } jcr->impl->res.read_storage = store->store; if (!jcr->impl->res.rstore_source) { @@ -134,14 +140,17 @@ void FreeRstorage(JobControlRecord* jcr) } // Copy the storage definitions from an alist to the JobControlRecord -void CopyWstorage(JobControlRecord* jcr, alist* storage, const char* where) +void CopyWstorage(JobControlRecord* jcr, + alist* storage, + const char* where) { if (storage) { StorageResource* st = nullptr; if (jcr->impl->res.write_storage_list) { delete jcr->impl->res.write_storage_list; } - jcr->impl->res.write_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.write_storage_list + = new alist(10, not_owned_by_alist); foreach_alist (st, storage) { Dmsg1(100, "write_storage_list=%s\n", st->resource_name_); jcr->impl->res.write_storage_list->append(st); @@ -171,7 +180,8 @@ void SetWstorage(JobControlRecord* jcr, UnifiedStorageResource* store) if (!store->store) { return; } if (jcr->impl->res.write_storage_list) { FreeWstorage(jcr); } if (!jcr->impl->res.write_storage_list) { - jcr->impl->res.write_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.write_storage_list + = new alist(10, not_owned_by_alist); } jcr->impl->res.write_storage = store->store; if (!jcr->impl->res.wstore_source) { @@ -220,7 +230,8 @@ void SetPairedStorage(JobControlRecord* jcr) */ jcr->impl->res.paired_read_write_storage_list = jcr->impl->res.write_storage_list; - jcr->impl->res.write_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.write_storage_list + = new alist(10, not_owned_by_alist); foreach_alist (store, jcr->impl->res.paired_read_write_storage_list) { if (store->paired_storage) { Dmsg1(100, "write_storage_list=%s\n", @@ -254,7 +265,7 @@ void SetPairedStorage(JobControlRecord* jcr) * jcr->impl_->res.read_storage_list. */ jcr->impl->res.paired_read_write_storage_list - = new alist(10, not_owned_by_alist); + = new alist(10, not_owned_by_alist); foreach_alist (paired_read_write_storage, jcr->impl->res.read_storage_list) { store = (StorageResource*)my_config->GetNextRes(R_STORAGE, NULL); @@ -299,7 +310,8 @@ void SetPairedStorage(JobControlRecord* jcr) */ jcr->impl->res.paired_read_write_storage_list = jcr->impl->res.read_storage_list; - jcr->impl->res.read_storage_list = new alist(10, not_owned_by_alist); + jcr->impl->res.read_storage_list + = new alist(10, not_owned_by_alist); foreach_alist (store, jcr->impl->res.paired_read_write_storage_list) { if (store->paired_storage) { Dmsg1(100, "read_storage_list=%s\n", @@ -509,12 +521,12 @@ void StorageStatus(UaContext* ua, StorageResource* store, char* cmd) } // Simple comparison function for binary insert of vol_list_t -int StorageCompareVolListEntry(void* e1, void* e2) +int StorageCompareVolListEntry(vol_list_t* e1, vol_list_t* e2) { vol_list_t *v1, *v2; - v1 = (vol_list_t*)e1; - v2 = (vol_list_t*)e2; + v1 = e1; + v2 = e2; ASSERT(v1); ASSERT(v2); @@ -553,7 +565,7 @@ changer_vol_list_t* get_vol_list_from_storage(UaContext* ua, bool cached) { vol_list_type type; - dlist* contents = NULL; + dlist* contents = NULL; changer_vol_list_t* vol_list = NULL; P(store->runtime_storage_status->changer_lock); @@ -776,7 +788,7 @@ vol_list_t* vol_is_loaded_in_drive(StorageResource* store, default: break; } - vl = (vol_list_t*)vol_list->contents->next((void*)vl); + vl = vol_list->contents->next(vl); } return NULL; diff --git a/core/src/dird/storage.h b/core/src/dird/storage.h index 6ae46a83b1b..115a8d6b2b5 100644 --- a/core/src/dird/storage.h +++ b/core/src/dird/storage.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -24,17 +24,24 @@ #include "ndmp/smc.h" +template class alist; namespace directordaemon { -void CopyRwstorage(JobControlRecord* jcr, alist* storage, const char* where); +void CopyRwstorage(JobControlRecord* jcr, + alist* storage, + const char* where); void SetRwstorage(JobControlRecord* jcr, UnifiedStorageResource* store); void FreeRwstorage(JobControlRecord* jcr); -void CopyWstorage(JobControlRecord* jcr, alist* storage, const char* where); +void CopyWstorage(JobControlRecord* jcr, + alist* storage, + const char* where); void SetWstorage(JobControlRecord* jcr, UnifiedStorageResource* store); void FreeWstorage(JobControlRecord* jcr); -void CopyRstorage(JobControlRecord* jcr, alist* storage, const char* where); +void CopyRstorage(JobControlRecord* jcr, + alist* storage, + const char* where); void SetRstorage(JobControlRecord* jcr, UnifiedStorageResource* store); void FreeRstorage(JobControlRecord* jcr); void SetPairedStorage(JobControlRecord* jcr); @@ -42,7 +49,7 @@ void FreePairedStorage(JobControlRecord* jcr); bool HasPairedStorage(JobControlRecord* jcr); bool SelectNextRstore(JobControlRecord* jcr, bootstrap_info& info); void StorageStatus(UaContext* ua, StorageResource* store, char* cmd); -int StorageCompareVolListEntry(void* e1, void* e2); +int StorageCompareVolListEntry(vol_list_t* e1, vol_list_t* e2); changer_vol_list_t* get_vol_list_from_storage(UaContext* ua, StorageResource* store, bool listall, diff --git a/core/src/dird/ua_acl.cc b/core/src/dird/ua_acl.cc index 44987af955f..0524df968e3 100644 --- a/core/src/dird/ua_acl.cc +++ b/core/src/dird/ua_acl.cc @@ -3,7 +3,7 @@ Copyright (C) 2004-2008 Free Software Foundation Europe e.V. Copyright (C) 2014-2016 Planets Communications B.V. - Copyright (C) 2014-2020 Bareos GmbH & Co. KG + Copyright (C) 2014-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -56,7 +56,7 @@ static bool is_regex(std::string string_to_check) * Loop over the items in the alist and verify if they match the given item * that access was requested for. */ -static inline bool FindInAclList(alist* list, +static inline bool FindInAclList(alist* list, int acl, const char* item, int len) diff --git a/core/src/dird/ua_cmds.cc b/core/src/dird/ua_cmds.cc index 9137a1f86ed..2818c34bd7e 100644 --- a/core/src/dird/ua_cmds.cc +++ b/core/src/dird/ua_cmds.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -812,7 +812,7 @@ static inline bool CancelJobs(UaContext* ua, const char* cmd) { JobControlRecord* jcr; JobId_t* JobId = nullptr; - alist* selection; + alist* selection; selection = select_jobs(ua, "cancel"); if (!selection) { return true; } @@ -978,7 +978,7 @@ static bool SetbwlimitCmd(UaContext* ua, const char* cmd) if (FindArgKeyword(ua, lst) > 0) { JobControlRecord* jcr; JobId_t* JobId = nullptr; - alist* selection; + alist* selection; selection = select_jobs(ua, "limit"); if (!selection) { return true; } @@ -1691,7 +1691,7 @@ static bool ResolveCmd(UaContext* ua, const char* cmd) if (storage) { DoStorageResolve(ua, storage); } if (!client && !storage) { - dlist* addr_list; + dlist* addr_list; const char* errstr; char addresses[2048]; diff --git a/core/src/dird/ua_impexp.cc b/core/src/dird/ua_impexp.cc index 72f90cfa137..0edfc3441a7 100644 --- a/core/src/dird/ua_impexp.cc +++ b/core/src/dird/ua_impexp.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -333,7 +333,7 @@ static inline changer_vol_list_t* scan_slots_for_volnames( // Lookup the drive in the old list. vls.element_address = vl1->element_address; vl2 = (vol_list_t*)vol_list->contents->binary_search( - (void*)&vls, StorageCompareVolListEntry); + &vls, StorageCompareVolListEntry); if (vl2 && vl2->slot_status == slot_status_t::kSlotStatusFull && vl2->currently_loaded_slot_number == vl1->currently_loaded_slot_number) { @@ -376,7 +376,7 @@ static inline changer_vol_list_t* scan_slots_for_volnames( // Lookup the slot in the old list. vls.element_address = vl1->element_address; vl2 = (vol_list_t*)vol_list->contents->binary_search( - (void*)&vls, StorageCompareVolListEntry); + &vls, StorageCompareVolListEntry); if (vl2 && vl2->slot_status == slot_status_t::kSlotStatusFull && vl2->bareos_slot_number == vl1->bareos_slot_number) { // Volume in slot is the same copy the volume name. diff --git a/core/src/dird/ua_prune.cc b/core/src/dird/ua_prune.cc index 1834e64f399..ea839fc4ee9 100644 --- a/core/src/dird/ua_prune.cc +++ b/core/src/dird/ua_prune.cc @@ -572,7 +572,7 @@ struct accurate_check_ctx { // row: Job.Name, FileSet, Client.Name, FileSetId, ClientId, Type static int JobSelectHandler(void* ctx, int num_fields, char** row) { - alist* lst = (alist*)ctx; + alist* lst = (alist*)ctx; struct accurate_check_ctx* res; ASSERT(num_fields == 6); @@ -618,7 +618,7 @@ static bool PruneBackupJobs(UaContext* ua, PoolMem sql_from(PM_MESSAGE); utime_t period; char ed1[50]; - alist* jobids_check = NULL; + alist* jobids_check = NULL; struct accurate_check_ctx* elt = nullptr; db_list_ctx jobids, tempids; JobDbRecord jr; @@ -673,7 +673,7 @@ static bool PruneBackupJobs(UaContext* ua, * able to restore files. (ie, last full, last diff, last incrs) * Note: The DISTINCT could be more useful if we don't get FileSetId */ - jobids_check = new alist(10, owned_by_alist); + jobids_check = new alist(10, owned_by_alist); Mmsg(query, "SELECT DISTINCT Job.Name, FileSet, Client.Name, Job.FileSetId, " "Job.ClientId, Job.Type " diff --git a/core/src/dird/ua_run.cc b/core/src/dird/ua_run.cc index 64c251fa265..bb0216f73cf 100644 --- a/core/src/dird/ua_run.cc +++ b/core/src/dird/ua_run.cc @@ -2,7 +2,7 @@ Copyright (C) 2001-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -1003,7 +1003,7 @@ static bool ResetRestoreContext(UaContext* ua, static void SelectWhereRegexp(UaContext* ua, JobControlRecord* jcr) { - alist* regs; + alist* regs; char *strip_prefix, *add_prefix, *add_suffix, *rwhere; strip_prefix = add_suffix = rwhere = add_prefix = NULL; diff --git a/core/src/dird/ua_select.cc b/core/src/dird/ua_select.cc index c2cbd3d7242..19b59da9d7f 100644 --- a/core/src/dird/ua_select.cc +++ b/core/src/dird/ua_select.cc @@ -1461,7 +1461,8 @@ bool GetLevelFromName(JobControlRecord* jcr, const char* level_name) } // Insert an JobId into the list of selected JobIds when its a unique new id. -static inline bool InsertSelectedJobid(alist* selected_jobids, JobId_t JobId) +static inline bool InsertSelectedJobid(alist* selected_jobids, + JobId_t JobId) { bool found; JobId_t* selected_jobid = nullptr; @@ -1491,7 +1492,7 @@ static inline bool InsertSelectedJobid(alist* selected_jobids, JobId_t JobId) * Returns: NULL on error * alist on success with the selected jobids. */ -alist* select_jobs(UaContext* ua, const char* reason) +alist* select_jobs(UaContext* ua, const char* reason) { int i; int cnt = 0; @@ -1499,7 +1500,7 @@ alist* select_jobs(UaContext* ua, const char* reason) JobControlRecord* jcr = NULL; bool select_all = false; bool select_by_state = false; - alist* selected_jobids; + alist* selected_jobids; const char* lst[] = {"job", "jobid", "ujobid", NULL}; enum { @@ -1512,7 +1513,7 @@ alist* select_jobs(UaContext* ua, const char* reason) } selection_criterium; // Allocate a list for holding the selected JobIds. - selected_jobids = new alist(10, owned_by_alist); + selected_jobids = new alist(10, owned_by_alist); // See if "all" is given. if (FindArg(ua, NT_("all")) > 0) { select_all = true; } diff --git a/core/src/dird/ua_select.h b/core/src/dird/ua_select.h index 137d4961234..ce2be3e1272 100644 --- a/core/src/dird/ua_select.h +++ b/core/src/dird/ua_select.h @@ -23,7 +23,9 @@ #define BAREOS_DIRD_UA_SELECT_H_ #include "dird/ua.h" +template class alist; + struct PoolDbRecord; struct MediaDbRecord; struct StorageDbRecord; @@ -77,7 +79,7 @@ bool GetClientDbr(UaContext* ua, ClientDbRecord* cr); PoolResource* get_pool_resource(UaContext* ua); JobResource* get_restore_job(UaContext* ua); PoolResource* select_pool_resource(UaContext* ua); -alist* select_jobs(UaContext* ua, const char* reason); +alist* select_jobs(UaContext* ua, const char* reason); ClientResource* get_client_resource(UaContext* ua); int GetJobDbr(UaContext* ua, JobDbRecord* jr); bool GetUserSlotList(UaContext* ua, diff --git a/core/src/dird/ua_status.cc b/core/src/dird/ua_status.cc index 455dfb95869..c94cac6c0c2 100644 --- a/core/src/dird/ua_status.cc +++ b/core/src/dird/ua_status.cc @@ -3,7 +3,7 @@ Copyright (C) 2001-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -753,7 +753,7 @@ static void PrtRunhdr(UaContext* ua) /* Scheduling packet */ struct sched_pkt { - dlink link; /* keep this as first item!!! */ + dlink link; /* keep this as first item!!! */ JobResource* job; int level; int priority; @@ -815,11 +815,8 @@ static void PrtRuntime(UaContext* ua, sched_pkt* sp) } // Sort items by runtime, priority -static int CompareByRuntimePriority(void* item1, void* item2) +static int CompareByRuntimePriority(sched_pkt* p1, sched_pkt* p2) { - sched_pkt* p1 = (sched_pkt*)item1; - sched_pkt* p2 = (sched_pkt*)item2; - if (p1->runtime < p2->runtime) { return -1; } else if (p1->runtime > p2->runtime) { @@ -844,7 +841,7 @@ static void ListScheduledJobs(UaContext* ua) int level, num_jobs = 0; int priority; bool hdr_printed = false; - auto sched = std::make_unique(); + auto sched = std::make_unique>(); sched_pkt* sp; int days, i; @@ -1223,7 +1220,7 @@ static void ListTerminatedJobs(UaContext* ua) static void ListConnectedClients(UaContext* ua) { Connection* connection = NULL; - alist* connections = NULL; + alist* connections = NULL; const char* separator = "===================="; char dt[MAX_TIME_LENGTH]; diff --git a/core/src/filed/dir_cmd.cc b/core/src/filed/dir_cmd.cc index 98dbf5f8d0a..89025b45abd 100644 --- a/core/src/filed/dir_cmd.cc +++ b/core/src/filed/dir_cmd.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -90,7 +90,7 @@ const bool have_encryption = true; /* Global variables to handle Client Initiated Connections */ static bool quit_client_initiate_connection = false; -static alist* client_initiated_connection_threads = nullptr; +static alist* client_initiated_connection_threads = nullptr; /* Imported functions */ extern bool AccurateCmd(JobControlRecord* jcr); @@ -275,7 +275,7 @@ static char read_close[] = "read close session %d\n"; // See if we are allowed to execute the command issued. static bool ValidateCommand(JobControlRecord* jcr, const char* cmd, - alist* allowed_job_cmds) + alist* allowed_job_cmds) { char* allowed_job_cmd = nullptr; bool allowed = false; @@ -398,7 +398,7 @@ JobControlRecord* create_new_director_session(BareosSocket* dir) jcr->dir_bsock = dir; jcr->impl->ff = init_find_files(); jcr->start_time = time(nullptr); - jcr->impl->RunScripts = new alist(10, not_owned_by_alist); + jcr->impl->RunScripts = new alist(10, not_owned_by_alist); jcr->impl->last_fname = GetPoolMemory(PM_FNAME); jcr->impl->last_fname[0] = 0; jcr->client_name = GetMemory(strlen(my_name) + 1); @@ -646,7 +646,7 @@ bool StartConnectToDirectorThreads() DirectorResource* dir_res = nullptr; int pthread_create_result = 0; if (!client_initiated_connection_threads) { - client_initiated_connection_threads = new alist(); + client_initiated_connection_threads = new alist(); } pthread_t* thread; @@ -708,7 +708,7 @@ bool StopConnectToDirectorThreads(bool wait) static bool ResolveCmd(JobControlRecord* jcr) { BareosSocket* dir = jcr->dir_bsock; - dlist* addr_list; + dlist* addr_list; const char* errstr; char addresses[2048]; char hostname[2048]; diff --git a/core/src/filed/fd_plugins.cc b/core/src/filed/fd_plugins.cc index 3cd22d1facf..38977844b13 100644 --- a/core/src/filed/fd_plugins.cc +++ b/core/src/filed/fd_plugins.cc @@ -66,7 +66,7 @@ const char* plugin_type = "-fd.dll"; #else const char* plugin_type = "-fd.so"; #endif -static alist* fd_plugin_list = NULL; +static alist* fd_plugin_list = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; extern int SaveFile(JobControlRecord* jcr, @@ -258,7 +258,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, bEvent* event, PluginContext* ctx, void* value, - alist* plugin_ctx_list, + alist* plugin_ctx_list, int* index, bRC* rc) @@ -310,7 +310,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, * that moved back a position in the alist. */ if (index) { - UnloadPlugin(plugin_ctx_list, ctx->plugin, *index); + UnloadPlugin(fd_plugin_list, ctx->plugin, *index); *index = ((*index) - 1); } break; @@ -349,7 +349,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, bool call_if_canceled = false; restore_object_pkt* rop; PluginContext* ctx = nullptr; - alist* plugin_ctx_list; + alist* plugin_ctx_list; bRC rc = bRC_OK; if (!fd_plugin_list) { @@ -463,7 +463,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, bool PluginCheckFile(JobControlRecord* jcr, char* fname) { PluginContext* ctx = nullptr; - alist* plugin_ctx_list; + alist* plugin_ctx_list; int retval = bRC_OK; if (!fd_plugin_list || !jcr || !jcr->plugin_ctx_list @@ -573,7 +573,7 @@ bRC PluginOptionHandleFile(JobControlRecord* jcr, bEvent event; bEventType eventType; PluginContext* ctx = nullptr; - alist* plugin_ctx_list; + alist* plugin_ctx_list; cmd = ff_pkt->plugin; eventType = bEventHandleBackupFile; @@ -660,7 +660,7 @@ int PluginSave(JobControlRecord* jcr, FindFilesPacket* ff_pkt, bool top_level) bEventType eventType; PoolMem fname(PM_FNAME); PoolMem link(PM_FNAME); - alist* plugin_ctx_list; + alist* plugin_ctx_list; char flags[FOPTS_BYTES]; cmd = ff_pkt->top_fname; @@ -900,7 +900,7 @@ int PluginEstimate(JobControlRecord* jcr, PoolMem fname(PM_FNAME); PoolMem link(PM_FNAME); PluginContext* ctx = nullptr; - alist* plugin_ctx_list; + alist* plugin_ctx_list; Attributes attr; plugin_ctx_list = jcr->plugin_ctx_list; @@ -1087,7 +1087,7 @@ bool PluginNameStream(JobControlRecord* jcr, char* name) bool start; bool retval = true; PluginContext* ctx = nullptr; - alist* plugin_ctx_list; + alist* plugin_ctx_list; Dmsg1(debuglevel, "Read plugin stream string=%s\n", name); SkipNonspaces(&p); /* skip over jcr->JobFiles */ @@ -1430,7 +1430,7 @@ BxattrExitCode PluginBuildXattrStreams(JobControlRecord* jcr, { Plugin* plugin; #if defined(HAVE_XATTR) - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; #endif BxattrExitCode retval = BxattrExitCode::kError; @@ -1488,7 +1488,7 @@ BxattrExitCode PluginBuildXattrStreams(JobControlRecord* jcr, + current_xattr->value_length; if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -1547,7 +1547,7 @@ BxattrExitCode PluginParseXattrStreams(JobControlRecord* jcr, { #if defined(HAVE_XATTR) Plugin* plugin; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; #endif BxattrExitCode retval = BxattrExitCode::kError; @@ -1561,7 +1561,7 @@ BxattrExitCode PluginParseXattrStreams(JobControlRecord* jcr, xattr_t* current_xattr = nullptr; struct xattr_pkt xp; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -1622,7 +1622,7 @@ static void DumpFdPlugins(FILE* fp) { DumpPlugins(fd_plugin_list, fp); } * This entry point is called internally by Bareos to ensure that the plugin * IO calls come into this code. */ -void LoadFdPlugins(const char* plugin_dir, alist* plugin_names) +void LoadFdPlugins(const char* plugin_dir, alist* plugin_names) { Plugin* plugin; @@ -1631,7 +1631,7 @@ void LoadFdPlugins(const char* plugin_dir, alist* plugin_names) return; } - fd_plugin_list = new alist(10, not_owned_by_alist); + fd_plugin_list = new alist(10, not_owned_by_alist); if (!LoadPlugins((void*)&bareos_plugin_interface_version, (void*)&bareos_core_functions, fd_plugin_list, plugin_dir, plugin_names, plugin_type, IsPluginCompatible)) { @@ -1769,7 +1769,7 @@ void NewPlugins(JobControlRecord* jcr) return; } - jcr->plugin_ctx_list = new alist(10, owned_by_alist); + jcr->plugin_ctx_list = new alist(10, owned_by_alist); Dmsg2(debuglevel, "Instantiate plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId); diff --git a/core/src/filed/fd_plugins.h b/core/src/filed/fd_plugins.h index 3d716d7af50..0a8b348d2de 100644 --- a/core/src/filed/fd_plugins.h +++ b/core/src/filed/fd_plugins.h @@ -64,6 +64,7 @@ struct BareosWinFilePacket; struct FindFilesPacket; #endif /* FILE_DAEMON */ +template class alist; namespace filedaemon { @@ -253,7 +254,7 @@ typedef struct s_bareosInfo { // Bareos Core Routines -- not used within a plugin #ifdef FILE_DAEMON -void LoadFdPlugins(const char* plugin_dir, alist* plugin_names); +void LoadFdPlugins(const char* plugin_dir, alist* plugin_names); void UnloadFdPlugins(void); int ListFdPlugins(PoolMem& msg); void NewPlugins(JobControlRecord* jcr); diff --git a/core/src/filed/filed.cc b/core/src/filed/filed.cc index 7814f63f5e0..371034576c1 100644 --- a/core/src/filed/filed.cc +++ b/core/src/filed/filed.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -39,6 +39,7 @@ #include "lib/watchdog.h" #include "lib/util.h" #include "lib/address_conf.h" +#include "lib/alist.h" using namespace filedaemon; @@ -269,7 +270,6 @@ int main(int argc, char* argv[]) GetFirstPortHostOrder(me->FDaddrs)); ReadStateFile(me->working_directory, "bareos-fd", GetFirstPortHostOrder(me->FDaddrs)); - LoadFdPlugins(me->plugin_directory, me->plugin_names); InitJcrChain(); @@ -419,7 +419,7 @@ static bool CheckResources() } // Trusted Signers. We're always trusted. - me->pki_signers = new alist(10, not_owned_by_alist); + me->pki_signers = new alist(10, not_owned_by_alist); if (me->pki_keypair) { me->pki_signers->append(crypto_keypair_dup(me->pki_keypair)); } @@ -463,7 +463,7 @@ static bool CheckResources() * Crypto recipients. We're always included as a recipient. * The symmetric session key will be encrypted for each of these readers. */ - me->pki_recipients = new alist(10, not_owned_by_alist); + me->pki_recipients = new alist(10, not_owned_by_alist); if (me->pki_keypair) { me->pki_recipients->append(crypto_keypair_dup(me->pki_keypair)); } diff --git a/core/src/filed/filed_conf.h b/core/src/filed/filed_conf.h index f2c3ca4381c..a7adfbbe080 100644 --- a/core/src/filed/filed_conf.h +++ b/core/src/filed/filed_conf.h @@ -31,9 +31,13 @@ #include "lib/messages_resource.h" #include "lib/tls_conf.h" - +template class alist; +template class dlist; +class IPADDR; + +typedef struct X509_Keypair X509_KEYPAIR; namespace filedaemon { @@ -70,10 +74,10 @@ class DirectorResource bool conn_from_dir_to_fd = false; /* Allow incoming connections */ bool conn_from_fd_to_dir = false; /* Connect to director */ bool monitor; /* Have only access to status and .status functions */ - alist* allowed_script_dirs + alist* allowed_script_dirs = nullptr; /* Only allow to run scripts in this directories */ - alist* allowed_job_cmds = nullptr; /* Only allow the following Job commands to - be executed */ + alist* allowed_job_cmds = nullptr; /* Only allow the following + Job commands to be executed */ uint64_t max_bandwidth_per_job = 0; /* Bandwidth limitation (per director) */ DirectorResource() = default; @@ -87,13 +91,13 @@ class ClientResource ClientResource() = default; virtual ~ClientResource() = default; - dlist* FDaddrs = nullptr; - dlist* FDsrc_addr = nullptr; /* Address to source connections from */ + dlist* FDaddrs = nullptr; + dlist* FDsrc_addr = nullptr; /* Address to source connections from */ char* working_directory = nullptr; char* pid_directory = nullptr; char* subsys_directory = nullptr; char* plugin_directory = nullptr; /* Plugin directory */ - alist* plugin_names = nullptr; + alist* plugin_names = nullptr; char* scripts_directory = nullptr; MessagesResource* messages = nullptr; /* Daemon message handler */ uint32_t MaxConcurrentJobs = 0; @@ -107,22 +111,24 @@ class ClientResource bool allow_bw_bursting = false; /* Allow bursting with bandwidth limiting */ bool pki_sign = false; /* Enable Data Integrity Verification via Digital Signatures */ - bool pki_encrypt = false; /* Enable Data Encryption */ - char* pki_keypair_file = nullptr; /* PKI Key Pair File */ - alist* pki_signing_key_files = nullptr; /* PKI Signing Key Files */ - alist* pki_master_key_files = nullptr; /* PKI Master Key Files */ - crypto_cipher_t pki_cipher = CRYPTO_CIPHER_NONE; /* PKI Cipher to use */ + bool pki_encrypt = false; /* Enable Data Encryption */ + char* pki_keypair_file = nullptr; /* PKI Key Pair File */ + alist* pki_signing_key_files + = nullptr; /* PKI Signing Key Files */ + alist* pki_master_key_files = nullptr; /* PKI Master Key Files */ + crypto_cipher_t pki_cipher = CRYPTO_CIPHER_NONE; /* PKI Cipher to use */ bool always_use_lmdb = false; /* Use LMDB for accurate data */ uint32_t lmdb_threshold = 0; /* Switch to using LDMD when number of accurate entries exceeds treshold. */ X509_KEYPAIR* pki_keypair = nullptr; /* Shared PKI Public/Private Keypair */ - alist* pki_signers = nullptr; /* Shared PKI Trusted Signers */ - alist* pki_recipients = nullptr; /* Shared PKI Recipients */ - alist* allowed_script_dirs + + alist* pki_signers = nullptr; /* Shared PKI Trusted Signers */ + alist* pki_recipients = nullptr; /* Shared PKI Recipients */ + alist* allowed_script_dirs = nullptr; /* Only allow to run scripts in this directories */ - alist* allowed_job_cmds = nullptr; /* Only allow the following Job commands to - be executed */ - char* verid = nullptr; /* Custom Id to print in version command */ + alist* allowed_job_cmds = nullptr; /* Only allow the following + Job commands to be executed */ + char* verid = nullptr; /* Custom Id to print in version command */ char* secure_erase_cmdline = nullptr; /* Cmdline to execute to perform secure erase of file */ char* log_timestamp_format = nullptr; /* Timestamp format to use in generic diff --git a/core/src/filed/jcr_private.h b/core/src/filed/jcr_private.h index 5ddd046083b..920b48cfd27 100644 --- a/core/src/filed/jcr_private.h +++ b/core/src/filed/jcr_private.h @@ -41,8 +41,8 @@ struct CryptoContext { bool pki_encrypt{}; /**< Enable PKI Encryption? */ DIGEST* digest{}; /**< Last file's digest context */ X509_KEYPAIR* pki_keypair{}; /**< Encryption key pair */ - alist* pki_signers{}; /**< Trusted Signers */ - alist* pki_recipients{}; /**< Trusted Recipients */ + alist* pki_signers{}; /**< Trusted Signers */ + alist* pki_recipients{}; /**< Trusted Recipients */ CRYPTO_SESSION* pki_session{}; /**< PKE Public Keys + Symmetric Session Keys */ POOLMEM* crypto_buf{}; /**< Encryption/Decryption buffer */ POOLMEM* pki_session_encoded{}; /**< Cached DER-encoded copy of pki_session */ @@ -74,7 +74,7 @@ struct JobControlRecordPrivate { std::atomic hb_running{}; /**< Heartbeat running */ std::shared_ptr hb_bsock; /**< Duped SD socket */ std::shared_ptr hb_dir_bsock; /**< Duped DIR socket */ - alist* RunScripts{}; /**< Commands to run before and after job */ + alist* RunScripts{}; /**< Commands to run before and after job */ CryptoContext crypto; /**< Crypto ctx */ filedaemon::DirectorResource* director{}; /**< Director resource */ bool enable_vss{}; /**< VSS used by FD */ diff --git a/core/src/filed/restore.cc b/core/src/filed/restore.cc index d1ee29a97e2..74488921164 100644 --- a/core/src/filed/restore.cc +++ b/core/src/filed/restore.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -190,7 +190,7 @@ static inline void PushDelayedDataStream(r_ctx& rctx, BareosSocket* sd) DelayedDataStream* dds; if (!rctx.delayed_streams) { - rctx.delayed_streams = new alist(10, owned_by_alist); + rctx.delayed_streams = new alist(10, owned_by_alist); } dds = (DelayedDataStream*)malloc(sizeof(DelayedDataStream)); diff --git a/core/src/filed/restore.h b/core/src/filed/restore.h index 70807cc4855..af8f633b126 100644 --- a/core/src/filed/restore.h +++ b/core/src/filed/restore.h @@ -24,7 +24,7 @@ #define BAREOS_FILED_RESTORE_H_ #include "findlib/bfile.h" - +template class alist; namespace filedaemon { @@ -62,7 +62,7 @@ struct r_ctx { int32_t type{0}; /* file type FT_ */ Attributes* attr{nullptr}; /* Pointer to attributes */ bool extract{false}; /* set when extracting */ - alist* delayed_streams{nullptr}; /* streams that should be restored as last */ + alist* delayed_streams{nullptr}; /* streams that should be restored as last */ SIGNATURE* sig{nullptr}; /* Cryptographic signature (if any) for file */ CRYPTO_SESSION* cs{nullptr}; /* Cryptographic session data (if any) for file */ RestoreCipherContext cipher_ctx{0}; /* Cryptographic restore context (if any) for file */ diff --git a/core/src/filed/socket_server.cc b/core/src/filed/socket_server.cc index e5a222e0bc5..d649ed314e2 100644 --- a/core/src/filed/socket_server.cc +++ b/core/src/filed/socket_server.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2011 Free Software Foundation Europe e.V. - Copyright (C) 2014-2020 Bareos GmbH & Co. KG + Copyright (C) 2014-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -43,7 +43,7 @@ namespace filedaemon { /* Global variables */ static ThreadList thread_list; static pthread_t tcp_server_tid; -static alist* sock_fds = NULL; +static alist* sock_fds = nullptr; /** * Connection request. We accept connections either from the Director or the @@ -111,7 +111,7 @@ static void* UserAgentShutdownCallback(void* bsock) return nullptr; } -void StartSocketServer(dlist* addrs) +void StartSocketServer(dlist* addrs) { IPADDR* p; @@ -123,7 +123,7 @@ void StartSocketServer(dlist* addrs) } // Permit MaxConnections connections. - sock_fds = new alist(10, not_owned_by_alist); + sock_fds = new alist(10, not_owned_by_alist); BnetThreadServerTcp(addrs, me->MaxConnections, sock_fds, thread_list, HandleConnectionRequest, my_config, nullptr, UserAgentShutdownCallback); diff --git a/core/src/filed/socket_server.h b/core/src/filed/socket_server.h index 86f92d6e3bb..72031a87a62 100644 --- a/core/src/filed/socket_server.h +++ b/core/src/filed/socket_server.h @@ -23,11 +23,13 @@ #ifndef BAREOS_FILED_SOCKET_SERVER_H_ #define BAREOS_FILED_SOCKET_SERVER_H_ +template class dlist; +class IPADDR; namespace filedaemon { -void StartSocketServer(dlist* addrs); +void StartSocketServer(dlist* addrs); void StopSocketServer(bool wait = false); } /* namespace filedaemon */ diff --git a/core/src/findlib/find.h b/core/src/findlib/find.h index ff995f3a5fd..6fe39713475 100644 --- a/core/src/findlib/find.h +++ b/core/src/findlib/find.h @@ -131,33 +131,33 @@ struct findFOPTS { char AccurateOpts[MAX_OPTS]{}; /**< Accurate mode options */ char BaseJobOpts[MAX_OPTS]{}; /**< Basejob mode options */ char* plugin{}; /**< Plugin that handle this section */ - alist regex; /**< Regex string(s) */ - alist regexdir; /**< Regex string(s) for directories */ - alist regexfile; /**< Regex string(s) for files */ - alist wild; /**< Wild card strings */ - alist wilddir; /**< Wild card strings for directories */ - alist wildfile; /**< Wild card strings for files */ - alist wildbase; /**< Wild card strings for basenames */ - alist base; /**< List of base names */ - alist fstype; /**< File system type limitation */ - alist Drivetype; /**< Drive type limitation */ + alist regex; /**< Regex string(s) */ + alist regexdir; /**< Regex string(s) for directories */ + alist regexfile; /**< Regex string(s) for files */ + alist wild; /**< Wild card strings */ + alist wilddir; /**< Wild card strings for directories */ + alist wildfile; /**< Wild card strings for files */ + alist wildbase; /**< Wild card strings for basenames */ + alist base; /**< List of base names */ + alist fstype; /**< File system type limitation */ + alist Drivetype; /**< Drive type limitation */ }; // This is either an include item or an exclude item struct findIncludeExcludeItem { - findFOPTS* current_opts; /**< Points to current options structure */ - alist opts_list; /**< Options list */ - dlist name_list; /**< Filename list -- holds dlistString */ - dlist plugin_list; /**< Plugin list -- holds dlistString */ - alist ignoredir; /**< Ignore directories with this file(s) */ + findFOPTS* current_opts; /**< Points to current options structure */ + alist opts_list; /**< Options list */ + dlist name_list; /**< Filename list -- holds dlistString */ + dlist plugin_list; /**< Plugin list -- holds dlistString */ + alist ignoredir; /**< Ignore directories with this file(s) */ }; // FileSet Resource struct findFILESET { int state; findIncludeExcludeItem* incexe; /**< Current item */ - alist include_list; - alist exclude_list; + alist include_list; + alist exclude_list; }; // OSX resource fork. @@ -246,8 +246,8 @@ struct FindFilesPacket { struct s_sz_matching* size_match{nullptr}; /**< Perform size matching ? */ bool cmd_plugin{false}; /**< Set if we have a command plugin */ bool opt_plugin{false}; /**< Set if we have an option plugin */ - alist fstypes; /**< Allowed file system types */ - alist drivetypes; /**< Allowed drive types */ + alist fstypes; /**< Allowed file system types */ + alist drivetypes; /**< Allowed drive types */ // List of all hard linked files found htable* linkhash{nullptr}; /**< Hard linked files */ diff --git a/core/src/findlib/xattr.cc b/core/src/findlib/xattr.cc index dbd5eee6716..4d6cf40fe67 100644 --- a/core/src/findlib/xattr.cc +++ b/core/src/findlib/xattr.cc @@ -3,7 +3,7 @@ Copyright (C) 2008-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -139,7 +139,7 @@ BxattrExitCode SendXattrStream(JobControlRecord* jcr, * First some generic functions for OSes that use the same xattr encoding * scheme. Currently for all OSes except for Solaris. */ -void XattrDropInternalTable(alist* xattr_value_list) +void XattrDropInternalTable(alist* xattr_value_list) { xattr_t* current_xattr = nullptr; @@ -174,7 +174,7 @@ void XattrDropInternalTable(alist* xattr_value_list) uint32_t SerializeXattrStream(JobControlRecord* jcr, XattrData* xattr_data, uint32_t expected_serialize_len, - alist* xattr_value_list) + alist* xattr_value_list) { xattr_t* current_xattr = nullptr; ser_declare; @@ -217,7 +217,7 @@ BxattrExitCode UnSerializeXattrStream(JobControlRecord* jcr, XattrData* xattr_data, char* content, uint32_t content_length, - alist* xattr_value_list) + alist* xattr_value_list) { unser_declare; xattr_t* current_xattr; @@ -330,7 +330,7 @@ static BxattrExitCode aix_build_xattr_streams(JobControlRecord* jcr, int32_t xattr_list_len, xattr_value_len; uint32_t expected_serialize_len = 0; xattr_t* current_xattr; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; BxattrExitCode retval = BxattrExitCode::kError; // First get the length of the available list with extended attributes. @@ -501,7 +501,7 @@ static BxattrExitCode aix_build_xattr_streams(JobControlRecord* jcr, } if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -553,10 +553,10 @@ static BxattrExitCode aix_parse_xattr_streams(JobControlRecord* jcr, uint32_t content_length) { xattr_t* current_xattr; - alist* xattr_value_list; + alist* xattr_value_list; BxattrExitCode retval = BxattrExitCode::kError; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -648,7 +648,7 @@ static BxattrExitCode irix_build_xattr_streams(JobControlRecord* jcr, attrlist_t* attrlist; attrlist_ent_t* attrlist_ent; xattr_t* current_xattr; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; uint32_t expected_serialize_len = 0; BxattrExitCode retval = BxattrExitCode::kError; POOLMEM* xattrbuf = GetMemory(ATTR_MAX_VALUELEN); @@ -797,7 +797,7 @@ static BxattrExitCode irix_build_xattr_streams(JobControlRecord* jcr, + current_xattr->value_length; if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -855,10 +855,10 @@ static BxattrExitCode irix_parse_xattr_streams(JobControlRecord* jcr, char* bp; int cnt, cmp_size, name_space_index, flags; xattr_t* current_xattr; - alist* xattr_value_list; + alist* xattr_value_list; BxattrExitCode retval = BxattrExitCode::kError; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -1021,7 +1021,7 @@ static BxattrExitCode generic_build_xattr_streams(JobControlRecord* jcr, int32_t xattr_list_len, xattr_value_len; uint32_t expected_serialize_len = 0; xattr_t* current_xattr; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; BxattrExitCode retval = BxattrExitCode::kError; // First get the length of the available list with extended attributes. @@ -1214,7 +1214,7 @@ static BxattrExitCode generic_build_xattr_streams(JobControlRecord* jcr, } if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -1266,10 +1266,10 @@ static BxattrExitCode generic_parse_xattr_streams(JobControlRecord* jcr, uint32_t content_length) { xattr_t* current_xattr = nullptr; - alist* xattr_value_list; + alist* xattr_value_list; BxattrExitCode retval = BxattrExitCode::kError; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -1400,7 +1400,7 @@ static BxattrExitCode bsd_build_xattr_streams(JobControlRecord* jcr, char* current_attrnamespace = NULL; char current_attrname[XATTR_BUFSIZ], current_attrtuple[XATTR_BUFSIZ]; xattr_t* current_xattr; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; BxattrExitCode retval = BxattrExitCode::kError; // Loop over all available xattr namespaces. @@ -1636,7 +1636,7 @@ static BxattrExitCode bsd_build_xattr_streams(JobControlRecord* jcr, } if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -1696,12 +1696,12 @@ static BxattrExitCode bsd_parse_xattr_streams(JobControlRecord* jcr, uint32_t content_length) { xattr_t* current_xattr = nullptr; - alist* xattr_value_list; + alist* xattr_value_list; int current_attrnamespace, cnt; char *attrnamespace, *attrname; BxattrExitCode retval = BxattrExitCode::kError; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -1812,7 +1812,7 @@ static BxattrExitCode tru64_build_xattr_streams(JobControlRecord* jcr, int32_t xattr_list_len, xattrbuf_size, xattrbuf_min_size; uint32_t expected_serialize_len = 0; xattr_t* current_xattr; - alist* xattr_value_list = NULL; + alist* xattr_value_list = NULL; struct proplistname_args prop_args; BxattrExitCode retval = BxattrExitCode::kError; POOLMEM* xattrbuf = GetPoolMemory(PM_MESSAGE); @@ -1965,7 +1965,7 @@ static BxattrExitCode tru64_build_xattr_streams(JobControlRecord* jcr, += sizeof(current_xattr->value_length) + current_xattr->value_length; if (xattr_value_list == NULL) { - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); } xattr_value_list->append(current_xattr); @@ -2016,10 +2016,10 @@ static BxattrExitCode tru64_parse_xattr_streams(JobControlRecord* jcr, char *bp, *xattrbuf = NULL; int32_t xattrbuf_size, cnt; xattr_t* current_xattr; - alist* xattr_value_list; + alist* xattr_value_list; BxattrExitCode retval = BxattrExitCode::kError; - xattr_value_list = new alist(10, not_owned_by_alist); + xattr_value_list = new alist(10, not_owned_by_alist); if (UnSerializeXattrStream(jcr, xattr_data, content, content_length, xattr_value_list) @@ -2251,7 +2251,8 @@ static inline void add_xattr_link_cache_entry(XattrData* xattr_data, ptr->target = strdup(target); if (!xattr_data->u.build->link_cache) { - xattr_data->u.build->link_cache = new alist(10, not_owned_by_alist); + xattr_data->u.build->link_cache + = new alist(10, not_owned_by_alist); } xattr_data->u.build->link_cache->append(ptr); } @@ -2410,7 +2411,7 @@ static BxattrExitCode solaris_save_xattr_acl(JobControlRecord* jcr, *acl_text = NULL; } retval = BxattrExitCode::kSuccess; -# else /* HAVE_EXTENDED_ACL */ +# else /* HAVE_EXTENDED_ACL */ int n; aclent_t* acls = NULL; @@ -2465,9 +2466,9 @@ static BxattrExitCode solaris_save_xattr_acl(JobControlRecord* jcr, *acl_text = NULL; } retval = BxattrExitCode::kSuccess; -# endif /* HAVE_EXTENDED_ACL */ +# endif /* HAVE_EXTENDED_ACL */ -# else /* HAVE_ACL */ +# else /* HAVE_ACL */ retval = BxattrExitCode::kSuccess; # endif /* HAVE_ACL */ @@ -3014,7 +3015,7 @@ static BxattrExitCode solaris_restore_xattr_acl(JobControlRecord* jcr, # endif /* HAVE_EXTENDED_ACL */ } -# endif /* HAVE_ACL */ +# endif /* HAVE_ACL */ static BxattrExitCode solaris_restore_xattrs(JobControlRecord* jcr, XattrData* xattr_data, diff --git a/core/src/findlib/xattr.h b/core/src/findlib/xattr.h index 3fcc259badd..80cebaac04a 100644 --- a/core/src/findlib/xattr.h +++ b/core/src/findlib/xattr.h @@ -70,7 +70,7 @@ struct xattr_build_data_t { uint32_t nr_saved; POOLMEM* content; uint32_t content_length; - alist* link_cache; + alist* link_cache; }; struct xattr_parse_data_t { @@ -94,21 +94,22 @@ struct XattrData { // Upperlimit on a xattr internal buffer #define XATTR_BUFSIZ 1024 +template class alist; BxattrExitCode SendXattrStream(JobControlRecord* jcr, XattrData* xattr_data, int stream); -void XattrDropInternalTable(alist* xattr_value_list); +void XattrDropInternalTable(alist* xattr_value_list); uint32_t SerializeXattrStream(JobControlRecord* jcr, XattrData* xattr_data, uint32_t expected_serialize_len, - alist* xattr_value_list); + alist* xattr_value_list); BxattrExitCode UnSerializeXattrStream(JobControlRecord* jcr, XattrData* xattr_data, char* content, uint32_t content_length, - alist* xattr_value_list); + alist* xattr_value_list); BxattrExitCode BuildXattrStreams(JobControlRecord* jcr, struct XattrData* xattr_data, FindFilesPacket* ff_pkt); diff --git a/core/src/include/jcr.h b/core/src/include/jcr.h index 7691299d70b..77e04b70c2a 100644 --- a/core/src/include/jcr.h +++ b/core/src/include/jcr.h @@ -39,11 +39,18 @@ #include "include/job_level.h" #include "include/job_status.h" #include "include/job_types.h" +#include "lib/message_queue_item.h" #include "lib/alist.h" #include "lib/tls_conf.h" +#include "lib/breg.h" +#include "lib/dlink.h" +#include + +struct job_callback_item; class BareosDb; class BareosSocket; +template class dlist; class htable; class JobControlRecord; @@ -138,16 +145,16 @@ class JobControlRecord { void SetKillable(bool killable); /**< in lib/jcr.c */ bool IsKillable() const { return my_thread_killable; } - dlink link; /**< JobControlRecord chain link */ + dlink link; /**< JobControlRecord chain link */ pthread_t my_thread_id{}; /**< Id of thread controlling jcr */ BareosSocket* dir_bsock{}; /**< Director bsock or NULL if we are him */ BareosSocket* store_bsock{}; /**< Storage connection socket */ BareosSocket* file_bsock{}; /**< File daemon connection socket */ JCR_free_HANDLER* daemon_free_jcr{}; /**< Local free routine */ - dlist* msg_queue{}; /**< Queued messages */ + dlist* msg_queue{}; /**< Queued messages */ pthread_mutex_t msg_queue_mutex = PTHREAD_MUTEX_INITIALIZER; /**< message queue mutex */ bool dequeuing_msgs{}; /**< Set when dequeuing messages */ - alist job_end_callbacks; /**< callbacks called at Job end */ + alist job_end_callbacks; /**< callbacks called at Job end */ POOLMEM* VolumeName{}; /**< Volume name desired -- pool_memory */ POOLMEM* errmsg{}; /**< Edited error message */ char Job[MAX_NAME_LENGTH]{}; /**< Unique name of this Job */ @@ -185,7 +192,7 @@ class JobControlRecord { uint32_t ClientId{}; /**< Client associated with Job */ char* where{}; /**< Prefix to restore files to */ char* RegexWhere{}; /**< File relocation in restore */ - alist* where_bregexp{}; /**< BareosRegex alist for path manipulation */ + alist* where_bregexp{}; /**< BareosRegex alist for path manipulation */ int32_t cached_pnl{}; /**< Cached path length */ POOLMEM* cached_path{}; /**< Cached path */ bool passive_client{}; /**< Client is a passive client e.g. doesn't initiate any network connection */ @@ -220,7 +227,7 @@ class JobControlRecord { AttributesDbRecord* ar{}; /**< DB attribute record */ guid_list* id_list{}; /**< User/group id to name list */ - alist* plugin_ctx_list{}; /**< List of contexts for plugins */ + alist* plugin_ctx_list{}; /**< List of contexts for plugins */ PluginContext* plugin_ctx{}; /**< Current plugin context */ POOLMEM* comment{}; /**< Comment for this Job */ int64_t max_bandwidth{}; /**< Bandwidth limit for this Job */ diff --git a/core/src/lib/address_conf.cc b/core/src/lib/address_conf.cc index 6e50652e04a..bb21c845826 100644 --- a/core/src/lib/address_conf.cc +++ b/core/src/lib/address_conf.cc @@ -3,7 +3,7 @@ Copyright (C) 2004-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -263,7 +263,19 @@ const char* IPADDR::build_address_str(char* buf, return buf; } -const char* BuildAddressesString(dlist* addrs, + +// check if two addresses are the same +bool IsSameIpAddress(IPADDR* first, IPADDR* second) +{ + if (first == nullptr || second == nullptr) { return false; } + return (first->GetSockaddrLen() == second->GetSockaddrLen() + && memcmp(first->get_sockaddr(), second->get_sockaddr(), + first->GetSockaddrLen()) + == 0); +} + + +const char* BuildAddressesString(dlist* addrs, char* buf, int blen, bool print_port /*=true*/) @@ -285,12 +297,12 @@ const char* BuildAddressesString(dlist* addrs, return buf; } -const char* GetFirstAddress(dlist* addrs, char* outputbuf, int outlen) +const char* GetFirstAddress(dlist* addrs, char* outputbuf, int outlen) { return ((IPADDR*)(addrs->first()))->GetAddress(outputbuf, outlen); } -int GetFirstPortNetOrder(dlist* addrs) +int GetFirstPortNetOrder(dlist* addrs) { if (!addrs) { return 0; @@ -299,7 +311,7 @@ int GetFirstPortNetOrder(dlist* addrs) } } -int GetFirstPortHostOrder(dlist* addrs) +int GetFirstPortHostOrder(dlist* addrs) { if (!addrs) { return 0; @@ -308,7 +320,8 @@ int GetFirstPortHostOrder(dlist* addrs) } } -int AddAddress(dlist** out, + +int AddAddress(dlist** out, IPADDR::i_type type, unsigned short defaultport, int family, @@ -319,15 +332,15 @@ int AddAddress(dlist** out, { IPADDR* iaddr; IPADDR* jaddr; - dlist* hostaddrs; + dlist* hostaddrs; unsigned short port; IPADDR::i_type intype = type; buf[0] = 0; - dlist* addrs = (dlist*)(*(out)); + dlist* addrs = (dlist*)(*(out)); if (!addrs) { IPADDR* tmp = 0; - addrs = *out = new dlist(tmp, &tmp->link); + addrs = *out = new dlist(tmp, &tmp->link); } type = (type == IPADDR::R_SINGLE_PORT || type == IPADDR::R_SINGLE_ADDR) @@ -395,11 +408,7 @@ int AddAddress(dlist** out, IPADDR* clone; /* for duplicates */ foreach_dlist (jaddr, addrs) { - if (iaddr->GetSockaddrLen() == jaddr->GetSockaddrLen() - && !memcmp(iaddr->get_sockaddr(), jaddr->get_sockaddr(), - iaddr->GetSockaddrLen())) { - goto skip; /* no price */ - } + if (IsSameIpAddress(iaddr, jaddr)) { goto skip; /* no price */ } } clone = new IPADDR(*iaddr); clone->SetType(type); @@ -413,7 +422,7 @@ int AddAddress(dlist** out, return 1; } -void InitDefaultAddresses(dlist** out, const char* port) +void InitDefaultAddresses(dlist** out, const char* port) { char buf[1024]; unsigned short sport = str_to_int32(port); @@ -424,7 +433,7 @@ void InitDefaultAddresses(dlist** out, const char* port) } } -void FreeAddresses(dlist* addrs) +void FreeAddresses(dlist* addrs) { while (!addrs->empty()) { IPADDR* ptr = (IPADDR*)addrs->first(); diff --git a/core/src/lib/address_conf.h b/core/src/lib/address_conf.h index f87e96ad92d..3cd87b5727e 100644 --- a/core/src/lib/address_conf.h +++ b/core/src/lib/address_conf.h @@ -84,18 +84,18 @@ class IPADDR { const char* build_address_str(char* buf, int blen, bool print_port = true); /* private */ - dlink link; + dlink link; }; /* clang-format on */ -void InitDefaultAddresses(dlist** addr, const char* port); -void FreeAddresses(dlist* addrs); +void InitDefaultAddresses(dlist** addr, const char* port); +void FreeAddresses(dlist* addrs); -const char* GetFirstAddress(dlist* addrs, char* outputbuf, int outlen); -int GetFirstPortNetOrder(dlist* addrs); -int GetFirstPortHostOrder(dlist* addrs); +const char* GetFirstAddress(dlist* addrs, char* outputbuf, int outlen); +int GetFirstPortNetOrder(dlist* addrs); +int GetFirstPortHostOrder(dlist* addrs); -int AddAddress(dlist** out, +int AddAddress(dlist** out, IPADDR::i_type type, unsigned short defaultport, int family, @@ -103,7 +103,8 @@ int AddAddress(dlist** out, const char* port_str, char* buf, int buflen); -const char* BuildAddressesString(dlist* addrs, +bool IsSameIpAddress(IPADDR* first, IPADDR* second); +const char* BuildAddressesString(dlist* addrs, char* buf, int blen, bool print_port = true); diff --git a/core/src/lib/alist.cc b/core/src/lib/alist.cc index a5588259114..428b60e0ca9 100644 --- a/core/src/lib/alist.cc +++ b/core/src/lib/alist.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2016-2019 Bareos GmbH & Co. KG + Copyright (C) 2016-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -19,124 +19,3 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// Kern Sibbald, June MMIII -/** - * @file - * BAREOS array list routines - * - * alist is a simple malloc'ed array of pointers. For the moment, - * it simply malloc's a bigger array controlled by num_grow. - * Default is to realloc the pointer array for each new member. - */ - -#include "include/bareos.h" -#include "lib/alist.h" - -/** - * Private grow list function. Used to insure that - * at least one more "slot" is available. - */ -void alist::GrowList() -{ - if (items == NULL) { - if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } - items = (void**)malloc(num_grow * sizeof(void*)); - max_items = num_grow; - } else if (num_items == max_items) { - max_items += num_grow; - items = (void**)realloc(items, max_items * sizeof(void*)); - } -} - -void* alist::first() -{ - cur_item = 1; - if (num_items == 0) { - return NULL; - } else { - return items[0]; - } -} - -void* alist::last() -{ - if (num_items == 0) { - return NULL; - } else { - cur_item = num_items; - return items[num_items - 1]; - } -} - -void* alist::next() -{ - if (cur_item >= num_items) { - return NULL; - } else { - return items[cur_item++]; - } -} - -void* alist::prev() -{ - if (cur_item <= 1) { - return NULL; - } else { - return items[--cur_item]; - } -} - -// prepend an item to the list -- i.e. add to beginning -void alist::prepend(void* item) -{ - GrowList(); - if (num_items == 0) { - items[num_items++] = item; - return; - } - for (int i = num_items; i > 0; i--) { items[i] = items[i - 1]; } - items[0] = item; - num_items++; -} - - -// Append an item to the list -void alist::append(void* item) -{ - GrowList(); - items[num_items++] = item; -} - -/* Remove an item from the list */ -void* alist::remove(int index) -{ - void* item; - if (index < 0 || index >= num_items) { return NULL; } - item = items[index]; - num_items--; - for (int i = index; i < num_items; i++) { items[i] = items[i + 1]; } - return item; -} - - -/* Get the index item -- we should probably allow real indexing here */ -void* alist::get(int index) -{ - if (index < 0 || index >= num_items) { return NULL; } - return items[index]; -} - -/* Destroy the list and its contents */ -void alist::destroy() -{ - if (items) { - if (own_items) { - for (int i = 0; i < num_items; i++) { - free(items[i]); - items[i] = NULL; - } - } - free(items); - items = NULL; - } -} diff --git a/core/src/lib/alist.h b/core/src/lib/alist.h index dca66291a24..a5d3eac72d3 100644 --- a/core/src/lib/alist.h +++ b/core/src/lib/alist.h @@ -20,16 +20,14 @@ 02110-1301, USA. */ // Kern Sibbald, June MMIII -/** - * @file +/* @file * alist header file */ #ifndef BAREOS_LIB_ALIST_H_ #define BAREOS_LIB_ALIST_H_ -/** - * There is a lot of extra casting here to work around the fact +/* There is a lot of extra casting here to work around the fact * that some compilers (Sun and Visual C++) do not accept * (void *) as an lvalue on the left side of an equal. * @@ -80,6 +78,11 @@ (inx)--) #endif + +#include +#include + + // Second arg of init enum { @@ -87,12 +90,13 @@ enum not_owned_by_alist = false }; -/** - * Array list -- much like a simplified STL vector +/* Array list -- much like a simplified STL vector * array of pointers to inserted items */ + +template class alist { - void** items = nullptr; + T* items = nullptr; int num_items = 0; int max_items = 0; int num_grow = 0; @@ -101,44 +105,52 @@ class alist { void GrowList(void); public: + // Ueb disable non pointer initialization alist(int num = 1, bool own = true); ~alist(); void init(int num = 1, bool own = true); - void append(void* item); - void prepend(void* item); - void* remove(int index); - void* get(int index); + void append(T item); + void prepend(T item); + T remove(int index); + T get(int index); bool empty() const; - void* prev(); - void* next(); - void* first(); - void* last(); - void* operator[](int index) const; + T prev(); + T next(); + T first(); + T last(); + T operator[](int index) const; int current() const { return cur_item; } int size() const; void destroy(); void grow(int num); - // Use it as a stack, pushing and poping from the end - void push(void* item) { append(item); } - void* pop() { return remove(num_items - 1); } + std::list to_std_list_string(); + + // Use it as a stack, pushing and popping from the end + void push(T item) { append(item); } + T pop() { return remove(num_items - 1); } }; // Define index operator [] -inline void* alist::operator[](int index) const +template +inline T alist::operator[](int index) const { if (index < 0 || index >= num_items) { return nullptr; } return items[index]; } -inline bool alist::empty() const { return num_items == 0; } +template +inline bool alist::empty() const +{ + return num_items == 0; +} -/** - * This allows us to do explicit initialization, +/* This allows us to do explicit initialization, * allowing us to mix C++ classes inside malloc'ed * C structures. Define before called in constructor. */ -inline void alist::init(int num, bool own) +template +inline void alist::init(int num, bool own) { items = nullptr; num_items = 0; @@ -148,18 +160,22 @@ inline void alist::init(int num, bool own) cur_item = 0; } -/* Constructor */ -inline alist::alist(int num, bool own) { init(num, own); } - -/* Destructor */ -inline alist::~alist() { destroy(); } +template +inline alist::alist(int num, bool own) +{ + init(num, own); +} +template +inline alist::~alist() +{ + destroy(); +} -/* Current size of list */ -inline int alist::size() const +template +inline int alist::size() const { - /* - * Check for null pointer, which allows test + /* Check for null pointer, which allows test * on size to succeed even if nothing put in * alist. */ @@ -167,6 +183,128 @@ inline int alist::size() const } /* How much to grow by each time */ -inline void alist::grow(int num) { num_grow = num; } +template +inline void alist::grow(int num) +{ + num_grow = num; +} + +/* Private grow list function. Used to insure that + * at least one more "slot" is available. + */ +template +void alist::GrowList() +{ + if (items == NULL) { + if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } + items = (T*)malloc(num_grow * sizeof(T)); + max_items = num_grow; + } else if (num_items == max_items) { + max_items += num_grow; + items = (T*)realloc(items, max_items * sizeof(T)); + } +} + +template +T alist::first() +{ + cur_item = 1; + if (num_items == 0) { + return NULL; + } else { + return items[0]; + } +} + +template +T alist::last() +{ + if (num_items == 0) { + return NULL; + } else { + cur_item = num_items; + return items[num_items - 1]; + } +} + +template +T alist::next() +{ + if (cur_item >= num_items) { + return NULL; + } else { + return items[cur_item++]; + } +} + +template +T alist::prev() +{ + if (cur_item <= 1) { + return NULL; + } else { + return items[--cur_item]; + } +} + +// prepend an item to the list -- i.e. add to beginning +template +void alist::prepend(T item) +{ + GrowList(); + if (num_items == 0) { + items[num_items++] = item; + return; + } + for (int i = num_items; i > 0; i--) { items[i] = items[i - 1]; } + items[0] = item; + num_items++; +} + + +// Append an item to the list +template +void alist::append(T item) +{ + GrowList(); + items[num_items++] = item; +} + +template +/* Remove an item from the list */ +T alist::remove(int index) +{ + T item; + if (index < 0 || index >= num_items) { return NULL; } + item = items[index]; + num_items--; + for (int i = index; i < num_items; i++) { items[i] = items[i + 1]; } + return item; +} + + +/* Get the index item -- we should probably allow real indexing here */ +template +T alist::get(int index) +{ + if (index < 0 || index >= num_items) { return NULL; } + return items[index]; +} + +/* Destroy the list and its contents */ +template +void alist::destroy() +{ + if (items) { + if (own_items) { + for (int i = 0; i < num_items; i++) { + free((void*)(items[i])); + items[i] = NULL; + } + } + free(items); + items = NULL; + } +} #endif // BAREOS_LIB_ALIST_H_ diff --git a/core/src/lib/bnet.cc b/core/src/lib/bnet.cc index 4864ebfa418..2438f7bb704 100644 --- a/core/src/lib/bnet.cc +++ b/core/src/lib/bnet.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -253,7 +253,7 @@ int BnetWaitDataIntr(BareosSocket* bsock, int sec) #endif #if HAVE_GETADDRINFO -const char* resolv_host(int family, const char* host, dlist* addr_list) +const char* resolv_host(int family, const char* host, dlist* addr_list) { int res; struct addrinfo hints; @@ -390,7 +390,9 @@ static IPADDR* add_any(int family) } // i host = 0 mean INADDR_ANY only ipv4 -dlist* BnetHost2IpAddrs(const char* host, int family, const char** errstr) +dlist* BnetHost2IpAddrs(const char* host, + int family, + const char** errstr) { struct in_addr inaddr; IPADDR* addr = 0; @@ -399,7 +401,7 @@ dlist* BnetHost2IpAddrs(const char* host, int family, const char** errstr) struct in6_addr inaddr6; #endif - dlist* addr_list = new dlist(addr, &addr->link); + dlist* addr_list = new dlist(addr, &addr->link); if (!host || host[0] == '\0') { if (family != 0) { addr_list->append(add_any(family)); diff --git a/core/src/lib/bnet.h b/core/src/lib/bnet.h index 632c12f72af..877af7d981d 100644 --- a/core/src/lib/bnet.h +++ b/core/src/lib/bnet.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2020 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -22,7 +22,8 @@ #define BAREOS_LIB_BNET_H_ #include "tls.h" - +#include "lib/address_conf.h" +template class dlist; int32_t BnetRecv(BareosSocket* bsock); @@ -46,7 +47,9 @@ int BnetWaitDataIntr(BareosSocket* bsock, int sec); bool IsBnetStop(BareosSocket* bsock); int IsBnetError(BareosSocket* bsock); void BnetSuppressErrorMessages(BareosSocket* bsock, bool flag); -dlist* BnetHost2IpAddrs(const char* host, int family, const char** errstr); +dlist* BnetHost2IpAddrs(const char* host, + int family, + const char** errstr); int BnetSetBlocking(BareosSocket* sock); int BnetSetNonblocking(BareosSocket* sock); void BnetRestoreBlocking(BareosSocket* sock, int flags); diff --git a/core/src/lib/bnet_server_tcp.cc b/core/src/lib/bnet_server_tcp.cc index 18938048225..e617f49ab3f 100644 --- a/core/src/lib/bnet_server_tcp.cc +++ b/core/src/lib/bnet_server_tcp.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -37,6 +37,7 @@ #include "lib/bsys.h" #include "lib/thread_list.h" #include "lib/watchdog.h" +#include "lib/address_conf.h" #include #include @@ -64,11 +65,6 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static std::atomic quit{false}; -struct s_sockfd { - int fd; - int port; -}; - /** * Stop the Threaded Network Server if its realy running in a separate thread. * e.g. set the quit flag and wait for the other thread to exit cleanly. @@ -88,7 +84,8 @@ void BnetStopAndWaitForThreadServerTcp(pthread_t tid) * Perform a cleanup for the Threaded Network Server check if there is still * something to do or that the cleanup already took place. */ -static void CleanupBnetThreadServerTcp(alist* sockfds, ThreadList& thread_list) +static void CleanupBnetThreadServerTcp(alist* sockfds, + ThreadList& thread_list) { Dmsg0(100, "CleanupBnetThreadServerTcp: start\n"); @@ -109,7 +106,8 @@ static void CleanupBnetThreadServerTcp(alist* sockfds, ThreadList& thread_list) class BNetThreadServerCleanupObject { public: - BNetThreadServerCleanupObject(alist* sockfds, ThreadList& thread_list) + BNetThreadServerCleanupObject(alist* sockfds, + ThreadList& thread_list) : sockfds_(sockfds), thread_list_(thread_list) { } @@ -120,11 +118,11 @@ class BNetThreadServerCleanupObject { } private: - alist* sockfds_; + alist* sockfds_; ThreadList& thread_list_; }; -static void RemoveDuplicateAddresses(dlist* addr_list) +static void RemoveDuplicateAddresses(dlist* addr_list) { IPADDR* ipaddr = nullptr; IPADDR* next = nullptr; @@ -135,10 +133,7 @@ static void RemoveDuplicateAddresses(dlist* addr_list) next = (IPADDR*)addr_list->next(ipaddr); while (next) { // See if the addresses match. - if (ipaddr->GetSockaddrLen() == next->GetSockaddrLen() - && memcmp(ipaddr->get_sockaddr(), next->get_sockaddr(), - ipaddr->GetSockaddrLen()) - == 0) { + if (IsSameIpAddress(ipaddr, next)) { to_free = next; next = (IPADDR*)addr_list->next(next); addr_list->remove(to_free); @@ -150,7 +145,7 @@ static void RemoveDuplicateAddresses(dlist* addr_list) } } -static void LogAllAddresses(dlist* addr_list) +static void LogAllAddresses(dlist* addr_list) { std::vector buf(256 * addr_list->size()); @@ -159,7 +154,7 @@ static void LogAllAddresses(dlist* addr_list) } static int OpenSocketAndBind(IPADDR* ipaddr, - dlist* addr_list, + dlist* addr_list, uint16_t port_number) { int fd = -1; @@ -223,9 +218,9 @@ static int OpenSocketAndBind(IPADDR* ipaddr, * At the moment it is impossible to bind to different ports. */ void BnetThreadServerTcp( - dlist* addr_list, + dlist* addr_list, int max_clients, - alist* sockfds, + alist* sockfds, ThreadList& thread_list, std::function HandleConnectionRequest, diff --git a/core/src/lib/bnet_server_tcp.h b/core/src/lib/bnet_server_tcp.h index f1b88828932..e9560e89381 100644 --- a/core/src/lib/bnet_server_tcp.h +++ b/core/src/lib/bnet_server_tcp.h @@ -26,6 +26,7 @@ class ConfigurationParser; class ThreadList; +class IPADDR; enum class BnetServerState { @@ -36,13 +37,18 @@ enum class BnetServerState kEnded }; -class alist; +template class dlist; +struct s_sockfd { + int fd; + int port; +}; + void BnetThreadServerTcp( - dlist* addr_list, + dlist* addr_list, int max_clients, - alist* sockfds, + alist* sockfds, ThreadList& thread_list, std::function HandleConnectionRequest, diff --git a/core/src/lib/breg.cc b/core/src/lib/breg.cc index 6c84ef7b49a..aff00a3de9d 100644 --- a/core/src/lib/breg.cc +++ b/core/src/lib/breg.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2006-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -63,7 +63,7 @@ void FreeBregexp(BareosRegex* self) /* Free a bregexps alist */ -void FreeBregexps(alist* bregexps) +void FreeBregexps(alist* bregexps) { Dmsg0(500, "bregexp: freeing all BareosRegex object\n"); @@ -75,7 +75,9 @@ void FreeBregexps(alist* bregexps) /* Apply all regexps to fname */ -bool ApplyBregexps(const char* fname, alist* bregexps, char** result) +bool ApplyBregexps(const char* fname, + alist* bregexps, + char** result) { BareosRegex* elt = nullptr; bool ok = false; @@ -94,10 +96,10 @@ bool ApplyBregexps(const char* fname, alist* bregexps, char** result) /* return an alist of BareosRegex or return NULL if it's not a * where=!tmp!opt!ig,!temp!opt!i */ -alist* get_bregexps(const char* where) +alist* get_bregexps(const char* where) { char* p = (char*)where; - alist* list = new alist(10, not_owned_by_alist); + alist* list = new alist(10, not_owned_by_alist); BareosRegex* reg; reg = NewBregexp(p); diff --git a/core/src/lib/breg.h b/core/src/lib/breg.h index 28f8bc8a4ac..27e5660b39d 100644 --- a/core/src/lib/breg.h +++ b/core/src/lib/breg.h @@ -76,23 +76,25 @@ class BareosRegex { /* create new BareosRegex and compile regex_t */ BareosRegex* NewBregexp(const char* motif); - +template class alist; /* launch each bregexp on filename */ -int RunBregexp(alist* bregexps, const char* fname); +int RunBregexp(alist* bregexps, const char* fname); /* free BareosRegex (and all POOLMEM) */ void FreeBregexp(BareosRegex* script); /* fill an alist with BareosRegex from where */ -alist* get_bregexps(const char* where); +alist* get_bregexps(const char* where); /* apply every regexps from the alist */ -bool ApplyBregexps(const char* fname, alist* bregexps, char** result); +bool ApplyBregexps(const char* fname, + alist* bregexps, + char** result); /* foreach_alist free RunScript */ -void FreeBregexps(alist* bregexps); /* you have to free alist */ +void FreeBregexps(alist* bregexps); /* you have to free alist */ /* get regexp size */ int BregexpGetBuildWhereSize(char* strip_prefix, diff --git a/core/src/lib/bsock.cc b/core/src/lib/bsock.cc index 4accb06bc2e..9769b69046b 100644 --- a/core/src/lib/bsock.cc +++ b/core/src/lib/bsock.cc @@ -3,7 +3,7 @@ Copyright (C) 2007-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -152,7 +152,7 @@ void BareosSocket::CloseTlsConnectionAndFreeMemory() } // Copy the address from the configuration dlist that gets passed in -void BareosSocket::SetSourceAddress(dlist* src_addr_list) +void BareosSocket::SetSourceAddress(dlist* src_addr_list) { char allbuf[256 * 10]; IPADDR* addr = nullptr; @@ -167,7 +167,7 @@ void BareosSocket::SetSourceAddress(dlist* src_addr_list) } if (src_addr_list) { - addr = (IPADDR*)src_addr_list->first(); + addr = src_addr_list->first(); src_addr = new IPADDR(*addr); } } diff --git a/core/src/lib/bsock.h b/core/src/lib/bsock.h index a96288b17ba..6992d541058 100644 --- a/core/src/lib/bsock.h +++ b/core/src/lib/bsock.h @@ -52,6 +52,7 @@ class BareosSocket; class Tls; class BStringList; class QualifiedResourceNameTypeConverter; +template class dlist; btimer_t* StartBsockTimer(BareosSocket* bs, uint32_t wait); void StopBsockTimer(btimer_t* wid); @@ -207,7 +208,7 @@ class BareosSocket { JobControlRecord* jcr = nullptr); bool SetLocking(); /* in bsock.c */ void ClearLocking(); /* in bsock.c */ - void SetSourceAddress(dlist* src_addr_list); + void SetSourceAddress(dlist* src_addr_list); void ControlBwlimit(int bytes); /* in bsock.c */ bool EvaluateCleartextBareosHello(bool& cleartext, std::string& client_name_out, @@ -316,7 +317,7 @@ enum BNET_TEXT_INPUT = -28 /* Get text input from user */ }; -#define BNET_SETBUF_READ 1 /* Arg for BnetSetBufferSize */ +#define BNET_SETBUF_READ 1 /* Arg for BnetSetBufferSize */ #define BNET_SETBUF_WRITE 2 /* Arg for BnetSetBufferSize */ /** diff --git a/core/src/lib/bsock_tcp.cc b/core/src/lib/bsock_tcp.cc index c351707c726..63189912480 100644 --- a/core/src/lib/bsock_tcp.cc +++ b/core/src/lib/bsock_tcp.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2007-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -168,7 +168,7 @@ bool BareosSocketTCP::open(JobControlRecord* jcr, int* fatal) { int sockfd = -1; - dlist* addr_list; + dlist* addr_list; IPADDR *ipaddr, *next, *to_free; bool connected = false; int value; @@ -194,11 +194,7 @@ bool BareosSocketTCP::open(JobControlRecord* jcr, ipaddr = (IPADDR*)addr_list->next(ipaddr)) { next = (IPADDR*)addr_list->next(ipaddr); while (next) { - // See if the addresses match. - if (ipaddr->GetSockaddrLen() == next->GetSockaddrLen() - && memcmp(ipaddr->get_sockaddr(), next->get_sockaddr(), - ipaddr->GetSockaddrLen()) - == 0) { + if (IsSameIpAddress(ipaddr, next)) { to_free = next; next = (IPADDR*)addr_list->next(next); addr_list->remove(to_free); diff --git a/core/src/lib/connection_pool.cc b/core/src/lib/connection_pool.cc index e9e9a79a25e..00767aa7a62 100644 --- a/core/src/lib/connection_pool.cc +++ b/core/src/lib/connection_pool.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2016-2020 Bareos GmbH & Co. KG + Copyright (C) 2016-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -88,7 +88,7 @@ bool Connection::take() // Connection Pool ConnectionPool::ConnectionPool() { - connections_ = new alist(10, false); + connections_ = new alist(10, false); // Initialize mutex and condition variable objects. pthread_mutex_init(&add_mutex_, nullptr); pthread_cond_init(&add_cond_var_, nullptr); @@ -106,7 +106,7 @@ void ConnectionPool::cleanup() Connection* connection = nullptr; int i = 0; for (i = connections_->size() - 1; i >= 0; i--) { - connection = (Connection*)connections_->get(i); + connection = connections_->get(i); Dmsg2(800, "checking connection %s (%d)\n", connection->name(), i); if (!connection->check()) { Dmsg2(120, "connection %s (%d) is terminated => removed\n", @@ -117,7 +117,7 @@ void ConnectionPool::cleanup() } } -alist* ConnectionPool::get_as_alist() +alist* ConnectionPool::get_as_alist() { cleanup(); return connections_; diff --git a/core/src/lib/connection_pool.h b/core/src/lib/connection_pool.h index ef751f41873..6a368d04745 100644 --- a/core/src/lib/connection_pool.h +++ b/core/src/lib/connection_pool.h @@ -28,7 +28,7 @@ #ifndef BAREOS_LIB_CONNECTION_POOL_H_ #define BAREOS_LIB_CONNECTION_POOL_H_ - +template class alist; class BareosSocket; @@ -73,11 +73,11 @@ class ConnectionPool { BareosSocket* socket, bool authenticated = true); Connection* remove(const char* name, int timeout_in_seconds = 0); - alist* get_as_alist(); + alist* get_as_alist(); void cleanup(); private: - alist* connections_; + alist* connections_; int WaitForNewConnection(timespec& timeout); bool add(Connection* connection); bool remove(Connection* connection); diff --git a/core/src/lib/crypto.h b/core/src/lib/crypto.h index c4b5c56621b..ef085c0f9da 100644 --- a/core/src/lib/crypto.h +++ b/core/src/lib/crypto.h @@ -28,6 +28,7 @@ #ifndef BAREOS_LIB_CRYPTO_H_ #define BAREOS_LIB_CRYPTO_H_ +template class alist; /* Opaque X509 Public/Private Key Pair Structure */ @@ -102,8 +103,8 @@ typedef enum } crypto_error_t; /* Message Digest Sizes */ -#define CRYPTO_DIGEST_MD5_SIZE 16 /* 128 bits */ -#define CRYPTO_DIGEST_SHA1_SIZE 20 /* 160 bits */ +#define CRYPTO_DIGEST_MD5_SIZE 16 /* 128 bits */ +#define CRYPTO_DIGEST_SHA1_SIZE 20 /* 160 bits */ #define CRYPTO_DIGEST_SHA256_SIZE 32 /* 256 bits */ #define CRYPTO_DIGEST_SHA512_SIZE 64 /* 512 bits */ @@ -153,12 +154,13 @@ SIGNATURE* crypto_sign_decode(JobControlRecord* jcr, const uint8_t* sigData, uint32_t length); void CryptoSignFree(SIGNATURE* sig); -CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, alist* pubkeys); +CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, + alist* pubkeys); void CryptoSessionFree(CRYPTO_SESSION* cs); bool CryptoSessionEncode(CRYPTO_SESSION* cs, uint8_t* dest, uint32_t* length); crypto_error_t CryptoSessionDecode(const uint8_t* data, uint32_t length, - alist* keypairs, + alist* keypairs, CRYPTO_SESSION** session); CRYPTO_SESSION* CryptoSessionDecode(const uint8_t* data, uint32_t length); CIPHER_CONTEXT* crypto_cipher_new(CRYPTO_SESSION* cs, diff --git a/core/src/lib/crypto_cache.cc b/core/src/lib/crypto_cache.cc index 3d920d85c54..e7640c7af3f 100644 --- a/core/src/lib/crypto_cache.cc +++ b/core/src/lib/crypto_cache.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -31,7 +31,7 @@ #include "lib/dlist.h" static pthread_mutex_t crypto_cache_lock = PTHREAD_MUTEX_INITIALIZER; -static dlist* cached_crypto_keys = NULL; +static dlist* cached_crypto_keys = NULL; static s_crypto_cache_hdr crypto_cache_hdr = {"BAREOS Crypto Cache\n", 1, 0}; @@ -74,7 +74,9 @@ void ReadCryptoCache(const char* cache_file) goto bail_out; } - if (!cached_crypto_keys) { cached_crypto_keys = new dlist(cce, &cce->link); } + if (!cached_crypto_keys) { + cached_crypto_keys = new dlist(cce, &cce->link); + } // Read as many crypto cache entries as available. cnt = 0; @@ -205,7 +207,7 @@ bool UpdateCryptoCache(const char* VolumeName, const char* EncryptionKey) // See if there are any cached encryption keys. if (!cached_crypto_keys) { - cached_crypto_keys = new dlist(cce, &cce->link); + cached_crypto_keys = new dlist(cce, &cce->link); cce = (crypto_cache_entry_t*)malloc(sizeof(crypto_cache_entry_t)); bstrncpy(cce->VolumeName, VolumeName, sizeof(cce->VolumeName)); diff --git a/core/src/lib/crypto_cache.h b/core/src/lib/crypto_cache.h index 3770a268820..f5cffb83529 100644 --- a/core/src/lib/crypto_cache.h +++ b/core/src/lib/crypto_cache.h @@ -39,7 +39,7 @@ struct s_crypto_cache_hdr { }; struct crypto_cache_entry_t { - dlink link; + dlink link; char VolumeName[MAX_NAME_LENGTH]; char EncryptionKey[MAX_NAME_LENGTH]; utime_t added; diff --git a/core/src/lib/crypto_none.cc b/core/src/lib/crypto_none.cc index bc71845a099..e9b923b639d 100644 --- a/core/src/lib/crypto_none.cc +++ b/core/src/lib/crypto_none.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2005-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -171,7 +171,8 @@ int CryptoKeypairLoadKey(X509_KEYPAIR* keypair, void CryptoKeypairFree(X509_KEYPAIR* keypair) {} -CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, alist* pubkeys) +CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, + alist* pubkeys) { return NULL; } @@ -185,7 +186,7 @@ bool CryptoSessionEncode(CRYPTO_SESSION* cs, uint8_t* dest, uint32_t* length) crypto_error_t CryptoSessionDecode(const uint8_t* data, uint32_t length, - alist* keypairs, + alist* keypairs, CRYPTO_SESSION** session) { return CRYPTO_ERROR_INTERNAL; diff --git a/core/src/lib/crypto_nss.cc b/core/src/lib/crypto_nss.cc index abba30529e0..d57826b3b28 100644 --- a/core/src/lib/crypto_nss.cc +++ b/core/src/lib/crypto_nss.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -172,7 +172,8 @@ int CryptoKeypairLoadKey(X509_KEYPAIR* keypair, void CryptoKeypairFree(X509_KEYPAIR* keypair) {} -CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, alist* pubkeys) +CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, + alist* pubkeys) { return NULL; } @@ -186,7 +187,7 @@ bool CryptoSessionEncode(CRYPTO_SESSION* cs, uint8_t* dest, uint32_t* length) crypto_error_t CryptoSessionDecode(const uint8_t* data, uint32_t length, - alist* keypairs, + alist* keypairs, CRYPTO_SESSION** session) { return CRYPTO_ERROR_INTERNAL; diff --git a/core/src/lib/crypto_openssl.cc b/core/src/lib/crypto_openssl.cc index 3f79ceb719d..ee5bd4a9541 100644 --- a/core/src/lib/crypto_openssl.cc +++ b/core/src/lib/crypto_openssl.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2005-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -38,9 +38,7 @@ # include "jcr.h" # include - # include "lib/alist.h" - # include # include # include @@ -1015,7 +1013,8 @@ void CryptoSignFree(SIGNATURE* sig) * * Note! BAREOS malloc() fails if out of memory. */ -CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, alist* pubkeys) +CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, + alist* pubkeys) { CRYPTO_SESSION* cs; X509_KEYPAIR* keypair = nullptr; @@ -1102,7 +1101,7 @@ CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, alist* pubkeys) break; # endif # endif /* OPENSSL_NO_CAMELLIA */ -# endif /* HAVE_OPENSSL_EXPORT_LIBRARY */ +# endif /* HAVE_OPENSSL_EXPORT_LIBRARY */ # if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1) # ifdef NID_aes_128_cbc_hmac_sha1 case CRYPTO_CIPHER_AES_128_CBC_HMAC_SHA1: @@ -1249,7 +1248,7 @@ bool CryptoSessionEncode(CRYPTO_SESSION* cs, uint8_t* dest, uint32_t* length) */ crypto_error_t CryptoSessionDecode(const uint8_t* data, uint32_t length, - alist* keypairs, + alist* keypairs, CRYPTO_SESSION** session) { CRYPTO_SESSION* cs; diff --git a/core/src/lib/dlink.h b/core/src/lib/dlink.h index b96971717be..185dd475acb 100644 --- a/core/src/lib/dlink.h +++ b/core/src/lib/dlink.h @@ -23,9 +23,10 @@ #ifndef BAREOS_LIB_DLINK_H_ #define BAREOS_LIB_DLINK_H_ +template struct dlink { - void* next; - void* prev; + T* next; + T* prev; dlink() { next = nullptr; diff --git a/core/src/lib/dlist.cc b/core/src/lib/dlist.cc index 9d4db03631e..5abf582c794 100644 --- a/core/src/lib/dlist.cc +++ b/core/src/lib/dlist.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -27,288 +27,8 @@ * Kern Sibbald, July MMIII */ -#include "include/bareos.h" #include "lib/dlist.h" - -// Init dlist -void dlist::init(void* item, dlink* link) -{ - head = tail = NULL; - loffset = (int)((char*)link - (char*)item); - if (loffset < 0 || loffset > 5000) { - Emsg0(M_ABORT, 0, "Improper dlist initialization.\n"); - } - num_items = 0; -} - -// Append an item to the list -void dlist::append(void* item) -{ - SetNext(item, NULL); - SetPrev(item, tail); - if (tail) { SetNext(tail, item); } - tail = item; - if (head == NULL) { /* if empty list, */ - head = item; /* item is head as well */ - } - num_items++; -} - -// Prepend an item to the list -void dlist::prepend(void* item) -{ - SetNext(item, head); - SetPrev(item, NULL); - if (head) { SetPrev(head, item); } - head = item; - if (tail == NULL) { /* if empty list, */ - tail = item; /* item is tail too */ - } - num_items++; -} - -void dlist::InsertBefore(void* item, void* where) -{ - dlink* where_link = get_link(where); - - SetNext(item, where); - SetPrev(item, where_link->prev); - - if (where_link->prev) { SetNext(where_link->prev, item); } - where_link->prev = item; - if (head == where) { head = item; } - num_items++; -} - -void dlist::InsertAfter(void* item, void* where) -{ - dlink* where_link = get_link(where); - - SetNext(item, where_link->next); - SetPrev(item, where); - - if (where_link->next) { SetPrev(where_link->next, item); } - where_link->next = item; - if (tail == where) { tail = item; } - num_items++; -} - -/* - * Insert an item in the list, but only if it is unique - * otherwise, the item is returned non inserted - * - * Returns: item if item inserted - * other_item if same value already exists (item not inserted) - */ -void* dlist::binary_insert(void* item, int compare(void* item1, void* item2)) -{ - int comp; - int low, high, cur; - void* cur_item; - - if (num_items == 0) { - // Dmsg0(000, "Append first.\n"); - append(item); - return item; - } - if (num_items == 1) { - comp = compare(item, first()); - if (comp < 0) { - prepend(item); - // Dmsg0(000, "Insert before first.\n"); - return item; - } else if (comp > 0) { - InsertAfter(item, first()); - // Dmsg0(000, "Insert after first.\n"); - return item; - } else { - // Dmsg0(000, "Same as first.\n"); - return first(); - } - } - /* Check against last item */ - comp = compare(item, last()); - if (comp > 0) { - append(item); - // Dmsg0(000, "Appended item.\n"); - return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as last.\n"); - return last(); - } - /* Check against first item */ - comp = compare(item, first()); - if (comp < 0) { - prepend(item); - // Dmsg0(000, "Inserted item before.\n"); - return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as first.\n"); - return first(); - } - if (num_items == 2) { - InsertAfter(item, first()); - // Dmsg0(000, "Inserted item after.\n"); - return item; - } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - while (nxt > cur) { - cur_item = next(cur_item); - cur++; - } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; - } - // Dmsg1(000, "Compare item to %d\n", cur); - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - // Dmsg1(000, "Same as item %d\n", cur); - return cur_item; - } - } - if (high == cur) { - InsertBefore(item, cur_item); - // Dmsg1(000, "Insert before item %d\n", cur); - } else { - InsertAfter(item, cur_item); - // Dmsg1(000, "Insert after item %d\n", cur); - } - return item; -} - -/* - * Insert an item in the list, regardless if it is unique - * or not. - */ -void dlist::BinaryInsertMultiple(void* item, - int compare(void* item1, void* item2)) -{ - void* ins_item = binary_insert(item, compare); - /* If identical, insert after the one found */ - if (ins_item != item) { InsertAfter(item, ins_item); } -} - -// Search for item -void* dlist::binary_search(void* item, int compare(void* item1, void* item2)) -{ - int comp; - int low, high, cur; - void* cur_item; - - - if (num_items == 0) { return NULL; } - cur_item = first(); - if (num_items == 1) { - comp = compare(item, cur_item); - if (comp == 0) { - return cur_item; - } else { - return NULL; - } - } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - /* Now get cur pointing to nxt */ - while (nxt > cur) { - cur_item = next(cur_item); - cur++; - } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; - } - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - return cur_item; - } - } - /* - * low == high can only happen if low just - * got incremented from cur, and we have - * not yet tested cur+1 - */ - if (low == high) { - cur_item = next(cur_item); - comp = compare(item, cur_item); - if (comp == 0) { return cur_item; } - } - return NULL; -} - - -void dlist::remove(void* item) -{ - void* xitem; - dlink* ilink = get_link(item); /* item's link */ - if (item == head) { - head = ilink->next; - if (head) { SetPrev(head, NULL); } - if (item == tail) { tail = ilink->prev; } - } else if (item == tail) { - tail = ilink->prev; - if (tail) { SetNext(tail, NULL); } - } else { - xitem = ilink->next; - SetPrev(xitem, ilink->prev); - xitem = ilink->prev; - SetNext(xitem, ilink->next); - } - num_items--; - if (num_items == 0) { head = tail = NULL; } -} - -void* dlist::next(void* item) -{ - if (item == NULL) { return head; } - return get_next(item); -} - -void* dlist::prev(void* item) -{ - if (item == NULL) { return tail; } - return get_prev(item); -} - - -/* Destroy the list contents */ -void dlist::destroy() -{ - for (void* n = head; n;) { - void* ni = get_next(n); - free(n); - n = ni; - } - num_items = 0; - head = tail = NULL; -} - /* String helpers for dlist usage */ dlistString* new_dlistString(const char* str) @@ -319,7 +39,8 @@ dlistString* new_dlistString(const char* str) dlistString* new_dlistString(const char* str, int len) { dlistString* node; - node = (dlistString*)malloc(sizeof(dlink) + len + 1); + node = (dlistString*)malloc(sizeof(dlink) + len + 1); bstrncpy(node->c_str(), str, len + 1); return node; } + diff --git a/core/src/lib/dlist.h b/core/src/lib/dlist.h index 35ae9810c57..b856039c83b 100644 --- a/core/src/lib/dlist.h +++ b/core/src/lib/dlist.h @@ -58,41 +58,43 @@ != nullptr;) #endif +template class dlist { - void* head; - void* tail; + T* head; + T* tail; int16_t loffset; uint32_t num_items; public: - dlist(void* item, dlink* link); + dlist(T* item, dlink* link); dlist(void); ~dlist() { destroy(); } - void init(void* item, dlink* link); + void init(T* item, dlink* link); void init(); - void prepend(void* item); - void append(void* item); - void SetPrev(void* item, void* prev); - void SetNext(void* item, void* next); - void* get_prev(void* item); - void* get_next(void* item); - dlink* get_link(void* item); - void InsertBefore(void* item, void* where); - void InsertAfter(void* item, void* where); - void* binary_insert(void* item, int compare(void* item1, void* item2)); - void* binary_search(void* item, int compare(void* item1, void* item2)); - void BinaryInsertMultiple(void* item, int compare(void* item1, void* item2)); - void remove(void* item); + void prepend(T* item); + void append(T* item); + void SetPrev(T* item, T* prev); + void SetNext(T* item, T* next); + T* get_prev(T* item); + T* get_next(T* item); + dlink* get_link(T* item); + void InsertBefore(T* item, T* where); + void InsertAfter(T* item, T* where); + T* binary_insert(T* item, int compare(T* item1, T* item2)); + T* binary_search(T* item, int compare(T* item1, T* item2)); + void BinaryInsertMultiple(T* item, int compare(T* item1, T* item2)); + void remove(T* item); bool empty() const; int size() const; - void* next(void* item); - void* prev(void* item); + T* next(T* item); + T* prev(T* item); void destroy(); - void* first() const; - void* last() const; + T* first() const; + T* last() const; }; -inline void dlist::init() +template +inline void dlist::init() { head = tail = nullptr; loffset = 0; @@ -108,50 +110,76 @@ inline void dlist::init() * then there is no need to specify the link address * since the offset is zero. */ -inline dlist::dlist(void* item, dlink* link) { init(item, link); } +template +inline dlist::dlist(T* item, dlink* link) +{ + init(item, link); +} /* Constructor with link at head of item */ -inline dlist::dlist(void) +template +inline dlist::dlist(void) : head(nullptr), tail(nullptr), loffset(0), num_items(0) { return; } -inline void dlist::SetPrev(void* item, void* prev) +template +inline void dlist::SetPrev(T* item, T* prev) { - ((dlink*)(((char*)item) + loffset))->prev = prev; + ((dlink*)(((char*)item) + loffset))->prev = prev; } -inline void dlist::SetNext(void* item, void* next) +template +inline void dlist::SetNext(T* item, T* next) { - ((dlink*)(((char*)item) + loffset))->next = next; + ((dlink*)(((char*)item) + loffset))->next = next; } -inline void* dlist::get_prev(void* item) +template +inline T* dlist::get_prev(T* item) { - return ((dlink*)(((char*)item) + loffset))->prev; + return ((dlink*)(((char*)item) + loffset))->prev; } -inline void* dlist::get_next(void* item) +template +inline T* dlist::get_next(T* item) { - return ((dlink*)(((char*)item) + loffset))->next; + return ((dlink*)(((char*)item) + loffset))->next; } -inline dlink* dlist::get_link(void* item) +template +inline dlink* dlist::get_link(T* item) { - return (dlink*)(((char*)item) + loffset); + return (dlink*)(((char*)item) + loffset); } -inline bool dlist::empty() const { return head == nullptr; } +template +inline bool dlist::empty() const +{ + return head == nullptr; +} -inline int dlist::size() const { return num_items; } +template +inline int dlist::size() const +{ + return num_items; +} -inline void* dlist::first() const { return head; } +template +inline T* dlist::first() const +{ + return head; +} -inline void* dlist::last() const { return tail; } +template +inline T* dlist::last() const +{ + return tail; +} /** * C string helper routines for dlist @@ -169,7 +197,7 @@ class dlistString { # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wunused-private-field" #endif - dlink link_; + dlink link_; #ifdef __clang__ # pragma clang diagnostic pop #endif @@ -182,4 +210,294 @@ class dlistString { extern dlistString* new_dlistString(const char* str, int len); extern dlistString* new_dlistString(const char* str); + +// Init dlist +template +void dlist::init(T* item, dlink* link) +{ + head = tail = NULL; + loffset = (int)((char*)link - (char*)item); + if (loffset < 0 || loffset > 5000) { + Emsg0(M_ABORT, 0, "Improper dlist initialization.\n"); + } + num_items = 0; +} + +// Append an item to the list +template +void dlist::append(T* item) +{ + SetNext(item, NULL); + SetPrev(item, tail); + if (tail) { SetNext(tail, item); } + tail = item; + if (head == NULL) { /* if empty list, */ + head = item; /* item is head as well */ + } + num_items++; +} + +// Prepend an item to the list +template +void dlist::prepend(T* item) +{ + SetNext(item, head); + SetPrev(item, NULL); + if (head) { SetPrev(head, item); } + head = item; + if (tail == NULL) { /* if empty list, */ + tail = item; /* item is tail too */ + } + num_items++; +} + +template +void dlist::InsertBefore(T* item, T* where) +{ + dlink* where_link = get_link(where); + + SetNext(item, where); + SetPrev(item, where_link->prev); + + if (where_link->prev) { SetNext(where_link->prev, item); } + where_link->prev = item; + if (head == where) { head = item; } + num_items++; +} + +template +void dlist::InsertAfter(T* item, T* where) +{ + dlink* where_link = get_link(where); + + SetNext(item, where_link->next); + SetPrev(item, where); + + if (where_link->next) { SetPrev(where_link->next, item); } + where_link->next = item; + if (tail == where) { tail = item; } + num_items++; +} + +/* + * Insert an item in the list, but only if it is unique + * otherwise, the item is returned non inserted + * + * Returns: item if item inserted + * other_item if same value already exists (item not inserted) + */ +template +T* dlist::binary_insert(T* item, int compare(T* item1, T* item2)) +{ + int comp; + int low, high, cur; + T* cur_item; + + if (num_items == 0) { + // Dmsg0(000, "Append first.\n"); + append(item); + return item; + } + if (num_items == 1) { + comp = compare(item, first()); + if (comp < 0) { + prepend(item); + // Dmsg0(000, "Insert before first.\n"); + return item; + } else if (comp > 0) { + InsertAfter(item, first()); + // Dmsg0(000, "Insert after first.\n"); + return item; + } else { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + } + /* Check against last item */ + comp = compare(item, last()); + if (comp > 0) { + append(item); + // Dmsg0(000, "Appended item.\n"); + return item; + } else if (comp == 0) { + // Dmsg0(000, "Same as last.\n"); + return last(); + } + /* Check against first item */ + comp = compare(item, first()); + if (comp < 0) { + prepend(item); + // Dmsg0(000, "Inserted item before.\n"); + return item; + } else if (comp == 0) { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + if (num_items == 2) { + InsertAfter(item, first()); + // Dmsg0(000, "Inserted item after.\n"); + return item; + } + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + // Dmsg1(000, "Compare item to %d\n", cur); + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + // Dmsg1(000, "Same as item %d\n", cur); + return cur_item; + } + } + if (high == cur) { + InsertBefore(item, cur_item); + // Dmsg1(000, "Insert before item %d\n", cur); + } else { + InsertAfter(item, cur_item); + // Dmsg1(000, "Insert after item %d\n", cur); + } + return item; +} + +/* + * Insert an item in the list, regardless if it is unique + * or not. + */ +template +void dlist::BinaryInsertMultiple(T* item, int compare(T* item1, T* item2)) +{ + T* ins_item = binary_insert(item, compare); + /* If identical, insert after the one found */ + if (ins_item != item) { InsertAfter(item, ins_item); } +} + +// Search for item +template +T* dlist::binary_search(T* item, int compare(T* item1, T* item2)) +{ + int comp; + int low, high, cur; + T* cur_item; + + + if (num_items == 0) { return NULL; } + cur_item = first(); + if (num_items == 1) { + comp = compare(item, cur_item); + if (comp == 0) { + return cur_item; + } else { + return NULL; + } + } + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + /* Now get cur pointing to nxt */ + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + return cur_item; + } + } + /* + * low == high can only happen if low just + * got incremented from cur, and we have + * not yet tested cur+1 + */ + if (low == high) { + cur_item = next(cur_item); + comp = compare(item, cur_item); + if (comp == 0) { return cur_item; } + } + return NULL; +} + + +template +void dlist::remove(T* item) +{ + T* xitem; + dlink* ilink = get_link(item); /* item's link */ + if (item == head) { + head = ilink->next; + if (head) { SetPrev(head, NULL); } + if (item == tail) { tail = ilink->prev; } + } else if (item == tail) { + tail = ilink->prev; + if (tail) { SetNext(tail, NULL); } + } else { + xitem = ilink->next; + SetPrev(xitem, ilink->prev); + xitem = ilink->prev; + SetNext(xitem, ilink->next); + } + num_items--; + if (num_items == 0) { head = tail = NULL; } +} + +template +T* dlist::next(T* item) +{ + if (item == NULL) { return head; } + return get_next(item); +} + +template +T* dlist::prev(T* item) +{ + if (item == NULL) { return tail; } + return get_prev(item); +} + + +/* Destroy the list contents */ +template +void dlist::destroy() +{ + for (T* n = head; n;) { + T* ni = get_next(n); + free(n); + n = ni; + } + num_items = 0; + head = tail = NULL; +} + #endif // BAREOS_LIB_DLIST_H_ diff --git a/core/src/lib/guid_to_name.cc b/core/src/lib/guid_to_name.cc index 84b73e2e051..2d07f1ff968 100644 --- a/core/src/lib/guid_to_name.cc +++ b/core/src/lib/guid_to_name.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2007-2011 Kern Sibbald - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -35,7 +35,7 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #endif struct guitem { - dlink link; + dlink link; char* name; union { uid_t uid; @@ -47,10 +47,10 @@ struct guitem { guid_list* new_guid_list() { guid_list* list; - guitem* item = NULL; + guitem* item = nullptr; list = (guid_list*)malloc(sizeof(guid_list)); - list->uid_list = new dlist(item, &item->link); - list->gid_list = new dlist(item, &item->link); + list->uid_list = new dlist(item, &item->link); + list->gid_list = new dlist(item, &item->link); return list; } @@ -68,10 +68,10 @@ void FreeGuidList(guid_list* list) free(list); } -static int UidCompare(void* item1, void* item2) +static int UidCompare(guitem* item1, guitem* item2) { - guitem* i1 = (guitem*)item1; - guitem* i2 = (guitem*)item2; + guitem* i1 = item1; + guitem* i2 = item2; if (i1->uid < i2->uid) { return -1; } else if (i1->uid > i2->uid) { @@ -81,10 +81,10 @@ static int UidCompare(void* item1, void* item2) } } -static int GidCompare(void* item1, void* item2) +static int GidCompare(guitem* item1, guitem* item2) { - guitem* i1 = (guitem*)item1; - guitem* i2 = (guitem*)item2; + guitem* i1 = item1; + guitem* i2 = item2; if (i1->gid < i2->gid) { return -1; } else if (i1->gid > i2->gid) { diff --git a/core/src/lib/guid_to_name.h b/core/src/lib/guid_to_name.h index 87c570e6013..247c6676cf5 100644 --- a/core/src/lib/guid_to_name.h +++ b/core/src/lib/guid_to_name.h @@ -28,13 +28,13 @@ #ifndef BAREOS_LIB_GUID_TO_NAME_H_ #define BAREOS_LIB_GUID_TO_NAME_H_ - +template class dlist; - +struct guitem; class guid_list { public: - dlist* uid_list; - dlist* gid_list; + dlist* uid_list; + dlist* gid_list; char* uid_to_name(uid_t uid, char* name, int maxlen); char* gid_to_name(gid_t gid, char* name, int maxlen); diff --git a/core/src/lib/ini.cc b/core/src/lib/ini.cc index 4343b7c4de0..24bd112dd48 100644 --- a/core/src/lib/ini.cc +++ b/core/src/lib/ini.cc @@ -1,7 +1,7 @@ /* Copyright (C) 2011-2011 Bacula Systems(R) SA Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can modify it under the terms of version three of the GNU Affero General Public License as published by the @@ -110,7 +110,7 @@ static bool IniStoreName(LEX* lc, ConfigFile* inifile, ini_items* item) static bool IniStoreAlistStr(LEX* lc, ConfigFile* inifile, ini_items* item) { - alist* list; + alist* list; if (!lc) { // TODO, write back the alist to edit buffer return true; @@ -118,7 +118,7 @@ static bool IniStoreAlistStr(LEX* lc, ConfigFile* inifile, ini_items* item) if (LexGetToken(lc, BCT_STRING) == BCT_ERROR) { return false; } if (item->val.alistval == NULL) { - list = new alist(10, owned_by_alist); + list = new alist(10, owned_by_alist); } else { list = item->val.alistval; } diff --git a/core/src/lib/ini.h b/core/src/lib/ini.h index a11be79fb83..05b1d3271b8 100644 --- a/core/src/lib/ini.h +++ b/core/src/lib/ini.h @@ -58,23 +58,23 @@ enum * On the Plugin side, it can get back the C structure and use it. * RestoreObject R2 -> parse() -> struct ini_items[] */ - +template +class alist; class ConfigFile; struct ini_items; // Used to store result -class alist; typedef union { char* strval; char nameval[MAX_NAME_LENGTH]; int64_t int64val; int32_t int32val; - alist* alistval; + alist* alistval; bool boolval; } item_value; /* - * If no items are registred at the scan time, we detect this list from + * If no items are registered at the scan time, we detect this list from * the file itself */ struct ini_items { diff --git a/core/src/lib/jcr.cc b/core/src/lib/jcr.cc index 0fe5a77bfa4..4fec6d1bf6f 100644 --- a/core/src/lib/jcr.cc +++ b/core/src/lib/jcr.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -66,7 +66,7 @@ static void JcrTimeoutCheck(watchdog_t* self); int num_jobs_run; static std::vector> job_control_record_cache; -static dlist* job_control_record_chain = nullptr; +static dlist* job_control_record_chain = nullptr; static int watch_dog_timeout = 0; static std::mutex jcr_chain_mutex; @@ -168,7 +168,7 @@ void RegisterJobEndCallback(JobControlRecord* jcr, item->JobEndCb = JobEndCb; item->ctx = ctx; - jcr->job_end_callbacks.push((void*)item); + jcr->job_end_callbacks.push(item); } // Pop each job_callback_item and process it. @@ -191,7 +191,8 @@ JobControlRecord::JobControlRecord() Dmsg0(100, "Construct JobControlRecord\n"); MessageQueueItem* item = nullptr; - msg_queue = new dlist(item, &item->link_); // calculate offset + msg_queue + = new dlist(item, &item->link_); // calculate offset int status; if ((status = pthread_mutex_init(&msg_queue_mutex, nullptr)) != 0) { @@ -909,7 +910,7 @@ void InitJcrChain() { JobControlRecord* jcr = nullptr; if (!job_control_record_chain) { - job_control_record_chain = new dlist(jcr, &jcr->link); + job_control_record_chain = new dlist(jcr, &jcr->link); } } diff --git a/core/src/lib/message_queue_item.h b/core/src/lib/message_queue_item.h index e41973ab781..9043333febc 100644 --- a/core/src/lib/message_queue_item.h +++ b/core/src/lib/message_queue_item.h @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2019-2019 Bareos GmbH & Co. KG + Copyright (C) 2019-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -32,7 +32,7 @@ class MessageQueueItem { public: MessageQueueItem() = default; virtual ~MessageQueueItem() = default; - dlink link_; + dlink link_; int type_ = 0; utime_t mtime_ = {0}; std::string msg_; diff --git a/core/src/lib/mntent_cache.cc b/core/src/lib/mntent_cache.cc index 6d683d18732..fb5a7f89049 100644 --- a/core/src/lib/mntent_cache.cc +++ b/core/src/lib/mntent_cache.cc @@ -3,7 +3,7 @@ Copyright (C) 2009-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -95,7 +95,7 @@ // Protected data by mutex lock. static pthread_mutex_t mntent_cache_lock = PTHREAD_MUTEX_INITIALIZER; static mntent_cache_entry_t* previous_cache_hit = NULL; -static dlist* mntent_cache_entries = NULL; +static dlist* mntent_cache_entries = NULL; // Last time a rescan of the mountlist took place. static time_t last_rescan = 0; @@ -107,12 +107,13 @@ static const char* skipped_fs_types[] = { NULL}; // Simple comparison function for binary search and insert. -static int CompareMntentMapping(void* e1, void* e2) +static int CompareMntentMapping(mntent_cache_entry_t* e1, + mntent_cache_entry_t* e2) { mntent_cache_entry_t *mce1, *mce2; - mce1 = (mntent_cache_entry_t*)e1; - mce2 = (mntent_cache_entry_t*)e2; + mce1 = e1; + mce2 = e2; if (mce1->dev == mce2->dev) { return 0; @@ -374,7 +375,7 @@ static inline void InitializeMntentCache(void) { mntent_cache_entry_t* mce = NULL; - mntent_cache_entries = new dlist(mce, &mce->link); + mntent_cache_entries = new dlist(mce, &mce->link); // Refresh the cache. refresh_mount_cache(add_mntent_mapping); diff --git a/core/src/lib/mntent_cache.h b/core/src/lib/mntent_cache.h index fa6fe5ba4b6..f6eedfbaf69 100644 --- a/core/src/lib/mntent_cache.h +++ b/core/src/lib/mntent_cache.h @@ -42,7 +42,7 @@ #define NR_MNTENT_HTABLE_PAGES 32 struct mntent_cache_entry_t { - dlink link; + dlink link; uint32_t dev{0}; char* special{nullptr}; char* mountpoint{nullptr}; diff --git a/core/src/lib/output_formatter.cc b/core/src/lib/output_formatter.cc index 8f7dfb1aa96..3cb50ce5ea9 100644 --- a/core/src/lib/output_formatter.cc +++ b/core/src/lib/output_formatter.cc @@ -63,7 +63,7 @@ OutputFormatter::OutputFormatter(SEND_HANDLER* send_func_arg, result_message_plain = new PoolMem(PM_MESSAGE); #if HAVE_JANSSON result_json = json_object(); - result_stack_json = new alist(10, false); + result_stack_json = new alist(10, false); result_stack_json->push(result_json); message_object_json = json_object(); #endif @@ -593,7 +593,7 @@ void OutputFormatter::CreateNewResFilter(of_filter_type type, { of_filter_tuple* tuple; - if (!filters) { filters = new alist(10, true); } + if (!filters) { filters = new alist(10, true); } tuple = (of_filter_tuple*)malloc(sizeof(of_filter_tuple)); tuple->type = type; @@ -607,7 +607,7 @@ void OutputFormatter::AddLimitFilterTuple(int limit) { of_filter_tuple* tuple; - if (!filters) { filters = new alist(10, true); } + if (!filters) { filters = new alist(10, true); } tuple = (of_filter_tuple*)malloc(sizeof(of_filter_tuple)); tuple->type = OF_FILTER_LIMIT; @@ -620,7 +620,7 @@ void OutputFormatter::AddOffsetFilterTuple(int offset) { of_filter_tuple* tuple; - if (!filters) { filters = new alist(10, true); } + if (!filters) { filters = new alist(10, true); } tuple = (of_filter_tuple*)malloc(sizeof(of_filter_tuple)); tuple->type = OF_FILTER_OFFSET; @@ -633,7 +633,7 @@ void OutputFormatter::AddAclFilterTuple(int column, int acltype) { of_filter_tuple* tuple; - if (!filters) { filters = new alist(10, true); } + if (!filters) { filters = new alist(10, true); } tuple = (of_filter_tuple*)malloc(sizeof(of_filter_tuple)); tuple->type = OF_FILTER_ACL; diff --git a/core/src/lib/output_formatter.h b/core/src/lib/output_formatter.h index 7e134b93d6d..28addba650f 100644 --- a/core/src/lib/output_formatter.h +++ b/core/src/lib/output_formatter.h @@ -120,14 +120,14 @@ class OutputFormatter { FILTER_HANDLER* filter_func = nullptr; void* send_ctx = nullptr; void* filter_ctx = nullptr; - alist* filters = nullptr; + alist* filters = nullptr; char* hidden_columns = nullptr; PoolMem* result_message_plain = nullptr; static const unsigned int max_message_length_shown_in_error = 1024; int num_rows_filtered = 0; #if HAVE_JANSSON json_t* result_json = nullptr; - alist* result_stack_json = nullptr; + alist* result_stack_json = nullptr; json_t* message_object_json = nullptr; #endif diff --git a/core/src/lib/output_formatter_resource.cc b/core/src/lib/output_formatter_resource.cc index 4dfc19be257..340c0291e48 100644 --- a/core/src/lib/output_formatter_resource.cc +++ b/core/src/lib/output_formatter_resource.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2020-2020 Bareos GmbH & Co. KG + Copyright (C) 2020-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -198,7 +198,7 @@ void OutputFormatterResource::KeyUnquotedString(const char* name, void OutputFormatterResource::KeyMultipleStringsInOneLine( const char* key, - alist* list, + alist* list, std::function GetValue, bool as_comment, bool quoted_strings) @@ -220,10 +220,11 @@ void OutputFormatterResource::KeyMultipleStringsInOneLine( send_->ArrayEnd(key, "\n"); } -void OutputFormatterResource::KeyMultipleStringsInOneLine(const char* key, - alist* list, - bool as_comment, - bool quoted_strings) +void OutputFormatterResource::KeyMultipleStringsInOneLine( + const char* key, + alist* list, + bool as_comment, + bool quoted_strings) { KeyMultipleStringsInOneLine(key, list, GetAsCString, as_comment, quoted_strings); @@ -253,7 +254,7 @@ void OutputFormatterResource::KeyMultipleStringsOnePerLineAddItem( void OutputFormatterResource::KeyMultipleStringsOnePerLine( const char* key, - alist* list, + alist* list, std::function GetValue, bool as_comment, bool quoted_strings, @@ -279,11 +280,12 @@ void OutputFormatterResource::KeyMultipleStringsOnePerLine( } -void OutputFormatterResource::KeyMultipleStringsOnePerLine(const char* key, - alist* list, - bool as_comment, - bool quoted_strings, - bool escape_strings) +void OutputFormatterResource::KeyMultipleStringsOnePerLine( + const char* key, + alist* list, + bool as_comment, + bool quoted_strings, + bool escape_strings) { KeyMultipleStringsOnePerLine(key, list, GetAsCString, as_comment, quoted_strings, escape_strings); diff --git a/core/src/lib/output_formatter_resource.h b/core/src/lib/output_formatter_resource.h index 7749e986708..6820e894baa 100644 --- a/core/src/lib/output_formatter_resource.h +++ b/core/src/lib/output_formatter_resource.h @@ -100,25 +100,25 @@ class OutputFormatterResource { bool as_comment = false); void KeyMultipleStringsInOneLine(const char* key, - alist* list, + alist* list, bool as_comment = false, bool quoted_strings = true); void KeyMultipleStringsInOneLine( const char* key, - alist* list, + alist* list, std::function GetValue, bool as_comment = false, bool quoted_strings = true); void KeyMultipleStringsOnePerLine(const char* key, - alist* list, + alist* list, bool as_comment = false, bool quoted_strings = true, bool escape_strings = false); void KeyMultipleStringsOnePerLine(const char* key, - alist* list, + alist* list, std::function GetValue, bool as_comment = false, bool quoted_strings = true, diff --git a/core/src/lib/parse_conf_init_resource.cc b/core/src/lib/parse_conf_init_resource.cc index 17a3ef09078..b0c955c461f 100644 --- a/core/src/lib/parse_conf_init_resource.cc +++ b/core/src/lib/parse_conf_init_resource.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -119,7 +119,8 @@ void ConfigurationParser::SetResourceDefaultsParserPass1(ResourceItem* item) break; } case CFG_TYPE_ADDRESSES: { - dlist** dlistvalue = GetItemVariablePointer(*item); + dlist** dlistvalue + = GetItemVariablePointer**>(*item); InitDefaultAddresses(dlistvalue, item->default_value); break; } @@ -139,16 +140,22 @@ void ConfigurationParser::SetResourceDefaultsParserPass2(ResourceItem* item) if (item->flags & CFG_ITEM_DEFAULT && item->default_value) { switch (item->type) { case CFG_TYPE_ALIST_STR: { - alist** alistvalue = GetItemVariablePointer(*item); - if (!alistvalue) { *(alistvalue) = new alist(10, owned_by_alist); } + alist** alistvalue + = GetItemVariablePointer**>(*item); + if (!alistvalue) { + *(alistvalue) = new alist(10, owned_by_alist); + } (*alistvalue)->append(strdup(item->default_value)); break; } case CFG_TYPE_ALIST_DIR: { PoolMem pathname(PM_FNAME); - alist** alistvalue = GetItemVariablePointer(*item); + alist** alistvalue + = GetItemVariablePointer**>(*item); - if (!*alistvalue) { *alistvalue = new alist(10, owned_by_alist); } + if (!*alistvalue) { + *alistvalue = new alist(10, owned_by_alist); + } PmStrcpy(pathname, item->default_value); if (*item->default_value != '|') { diff --git a/core/src/lib/plugins.cc b/core/src/lib/plugins.cc index f364faf7a8f..a68e23b692a 100644 --- a/core/src/lib/plugins.cc +++ b/core/src/lib/plugins.cc @@ -3,7 +3,7 @@ Copyright (C) 2007-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -111,7 +111,7 @@ static bool load_a_plugin(void* bareos_plugin_interface_version, const char* plugin_pathname, const char* plugin_name, const char* type, - alist* plugin_list, + alist* plugin_list, bool IsPluginCompatible(Plugin* plugin)) { t_loadPlugin loadPlugin; @@ -193,9 +193,9 @@ static bool load_a_plugin(void* bareos_plugin_interface_version, */ bool LoadPlugins(void* bareos_plugin_interface_version, void* bareos_core_functions, - alist* plugin_list, + alist* plugin_list, const char* plugin_dir, - alist* plugin_names, + alist* plugin_names, const char* type, bool IsPluginCompatible(Plugin* plugin)) { @@ -317,7 +317,7 @@ bool LoadPlugins(void* bareos_plugin_interface_version, } // Unload all the loaded plugins -void UnloadPlugins(alist* plugin_list) +void UnloadPlugins(alist* plugin_list) { int i{}; Plugin* plugin{}; @@ -332,7 +332,7 @@ void UnloadPlugins(alist* plugin_list) } } -void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index) +void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index) { // Shut it down and unload it plugin->unloadPlugin(); @@ -342,7 +342,7 @@ void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index) free(plugin); } -int ListPlugins(alist* plugin_list, PoolMem& msg) +int ListPlugins(alist* plugin_list, PoolMem& msg) { int i{}; int len{}; @@ -411,7 +411,7 @@ void DbgPrintPluginAddHook(dbg_print_plugin_hook_t* fct) dbg_print_plugin_hook = fct; } -void DumpPlugins(alist* plugin_list, FILE* fp) +void DumpPlugins(alist* plugin_list, FILE* fp) { int i{}; Plugin* plugin{}; diff --git a/core/src/lib/plugins.h b/core/src/lib/plugins.h index 5024c7780cb..5c5418d36a6 100644 --- a/core/src/lib/plugins.h +++ b/core/src/lib/plugins.h @@ -92,19 +92,20 @@ typedef struct gen_pluginInfo { const char* plugin_usage; } PluginInformation; +template class alist; /* Functions */ bool LoadPlugins(void* bareos_plugin_interface_version, void* bareos_core_functions, - alist* plugin_list, + alist* plugin_list, const char* plugin_dir, - alist* plugin_names, + alist* plugin_names, const char* type, bool IsPluginCompatible(Plugin* plugin)); -void UnloadPlugins(alist* plugin_list); -void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index); -int ListPlugins(alist* plugin_list, PoolMem& msg); +void UnloadPlugins(alist* plugin_list); +void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index); +int ListPlugins(alist* plugin_list, PoolMem& msg); /* Each daemon can register a debug hook that will be called * after a fatal signal @@ -113,6 +114,6 @@ typedef void(dbg_plugin_hook_t)(Plugin* plug, FILE* fp); void DbgPluginAddHook(dbg_plugin_hook_t* fct); typedef void(dbg_print_plugin_hook_t)(FILE* fp); void DbgPrintPluginAddHook(dbg_print_plugin_hook_t* fct); -void DumpPlugins(alist* plugin_list, FILE* fp); +void DumpPlugins(alist* plugin_list, FILE* fp); #endif // BAREOS_LIB_PLUGINS_H_ diff --git a/core/src/lib/res.cc b/core/src/lib/res.cc index 8b80ac08ff2..2f6f89b9cfc 100644 --- a/core/src/lib/res.cc +++ b/core/src/lib/res.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -641,11 +641,14 @@ void ConfigurationParser::StoreAlistRes(LEX* lc, int index, int pass) { - alist** alistvalue = GetItemVariablePointer(*item); + alist** alistvalue + = GetItemVariablePointer**>(*item); if (pass == 2) { - if (!*alistvalue) { *alistvalue = new alist(10, not_owned_by_alist); } + if (!*alistvalue) { + *alistvalue = new alist(10, not_owned_by_alist); + } } - alist* list = *alistvalue; + alist* list = *alistvalue; int token = BCT_COMMA; while (token == BCT_COMMA) { @@ -711,11 +714,14 @@ void ConfigurationParser::StoreAlistStr(LEX* lc, int index, int pass) { - alist** alistvalue = GetItemVariablePointer(*item); + alist** alistvalue + = GetItemVariablePointer**>(*item); if (pass == 2) { - if (!*alistvalue) { *alistvalue = new alist(10, owned_by_alist); } + if (!*alistvalue) { + *alistvalue = new alist(10, owned_by_alist); + } } - alist* list = *alistvalue; + alist* list = *alistvalue; int token = BCT_COMMA; while (token == BCT_COMMA) { @@ -760,9 +766,12 @@ void ConfigurationParser::StoreAlistDir(LEX* lc, int pass) { if (pass == 2) { - alist** alistvalue = GetItemVariablePointer(*item); - if (!*alistvalue) { *alistvalue = new alist(10, owned_by_alist); } - alist* list = *alistvalue; + alist** alistvalue + = GetItemVariablePointer**>(*item); + if (!*alistvalue) { + *alistvalue = new alist(10, owned_by_alist); + } + alist* list = *alistvalue; LexGetToken(lc, BCT_STRING); /* scan next item */ Dmsg4(900, "Append %s to alist %p size=%d %s\n", lc->str, list, @@ -806,8 +815,11 @@ void ConfigurationParser::StorePluginNames(LEX* lc, return; } - alist** alistvalue = GetItemVariablePointer(*item); - if (!*alistvalue) { *alistvalue = new alist(10, owned_by_alist); } + alist** alistvalue + = GetItemVariablePointer**>(*item); + if (!*alistvalue) { + *alistvalue = new alist(10, owned_by_alist); + } bool finish = false; while (!finish) { @@ -1274,7 +1286,7 @@ void ConfigurationParser::StoreAddresses(LEX* lc, scan_err1(lc, _("Expected a end of block }, got: %s"), lc->str); } if (pass == 1 - && !AddAddress(GetItemVariablePointer(*item), + && !AddAddress(GetItemVariablePointer**>(*item), IPADDR::R_MULTIPLE, htons(port), family, hostname_str, port_str, errmsg, sizeof(errmsg))) { scan_err3(lc, _("Can't add hostname(%s) and port(%s) to addrlist (%s)"), @@ -1304,7 +1316,7 @@ void ConfigurationParser::StoreAddressesAddress(LEX* lc, scan_err1(lc, _("Expected an IP number or a hostname, got: %s"), lc->str); } if (pass == 1 - && !AddAddress(GetItemVariablePointer(*item), + && !AddAddress(GetItemVariablePointer**>(*item), IPADDR::R_SINGLE_ADDR, htons(port), AF_INET, lc->str, 0, errmsg, sizeof(errmsg))) { scan_err2(lc, _("can't add port (%s) to (%s)"), lc->str, errmsg); @@ -1326,7 +1338,7 @@ void ConfigurationParser::StoreAddressesPort(LEX* lc, scan_err1(lc, _("Expected a port number or string, got: %s"), lc->str); } if (pass == 1 - && !AddAddress(GetItemVariablePointer(*item), + && !AddAddress(GetItemVariablePointer**>(*item), IPADDR::R_SINGLE_PORT, htons(port), AF_INET, 0, lc->str, errmsg, sizeof(errmsg))) { scan_err2(lc, _("can't add port (%s) to (%s)"), lc->str, errmsg); @@ -1934,14 +1946,14 @@ void BareosResource::PrintResourceItem(ResourceItem& item, case CFG_TYPE_PLUGIN_NAMES: { // One line for each member of the list send.KeyMultipleStringsOnePerLine( - item.name, GetItemVariable(item), inherited); + item.name, GetItemVariable*>(item), inherited); break; } case CFG_TYPE_ALIST_RES: { // Each member of the list is comma-separated send.KeyMultipleStringsOnePerLine( - item.name, GetItemVariable(item), GetResourceName, inherited, - true, false); + item.name, GetItemVariable*>(item), + GetResourceName, inherited, true, false); break; } case CFG_TYPE_RES: { @@ -1968,7 +1980,7 @@ void BareosResource::PrintResourceItem(ResourceItem& item, */ break; case CFG_TYPE_ADDRESSES: { - dlist* addrs = GetItemVariable(item); + dlist* addrs = GetItemVariable*>(item); IPADDR* adr; send.ArrayStart(item.name, inherited, "%s = {\n"); foreach_dlist (adr, addrs) { diff --git a/core/src/lib/resource_item.h b/core/src/lib/resource_item.h index 36d06306580..5a2320dcb1c 100644 --- a/core/src/lib/resource_item.h +++ b/core/src/lib/resource_item.h @@ -25,7 +25,9 @@ #define BAREOS_LIB_RESOURCE_ITEM_H_ struct s_password; +template class alist; +template class dlist; /* @@ -35,24 +37,6 @@ class dlist; struct ResourceItem { const char* name; /* Resource name i.e. Director, ... */ const int type; - // union { - // char** value; /* Where to store the item */ - // std::string* strValue; - // uint16_t* ui16value; - // uint32_t* ui32value; - // int16_t* i16value; - // int32_t* i32value; - // uint64_t* ui64value; - // int64_t* i64value; - // bool* boolvalue; - // utime_t* utimevalue; - // s_password* pwdvalue; - // BareosResource** resvalue; - // alist** alistvalue; - // dlist** dlistvalue; - // char* bitvalue; - // std::vector* std_vector_of_strings; - //}; std::size_t offset; BareosResource** allocated_resource; int32_t code; /* Item code/additional info */ diff --git a/core/src/lib/runscript.cc b/core/src/lib/runscript.cc index 3e2b8117cce..ac7878dc967 100644 --- a/core/src/lib/runscript.cc +++ b/core/src/lib/runscript.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2006-2011 Free Software Foundation Europe e.V. - Copyright (C) 2019-2019 Bareos GmbH & Co. KG + Copyright (C) 2019-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -59,7 +59,7 @@ void FreeRunscript(RunScript* script) static bool ScriptDirAllowed(JobControlRecord* jcr, RunScript* script, - alist* allowed_script_dirs) + alist* allowed_script_dirs) { char *bp, *allowed_script_dir = nullptr; bool allowed = false; @@ -102,9 +102,9 @@ static bool ScriptDirAllowed(JobControlRecord* jcr, } int RunScripts(JobControlRecord* jcr, - alist* runscripts, + alist* runscripts, const char* label, - alist* allowed_script_dirs) + alist* allowed_script_dirs) { RunScript* script = nullptr; bool runit; @@ -277,7 +277,7 @@ bool RunScript::Run(JobControlRecord* jcr, const char* name) return false; } -void FreeRunscripts(alist* runscripts) +void FreeRunscripts(alist* runscripts) { Dmsg0(500, "runscript: freeing all RUNSCRIPTS object\n"); diff --git a/core/src/lib/runscript.h b/core/src/lib/runscript.h index 6d0173389ee..50d255cb8d2 100644 --- a/core/src/lib/runscript.h +++ b/core/src/lib/runscript.h @@ -30,7 +30,7 @@ #include "jcr.h" #include "lib/bareos_resource.h" - +template class alist; /* Usage: @@ -105,14 +105,14 @@ RunScript* DuplicateRunscript(RunScript* src); /* launch each script from runscripts*/ int RunScripts(JobControlRecord* jcr, - alist* runscripts, + alist* runscripts, const char* name, - alist* allowed_script_dirs = NULL); + alist* allowed_script_dirs = NULL); void FreeRunscript(RunScript* script); /* foreach_alist free RunScript */ -void FreeRunscripts(alist* runscripts); /* you have to free alist */ +void FreeRunscripts(alist* runscripts); /* you have to free alist */ extern bool (*console_command)(JobControlRecord* jcr, const char* cmd); diff --git a/core/src/lib/watchdog.cc b/core/src/lib/watchdog.cc index 8b06fa9519f..7cf0e330663 100644 --- a/core/src/lib/watchdog.cc +++ b/core/src/lib/watchdog.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2002-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -55,8 +55,8 @@ static bool wd_is_init = false; static brwlock_t lock; /* watchdog lock */ static pthread_t wd_tid; -static dlist* wd_queue; -static dlist* wd_inactive; +static dlist* wd_queue; +static dlist* wd_inactive; /* * Returns: 0 if the current thread is NOT the watchdog @@ -92,8 +92,8 @@ int StartWatchdog(void) Jmsg1(NULL, M_ABORT, 0, _("Unable to initialize watchdog lock. ERR=%s\n"), be.bstrerror(errstat)); } - wd_queue = new dlist(dummy, &dummy->link); - wd_inactive = new dlist(dummy, &dummy->link); + wd_queue = new dlist(dummy, &dummy->link); + wd_inactive = new dlist(dummy, &dummy->link); wd_is_init = true; if ((status = pthread_create(&wd_tid, NULL, watchdog_thread, NULL)) != 0) { @@ -133,9 +133,9 @@ int StopWatchdog(void) status = pthread_join(wd_tid, NULL); while (!wd_queue->empty()) { - void* item = wd_queue->first(); + watchdog_t* item = wd_queue->first(); wd_queue->remove(item); - p = (watchdog_t*)item; + p = item; if (p->destructor != NULL) { p->destructor(p); } free(p); } @@ -143,9 +143,9 @@ int StopWatchdog(void) wd_queue = NULL; while (!wd_inactive->empty()) { - void* item = wd_inactive->first(); + watchdog_t* item = wd_inactive->first(); wd_inactive->remove(item); - p = (watchdog_t*)item; + p = item; if (p->destructor != NULL) { p->destructor(p); } free(p); } diff --git a/core/src/lib/watchdog.h b/core/src/lib/watchdog.h index 283bb62a5bc..837dd757cec 100644 --- a/core/src/lib/watchdog.h +++ b/core/src/lib/watchdog.h @@ -45,7 +45,7 @@ struct s_watchdog_t { void (*destructor)(struct s_watchdog_t* wd); void* data; /* Private data below - don't touch outside of watchdog.c */ - dlink link; + dlink link; utime_t next_fire; }; typedef struct s_watchdog_t watchdog_t; diff --git a/core/src/plugins/filed/cephfs/cephfs-fd.cc b/core/src/plugins/filed/cephfs/cephfs-fd.cc index e4eef8ed804..401a5a8f8c4 100644 --- a/core/src/plugins/filed/cephfs/cephfs-fd.cc +++ b/core/src/plugins/filed/cephfs/cephfs-fd.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2014-2015 Planets Communications B.V. - Copyright (C) 2014-2020 Bareos GmbH & Co. KG + Copyright (C) 2014-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -36,6 +36,8 @@ namespace filedaemon { +struct dir_stack_entry; + static const int debuglevel = 150; #define PLUGIN_LICENSE "Bareos AGPLv3" @@ -115,9 +117,9 @@ struct plugin_ctx { POOLMEM* next_filename; /* Next filename to save */ POOLMEM* link_target; /* Target symlink points to */ POOLMEM* xattr_list; /* List of xattrs */ - alist* dir_stack; /* Stack of directories when recursing */ - htable* path_list; /* Hash table with directories created on restore. */ - struct dirent de; /* Current directory entry being processed. */ + alist* dir_stack; /* Stack of directories when recursing */ + htable* path_list; /* Hash table with directories created on restore. */ + struct dirent de; /* Current directory entry being processed. */ struct ceph_mount_info* cmount; /* CEPHFS mountpoint */ struct ceph_dir_result* cdir; /* CEPHFS directory handle */ int cfd; /* CEPHFS file handle */ @@ -229,7 +231,7 @@ static bRC newPlugin(PluginContext* ctx) * processing a sub directory and pop it from this list when we are * done processing that sub directory. */ - p_ctx->dir_stack = new alist(10, owned_by_alist); + p_ctx->dir_stack = new alist(10, owned_by_alist); // Only register the events we are really interested in. bareos_core_functions->registerBareosEvents( diff --git a/core/src/plugins/filed/gfapi/gfapi-fd.cc b/core/src/plugins/filed/gfapi/gfapi-fd.cc index 4b7b51c0ca5..de665934dd2 100644 --- a/core/src/plugins/filed/gfapi/gfapi-fd.cc +++ b/core/src/plugins/filed/gfapi/gfapi-fd.cc @@ -109,6 +109,14 @@ static PluginFunctions pluginFuncs endBackupFile, startRestoreFile, endRestoreFile, pluginIO, createFile, setFileAttributes, checkFile, getAcl, setAcl, getXattr, setXattr}; +/** + * If we recurse into a subdir we push the current directory onto + * a stack so we can pop it after we have processed the subdir. + */ +struct dir_stack_entry { + struct stat statp; /* Stat struct of directory */ + glfs_fd_t* gdir; /* Gluster directory handle */ +}; // Plugin private context struct plugin_ctx { int32_t backup_level; /* Backup level e.g. Full/Differential/Incremental */ @@ -138,7 +146,7 @@ struct plugin_ctx { #ifndef HAVE_GLFS_READDIRPLUS POOLMEM* dirent_buffer; /* Temporary buffer for current dirent structure */ #endif - alist* dir_stack; /* Stack of directories when recursing */ + alist* dir_stack; /* Stack of directories when recursing */ htable* path_list; /* Hash table with directories created on restore. */ glfs_t* glfs; /* Gluster volume handle */ glfs_fd_t* gdir; /* Gluster directory handle */ @@ -175,14 +183,6 @@ enum gluster_find_type gf_type_delete }; -/** - * If we recurse into a subdir we push the current directory onto - * a stack so we can pop it after we have processed the subdir. - */ -struct dir_stack_entry { - struct stat statp; /* Stat struct of directory */ - glfs_fd_t* gdir; /* Gluster directory handle */ -}; struct gluster_find_mapping { const char* name; @@ -1527,7 +1527,7 @@ static bRC setup_backup(PluginContext* ctx, void* value) * processing a sub directory and pop it from this list when we are * done processing that sub directory. */ - p_ctx->dir_stack = new alist(10, owned_by_alist); + p_ctx->dir_stack = new alist(10, owned_by_alist); /* * Setup the directory we need to start scanning by setting the filetype diff --git a/core/src/stored/acquire.cc b/core/src/stored/acquire.cc index a6077b1c5d9..c7bdc62e260 100644 --- a/core/src/stored/acquire.cc +++ b/core/src/stored/acquire.cc @@ -170,7 +170,7 @@ bool AcquireDeviceForRead(DeviceControlRecord* dcr) memset(&rctx, 0, sizeof(ReserveContext)); rctx.jcr = jcr; jcr->impl->read_dcr = dcr; - jcr->impl->reserve_msgs = new alist(10, not_owned_by_alist); + jcr->impl->reserve_msgs = new alist(10, not_owned_by_alist); rctx.any_drive = true; rctx.device_name = vol->device; store = new DirectorStorage; diff --git a/core/src/stored/autochanger_resource.cc b/core/src/stored/autochanger_resource.cc index effe260d4cd..a765cec9401 100644 --- a/core/src/stored/autochanger_resource.cc +++ b/core/src/stored/autochanger_resource.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2019-2020 Bareos GmbH & Co. KG + Copyright (C) 2019-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -53,8 +53,9 @@ bool AutochangerResource::PrintConfig(OutputFormatterResource& send, bool hide_sensitive_data, bool verbose) { - alist* original_alist = device_resources; - alist* temp_alist = new alist(original_alist->size(), not_owned_by_alist); + alist* original_alist = device_resources; + alist* temp_alist + = new alist(original_alist->size(), not_owned_by_alist); DeviceResource* device_resource = nullptr; foreach_alist (device_resource, original_alist) { if (device_resource->multiplied_device_resource) { diff --git a/core/src/stored/autochanger_resource.h b/core/src/stored/autochanger_resource.h index 7fd434e203c..4ac29118b61 100644 --- a/core/src/stored/autochanger_resource.h +++ b/core/src/stored/autochanger_resource.h @@ -26,9 +26,11 @@ #include "lib/bareos_resource.h" +template class alist; namespace storagedaemon { +class DeviceResource; class AutochangerResource : public BareosResource { public: @@ -41,10 +43,11 @@ class AutochangerResource : public BareosResource { bool verbose = false) override; - alist* device_resources; /**< List of DeviceResource device pointers */ - char* changer_name; /**< Changer device name */ - char* changer_command; /**< Changer command -- external program */ - brwlock_t changer_lock; /**< One changer operation at a time */ + alist* + device_resources; /**< List of DeviceResource device pointers */ + char* changer_name; /**< Changer device name */ + char* changer_command; /**< Changer command -- external program */ + brwlock_t changer_lock; /**< One changer operation at a time */ }; } /* namespace storagedaemon */ diff --git a/core/src/stored/backends/CMakeLists.txt b/core/src/stored/backends/CMakeLists.txt index 20b80661e79..54b620fac79 100644 --- a/core/src/stored/backends/CMakeLists.txt +++ b/core/src/stored/backends/CMakeLists.txt @@ -1,6 +1,6 @@ # BAREOS® - Backup Archiving REcovery Open Sourced # -# Copyright (C) 2017-2020 Bareos GmbH & Co. KG +# Copyright (C) 2017-2021 Bareos GmbH & Co. KG # # This program is Free Software; you can redistribute it and/or # modify it under the terms of version three of the GNU Affero General Public @@ -40,7 +40,9 @@ if(${HAVE_GLUSTERFS}) endif() if(TARGET droplet) - add_library(bareossd-chunked SHARED ordered_cbuf.cc chunked_device.cc) + add_library( + bareossd-chunked SHARED ordered_cbuf.cc chunked_device.cc old_dlist.cc + ) target_link_libraries(bareossd-chunked droplet) install(TARGETS bareossd-chunked DESTINATION ${backenddir}) diff --git a/core/src/stored/backends/chunked_device.cc b/core/src/stored/backends/chunked_device.cc index cafd11f9761..2442e83c8c2 100644 --- a/core/src/stored/backends/chunked_device.cc +++ b/core/src/stored/backends/chunked_device.cc @@ -146,7 +146,9 @@ bool ChunkedDevice::StartIoThreads() } // Start all IO threads and keep track of their thread ids in thread_ids_. - if (!thread_ids_) { thread_ids_ = new alist(10, owned_by_alist); } + if (!thread_ids_) { + thread_ids_ = new alist(10, owned_by_alist); + } for (thread_nr = 1; thread_nr <= io_threads_; thread_nr++) { if (pthread_create(&thread_id, NULL, io_thread, (void*)this)) { diff --git a/core/src/stored/backends/chunked_device.h b/core/src/stored/backends/chunked_device.h index 1034ed9c895..8a1af4e1813 100644 --- a/core/src/stored/backends/chunked_device.h +++ b/core/src/stored/backends/chunked_device.h @@ -28,6 +28,7 @@ #ifndef BAREOS_STORED_BACKENDS_CHUNKED_DEVICE_H_ #define BAREOS_STORED_BACKENDS_CHUNKED_DEVICE_H_ +template class alist; #include "ordered_cbuf.h" @@ -107,7 +108,7 @@ class ChunkedDevice : public Device { uint8_t inflight_chunks_{}; char* current_volname_{}; ordered_circbuf* cb_{}; - alist* thread_ids_{}; + alist* thread_ids_{}; chunk_descriptor* current_chunk_{}; // Private Methods diff --git a/core/src/stored/backends/old_dlink.h b/core/src/stored/backends/old_dlink.h new file mode 100644 index 00000000000..5a54444bcc3 --- /dev/null +++ b/core/src/stored/backends/old_dlink.h @@ -0,0 +1,36 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2004-2010 Free Software Foundation Europe e.V. + Copyright (C) 2016-2021 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef BAREOS_LIB_OLD_DLINK_H_ +#define BAREOS_LIB_OLD_DLINK_H_ + +struct old_dlink { + void* next; + void* prev; + old_dlink() + { + next = nullptr; + prev = nullptr; + } +}; + +#endif // BAREOS_LIB_OLD_DLINK_H_ diff --git a/core/src/stored/backends/old_dlist.cc b/core/src/stored/backends/old_dlist.cc new file mode 100644 index 00000000000..8de6fe016bd --- /dev/null +++ b/core/src/stored/backends/old_dlist.cc @@ -0,0 +1,336 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2003-2011 Free Software Foundation Europe e.V. + Copyright (C) 2013-2019 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +/* + * BAREOS doubly linked list routines. + * + * old_dlist is a doubly linked list with the links being in the list data item. + * + * Kern Sibbald, July MMIII + */ + +#include "include/bareos.h" +#include "old_dlink.h" +#include "old_dlist.h" + + +/** + * Init old_dlist + */ +void old_dlist::init(void* item, old_dlink* link) +{ + head = tail = NULL; + loffset = (int)((char*)link - (char*)item); + if (loffset < 0 || loffset > 5000) { + Emsg0(M_ABORT, 0, "Improper old_dlist initialization.\n"); + } + num_items = 0; +} + +/* + * Append an item to the list + */ +void old_dlist::append(void* item) +{ + SetNext(item, NULL); + SetPrev(item, tail); + if (tail) { SetNext(tail, item); } + tail = item; + if (head == NULL) { /* if empty list, */ + head = item; /* item is head as well */ + } + num_items++; +} + +/* + * Prepend an item to the list + */ +void old_dlist::prepend(void* item) +{ + SetNext(item, head); + SetPrev(item, NULL); + if (head) { SetPrev(head, item); } + head = item; + if (tail == NULL) { /* if empty list, */ + tail = item; /* item is tail too */ + } + num_items++; +} + +void old_dlist::InsertBefore(void* item, void* where) +{ + old_dlink* where_link = get_link(where); + + SetNext(item, where); + SetPrev(item, where_link->prev); + + if (where_link->prev) { SetNext(where_link->prev, item); } + where_link->prev = item; + if (head == where) { head = item; } + num_items++; +} + +void old_dlist::InsertAfter(void* item, void* where) +{ + old_dlink* where_link = get_link(where); + + SetNext(item, where_link->next); + SetPrev(item, where); + + if (where_link->next) { SetPrev(where_link->next, item); } + where_link->next = item; + if (tail == where) { tail = item; } + num_items++; +} + +/* + * Insert an item in the list, but only if it is unique + * otherwise, the item is returned non inserted + * + * Returns: item if item inserted + * other_item if same value already exists (item not inserted) + */ +void* old_dlist::binary_insert(void* item, + int compare(void* item1, void* item2)) +{ + int comp; + int low, high, cur; + void* cur_item; + + if (num_items == 0) { + // Dmsg0(000, "Append first.\n"); + append(item); + return item; + } + if (num_items == 1) { + comp = compare(item, first()); + if (comp < 0) { + prepend(item); + // Dmsg0(000, "Insert before first.\n"); + return item; + } else if (comp > 0) { + InsertAfter(item, first()); + // Dmsg0(000, "Insert after first.\n"); + return item; + } else { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + } + /* Check against last item */ + comp = compare(item, last()); + if (comp > 0) { + append(item); + // Dmsg0(000, "Appended item.\n"); + return item; + } else if (comp == 0) { + // Dmsg0(000, "Same as last.\n"); + return last(); + } + /* Check against first item */ + comp = compare(item, first()); + if (comp < 0) { + prepend(item); + // Dmsg0(000, "Inserted item before.\n"); + return item; + } else if (comp == 0) { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + if (num_items == 2) { + InsertAfter(item, first()); + // Dmsg0(000, "Inserted item after.\n"); + return item; + } + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + // Dmsg1(000, "Compare item to %d\n", cur); + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + // Dmsg1(000, "Same as item %d\n", cur); + return cur_item; + } + } + if (high == cur) { + InsertBefore(item, cur_item); + // Dmsg1(000, "Insert before item %d\n", cur); + } else { + InsertAfter(item, cur_item); + // Dmsg1(000, "Insert after item %d\n", cur); + } + return item; +} + +/* + * Insert an item in the list, regardless if it is unique + * or not. + */ +void old_dlist::BinaryInsertMultiple(void* item, + int compare(void* item1, void* item2)) +{ + void* ins_item = binary_insert(item, compare); + /* If identical, insert after the one found */ + if (ins_item != item) { InsertAfter(item, ins_item); } +} + +/* + * Search for item + */ +void* old_dlist::binary_search(void* item, + int compare(void* item1, void* item2)) +{ + int comp; + int low, high, cur; + void* cur_item; + + + if (num_items == 0) { return NULL; } + cur_item = first(); + if (num_items == 1) { + comp = compare(item, cur_item); + if (comp == 0) { + return cur_item; + } else { + return NULL; + } + } + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + /* Now get cur pointing to nxt */ + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + return cur_item; + } + } + /* + * low == high can only happen if low just + * got incremented from cur, and we have + * not yet tested cur+1 + */ + if (low == high) { + cur_item = next(cur_item); + comp = compare(item, cur_item); + if (comp == 0) { return cur_item; } + } + return NULL; +} + + +void old_dlist::remove(void* item) +{ + void* xitem; + old_dlink* ilink = get_link(item); /* item's link */ + if (item == head) { + head = ilink->next; + if (head) { SetPrev(head, NULL); } + if (item == tail) { tail = ilink->prev; } + } else if (item == tail) { + tail = ilink->prev; + if (tail) { SetNext(tail, NULL); } + } else { + xitem = ilink->next; + SetPrev(xitem, ilink->prev); + xitem = ilink->prev; + SetNext(xitem, ilink->next); + } + num_items--; + if (num_items == 0) { head = tail = NULL; } +} + +void* old_dlist::next(void* item) +{ + if (item == NULL) { return head; } + return get_next(item); +} + +void* old_dlist::prev(void* item) +{ + if (item == NULL) { return tail; } + return get_prev(item); +} + + +/* Destroy the list contents */ +void old_dlist::destroy() +{ + for (void* n = head; n;) { + void* ni = get_next(n); + free(n); + n = ni; + } + num_items = 0; + head = tail = NULL; +} + +/* String helpers for old_dlist usage */ + +old_dlistString* new_old_dlistString(const char* str) +{ + return new_old_dlistString(str, strlen(str)); +} + +old_dlistString* new_old_dlistString(const char* str, int len) +{ + old_dlistString* node; + node = (old_dlistString*)malloc(sizeof(old_dlink) + len + 1); + bstrncpy(node->c_str(), str, len + 1); + return node; +} diff --git a/core/src/stored/backends/old_dlist.h b/core/src/stored/backends/old_dlist.h new file mode 100644 index 00000000000..7efec58138f --- /dev/null +++ b/core/src/stored/backends/old_dlist.h @@ -0,0 +1,187 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2004-2010 Free Software Foundation Europe e.V. + Copyright (C) 2016-2021 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +/* + * Kern Sibbald, MMIV and MMVII + */ +/** + * @file + * Doubly linked list -- old_dlist + * + * See the end of the file for the old_dlistString class which + * facilitates storing strings in a old_dlist. + */ + +#ifndef BAREOS_LIB_OLD_DLIST_H_ +#define BAREOS_LIB_OLD_DLIST_H_ + +#include "include/bareos.h" +#include "old_dlink.h" +#include "lib/message.h" + +#define M_ABORT 1 + +/* In case you want to specifically specify the offset to the link */ +#define OFFSET(item, link) (int)((char*)(link) - (char*)(item)) +/** + * There is a lot of extra casting here to work around the fact + * that some compilers (Sun and Visual C++) do not accept + * (void *) as an lvalue on the left side of an equal. + * + * Loop var through each member of list + */ +#ifdef HAVE_TYPEOF +# define foreach_old_dlist(var, list) \ + for ((var) = nullptr; \ + (list ? ((var) = (typeof(var))(list)->next(var)) : nullptr) \ + != nullptr;) +#else +# define foreach_old_dlist(var, list) \ + for ((var) = nullptr; \ + (list ? (*((void**)&(var)) = (void*)((list)->next(var))) : nullptr) \ + != nullptr;) +#endif + +class old_dlist { + void* head; + void* tail; + int16_t loffset; + uint32_t num_items; + + public: + old_dlist(void* item, old_dlink* link); + old_dlist(void); + ~old_dlist() { destroy(); } + void init(void* item, old_dlink* link); + void init(); + void prepend(void* item); + void append(void* item); + void SetPrev(void* item, void* prev); + void SetNext(void* item, void* next); + void* get_prev(void* item); + void* get_next(void* item); + old_dlink* get_link(void* item); + void InsertBefore(void* item, void* where); + void InsertAfter(void* item, void* where); + void* binary_insert(void* item, int compare(void* item1, void* item2)); + void* binary_search(void* item, int compare(void* item1, void* item2)); + void BinaryInsertMultiple(void* item, int compare(void* item1, void* item2)); + void remove(void* item); + bool empty() const; + int size() const; + void* next(void* item); + void* prev(void* item); + void destroy(); + void* first() const; + void* last() const; +}; + +inline void old_dlist::init() +{ + head = tail = nullptr; + loffset = 0; + num_items = 0; +} + + +/** + * Constructor called with the address of a + * member of the list (not the list head), and + * the address of the link within that member. + * If the link is at the beginning of the list member, + * then there is no need to specify the link address + * since the offset is zero. + */ +inline old_dlist::old_dlist(void* item, old_dlink* link) { init(item, link); } + +/* Constructor with link at head of item */ +inline old_dlist::old_dlist(void) + : head(nullptr), tail(nullptr), loffset(0), num_items(0) +{ + return; +} + +inline void old_dlist::SetPrev(void* item, void* prev) +{ + ((old_dlink*)(((char*)item) + loffset))->prev = prev; +} + +inline void old_dlist::SetNext(void* item, void* next) +{ + ((old_dlink*)(((char*)item) + loffset))->next = next; +} + +inline void* old_dlist::get_prev(void* item) +{ + return ((old_dlink*)(((char*)item) + loffset))->prev; +} + +inline void* old_dlist::get_next(void* item) +{ + return ((old_dlink*)(((char*)item) + loffset))->next; +} + + +inline old_dlink* old_dlist::get_link(void* item) +{ + return (old_dlink*)(((char*)item) + loffset); +} + + +inline bool old_dlist::empty() const { return head == nullptr; } + +inline int old_dlist::size() const { return num_items; } + + +inline void* old_dlist::first() const { return head; } + +inline void* old_dlist::last() const { return tail; } + +/** + * C string helper routines for old_dlist + * The string (char *) is kept in the node + * + * Kern Sibbald, February 2007 + * + */ +class old_dlistString { + public: + char* c_str() { return str_; } + + private: +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunused-private-field" +#endif + old_dlink link_; +#ifdef __clang__ +# pragma clang diagnostic pop +#endif + char str_[1]; + /* !!! Don't put anything after this as this space is used + * to hold the string in inline + */ +}; + +extern old_dlistString* new_old_dlistString(const char* str, int len); +extern old_dlistString* new_old_dlistString(const char* str); + +#endif // BAREOS_LIB_OLD_DLIST_H_ diff --git a/core/src/stored/backends/ordered_cbuf.cc b/core/src/stored/backends/ordered_cbuf.cc index 17979980af1..2ada661239e 100644 --- a/core/src/stored/backends/ordered_cbuf.cc +++ b/core/src/stored/backends/ordered_cbuf.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2016-2017 Planets Communications B.V. - Copyright (C) 2017-2019 Bareos GmbH & Co. KG + Copyright (C) 2017-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -53,7 +53,7 @@ int ordered_circbuf::init(int capacity) data_->destroy(); delete data_; } - data_ = new dlist(item, &item->link); + data_ = new old_dlist(item, &item->link); return 0; } diff --git a/core/src/stored/backends/ordered_cbuf.h b/core/src/stored/backends/ordered_cbuf.h index e085c2e6a80..ddd3bc66e75 100644 --- a/core/src/stored/backends/ordered_cbuf.h +++ b/core/src/stored/backends/ordered_cbuf.h @@ -25,8 +25,8 @@ #ifndef BAREOS_STORED_BACKENDS_ORDERED_CBUF_H_ #define BAREOS_STORED_BACKENDS_ORDERED_CBUF_H_ - -class dlist; +#include "old_dlist.h" +class old_dlist; #define OQSIZE 10 /* # of pointers in the queue */ @@ -41,7 +41,7 @@ enum oc_peek_types }; struct ocbuf_item { - dlink link; + old_dlink link; uint32_t data_size = 0; void* data = nullptr; }; @@ -57,7 +57,7 @@ class ordered_circbuf { = PTHREAD_COND_INITIALIZER; /* Full -> not full condition */ pthread_cond_t notempty_ = PTHREAD_COND_INITIALIZER; /* Empty -> not empty condition */ - dlist* data_ = nullptr; /* Circular buffer of pointers */ + old_dlist* data_ = nullptr; /* Circular buffer of pointers */ public: ordered_circbuf(int capacity = OQSIZE); diff --git a/core/src/stored/bextract.cc b/core/src/stored/bextract.cc index 57a7464fc0f..4013f9297c9 100644 --- a/core/src/stored/bextract.cc +++ b/core/src/stored/bextract.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -79,7 +79,7 @@ static DirectorResource* director = NULL; static AclData acl_data; static XattrData xattr_data; -static alist* delayed_streams = NULL; +static alist* delayed_streams = NULL; static char* wbuf; /* write buffer address */ static uint32_t wsize; /* write size */ @@ -279,7 +279,9 @@ static inline void PushDelayedDataStream(int stream, { DelayedDataStream* dds; - if (!delayed_streams) { delayed_streams = new alist(10, owned_by_alist); } + if (!delayed_streams) { + delayed_streams = new alist(10, owned_by_alist); + } dds = (DelayedDataStream*)malloc(sizeof(DelayedDataStream)); dds->stream = stream; diff --git a/core/src/stored/bsr.h b/core/src/stored/bsr.h index 871fb26ded1..7c3f4318b88 100644 --- a/core/src/stored/bsr.h +++ b/core/src/stored/bsr.h @@ -44,7 +44,7 @@ struct VolumeList { VolumeList* next; char VolumeName[MAX_NAME_LENGTH]; char MediaType[MAX_NAME_LENGTH]; - char device[MAX_NAME_LENGTH]; /* ***FIXME*** use alist here */ + char device[MAX_NAME_LENGTH]; int Slot; uint32_t start_file; }; @@ -62,8 +62,8 @@ struct BsrVolume { BsrVolume* next; char VolumeName[MAX_NAME_LENGTH]; char MediaType[MAX_NAME_LENGTH]; - char device[MAX_NAME_LENGTH]; /* ***FIXME*** use alist here */ - int32_t Slot; /* Slot */ + char device[MAX_NAME_LENGTH]; + int32_t Slot; /* Slot */ }; struct BsrClient { diff --git a/core/src/stored/dev.h b/core/src/stored/dev.h index 56a6118ce03..36c294e8edb 100644 --- a/core/src/stored/dev.h +++ b/core/src/stored/dev.h @@ -65,7 +65,7 @@ #include "stored/volume_catalog_info.h" #include - +template class dlist; namespace storagedaemon { diff --git a/core/src/stored/dir_cmd.cc b/core/src/stored/dir_cmd.cc index 3998f65e2b4..7a746a0a9f4 100644 --- a/core/src/stored/dir_cmd.cc +++ b/core/src/stored/dir_cmd.cc @@ -3,7 +3,7 @@ Copyright (C) 2001-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -587,7 +587,7 @@ static bool CancelCmd(JobControlRecord* cjcr) static bool ResolveCmd(JobControlRecord* jcr) { BareosSocket* dir = jcr->dir_bsock; - dlist* addr_list; + dlist* addr_list; const char* errstr; char addresses[2048]; char hostname[2048]; @@ -1818,7 +1818,7 @@ static bool PluginoptionsCmd(JobControlRecord* jcr) UnbashSpaces(plugin_options); if (!jcr->impl->plugin_options) { - jcr->impl->plugin_options = new alist(10, owned_by_alist); + jcr->impl->plugin_options = new alist(10, owned_by_alist); } jcr->impl->plugin_options->append(strdup(plugin_options)); diff --git a/core/src/stored/jcr_private.h b/core/src/stored/jcr_private.h index 3ed7d8cb158..22d56bd0cda 100644 --- a/core/src/stored/jcr_private.h +++ b/core/src/stored/jcr_private.h @@ -25,16 +25,21 @@ #define BAREOS_STORED_JCR_PRIVATE_H_ #include "stored/read_ctx.h" +#include "stored/stored_conf.h" #define SD_APPEND 1 #define SD_READ 0 +template +class alist; + namespace storagedaemon { struct VolumeList; class DeviceControlRecord; class DirectorResource; struct BootStrapRecord; +class DirectorStorage; struct ReadSession { READ_CTX* rctx{}; @@ -86,10 +91,10 @@ struct JobControlRecordPrivate { int64_t spool_size{}; /**< Spool size for this job */ bool spool_data{}; /**< Set to spool data */ storagedaemon::DirectorResource* director{}; /**< Director resource */ - alist* plugin_options{}; /**< Specific Plugin Options sent by DIR */ - alist* write_store{}; /**< List of write storage devices sent by DIR */ - alist* read_store{}; /**< List of read devices sent by DIR */ - alist* reserve_msgs{}; /**< Reserve fail messages */ + alist* plugin_options{}; /**< Specific Plugin Options sent by DIR */ + alist* write_store{}; /**< List of write storage devices sent by DIR */ + alist* read_store{}; /**< List of read devices sent by DIR */ + alist* reserve_msgs{}; /**< Reserve fail messages */ bool acquired_storage{}; /**< Did we acquire our reserved storage already or not */ bool PreferMountedVols{}; /**< Prefer mounted vols rather than new */ bool insert_jobmedia_records{}; /**< Need to insert job media records */ diff --git a/core/src/stored/ndmp_tape.cc b/core/src/stored/ndmp_tape.cc index c43d322f2c8..7275e8de178 100644 --- a/core/src/stored/ndmp_tape.cc +++ b/core/src/stored/ndmp_tape.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -87,7 +87,7 @@ namespace storagedaemon { * structure. */ struct ndmp_thread_server_args { - dlist* addr_list; + dlist* addr_list; int max_clients; ThreadList* thread_list; }; @@ -1155,7 +1155,7 @@ extern "C" void* ndmp_thread_server(void* arg) int turnon = 1; IPADDR *ipaddr, *next; struct s_sockfd { - dlink link; /* this MUST be the first item */ + dlink link; /* this MUST be the first item */ int fd; int port; }* fd_ptr = NULL; @@ -1174,12 +1174,7 @@ extern "C" void* ndmp_thread_server(void* arg) ipaddr = (IPADDR*)ntsa->addr_list->next(ipaddr)) { for (next = (IPADDR*)ntsa->addr_list->next(ipaddr); next; next = (IPADDR*)ntsa->addr_list->next(next)) { - if (ipaddr->GetSockaddrLen() == next->GetSockaddrLen() - && memcmp(ipaddr->get_sockaddr(), next->get_sockaddr(), - ipaddr->GetSockaddrLen()) - == 0) { - ntsa->addr_list->remove(next); - } + if (IsSameIpAddress(ipaddr, next)) { ntsa->addr_list->remove(next); } } } @@ -1346,7 +1341,7 @@ extern "C" void* ndmp_thread_server(void* arg) return NULL; } -int StartNdmpThreadServer(dlist* addr_list, int max_clients) +int StartNdmpThreadServer(dlist* addr_list, int max_clients) { int status; diff --git a/core/src/stored/ndmp_tape.h b/core/src/stored/ndmp_tape.h index 78135d4ead9..4be309bdb10 100644 --- a/core/src/stored/ndmp_tape.h +++ b/core/src/stored/ndmp_tape.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2019 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -21,13 +21,14 @@ #ifndef BAREOS_STORED_NDMP_TAPE_H_ #define BAREOS_STORED_NDMP_TAPE_H_ +template class dlist; namespace storagedaemon { void EndOfNdmpBackup(JobControlRecord* jcr); void EndOfNdmpRestore(JobControlRecord* jcr); -int StartNdmpThreadServer(dlist* addr_list, int max_clients); +int StartNdmpThreadServer(dlist* addr_list, int max_clients); void StopNdmpThreadServer(); } /* namespace storagedaemon */ diff --git a/core/src/stored/read_ctx.h b/core/src/stored/read_ctx.h index c691e9c84d5..54561c3c58c 100644 --- a/core/src/stored/read_ctx.h +++ b/core/src/stored/read_ctx.h @@ -33,7 +33,7 @@ struct DeviceRecord; /* clang-format off */ struct Read_Context { DeviceRecord* rec = nullptr; /**< Record currently being processed */ - dlist* recs = nullptr; /**< Linked list of record packets open */ + dlist* recs = nullptr; /**< Linked list of record packets open */ Session_Label sessrec; /**< Start Of Session record info */ uint32_t records_processed = 0; /**< Number of records processed from this block */ int32_t lastFileIndex = 0; /**< Last File Index processed */ diff --git a/core/src/stored/read_record.cc b/core/src/stored/read_record.cc index 8190b0a855b..9b8ff5a4fbd 100644 --- a/core/src/stored/read_record.cc +++ b/core/src/stored/read_record.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -126,7 +126,7 @@ READ_CTX* new_read_context(void) READ_CTX empty_READ_CTX; *rctx = empty_READ_CTX; - rctx->recs = new dlist(rec, &rec->link); + rctx->recs = new dlist(rec, &rec->link); return rctx; } diff --git a/core/src/stored/record.h b/core/src/stored/record.h index dc26a66b407..430cc3043a5 100644 --- a/core/src/stored/record.h +++ b/core/src/stored/record.h @@ -31,6 +31,7 @@ #include "lib/dlist.h" +template class dlist; namespace storagedaemon { @@ -105,7 +106,7 @@ enum */ struct BootStrapRecord; /* satisfy forward reference */ struct DeviceRecord { - dlink link; /**< link for chaining in read_record.c */ + dlink link; /**< link for chaining in read_record.c */ /**< * File and Block are always returned during reading and writing records. */ diff --git a/core/src/stored/reserve.cc b/core/src/stored/reserve.cc index 21b76692a2b..edf0867f969 100644 --- a/core/src/stored/reserve.cc +++ b/core/src/stored/reserve.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2012 Free Software Foundation Europe e.V. - Copyright (C) 2016-2020 Bareos GmbH & Co. KG + Copyright (C) 2016-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -193,7 +193,6 @@ static bool UseDeviceCmd(JobControlRecord* jcr) int32_t Copy, Stripe; DirectorStorage* store; ReserveContext rctx; - alist* dirstore; memset(&rctx, 0, sizeof(ReserveContext)); rctx.jcr = jcr; @@ -202,14 +201,15 @@ static bool UseDeviceCmd(JobControlRecord* jcr) * If there are multiple devices, the director sends us * use_device for each device that it wants to use. */ - jcr->impl->reserve_msgs = new alist(10, not_owned_by_alist); + jcr->impl->reserve_msgs = new alist(10, not_owned_by_alist); do { Dmsg1(debuglevel, "msg); ok = sscanf(dir->msg, use_storage, StoreName.c_str(), media_type.c_str(), pool_name.c_str(), pool_type.c_str(), &append, &Copy, &Stripe) == 7; if (!ok) { break; } - dirstore = new alist(10, not_owned_by_alist); + alist* dirstore + = new alist(10, not_owned_by_alist); if (append) { jcr->impl->write_store = dirstore; } else { @@ -223,7 +223,7 @@ static bool UseDeviceCmd(JobControlRecord* jcr) store = new DirectorStorage; dirstore->append(store); memset(store, 0, sizeof(DirectorStorage)); - store->device = new alist(10); + store->device = new alist(10); bstrncpy(store->name, StoreName, sizeof(store->name)); bstrncpy(store->media_type, media_type, sizeof(store->media_type)); bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name)); @@ -406,7 +406,7 @@ bool FindSuitableDeviceForJob(JobControlRecord* jcr, ReserveContext& rctx) bool ok = false; DirectorStorage* store = nullptr; char* device_name = nullptr; - alist* dirstore; + alist* dirstore; DeviceControlRecord* dcr = jcr->impl->dcr; if (rctx.append) { @@ -427,7 +427,7 @@ bool FindSuitableDeviceForJob(JobControlRecord* jcr, ReserveContext& rctx) * start by looking at all the Volumes in the volume list. */ if (!IsVolListEmpty() && rctx.append && rctx.PreferMountedVols) { - dlist* temp_vol_list; + dlist* temp_vol_list; VolumeReservationItem* vol = NULL; temp_vol_list = dup_vol_list(jcr); @@ -1135,16 +1135,15 @@ static int CanReserveDrive(DeviceControlRecord* dcr, ReserveContext& rctx) static void QueueReserveMessage(JobControlRecord* jcr) { int i; - alist* msgs; - char* msg; + const char* msg; jcr->lock(); - msgs = jcr->impl->reserve_msgs; + auto msgs = jcr->impl->reserve_msgs; if (!msgs) { goto bail_out; } // Look for duplicate message. If found, do not insert for (i = msgs->size() - 1; i >= 0; i--) { - msg = (char*)msgs->get(i); + msg = msgs->get(i); if (!msg) { goto bail_out; } // Comparison based on 4 digit message number @@ -1161,11 +1160,10 @@ static void QueueReserveMessage(JobControlRecord* jcr) // Pop and release any reservations messages static void PopReserveMessages(JobControlRecord* jcr) { - alist* msgs; char* msg; jcr->lock(); - msgs = jcr->impl->reserve_msgs; + auto msgs = jcr->impl->reserve_msgs; if (!msgs) { goto bail_out; } while ((msg = (char*)msgs->pop())) { free(msg); } bail_out: diff --git a/core/src/stored/reserve.h b/core/src/stored/reserve.h index 3927c838b68..bf885e495f3 100644 --- a/core/src/stored/reserve.h +++ b/core/src/stored/reserve.h @@ -38,13 +38,14 @@ #ifndef BAREOS_STORED_RESERVE_H_ #define BAREOS_STORED_RESERVE_H_ +template class alist; namespace storagedaemon { class DirectorStorage { public: - alist* device; + alist* device; bool append; char name[MAX_NAME_LENGTH]; char media_type[MAX_NAME_LENGTH]; diff --git a/core/src/stored/sd_plugins.cc b/core/src/stored/sd_plugins.cc index 5c9c2c1b236..8c1db3c919b 100644 --- a/core/src/stored/sd_plugins.cc +++ b/core/src/stored/sd_plugins.cc @@ -3,7 +3,7 @@ Copyright (C) 2007-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -45,7 +45,7 @@ const char* plugin_type = "-sd.dll"; #else const char* plugin_type = "-sd.so"; #endif -static alist* sd_plugin_list = NULL; +static alist* sd_plugin_list = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -238,7 +238,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, bSdEvent* event, PluginContext* ctx, void* value, - alist* plugin_ctx_list, + alist* plugin_ctx_list, int* index, bRC* rc) { @@ -274,7 +274,7 @@ static inline bool trigger_plugin_event(JobControlRecord* jcr, * that moved back a position in the alist. */ if (index) { - UnloadPlugin(plugin_ctx_list, ctx->plugin, *index); + UnloadPlugin(sd_plugin_list, ctx->plugin, *index); *index = ((*index) - 1); } break; @@ -306,7 +306,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, { int i; bSdEvent event; - alist* plugin_ctx_list; + alist* plugin_ctx_list; bRC rc = bRC_OK; if (!sd_plugin_list) { @@ -384,7 +384,7 @@ static void DumpSdPlugins(FILE* fp) { DumpPlugins(sd_plugin_list, fp); } * This entry point is called internally by Bareos to ensure * that the plugin IO calls come into this code. */ -void LoadSdPlugins(const char* plugin_dir, alist* plugin_names) +void LoadSdPlugins(const char* plugin_dir, alist* plugin_names) { Plugin* plugin; int i; @@ -394,7 +394,7 @@ void LoadSdPlugins(const char* plugin_dir, alist* plugin_names) Dmsg0(debuglevel, "No sd plugin dir!\n"); return; } - sd_plugin_list = new alist(10, not_owned_by_alist); + sd_plugin_list = new alist(10, not_owned_by_alist); if (!LoadPlugins((void*)&bareos_plugin_interface_version, (void*)&bareos_core_functions, sd_plugin_list, plugin_dir, plugin_names, plugin_type, IsPluginCompatible)) { @@ -611,7 +611,7 @@ void NewPlugins(JobControlRecord* jcr) Dmsg1(debuglevel, "sd-plugin-list size=%d\n", num); if (num == 0) { return; } - jcr->plugin_ctx_list = new alist(10, owned_by_alist); + jcr->plugin_ctx_list = new alist(10, owned_by_alist); foreach_alist_index (i, plugin, sd_plugin_list) { // Start a new instance of each plugin instantiate_plugin(jcr, plugin, 0); diff --git a/core/src/stored/sd_plugins.h b/core/src/stored/sd_plugins.h index 8beca5d729d..fa00ed93751 100644 --- a/core/src/stored/sd_plugins.h +++ b/core/src/stored/sd_plugins.h @@ -44,6 +44,7 @@ #include "include/bc_types.h" #include "lib/plugins.h" +template class alist; namespace storagedaemon { @@ -164,7 +165,7 @@ typedef struct s_sdbareosFuncs { // Bareos Core Routines -- not used within a plugin #ifdef STORAGE_DAEMON -void LoadSdPlugins(const char* plugin_dir, alist* plugin_names); +void LoadSdPlugins(const char* plugin_dir, alist* plugin_names); void UnloadSdPlugins(void); int ListSdPlugins(PoolMem& msg); void DispatchNewPluginOptions(JobControlRecord* jcr); diff --git a/core/src/stored/sd_stats.cc b/core/src/stored/sd_stats.cc index 63d1c5fce31..f2de658bdde 100644 --- a/core/src/stored/sd_stats.cc +++ b/core/src/stored/sd_stats.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2013-2020 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -55,8 +55,8 @@ static pthread_t statistics_tid; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t wait_for_next_run = PTHREAD_COND_INITIALIZER; -struct device_statistic { - dlink link; +struct device_statistic_item { + dlink link; bool collected{false}; utime_t timestamp{0}; btime_t DevReadTime{0}; @@ -72,22 +72,22 @@ struct device_statistic { uint64_t VolCatBlocks{0}; }; -struct device_tapealert { - dlink link; +struct device_tapealert_item { + dlink link; utime_t timestamp{0}; uint64_t flags{0}; }; -struct device_statistics { - dlink link; +struct device_statistics_t { + dlink link; char DevName[MAX_NAME_LENGTH]{}; - struct device_statistic* cached{nullptr}; - dlist* statistics{nullptr}; - dlist* tapealerts{nullptr}; + struct device_statistic_item* cached{nullptr}; + dlist* device_statistics{nullptr}; + dlist* device_tapealerts{nullptr}; }; -struct job_statistic { - dlink link; +struct jobstatistic_item { + dlink link; bool collected{false}; utime_t timestamp{0}; uint32_t JobFiles{0}; @@ -95,30 +95,31 @@ struct job_statistic { char* DevName{nullptr}; }; -struct job_statistics { - dlink link; +struct job_statistics_t { + dlink link; uint32_t JobId{0}; - struct job_statistic* cached{nullptr}; - dlist* statistics{nullptr}; + struct jobstatistic_item* cached{nullptr}; + dlist* job_statistics{nullptr}; }; -static dlist* device_statistics = NULL; -static dlist* job_statistics = NULL; +static dlist* device_statistics = NULL; +static dlist* job_statistics = NULL; static inline void setup_statistics() { - struct device_statistics* dev_stats = NULL; - struct job_statistics* job_stats = NULL; + device_statistics_t* dev_stats = NULL; + job_statistics_t* job_stats = NULL; - device_statistics = new dlist(dev_stats, &dev_stats->link); - job_statistics = new dlist(job_stats, &job_stats->link); + device_statistics + = new dlist(dev_stats, &dev_stats->link); + job_statistics = new dlist(job_stats, &job_stats->link); } void UpdateDeviceTapealert(const char* devname, uint64_t flags, utime_t now) { bool found = false; - struct device_statistics* dev_stats = NULL; - struct device_tapealert* tape_alert = NULL; + device_statistics_t* dev_stats = NULL; + struct device_tapealert_item* tape_alert = NULL; if (!me || !me->collect_dev_stats || !device_statistics) { return; } @@ -130,9 +131,8 @@ void UpdateDeviceTapealert(const char* devname, uint64_t flags, utime_t now) } if (!found) { - dev_stats - = (struct device_statistics*)malloc(sizeof(struct device_statistics)); - struct device_statistics empty_device_statistics; + dev_stats = (device_statistics_t*)malloc(sizeof(device_statistics_t)); + device_statistics_t empty_device_statistics; *dev_stats = empty_device_statistics; bstrncpy(dev_stats->DevName, devname, sizeof(dev_stats->DevName)); @@ -142,20 +142,21 @@ void UpdateDeviceTapealert(const char* devname, uint64_t flags, utime_t now) } // Add a new tapealert message. - tape_alert - = (struct device_tapealert*)malloc(sizeof(struct device_tapealert)); - struct device_tapealert empty_device_tapealert; + tape_alert = (struct device_tapealert_item*)malloc( + sizeof(struct device_tapealert_item)); + struct device_tapealert_item empty_device_tapealert; *tape_alert = empty_device_tapealert; tape_alert->timestamp = now; tape_alert->flags = flags; - if (!dev_stats->tapealerts) { - dev_stats->tapealerts = new dlist(tape_alert, &tape_alert->link); + if (!dev_stats->device_tapealerts) { + dev_stats->device_tapealerts + = new dlist(tape_alert, &tape_alert->link); } P(mutex); - dev_stats->tapealerts->append(tape_alert); + dev_stats->device_tapealerts->append(tape_alert); V(mutex); Dmsg3(200, "New stats [%lld]: Device %s TapeAlert %llu\n", @@ -167,8 +168,8 @@ static inline void UpdateDeviceStatistics(const char* devname, utime_t now) { bool found = false; - struct device_statistics* dev_stats = NULL; - struct device_statistic* dev_stat = NULL; + device_statistics_t* dev_stats = NULL; + struct device_statistic_item* dev_stat = NULL; if (!me || !me->collect_dev_stats || !device_statistics) { return; } @@ -194,9 +195,8 @@ static inline void UpdateDeviceStatistics(const char* devname, return; } } else if (!found) { - dev_stats - = (struct device_statistics*)malloc(sizeof(struct device_statistics)); - struct device_statistics empty_device_statistics; + dev_stats = (device_statistics_t*)malloc(sizeof(device_statistics_t)); + device_statistics_t empty_device_statistics; *dev_stats = empty_device_statistics; bstrncpy(dev_stats->DevName, devname, sizeof(dev_stats->DevName)); @@ -206,9 +206,10 @@ static inline void UpdateDeviceStatistics(const char* devname, } // Add a new set of statistics. - dev_stat = (struct device_statistic*)malloc(sizeof(struct device_statistic)); + dev_stat = (struct device_statistic_item*)malloc( + sizeof(struct device_statistic_item)); - struct device_statistic empty_device_statistic; + struct device_statistic_item empty_device_statistic; *dev_stat = empty_device_statistic; dev_stat->timestamp = now; @@ -225,13 +226,14 @@ static inline void UpdateDeviceStatistics(const char* devname, dev_stat->VolCatBlocks = dev->VolCatInfo.VolCatBlocks; - if (!dev_stats->statistics) { - dev_stats->statistics = new dlist(dev_stat, &dev_stat->link); + if (!dev_stats->device_statistics) { + dev_stats->device_statistics + = new dlist(dev_stat, &dev_stat->link); } P(mutex); dev_stats->cached = dev_stat; - dev_stats->statistics->append(dev_stat); + dev_stats->device_statistics->append(dev_stat); V(mutex); Dmsg5(200, @@ -249,8 +251,8 @@ static inline void UpdateDeviceStatistics(const char* devname, void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) { bool found = false; - struct job_statistics* job_stats = NULL; - struct job_statistic* job_stat = NULL; + job_statistics_t* job_stats = NULL; + jobstatistic_item* job_stat = NULL; if (!me || !me->collect_job_stats || !job_statistics) { return; } @@ -278,8 +280,8 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) return; } } else if (!found) { - job_stats = (struct job_statistics*)malloc(sizeof(struct job_statistics)); - struct job_statistics empty_job_statistics; + job_stats = (job_statistics_t*)malloc(sizeof(job_statistics_t)); + job_statistics_t empty_job_statistics; *job_stats = empty_job_statistics; job_stats->JobId = jcr->JobId; @@ -289,8 +291,9 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) } // Add a new set of statistics. - job_stat = (struct job_statistic*)malloc(sizeof(struct job_statistic)); - struct job_statistic empty_job_statistic; + job_stat + = (struct jobstatistic_item*)malloc(sizeof(struct jobstatistic_item)); + struct jobstatistic_item empty_job_statistic; *job_stat = empty_job_statistic; job_stat->timestamp = now; @@ -302,13 +305,14 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) job_stat->DevName = strdup("unknown"); } - if (!job_stats->statistics) { - job_stats->statistics = new dlist(job_stat, &job_stat->link); + if (!job_stats->job_statistics) { + job_stats->job_statistics + = new dlist(job_stat, &job_stat->link); } P(mutex); job_stats->cached = job_stat; - job_stats->statistics->append(job_stat); + job_stats->job_statistics->append(job_stat); V(mutex); Dmsg5( @@ -320,14 +324,14 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) static inline void cleanup_cached_statistics() { - struct device_statistics* dev_stats; - struct job_statistics* job_stats; + device_statistics_t* dev_stats; + job_statistics_t* job_stats; P(mutex); if (device_statistics) { foreach_dlist (dev_stats, device_statistics) { - dev_stats->statistics->destroy(); - dev_stats->statistics = NULL; + dev_stats->device_statistics->destroy(); + dev_stats->device_statistics = NULL; } device_statistics->destroy(); @@ -336,8 +340,8 @@ static inline void cleanup_cached_statistics() if (job_statistics) { foreach_dlist (job_stats, job_statistics) { - job_stats->statistics->destroy(); - job_stats->statistics = NULL; + job_stats->job_statistics->destroy(); + job_stats->job_statistics = NULL; } job_statistics->destroy(); @@ -458,16 +462,15 @@ bool StatsCmd(JobControlRecord* jcr) PoolMem dev_tmp(PM_MESSAGE); if (device_statistics) { - struct device_statistics* dev_stats; + device_statistics_t* dev_stats; foreach_dlist (dev_stats, device_statistics) { - if (dev_stats->statistics) { - struct device_statistic *dev_stat, *next_dev_stat; + if (dev_stats->device_statistics) { + struct device_statistic_item *dev_stat, *next_dev_stat; - dev_stat = (struct device_statistic*)dev_stats->statistics->first(); + dev_stat = dev_stats->device_statistics->first(); while (dev_stat) { - next_dev_stat - = (struct device_statistic*)dev_stats->statistics->next(dev_stat); + next_dev_stat = dev_stats->device_statistics->next(dev_stat); // If the entry was already collected no need to do it again. if (!dev_stat->collected) { @@ -489,7 +492,7 @@ bool StatsCmd(JobControlRecord* jcr) if (!next_dev_stat) { dev_stat->collected = true; } else { - dev_stats->statistics->remove(dev_stat); + dev_stats->device_statistics->remove(dev_stat); if (dev_stats->cached == dev_stat) { dev_stats->cached = NULL; } } @@ -498,10 +501,10 @@ bool StatsCmd(JobControlRecord* jcr) } } - if (dev_stats->tapealerts) { - struct device_tapealert *tape_alert, *next_tape_alert; + if (dev_stats->device_tapealerts) { + struct device_tapealert_item *tape_alert, *next_tape_alert; - tape_alert = (struct device_tapealert*)dev_stats->tapealerts->first(); + tape_alert = dev_stats->device_tapealerts->first(); while (tape_alert) { PmStrcpy(dev_tmp, dev_stats->DevName); BashSpaces(dev_tmp); @@ -510,11 +513,9 @@ bool StatsCmd(JobControlRecord* jcr) Dmsg1(100, ">dird: %s", msg.c_str()); dir->fsend(msg.c_str()); - next_tape_alert - = (struct device_tapealert*)dev_stats->tapealerts->next( - tape_alert); + next_tape_alert = dev_stats->device_tapealerts->next(tape_alert); P(mutex); - dev_stats->tapealerts->remove(tape_alert); + dev_stats->device_tapealerts->remove(tape_alert); V(mutex); tape_alert = next_tape_alert; } @@ -525,17 +526,16 @@ bool StatsCmd(JobControlRecord* jcr) if (job_statistics) { bool found; JobControlRecord* jcr; - struct job_statistics *job_stats, *next_job_stats; + job_statistics_t *job_stats, *next_job_stats; - job_stats = (struct job_statistics*)job_statistics->first(); + job_stats = (job_statistics_t*)job_statistics->first(); while (job_stats) { - if (job_stats->statistics) { - struct job_statistic *job_stat, *next_job_stat; + if (job_stats->job_statistics) { + struct jobstatistic_item *job_stat, *next_job_stat; - job_stat = (struct job_statistic*)job_stats->statistics->first(); + job_stat = job_stats->job_statistics->first(); while (job_stat) { - next_job_stat - = (struct job_statistic*)job_stats->statistics->next(job_stat); + next_job_stat = job_stats->job_statistics->next(job_stat); // If the entry was already collected no need to do it again. if (!job_stat->collected) { @@ -552,7 +552,7 @@ bool StatsCmd(JobControlRecord* jcr) if (!next_job_stat) { job_stat->collected = true; } else { - job_stats->statistics->remove(job_stat); + job_stats->job_statistics->remove(job_stat); if (job_stats->cached == job_stat) { job_stats->cached = NULL; } } V(mutex); @@ -561,7 +561,7 @@ bool StatsCmd(JobControlRecord* jcr) } // If the Job doesn't exist anymore remove it from the job_statistics. - next_job_stats = (struct job_statistics*)job_statistics->next(job_stats); + next_job_stats = job_statistics->next(job_stats); found = false; foreach_jcr (jcr) { diff --git a/core/src/stored/socket_server.cc b/core/src/stored/socket_server.cc index eda71075ccb..2bb87457c0b 100644 --- a/core/src/stored/socket_server.cc +++ b/core/src/stored/socket_server.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2012 Free Software Foundation Europe e.V. - Copyright (C) 2014-2020 Bareos GmbH & Co. KG + Copyright (C) 2014-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -43,7 +43,7 @@ namespace storagedaemon { static ThreadList thread_list; -static alist* sock_fds = NULL; +static alist* sock_fds = NULL; static pthread_t tcp_server_tid; // Sanity check for the lengths of the Hello messages. @@ -125,7 +125,7 @@ static void* UserAgentShutdownCallback(void* bsock) return nullptr; } -void StartSocketServer(dlist* addrs) +void StartSocketServer(dlist* addrs) { IPADDR* p; @@ -136,7 +136,7 @@ void StartSocketServer(dlist* addrs) Dmsg1(10, "stored: listening on port %d\n", p->GetPortHostOrder()); } - sock_fds = new alist(10, not_owned_by_alist); + sock_fds = new alist(10, not_owned_by_alist); BnetThreadServerTcp(addrs, me->MaxConnections, sock_fds, thread_list, HandleConnectionRequest, my_config, nullptr, UserAgentShutdownCallback); diff --git a/core/src/stored/socket_server.h b/core/src/stored/socket_server.h index e7784fefb55..e753d828289 100644 --- a/core/src/stored/socket_server.h +++ b/core/src/stored/socket_server.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2018 Bareos GmbH & Co. KG + Copyright (C) 2018-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -21,12 +21,14 @@ #ifndef BAREOS_STORED_SOCKET_SERVER_H_ #define BAREOS_STORED_SOCKET_SERVER_H_ +template class dlist; class ConfigurationParser; +class IPADDR; namespace storagedaemon { -void StartSocketServer(dlist* addrs); +void StartSocketServer(dlist* addrs); void StopSocketServer(); void* HandleConnectionRequest(ConfigurationParser* config, void* arg); diff --git a/core/src/stored/status.cc b/core/src/stored/status.cc index 61a709f4f38..2040519002d 100644 --- a/core/src/stored/status.cc +++ b/core/src/stored/status.cc @@ -752,7 +752,7 @@ static inline void SendDriveReserveMessages(JobControlRecord* jcr, StatusPacket* sp) { int i; - alist* msgs; + alist* msgs; char* msg; jcr->lock(); diff --git a/core/src/stored/stored_conf.h b/core/src/stored/stored_conf.h index cb47b37c97c..81f917a9385 100644 --- a/core/src/stored/stored_conf.h +++ b/core/src/stored/stored_conf.h @@ -34,9 +34,13 @@ #include "lib/messages_resource.h" #include "lib/tls_conf.h" +template class alist; +template class dlist; +class IPADDR; + namespace storagedaemon { static const std::string default_config_filename("bareos-sd.conf"); @@ -95,14 +99,15 @@ class StorageResource : public BareosResource , public TlsResource { public: - dlist* SDaddrs = nullptr; - dlist* SDsrc_addr = nullptr; /**< Address to source connections from */ - dlist* NDMPaddrs = nullptr; + dlist* SDaddrs = nullptr; + dlist* SDsrc_addr + = nullptr; /**< Address to source connections from */ + dlist* NDMPaddrs = nullptr; char* working_directory = nullptr; /**< Working directory for checkpoints */ char* pid_directory = nullptr; char* subsys_directory = nullptr; char* plugin_directory = nullptr; /**< Plugin directory */ - alist* plugin_names = nullptr; + alist* plugin_names = nullptr; char* scripts_directory = nullptr; std::vector backend_directories; uint32_t MaxConcurrentJobs = 0; /**< Maximum concurrent jobs to run */ diff --git a/core/src/stored/vol_mgr.cc b/core/src/stored/vol_mgr.cc index 2bc61c5cebd..b4a0a55938f 100644 --- a/core/src/stored/vol_mgr.cc +++ b/core/src/stored/vol_mgr.cc @@ -41,8 +41,8 @@ namespace storagedaemon { const int debuglevel = 150; static brwlock_t vol_list_lock; -static dlist* vol_list = NULL; -static dlist* read_vol_list = NULL; +static dlist* vol_list = NULL; +static dlist* read_vol_list = NULL; static pthread_mutex_t read_vol_lock = PTHREAD_MUTEX_INITIALIZER; /* Global static variables */ @@ -57,10 +57,11 @@ static VolumeReservationItem* new_vol_item(DeviceControlRecord* dcr, static void DebugListVolumes(const char* imsg); // For append volumes the key is the VolumeName. -static int CompareByVolumename(void* item1, void* item2) +static int CompareByVolumename(VolumeReservationItem* item1, + VolumeReservationItem* item2) { - VolumeReservationItem* vol1 = (VolumeReservationItem*)item1; - VolumeReservationItem* vol2 = (VolumeReservationItem*)item2; + VolumeReservationItem* vol1 = item1; + VolumeReservationItem* vol2 = item2; ASSERT(vol1->vol_name); ASSERT(vol2->vol_name); @@ -69,10 +70,11 @@ static int CompareByVolumename(void* item1, void* item2) } // For read volumes the key is JobId, VolumeName. -static int ReadCompare(void* item1, void* item2) +static int ReadCompare(VolumeReservationItem* item1, + VolumeReservationItem* item2) { - VolumeReservationItem* vol1 = (VolumeReservationItem*)item1; - VolumeReservationItem* vol2 = (VolumeReservationItem*)item2; + VolumeReservationItem* vol1 = item1; + VolumeReservationItem* vol2 = item2; ASSERT(vol1->vol_name); ASSERT(vol2->vol_name); @@ -798,12 +800,17 @@ bool FreeVolume(Device* dev) void CreateVolumeLists() { VolumeReservationItem* vol = NULL; - if (vol_list == NULL) { vol_list = new dlist(vol, &vol->link); } - if (read_vol_list == NULL) { read_vol_list = new dlist(vol, &vol->link); } + if (vol_list == NULL) { + vol_list = new dlist(vol, &vol->link); + } + if (read_vol_list == NULL) { + read_vol_list = new dlist(vol, &vol->link); + } } // Free normal append volumes list -static inline void FreeVolumeList(const char* what, dlist* vollist) +static inline void FreeVolumeList(const char* what, + dlist* vollist) { VolumeReservationItem* vol; @@ -904,15 +911,15 @@ bool DeviceControlRecord::Can_i_use_volume() * we can take note and act accordingly (probably redo the * search at least a few times). */ -dlist* dup_vol_list(JobControlRecord* jcr) +dlist* dup_vol_list(JobControlRecord* jcr) { - dlist* temp_vol_list; + dlist* temp_vol_list; VolumeReservationItem* vol = NULL; Dmsg0(debuglevel, "lock volumes\n"); Dmsg0(debuglevel, "duplicate vol list\n"); - temp_vol_list = new dlist(vol, &vol->link); + temp_vol_list = new dlist(vol, &vol->link); foreach_vol (vol) { VolumeReservationItem *nvol, *tvol; @@ -935,7 +942,7 @@ dlist* dup_vol_list(JobControlRecord* jcr) } // Free the specified temp list. -void FreeTempVolList(dlist* temp_vol_list) +void FreeTempVolList(dlist* temp_vol_list) { FreeVolumeList("temp_vol_list", temp_vol_list); delete temp_vol_list; diff --git a/core/src/stored/vol_mgr.h b/core/src/stored/vol_mgr.h index d8657d38ebf..9adb08d982a 100644 --- a/core/src/stored/vol_mgr.h +++ b/core/src/stored/vol_mgr.h @@ -46,6 +46,7 @@ #ifndef BAREOS_STORED_VOL_MGR_H_ #define BAREOS_STORED_VOL_MGR_H_ +template class dlist; namespace storagedaemon { @@ -68,7 +69,7 @@ class VolumeReservationItem { volatile int32_t use_count_{0}; /**< Use count */ pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Vol muntex */ public: - dlink link; + dlink link; char* vol_name{nullptr}; /**< Volume name */ Device* dev{nullptr}; /**< Pointer to device to which we are attached */ @@ -122,8 +123,8 @@ VolumeReservationItem* reserve_volume(DeviceControlRecord* dcr, const char* VolumeName); bool FreeVolume(Device* dev); bool IsVolListEmpty(); -dlist* dup_vol_list(JobControlRecord* jcr); -void FreeTempVolList(dlist* temp_vol_list); +dlist* dup_vol_list(JobControlRecord* jcr); +void FreeTempVolList(dlist* temp_vol_list); bool VolumeUnused(DeviceControlRecord* dcr); void CreateVolumeLists(); void FreeVolumeLists(); diff --git a/core/src/tests/alist_test.cc b/core/src/tests/alist_test.cc index 0797626400f..9869a1f9086 100644 --- a/core/src/tests/alist_test.cc +++ b/core/src/tests/alist_test.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2015-2019 Bareos GmbH & Co. KG + Copyright (C) 2015-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -41,12 +41,12 @@ struct FileSet { FileSet() = default; ~FileSet() = default; - alist mylist; + alist mylist; }; // helper functions -void AlistFill(alist* list, int max) +void AlistFill(alist* list, int max) { char buf[30]; int start = 0; @@ -64,7 +64,7 @@ void AlistFill(alist* list, int max) } // we expect, that the list is filled with strings of numbers from 0 to n. -void TestForeachAlist(alist* list) +void TestForeachAlist(alist* list) { char* str = NULL; char buf[30]; @@ -108,7 +108,7 @@ void test_alist_init_destroy() void test_alist_dynamic() { - alist* list = NULL; + alist* list = nullptr; char* buf; // NULL->size() will segfault @@ -118,7 +118,7 @@ void test_alist_dynamic() TestForeachAlist(list); // create empty list, which is prepared for a number of entires - list = new alist(10); + list = new alist(10); EXPECT_EQ(list->size(), 0); // does foreach work for empty lists? diff --git a/core/src/tests/dlist_test.cc b/core/src/tests/dlist_test.cc index 3d44790a2f5..f7a58db49b1 100644 --- a/core/src/tests/dlist_test.cc +++ b/core/src/tests/dlist_test.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2015-2020 Bareos GmbH & Co. KG + Copyright (C) 2015-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -41,7 +41,7 @@ struct MYJCR { char* buf; - dlink link; + dlink link; }; /* @@ -50,22 +50,19 @@ struct MYJCR { * (otherwise loffset must be calculated) */ struct ListItem { - dlink link; + dlink link; char* buf; }; - -static int MyCompare(void* item1, void* item2) +template +static int MyCompare(T* item1, T* item2) { - MYJCR *jcr1, *jcr2; int comp; - jcr1 = (MYJCR*)item1; - jcr2 = (MYJCR*)item2; - comp = strcmp(jcr1->buf, jcr2->buf); + comp = strcmp(item1->buf, item2->buf); return comp; } // we expect, that the list is filled with strings of numbers from 0 to n. -void TestForeachDlist(dlist* list) +void TestForeachDlist(dlist* list) { ListItem* val = NULL; char buf[30]; @@ -79,7 +76,7 @@ void TestForeachDlist(dlist* list) } } -void DlistFill(dlist* list, int max) +void DlistFill(dlist* list, int max) { ListItem* jcr = NULL; char buf[30]; @@ -106,7 +103,7 @@ void FreeItem(ListItem* item) } } -void FreeDlist(dlist* list) +void FreeDlist(dlist* list) { ListItem* val = NULL; int number = list->size(); @@ -124,7 +121,7 @@ void FreeDlist(dlist* list) void test_dlist_dynamic() { - dlist* list = NULL; + dlist* list = NULL; ListItem* item = NULL; // NULL->size() will segfault @@ -134,7 +131,7 @@ void test_dlist_dynamic() TestForeachDlist(list); // create empty list - list = new dlist(); + list = new dlist(); EXPECT_EQ(list->size(), 0); // does foreach work for empty lists? @@ -170,7 +167,7 @@ void test_dlist_dynamic() TEST(dlist, dlist) { char buf[30]; - dlist* jcr_chain; + dlist* jcr_chain; MYJCR* jcr = NULL; MYJCR* jcr1; MYJCR* save_jcr = NULL; @@ -178,7 +175,7 @@ TEST(dlist, dlist) int count; int index = 0; - jcr_chain = (dlist*)malloc(sizeof(dlist)); + jcr_chain = (dlist*)malloc(sizeof(dlist)); jcr_chain->init(jcr, &jcr->link); for (int i = 0; i < 20; i++) { @@ -210,7 +207,7 @@ TEST(dlist, dlist) * of jcr objects. Within a jcr object, there is a buf * that points to a malloced string containing data */ - jcr_chain = new dlist(jcr, &jcr->link); + jcr_chain = new dlist(jcr, &jcr->link); for (int i = 0; i < 20; i++) { sprintf(buf, "%d", i); jcr = (MYJCR*)malloc(sizeof(MYJCR)); @@ -238,7 +235,7 @@ TEST(dlist, dlist) /* Now do a binary insert for the list */ - jcr_chain = new dlist(jcr, &jcr->link); + jcr_chain = new dlist(jcr, &jcr->link); #define CNT 6 strcpy(buf, "ZZZ"); count = 0; @@ -248,7 +245,7 @@ TEST(dlist, dlist) count++; jcr = (MYJCR*)malloc(sizeof(MYJCR)); jcr->buf = strdup(buf); - jcr1 = (MYJCR*)jcr_chain->binary_insert(jcr, MyCompare); + jcr1 = jcr_chain->binary_insert(jcr, MyCompare); EXPECT_EQ(jcr, jcr1); buf[1]--; } @@ -287,7 +284,7 @@ TEST(dlist, dlist) * allocates a dlist node and stores the string directly in * it. */ - auto chain = std::make_unique(); + auto chain = std::make_unique>(); chain->append(new_dlistString("This is a long test line")); #define CNT 6 strcpy(buf, "ZZZ"); diff --git a/core/src/tests/test_config_parser_dir.cc b/core/src/tests/test_config_parser_dir.cc index 4b03c1a875d..22b7e43afc6 100644 --- a/core/src/tests/test_config_parser_dir.cc +++ b/core/src/tests/test_config_parser_dir.cc @@ -220,8 +220,9 @@ void test_CFG_TYPE_FNAME(DirectorResource* me) EXPECT_EQ(fileset1->exclude_items.size(), 0); EXPECT_EQ(fileset1->include_items.size(), 1); - alist* files = std::addressof(fileset1->include_items.at(0)->name_list); - char* val = nullptr; + alist* files + = std::addressof(fileset1->include_items.at(0)->name_list); + const char* val = nullptr; foreach_alist (val, files) { printf("Files = %s\n", val); } diff --git a/core/src/win32/findlib/win32.cc b/core/src/win32/findlib/win32.cc index 5619b25a2c0..593a5be69fe 100644 --- a/core/src/win32/findlib/win32.cc +++ b/core/src/win32/findlib/win32.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2013-2019 Bareos GmbH & Co. KG + Copyright (C) 2013-2021 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -146,7 +146,8 @@ int get_win32_driveletters(findFILESET* fileset, char* szDrives) * function returns the number of used virtual mountpoints and fills szVmps with * a list of all virtual mountpoints. */ -int get_win32_virtualmountpoints(findFILESET* fileset, dlist** szVmps) +int get_win32_virtualmountpoints(findFILESET* fileset, + dlist** szVmps) { int i, cnt; char* fname; @@ -175,7 +176,7 @@ int get_win32_virtualmountpoints(findFILESET* fileset, dlist** szVmps) // See if we need to allocate a new dlist. if (!cnt) { if (!*szVmps) { - *szVmps = (dlist*)malloc(sizeof(dlist)); + *szVmps = (dlist*)malloc(sizeof(dlist)); (*szVmps)->init(); } } From f88556aa5c48249677e75d29ec287465183df742 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Wed, 28 Apr 2021 15:07:58 +0200 Subject: [PATCH 2/6] stored: remove old_dlist/old_dlink Because of weird usage of dlist and dlink in ordered_cbuf there was a copy of the original dlist/dlink as old_dlist/old_dlink. As this is basically the same as using the templated version with typename void, this patch replaces all uses of old_dlist/old_dlink with dlist and dlink rendering old_dlist/old_dlink unused. --- core/src/stored/backends/CMakeLists.txt | 4 +- core/src/stored/backends/old_dlink.h | 36 --- core/src/stored/backends/old_dlist.cc | 336 ----------------------- core/src/stored/backends/old_dlist.h | 187 ------------- core/src/stored/backends/ordered_cbuf.cc | 2 +- core/src/stored/backends/ordered_cbuf.h | 6 +- 6 files changed, 4 insertions(+), 567 deletions(-) delete mode 100644 core/src/stored/backends/old_dlink.h delete mode 100644 core/src/stored/backends/old_dlist.cc delete mode 100644 core/src/stored/backends/old_dlist.h diff --git a/core/src/stored/backends/CMakeLists.txt b/core/src/stored/backends/CMakeLists.txt index 54b620fac79..38f4f956add 100644 --- a/core/src/stored/backends/CMakeLists.txt +++ b/core/src/stored/backends/CMakeLists.txt @@ -40,9 +40,7 @@ if(${HAVE_GLUSTERFS}) endif() if(TARGET droplet) - add_library( - bareossd-chunked SHARED ordered_cbuf.cc chunked_device.cc old_dlist.cc - ) + add_library(bareossd-chunked SHARED ordered_cbuf.cc chunked_device.cc) target_link_libraries(bareossd-chunked droplet) install(TARGETS bareossd-chunked DESTINATION ${backenddir}) diff --git a/core/src/stored/backends/old_dlink.h b/core/src/stored/backends/old_dlink.h deleted file mode 100644 index 5a54444bcc3..00000000000 --- a/core/src/stored/backends/old_dlink.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2004-2010 Free Software Foundation Europe e.V. - Copyright (C) 2016-2021 Bareos GmbH & Co. KG - - This program is Free Software; you can redistribute it and/or - modify it under the terms of version three of the GNU Affero General Public - License as published by the Free Software Foundation and included - in the file LICENSE. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. -*/ - -#ifndef BAREOS_LIB_OLD_DLINK_H_ -#define BAREOS_LIB_OLD_DLINK_H_ - -struct old_dlink { - void* next; - void* prev; - old_dlink() - { - next = nullptr; - prev = nullptr; - } -}; - -#endif // BAREOS_LIB_OLD_DLINK_H_ diff --git a/core/src/stored/backends/old_dlist.cc b/core/src/stored/backends/old_dlist.cc deleted file mode 100644 index 8de6fe016bd..00000000000 --- a/core/src/stored/backends/old_dlist.cc +++ /dev/null @@ -1,336 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2013-2019 Bareos GmbH & Co. KG - - This program is Free Software; you can redistribute it and/or - modify it under the terms of version three of the GNU Affero General Public - License as published by the Free Software Foundation and included - in the file LICENSE. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. -*/ -/* - * BAREOS doubly linked list routines. - * - * old_dlist is a doubly linked list with the links being in the list data item. - * - * Kern Sibbald, July MMIII - */ - -#include "include/bareos.h" -#include "old_dlink.h" -#include "old_dlist.h" - - -/** - * Init old_dlist - */ -void old_dlist::init(void* item, old_dlink* link) -{ - head = tail = NULL; - loffset = (int)((char*)link - (char*)item); - if (loffset < 0 || loffset > 5000) { - Emsg0(M_ABORT, 0, "Improper old_dlist initialization.\n"); - } - num_items = 0; -} - -/* - * Append an item to the list - */ -void old_dlist::append(void* item) -{ - SetNext(item, NULL); - SetPrev(item, tail); - if (tail) { SetNext(tail, item); } - tail = item; - if (head == NULL) { /* if empty list, */ - head = item; /* item is head as well */ - } - num_items++; -} - -/* - * Prepend an item to the list - */ -void old_dlist::prepend(void* item) -{ - SetNext(item, head); - SetPrev(item, NULL); - if (head) { SetPrev(head, item); } - head = item; - if (tail == NULL) { /* if empty list, */ - tail = item; /* item is tail too */ - } - num_items++; -} - -void old_dlist::InsertBefore(void* item, void* where) -{ - old_dlink* where_link = get_link(where); - - SetNext(item, where); - SetPrev(item, where_link->prev); - - if (where_link->prev) { SetNext(where_link->prev, item); } - where_link->prev = item; - if (head == where) { head = item; } - num_items++; -} - -void old_dlist::InsertAfter(void* item, void* where) -{ - old_dlink* where_link = get_link(where); - - SetNext(item, where_link->next); - SetPrev(item, where); - - if (where_link->next) { SetPrev(where_link->next, item); } - where_link->next = item; - if (tail == where) { tail = item; } - num_items++; -} - -/* - * Insert an item in the list, but only if it is unique - * otherwise, the item is returned non inserted - * - * Returns: item if item inserted - * other_item if same value already exists (item not inserted) - */ -void* old_dlist::binary_insert(void* item, - int compare(void* item1, void* item2)) -{ - int comp; - int low, high, cur; - void* cur_item; - - if (num_items == 0) { - // Dmsg0(000, "Append first.\n"); - append(item); - return item; - } - if (num_items == 1) { - comp = compare(item, first()); - if (comp < 0) { - prepend(item); - // Dmsg0(000, "Insert before first.\n"); - return item; - } else if (comp > 0) { - InsertAfter(item, first()); - // Dmsg0(000, "Insert after first.\n"); - return item; - } else { - // Dmsg0(000, "Same as first.\n"); - return first(); - } - } - /* Check against last item */ - comp = compare(item, last()); - if (comp > 0) { - append(item); - // Dmsg0(000, "Appended item.\n"); - return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as last.\n"); - return last(); - } - /* Check against first item */ - comp = compare(item, first()); - if (comp < 0) { - prepend(item); - // Dmsg0(000, "Inserted item before.\n"); - return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as first.\n"); - return first(); - } - if (num_items == 2) { - InsertAfter(item, first()); - // Dmsg0(000, "Inserted item after.\n"); - return item; - } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - while (nxt > cur) { - cur_item = next(cur_item); - cur++; - } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; - } - // Dmsg1(000, "Compare item to %d\n", cur); - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - // Dmsg1(000, "Same as item %d\n", cur); - return cur_item; - } - } - if (high == cur) { - InsertBefore(item, cur_item); - // Dmsg1(000, "Insert before item %d\n", cur); - } else { - InsertAfter(item, cur_item); - // Dmsg1(000, "Insert after item %d\n", cur); - } - return item; -} - -/* - * Insert an item in the list, regardless if it is unique - * or not. - */ -void old_dlist::BinaryInsertMultiple(void* item, - int compare(void* item1, void* item2)) -{ - void* ins_item = binary_insert(item, compare); - /* If identical, insert after the one found */ - if (ins_item != item) { InsertAfter(item, ins_item); } -} - -/* - * Search for item - */ -void* old_dlist::binary_search(void* item, - int compare(void* item1, void* item2)) -{ - int comp; - int low, high, cur; - void* cur_item; - - - if (num_items == 0) { return NULL; } - cur_item = first(); - if (num_items == 1) { - comp = compare(item, cur_item); - if (comp == 0) { - return cur_item; - } else { - return NULL; - } - } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - /* Now get cur pointing to nxt */ - while (nxt > cur) { - cur_item = next(cur_item); - cur++; - } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; - } - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - return cur_item; - } - } - /* - * low == high can only happen if low just - * got incremented from cur, and we have - * not yet tested cur+1 - */ - if (low == high) { - cur_item = next(cur_item); - comp = compare(item, cur_item); - if (comp == 0) { return cur_item; } - } - return NULL; -} - - -void old_dlist::remove(void* item) -{ - void* xitem; - old_dlink* ilink = get_link(item); /* item's link */ - if (item == head) { - head = ilink->next; - if (head) { SetPrev(head, NULL); } - if (item == tail) { tail = ilink->prev; } - } else if (item == tail) { - tail = ilink->prev; - if (tail) { SetNext(tail, NULL); } - } else { - xitem = ilink->next; - SetPrev(xitem, ilink->prev); - xitem = ilink->prev; - SetNext(xitem, ilink->next); - } - num_items--; - if (num_items == 0) { head = tail = NULL; } -} - -void* old_dlist::next(void* item) -{ - if (item == NULL) { return head; } - return get_next(item); -} - -void* old_dlist::prev(void* item) -{ - if (item == NULL) { return tail; } - return get_prev(item); -} - - -/* Destroy the list contents */ -void old_dlist::destroy() -{ - for (void* n = head; n;) { - void* ni = get_next(n); - free(n); - n = ni; - } - num_items = 0; - head = tail = NULL; -} - -/* String helpers for old_dlist usage */ - -old_dlistString* new_old_dlistString(const char* str) -{ - return new_old_dlistString(str, strlen(str)); -} - -old_dlistString* new_old_dlistString(const char* str, int len) -{ - old_dlistString* node; - node = (old_dlistString*)malloc(sizeof(old_dlink) + len + 1); - bstrncpy(node->c_str(), str, len + 1); - return node; -} diff --git a/core/src/stored/backends/old_dlist.h b/core/src/stored/backends/old_dlist.h deleted file mode 100644 index 7efec58138f..00000000000 --- a/core/src/stored/backends/old_dlist.h +++ /dev/null @@ -1,187 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2004-2010 Free Software Foundation Europe e.V. - Copyright (C) 2016-2021 Bareos GmbH & Co. KG - - This program is Free Software; you can redistribute it and/or - modify it under the terms of version three of the GNU Affero General Public - License as published by the Free Software Foundation and included - in the file LICENSE. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. -*/ -/* - * Kern Sibbald, MMIV and MMVII - */ -/** - * @file - * Doubly linked list -- old_dlist - * - * See the end of the file for the old_dlistString class which - * facilitates storing strings in a old_dlist. - */ - -#ifndef BAREOS_LIB_OLD_DLIST_H_ -#define BAREOS_LIB_OLD_DLIST_H_ - -#include "include/bareos.h" -#include "old_dlink.h" -#include "lib/message.h" - -#define M_ABORT 1 - -/* In case you want to specifically specify the offset to the link */ -#define OFFSET(item, link) (int)((char*)(link) - (char*)(item)) -/** - * There is a lot of extra casting here to work around the fact - * that some compilers (Sun and Visual C++) do not accept - * (void *) as an lvalue on the left side of an equal. - * - * Loop var through each member of list - */ -#ifdef HAVE_TYPEOF -# define foreach_old_dlist(var, list) \ - for ((var) = nullptr; \ - (list ? ((var) = (typeof(var))(list)->next(var)) : nullptr) \ - != nullptr;) -#else -# define foreach_old_dlist(var, list) \ - for ((var) = nullptr; \ - (list ? (*((void**)&(var)) = (void*)((list)->next(var))) : nullptr) \ - != nullptr;) -#endif - -class old_dlist { - void* head; - void* tail; - int16_t loffset; - uint32_t num_items; - - public: - old_dlist(void* item, old_dlink* link); - old_dlist(void); - ~old_dlist() { destroy(); } - void init(void* item, old_dlink* link); - void init(); - void prepend(void* item); - void append(void* item); - void SetPrev(void* item, void* prev); - void SetNext(void* item, void* next); - void* get_prev(void* item); - void* get_next(void* item); - old_dlink* get_link(void* item); - void InsertBefore(void* item, void* where); - void InsertAfter(void* item, void* where); - void* binary_insert(void* item, int compare(void* item1, void* item2)); - void* binary_search(void* item, int compare(void* item1, void* item2)); - void BinaryInsertMultiple(void* item, int compare(void* item1, void* item2)); - void remove(void* item); - bool empty() const; - int size() const; - void* next(void* item); - void* prev(void* item); - void destroy(); - void* first() const; - void* last() const; -}; - -inline void old_dlist::init() -{ - head = tail = nullptr; - loffset = 0; - num_items = 0; -} - - -/** - * Constructor called with the address of a - * member of the list (not the list head), and - * the address of the link within that member. - * If the link is at the beginning of the list member, - * then there is no need to specify the link address - * since the offset is zero. - */ -inline old_dlist::old_dlist(void* item, old_dlink* link) { init(item, link); } - -/* Constructor with link at head of item */ -inline old_dlist::old_dlist(void) - : head(nullptr), tail(nullptr), loffset(0), num_items(0) -{ - return; -} - -inline void old_dlist::SetPrev(void* item, void* prev) -{ - ((old_dlink*)(((char*)item) + loffset))->prev = prev; -} - -inline void old_dlist::SetNext(void* item, void* next) -{ - ((old_dlink*)(((char*)item) + loffset))->next = next; -} - -inline void* old_dlist::get_prev(void* item) -{ - return ((old_dlink*)(((char*)item) + loffset))->prev; -} - -inline void* old_dlist::get_next(void* item) -{ - return ((old_dlink*)(((char*)item) + loffset))->next; -} - - -inline old_dlink* old_dlist::get_link(void* item) -{ - return (old_dlink*)(((char*)item) + loffset); -} - - -inline bool old_dlist::empty() const { return head == nullptr; } - -inline int old_dlist::size() const { return num_items; } - - -inline void* old_dlist::first() const { return head; } - -inline void* old_dlist::last() const { return tail; } - -/** - * C string helper routines for old_dlist - * The string (char *) is kept in the node - * - * Kern Sibbald, February 2007 - * - */ -class old_dlistString { - public: - char* c_str() { return str_; } - - private: -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wunused-private-field" -#endif - old_dlink link_; -#ifdef __clang__ -# pragma clang diagnostic pop -#endif - char str_[1]; - /* !!! Don't put anything after this as this space is used - * to hold the string in inline - */ -}; - -extern old_dlistString* new_old_dlistString(const char* str, int len); -extern old_dlistString* new_old_dlistString(const char* str); - -#endif // BAREOS_LIB_OLD_DLIST_H_ diff --git a/core/src/stored/backends/ordered_cbuf.cc b/core/src/stored/backends/ordered_cbuf.cc index 2ada661239e..51bee46250f 100644 --- a/core/src/stored/backends/ordered_cbuf.cc +++ b/core/src/stored/backends/ordered_cbuf.cc @@ -53,7 +53,7 @@ int ordered_circbuf::init(int capacity) data_->destroy(); delete data_; } - data_ = new old_dlist(item, &item->link); + data_ = new dlist(item, &item->link); return 0; } diff --git a/core/src/stored/backends/ordered_cbuf.h b/core/src/stored/backends/ordered_cbuf.h index ddd3bc66e75..da81456058b 100644 --- a/core/src/stored/backends/ordered_cbuf.h +++ b/core/src/stored/backends/ordered_cbuf.h @@ -25,8 +25,6 @@ #ifndef BAREOS_STORED_BACKENDS_ORDERED_CBUF_H_ #define BAREOS_STORED_BACKENDS_ORDERED_CBUF_H_ -#include "old_dlist.h" -class old_dlist; #define OQSIZE 10 /* # of pointers in the queue */ @@ -41,7 +39,7 @@ enum oc_peek_types }; struct ocbuf_item { - old_dlink link; + dlink link; uint32_t data_size = 0; void* data = nullptr; }; @@ -57,7 +55,7 @@ class ordered_circbuf { = PTHREAD_COND_INITIALIZER; /* Full -> not full condition */ pthread_cond_t notempty_ = PTHREAD_COND_INITIALIZER; /* Empty -> not empty condition */ - old_dlist* data_ = nullptr; /* Circular buffer of pointers */ + dlist* data_ = nullptr; /* Circular buffer of pointers */ public: ordered_circbuf(int capacity = OQSIZE); From ab89ab571c4ca82a8abe769257f39cf2e7045a3e Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Mon, 10 May 2021 11:59:29 +0200 Subject: [PATCH 3/6] clang-format: unbreak template<> one-liners Previously clang-format always added a linebreak in templates. Even in a forward declaration like `template class alist`. This patch changes the configuration for clang-format to not add this linebreak and reruns clang-format on recently changed files. --- .clang-format | 1 + core/src/cats/cats.h | 3 +- core/src/cats/cats_backends.cc | 4 +- core/src/console/console.cc | 2 +- core/src/dird/dir_plugins.h | 3 +- core/src/dird/dird.h | 3 +- core/src/dird/dird_conf.h | 3 +- core/src/dird/jobq.h | 3 +- core/src/dird/migrate.cc | 2 +- core/src/dird/msgchan.h | 3 +- core/src/dird/ndmp_dma_storage.h | 3 +- core/src/dird/sd_cmds.h | 3 +- core/src/dird/socket_server.h | 3 +- core/src/dird/storage.h | 3 +- core/src/dird/ua_select.h | 3 +- core/src/filed/fd_plugins.h | 3 +- core/src/filed/filed_conf.h | 6 +- core/src/filed/restore.h | 3 +- core/src/filed/socket_server.h | 3 +- core/src/findlib/xattr.cc | 8 +-- core/src/findlib/xattr.h | 3 +- core/src/include/jcr.h | 3 +- core/src/lib/alist.h | 60 ++++++------------- core/src/lib/bnet.h | 3 +- core/src/lib/bnet_server_tcp.h | 3 +- core/src/lib/breg.h | 3 +- core/src/lib/bsock.h | 5 +- core/src/lib/connection_pool.h | 3 +- core/src/lib/crypto.h | 7 +-- core/src/lib/crypto_openssl.cc | 2 +- core/src/lib/dlink.h | 3 +- core/src/lib/dlist.cc | 1 - core/src/lib/dlist.h | 72 +++++++---------------- core/src/lib/guid_to_name.h | 3 +- core/src/lib/ini.h | 3 +- core/src/lib/plugins.h | 3 +- core/src/lib/resource_item.h | 12 ++-- core/src/lib/runscript.h | 3 +- core/src/stored/autochanger_resource.h | 3 +- core/src/stored/backends/chunked_device.h | 3 +- core/src/stored/backends/ordered_cbuf.h | 2 +- core/src/stored/dev.h | 3 +- core/src/stored/jcr_private.h | 3 +- core/src/stored/ndmp_tape.h | 3 +- core/src/stored/record.h | 3 +- core/src/stored/reserve.h | 3 +- core/src/stored/sd_plugins.h | 3 +- core/src/stored/socket_server.h | 3 +- core/src/stored/stored_conf.h | 6 +- core/src/stored/vol_mgr.h | 3 +- core/src/tests/dlist_test.cc | 3 +- 51 files changed, 98 insertions(+), 200 deletions(-) diff --git a/.clang-format b/.clang-format index 96a22549a96..9b35bbbd088 100644 --- a/.clang-format +++ b/.clang-format @@ -10,6 +10,7 @@ AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: true AllowShortFunctionsOnASingleLine: true AllowShortLambdasOnASingleLine: All +AlwaysBreakTemplateDeclarations: MultiLine BinPackParameters: false BraceWrapping: AfterFunction: true diff --git a/core/src/cats/cats.h b/core/src/cats/cats.h index d1b0d45aea9..943354ed2be 100644 --- a/core/src/cats/cats.h +++ b/core/src/cats/cats.h @@ -42,8 +42,7 @@ #include #include -template -class dlist; +template class dlist; /* import automatically generated SQL_QUERY */ #include "bdb_query_enum_class.h" diff --git a/core/src/cats/cats_backends.cc b/core/src/cats/cats_backends.cc index 243cd2a695e..96e19f4d3fd 100644 --- a/core/src/cats/cats_backends.cc +++ b/core/src/cats/cats_backends.cc @@ -295,5 +295,5 @@ BareosDb* db_init_database(JobControlRecord* jcr, void DbFlushBackends(void) {} # endif /* HAVE_DYNAMIC_CATS_BACKENDS */ -#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \ - HAVE_DBI */ +#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \ + HAVE_DBI */ diff --git a/core/src/console/console.cc b/core/src/console/console.cc index 2bb173cb90e..f2110a3cf33 100644 --- a/core/src/console/console.cc +++ b/core/src/console/console.cc @@ -1075,7 +1075,7 @@ int main(int argc, char* argv[]) TerminateConsole(0); return 1; #endif /* HAVE_PAM */ - } /* kMessageIdPamRequired */ + } /* kMessageIdPamRequired */ if (response_id == kMessageIdOk) { ConsoleOutput(response_args.JoinReadable().c_str()); diff --git a/core/src/dird/dir_plugins.h b/core/src/dird/dir_plugins.h index e78d769a644..e26f6b349d4 100644 --- a/core/src/dird/dir_plugins.h +++ b/core/src/dird/dir_plugins.h @@ -47,8 +47,7 @@ #include "include/bc_types.h" #include "lib/plugins.h" -template -class alist; +template class alist; namespace directordaemon { diff --git a/core/src/dird/dird.h b/core/src/dird/dird.h index aebfc994ca4..3370a3b7dc3 100644 --- a/core/src/dird/dird.h +++ b/core/src/dird/dird.h @@ -43,8 +43,7 @@ #include "include/jcr.h" #include "jobq.h" #include "ua.h" -template -class dlist; +template class dlist; namespace directordaemon { diff --git a/core/src/dird/dird_conf.h b/core/src/dird/dird_conf.h index 508338767e2..c607861f72d 100644 --- a/core/src/dird/dird_conf.h +++ b/core/src/dird/dird_conf.h @@ -35,8 +35,7 @@ #include "lib/resource_item.h" #include "lib/tls_conf.h" -template -class dlist; +template class dlist; struct json_t; class RunScript; class IPADDR; diff --git a/core/src/dird/jobq.h b/core/src/dird/jobq.h index e1923563231..cc76fcc86e5 100644 --- a/core/src/dird/jobq.h +++ b/core/src/dird/jobq.h @@ -36,8 +36,7 @@ #include "lib/dlink.h" #include "include/jcr.h" -template -class dlist; +template class dlist; namespace directordaemon { diff --git a/core/src/dird/migrate.cc b/core/src/dird/migrate.cc index 6c42f7e30c7..d760d2d6ddc 100644 --- a/core/src/dird/migrate.cc +++ b/core/src/dird/migrate.cc @@ -1155,7 +1155,7 @@ bool DoMigrationInit(JobControlRecord* jcr) */ SetJcrDefaults(mig_jcr, prev_job); - //Time value on this Job + // Time value on this Job mig_jcr->impl->no_maxtime = true; // Don't check for duplicates on migration and copy jobs diff --git a/core/src/dird/msgchan.h b/core/src/dird/msgchan.h index 642cb089d3b..048adb97752 100644 --- a/core/src/dird/msgchan.h +++ b/core/src/dird/msgchan.h @@ -22,8 +22,7 @@ #ifndef BAREOS_DIRD_MSGCHAN_H_ #define BAREOS_DIRD_MSGCHAN_H_ -template -class alist; +template class alist; namespace directordaemon { diff --git a/core/src/dird/ndmp_dma_storage.h b/core/src/dird/ndmp_dma_storage.h index 5b51cafdef7..e75af3746f4 100644 --- a/core/src/dird/ndmp_dma_storage.h +++ b/core/src/dird/ndmp_dma_storage.h @@ -23,8 +23,7 @@ #define BAREOS_DIRD_NDMP_DMA_STORAGE_H_ struct ndm_job_param; -template -class dlist; +template class dlist; struct vol_list_t; namespace directordaemon { diff --git a/core/src/dird/sd_cmds.h b/core/src/dird/sd_cmds.h index b81da26c9f5..b88cb850b8b 100644 --- a/core/src/dird/sd_cmds.h +++ b/core/src/dird/sd_cmds.h @@ -23,8 +23,7 @@ #define BAREOS_DIRD_SD_CMDS_H_ class JobControlRecord; -template -class dlist; +template class dlist; namespace directordaemon { diff --git a/core/src/dird/socket_server.h b/core/src/dird/socket_server.h index 1efccf4a11e..5ff358777de 100644 --- a/core/src/dird/socket_server.h +++ b/core/src/dird/socket_server.h @@ -22,8 +22,7 @@ #ifndef BAREOS_DIRD_SOCKET_SERVER_H_ #define BAREOS_DIRD_SOCKET_SERVER_H_ -template -class dlist; +template class dlist; class IPADDR; namespace directordaemon { diff --git a/core/src/dird/storage.h b/core/src/dird/storage.h index 115a8d6b2b5..bcd445e98d3 100644 --- a/core/src/dird/storage.h +++ b/core/src/dird/storage.h @@ -24,8 +24,7 @@ #include "ndmp/smc.h" -template -class alist; +template class alist; namespace directordaemon { diff --git a/core/src/dird/ua_select.h b/core/src/dird/ua_select.h index ce2be3e1272..4561be8bb56 100644 --- a/core/src/dird/ua_select.h +++ b/core/src/dird/ua_select.h @@ -23,8 +23,7 @@ #define BAREOS_DIRD_UA_SELECT_H_ #include "dird/ua.h" -template -class alist; +template class alist; struct PoolDbRecord; struct MediaDbRecord; diff --git a/core/src/filed/fd_plugins.h b/core/src/filed/fd_plugins.h index 0a8b348d2de..c330dd91dd1 100644 --- a/core/src/filed/fd_plugins.h +++ b/core/src/filed/fd_plugins.h @@ -64,8 +64,7 @@ struct BareosWinFilePacket; struct FindFilesPacket; #endif /* FILE_DAEMON */ -template -class alist; +template class alist; namespace filedaemon { diff --git a/core/src/filed/filed_conf.h b/core/src/filed/filed_conf.h index a7adfbbe080..fde40572e7d 100644 --- a/core/src/filed/filed_conf.h +++ b/core/src/filed/filed_conf.h @@ -31,10 +31,8 @@ #include "lib/messages_resource.h" #include "lib/tls_conf.h" -template -class alist; -template -class dlist; +template class alist; +template class dlist; class IPADDR; typedef struct X509_Keypair X509_KEYPAIR; diff --git a/core/src/filed/restore.h b/core/src/filed/restore.h index af8f633b126..df1e62b2e42 100644 --- a/core/src/filed/restore.h +++ b/core/src/filed/restore.h @@ -24,8 +24,7 @@ #define BAREOS_FILED_RESTORE_H_ #include "findlib/bfile.h" -template -class alist; +template class alist; namespace filedaemon { diff --git a/core/src/filed/socket_server.h b/core/src/filed/socket_server.h index 72031a87a62..66a1d3345ba 100644 --- a/core/src/filed/socket_server.h +++ b/core/src/filed/socket_server.h @@ -23,8 +23,7 @@ #ifndef BAREOS_FILED_SOCKET_SERVER_H_ #define BAREOS_FILED_SOCKET_SERVER_H_ -template -class dlist; +template class dlist; class IPADDR; namespace filedaemon { diff --git a/core/src/findlib/xattr.cc b/core/src/findlib/xattr.cc index 4d6cf40fe67..685ac251cef 100644 --- a/core/src/findlib/xattr.cc +++ b/core/src/findlib/xattr.cc @@ -2411,7 +2411,7 @@ static BxattrExitCode solaris_save_xattr_acl(JobControlRecord* jcr, *acl_text = NULL; } retval = BxattrExitCode::kSuccess; -# else /* HAVE_EXTENDED_ACL */ +# else /* HAVE_EXTENDED_ACL */ int n; aclent_t* acls = NULL; @@ -2466,9 +2466,9 @@ static BxattrExitCode solaris_save_xattr_acl(JobControlRecord* jcr, *acl_text = NULL; } retval = BxattrExitCode::kSuccess; -# endif /* HAVE_EXTENDED_ACL */ +# endif /* HAVE_EXTENDED_ACL */ -# else /* HAVE_ACL */ +# else /* HAVE_ACL */ retval = BxattrExitCode::kSuccess; # endif /* HAVE_ACL */ @@ -3015,7 +3015,7 @@ static BxattrExitCode solaris_restore_xattr_acl(JobControlRecord* jcr, # endif /* HAVE_EXTENDED_ACL */ } -# endif /* HAVE_ACL */ +# endif /* HAVE_ACL */ static BxattrExitCode solaris_restore_xattrs(JobControlRecord* jcr, XattrData* xattr_data, diff --git a/core/src/findlib/xattr.h b/core/src/findlib/xattr.h index 80cebaac04a..e3cc9081119 100644 --- a/core/src/findlib/xattr.h +++ b/core/src/findlib/xattr.h @@ -94,8 +94,7 @@ struct XattrData { // Upperlimit on a xattr internal buffer #define XATTR_BUFSIZ 1024 -template -class alist; +template class alist; BxattrExitCode SendXattrStream(JobControlRecord* jcr, XattrData* xattr_data, diff --git a/core/src/include/jcr.h b/core/src/include/jcr.h index 77e04b70c2a..fae31cbdd42 100644 --- a/core/src/include/jcr.h +++ b/core/src/include/jcr.h @@ -50,8 +50,7 @@ struct job_callback_item; class BareosDb; class BareosSocket; -template -class dlist; +template class dlist; class htable; class JobControlRecord; diff --git a/core/src/lib/alist.h b/core/src/lib/alist.h index a5d3eac72d3..e8ea91bdc64 100644 --- a/core/src/lib/alist.h +++ b/core/src/lib/alist.h @@ -94,8 +94,7 @@ enum * array of pointers to inserted items */ -template -class alist { +template class alist { T* items = nullptr; int num_items = 0; int max_items = 0; @@ -132,15 +131,13 @@ class alist { }; // Define index operator [] -template -inline T alist::operator[](int index) const +template inline T alist::operator[](int index) const { if (index < 0 || index >= num_items) { return nullptr; } return items[index]; } -template -inline bool alist::empty() const +template inline bool alist::empty() const { return num_items == 0; } @@ -149,8 +146,7 @@ inline bool alist::empty() const * allowing us to mix C++ classes inside malloc'ed * C structures. Define before called in constructor. */ -template -inline void alist::init(int num, bool own) +template inline void alist::init(int num, bool own) { items = nullptr; num_items = 0; @@ -160,20 +156,11 @@ inline void alist::init(int num, bool own) cur_item = 0; } -template -inline alist::alist(int num, bool own) -{ - init(num, own); -} +template inline alist::alist(int num, bool own) { init(num, own); } -template -inline alist::~alist() -{ - destroy(); -} +template inline alist::~alist() { destroy(); } -template -inline int alist::size() const +template inline int alist::size() const { /* Check for null pointer, which allows test * on size to succeed even if nothing put in @@ -183,17 +170,12 @@ inline int alist::size() const } /* How much to grow by each time */ -template -inline void alist::grow(int num) -{ - num_grow = num; -} +template inline void alist::grow(int num) { num_grow = num; } /* Private grow list function. Used to insure that * at least one more "slot" is available. */ -template -void alist::GrowList() +template void alist::GrowList() { if (items == NULL) { if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } @@ -205,8 +187,7 @@ void alist::GrowList() } } -template -T alist::first() +template T alist::first() { cur_item = 1; if (num_items == 0) { @@ -216,8 +197,7 @@ T alist::first() } } -template -T alist::last() +template T alist::last() { if (num_items == 0) { return NULL; @@ -227,8 +207,7 @@ T alist::last() } } -template -T alist::next() +template T alist::next() { if (cur_item >= num_items) { return NULL; @@ -237,8 +216,7 @@ T alist::next() } } -template -T alist::prev() +template T alist::prev() { if (cur_item <= 1) { return NULL; @@ -248,8 +226,7 @@ T alist::prev() } // prepend an item to the list -- i.e. add to beginning -template -void alist::prepend(T item) +template void alist::prepend(T item) { GrowList(); if (num_items == 0) { @@ -263,8 +240,7 @@ void alist::prepend(T item) // Append an item to the list -template -void alist::append(T item) +template void alist::append(T item) { GrowList(); items[num_items++] = item; @@ -284,16 +260,14 @@ T alist::remove(int index) /* Get the index item -- we should probably allow real indexing here */ -template -T alist::get(int index) +template T alist::get(int index) { if (index < 0 || index >= num_items) { return NULL; } return items[index]; } /* Destroy the list and its contents */ -template -void alist::destroy() +template void alist::destroy() { if (items) { if (own_items) { diff --git a/core/src/lib/bnet.h b/core/src/lib/bnet.h index 877af7d981d..b2a5451484d 100644 --- a/core/src/lib/bnet.h +++ b/core/src/lib/bnet.h @@ -23,8 +23,7 @@ #include "tls.h" #include "lib/address_conf.h" -template -class dlist; +template class dlist; int32_t BnetRecv(BareosSocket* bsock); bool BnetSend(BareosSocket* bsock); diff --git a/core/src/lib/bnet_server_tcp.h b/core/src/lib/bnet_server_tcp.h index e9560e89381..a1bc0b989dc 100644 --- a/core/src/lib/bnet_server_tcp.h +++ b/core/src/lib/bnet_server_tcp.h @@ -37,8 +37,7 @@ enum class BnetServerState kEnded }; -template -class dlist; +template class dlist; struct s_sockfd { int fd; diff --git a/core/src/lib/breg.h b/core/src/lib/breg.h index 27e5660b39d..0c2e6a33ae7 100644 --- a/core/src/lib/breg.h +++ b/core/src/lib/breg.h @@ -76,8 +76,7 @@ class BareosRegex { /* create new BareosRegex and compile regex_t */ BareosRegex* NewBregexp(const char* motif); -template -class alist; +template class alist; /* launch each bregexp on filename */ int RunBregexp(alist* bregexps, const char* fname); diff --git a/core/src/lib/bsock.h b/core/src/lib/bsock.h index 6992d541058..859c5798365 100644 --- a/core/src/lib/bsock.h +++ b/core/src/lib/bsock.h @@ -52,8 +52,7 @@ class BareosSocket; class Tls; class BStringList; class QualifiedResourceNameTypeConverter; -template -class dlist; +template class dlist; btimer_t* StartBsockTimer(BareosSocket* bs, uint32_t wait); void StopBsockTimer(btimer_t* wid); @@ -317,7 +316,7 @@ enum BNET_TEXT_INPUT = -28 /* Get text input from user */ }; -#define BNET_SETBUF_READ 1 /* Arg for BnetSetBufferSize */ +#define BNET_SETBUF_READ 1 /* Arg for BnetSetBufferSize */ #define BNET_SETBUF_WRITE 2 /* Arg for BnetSetBufferSize */ /** diff --git a/core/src/lib/connection_pool.h b/core/src/lib/connection_pool.h index 6a368d04745..f1d98b468ae 100644 --- a/core/src/lib/connection_pool.h +++ b/core/src/lib/connection_pool.h @@ -28,8 +28,7 @@ #ifndef BAREOS_LIB_CONNECTION_POOL_H_ #define BAREOS_LIB_CONNECTION_POOL_H_ -template -class alist; +template class alist; class BareosSocket; class Connection { diff --git a/core/src/lib/crypto.h b/core/src/lib/crypto.h index ef085c0f9da..be8222c9124 100644 --- a/core/src/lib/crypto.h +++ b/core/src/lib/crypto.h @@ -28,8 +28,7 @@ #ifndef BAREOS_LIB_CRYPTO_H_ #define BAREOS_LIB_CRYPTO_H_ -template -class alist; +template class alist; /* Opaque X509 Public/Private Key Pair Structure */ typedef struct X509_Keypair X509_KEYPAIR; @@ -103,8 +102,8 @@ typedef enum } crypto_error_t; /* Message Digest Sizes */ -#define CRYPTO_DIGEST_MD5_SIZE 16 /* 128 bits */ -#define CRYPTO_DIGEST_SHA1_SIZE 20 /* 160 bits */ +#define CRYPTO_DIGEST_MD5_SIZE 16 /* 128 bits */ +#define CRYPTO_DIGEST_SHA1_SIZE 20 /* 160 bits */ #define CRYPTO_DIGEST_SHA256_SIZE 32 /* 256 bits */ #define CRYPTO_DIGEST_SHA512_SIZE 64 /* 512 bits */ diff --git a/core/src/lib/crypto_openssl.cc b/core/src/lib/crypto_openssl.cc index ee5bd4a9541..21e04657f05 100644 --- a/core/src/lib/crypto_openssl.cc +++ b/core/src/lib/crypto_openssl.cc @@ -1101,7 +1101,7 @@ CRYPTO_SESSION* crypto_session_new(crypto_cipher_t cipher, break; # endif # endif /* OPENSSL_NO_CAMELLIA */ -# endif /* HAVE_OPENSSL_EXPORT_LIBRARY */ +# endif /* HAVE_OPENSSL_EXPORT_LIBRARY */ # if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1) # ifdef NID_aes_128_cbc_hmac_sha1 case CRYPTO_CIPHER_AES_128_CBC_HMAC_SHA1: diff --git a/core/src/lib/dlink.h b/core/src/lib/dlink.h index 185dd475acb..86e8cec2a85 100644 --- a/core/src/lib/dlink.h +++ b/core/src/lib/dlink.h @@ -23,8 +23,7 @@ #ifndef BAREOS_LIB_DLINK_H_ #define BAREOS_LIB_DLINK_H_ -template -struct dlink { +template struct dlink { T* next; T* prev; dlink() diff --git a/core/src/lib/dlist.cc b/core/src/lib/dlist.cc index 5abf582c794..9601e18d1cb 100644 --- a/core/src/lib/dlist.cc +++ b/core/src/lib/dlist.cc @@ -43,4 +43,3 @@ dlistString* new_dlistString(const char* str, int len) bstrncpy(node->c_str(), str, len + 1); return node; } - diff --git a/core/src/lib/dlist.h b/core/src/lib/dlist.h index b856039c83b..c6ba3baa7ca 100644 --- a/core/src/lib/dlist.h +++ b/core/src/lib/dlist.h @@ -58,8 +58,7 @@ != nullptr;) #endif -template -class dlist { +template class dlist { T* head; T* tail; int16_t loffset; @@ -93,8 +92,7 @@ class dlist { T* last() const; }; -template -inline void dlist::init() +template inline void dlist::init() { head = tail = nullptr; loffset = 0; @@ -110,8 +108,7 @@ inline void dlist::init() * then there is no need to specify the link address * since the offset is zero. */ -template -inline dlist::dlist(T* item, dlink* link) +template inline dlist::dlist(T* item, dlink* link) { init(item, link); } @@ -124,62 +121,44 @@ inline dlist::dlist(void) return; } -template -inline void dlist::SetPrev(T* item, T* prev) +template inline void dlist::SetPrev(T* item, T* prev) { ((dlink*)(((char*)item) + loffset))->prev = prev; } -template -inline void dlist::SetNext(T* item, T* next) +template inline void dlist::SetNext(T* item, T* next) { ((dlink*)(((char*)item) + loffset))->next = next; } -template -inline T* dlist::get_prev(T* item) +template inline T* dlist::get_prev(T* item) { return ((dlink*)(((char*)item) + loffset))->prev; } -template -inline T* dlist::get_next(T* item) +template inline T* dlist::get_next(T* item) { return ((dlink*)(((char*)item) + loffset))->next; } -template -inline dlink* dlist::get_link(T* item) +template inline dlink* dlist::get_link(T* item) { return (dlink*)(((char*)item) + loffset); } -template -inline bool dlist::empty() const +template inline bool dlist::empty() const { return head == nullptr; } -template -inline int dlist::size() const -{ - return num_items; -} +template inline int dlist::size() const { return num_items; } -template -inline T* dlist::first() const -{ - return head; -} +template inline T* dlist::first() const { return head; } -template -inline T* dlist::last() const -{ - return tail; -} +template inline T* dlist::last() const { return tail; } /** * C string helper routines for dlist @@ -212,8 +191,7 @@ extern dlistString* new_dlistString(const char* str); // Init dlist -template -void dlist::init(T* item, dlink* link) +template void dlist::init(T* item, dlink* link) { head = tail = NULL; loffset = (int)((char*)link - (char*)item); @@ -224,8 +202,7 @@ void dlist::init(T* item, dlink* link) } // Append an item to the list -template -void dlist::append(T* item) +template void dlist::append(T* item) { SetNext(item, NULL); SetPrev(item, tail); @@ -238,8 +215,7 @@ void dlist::append(T* item) } // Prepend an item to the list -template -void dlist::prepend(T* item) +template void dlist::prepend(T* item) { SetNext(item, head); SetPrev(item, NULL); @@ -251,8 +227,7 @@ void dlist::prepend(T* item) num_items++; } -template -void dlist::InsertBefore(T* item, T* where) +template void dlist::InsertBefore(T* item, T* where) { dlink* where_link = get_link(where); @@ -265,8 +240,7 @@ void dlist::InsertBefore(T* item, T* where) num_items++; } -template -void dlist::InsertAfter(T* item, T* where) +template void dlist::InsertAfter(T* item, T* where) { dlink* where_link = get_link(where); @@ -450,8 +424,7 @@ T* dlist::binary_search(T* item, int compare(T* item1, T* item2)) } -template -void dlist::remove(T* item) +template void dlist::remove(T* item) { T* xitem; dlink* ilink = get_link(item); /* item's link */ @@ -472,15 +445,13 @@ void dlist::remove(T* item) if (num_items == 0) { head = tail = NULL; } } -template -T* dlist::next(T* item) +template T* dlist::next(T* item) { if (item == NULL) { return head; } return get_next(item); } -template -T* dlist::prev(T* item) +template T* dlist::prev(T* item) { if (item == NULL) { return tail; } return get_prev(item); @@ -488,8 +459,7 @@ T* dlist::prev(T* item) /* Destroy the list contents */ -template -void dlist::destroy() +template void dlist::destroy() { for (T* n = head; n;) { T* ni = get_next(n); diff --git a/core/src/lib/guid_to_name.h b/core/src/lib/guid_to_name.h index 247c6676cf5..2ca89aa07ff 100644 --- a/core/src/lib/guid_to_name.h +++ b/core/src/lib/guid_to_name.h @@ -28,8 +28,7 @@ #ifndef BAREOS_LIB_GUID_TO_NAME_H_ #define BAREOS_LIB_GUID_TO_NAME_H_ -template -class dlist; +template class dlist; struct guitem; class guid_list { public: diff --git a/core/src/lib/ini.h b/core/src/lib/ini.h index 05b1d3271b8..b5b0322f7af 100644 --- a/core/src/lib/ini.h +++ b/core/src/lib/ini.h @@ -58,8 +58,7 @@ enum * On the Plugin side, it can get back the C structure and use it. * RestoreObject R2 -> parse() -> struct ini_items[] */ -template -class alist; +template class alist; class ConfigFile; struct ini_items; diff --git a/core/src/lib/plugins.h b/core/src/lib/plugins.h index 5c5418d36a6..bc9d666b989 100644 --- a/core/src/lib/plugins.h +++ b/core/src/lib/plugins.h @@ -92,8 +92,7 @@ typedef struct gen_pluginInfo { const char* plugin_usage; } PluginInformation; -template -class alist; +template class alist; /* Functions */ bool LoadPlugins(void* bareos_plugin_interface_version, diff --git a/core/src/lib/resource_item.h b/core/src/lib/resource_item.h index 5a2320dcb1c..0e0c18b327a 100644 --- a/core/src/lib/resource_item.h +++ b/core/src/lib/resource_item.h @@ -25,10 +25,8 @@ #define BAREOS_LIB_RESOURCE_ITEM_H_ struct s_password; -template -class alist; -template -class dlist; +template class alist; +template class dlist; /* * This is the structure that defines the record types (items) permitted within @@ -62,15 +60,13 @@ static inline void* CalculateAddressOfMemberVariable(const ResourceItem& item) return static_cast(base + item.offset); } -template -P GetItemVariable(const ResourceItem& item) +template P GetItemVariable(const ResourceItem& item) { void* p = CalculateAddressOfMemberVariable(item); return *(static_cast::type*>(p)); } -template -P GetItemVariablePointer(const ResourceItem& item) +template P GetItemVariablePointer(const ResourceItem& item) { void* p = CalculateAddressOfMemberVariable(item); return static_cast

(p); diff --git a/core/src/lib/runscript.h b/core/src/lib/runscript.h index 50d255cb8d2..6616e58ffac 100644 --- a/core/src/lib/runscript.h +++ b/core/src/lib/runscript.h @@ -30,8 +30,7 @@ #include "jcr.h" #include "lib/bareos_resource.h" -template -class alist; +template class alist; /* Usage: * diff --git a/core/src/stored/autochanger_resource.h b/core/src/stored/autochanger_resource.h index 4ac29118b61..6ad72309abe 100644 --- a/core/src/stored/autochanger_resource.h +++ b/core/src/stored/autochanger_resource.h @@ -26,8 +26,7 @@ #include "lib/bareos_resource.h" -template -class alist; +template class alist; namespace storagedaemon { class DeviceResource; diff --git a/core/src/stored/backends/chunked_device.h b/core/src/stored/backends/chunked_device.h index 8a1af4e1813..917b0543821 100644 --- a/core/src/stored/backends/chunked_device.h +++ b/core/src/stored/backends/chunked_device.h @@ -28,8 +28,7 @@ #ifndef BAREOS_STORED_BACKENDS_CHUNKED_DEVICE_H_ #define BAREOS_STORED_BACKENDS_CHUNKED_DEVICE_H_ -template -class alist; +template class alist; #include "ordered_cbuf.h" namespace storagedaemon { diff --git a/core/src/stored/backends/ordered_cbuf.h b/core/src/stored/backends/ordered_cbuf.h index da81456058b..7797d7b13d1 100644 --- a/core/src/stored/backends/ordered_cbuf.h +++ b/core/src/stored/backends/ordered_cbuf.h @@ -55,7 +55,7 @@ class ordered_circbuf { = PTHREAD_COND_INITIALIZER; /* Full -> not full condition */ pthread_cond_t notempty_ = PTHREAD_COND_INITIALIZER; /* Empty -> not empty condition */ - dlist* data_ = nullptr; /* Circular buffer of pointers */ + dlist* data_ = nullptr; /* Circular buffer of pointers */ public: ordered_circbuf(int capacity = OQSIZE); diff --git a/core/src/stored/dev.h b/core/src/stored/dev.h index 36c294e8edb..9db56954d7b 100644 --- a/core/src/stored/dev.h +++ b/core/src/stored/dev.h @@ -65,8 +65,7 @@ #include "stored/volume_catalog_info.h" #include -template -class dlist; +template class dlist; namespace storagedaemon { diff --git a/core/src/stored/jcr_private.h b/core/src/stored/jcr_private.h index 22d56bd0cda..66f2f2d3116 100644 --- a/core/src/stored/jcr_private.h +++ b/core/src/stored/jcr_private.h @@ -30,8 +30,7 @@ #define SD_APPEND 1 #define SD_READ 0 -template -class alist; +template class alist; namespace storagedaemon { diff --git a/core/src/stored/ndmp_tape.h b/core/src/stored/ndmp_tape.h index 4be309bdb10..a8db48109e7 100644 --- a/core/src/stored/ndmp_tape.h +++ b/core/src/stored/ndmp_tape.h @@ -21,8 +21,7 @@ #ifndef BAREOS_STORED_NDMP_TAPE_H_ #define BAREOS_STORED_NDMP_TAPE_H_ -template -class dlist; +template class dlist; namespace storagedaemon { diff --git a/core/src/stored/record.h b/core/src/stored/record.h index 430cc3043a5..7fc0d9b8786 100644 --- a/core/src/stored/record.h +++ b/core/src/stored/record.h @@ -31,8 +31,7 @@ #include "lib/dlist.h" -template -class dlist; +template class dlist; namespace storagedaemon { diff --git a/core/src/stored/reserve.h b/core/src/stored/reserve.h index bf885e495f3..35ae84e93a7 100644 --- a/core/src/stored/reserve.h +++ b/core/src/stored/reserve.h @@ -38,8 +38,7 @@ #ifndef BAREOS_STORED_RESERVE_H_ #define BAREOS_STORED_RESERVE_H_ -template -class alist; +template class alist; namespace storagedaemon { diff --git a/core/src/stored/sd_plugins.h b/core/src/stored/sd_plugins.h index fa00ed93751..aa5e8bbc0ae 100644 --- a/core/src/stored/sd_plugins.h +++ b/core/src/stored/sd_plugins.h @@ -44,8 +44,7 @@ #include "include/bc_types.h" #include "lib/plugins.h" -template -class alist; +template class alist; namespace storagedaemon { diff --git a/core/src/stored/socket_server.h b/core/src/stored/socket_server.h index e753d828289..8e4cebe94ec 100644 --- a/core/src/stored/socket_server.h +++ b/core/src/stored/socket_server.h @@ -21,8 +21,7 @@ #ifndef BAREOS_STORED_SOCKET_SERVER_H_ #define BAREOS_STORED_SOCKET_SERVER_H_ -template -class dlist; +template class dlist; class ConfigurationParser; class IPADDR; diff --git a/core/src/stored/stored_conf.h b/core/src/stored/stored_conf.h index 81f917a9385..f45848e39e2 100644 --- a/core/src/stored/stored_conf.h +++ b/core/src/stored/stored_conf.h @@ -34,10 +34,8 @@ #include "lib/messages_resource.h" #include "lib/tls_conf.h" -template -class alist; -template -class dlist; +template class alist; +template class dlist; class IPADDR; diff --git a/core/src/stored/vol_mgr.h b/core/src/stored/vol_mgr.h index 9adb08d982a..4c7f28b526e 100644 --- a/core/src/stored/vol_mgr.h +++ b/core/src/stored/vol_mgr.h @@ -46,8 +46,7 @@ #ifndef BAREOS_STORED_VOL_MGR_H_ #define BAREOS_STORED_VOL_MGR_H_ -template -class dlist; +template class dlist; namespace storagedaemon { diff --git a/core/src/tests/dlist_test.cc b/core/src/tests/dlist_test.cc index f7a58db49b1..b9736378cec 100644 --- a/core/src/tests/dlist_test.cc +++ b/core/src/tests/dlist_test.cc @@ -53,8 +53,7 @@ struct ListItem { dlink link; char* buf; }; -template -static int MyCompare(T* item1, T* item2) +template static int MyCompare(T* item1, T* item2) { int comp; comp = strcmp(item1->buf, item2->buf); From 9c8d22bcba51ffd2d6380fba0c0f2f63d76a7046 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Mon, 10 May 2021 12:12:27 +0200 Subject: [PATCH 4/6] replace keyword class with typename in templates The C++ standard says: There is no semantic difference between class and typename in a type-parameter-key. We change most uses of class to typename, so our declarations (especially forward-declarations) look uniform. --- core/src/dird/dird_conf.cc | 2 +- core/src/dird/msgchan.h | 2 +- core/src/dird/storage.h | 2 +- core/src/dird/ua_select.h | 2 +- core/src/filed/filed_conf.h | 4 ++-- core/src/filed/restore.h | 2 +- core/src/findlib/xattr.h | 2 +- core/src/lib/alist.h | 39 +++++++++++++++++++---------------- core/src/lib/crypto.h | 2 +- core/src/lib/ini.h | 2 +- core/src/stored/jcr_private.h | 2 +- 11 files changed, 32 insertions(+), 29 deletions(-) diff --git a/core/src/dird/dird_conf.cc b/core/src/dird/dird_conf.cc index 8574f6d1590..94fca9a02a1 100644 --- a/core/src/dird/dird_conf.cc +++ b/core/src/dird/dird_conf.cc @@ -3317,7 +3317,7 @@ static bool HasDefaultValue(ResourceItem& item, s_jl* keywords) return is_default; } -template +template static bool HasDefaultValue(ResourceItem& item, alist* values) { if (item.flags & CFG_ITEM_DEFAULT) { diff --git a/core/src/dird/msgchan.h b/core/src/dird/msgchan.h index 048adb97752..e637ee849de 100644 --- a/core/src/dird/msgchan.h +++ b/core/src/dird/msgchan.h @@ -22,7 +22,7 @@ #ifndef BAREOS_DIRD_MSGCHAN_H_ #define BAREOS_DIRD_MSGCHAN_H_ -template class alist; +template class alist; namespace directordaemon { diff --git a/core/src/dird/storage.h b/core/src/dird/storage.h index bcd445e98d3..3212a167212 100644 --- a/core/src/dird/storage.h +++ b/core/src/dird/storage.h @@ -24,7 +24,7 @@ #include "ndmp/smc.h" -template class alist; +template class alist; namespace directordaemon { diff --git a/core/src/dird/ua_select.h b/core/src/dird/ua_select.h index 4561be8bb56..b903917b0b9 100644 --- a/core/src/dird/ua_select.h +++ b/core/src/dird/ua_select.h @@ -23,7 +23,7 @@ #define BAREOS_DIRD_UA_SELECT_H_ #include "dird/ua.h" -template class alist; +template class alist; struct PoolDbRecord; struct MediaDbRecord; diff --git a/core/src/filed/filed_conf.h b/core/src/filed/filed_conf.h index fde40572e7d..b5b8a6c5c67 100644 --- a/core/src/filed/filed_conf.h +++ b/core/src/filed/filed_conf.h @@ -31,8 +31,8 @@ #include "lib/messages_resource.h" #include "lib/tls_conf.h" -template class alist; -template class dlist; +template class alist; +template class dlist; class IPADDR; typedef struct X509_Keypair X509_KEYPAIR; diff --git a/core/src/filed/restore.h b/core/src/filed/restore.h index df1e62b2e42..51d5f747cc1 100644 --- a/core/src/filed/restore.h +++ b/core/src/filed/restore.h @@ -24,7 +24,7 @@ #define BAREOS_FILED_RESTORE_H_ #include "findlib/bfile.h" -template class alist; +template class alist; namespace filedaemon { diff --git a/core/src/findlib/xattr.h b/core/src/findlib/xattr.h index e3cc9081119..2fe32e30443 100644 --- a/core/src/findlib/xattr.h +++ b/core/src/findlib/xattr.h @@ -94,7 +94,7 @@ struct XattrData { // Upperlimit on a xattr internal buffer #define XATTR_BUFSIZ 1024 -template class alist; +template class alist; BxattrExitCode SendXattrStream(JobControlRecord* jcr, XattrData* xattr_data, diff --git a/core/src/lib/alist.h b/core/src/lib/alist.h index e8ea91bdc64..2f08718ab25 100644 --- a/core/src/lib/alist.h +++ b/core/src/lib/alist.h @@ -94,7 +94,7 @@ enum * array of pointers to inserted items */ -template class alist { +template class alist { T* items = nullptr; int num_items = 0; int max_items = 0; @@ -131,13 +131,13 @@ template class alist { }; // Define index operator [] -template inline T alist::operator[](int index) const +template inline T alist::operator[](int index) const { if (index < 0 || index >= num_items) { return nullptr; } return items[index]; } -template inline bool alist::empty() const +template inline bool alist::empty() const { return num_items == 0; } @@ -146,7 +146,7 @@ template inline bool alist::empty() const * allowing us to mix C++ classes inside malloc'ed * C structures. Define before called in constructor. */ -template inline void alist::init(int num, bool own) +template inline void alist::init(int num, bool own) { items = nullptr; num_items = 0; @@ -156,11 +156,14 @@ template inline void alist::init(int num, bool own) cur_item = 0; } -template inline alist::alist(int num, bool own) { init(num, own); } +template inline alist::alist(int num, bool own) +{ + init(num, own); +} -template inline alist::~alist() { destroy(); } +template inline alist::~alist() { destroy(); } -template inline int alist::size() const +template inline int alist::size() const { /* Check for null pointer, which allows test * on size to succeed even if nothing put in @@ -170,12 +173,12 @@ template inline int alist::size() const } /* How much to grow by each time */ -template inline void alist::grow(int num) { num_grow = num; } +template inline void alist::grow(int num) { num_grow = num; } /* Private grow list function. Used to insure that * at least one more "slot" is available. */ -template void alist::GrowList() +template void alist::GrowList() { if (items == NULL) { if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } @@ -187,7 +190,7 @@ template void alist::GrowList() } } -template T alist::first() +template T alist::first() { cur_item = 1; if (num_items == 0) { @@ -197,7 +200,7 @@ template T alist::first() } } -template T alist::last() +template T alist::last() { if (num_items == 0) { return NULL; @@ -207,7 +210,7 @@ template T alist::last() } } -template T alist::next() +template T alist::next() { if (cur_item >= num_items) { return NULL; @@ -216,7 +219,7 @@ template T alist::next() } } -template T alist::prev() +template T alist::prev() { if (cur_item <= 1) { return NULL; @@ -226,7 +229,7 @@ template T alist::prev() } // prepend an item to the list -- i.e. add to beginning -template void alist::prepend(T item) +template void alist::prepend(T item) { GrowList(); if (num_items == 0) { @@ -240,13 +243,13 @@ template void alist::prepend(T item) // Append an item to the list -template void alist::append(T item) +template void alist::append(T item) { GrowList(); items[num_items++] = item; } -template +template /* Remove an item from the list */ T alist::remove(int index) { @@ -260,14 +263,14 @@ T alist::remove(int index) /* Get the index item -- we should probably allow real indexing here */ -template T alist::get(int index) +template T alist::get(int index) { if (index < 0 || index >= num_items) { return NULL; } return items[index]; } /* Destroy the list and its contents */ -template void alist::destroy() +template void alist::destroy() { if (items) { if (own_items) { diff --git a/core/src/lib/crypto.h b/core/src/lib/crypto.h index be8222c9124..8bd070784f8 100644 --- a/core/src/lib/crypto.h +++ b/core/src/lib/crypto.h @@ -28,7 +28,7 @@ #ifndef BAREOS_LIB_CRYPTO_H_ #define BAREOS_LIB_CRYPTO_H_ -template class alist; +template class alist; /* Opaque X509 Public/Private Key Pair Structure */ typedef struct X509_Keypair X509_KEYPAIR; diff --git a/core/src/lib/ini.h b/core/src/lib/ini.h index b5b0322f7af..a1b461e2504 100644 --- a/core/src/lib/ini.h +++ b/core/src/lib/ini.h @@ -58,7 +58,7 @@ enum * On the Plugin side, it can get back the C structure and use it. * RestoreObject R2 -> parse() -> struct ini_items[] */ -template class alist; +template class alist; class ConfigFile; struct ini_items; diff --git a/core/src/stored/jcr_private.h b/core/src/stored/jcr_private.h index 66f2f2d3116..485ad327335 100644 --- a/core/src/stored/jcr_private.h +++ b/core/src/stored/jcr_private.h @@ -30,7 +30,7 @@ #define SD_APPEND 1 #define SD_READ 0 -template class alist; +template class alist; namespace storagedaemon { From 3681a50c0e6caee9888571b3a08793947859c58d Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Mon, 10 May 2021 16:35:33 +0200 Subject: [PATCH 5/6] lib: refactor dlist.h to inline all methods previously some methods were not inlined. However, this is required to not violate ODR. To improve readability all methods have been moved into the class declaration itself which will imply the class templating and inlining. --- core/src/lib/dlist.h | 656 +++++++++++++++++++------------------------ 1 file changed, 290 insertions(+), 366 deletions(-) diff --git a/core/src/lib/dlist.h b/core/src/lib/dlist.h index c6ba3baa7ca..6022fcbf8a9 100644 --- a/core/src/lib/dlist.h +++ b/core/src/lib/dlist.h @@ -34,8 +34,7 @@ #include "include/bareos.h" #include "lib/dlink.h" #include "lib/message.h" - -#define M_ABORT 1 +#include "lib/message_severity.h" /* In case you want to specifically specify the offset to the link */ #define OFFSET(item, link) (int)((char*)(link) - (char*)(item)) @@ -65,409 +64,334 @@ template class dlist { uint32_t num_items; public: - dlist(T* item, dlink* link); - dlist(void); - ~dlist() { destroy(); } - void init(T* item, dlink* link); - void init(); - void prepend(T* item); - void append(T* item); - void SetPrev(T* item, T* prev); - void SetNext(T* item, T* next); - T* get_prev(T* item); - T* get_next(T* item); - dlink* get_link(T* item); - void InsertBefore(T* item, T* where); - void InsertAfter(T* item, T* where); - T* binary_insert(T* item, int compare(T* item1, T* item2)); - T* binary_search(T* item, int compare(T* item1, T* item2)); - void BinaryInsertMultiple(T* item, int compare(T* item1, T* item2)); - void remove(T* item); - bool empty() const; - int size() const; - T* next(T* item); - T* prev(T* item); - void destroy(); - T* first() const; - T* last() const; -}; - -template inline void dlist::init() -{ - head = tail = nullptr; - loffset = 0; - num_items = 0; -} - - -/** - * Constructor called with the address of a - * member of the list (not the list head), and - * the address of the link within that member. - * If the link is at the beginning of the list member, - * then there is no need to specify the link address - * since the offset is zero. - */ -template inline dlist::dlist(T* item, dlink* link) -{ - init(item, link); -} - -/* Constructor with link at head of item */ -template -inline dlist::dlist(void) - : head(nullptr), tail(nullptr), loffset(0), num_items(0) -{ - return; -} - -template inline void dlist::SetPrev(T* item, T* prev) -{ - ((dlink*)(((char*)item) + loffset))->prev = prev; -} - -template inline void dlist::SetNext(T* item, T* next) -{ - ((dlink*)(((char*)item) + loffset))->next = next; -} - -template inline T* dlist::get_prev(T* item) -{ - return ((dlink*)(((char*)item) + loffset))->prev; -} - -template inline T* dlist::get_next(T* item) -{ - return ((dlink*)(((char*)item) + loffset))->next; -} - - -template inline dlink* dlist::get_link(T* item) -{ - return (dlink*)(((char*)item) + loffset); -} - - -template inline bool dlist::empty() const -{ - return head == nullptr; -} - -template inline int dlist::size() const { return num_items; } - - -template inline T* dlist::first() const { return head; } - -template inline T* dlist::last() const { return tail; } - -/** - * C string helper routines for dlist - * The string (char *) is kept in the node - * - * Kern Sibbald, February 2007 - * - */ -class dlistString { - public: - char* c_str() { return str_; } - - private: -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wunused-private-field" -#endif - dlink link_; -#ifdef __clang__ -# pragma clang diagnostic pop -#endif - char str_[1]; - /* !!! Don't put anything after this as this space is used - * to hold the string in inline + /** + * Constructor called with the address of a + * member of the list (not the list head), and + * the address of the link within that member. + * If the link is at the beginning of the list member, + * then there is no need to specify the link address + * since the offset is zero. */ -}; - -extern dlistString* new_dlistString(const char* str, int len); -extern dlistString* new_dlistString(const char* str); + dlist(T* item, dlink* link) { init(item, link); } - -// Init dlist -template void dlist::init(T* item, dlink* link) -{ - head = tail = NULL; - loffset = (int)((char*)link - (char*)item); - if (loffset < 0 || loffset > 5000) { - Emsg0(M_ABORT, 0, "Improper dlist initialization.\n"); + /* Constructor with link at head of item */ + dlist(void) : head(nullptr), tail(nullptr), loffset(0), num_items(0) {} + ~dlist() { destroy(); } + void init(T* item, dlink* link) + { + head = tail = NULL; + loffset = (int)((char*)link - (char*)item); + if (loffset < 0 || loffset > 5000) { + Emsg0(M_ABORT, 0, "Improper dlist initialization.\n"); + } + num_items = 0; } - num_items = 0; -} - -// Append an item to the list -template void dlist::append(T* item) -{ - SetNext(item, NULL); - SetPrev(item, tail); - if (tail) { SetNext(tail, item); } - tail = item; - if (head == NULL) { /* if empty list, */ - head = item; /* item is head as well */ + void init() + { + head = tail = nullptr; + loffset = 0; + num_items = 0; + } + void prepend(T* item) + { + SetNext(item, head); + SetPrev(item, NULL); + if (head) { SetPrev(head, item); } + head = item; + if (tail == NULL) { /* if empty list, */ + tail = item; /* item is tail too */ + } + num_items++; + } + void append(T* item) + { + SetNext(item, NULL); + SetPrev(item, tail); + if (tail) { SetNext(tail, item); } + tail = item; + if (head == NULL) { /* if empty list, */ + head = item; /* item is head as well */ + } + num_items++; } - num_items++; -} -// Prepend an item to the list -template void dlist::prepend(T* item) -{ - SetNext(item, head); - SetPrev(item, NULL); - if (head) { SetPrev(head, item); } - head = item; - if (tail == NULL) { /* if empty list, */ - tail = item; /* item is tail too */ + void SetPrev(T* item, T* prev) + { + ((dlink*)(((char*)item) + loffset))->prev = prev; } - num_items++; -} -template void dlist::InsertBefore(T* item, T* where) -{ - dlink* where_link = get_link(where); + void SetNext(T* item, T* next) + { + ((dlink*)(((char*)item) + loffset))->next = next; + } - SetNext(item, where); - SetPrev(item, where_link->prev); + T* get_prev(T* item) { return ((dlink*)(((char*)item) + loffset))->prev; } - if (where_link->prev) { SetNext(where_link->prev, item); } - where_link->prev = item; - if (head == where) { head = item; } - num_items++; -} + T* get_next(T* item) { return ((dlink*)(((char*)item) + loffset))->next; } + dlink* get_link(T* item) { return (dlink*)(((char*)item) + loffset); } + void InsertBefore(T* item, T* where) + { + dlink* where_link = get_link(where); -template void dlist::InsertAfter(T* item, T* where) -{ - dlink* where_link = get_link(where); + SetNext(item, where); + SetPrev(item, where_link->prev); - SetNext(item, where_link->next); - SetPrev(item, where); + if (where_link->prev) { SetNext(where_link->prev, item); } + where_link->prev = item; + if (head == where) { head = item; } + num_items++; + } - if (where_link->next) { SetPrev(where_link->next, item); } - where_link->next = item; - if (tail == where) { tail = item; } - num_items++; -} + void InsertAfter(T* item, T* where) + { + dlink* where_link = get_link(where); -/* - * Insert an item in the list, but only if it is unique - * otherwise, the item is returned non inserted - * - * Returns: item if item inserted - * other_item if same value already exists (item not inserted) - */ -template -T* dlist::binary_insert(T* item, int compare(T* item1, T* item2)) -{ - int comp; - int low, high, cur; - T* cur_item; + SetNext(item, where_link->next); + SetPrev(item, where); - if (num_items == 0) { - // Dmsg0(000, "Append first.\n"); - append(item); - return item; + if (where_link->next) { SetPrev(where_link->next, item); } + where_link->next = item; + if (tail == where) { tail = item; } + num_items++; } - if (num_items == 1) { + /* + * Insert an item in the list, but only if it is unique + * otherwise, the item is returned non inserted + * + * Returns: item if item inserted + * other_item if same value already exists (item not inserted) + */ + T* binary_insert(T* item, int compare(T* item1, T* item2)) + { + int comp; + int low, high, cur; + T* cur_item; + + if (num_items == 0) { + // Dmsg0(000, "Append first.\n"); + append(item); + return item; + } + if (num_items == 1) { + comp = compare(item, first()); + if (comp < 0) { + prepend(item); + // Dmsg0(000, "Insert before first.\n"); + return item; + } else if (comp > 0) { + InsertAfter(item, first()); + // Dmsg0(000, "Insert after first.\n"); + return item; + } else { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + } + /* Check against last item */ + comp = compare(item, last()); + if (comp > 0) { + append(item); + // Dmsg0(000, "Appended item.\n"); + return item; + } else if (comp == 0) { + // Dmsg0(000, "Same as last.\n"); + return last(); + } + /* Check against first item */ comp = compare(item, first()); if (comp < 0) { prepend(item); - // Dmsg0(000, "Insert before first.\n"); + // Dmsg0(000, "Inserted item before.\n"); return item; - } else if (comp > 0) { + } else if (comp == 0) { + // Dmsg0(000, "Same as first.\n"); + return first(); + } + if (num_items == 2) { InsertAfter(item, first()); - // Dmsg0(000, "Insert after first.\n"); + // Dmsg0(000, "Inserted item after.\n"); return item; + } + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + // Dmsg1(000, "Compare item to %d\n", cur); + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + // Dmsg1(000, "Same as item %d\n", cur); + return cur_item; + } + } + if (high == cur) { + InsertBefore(item, cur_item); + // Dmsg1(000, "Insert before item %d\n", cur); } else { - // Dmsg0(000, "Same as first.\n"); - return first(); + InsertAfter(item, cur_item); + // Dmsg1(000, "Insert after item %d\n", cur); } - } - /* Check against last item */ - comp = compare(item, last()); - if (comp > 0) { - append(item); - // Dmsg0(000, "Appended item.\n"); - return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as last.\n"); - return last(); - } - /* Check against first item */ - comp = compare(item, first()); - if (comp < 0) { - prepend(item); - // Dmsg0(000, "Inserted item before.\n"); return item; - } else if (comp == 0) { - // Dmsg0(000, "Same as first.\n"); - return first(); } - if (num_items == 2) { - InsertAfter(item, first()); - // Dmsg0(000, "Inserted item after.\n"); - return item; - } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - while (nxt > cur) { - cur_item = next(cur_item); - cur++; + T* binary_search(T* item, int compare(T* item1, T* item2)) + { + int comp; + int low, high, cur; + T* cur_item; + + + if (num_items == 0) { return NULL; } + cur_item = first(); + if (num_items == 1) { + comp = compare(item, cur_item); + if (comp == 0) { + return cur_item; + } else { + return NULL; + } } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; + low = 1; + high = num_items; + cur = 1; + cur_item = first(); + while (low < high) { + int nxt; + nxt = (low + high) / 2; + /* Now get cur pointing to nxt */ + while (nxt > cur) { + cur_item = next(cur_item); + cur++; + } + while (nxt < cur) { + cur_item = prev(cur_item); + cur--; + } + comp = compare(item, cur_item); + // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); + if (comp < 0) { + high = cur; + // Dmsg2(000, "set high; low=%d high=%d\n", low, high); + } else if (comp > 0) { + low = cur + 1; + // Dmsg2(000, "set low; low=%d high=%d\n", low, high); + } else { + return cur_item; + } } - // Dmsg1(000, "Compare item to %d\n", cur); - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - // Dmsg1(000, "Same as item %d\n", cur); - return cur_item; + /* + * low == high can only happen if low just + * got incremented from cur, and we have + * not yet tested cur+1 + */ + if (low == high) { + cur_item = next(cur_item); + comp = compare(item, cur_item); + if (comp == 0) { return cur_item; } } + return NULL; } - if (high == cur) { - InsertBefore(item, cur_item); - // Dmsg1(000, "Insert before item %d\n", cur); - } else { - InsertAfter(item, cur_item); - // Dmsg1(000, "Insert after item %d\n", cur); + /* + * Insert an item in the list, regardless if it is unique + * or not. + */ + void BinaryInsertMultiple(T* item, int compare(T* item1, T* item2)) + { + T* ins_item = binary_insert(item, compare); + /* If identical, insert after the one found */ + if (ins_item != item) { InsertAfter(item, ins_item); } } - return item; -} - -/* - * Insert an item in the list, regardless if it is unique - * or not. - */ -template -void dlist::BinaryInsertMultiple(T* item, int compare(T* item1, T* item2)) -{ - T* ins_item = binary_insert(item, compare); - /* If identical, insert after the one found */ - if (ins_item != item) { InsertAfter(item, ins_item); } -} - -// Search for item -template -T* dlist::binary_search(T* item, int compare(T* item1, T* item2)) -{ - int comp; - int low, high, cur; - T* cur_item; - - - if (num_items == 0) { return NULL; } - cur_item = first(); - if (num_items == 1) { - comp = compare(item, cur_item); - if (comp == 0) { - return cur_item; + void remove(T* item) + { + T* xitem; + dlink* ilink = get_link(item); /* item's link */ + if (item == head) { + head = ilink->next; + if (head) { SetPrev(head, NULL); } + if (item == tail) { tail = ilink->prev; } + } else if (item == tail) { + tail = ilink->prev; + if (tail) { SetNext(tail, NULL); } } else { - return NULL; + xitem = ilink->next; + SetPrev(xitem, ilink->prev); + xitem = ilink->prev; + SetNext(xitem, ilink->next); } + num_items--; + if (num_items == 0) { head = tail = NULL; } } - low = 1; - high = num_items; - cur = 1; - cur_item = first(); - while (low < high) { - int nxt; - nxt = (low + high) / 2; - /* Now get cur pointing to nxt */ - while (nxt > cur) { - cur_item = next(cur_item); - cur++; - } - while (nxt < cur) { - cur_item = prev(cur_item); - cur--; - } - comp = compare(item, cur_item); - // Dmsg2(000, "Compare item to %d = %d\n", cur, comp); - if (comp < 0) { - high = cur; - // Dmsg2(000, "set high; low=%d high=%d\n", low, high); - } else if (comp > 0) { - low = cur + 1; - // Dmsg2(000, "set low; low=%d high=%d\n", low, high); - } else { - return cur_item; - } + bool empty() const { return head == nullptr; } + int size() const { return num_items; } + T* next(T* item) + { + if (item == NULL) { return head; } + return get_next(item); } - /* - * low == high can only happen if low just - * got incremented from cur, and we have - * not yet tested cur+1 - */ - if (low == high) { - cur_item = next(cur_item); - comp = compare(item, cur_item); - if (comp == 0) { return cur_item; } + T* prev(T* item) + { + if (item == NULL) { return tail; } + return get_prev(item); } - return NULL; -} - - -template void dlist::remove(T* item) -{ - T* xitem; - dlink* ilink = get_link(item); /* item's link */ - if (item == head) { - head = ilink->next; - if (head) { SetPrev(head, NULL); } - if (item == tail) { tail = ilink->prev; } - } else if (item == tail) { - tail = ilink->prev; - if (tail) { SetNext(tail, NULL); } - } else { - xitem = ilink->next; - SetPrev(xitem, ilink->prev); - xitem = ilink->prev; - SetNext(xitem, ilink->next); + /* Destroy the list contents */ + void destroy() + { + for (T* n = head; n;) { + T* ni = get_next(n); + free(n); + n = ni; + } + num_items = 0; + head = tail = NULL; } - num_items--; - if (num_items == 0) { head = tail = NULL; } -} -template T* dlist::next(T* item) -{ - if (item == NULL) { return head; } - return get_next(item); -} + T* first() const { return head; } + T* last() const { return tail; } +}; -template T* dlist::prev(T* item) -{ - if (item == NULL) { return tail; } - return get_prev(item); -} +/** + * C string helper routines for dlist + * The string (char *) is kept in the node + * + * Kern Sibbald, February 2007 + * + */ +class dlistString { + public: + char* c_str() { return str_; } + private: +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunused-private-field" +#endif + dlink link_; +#ifdef __clang__ +# pragma clang diagnostic pop +#endif + char str_[1]; + /* !!! Don't put anything after this as this space is used + * to hold the string in inline + */ +}; -/* Destroy the list contents */ -template void dlist::destroy() -{ - for (T* n = head; n;) { - T* ni = get_next(n); - free(n); - n = ni; - } - num_items = 0; - head = tail = NULL; -} +extern dlistString* new_dlistString(const char* str, int len); +extern dlistString* new_dlistString(const char* str); #endif // BAREOS_LIB_DLIST_H_ From 4457ed6a79ebcd836f876dadd00faa08ce4fc445 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Mon, 10 May 2021 21:07:10 +0200 Subject: [PATCH 6/6] lib: refactor alist.h to inline all methods To improve readability all methods have been moved into the class declaration itself which will imply the class templating and inlining. --- core/src/lib/alist.h | 278 +++++++++++++++++-------------------------- 1 file changed, 110 insertions(+), 168 deletions(-) diff --git a/core/src/lib/alist.h b/core/src/lib/alist.h index 2f08718ab25..021e2d85a8c 100644 --- a/core/src/lib/alist.h +++ b/core/src/lib/alist.h @@ -101,187 +101,129 @@ template class alist { int num_grow = 0; int cur_item = 0; bool own_items = false; - void GrowList(void); + /* Private grow list function. Used to insure that + * at least one more "slot" is available. + */ + void GrowList(void) + { + if (items == NULL) { + if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } + items = (T*)malloc(num_grow * sizeof(T)); + max_items = num_grow; + } else if (num_items == max_items) { + max_items += num_grow; + items = (T*)realloc(items, max_items * sizeof(T)); + } + } public: // Ueb disable non pointer initialization - alist(int num = 1, bool own = true); - ~alist(); - void init(int num = 1, bool own = true); - void append(T item); - void prepend(T item); - T remove(int index); - T get(int index); - bool empty() const; - T prev(); - T next(); - T first(); - T last(); - T operator[](int index) const; - int current() const { return cur_item; } - int size() const; - void destroy(); - void grow(int num); - - std::list to_std_list_string(); - - // Use it as a stack, pushing and popping from the end - void push(T item) { append(item); } - T pop() { return remove(num_items - 1); } -}; - -// Define index operator [] -template inline T alist::operator[](int index) const -{ - if (index < 0 || index >= num_items) { return nullptr; } - return items[index]; -} + alist(int num = 1, bool own = true) { init(num, own); } + ~alist() { destroy(); } -template inline bool alist::empty() const -{ - return num_items == 0; -} - -/* This allows us to do explicit initialization, - * allowing us to mix C++ classes inside malloc'ed - * C structures. Define before called in constructor. - */ -template inline void alist::init(int num, bool own) -{ - items = nullptr; - num_items = 0; - max_items = 0; - num_grow = num; - own_items = own; - cur_item = 0; -} - -template inline alist::alist(int num, bool own) -{ - init(num, own); -} - -template inline alist::~alist() { destroy(); } - -template inline int alist::size() const -{ - /* Check for null pointer, which allows test - * on size to succeed even if nothing put in - * alist. + /* This allows us to do explicit initialization, + * allowing us to mix C++ classes inside malloc'ed + * C structures. Define before called in constructor. */ - return num_items; -} - -/* How much to grow by each time */ -template inline void alist::grow(int num) { num_grow = num; } - -/* Private grow list function. Used to insure that - * at least one more "slot" is available. - */ -template void alist::GrowList() -{ - if (items == NULL) { - if (num_grow == 0) { num_grow = 1; /* default if not initialized */ } - items = (T*)malloc(num_grow * sizeof(T)); - max_items = num_grow; - } else if (num_items == max_items) { - max_items += num_grow; - items = (T*)realloc(items, max_items * sizeof(T)); + void init(int num = 1, bool own = true) + { + items = nullptr; + num_items = 0; + max_items = 0; + num_grow = num; + own_items = own; + cur_item = 0; } -} - -template T alist::first() -{ - cur_item = 1; - if (num_items == 0) { - return NULL; - } else { - return items[0]; + void append(T item) + { + GrowList(); + items[num_items++] = item; } -} - -template T alist::last() -{ - if (num_items == 0) { - return NULL; - } else { - cur_item = num_items; - return items[num_items - 1]; + void prepend(T item) + { + GrowList(); + if (num_items == 0) { + items[num_items++] = item; + return; + } + for (int i = num_items; i > 0; i--) { items[i] = items[i - 1]; } + items[0] = item; + num_items++; } -} - -template T alist::next() -{ - if (cur_item >= num_items) { - return NULL; - } else { - return items[cur_item++]; + T remove(int index) + { + T item; + if (index < 0 || index >= num_items) { return NULL; } + item = items[index]; + num_items--; + for (int i = index; i < num_items; i++) { items[i] = items[i + 1]; } + return item; } -} - -template T alist::prev() -{ - if (cur_item <= 1) { - return NULL; - } else { - return items[--cur_item]; + T get(int index) + { + if (index < 0 || index >= num_items) { return NULL; } + return items[index]; } -} - -// prepend an item to the list -- i.e. add to beginning -template void alist::prepend(T item) -{ - GrowList(); - if (num_items == 0) { - items[num_items++] = item; - return; + bool empty() const { return num_items == 0; } + T prev() + { + if (cur_item <= 1) { + return NULL; + } else { + return items[--cur_item]; + } } - for (int i = num_items; i > 0; i--) { items[i] = items[i - 1]; } - items[0] = item; - num_items++; -} - - -// Append an item to the list -template void alist::append(T item) -{ - GrowList(); - items[num_items++] = item; -} - -template -/* Remove an item from the list */ -T alist::remove(int index) -{ - T item; - if (index < 0 || index >= num_items) { return NULL; } - item = items[index]; - num_items--; - for (int i = index; i < num_items; i++) { items[i] = items[i + 1]; } - return item; -} - - -/* Get the index item -- we should probably allow real indexing here */ -template T alist::get(int index) -{ - if (index < 0 || index >= num_items) { return NULL; } - return items[index]; -} - -/* Destroy the list and its contents */ -template void alist::destroy() -{ - if (items) { - if (own_items) { - for (int i = 0; i < num_items; i++) { - free((void*)(items[i])); - items[i] = NULL; + T next() + { + if (cur_item >= num_items) { + return NULL; + } else { + return items[cur_item++]; + } + } + T first() + { + cur_item = 1; + if (num_items == 0) { + return NULL; + } else { + return items[0]; + } + } + T last() + { + if (num_items == 0) { + return NULL; + } else { + cur_item = num_items; + return items[num_items - 1]; + } + } + T operator[](int index) const + { + if (index < 0 || index >= num_items) { return nullptr; } + return items[index]; + } + int current() const { return cur_item; } + int size() const { return num_items; } + void destroy() + { + if (items) { + if (own_items) { + for (int i = 0; i < num_items; i++) { + free((void*)(items[i])); + items[i] = NULL; + } } + free(items); + items = NULL; } - free(items); - items = NULL; } -} + void grow(int num) { num_grow = num; } + + // Use it as a stack, pushing and popping from the end + void push(T item) { append(item); } + T pop() { return remove(num_items - 1); } +}; #endif // BAREOS_LIB_ALIST_H_