diff --git a/AUTHORS b/AUTHORS index dd05742d607..3e7724cc9dc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -142,6 +142,7 @@ Philipp Storz Philippe Chauvat Preben Guldberg Radosław Korzeniewski +Renout Gerrits Riccardo Ghetta Richard Mortimer Robert Nelson diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 6a965c2637a..1333100e507 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -83,6 +83,14 @@ if (cxx_compiler_macro_prefix_map) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.") endif() + +CHECK_CXX_COMPILER_FLAG(-Wno-unknown-pragmas compiler_has_no_unknown_pragmas) +if (${compiler_has_no_unknown_pragmas}) + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas") +endif() + + + # warn on sign-conversion #include(CheckCCompilerFlag) #CHECK_C_COMPILER_FLAG(-Wsign-conversion c_compiler_will_warn_sign_conversion) @@ -205,7 +213,7 @@ endif() if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") - include_directories( + include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/win32/include ${PROJECT_SOURCE_DIR}/src/win32/compat/include ) @@ -325,9 +333,9 @@ INCLUDE(BareosDetermineHaveLowLevelScsiInterface) INCLUDE(acltypes) -include_directories(${PYTHON_INCLUDE_DIRS}) +include_directories(SYSTEM ${PYTHON_INCLUDE_DIRS}) -include_directories(${PYTHON_INCLUDE_PATH}) +include_directories(SYSTEM ${PYTHON_INCLUDE_PATH}) include_directories(${PROJECT_SOURCE_DIR}/src) diff --git a/core/src/cats/bdb_query_enum_class.h b/core/src/cats/bdb_query_enum_class.h index 628d588f224..02f4ec758f0 100644 --- a/core/src/cats/bdb_query_enum_class.h +++ b/core/src/cats/bdb_query_enum_class.h @@ -2,85 +2,85 @@ class BareosDbQueryEnum { public: - typedef enum + enum class SQL_QUERY { - SQL_QUERY_get_restore_objects = 0, - SQL_QUERY_cleanup_created_job = 1, - SQL_QUERY_cleanup_running_job = 2, - SQL_QUERY_fill_jobhisto = 3, - SQL_QUERY_list_pool = 4, - SQL_QUERY_list_jobs = 5, - SQL_QUERY_list_jobs_long = 6, - SQL_QUERY_list_jobs_count = 7, - SQL_QUERY_list_jobs_last = 8, - SQL_QUERY_list_jobs_long_last = 9, - SQL_QUERY_sel_JobMedia = 10, - SQL_QUERY_drop_deltabs = 11, - SQL_QUERY_create_deltabs = 12, - SQL_QUERY_create_delindex = 13, - SQL_QUERY_uar_count_files = 14, - SQL_QUERY_uar_list_jobs = 15, - SQL_QUERY_uar_print_jobs = 16, - SQL_QUERY_uar_del_temp = 17, - SQL_QUERY_uar_del_temp1 = 18, - SQL_QUERY_uar_last_full = 19, - SQL_QUERY_uar_last_full_no_pool = 20, - SQL_QUERY_uar_full = 21, - SQL_QUERY_uar_dif = 22, - SQL_QUERY_uar_inc = 23, - SQL_QUERY_uar_list_jobs_by_idlist = 24, - SQL_QUERY_uar_sel_jobid_temp = 25, - SQL_QUERY_uar_sel_all_temp1 = 26, - SQL_QUERY_uar_sel_all_temp = 27, - SQL_QUERY_uar_sel_fileset = 28, - SQL_QUERY_uar_sel_filesetid = 29, - SQL_QUERY_uar_jobid_fileindex = 30, - SQL_QUERY_uar_jobids_fileindex = 31, - SQL_QUERY_uar_jobid_fileindex_from_table = 32, - SQL_QUERY_select_recent_version_with_basejob = 33, - SQL_QUERY_select_recent_version_with_basejob_and_delta = 34, - SQL_QUERY_select_recent_version = 35, - SQL_QUERY_create_temp_accurate_jobids = 36, - SQL_QUERY_create_temp_basefile = 37, - SQL_QUERY_create_temp_new_basefile = 38, - SQL_QUERY_uap_upgrade_copies_oldest_job = 39, - SQL_QUERY_uar_file = 40, - SQL_QUERY_uar_create_temp = 41, - SQL_QUERY_uar_create_temp1 = 42, - SQL_QUERY_uar_jobid_fileindex_from_dir = 43, - SQL_QUERY_sql_media_order_most_recently_written = 44, - SQL_QUERY_sql_get_max_connections = 45, - SQL_QUERY_bvfs_select = 46, - SQL_QUERY_bvfs_list_files = 47, - SQL_QUERY_batch_lock_path_query = 48, - SQL_QUERY_batch_lock_filename_query = 49, - SQL_QUERY_batch_unlock_tables_query = 50, - SQL_QUERY_batch_fill_path_query = 51, - SQL_QUERY_batch_fill_filename_query = 52, - SQL_QUERY_match_query = 53, - SQL_QUERY_match_query2 = 54, - SQL_QUERY_insert_counter_values = 55, - SQL_QUERY_select_counter_values = 56, - SQL_QUERY_update_counter_values = 57, - SQL_QUERY_get_quota_jobbytes = 58, - SQL_QUERY_get_quota_jobbytes_nofailed = 59, - SQL_QUERY_uar_sel_jobid_copies = 60, - SQL_QUERY_get_jobstatus_details = 61, - SQL_QUERY_bvfs_versions_6 = 62, - SQL_QUERY_bvfs_lsdirs_4 = 63, - SQL_QUERY_bvfs_clear_cache_0 = 64, - SQL_QUERY_bvfs_update_path_visibility_3 = 65, - SQL_QUERY_list_volumes_count_0 = 66, - SQL_QUERY_list_volumes_by_name_count_1 = 67, - SQL_QUERY_list_volumes_by_poolid_count_1 = 68, - SQL_QUERY_list_joblog_2 = 69, - SQL_QUERY_list_joblog_count_1 = 70, - SQL_QUERY_get_orphaned_paths_0 = 71, - SQL_QUERY_get_bad_paths_0 = 72, - SQL_QUERY_bvfs_ls_special_dirs_3 = 73, - SQL_QUERY_bvfs_ls_sub_dirs_5 = 74, - SQL_QUERY_list_volumes_select_0 = 75, - SQL_QUERY_list_volumes_select_long_0 = 76, + get_restore_objects = 0, + cleanup_created_job = 1, + cleanup_running_job = 2, + fill_jobhisto = 3, + list_pool = 4, + list_jobs = 5, + list_jobs_long = 6, + list_jobs_count = 7, + list_jobs_last = 8, + list_jobs_long_last = 9, + sel_JobMedia = 10, + drop_deltabs = 11, + create_deltabs = 12, + create_delindex = 13, + uar_count_files = 14, + uar_list_jobs = 15, + uar_print_jobs = 16, + uar_del_temp = 17, + uar_del_temp1 = 18, + uar_last_full = 19, + uar_last_full_no_pool = 20, + uar_full = 21, + uar_dif = 22, + uar_inc = 23, + uar_list_jobs_by_idlist = 24, + uar_sel_jobid_temp = 25, + uar_sel_all_temp1 = 26, + uar_sel_all_temp = 27, + uar_sel_fileset = 28, + uar_sel_filesetid = 29, + uar_jobid_fileindex = 30, + uar_jobids_fileindex = 31, + uar_jobid_fileindex_from_table = 32, + select_recent_version_with_basejob = 33, + select_recent_version_with_basejob_and_delta = 34, + select_recent_version = 35, + create_temp_accurate_jobids = 36, + create_temp_basefile = 37, + create_temp_new_basefile = 38, + uap_upgrade_copies_oldest_job = 39, + uar_file = 40, + uar_create_temp = 41, + uar_create_temp1 = 42, + uar_jobid_fileindex_from_dir = 43, + sql_media_order_most_recently_written = 44, + sql_get_max_connections = 45, + bvfs_select = 46, + bvfs_list_files = 47, + batch_lock_path_query = 48, + batch_lock_filename_query = 49, + batch_unlock_tables_query = 50, + batch_fill_path_query = 51, + batch_fill_filename_query = 52, + match_query = 53, + match_query2 = 54, + insert_counter_values = 55, + select_counter_values = 56, + update_counter_values = 57, + get_quota_jobbytes = 58, + get_quota_jobbytes_nofailed = 59, + uar_sel_jobid_copies = 60, + get_jobstatus_details = 61, + bvfs_versions_6 = 62, + bvfs_lsdirs_4 = 63, + bvfs_clear_cache_0 = 64, + bvfs_update_path_visibility_3 = 65, + list_volumes_count_0 = 66, + list_volumes_by_name_count_1 = 67, + list_volumes_by_poolid_count_1 = 68, + list_joblog_2 = 69, + list_joblog_count_1 = 70, + get_orphaned_paths_0 = 71, + get_bad_paths_0 = 72, + bvfs_ls_special_dirs_3 = 73, + bvfs_ls_sub_dirs_5 = 74, + list_volumes_select_0 = 75, + list_volumes_select_long_0 = 76, SQL_QUERY_NUMBER = 77 - } SQL_QUERY_ENUM; + }; }; diff --git a/core/src/cats/bvfs.cc b/core/src/cats/bvfs.cc index 9970e41f0f3..8ba775f1687 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-2017 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -295,7 +295,7 @@ bool BareosDb::UpdatePathHierarchyCache(JobControlRecord* jcr, StartTransaction(jcr); - FillQuery(cmd, SQL_QUERY_bvfs_update_path_visibility_3, jobid, jobid, jobid); + FillQuery(cmd, SQL_QUERY::bvfs_update_path_visibility_3, jobid, jobid, jobid); do { retval = QUERY_DB(jcr, cmd); @@ -596,7 +596,7 @@ void Bvfs::GetAllFileVersions(DBId_t pathid, db->EscapeString(jcr, fname_esc, (char*)fname, strlen(fname)); db->EscapeString(jcr, client_esc, (char*)client, strlen(client)); - db->FillQuery(query, BareosDb::SQL_QUERY_bvfs_versions_6, fname_esc, + db->FillQuery(query, BareosDb::SQL_QUERY::bvfs_versions_6, fname_esc, edit_uint64(pathid, ed1), client_esc, filter.c_str(), limit, offset); db->SqlQuery(query.c_str(), list_entries, user_data); @@ -648,16 +648,16 @@ bool Bvfs::ls_dirs() */ *prev_dir = 0; - db->FillQuery(special_dirs_query, BareosDb::SQL_QUERY_bvfs_ls_special_dirs_3, + db->FillQuery(special_dirs_query, BareosDb::SQL_QUERY::bvfs_ls_special_dirs_3, pathid, pathid, jobids); if (*pattern) { - db->FillQuery(filter, BareosDb::SQL_QUERY_match_query, pattern); + db->FillQuery(filter, BareosDb::SQL_QUERY::match_query, pattern); } - db->FillQuery(sub_dirs_query, BareosDb::SQL_QUERY_bvfs_ls_sub_dirs_5, pathid, + db->FillQuery(sub_dirs_query, BareosDb::SQL_QUERY::bvfs_ls_sub_dirs_5, pathid, jobids, jobids, filter.c_str(), jobids); - db->FillQuery(union_query, BareosDb::SQL_QUERY_bvfs_lsdirs_4, + db->FillQuery(union_query, BareosDb::SQL_QUERY::bvfs_lsdirs_4, special_dirs_query.c_str(), sub_dirs_query.c_str(), limit, offset); @@ -677,10 +677,10 @@ static void build_ls_files_query(JobControlRecord* jcr, int64_t offset) { if (db->GetTypeIndex() == SQL_TYPE_POSTGRESQL) { - db->FillQuery(query, BareosDb::SQL_QUERY_bvfs_list_files, JobId, PathId, + db->FillQuery(query, BareosDb::SQL_QUERY::bvfs_list_files, JobId, PathId, JobId, PathId, filter, limit, offset); } else { - db->FillQuery(query, BareosDb::SQL_QUERY_bvfs_list_files, JobId, PathId, + db->FillQuery(query, BareosDb::SQL_QUERY::bvfs_list_files, JobId, PathId, JobId, PathId, limit, offset, filter, JobId, JobId); } } @@ -701,7 +701,7 @@ bool Bvfs::ls_files() edit_uint64(pwd_id, pathid); if (*pattern) { - db->FillQuery(filter, BareosDb::SQL_QUERY_match_query2, pattern); + db->FillQuery(filter, BareosDb::SQL_QUERY::match_query2, pattern); } build_ls_files_query(jcr, db, query, jobids, pathid, filter.c_str(), limit, @@ -764,7 +764,7 @@ void Bvfs::clear_cache() * as MySQL queries do only support single SQL statements, * not multiple. */ - // db->SqlQuery(BareosDb::SQL_QUERY_bvfs_clear_cache_0); + // db->SqlQuery(BareosDb::SQL_QUERY::bvfs_clear_cache_0); db->StartTransaction(jcr); db->SqlQuery("UPDATE Job SET HasCache=0"); if (db->GetTypeIndex() == SQL_TYPE_SQLITE3) { @@ -928,7 +928,7 @@ bool Bvfs::compute_restore_list(char* fileid, goto bail_out; } - db->FillQuery(query, BareosDb::SQL_QUERY_bvfs_select, output_table, + db->FillQuery(query, BareosDb::SQL_QUERY::bvfs_select, output_table, output_table, output_table); /* TODO: handle jobid filter */ diff --git a/core/src/cats/cats.h b/core/src/cats/cats.h index bd2caa572de..5031fa7f573 100644 --- a/core/src/cats/cats.h +++ b/core/src/cats/cats.h @@ -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-2018 Bareos GmbH & Co. KG + 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 @@ -40,7 +40,7 @@ class dlist; -/* import automatically generated SQL_QUERY_ENUM */ +/* import automatically generated SQL_QUERY */ #include "bdb_query_enum_class.h" /* ============================================================== @@ -402,9 +402,9 @@ struct ClientDbRecord { */ struct CounterDbRecord { char Counter[MAX_NAME_LENGTH]{0}; - int32_t MinValue = 0; - int32_t MaxValue = 0; - int32_t CurrentValue = 0; + int32_t MinValue{0}; + int32_t MaxValue{0}; + int32_t CurrentValue{0}; char WrapCounter[MAX_NAME_LENGTH]{0}; }; @@ -648,10 +648,10 @@ class BareosDb : public BareosDbQueryEnum { pathid_cache& ppathid_cache, JobId_t JobId); void FillQueryVaList(POOLMEM*& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, va_list arg_ptr); void FillQueryVaList(PoolMem& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, va_list arg_ptr); public: @@ -922,7 +922,7 @@ class BareosDb : public BareosDbQueryEnum { e_list_type type, bool verbose); bool ListSqlQuery(JobControlRecord* jcr, - SQL_QUERY_ENUM query, + SQL_QUERY query, OutputFormatter* sendit, e_list_type type, bool verbose); @@ -933,7 +933,7 @@ class BareosDb : public BareosDbQueryEnum { const char* description, bool verbose = false); bool ListSqlQuery(JobControlRecord* jcr, - SQL_QUERY_ENUM query, + SQL_QUERY query, OutputFormatter* sendit, e_list_type type, const char* description, @@ -952,14 +952,14 @@ class BareosDb : public BareosDbQueryEnum { OutputFormatter* sendit); /* SqlQuery.c */ - const char* get_predefined_query_name(SQL_QUERY_ENUM query); - const char* get_predefined_query(SQL_QUERY_ENUM query); + const char* get_predefined_query_name(SQL_QUERY query); + const char* get_predefined_query(SQL_QUERY query); - void FillQuery(SQL_QUERY_ENUM predefined_query, ...); - void FillQuery(POOLMEM*& query, SQL_QUERY_ENUM predefined_query, ...); - void FillQuery(PoolMem& query, SQL_QUERY_ENUM predefined_query, ...); + void FillQuery(SQL_QUERY predefined_query, ...); + void FillQuery(POOLMEM*& query, SQL_QUERY predefined_query, ...); + void FillQuery(PoolMem& query, SQL_QUERY predefined_query, ...); - bool SqlQuery(SQL_QUERY_ENUM query, ...); + bool SqlQuery(SQL_QUERY query, ...); bool SqlQuery(const char* query, int flags = 0); bool SqlQuery(const char* query, DB_RESULT_HANDLER* ResultHandler, void* ctx); diff --git a/core/src/cats/dml/create_queryfiles.sh b/core/src/cats/dml/create_queryfiles.sh index 5be09045bf0..f8e853401bd 100755 --- a/core/src/cats/dml/create_queryfiles.sh +++ b/core/src/cats/dml/create_queryfiles.sh @@ -49,7 +49,7 @@ printf "%s\n\n" "$NOTE" >> $QUERY_ENUM_FILE cat >> $QUERY_ENUM_FILE << EOF class BareosDbQueryEnum { public: - typedef enum + enum class SQL_QUERY { EOF @@ -69,7 +69,7 @@ let i=0 for query in `ls ????_* | sed 's#\..*##g' | sort | uniq`; do queryname=`sed 's/[0-9]*_//' <<< $query` printf '"%s",\n' "$queryname" >> $QUERY_NAMES_FILE - printf " SQL_QUERY_%s = %s,\n" "$queryname" "$i" >> $QUERY_ENUM_FILE + printf " %s = %s,\n" "$queryname" "$i" >> $QUERY_ENUM_FILE let i++ for db in $DATABASES; do @@ -92,7 +92,7 @@ printf "NULL\n};\n" >> $QUERY_NAMES_FILE cat >> $QUERY_ENUM_FILE << EOF SQL_QUERY_NUMBER = $i - } SQL_QUERY_ENUM; + }; }; EOF diff --git a/core/src/cats/sql.cc b/core/src/cats/sql.cc index 1ea7b4c25b3..c2ea2b19f53 100644 --- a/core/src/cats/sql.cc +++ b/core/src/cats/sql.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-2016 Bareos GmbH & Co. KG + 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 @@ -180,7 +180,7 @@ bool BareosDb::CheckMaxConnections(JobControlRecord* jcr, /* * Check max_connections setting */ - FillQuery(query, SQL_QUERY_sql_get_max_connections); + FillQuery(query, SQL_QUERY::sql_get_max_connections); if (!SqlQueryWithHandler(query.c_str(), DbMaxConnectionsHandler, &context)) { Jmsg(jcr, M_ERROR, 0, "Can't verify max_connections settings %s", errmsg); return false; @@ -723,10 +723,11 @@ int BareosDb::ListResult(JobControlRecord* jcr, num_fields = SqlNumFields(); switch (type) { + case E_LIST_INIT: case NF_LIST: case RAW_LIST: /* - * No need to calculate things like column widths for unformated or raw + * No need to calculate things like column widths for unformatted or raw * output. */ break; @@ -779,6 +780,7 @@ int BareosDb::ListResult(JobControlRecord* jcr, filters_enabled = send->HasFilters(); switch (type) { + case E_LIST_INIT: case NF_LIST: case RAW_LIST: Dmsg1(800, "ListResult starts second loop looking at %d fields\n", diff --git a/core/src/cats/sql_create.cc b/core/src/cats/sql_create.cc index 7ea8f8491d5..32aa4b38d0e 100644 --- a/core/src/cats/sql_create.cc +++ b/core/src/cats/sql_create.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-2017 Bareos GmbH & Co. KG + 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 @@ -683,7 +683,6 @@ bool BareosDb::CreateCounterRecord(JobControlRecord* jcr, CounterDbRecord* cr) CounterDbRecord mcr; DbLock(this); - memset(&mcr, 0, sizeof(mcr)); bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter)); if (GetCounterRecord(jcr, &mcr)) { memcpy(cr, &mcr, sizeof(CounterDbRecord)); @@ -692,7 +691,7 @@ bool BareosDb::CreateCounterRecord(JobControlRecord* jcr, CounterDbRecord* cr) } EscapeString(jcr, esc, cr->Counter, strlen(cr->Counter)); - FillQuery(SQL_QUERY_insert_counter_values, esc, cr->MinValue, cr->MaxValue, + FillQuery(SQL_QUERY::insert_counter_values, esc, cr->MinValue, cr->MaxValue, cr->CurrentValue, cr->WrapCounter); if (!INSERT_DB(jcr, cmd)) { @@ -838,18 +837,18 @@ bool BareosDb::WriteBatchFileRecords(JobControlRecord* jcr) if (JobCanceled(jcr)) { goto bail_out; } - if (!jcr->db_batch->SqlQuery(SQL_QUERY_batch_lock_path_query)) { + if (!jcr->db_batch->SqlQuery(SQL_QUERY::batch_lock_path_query)) { Jmsg1(jcr, M_FATAL, 0, "Lock Path table %s\n", errmsg); goto bail_out; } - if (!jcr->db_batch->SqlQuery(SQL_QUERY_batch_fill_path_query)) { + if (!jcr->db_batch->SqlQuery(SQL_QUERY::batch_fill_path_query)) { Jmsg1(jcr, M_FATAL, 0, "Fill Path table %s\n", errmsg); - jcr->db_batch->SqlQuery(SQL_QUERY_batch_unlock_tables_query); + jcr->db_batch->SqlQuery(SQL_QUERY::batch_unlock_tables_query); goto bail_out; } - if (!jcr->db_batch->SqlQuery(SQL_QUERY_batch_unlock_tables_query)) { + if (!jcr->db_batch->SqlQuery(SQL_QUERY::batch_unlock_tables_query)) { Jmsg1(jcr, M_FATAL, 0, "Unlock Path table %s\n", errmsg); goto bail_out; } @@ -1147,11 +1146,11 @@ bool BareosDb::CreateBaseFileList(JobControlRecord* jcr, char* jobids) goto bail_out; } - FillQuery(SQL_QUERY_create_temp_basefile, (uint64_t)jcr->JobId); + FillQuery(SQL_QUERY::create_temp_basefile, (uint64_t)jcr->JobId); if (!SqlQuery(cmd)) { goto bail_out; } - FillQuery(buf, SQL_QUERY_select_recent_version, jobids, jobids); - FillQuery(SQL_QUERY_create_temp_new_basefile, (uint64_t)jcr->JobId, + FillQuery(buf, SQL_QUERY::select_recent_version, jobids, jobids); + FillQuery(SQL_QUERY::create_temp_new_basefile, (uint64_t)jcr->JobId, buf.c_str()); retval = SqlQuery(cmd); diff --git a/core/src/cats/sql_find.cc b/core/src/cats/sql_find.cc index c4b7611df48..e39436c48d8 100644 --- a/core/src/cats/sql_find.cc +++ b/core/src/cats/sql_find.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-2016 Bareos GmbH & Co. KG + 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 @@ -417,10 +417,11 @@ int BareosDb::FindNextVolume(JobControlRecord* jcr, that can be recycled */ } else { - FillQuery(order, - SQL_QUERY_sql_media_order_most_recently_written); /* Take most - recently - written */ + FillQuery( + order, + SQL_QUERY::sql_media_order_most_recently_written); /* Take + most recently + written */ } Mmsg(cmd, diff --git a/core/src/cats/sql_get.cc b/core/src/cats/sql_get.cc index 59edbb742f9..8c289de77c5 100644 --- a/core/src/cats/sql_get.cc +++ b/core/src/cats/sql_get.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-2017 Bareos GmbH & Co. KG + 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 @@ -831,7 +831,7 @@ bool BareosDb::GetCounterRecord(JobControlRecord* jcr, CounterDbRecord* cr) DbLock(this); EscapeString(jcr, esc, cr->Counter, strlen(cr->Counter)); - FillQuery(SQL_QUERY_select_counter_values, esc); + FillQuery(SQL_QUERY::select_counter_values, esc); if (QUERY_DB(jcr, cmd)) { num_rows = SqlNumRows(); @@ -1267,10 +1267,10 @@ bool BareosDb::GetFileList(JobControlRecord* jcr, } if (use_delta) { - FillQuery(query2, SQL_QUERY_select_recent_version_with_basejob_and_delta, + FillQuery(query2, SQL_QUERY::select_recent_version_with_basejob_and_delta, jobids, jobids, jobids, jobids); } else { - FillQuery(query2, SQL_QUERY_select_recent_version_with_basejob, jobids, + FillQuery(query2, SQL_QUERY::select_recent_version_with_basejob, jobids, jobids, jobids, jobids); } @@ -1346,7 +1346,7 @@ bool BareosDb::AccurateGetJobids(JobControlRecord* jcr, /* * First, find the last good Full backup for this job/client/fileset */ - FillQuery(query, SQL_QUERY_create_temp_accurate_jobids, + FillQuery(query, SQL_QUERY::create_temp_accurate_jobids, edit_uint64(jcr->JobId, jobid), edit_uint64(jr->ClientId, clientid), date, edit_uint64(jr->FileSetId, filesetid)); @@ -1537,7 +1537,7 @@ bool BareosDb::get_quota_jobbytes(JobControlRecord* jcr, DbLock(this); - FillQuery(SQL_QUERY_get_quota_jobbytes, edit_uint64(jr->ClientId, ed1), + FillQuery(SQL_QUERY::get_quota_jobbytes, edit_uint64(jr->ClientId, ed1), edit_uint64(jr->JobId, ed2), dt); if (QUERY_DB(jcr, cmd)) { num_rows = SqlNumRows(); @@ -1592,7 +1592,7 @@ bool BareosDb::get_quota_jobbytes_nofailed(JobControlRecord* jcr, DbLock(this); - FillQuery(SQL_QUERY_get_quota_jobbytes_nofailed, + FillQuery(SQL_QUERY::get_quota_jobbytes_nofailed, edit_uint64(jr->ClientId, ed1), edit_uint64(jr->JobId, ed2), dt); if (QUERY_DB(jcr, cmd)) { num_rows = SqlNumRows(); diff --git a/core/src/cats/sql_list.cc b/core/src/cats/sql_list.cc index 108415691af..0b7bc5f9ab6 100644 --- a/core/src/cats/sql_list.cc +++ b/core/src/cats/sql_list.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-2018 Bareos GmbH & Co. KG + 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 @@ -55,7 +55,7 @@ bool BareosDb::ListSqlQuery(JobControlRecord* jcr, } bool BareosDb::ListSqlQuery(JobControlRecord* jcr, - BareosDb::SQL_QUERY_ENUM query, + SQL_QUERY query, OutputFormatter* sendit, e_list_type type, bool verbose) @@ -92,7 +92,7 @@ bool BareosDb::ListSqlQuery(JobControlRecord* jcr, } bool BareosDb::ListSqlQuery(JobControlRecord* jcr, - BareosDb::SQL_QUERY_ENUM query, + SQL_QUERY query, OutputFormatter* sendit, e_list_type type, const char* description, @@ -237,18 +237,18 @@ void BareosDb::ListMediaRecords(JobControlRecord* jcr, if (count) { /* NOTE: ACLs are ignored. */ if (mdbr->VolumeName[0] != 0) { - FillQuery(query, SQL_QUERY_list_volumes_by_name_count_1, esc); + FillQuery(query, SQL_QUERY::list_volumes_by_name_count_1, esc); } else if (mdbr->PoolId > 0) { - FillQuery(query, SQL_QUERY_list_volumes_by_poolid_count_1, + FillQuery(query, SQL_QUERY::list_volumes_by_poolid_count_1, edit_int64(mdbr->PoolId, ed1)); } else { - FillQuery(query, SQL_QUERY_list_volumes_count_0); + FillQuery(query, SQL_QUERY::list_volumes_count_0); } } else { if (type == VERT_LIST) { - FillQuery(select, SQL_QUERY_list_volumes_select_long_0); + FillQuery(select, SQL_QUERY::list_volumes_select_long_0); } else { - FillQuery(select, SQL_QUERY_list_volumes_select_0); + FillQuery(select, SQL_QUERY::list_volumes_select_0); } if (mdbr->VolumeName[0] != 0) { @@ -480,9 +480,9 @@ void BareosDb::ListJoblogRecords(JobControlRecord* jcr, DbLock(this); if (count) { - FillQuery(SQL_QUERY_list_joblog_count_1, edit_int64(JobId, ed1)); + FillQuery(SQL_QUERY::list_joblog_count_1, edit_int64(JobId, ed1)); } else { - FillQuery(SQL_QUERY_list_joblog_2, edit_int64(JobId, ed1), range); + FillQuery(SQL_QUERY::list_joblog_2, edit_int64(JobId, ed1), range); if (type != VERT_LIST) { /* * When something else then a vertical list is requested set the list type @@ -606,18 +606,18 @@ void BareosDb::ListJobRecords(JobControlRecord* jcr, DbLock(this); if (count) { - FillQuery(SQL_QUERY_list_jobs_count, selection.c_str(), range); + FillQuery(SQL_QUERY::list_jobs_count, selection.c_str(), range); } else if (last) { if (type == VERT_LIST) { - FillQuery(SQL_QUERY_list_jobs_long_last, selection.c_str(), range); + FillQuery(SQL_QUERY::list_jobs_long_last, selection.c_str(), range); } else { - FillQuery(SQL_QUERY_list_jobs_last, selection.c_str(), range); + FillQuery(SQL_QUERY::list_jobs_last, selection.c_str(), range); } } else { if (type == VERT_LIST) { - FillQuery(SQL_QUERY_list_jobs_long, selection.c_str(), range); + FillQuery(SQL_QUERY::list_jobs_long, selection.c_str(), range); } else { - FillQuery(SQL_QUERY_list_jobs, selection.c_str(), range); + FillQuery(SQL_QUERY::list_jobs, selection.c_str(), range); } } diff --git a/core/src/cats/sql_query.cc b/core/src/cats/sql_query.cc index 017e69904ad..8192b9cf31c 100644 --- a/core/src/cats/sql_query.cc +++ b/core/src/cats/sql_query.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2016-2016 Planets Communications B.V. - Copyright (C) 2016-2017 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -33,22 +33,22 @@ static const int debuglevel = 100; -const char* BareosDb::get_predefined_query_name(BareosDb::SQL_QUERY_ENUM query) +const char* BareosDb::get_predefined_query_name(BareosDb::SQL_QUERY query) { - return query_names[query]; + return query_names[static_cast(query)]; } -const char* BareosDb::get_predefined_query(BareosDb::SQL_QUERY_ENUM query) +const char* BareosDb::get_predefined_query(BareosDb::SQL_QUERY query) { if (!queries) { Emsg0(M_ERROR, 0, "No SQL queries defined. This should not happen."); return NULL; } - return queries[query]; + return queries[static_cast(query)]; } -void BareosDb::FillQuery(BareosDb::SQL_QUERY_ENUM predefined_query, ...) +void BareosDb::FillQuery(BareosDb::SQL_QUERY predefined_query, ...) { va_list arg_ptr; @@ -58,7 +58,7 @@ void BareosDb::FillQuery(BareosDb::SQL_QUERY_ENUM predefined_query, ...) } void BareosDb::FillQuery(PoolMem& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, ...) { va_list arg_ptr; @@ -69,7 +69,7 @@ void BareosDb::FillQuery(PoolMem& query, } void BareosDb::FillQuery(POOLMEM*& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, ...) { va_list arg_ptr; @@ -81,7 +81,7 @@ void BareosDb::FillQuery(POOLMEM*& query, void BareosDb::FillQueryVaList(POOLMEM*& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, va_list arg_ptr) { PoolMem query_tmp(PM_MESSAGE); @@ -92,7 +92,7 @@ void BareosDb::FillQueryVaList(POOLMEM*& query, void BareosDb::FillQueryVaList(PoolMem& query, - BareosDb::SQL_QUERY_ENUM predefined_query, + BareosDb::SQL_QUERY predefined_query, va_list arg_ptr) { const char* query_name; @@ -111,7 +111,7 @@ void BareosDb::FillQueryVaList(PoolMem& query, } -bool BareosDb::SqlQuery(BareosDb::SQL_QUERY_ENUM predefined_query, ...) +bool BareosDb::SqlQuery(BareosDb::SQL_QUERY predefined_query, ...) { va_list arg_ptr; PoolMem query(PM_MESSAGE); diff --git a/core/src/cats/sql_update.cc b/core/src/cats/sql_update.cc index 9f69440ffc1..b596d2233aa 100644 --- a/core/src/cats/sql_update.cc +++ b/core/src/cats/sql_update.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-2016 Bareos GmbH & Co. KG + 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 @@ -133,7 +133,7 @@ int BareosDb::UpdateStats(JobControlRecord* jcr, utime_t age) DbLock(this); edit_uint64(now - age, ed1); - FillQuery(SQL_QUERY_fill_jobhisto, ed1); + FillQuery(SQL_QUERY::fill_jobhisto, ed1); if (QUERY_DB(jcr, cmd)) { rows = SqlAffectedRows(); } else { @@ -240,7 +240,7 @@ bool BareosDb::UpdateCounterRecord(JobControlRecord* jcr, CounterDbRecord* cr) DbLock(this); EscapeString(jcr, esc, cr->Counter, strlen(cr->Counter)); - FillQuery(SQL_QUERY_update_counter_values, cr->MinValue, cr->MaxValue, + FillQuery(SQL_QUERY::update_counter_values, cr->MinValue, cr->MaxValue, cr->CurrentValue, cr->WrapCounter, esc); retval = UPDATE_DB(jcr, cmd); diff --git a/core/src/dird/autoprune.cc b/core/src/dird/autoprune.cc index ff84b6223b2..e5e1ec16f2c 100644 --- a/core/src/dird/autoprune.cc +++ b/core/src/dird/autoprune.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -87,10 +87,9 @@ void PruneVolumes(JobControlRecord* jcr, { int i; int count; - PoolDbRecord spr; UaContext* ua; dbid_list ids; - struct del_ctx prune_list; + del_ctx prune_list; PoolMem query(PM_MESSAGE); char ed1[50], ed2[100], ed3[50]; @@ -100,7 +99,6 @@ void PruneVolumes(JobControlRecord* jcr, return; } - memset(&prune_list, 0, sizeof(prune_list)); prune_list.max_ids = 10000; prune_list.JobId = (JobId_t*)malloc(sizeof(JobId_t) * prune_list.max_ids); @@ -115,7 +113,7 @@ void PruneVolumes(JobControlRecord* jcr, /* * Get Pool record for Scratch Pool */ - memset(&spr, 0, sizeof(spr)); + PoolDbRecord spr; bstrncpy(spr.Name, "Scratch", sizeof(spr.Name)); if (jcr->db->GetPoolRecord(jcr, &spr)) { edit_int64(spr.PoolId, ed2); @@ -168,7 +166,6 @@ void PruneVolumes(JobControlRecord* jcr, for (i = 0; i < ids.num_ids; i++) { MediaDbRecord lmr; - memset(&lmr, 0, sizeof(lmr)); lmr.MediaId = ids.DBId[i]; Dmsg1(100, "Get record MediaId=%d\n", (int)lmr.MediaId); if (!jcr->db->GetMediaRecord(jcr, &lmr)) { diff --git a/core/src/dird/backup.cc b/core/src/dird/backup.cc index 66d9a517347..8d503e0f489 100644 --- a/core/src/dird/backup.cc +++ b/core/src/dird/backup.cc @@ -137,7 +137,8 @@ static inline bool ValidateStorage(JobControlRecord* jcr) default: Jmsg(jcr, M_FATAL, 0, _("Storage %s has illegal backup protocol %s for Native backup\n"), - store->resource_name_, AuthenticationProtocolTypeToString(store->Protocol)); + store->resource_name_, + AuthenticationProtocolTypeToString(store->Protocol)); return false; } } @@ -783,7 +784,6 @@ void NativeBackupCleanup(JobControlRecord* jcr, int TermCode) ClientDbRecord cr; Dmsg2(100, "Enter backup_cleanup %d %c\n", TermCode, TermCode); - memset(&cr, 0, sizeof(cr)); if (jcr->is_JobStatus(JS_Terminated) && (jcr->JobErrors || jcr->SDErrors || jcr->JobWarnings)) { @@ -941,7 +941,6 @@ void GenerateBackupSummary(JobControlRecord *jcr, ClientDbRecord *cr, int msg_ty secure_erase_status, compress_algo_list; - memset(&mr, 0, sizeof(mr)); bstrftimes(schedt, sizeof(schedt), jcr->jr.SchedTime); bstrftimes(sdt, sizeof(sdt), jcr->jr.StartTime); bstrftimes(edt, sizeof(edt), jcr->jr.EndTime); diff --git a/core/src/dird/catreq.cc b/core/src/dird/catreq.cc index 7571d0f1025..7a9d7c93b5c 100644 --- a/core/src/dird/catreq.cc +++ b/core/src/dird/catreq.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-2016 Bareos GmbH & Co. KG + 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 @@ -114,16 +114,11 @@ void CatalogRequest(JobControlRecord* jcr, BareosSocket* bs) PoolMem unwanted_volumes(PM_MESSAGE); int index, ok, label, writing; POOLMEM* omsg; - PoolDbRecord pr; uint32_t Stripe, Copy; uint64_t MediaId; utime_t VolFirstWritten; utime_t VolLastWritten; - memset(&sdmr, 0, sizeof(sdmr)); - memset(&jm, 0, sizeof(jm)); - memset(&mr, 0, sizeof(mr)); - /* * Request to find next appendable Volume for this Job */ @@ -144,7 +139,7 @@ void CatalogRequest(JobControlRecord* jcr, BareosSocket* bs) unwanted_volumes.check_size(bs->message_length); if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType, unwanted_volumes.c_str()) == 5) { - memset(&pr, 0, sizeof(pr)); + PoolDbRecord pr; bstrncpy(pr.Name, pool_name, sizeof(pr.Name)); UnbashSpaces(pr.Name); ok = jcr->db->GetPoolRecord(jcr, &pr); @@ -552,7 +547,6 @@ static void UpdateAttribute(JobControlRecord* jcr, case STREAM_RESTORE_OBJECT: { RestoreObjectDbRecord ro; - memset(&ro, 0, sizeof(ro)); ro.Stream = Stream; ro.FileIndex = FileIndex; if (jcr->mig_jcr) { diff --git a/core/src/dird/consolidate.cc b/core/src/dird/consolidate.cc index 473a4feba69..12589fa97c8 100644 --- a/core/src/dird/consolidate.cc +++ b/core/src/dird/consolidate.cc @@ -255,7 +255,7 @@ bool DoConsolidate(JobControlRecord* jcr) /** * Get db record of oldest jobid and check its age */ - memset(&jcr->previous_jr, 0, sizeof(jcr->previous_jr)); + jcr->previous_jr = JobDbRecord{}; jcr->previous_jr.JobId = str_to_int64(jobids); Dmsg1(10, "Previous JobId=%s\n", jobids); @@ -311,7 +311,8 @@ bool DoConsolidate(JobControlRecord* jcr) if (!jcr->vf_jobids) { jcr->vf_jobids = GetPoolMemory(PM_MESSAGE); } PmStrcpy(jcr->vf_jobids, p); - Jmsg(jcr, M_INFO, 0, _("%s: Start new consolidation\n"), job->resource_name_); + Jmsg(jcr, M_INFO, 0, _("%s: Start new consolidation\n"), + job->resource_name_); StartNewConsolidationJob(jcr, job->resource_name_); } } diff --git a/core/src/dird/dbcheck.cc b/core/src/dird/dbcheck.cc index 4cd9f9f0269..b6b25776d4c 100644 --- a/core/src/dird/dbcheck.cc +++ b/core/src/dird/dbcheck.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -571,7 +571,7 @@ static void eliminate_orphaned_path_records() lctx.count = 0; idx_tmp_name = NULL; - db->FillQuery(query, BareosDb::SQL_QUERY_get_orphaned_paths_0); + db->FillQuery(query, BareosDb::SQL_QUERY::get_orphaned_paths_0); printf(_("Checking for orphaned Path entries. This may take some time!\n")); if (verbose > 1) { printf("%s\n", query.c_str()); } @@ -863,7 +863,7 @@ static void repair_bad_paths() int i; printf(_("Checking for Paths without a trailing slash\n")); - db->FillQuery(query, BareosDb::SQL_QUERY_get_bad_paths_0); + db->FillQuery(query, BareosDb::SQL_QUERY::get_bad_paths_0); if (verbose > 1) { printf("%s\n", query.c_str()); } fflush(stdout); if (!MakeIdList(query.c_str(), &id_list)) { exit(1); } diff --git a/core/src/dird/dir_plugins.cc b/core/src/dird/dir_plugins.cc index 396afc78eaf..a73106ed73c 100644 --- a/core/src/dird/dir_plugins.cc +++ b/core/src/dird/dir_plugins.cc @@ -611,7 +611,6 @@ static bRC bareosGetValue(bpContext* ctx, brDirVariable var, void* value) case bDirVarNumVols: { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, jcr->res.pool->resource_name_, sizeof(pr.Name)); if (!jcr->db->GetPoolRecord(jcr, &pr)) { retval = bRC_Error; } *((int*)value) = pr.NumVols; diff --git a/core/src/dird/dird.cc b/core/src/dird/dird.cc index 7e3afb0bb47..782c9b364b7 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-2018 Bareos GmbH & Co. KG + 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 @@ -67,6 +67,7 @@ using namespace directordaemon; /* Forward referenced subroutines */ namespace directordaemon { + #if !defined(HAVE_WIN32) static #endif @@ -232,7 +233,6 @@ int main(int argc, char* argv[]) char* uid = NULL; char* gid = NULL; - start_heap = sbrk(0); setlocale(LC_ALL, ""); bindtextdomain("bareos", LOCALEDIR); textdomain("bareos"); @@ -543,7 +543,7 @@ static bool InitSighandlerSighup() bool retval = false; #if !defined(HAVE_WIN32) sigset_t block_mask; - struct sigaction action; + struct sigaction action = {}; /* * while handling SIGHUP signal, @@ -552,7 +552,6 @@ static bool InitSighandlerSighup() sigemptyset(&block_mask); sigaddset(&block_mask, SIGHUP); - memset(&action, 0, sizeof(action)); action.sa_sigaction = SighandlerReloadConfig; action.sa_mask = block_mask; action.sa_flags = SA_SIGINFO; @@ -993,7 +992,6 @@ static bool CheckCatalog(cat_op mode) } Dmsg2(500, "create cat=%s for client=%s\n", client->catalog->resource_name_, client->resource_name_); - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name)); db->CreateClientRecord(NULL, &cr); } @@ -1003,8 +1001,6 @@ static bool CheckCatalog(cat_op mode) foreach_res (store, R_STORAGE) { StorageDbRecord sr; MediaTypeDbRecord mtr; - memset(&sr, 0, sizeof(sr)); - memset(&mtr, 0, sizeof(mtr)); if (store->media_type) { bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType)); mtr.ReadOnly = 0; @@ -1062,8 +1058,8 @@ static bool CheckCatalog(cat_op mode) } /* cleanup old job records */ if (mode == UPDATE_AND_FIX) { - db->SqlQuery(BareosDb::SQL_QUERY_cleanup_created_job); - db->SqlQuery(BareosDb::SQL_QUERY_cleanup_running_job); + db->SqlQuery(BareosDb::SQL_QUERY::cleanup_created_job); + db->SqlQuery(BareosDb::SQL_QUERY::cleanup_running_job); } /* Set type in global for debugging */ diff --git a/core/src/dird/dird.h b/core/src/dird/dird.h index 7b75c43e4e8..ed201254352 100644 --- a/core/src/dird/dird.h +++ b/core/src/dird/dird.h @@ -29,21 +29,22 @@ */ #ifndef BAREOS_DIRD_DIRD_H_ #define BAREOS_DIRD_DIRD_H_ +#include "dird/dird_conf.h" +#include "include/bareos.h" #include "lib/connection_pool.h" #include "lib/runscript.h" -#include "lib/breg.h" #include "stored/bsr.h" -#include "dird_conf.h" +#include "ndmp/smc.h" #define DIRECTOR_DAEMON 1 -#include "dir_plugins.h" #include "cats/cats.h" +#include "dir_plugins.h" -#include "include/jcr.h" #include "dird/bsr.h" -#include "ua.h" +#include "include/jcr.h" #include "jobq.h" +#include "ua.h" class dlist; @@ -52,22 +53,22 @@ namespace directordaemon { /* Used in ua_prune.c and ua_purge.c */ struct s_count_ctx { - int count; + int count{}; }; #define MAX_DEL_LIST_LEN 2000000 struct del_ctx { - JobId_t* JobId; /**< array of JobIds */ - char* PurgedFiles; /**< Array of PurgedFile flags */ - int num_ids; /**< ids stored */ - int max_ids; /**< size of array */ - int num_del; /**< number deleted */ - int tot_ids; /**< total to process */ + JobId_t* JobId{nullptr}; /**< array of JobIds */ + char* PurgedFiles{nullptr}; /**< Array of PurgedFile flags */ + int num_ids{0}; /**< ids stored */ + int max_ids{0}; /**< size of array */ + int num_del{0}; /**< number deleted */ + int tot_ids{0}; /**< total to process */ }; /* Flags for FindNextVolumeForAppend() */ -enum +enum : bool { fnv_create_vol = true, fnv_no_create_vol = false, @@ -156,52 +157,31 @@ 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 */ - slot_type_t slot_type; /**< See slot_type_* */ - slot_number_t element_address; /**< scsi element address */ - slot_number_t - Slot; /**< Drive number when kSlotTypeDrive or actual slot number */ + 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 + or actual slot number */ }; - #if HAVE_NDMP -/** - * same as smc_element_address_assignment - * from ndmp/smc.h - * TODO: check if original definition can be used - */ -struct smc_element_address_assignment { - unsigned mte_addr; /* media transport element */ - unsigned mte_count; - - unsigned se_addr; /* storage element */ - unsigned se_count; - - unsigned iee_addr; /* import/export element */ - unsigned iee_count; - - unsigned dte_addr; /* data transfer element */ - unsigned dte_count; -}; - struct ndmp_deviceinfo_t { std::string device; std::string model; - JobId_t JobIdUsingDevice; + JobId_t JobIdUsingDevice{}; }; #endif - struct RuntimeStorageStatus { RuntimeStorageStatus() = default; ~RuntimeStorageStatus() = default; @@ -218,17 +198,17 @@ struct RuntimeStorageStatus { PTHREAD_MUTEX_INITIALIZER; /**< Any access to the list devices is controlled by this lock */ #if HAVE_NDMP - smc_element_address_assignment storage_mapping = {0}; + struct smc_element_address_assignment storage_mapping = {0}; std::list ndmp_deviceinfo; #endif }; struct runtime_client_status_t { - int32_t NumConcurrentJobs; /**< Number of concurrent jobs running */ + int32_t NumConcurrentJobs{}; /**< Number of concurrent jobs running */ }; struct runtime_job_status_t { - int32_t NumConcurrentJobs; /**< Number of concurrent jobs running */ + int32_t NumConcurrentJobs{}; /**< Number of concurrent jobs running */ }; #define INDEX_DRIVE_OFFSET 0 diff --git a/core/src/dird/dird_conf.cc b/core/src/dird/dird_conf.cc index 073216fb0aa..426a9b860bb 100644 --- a/core/src/dird/dird_conf.cc +++ b/core/src/dird/dird_conf.cc @@ -52,6 +52,7 @@ #include "dird/inc_conf.h" #include "dird/dird_globals.h" #include "lib/berrno.h" +#include "lib/breg.h" #include "lib/tls_conf.h" #include "lib/qualified_resource_name_type_converter.h" #include "lib/parse_conf.h" @@ -2512,7 +2513,8 @@ static bool UpdateResourcePointer(int type, ResourceItem* items) if (type == R_JOB) { p->rjs = (runtime_job_status_t*)malloc(sizeof(runtime_job_status_t)); - memset(p->rjs, 0, sizeof(runtime_job_status_t)); + runtime_job_status_t empty_rjs; + *p->rjs = empty_rjs; } } break; @@ -2551,7 +2553,8 @@ static bool UpdateResourcePointer(int type, ResourceItem* items) p->rcs = (runtime_client_status_t*)malloc(sizeof(runtime_client_status_t)); - memset(p->rcs, 0, sizeof(runtime_client_status_t)); + runtime_client_status_t empty_rcs; + *p->rcs = empty_rcs; } break; } diff --git a/core/src/dird/dird_globals.cc b/core/src/dird/dird_globals.cc index 32590201ead..5495c963ee7 100644 --- a/core/src/dird/dird_globals.cc +++ b/core/src/dird/dird_globals.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-2018 Bareos GmbH & Co. KG + 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 @@ -29,6 +29,5 @@ namespace directordaemon { ConfigurationParser* my_config = nullptr; DirectorResource* me = nullptr; char* configfile = nullptr; -void* start_heap = nullptr; } /* namespace directordaemon */ diff --git a/core/src/dird/expand.cc b/core/src/dird/expand.cc index b38042e1017..93ca3a598e4 100644 --- a/core/src/dird/expand.cc +++ b/core/src/dird/expand.cc @@ -3,7 +3,7 @@ Copyright (C) 2003-2006 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -261,7 +261,7 @@ static var_rc_t lookup_counter_var(var_t* ctx, if (counter->Catalog) { /* update catalog if need be */ CounterDbRecord cr; JobControlRecord* jcr = (JobControlRecord*)my_ctx; - memset(&cr, 0, sizeof(cr)); + bstrncpy(cr.Counter, counter->resource_name_, sizeof(cr.Counter)); cr.MinValue = counter->MinValue; cr.MaxValue = counter->MaxValue; @@ -487,8 +487,8 @@ int VariableExpansion(JobControlRecord* jcr, char* inp, POOLMEM*& exp) /* * Define callback */ - if ((status = var_config(var_ctx, VAR_CONFIG_CB_VALUE, lookup_var, - (void*)jcr)) != VAR_OK) { + if ((status = var_config(var_ctx, var_config_t::VAR_CONFIG_CB_VALUE, + lookup_var, (void*)jcr)) != VAR_OK) { Jmsg(jcr, M_ERROR, 0, _("Cannot set var callback: ERR=%s\n"), var_strerror(var_ctx, status)); goto bail_out; @@ -497,8 +497,8 @@ int VariableExpansion(JobControlRecord* jcr, char* inp, POOLMEM*& exp) /* * Define special operations */ - if ((status = var_config(var_ctx, VAR_CONFIG_CB_OPERATION, operate_var, - (void*)jcr)) != VAR_OK) { + if ((status = var_config(var_ctx, var_config_t::VAR_CONFIG_CB_OPERATION, + operate_var, (void*)jcr)) != VAR_OK) { Jmsg(jcr, M_ERROR, 0, _("Cannot set var operate: ERR=%s\n"), var_strerror(var_ctx, status)); goto bail_out; diff --git a/core/src/dird/fd_cmds.cc b/core/src/dird/fd_cmds.cc index 1a53c335568..fff6cf20b96 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-2018 Bareos GmbH & Co. KG + 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 @@ -376,7 +376,6 @@ int SendJobInfoToFileDaemon(JobControlRecord* jcr) } else if (jcr->db) { ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, jcr->res.client->resource_name_, sizeof(cr.Name)); cr.AutoPrune = jcr->res.client->AutoPrune; cr.FileRetention = jcr->res.client->FileRetention; @@ -989,7 +988,7 @@ bool SendPluginOptions(JobControlRecord* jcr) fd->fsend(pluginoptionscmd, cur_plugin_options.c_str()); if (!response(jcr, fd, OKPluginOptions, "PluginOptions", DISPLAY_ERROR)) { - Jmsg(jcr, M_FATAL, 0, _("Plugin options failed.\n")); + Jmsg(jcr, M_FATAL, 0, _("Plugin options failed.\n")); return false; } } @@ -1009,18 +1008,18 @@ static void SendGlobalRestoreObjects(JobControlRecord* jcr, /* * Send restore objects for all jobs involved */ - jcr->db->FillQuery(query, BareosDb::SQL_QUERY_get_restore_objects, + jcr->db->FillQuery(query, BareosDb::SQL_QUERY::get_restore_objects, jcr->JobIds, FT_RESTORE_FIRST); jcr->db->SqlQuery(query.c_str(), RestoreObjectHandler, (void*)octx); - jcr->db->FillQuery(query, BareosDb::SQL_QUERY_get_restore_objects, + jcr->db->FillQuery(query, BareosDb::SQL_QUERY::get_restore_objects, jcr->JobIds, FT_PLUGIN_CONFIG); jcr->db->SqlQuery(query.c_str(), RestoreObjectHandler, (void*)octx); /* * Send config objects for the current restore job */ - jcr->db->FillQuery(query, BareosDb::SQL_QUERY_get_restore_objects, + jcr->db->FillQuery(query, BareosDb::SQL_QUERY::get_restore_objects, edit_uint64(jcr->JobId, ed1), FT_PLUGIN_CONFIG_FILLED); jcr->db->SqlQuery(query.c_str(), RestoreObjectHandler, (void*)octx); } @@ -1035,11 +1034,11 @@ static void SendJobSpecificRestoreObjects(JobControlRecord* jcr, /* * Send restore objects for specific JobId. */ - jcr->db->FillQuery(query, BareosDb::SQL_QUERY_get_restore_objects, + jcr->db->FillQuery(query, BareosDb::SQL_QUERY::get_restore_objects, edit_uint64(JobId, ed1), FT_RESTORE_FIRST); jcr->db->SqlQuery(query.c_str(), RestoreObjectHandler, (void*)octx); - jcr->db->FillQuery(query, BareosDb::SQL_QUERY_get_restore_objects, + jcr->db->FillQuery(query, BareosDb::SQL_QUERY::get_restore_objects, edit_uint64(JobId, ed1), FT_PLUGIN_CONFIG); jcr->db->SqlQuery(query.c_str(), RestoreObjectHandler, (void*)octx); } diff --git a/core/src/dird/job.cc b/core/src/dird/job.cc index e0b552f78a4..1998d59322c 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-2018 Bareos GmbH & Co. KG + 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 @@ -894,7 +894,6 @@ DBId_t GetOrCreatePoolRecord(JobControlRecord* jcr, char* pool_name) { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, pool_name, sizeof(pr.Name)); Dmsg1(110, "get_or_create_pool=%s\n", pool_name); @@ -1357,7 +1356,6 @@ bool GetOrCreateClientRecord(JobControlRecord* jcr) { ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, jcr->res.client->resource_name_, sizeof(cr.Name)); cr.AutoPrune = jcr->res.client->AutoPrune; cr.FileRetention = jcr->res.client->FileRetention; @@ -1401,7 +1399,6 @@ bool GetOrCreateFilesetRecord(JobControlRecord* jcr) /* * Get or Create FileSet record */ - memset(&fsr, 0, sizeof(fsr)); bstrncpy(fsr.FileSet, jcr->res.fileset->resource_name_, sizeof(fsr.FileSet)); if (jcr->res.fileset->have_MD5) { MD5_CTX md5c; diff --git a/core/src/dird/migrate.cc b/core/src/dird/migrate.cc index ce29c807536..25305162bb8 100644 --- a/core/src/dird/migrate.cc +++ b/core/src/dird/migrate.cc @@ -293,7 +293,6 @@ static inline bool SetMigrationNextPool(JobControlRecord* jcr, * Get the PoolId used with the original job. Then * find the pool name from the database record. */ - memset(&pr, 0, sizeof(pr)); pr.PoolId = jcr->jr.PoolId; if (!jcr->db->GetPoolRecord(jcr, &pr)) { Jmsg(jcr, M_FATAL, 0, _("Pool for JobId %s not in database. ERR=%s\n"), @@ -1810,7 +1809,6 @@ void MigrationCleanup(JobControlRecord* jcr, int TermCode) JobControlRecord* mig_jcr = jcr->mig_jcr; PoolMem query(PM_MESSAGE); - memset(&mr, 0, sizeof(mr)); Dmsg2(100, "Enter migrate_cleanup %d %c\n", TermCode, TermCode); UpdateJobEnd(jcr, TermCode); diff --git a/core/src/dird/ndmp_dma_backup_NDMP_BAREOS.cc b/core/src/dird/ndmp_dma_backup_NDMP_BAREOS.cc index 8fb4dc9f6e8..b4e6a14e9ef 100644 --- a/core/src/dird/ndmp_dma_backup_NDMP_BAREOS.cc +++ b/core/src/dird/ndmp_dma_backup_NDMP_BAREOS.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -185,7 +185,8 @@ bool DoNdmpBackupInit(JobControlRecord* jcr) bool DoNdmpBackup(JobControlRecord* jcr) { unsigned int cnt; - int i, status; + int status; + unsigned int i; char ed1[100]; NIS* nis = NULL; FilesetResource* fileset; diff --git a/core/src/dird/ndmp_dma_backup_NDMP_NATIVE.cc b/core/src/dird/ndmp_dma_backup_NDMP_NATIVE.cc index 46b376d5b79..c8b3f308b13 100644 --- a/core/src/dird/ndmp_dma_backup_NDMP_NATIVE.cc +++ b/core/src/dird/ndmp_dma_backup_NDMP_NATIVE.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2015 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -169,7 +169,7 @@ int NdmpLoadNext(struct ndm_session* sess) */ bool DoNdmpBackupNdmpNative(JobControlRecord* jcr) { - int i, status; + int status; char ed1[100]; NIS* nis = NULL; FilesetResource* fileset; @@ -179,7 +179,6 @@ bool DoNdmpBackupNdmpNative(JobControlRecord* jcr) bool retval = false; uint32_t ndmp_log_level; - int driveindex; char* item; ndmp_log_level = std::max(jcr->res.client->ndmp_loglevel, me->ndmp_loglevel); diff --git a/core/src/dird/ndmp_dma_backup_common.cc b/core/src/dird/ndmp_dma_backup_common.cc index 57d4e28d647..86dc7550472 100644 --- a/core/src/dird/ndmp_dma_backup_common.cc +++ b/core/src/dird/ndmp_dma_backup_common.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -50,7 +50,8 @@ bool FillBackupEnvironment(JobControlRecord* jcr, char* filesystem, struct ndm_job_param* job) { - int i, j, cnt; + uint64_t i; + int j, cnt; bool exclude; FileOptions* fo; ndmp9_pval pv; @@ -297,7 +298,6 @@ void NdmpBackupCleanup(JobControlRecord* jcr, int TermCode) ClientDbRecord cr; Dmsg2(100, "Enter NdmpBackupCleanup %d %c\n", TermCode, TermCode); - memset(&cr, 0, sizeof(cr)); if (jcr->is_JobStatus(JS_Terminated) && (jcr->JobErrors || jcr->SDErrors || jcr->JobWarnings)) { diff --git a/core/src/dird/ndmp_dma_restore_NDMP_BAREOS.cc b/core/src/dird/ndmp_dma_restore_NDMP_BAREOS.cc index 3321744a801..50bcab5c658 100644 --- a/core/src/dird/ndmp_dma_restore_NDMP_BAREOS.cc +++ b/core/src/dird/ndmp_dma_restore_NDMP_BAREOS.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -36,6 +36,7 @@ #include "dird/storage.h" #include "lib/parse_bsr.h" +#include "lib/tree.h" #include "lib/volume_session_info.h" #if HAVE_NDMP @@ -769,7 +770,6 @@ static inline bool DoNdmpRestoreBootstrap(JobControlRecord* jcr) */ bool DoNdmpRestore(JobControlRecord* jcr) { - JobDbRecord rjr; /* restore job record */ int status; jcr->jr.JobLevel = L_FULL; /* Full restore */ diff --git a/core/src/dird/ndmp_dma_restore_NDMP_NATIVE.cc b/core/src/dird/ndmp_dma_restore_NDMP_NATIVE.cc index 3c17002651f..f37b7685b02 100644 --- a/core/src/dird/ndmp_dma_restore_NDMP_NATIVE.cc +++ b/core/src/dird/ndmp_dma_restore_NDMP_NATIVE.cc @@ -32,6 +32,7 @@ #include "dird/dird_globals.h" #include "dird/storage.h" #include "lib/edit.h" +#include "lib/tree.h" #if HAVE_NDMP @@ -458,7 +459,6 @@ static bool DoNdmpNativeRestore(JobControlRecord* jcr) */ bool DoNdmpRestoreNdmpNative(JobControlRecord* jcr) { - JobDbRecord rjr; /* restore job record */ int status; jcr->jr.JobLevel = L_FULL; /* Full restore */ diff --git a/core/src/dird/ndmp_dma_storage.cc b/core/src/dird/ndmp_dma_storage.cc index db08346b04d..abbd9af7bb4 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-2017 Bareos GmbH & Co. KG + 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 @@ -54,7 +54,9 @@ int get_tape_info_cb(struct ndm_session* sess, unsigned n_info) { Dmsg0(100, "Get tape info called\n"); - unsigned int i, j, k; + unsigned int i = 0; + unsigned int j; + unsigned int k; const char* what = "tape"; JobControlRecord* jcr = NULL; StorageResource* store = NULL; @@ -199,7 +201,6 @@ void DoNdmpNativeStorageStatus(UaContext* ua, StorageResource* store, char* cmd) NdmpDoQuery(ua, NULL, &ndmp_job, me->ndmp_loglevel, query_cbs); - ndmp_deviceinfo_t* deviceinfo = NULL; int i = 0; if (!store->runtime_storage_status->ndmp_deviceinfo.empty()) { ua->InfoMsg("NDMP Devices for storage %s:(%s)\n", store->resource_name_, @@ -407,8 +408,6 @@ static void FillVolumeName(vol_list_t* vl, struct smc_element_descriptor* edp) static void NdmpFillStorageMappings(StorageResource* store, struct ndm_session* ndmp_sess) { - drive_number_t drive; - slot_number_t slot, picker; struct smc_ctrl_block* smc; smc = ndmp_sess->control_acb->smc_cb; @@ -460,7 +459,7 @@ dlist* ndmp_get_vol_list(UaContext* ua, smc = ndmp_sess->control_acb->smc_cb; for (edp = smc->elem_desc; edp; edp = edp->next) { vl = (vol_list_t*)malloc(sizeof(vol_list_t)); - memset(vl, 0, sizeof(vol_list_t)); + *vl = vol_list_t{}; if (scan && !listall) { /* diff --git a/core/src/dird/ndmp_fhdb_mem.cc b/core/src/dird/ndmp_fhdb_mem.cc index dccc521efbd..8b1b1a6929a 100644 --- a/core/src/dird/ndmp_fhdb_mem.cc +++ b/core/src/dird/ndmp_fhdb_mem.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2015-2015 Planets Communications B.V. - Copyright (C) 2015-2015 Bareos GmbH & Co. KG + Copyright (C) 2015-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 @@ -29,6 +29,8 @@ #include "include/bareos.h" #include "dird.h" +#include "lib/rblist.h" +#include "lib/htable.h" #if HAVE_NDMP #include "ndmp/ndmagents.h" @@ -81,26 +83,26 @@ struct ndmp_fhdb_root { */ rblink sibling; rblist child; - char* fname; /**< File name */ - char* attr; /**< Encoded stat struct */ - int8_t FileType; /**< Type of File */ - int32_t FileIndex; /**< File index */ - uint64_t Offset; /**< File Offset in NDMP stream */ - uint64_t inode; /**< Inode nr */ - uint16_t fname_len; /**< Filename length */ - ndmp_fhdb_node* next; - ndmp_fhdb_node* parent; + char* fname{nullptr}; /**< File name */ + char* attr{nullptr}; /**< Encoded stat struct */ + int8_t FileType{0}; /**< Type of File */ + int32_t FileIndex{0}; /**< File index */ + uint64_t Offset{0}; /**< File Offset in NDMP stream */ + uint64_t inode{0}; /**< Inode nr */ + uint16_t fname_len{0}; /**< Filename length */ + ndmp_fhdb_node* next{nullptr}; + ndmp_fhdb_node* parent{nullptr}; /* * The above ^^^ must be identical to a ndmp_fhdb_node structure * The below vvv is only for the root of the tree. */ - ndmp_fhdb_node* first; /**< first entry in the tree */ - ndmp_fhdb_node* last; /**< last entry in the tree */ - ndmp_fhdb_mem* mem; /**< tree memory */ - uint32_t total_size; /**< total bytes allocated */ - uint32_t blocks; /**< total mallocs */ - ndmp_fhdb_node* cached_parent; /**< cached parent */ + ndmp_fhdb_node* first{nullptr}; /**< first entry in the tree */ + ndmp_fhdb_node* last{nullptr}; /**< last entry in the tree */ + ndmp_fhdb_mem* mem{nullptr}; /**< tree memory */ + uint32_t total_size{0}; /**< total bytes allocated */ + uint32_t blocks{0}; /**< total mallocs */ + ndmp_fhdb_node* cached_parent{nullptr}; /**< cached parent */ }; typedef struct ndmp_fhdb_root N_TREE_ROOT; @@ -145,7 +147,8 @@ static inline N_TREE_ROOT* ndmp_fhdb_new_tree() uint32_t size; root = (N_TREE_ROOT*)malloc(sizeof(N_TREE_ROOT)); - memset(root, 0, sizeof(N_TREE_ROOT)); + static const N_TREE_ROOT empty_N_TREE_ROOT{}; + *root = empty_N_TREE_ROOT; /* * Assume filename + node = 40 characters average length @@ -207,7 +210,8 @@ static N_TREE_NODE* ndmp_fhdb_new_tree_node(N_TREE_ROOT* root) int size = sizeof(N_TREE_NODE); node = (N_TREE_NODE*)ndmp_fhdb_tree_alloc(root, size); - memset(node, 0, size); + static N_TREE_NODE empty_N_TREE_NODE{}; + *node = empty_N_TREE_NODE; return node; } diff --git a/core/src/dird/ndmp_ndmmedia_db_helpers.cc b/core/src/dird/ndmp_ndmmedia_db_helpers.cc index 9f6dc53bbc4..6a817e72365 100644 --- a/core/src/dird/ndmp_ndmmedia_db_helpers.cc +++ b/core/src/dird/ndmp_ndmmedia_db_helpers.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2015 Planets Communications B.V. - Copyright (C) 2013-2017 Bareos GmbH & Co. KG + 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 @@ -129,9 +129,6 @@ bool StoreNdmmediaInfoInDatabase(ndmmedia* media, JobControlRecord* jcr) JobMediaDbRecord jm; MediaDbRecord mr; - memset(&jm, 0, sizeof(jm)); - memset(&mr, 0, sizeof(mr)); - /* * get media record by name */ @@ -176,12 +173,6 @@ bool StoreNdmmediaInfoInDatabase(ndmmedia* media, JobControlRecord* jcr) bool GetNdmmediaInfoFromDatabase(ndm_media_table* media_tab, JobControlRecord* jcr) { - JobMediaDbRecord jm; - MediaDbRecord mr; - - memset(&jm, 0, sizeof(jm)); - memset(&mr, 0, sizeof(mr)); - int VolCount; VolumeParameters* VolParams = NULL; bool retval = false; diff --git a/core/src/dird/newvol.cc b/core/src/dird/newvol.cc index a529e02306f..24e5734dd7b 100644 --- a/core/src/dird/newvol.cc +++ b/core/src/dird/newvol.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-2016 Bareos GmbH & Co. KG + 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 @@ -67,8 +67,6 @@ bool newVolume(JobControlRecord* jcr, MediaDbRecord* mr, StorageResource* store) bool retval = false; PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); - /* * See if we can create a new Volume */ @@ -76,7 +74,7 @@ bool newVolume(JobControlRecord* jcr, MediaDbRecord* mr, StorageResource* store) pr.PoolId = mr->PoolId; if (!jcr->db->GetPoolRecord(jcr, &pr)) { goto bail_out; } if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) { - memset(mr, 0, sizeof(MediaDbRecord)); + *mr = MediaDbRecord{}; SetPoolDbrDefaultsInMediaDbr(mr, &pr); jcr->VolumeName[0] = 0; bstrncpy(mr->MediaType, jcr->res.write_storage->media_type, @@ -150,7 +148,6 @@ static bool CreateSimpleName(JobControlRecord* jcr, for (int i = (int)ctx.value + 1; i < (int)ctx.value + 100; i++) { MediaDbRecord tmr; - memset(&tmr, 0, sizeof(tmr)); sprintf(num, "%04d", i); bstrncpy(tmr.VolumeName, name.c_str(), sizeof(tmr.VolumeName)); bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName)); diff --git a/core/src/dird/next_vol.cc b/core/src/dird/next_vol.cc index 0b9a98b8614..298b56d0e56 100644 --- a/core/src/dird/next_vol.cc +++ b/core/src/dird/next_vol.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-2018 Bareos GmbH & Co. KG + 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 @@ -437,7 +437,7 @@ bool GetScratchVolume(JobControlRecord* jcr, StorageResource* store) { MediaDbRecord smr; /* for searching scratch pool */ - PoolDbRecord spr, pr; + PoolDbRecord spr; bool ok = false; bool found = false; @@ -452,9 +452,6 @@ bool GetScratchVolume(JobControlRecord* jcr, * GetPoolRecord will first try ScratchPoolId, * and then try the pool named Scratch */ - memset(&smr, 0, sizeof(smr)); - memset(&spr, 0, sizeof(spr)); - bstrncpy(spr.Name, "Scratch", sizeof(spr.Name)); spr.PoolId = mr->ScratchPoolId; if (jcr->db->GetPoolRecord(jcr, &spr)) { @@ -487,7 +484,7 @@ bool GetScratchVolume(JobControlRecord* jcr, * Get pool record where the Scratch Volume will go to ensure that we can * add a Volume. */ - memset(&pr, 0, sizeof(pr)); + PoolDbRecord pr; bstrncpy(pr.Name, jcr->res.pool->resource_name_, sizeof(pr.Name)); if (!jcr->db->GetPoolRecord(jcr, &pr)) { diff --git a/core/src/dird/quota.cc b/core/src/dird/quota.cc index 856617341eb..bfb798b16e2 100644 --- a/core/src/dird/quota.cc +++ b/core/src/dird/quota.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-2016 Bareos GmbH & Co. KG + 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 two of the GNU General Public @@ -297,7 +297,6 @@ bool CheckSoftquotas(JobControlRecord* jcr) * Reset softquota */ ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); cr.ClientId = jcr->jr.ClientId; if (!jcr->db->ResetQuotaRecord(jcr, &cr)) { Jmsg(jcr, M_WARNING, 0, _("Error setting Quota gracetime: ERR=%s\n"), diff --git a/core/src/dird/restore.cc b/core/src/dird/restore.cc index 401be322900..581d962d14d 100644 --- a/core/src/dird/restore.cc +++ b/core/src/dird/restore.cc @@ -411,7 +411,6 @@ bool DoNativeRestoreInit(JobControlRecord* jcr) */ bool DoNativeRestore(JobControlRecord* jcr) { - JobDbRecord rjr; /* restore job record */ int status; jcr->jr.JobLevel = L_FULL; /* Full restore */ @@ -560,8 +559,8 @@ void GenerateRestoreSummary(JobControlRecord* jcr, " Bareos binary info: %s\n" " Termination: %s\n\n"), BAREOS, my_name, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER, - jcr->jr.JobId, jcr->jr.Job, jcr->res.client->resource_name_, sdt, edt, - edit_utime(RunTime, elapsed, sizeof(elapsed)), + jcr->jr.JobId, jcr->jr.Job, jcr->res.client->resource_name_, sdt, + edt, edit_utime(RunTime, elapsed, sizeof(elapsed)), edit_uint64_with_commas((uint64_t)jcr->ExpectedFiles, ec1), edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec2), edit_uint64_with_commas(jcr->jr.JobBytes, ec3), (float)kbps, @@ -601,8 +600,8 @@ void GenerateRestoreSummary(JobControlRecord* jcr, " Bareos binary info: %s\n" " Termination: %s\n\n"), BAREOS, my_name, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER, - jcr->jr.JobId, jcr->jr.Job, jcr->res.client->resource_name_, sdt, edt, - edit_utime(RunTime, elapsed, sizeof(elapsed)), + jcr->jr.JobId, jcr->jr.Job, jcr->res.client->resource_name_, sdt, + edt, edit_utime(RunTime, elapsed, sizeof(elapsed)), edit_uint64_with_commas((uint64_t)jcr->ExpectedFiles, ec1), edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec2), edit_uint64_with_commas(jcr->jr.JobBytes, ec3), (float)kbps, diff --git a/core/src/dird/sd_cmds.cc b/core/src/dird/sd_cmds.cc index 76fc16302c7..b494dc06de4 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-2018 Bareos GmbH & Co. KG + 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 @@ -362,7 +362,9 @@ dlist* native_get_vol_list(UaContext* ua, if (!field1 || !field2) { goto parse_error; } vl = (vol_list_t*)malloc(sizeof(vol_list_t)); - memset(vl, 0, sizeof(vol_list_t)); + { + *vl = vol_list_t{}; + } if (scan && !listall) { /* diff --git a/core/src/dird/stats.cc b/core/src/dird/stats.cc index 64be019fe05..cd73c3d5136 100644 --- a/core/src/dird/stats.cc +++ b/core/src/dird/stats.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2016-2016 Planets Communications B.V. - Copyright (C) 2014-2016 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -74,8 +74,6 @@ static inline bool LookupDevice(JobControlRecord* jcr, { DeviceDbRecord dr; - memset(&dr, 0, sizeof(dr)); - if (cached_device.StorageId == StorageId && bstrcmp(cached_device.device_name, device_name)) { *DeviceId = cached_device.DeviceId; @@ -177,8 +175,8 @@ extern "C" void* statistics_thread(void* arg) store = NULL; } - store = (StorageResource*)my_config->GetNextRes( - R_STORAGE, (BareosResource*)store); + store = (StorageResource*)my_config->GetNextRes(R_STORAGE, + (BareosResource*)store); if (!store) { PmStrcpy(current_store, ""); UnlockRes(my_config); @@ -220,7 +218,6 @@ extern "C" void* statistics_thread(void* arg) PoolMem DevName(PM_NAME); DeviceStatisticsDbRecord dsr; - memset(&dsr, 0, sizeof(dsr)); if (sscanf(sd->msg, DevStats, &dsr.SampleTime, DevName.c_str(), &dsr.ReadBytes, &dsr.WriteBytes, &dsr.SpoolSize, &dsr.NumWaiting, &dsr.NumWriters, &dsr.ReadTime, @@ -255,7 +252,6 @@ extern "C" void* statistics_thread(void* arg) PoolMem DevName(PM_NAME); TapealertStatsDbRecord tsr; - memset(&tsr, 0, sizeof(tsr)); if (sscanf(sd->msg, TapeAlerts, &tsr.SampleTime, DevName.c_str(), &tsr.AlertFlags) == 3) { UnbashSpaces(DevName); @@ -276,7 +272,6 @@ extern "C" void* statistics_thread(void* arg) PoolMem DevName(PM_NAME); JobStatisticsDbRecord jsr; - memset(&jsr, 0, sizeof(jsr)); if (sscanf(sd->msg, JobStats, &jsr.SampleTime, &jsr.JobId, &jsr.JobFiles, &jsr.JobBytes, DevName.c_str()) == 5) { UnbashSpaces(DevName); diff --git a/core/src/dird/storage.cc b/core/src/dird/storage.cc index 27f9e39079f..627018bf7f2 100644 --- a/core/src/dird/storage.cc +++ b/core/src/dird/storage.cc @@ -620,8 +620,8 @@ changer_vol_list_t* get_vol_list_from_storage(UaContext* ua, Dmsg0(100, "Need to free still referenced vol_list\n"); store->runtime_storage_status->vol_list = (changer_vol_list_t*)malloc(sizeof(changer_vol_list_t)); - memset(store->runtime_storage_status->vol_list, 0, - sizeof(changer_vol_list_t)); + changer_vol_list_t empty_vol_list; + *store->runtime_storage_status->vol_list = empty_vol_list; } } @@ -648,8 +648,8 @@ changer_vol_list_t* get_vol_list_from_storage(UaContext* ua, if (!store->runtime_storage_status->vol_list) { store->runtime_storage_status->vol_list = (changer_vol_list_t*)malloc(sizeof(changer_vol_list_t)); - memset(store->runtime_storage_status->vol_list, 0, - sizeof(changer_vol_list_t)); + changer_vol_list_t empty_vol_list; + *store->runtime_storage_status->vol_list = empty_vol_list; } vol_list = store->runtime_storage_status->vol_list; vol_list->reference_count++; diff --git a/core/src/dird/ua.h b/core/src/dird/ua.h index ae48e288cac..ecac7f5aad0 100644 --- a/core/src/dird/ua.h +++ b/core/src/dird/ua.h @@ -34,11 +34,12 @@ #include "include/bareos.h" #include "lib/bsock.h" #include "lib/output_formatter.h" -#include "lib/tree.h" class JobControlRecord; class BareosDb; class guid_list; +typedef struct s_tree_root TREE_ROOT; +typedef struct s_tree_node TREE_NODE; namespace directordaemon { diff --git a/core/src/dird/ua_acl.cc b/core/src/dird/ua_acl.cc index 40881ee10d4..e5d9b72f62b 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-2016 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -422,7 +422,6 @@ StorageResource* UaContext::GetStoreResWithId(DBId_t id, bool lock) { StorageDbRecord storage_dbr; - memset(&storage_dbr, 0, sizeof(storage_dbr)); storage_dbr.StorageId = id; if (db->GetStorageRecord(jcr, &storage_dbr)) { diff --git a/core/src/dird/ua_cmds.cc b/core/src/dird/ua_cmds.cc index 0d0249daaab..ca2a6aa60c5 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-2018 Bareos GmbH & Co. KG + 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 @@ -656,9 +656,6 @@ static bool add_cmd(UaContext* ua, const char* cmd) if (!OpenClientDb(ua)) { return true; } - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - if (!GetPoolDbr(ua, &pr)) { return true; } Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols, pr.MaxVols, @@ -993,7 +990,6 @@ static bool SetbwlimitCmd(UaContext* ua, const char* cmd) char Job[MAX_NAME_LENGTH]; const char* lst[] = {"job", "jobid", "ujobid", "all", "state", NULL}; - memset(Job, 0, sizeof(Job)); i = FindArgWithValue(ua, NT_("limit")); if (i >= 0) { limit = ((int64_t)atoi(ua->argv[i]) * 1024); } @@ -1936,14 +1932,10 @@ static bool TruncateCmd(UaContext* ua, const char* cmd) PoolDbRecord pool_dbr; StorageDbRecord storage_dbr; - memset(&pool_dbr, 0, sizeof(pool_dbr)); - memset(&storage_dbr, 0, sizeof(storage_dbr)); - /* * Look for volumes that can be recycled, * are enabled and have used more than the first block. */ - memset(&mr, 0, sizeof(mr)); mr.Recycle = 1; mr.Enabled = VOL_ENABLED; mr.VolBytes = (512 * 126); /* search volumes with more than 64,512 bytes @@ -2085,7 +2077,6 @@ static bool TruncateCmd(UaContext* ua, const char* cmd) * Loop over the candidate Volumes and actually truncate them */ for (int i = 0; i < mediaIds.size(); i++) { - memset(&mr, 0, sizeof(mr)); mr.MediaId = mediaIds.get(i); if (!ua->db->GetMediaRecord(ua->jcr, &mr)) { Dmsg1(0, "Can't find MediaId=%lld\n", (uint64_t)mr.MediaId); @@ -2111,8 +2102,6 @@ static bool DoTruncate(UaContext* ua, MediaDbRecord& mr) StorageDbRecord storage_dbr; PoolDbRecord pool_dbr; - memset(&storage_dbr, 0, sizeof(storage_dbr)); - memset(&pool_dbr, 0, sizeof(pool_dbr)); storage_dbr.StorageId = mr.StorageId; if (!ua->db->GetStorageRecord(ua->jcr, &storage_dbr)) { @@ -2361,7 +2350,6 @@ static bool DeleteVolume(UaContext* ua) char buf[1000]; db_list_ctx lst; - memset(&mr, 0, sizeof(mr)); if (!SelectMediaDbr(ua, &mr)) { return true; } ua->WarningMsg(_("\nThis command will delete volume %s\n" "and all Jobs saved on that volume from the Catalog\n"), @@ -2400,7 +2388,6 @@ static bool DeletePool(UaContext* ua) PoolDbRecord pr; char buf[200]; - memset(&pr, 0, sizeof(pr)); if (!GetPoolDbr(ua, &pr)) { return true; } Bsnprintf(buf, sizeof(buf), diff --git a/core/src/dird/ua_db.cc b/core/src/dird/ua_db.cc index 418adb52bf3..b595aa4ba96 100644 --- a/core/src/dird/ua_db.cc +++ b/core/src/dird/ua_db.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-2015 Bareos GmbH & Co. KG + 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 @@ -207,8 +207,6 @@ int CreatePool(JobControlRecord* jcr, { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); - bstrncpy(pr.Name, pool->resource_name_, sizeof(pr.Name)); if (db->GetPoolRecord(jcr, &pr)) { @@ -261,11 +259,10 @@ bool SetPooldbrReferences(JobControlRecord* jcr, PoolDbRecord* pr, PoolResource* pool) { - PoolDbRecord rpool; bool ret = true; if (pool->RecyclePool) { - memset(&rpool, 0, sizeof(rpool)); + PoolDbRecord rpool; bstrncpy(rpool.Name, pool->RecyclePool->resource_name_, sizeof(rpool.Name)); if (db->GetPoolRecord(jcr, &rpool)) { @@ -283,7 +280,7 @@ bool SetPooldbrReferences(JobControlRecord* jcr, } if (pool->ScratchPool) { - memset(&rpool, 0, sizeof(rpool)); + PoolDbRecord rpool; bstrncpy(rpool.Name, pool->ScratchPool->resource_name_, sizeof(rpool.Name)); if (db->GetPoolRecord(jcr, &rpool)) { @@ -359,8 +356,6 @@ int UpdatePoolReferences(JobControlRecord* jcr, PoolDbRecord pr; if (!pool->RecyclePool && !pool->ScratchPool) { return true; } - - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, pool->resource_name_, sizeof(pr.Name)); if (!db->GetPoolRecord(jcr, &pr)) { return -1; /* not exists in database */ } diff --git a/core/src/dird/ua_dotcmds.cc b/core/src/dird/ua_dotcmds.cc index e7f0de555a6..e140dc2fba3 100644 --- a/core/src/dird/ua_dotcmds.cc +++ b/core/src/dird/ua_dotcmds.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -636,14 +636,12 @@ bool DotBvfsGetJobidsCmd(UaContext* ua, const char* cmd) if (jr.JobLevel == L_BASE) { jobids.add(edit_int64(jr.JobId, ed1)); } else { - FileSetDbRecord fs; - /* * If we have the "all" option, we do a search on all defined fileset for * this client */ if (FindArg(ua, "all") > 0) { - ua->db->FillQuery(query, BareosDb::SQL_QUERY_uar_sel_filesetid, + ua->db->FillQuery(query, BareosDb::SQL_QUERY::uar_sel_filesetid, edit_int64(jr.ClientId, ed1)); ua->db->GetQueryDbids(ua->jcr, query, ids); } else { @@ -657,7 +655,7 @@ bool DotBvfsGetJobidsCmd(UaContext* ua, const char* cmd) * Foreach different FileSet, we build a restore jobid list */ for (int i = 0; i < ids.num_ids; i++) { - memset(&fs, 0, sizeof(fs)); + FileSetDbRecord fs; /* * Lookup the FileSet. @@ -958,7 +956,7 @@ bool DotJobstatusCmd(UaContext* ua, const char* cmd) } } - ua->db->FillQuery(select, BareosDb::SQL_QUERY_get_jobstatus_details, + ua->db->FillQuery(select, BareosDb::SQL_QUERY::get_jobstatus_details, where.c_str()); if (!OpenClientDb(ua)) { return false; } diff --git a/core/src/dird/ua_label.cc b/core/src/dird/ua_label.cc index a17581266d7..ee6ab5174d6 100644 --- a/core/src/dird/ua_label.cc +++ b/core/src/dird/ua_label.cc @@ -324,7 +324,6 @@ static void label_from_barcodes(UaContext* ua, char* slot_list; int max_slots; - memset(&mr, 0, sizeof(mr)); max_slots = GetNumSlots(ua, ua->jcr->res.write_storage); if (max_slots <= 0) { @@ -366,7 +365,6 @@ static void label_from_barcodes(UaContext* ua, /* * Select a pool */ - memset(&pr, 0, sizeof(pr)); if (!SelectPoolDbr(ua, &pr)) { goto bail_out; } /* @@ -376,7 +374,7 @@ static void label_from_barcodes(UaContext* ua, if (!vl->VolName || !BitIsSet(vl->bareos_slot_number - 1, slot_list)) { continue; } - memset(&mr, 0, sizeof(mr)); + mr = MediaDbRecord{}; bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName)); media_record_exists = false; if (ua->db->GetMediaRecord(ua->jcr, &mr)) { @@ -479,10 +477,6 @@ static int do_label(UaContext* ua, const char* cmd, bool relabel) if (!OpenClientDb(ua)) { return 1; } - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - memset(&omr, 0, sizeof(omr)); - if (ua->batch || FindArg(ua, NT_("yes")) > 0) { yes = true; } /* @@ -626,7 +620,7 @@ static int do_label(UaContext* ua, const char* cmd, bool relabel) * Must select Pool if not already done */ if (pr.PoolId == 0) { - memset(&pr, 0, sizeof(pr)); + pr = PoolDbRecord{}; if (!SelectPoolDbr(ua, &pr)) { return 1; } } diff --git a/core/src/dird/ua_output.cc b/core/src/dird/ua_output.cc index 07e5e5b3560..745ad5a0024 100644 --- a/core/src/dird/ua_output.cc +++ b/core/src/dird/ua_output.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-2018 Bareos GmbH & Co. KG + 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 @@ -1258,11 +1258,11 @@ static bool ParseListBackupsCmd(UaContext* ua, PmStrcat(criteria, range); if (llist == VERT_LIST) { - ua->db->FillQuery(ua->cmd, BareosDb::SQL_QUERY_list_jobs_long, + ua->db->FillQuery(ua->cmd, BareosDb::SQL_QUERY::list_jobs_long, selection.c_str(), criteria.c_str()); } else { - ua->db->FillQuery(ua->cmd, BareosDb::SQL_QUERY_list_jobs, selection.c_str(), - criteria.c_str()); + ua->db->FillQuery(ua->cmd, BareosDb::SQL_QUERY::list_jobs, + selection.c_str(), criteria.c_str()); } return true; diff --git a/core/src/dird/ua_prune.cc b/core/src/dird/ua_prune.cc index d14a1525aba..8b9536236cb 100644 --- a/core/src/dird/ua_prune.cc +++ b/core/src/dird/ua_prune.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2009 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -47,14 +47,14 @@ namespace directordaemon { /* Forward referenced functions */ static bool PruneDirectory(UaContext* ua, ClientResource* client); static bool PruneStats(UaContext* ua, utime_t retention); -static bool GrowDelList(struct del_ctx* del); +static bool GrowDelList(del_ctx* del); /** * Called here to count entries to be deleted */ int DelCountHandler(void* ctx, int num_fields, char** row) { - struct s_count_ctx* cnt = (struct s_count_ctx*)ctx; + s_count_ctx* cnt = static_cast(ctx); if (row[0]) { cnt->count = str_to_int64(row[0]); @@ -74,7 +74,7 @@ int DelCountHandler(void* ctx, int num_fields, char** row) */ int JobDeleteHandler(void* ctx, int num_fields, char** row) { - struct del_ctx* del = (struct del_ctx*)ctx; + del_ctx* del = static_cast(ctx); if (!GrowDelList(del)) { return 1; } del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]); @@ -86,7 +86,7 @@ int JobDeleteHandler(void* ctx, int num_fields, char** row) int FileDeleteHandler(void* ctx, int num_fields, char** row) { - struct del_ctx* del = (struct del_ctx*)ctx; + del_ctx* del = static_cast(ctx); if (!GrowDelList(del)) { return 1; } del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]); @@ -128,9 +128,6 @@ bool PruneCmd(UaContext* ua, const char* cmd) if (!OpenClientDb(ua, true)) { return false; } - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - /* * First search args */ @@ -339,8 +336,7 @@ static bool PruneDirectory(UaContext* ua, ClientResource* client) if (client) { char ed1[50]; - - memset(&cr, 0, sizeof(cr)); + cr = ClientDbRecord{}; bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name)); if (!ua->db->CreateClientRecord(ua->jcr, &cr)) { goto bail_out; } @@ -491,7 +487,7 @@ static bool prune_set_filter(UaContext* ua, */ bool PruneFiles(UaContext* ua, ClientResource* client, PoolResource* pool) { - struct del_ctx del; + del_ctx del; struct s_count_ctx cnt; PoolMem query(PM_MESSAGE); PoolMem sql_where(PM_MESSAGE); @@ -499,8 +495,6 @@ bool PruneFiles(UaContext* ua, ClientResource* client, PoolResource* pool) utime_t period; char ed1[50]; - memset(&del, 0, sizeof(del)); - if (pool && pool->FileRetention > 0) { period = pool->FileRetention; @@ -566,19 +560,19 @@ bool PruneFiles(UaContext* ua, ClientResource* client, PoolResource* pool) static void DropTempTables(UaContext* ua) { - ua->db->SqlQuery(BareosDb::SQL_QUERY_drop_deltabs); + ua->db->SqlQuery(BareosDb::SQL_QUERY::drop_deltabs); } static bool CreateTempTables(UaContext* ua) { /* Create temp tables and indicies */ - if (!ua->db->SqlQuery(BareosDb::SQL_QUERY_create_deltabs)) { + if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::create_deltabs)) { ua->ErrorMsg("%s", ua->db->strerror()); Dmsg0(050, "create DelTables table failed\n"); return false; } - if (!ua->db->SqlQuery(BareosDb::SQL_QUERY_create_delindex)) { + if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::create_delindex)) { ua->ErrorMsg("%s", ua->db->strerror()); Dmsg0(050, "create DelInx1 index failed\n"); return false; @@ -587,7 +581,7 @@ static bool CreateTempTables(UaContext* ua) return true; } -static bool GrowDelList(struct del_ctx* del) +static bool GrowDelList(del_ctx* del) { if (del->num_ids == MAX_DEL_LIST_LEN) { return false; } @@ -668,8 +662,7 @@ static bool PruneBackupJobs(UaContext* ua, struct accurate_check_ctx* elt = nullptr; db_list_ctx jobids, tempids; JobDbRecord jr; - struct del_ctx del; - memset(&del, 0, sizeof(del)); + del_ctx del; if (pool && pool->JobRetention > 0) { period = pool->JobRetention; @@ -845,7 +838,7 @@ bool PruneJobs(UaContext* ua, bool PruneVolume(UaContext* ua, MediaDbRecord* mr) { PoolMem query(PM_MESSAGE); - struct del_ctx del; + del_ctx del; bool ok = false; int count; @@ -853,7 +846,6 @@ bool PruneVolume(UaContext* ua, MediaDbRecord* mr) return false; /* Cannot prune archived volumes */ } - memset(&del, 0, sizeof(del)); del.max_ids = 10000; del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids); @@ -893,7 +885,7 @@ int GetPruneListForVolume(UaContext* ua, MediaDbRecord* mr, del_ctx* del) */ period = mr->VolRetention; now = (utime_t)time(NULL); - ua->db->FillQuery(query, BareosDb::SQL_QUERY_sel_JobMedia, + ua->db->FillQuery(query, BareosDb::SQL_QUERY::sel_JobMedia, edit_int64(mr->MediaId, ed1), edit_int64(now - period, ed2)); diff --git a/core/src/dird/ua_purge.cc b/core/src/dird/ua_purge.cc index 72b2da7d62c..7b5ddcd0ba5 100644 --- a/core/src/dird/ua_purge.cc +++ b/core/src/dird/ua_purge.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -236,16 +236,14 @@ bool PurgeCmd(UaContext* ua, const char* cmd) */ static bool PurgeFilesFromClient(UaContext* ua, ClientResource* client) { - struct del_ctx del; + del_ctx del; PoolMem query(PM_MESSAGE); ClientDbRecord cr; char ed1[50]; - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name)); if (!ua->db->CreateClientRecord(ua->jcr, &cr)) { return false; } - memset(&del, 0, sizeof(del)); del.max_ids = 1000; del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids); @@ -284,17 +282,15 @@ static bool PurgeFilesFromClient(UaContext* ua, ClientResource* client) */ static bool PurgeJobsFromClient(UaContext* ua, ClientResource* client) { - struct del_ctx del; + del_ctx del; PoolMem query(PM_MESSAGE); ClientDbRecord cr; char ed1[50]; - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name)); if (!ua->db->CreateClientRecord(ua->jcr, &cr)) { return false; } - memset(&del, 0, sizeof(del)); del.max_ids = 1000; del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids); del.PurgedFiles = (char*)malloc(del.max_ids); @@ -424,7 +420,6 @@ static bool PurgeQuotaFromClient(UaContext* ua, ClientResource* client) { ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); bstrncpy(cr.Name, client->resource_name_, sizeof(cr.Name)); if (!ua->db->CreateClientRecord(ua->jcr, &cr)) { return false; } if (!ua->db->CreateQuotaRecord(ua->jcr, &cr)) { return false; } @@ -457,7 +452,7 @@ void UpgradeCopies(UaContext* ua, char* jobs) DbLock(ua->db); /* Do it in two times for mysql */ - ua->db->FillQuery(query, BareosDb::SQL_QUERY_uap_upgrade_copies_oldest_job, + ua->db->FillQuery(query, BareosDb::SQL_QUERY::uap_upgrade_copies_oldest_job, JT_JOB_COPY, jobs, jobs); ua->db->SqlQuery(query.c_str()); @@ -728,8 +723,6 @@ static bool ActionOnPurgeCmd(UaContext* ua, const char* cmd) char esc[MAX_NAME_LENGTH * 2 + 1]; PoolMem buf(PM_MESSAGE), volumes(PM_MESSAGE); - memset(&mr, 0, sizeof(mr)); - memset(&pr, 0, sizeof(pr)); PmStrcpy(volumes, ""); /* @@ -825,15 +818,16 @@ static bool ActionOnPurgeCmd(UaContext* ua, const char* cmd) * Loop over the candidate Volumes and actually truncate them */ for (int i = 0; i < nb; i++) { - memset(&mr, 0, sizeof(mr)); - mr.MediaId = results[i]; - if (ua->db->GetMediaRecord(ua->jcr, &mr)) { + MediaDbRecord mr_temp; + mr_temp.MediaId = results[i]; + if (ua->db->GetMediaRecord(ua->jcr, &mr_temp)) { /* TODO: ask for drive and change Pool */ if (Bstrcasecmp("truncate", action) || Bstrcasecmp("all", action)) { - do_truncate_on_purge(ua, &mr, pr.Name, store->dev_name(), drive, sd); + do_truncate_on_purge(ua, &mr_temp, pr.Name, store->dev_name(), drive, + sd); } } else { - Dmsg1(0, "Can't find MediaId=%lld\n", (uint64_t)mr.MediaId); + Dmsg1(0, "Can't find MediaId=%lld\n", (uint64_t)mr_temp.MediaId); } } @@ -872,8 +866,6 @@ bool MarkMediaPurged(UaContext* ua, MediaDbRecord* mr) */ if (mr->RecyclePoolId && mr->RecyclePoolId != mr->PoolId) { PoolDbRecord oldpr, newpr; - memset(&oldpr, 0, sizeof(oldpr)); - memset(&newpr, 0, sizeof(newpr)); newpr.PoolId = mr->RecyclePoolId; oldpr.PoolId = mr->PoolId; if (ua->db->GetPoolRecord(jcr, &oldpr) && diff --git a/core/src/dird/ua_restore.cc b/core/src/dird/ua_restore.cc index d8d974b2523..dd28ddbd3ff 100644 --- a/core/src/dird/ua_restore.cc +++ b/core/src/dird/ua_restore.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -42,9 +42,11 @@ #include "dird/ua_tree.h" #include "dird/ua_run.h" #include "dird/bsr.h" +#include "lib/breg.h" #include "lib/edit.h" #include "lib/berrno.h" #include "lib/parse_conf.h" +#include "lib/tree.h" #include "include/make_unique.h" namespace directordaemon { @@ -352,7 +354,7 @@ static void GetAndDisplayBasejobs(UaContext* ua, RestoreContext* rx) PoolMem query(PM_MESSAGE); ua->SendMsg(_("The restore will use the following job(s) as Base\n")); - ua->db->FillQuery(query, BareosDb::SQL_QUERY_uar_print_jobs, jobids.list); + ua->db->FillQuery(query, BareosDb::SQL_QUERY::uar_print_jobs, jobids.list); ua->db->ListSqlQuery(ua->jcr, query.c_str(), ua->send, HORZ_LIST, true); } PmStrcpy(rx->BaseJobIds, jobids.list); @@ -396,7 +398,6 @@ static bool GetClientName(UaContext* ua, RestoreContext* rx) { int i; ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); /* * If no client name specified yet, get it now @@ -472,8 +473,6 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) /* Include current second if using current time */ utime_t now = time(NULL) + 1; JobId_t JobId; - JobDbRecord jr; - jr.JobId = -1; bool done = false; int i, j; const char* list[] = { @@ -628,7 +627,7 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) } gui_save = ua->jcr->gui; ua->jcr->gui = true; - ua->db->ListSqlQuery(ua->jcr, BareosDb::SQL_QUERY_uar_list_jobs, + ua->db->ListSqlQuery(ua->jcr, BareosDb::SQL_QUERY::uar_list_jobs, ua->send, HORZ_LIST, true); ua->jcr->gui = gui_save; done = false; @@ -639,7 +638,7 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) len = strlen(ua->cmd); fname = (char*)malloc(len * 2 + 1); ua->db->EscapeString(ua->jcr, fname, ua->cmd, len); - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_file, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_file, rx->ClientName, fname); free(fname); gui_save = ua->jcr->gui; @@ -757,18 +756,19 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) !IsAnInteger(ua->cmd)) { return 0; } - - memset(&jr, 0, sizeof(jr)); - jr.JobId = str_to_int64(ua->cmd); - if (!ua->db->GetJobRecord(ua->jcr, &jr)) { - ua->ErrorMsg(_("Unable to get Job record for JobId=%s: ERR=%s\n"), - ua->cmd, ua->db->strerror()); - return 0; + { + JobDbRecord jr; + jr.JobId = str_to_int64(ua->cmd); + if (!ua->db->GetJobRecord(ua->jcr, &jr)) { + ua->ErrorMsg(_("Unable to get Job record for JobId=%s: ERR=%s\n"), + ua->cmd, ua->db->strerror()); + return 0; + } + ua->SendMsg(_("Selecting jobs to build the Full state at %s\n"), + jr.cStartTime); + jr.JobLevel = L_INCREMENTAL; /* Take Full+Diff+Incr */ + if (!ua->db->AccurateGetJobids(ua->jcr, &jr, &jobids)) { return 0; } } - ua->SendMsg(_("Selecting jobs to build the Full state at %s\n"), - jr.cStartTime); - jr.JobLevel = L_INCREMENTAL; /* Take Full+Diff+Incr */ - if (!ua->db->AccurateGetJobids(ua->jcr, &jr, &jobids)) { return 0; } PmStrcpy(rx->JobIds, jobids.list); Dmsg1(30, "Item 12: jobids = %s\n", rx->JobIds); break; @@ -777,7 +777,6 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) } } - memset(&jr, 0, sizeof(jr)); POOLMEM* JobIds = GetPoolMemory(PM_FNAME); *JobIds = 0; rx->TotalFiles = 0; @@ -785,6 +784,7 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) * Find total number of files to be restored, and filter the JobId * list to contain only ones permitted by the ACL conditions. */ + JobDbRecord jr; for (p = rx->JobIds;;) { char ed1[50]; int status = GetNextJobidFromList(&p, &JobId); @@ -795,7 +795,7 @@ static int UserSelectJobidsOrFiles(UaContext* ua, RestoreContext* rx) } if (status == 0) { break; } if (jr.JobId == JobId) { continue; /* duplicate of last JobId */ } - memset(&jr, 0, sizeof(jr)); + jr = JobDbRecord{}; jr.JobId = JobId; if (!ua->db->GetJobRecord(ua->jcr, &jr)) { ua->ErrorMsg(_("Unable to get Job record for JobId=%s: ERR=%s\n"), @@ -910,10 +910,10 @@ static bool InsertFileIntoFindexList(UaContext* ua, SplitPathAndFilename(ua, rx, file); if (*rx->JobIds == 0) { - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_jobid_fileindex, date, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_jobid_fileindex, date, rx->path, rx->fname, rx->ClientName); } else { - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_jobids_fileindex, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_jobids_fileindex, rx->JobIds, date, rx->path, rx->fname, rx->ClientName); } @@ -948,7 +948,7 @@ static bool InsertDirIntoFindexList(UaContext* ua, return false; } else { ua->db->FillQuery(rx->query, - BareosDb::SQL_QUERY_uar_jobid_fileindex_from_dir, + BareosDb::SQL_QUERY::uar_jobid_fileindex_from_dir, rx->JobIds, dir, rx->ClientName); } @@ -977,7 +977,7 @@ static bool InsertTableIntoFindexList(UaContext* ua, StripTrailingJunk(table); ua->db->FillQuery(rx->query, - BareosDb::SQL_QUERY_uar_jobid_fileindex_from_table, table); + BareosDb::SQL_QUERY::uar_jobid_fileindex_from_table, table); /* * Find and insert jobid and File Index @@ -1118,7 +1118,7 @@ static bool BuildDirectoryTree(UaContext* ua, RestoreContext* rx) /* * Use first JobId as estimate of the number of files to restore */ - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_count_files, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_count_files, edit_int64(JobId, ed1)); if (!ua->db->SqlQuery(rx->query, RestoreCountHandler, (void*)rx)) { ua->ErrorMsg("%s\n", ua->db->strerror()); @@ -1252,26 +1252,24 @@ static bool SelectBackupsBeforeDate(UaContext* ua, /* * Create temp tables */ - ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_del_temp); - ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_del_temp1); + ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_del_temp); + ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_del_temp1); - if (!ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_create_temp)) { + if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_create_temp)) { ua->ErrorMsg("%s\n", ua->db->strerror()); } - if (!ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_create_temp1)) { + if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_create_temp1)) { ua->ErrorMsg("%s\n", ua->db->strerror()); } /* * Select Client from the Catalog */ - memset(&cr, 0, sizeof(cr)); if (!GetClientDbr(ua, &cr)) { goto bail_out; } rx->ClientName = strdup(cr.Name); /* * Get FileSet */ - memset(&fsr, 0, sizeof(fsr)); i = FindArgWithValue(ua, "FileSet"); if (i >= 0 && IsNameValid(ua->argv[i], ua->errmsg)) { @@ -1286,7 +1284,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, } if (i < 0) { /* fileset not found */ - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_sel_fileset, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_sel_fileset, edit_int64(cr.ClientId, ed1), ed1); StartPrompt(ua, _("The defined FileSet resources are:\n")); @@ -1315,8 +1313,6 @@ static bool SelectBackupsBeforeDate(UaContext* ua, pool_select[0] = 0; if (rx->pool) { PoolDbRecord pr; - - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, rx->pool->resource_name_, sizeof(pr.Name)); if (ua->db->GetPoolRecord(ua->jcr, &pr)) { Bsnprintf(pool_select, sizeof(pool_select), "AND Media.PoolId=%s ", @@ -1330,7 +1326,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, * Find JobId of last Full backup for this client, fileset */ if (pool_select[0]) { - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_last_full, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_last_full, edit_int64(cr.ClientId, ed1), date, fsr.FileSet, pool_select); @@ -1339,7 +1335,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, goto bail_out; } } else { - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_last_full_no_pool, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_last_full_no_pool, edit_int64(cr.ClientId, ed1), date, fsr.FileSet); if (!ua->db->SqlQuery(rx->query)) { @@ -1351,7 +1347,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, /* * Find all Volumes used by that JobId */ - if (!ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_full)) { + if (!ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_full)) { ua->ErrorMsg("%s\n", ua->db->strerror()); goto bail_out; } @@ -1361,7 +1357,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, * just above. */ rx->JobTDate = 0; - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_sel_all_temp1); + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_sel_all_temp1); if (!ua->db->SqlQuery(rx->query, LastFullHandler, (void*)rx)) { ua->WarningMsg("%s\n", ua->db->strerror()); } @@ -1373,7 +1369,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, /* * Now find most recent Differential Job after Full save, if any */ - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_dif, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_dif, edit_uint64(rx->JobTDate, ed1), date, edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select); if (!ua->db->SqlQuery(rx->query)) { @@ -1384,7 +1380,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, * Now update JobTDate to look into Differential, if any */ rx->JobTDate = 0; - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_sel_all_temp); + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_sel_all_temp); if (!ua->db->SqlQuery(rx->query, LastFullHandler, (void*)rx)) { ua->WarningMsg("%s\n", ua->db->strerror()); } @@ -1396,7 +1392,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, /* * Now find all Incremental Jobs after Full/dif save */ - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_inc, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_inc, edit_uint64(rx->JobTDate, ed1), date, edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select); if (!ua->db->SqlQuery(rx->query)) { @@ -1408,7 +1404,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, */ rx->last_jobid[0] = rx->JobIds[0] = 0; - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_sel_jobid_temp); + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_sel_jobid_temp); if (!ua->db->SqlQuery(rx->query, JobidHandler, (void*)rx)) { ua->WarningMsg("%s\n", ua->db->strerror()); } @@ -1436,7 +1432,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, */ PmStrcpy(JobIds, rx->JobIds); rx->last_jobid[0] = rx->JobIds[0] = 0; - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_sel_jobid_copies, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_sel_jobid_copies, JobIds.c_str()); if (!ua->db->SqlQuery(rx->query, JobidHandler, (void*)rx)) { ua->WarningMsg("%s\n", ua->db->strerror()); @@ -1447,7 +1443,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua, /* * Display a list of Jobs selected for this restore */ - ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY_uar_list_jobs_by_idlist, + ua->db->FillQuery(rx->query, BareosDb::SQL_QUERY::uar_list_jobs_by_idlist, rx->JobIds); ua->db->ListSqlQuery(ua->jcr, rx->query, ua->send, HORZ_LIST, true); @@ -1457,8 +1453,8 @@ static bool SelectBackupsBeforeDate(UaContext* ua, } bail_out: - ua->db->SqlQuery(BareosDb::SQL_QUERY_drop_deltabs); - ua->db->SqlQuery(BareosDb::SQL_QUERY_uar_del_temp1); + ua->db->SqlQuery(BareosDb::SQL_QUERY::drop_deltabs); + ua->db->SqlQuery(BareosDb::SQL_QUERY::uar_del_temp1); return ok; } diff --git a/core/src/dird/ua_run.cc b/core/src/dird/ua_run.cc index ff9589c9225..4df3333256c 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-2018 Bareos GmbH & Co. KG + 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 @@ -35,6 +35,7 @@ #include "dird/ua_input.h" #include "dird/ua_select.h" #include "dird/ua_run.h" +#include "lib/breg.h" #include "lib/berrno.h" #include "lib/edit.h" #include "lib/keyword_table_s.h" @@ -102,7 +103,6 @@ static inline bool reRunJob(UaContext* ua, JobId_t JobId, bool yes, utime_t now) if (jr.ClientId) { ClientDbRecord cr; - memset(&cr, 0, sizeof(cr)); cr.ClientId = jr.ClientId; if (!ua->db->GetClientRecord(ua->jcr, &cr)) { Jmsg(ua->jcr, M_WARNING, 0, @@ -117,7 +117,6 @@ static inline bool reRunJob(UaContext* ua, JobId_t JobId, bool yes, utime_t now) if (jr.PoolId) { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); pr.PoolId = jr.PoolId; if (!ua->db->GetPoolRecord(ua->jcr, &pr)) { Jmsg(ua->jcr, M_WARNING, 0, @@ -186,7 +185,6 @@ static inline bool reRunJob(UaContext* ua, JobId_t JobId, bool yes, utime_t now) if (jr.FileSetId) { FileSetDbRecord fs; - memset(&fs, 0, sizeof(fs)); fs.FileSetId = jr.FileSetId; if (!ua->db->GetFilesetRecord(ua->jcr, &fs)) { Jmsg(ua->jcr, M_WARNING, 0, @@ -520,7 +518,8 @@ int DoRunCmd(UaContext* ua, const char* cmd) JobId = RunJob(jcr); Dmsg4(100, "JobId=%u NewJobId=%d using pool %s priority=%d\n", - (int)jcr->JobId, JobId, jcr->res.pool->resource_name_, jcr->JobPriority); + (int)jcr->JobId, JobId, jcr->res.pool->resource_name_, + jcr->JobPriority); FreeJcr(jcr); /* release jcr */ @@ -730,7 +729,8 @@ int ModifyJobParameters(UaContext* ua, JobControlRecord* jcr, RunContext& rc) rc.next_pool = select_pool_resource(ua); if (rc.next_pool) { jcr->res.next_pool = rc.next_pool; - Dmsg1(100, "Set new next_pool=%s\n", jcr->res.next_pool->resource_name_); + Dmsg1(100, "Set new next_pool=%s\n", + jcr->res.next_pool->resource_name_); goto try_again; } } else if (jcr->is_JobType(JT_VERIFY)) { /* Verify Job */ @@ -867,7 +867,9 @@ static bool ResetRestoreContext(UaContext* ua, } jcr->res.client = rc.client; - if (jcr->res.client) { PmStrcpy(jcr->client_name, rc.client->resource_name_); } + if (jcr->res.client) { + PmStrcpy(jcr->client_name, rc.client->resource_name_); + } jcr->res.fileset = rc.fileset; jcr->ExpectedFiles = rc.files; @@ -1236,19 +1238,19 @@ static bool DisplayJobParameters(UaContext* ua, : _("*None*"), bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } else { - ua->SendMsg(_("Run Admin Job\n" - "JobName: %s\n" - "FileSet: %s\n" - "Client: %s\n" - "Storage: %s\n" - "When: %s\n" - "Priority: %d\n"), - job->resource_name_, jcr->res.fileset->resource_name_, - NPRT(jcr->res.client->resource_name_), - jcr->res.write_storage ? jcr->res.write_storage->resource_name_ - : _("*None*"), - bstrutime(dt, sizeof(dt), jcr->sched_time), - jcr->JobPriority); + ua->SendMsg( + _("Run Admin Job\n" + "JobName: %s\n" + "FileSet: %s\n" + "Client: %s\n" + "Storage: %s\n" + "When: %s\n" + "Priority: %d\n"), + job->resource_name_, jcr->res.fileset->resource_name_, + NPRT(jcr->res.client->resource_name_), + jcr->res.write_storage ? jcr->res.write_storage->resource_name_ + : _("*None*"), + bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } jcr->setJobLevel(L_FULL); break; @@ -1270,19 +1272,19 @@ static bool DisplayJobParameters(UaContext* ua, : _("*None*"), bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } else { - ua->SendMsg(_("Run Archive Job\n" - "JobName: %s\n" - "FileSet: %s\n" - "Client: %s\n" - "Storage: %s\n" - "When: %s\n" - "Priority: %d\n"), - job->resource_name_, jcr->res.fileset->resource_name_, - NPRT(jcr->res.client->resource_name_), - jcr->res.write_storage ? jcr->res.write_storage->resource_name_ - : _("*None*"), - bstrutime(dt, sizeof(dt), jcr->sched_time), - jcr->JobPriority); + ua->SendMsg( + _("Run Archive Job\n" + "JobName: %s\n" + "FileSet: %s\n" + "Client: %s\n" + "Storage: %s\n" + "When: %s\n" + "Priority: %d\n"), + job->resource_name_, jcr->res.fileset->resource_name_, + NPRT(jcr->res.client->resource_name_), + jcr->res.write_storage ? jcr->res.write_storage->resource_name_ + : _("*None*"), + bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } jcr->setJobLevel(L_FULL); break; @@ -1304,19 +1306,19 @@ static bool DisplayJobParameters(UaContext* ua, : _("*None*"), bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } else { - ua->SendMsg(_("Run Consolidate Job\n" - "JobName: %s\n" - "FileSet: %s\n" - "Client: %s\n" - "Storage: %s\n" - "When: %s\n" - "Priority: %d\n"), - job->resource_name_, jcr->res.fileset->resource_name_, - NPRT(jcr->res.client->resource_name_), - jcr->res.write_storage ? jcr->res.write_storage->resource_name_ - : _("*None*"), - bstrutime(dt, sizeof(dt), jcr->sched_time), - jcr->JobPriority); + ua->SendMsg( + _("Run Consolidate Job\n" + "JobName: %s\n" + "FileSet: %s\n" + "Client: %s\n" + "Storage: %s\n" + "When: %s\n" + "Priority: %d\n"), + job->resource_name_, jcr->res.fileset->resource_name_, + NPRT(jcr->res.client->resource_name_), + jcr->res.write_storage ? jcr->res.write_storage->resource_name_ + : _("*None*"), + bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } jcr->setJobLevel(L_FULL); break; @@ -1343,11 +1345,13 @@ static bool DisplayJobParameters(UaContext* ua, "%s%s%s", job->resource_name_, JobLevelToString(jcr->getJobLevel()), jcr->res.client->resource_name_, jcr->backup_format, - jcr->res.fileset->resource_name_, NPRT(jcr->res.pool->resource_name_), + jcr->res.fileset->resource_name_, + NPRT(jcr->res.pool->resource_name_), is_virtual ? "NextPool: " : "", - is_virtual ? (jcr->res.next_pool ? jcr->res.next_pool->resource_name_ - : _("*None*")) - : "", + is_virtual + ? (jcr->res.next_pool ? jcr->res.next_pool->resource_name_ + : _("*None*")) + : "", is_virtual ? "\n" : "", jcr->res.write_storage ? jcr->res.write_storage->resource_name_ : _("*None*"), @@ -1371,11 +1375,13 @@ static bool DisplayJobParameters(UaContext* ua, "%s%s%s"), job->resource_name_, JobLevelToString(jcr->getJobLevel()), jcr->res.client->resource_name_, jcr->backup_format, - jcr->res.fileset->resource_name_, NPRT(jcr->res.pool->resource_name_), - jcr->res.pool_source, is_virtual ? "NextPool: " : "", - is_virtual ? (jcr->res.next_pool ? jcr->res.next_pool->resource_name_ - : _("*None*")) - : "", + jcr->res.fileset->resource_name_, + NPRT(jcr->res.pool->resource_name_), jcr->res.pool_source, + is_virtual ? "NextPool: " : "", + is_virtual + ? (jcr->res.next_pool ? jcr->res.next_pool->resource_name_ + : _("*None*")) + : "", is_virtual ? " (From " : "", is_virtual ? jcr->res.npool_source : "", is_virtual ? ")\n" : "", jcr->res.write_storage ? jcr->res.write_storage->resource_name_ @@ -1424,28 +1430,28 @@ static bool DisplayJobParameters(UaContext* ua, job->resource_name_, JobLevelToString(jcr->getJobLevel()), jcr->res.client->resource_name_, jcr->res.fileset->resource_name_, NPRT(jcr->res.pool->resource_name_), jcr->res.pool_source, - jcr->res.read_storage->resource_name_, jcr->res.rstore_source, Name, - verify_list, bstrutime(dt, sizeof(dt), jcr->sched_time), + jcr->res.read_storage->resource_name_, jcr->res.rstore_source, + Name, verify_list, bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->JobPriority); } else { - ua->SendMsg(_("Run Verify Job\n" - "JobName: %s\n" - "Level: %s\n" - "Client: %s\n" - "FileSet: %s\n" - "Pool: %s (From %s)\n" - "Storage: %s (From %s)\n" - "Verify Job: %s\n" - "Verify List: %s\n" - "When: %s\n" - "Priority: %d\n"), - job->resource_name_, JobLevelToString(jcr->getJobLevel()), - jcr->res.client->resource_name_, jcr->res.fileset->resource_name_, - NPRT(jcr->res.pool->resource_name_), jcr->res.pool_source, - jcr->res.read_storage->resource_name_, jcr->res.rstore_source, - Name, verify_list, - bstrutime(dt, sizeof(dt), jcr->sched_time), - jcr->JobPriority); + ua->SendMsg( + _("Run Verify Job\n" + "JobName: %s\n" + "Level: %s\n" + "Client: %s\n" + "FileSet: %s\n" + "Pool: %s (From %s)\n" + "Storage: %s (From %s)\n" + "Verify Job: %s\n" + "Verify List: %s\n" + "When: %s\n" + "Priority: %d\n"), + job->resource_name_, JobLevelToString(jcr->getJobLevel()), + jcr->res.client->resource_name_, jcr->res.fileset->resource_name_, + NPRT(jcr->res.pool->resource_name_), jcr->res.pool_source, + jcr->res.read_storage->resource_name_, jcr->res.rstore_source, + Name, verify_list, bstrutime(dt, sizeof(dt), jcr->sched_time), + jcr->JobPriority); } } break; @@ -1508,8 +1514,9 @@ static bool DisplayJobParameters(UaContext* ua, "Plugin Options: %s\n"), job->resource_name_, NPRT(jcr->RestoreBootstrap), jcr->RegexWhere ? jcr->RegexWhere : job->RegexWhere, - rc.replace, jcr->res.fileset->resource_name_, rc.client_name, - jcr->res.client->resource_name_, jcr->backup_format, + rc.replace, jcr->res.fileset->resource_name_, + rc.client_name, jcr->res.client->resource_name_, + jcr->backup_format, jcr->res.read_storage->resource_name_, bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->res.catalog->resource_name_, jcr->JobPriority, @@ -1559,8 +1566,9 @@ static bool DisplayJobParameters(UaContext* ua, "Plugin Options: %s\n"), job->resource_name_, NPRT(jcr->RestoreBootstrap), jcr->where ? jcr->where : NPRT(job->RestoreWhere), - rc.replace, jcr->res.fileset->resource_name_, rc.client_name, - jcr->res.client->resource_name_, jcr->backup_format, + rc.replace, jcr->res.fileset->resource_name_, + rc.client_name, jcr->res.client->resource_name_, + jcr->backup_format, jcr->res.read_storage->resource_name_, bstrutime(dt, sizeof(dt), jcr->sched_time), jcr->res.catalog->resource_name_, jcr->JobPriority, @@ -1594,8 +1602,8 @@ static bool DisplayJobParameters(UaContext* ua, "Catalog: %s\n" "Priority: %d\n" "Plugin Options: %s\n"), - rc.replace, jcr->res.client->resource_name_, jcr->backup_format, - jcr->res.read_storage->resource_name_, + rc.replace, jcr->res.client->resource_name_, + jcr->backup_format, jcr->res.read_storage->resource_name_, (jcr->RestoreJobId == 0) ? _("*None*") : edit_uint64(jcr->RestoreJobId, ec1), @@ -1629,9 +1637,11 @@ static bool DisplayJobParameters(UaContext* ua, "Catalog: %s\n" "Priority: %d\n", prt_type, job->resource_name_, NPRT(jcr->RestoreBootstrap), - jcr->res.read_storage ? jcr->res.read_storage->resource_name_ : _("*None*"), + jcr->res.read_storage ? jcr->res.read_storage->resource_name_ + : _("*None*"), NPRT(jcr->res.pool->resource_name_), - jcr->res.next_pool ? jcr->res.next_pool->resource_name_ : _("*None*"), + jcr->res.next_pool ? jcr->res.next_pool->resource_name_ + : _("*None*"), jcr->res.write_storage ? jcr->res.write_storage->resource_name_ : _("*None*"), (jcr->MigrateJobId == 0) ? _("*None*") @@ -1657,13 +1667,15 @@ static bool DisplayJobParameters(UaContext* ua, "Catalog: %s\n" "Priority: %d\n"), prt_type, job->resource_name_, NPRT(jcr->RestoreBootstrap), - jcr->res.read_storage ? jcr->res.read_storage->resource_name_ : _("*None*"), + jcr->res.read_storage ? jcr->res.read_storage->resource_name_ + : _("*None*"), jcr->res.rstore_source, NPRT(jcr->res.pool->resource_name_), jcr->res.pool_source, jcr->res.write_storage ? jcr->res.write_storage->resource_name_ : _("*None*"), jcr->res.wstore_source, - jcr->res.next_pool ? jcr->res.next_pool->resource_name_ : _("*None*"), + jcr->res.next_pool ? jcr->res.next_pool->resource_name_ + : _("*None*"), NPRT(jcr->res.npool_source), jcr->MigrateJobId == 0 ? _("*None*") : edit_uint64(jcr->MigrateJobId, ec1), @@ -2108,7 +2120,8 @@ static bool ScanCommandLineArguments(UaContext* ua, RunContext& rc) if (!rc.store->store) { ua->ErrorMsg(_("No storage specified.\n")); return false; - } else if (!ua->AclAccessOk(Storage_ACL, rc.store->store->resource_name_, true)) { + } else if (!ua->AclAccessOk(Storage_ACL, rc.store->store->resource_name_, + true)) { ua->ErrorMsg(_("No authorization. Storage \"%s\".\n"), rc.store->store->resource_name_); return false; @@ -2131,7 +2144,8 @@ static bool ScanCommandLineArguments(UaContext* ua, RunContext& rc) if (rc.client) { if (!ua->AclAccessOk(Client_ACL, rc.client->resource_name_, true)) { - ua->ErrorMsg(_("No authorization. Client \"%s\".\n"), rc.client->resource_name_); + ua->ErrorMsg(_("No authorization. Client \"%s\".\n"), + rc.client->resource_name_); return false; } else { Dmsg1(800, "Using client=%s\n", rc.client->resource_name_); @@ -2153,7 +2167,8 @@ static bool ScanCommandLineArguments(UaContext* ua, RunContext& rc) if (rc.client) { if (!ua->AclAccessOk(Client_ACL, rc.client->resource_name_, true)) { - ua->ErrorMsg(_("No authorization. Client \"%s\".\n"), rc.client->resource_name_); + ua->ErrorMsg(_("No authorization. Client \"%s\".\n"), + rc.client->resource_name_); return false; } else { Dmsg1(800, "Using restore client=%s\n", rc.client->resource_name_); diff --git a/core/src/dird/ua_status.cc b/core/src/dird/ua_status.cc index 9e8a71efe92..d491469142a 100644 --- a/core/src/dird/ua_status.cc +++ b/core/src/dird/ua_status.cc @@ -53,8 +53,6 @@ namespace directordaemon { -extern void* start_heap; - static void ListScheduledJobs(UaContext* ua); static void ListRunningJobs(UaContext* ua); static void ListTerminatedJobs(UaContext* ua); diff --git a/core/src/dird/ua_tree.cc b/core/src/dird/ua_tree.cc index 27291836782..179b40461e7 100644 --- a/core/src/dird/ua_tree.cc +++ b/core/src/dird/ua_tree.cc @@ -43,6 +43,7 @@ #include "dird/ua_input.h" #include "dird/ua_server.h" #include "lib/edit.h" +#include "lib/tree.h" #include "lib/util.h" namespace directordaemon { diff --git a/core/src/dird/ua_update.cc b/core/src/dird/ua_update.cc index 847f8a7d771..a99e7e89a84 100644 --- a/core/src/dird/ua_update.cc +++ b/core/src/dird/ua_update.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-2016 Bareos GmbH & Co. KG + 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 @@ -288,7 +288,6 @@ static void UpdateVolslot(UaContext* ua, char* val, MediaDbRecord* mr) { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); pr.PoolId = mr->PoolId; if (!ua->db->GetPoolRecord(ua->jcr, &pr)) { ua->ErrorMsg("%s", ua->db->strerror()); @@ -325,7 +324,6 @@ void UpdateVolPool(UaContext* ua, PoolMem query(PM_MESSAGE); char ed1[50], ed2[50]; - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, val, sizeof(pr.Name)); if (!GetPoolDbr(ua, &pr)) { return; } mr->PoolId = pr.PoolId; /* set new PoolId */ @@ -363,7 +361,6 @@ void UpdateVolRecyclepool(UaContext* ua, char* val, MediaDbRecord* mr) /* * If a pool name is given, look up the PoolId */ - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, val, sizeof(pr.Name)); if (!GetPoolDbr(ua, &pr, NT_("recyclepool"))) { return; } mr->RecyclePoolId = pr.PoolId; /* get the PoolId */ @@ -396,7 +393,6 @@ void UpdateVolStorage(UaContext* ua, char* val, MediaDbRecord* mr) PoolMem query(PM_MESSAGE); char ed1[50], ed2[50]; - memset(&sr, 0, sizeof(sr)); bstrncpy(sr.Name, val, sizeof(sr.Name)); if (!GetStorageDbr(ua, &sr)) { return; } mr->StorageId = sr.StorageId; /* set new StorageId */ @@ -418,7 +414,6 @@ static void UpdateVolFromPool(UaContext* ua, MediaDbRecord* mr) { PoolDbRecord pr; - memset(&pr, 0, sizeof(pr)); pr.PoolId = mr->PoolId; if (!ua->db->GetPoolRecord(ua->jcr, &pr) || !ua->AclAccessOk(Pool_ACL, pr.Name, true)) { @@ -441,9 +436,6 @@ static void UpdateAllVolsFromPool(UaContext* ua, const char* pool_name) PoolDbRecord pr; MediaDbRecord mr; - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - bstrncpy(pr.Name, pool_name, sizeof(pr.Name)); if (!GetPoolDbr(ua, &pr)) { return; } SetPoolDbrDefaultsInMediaDbr(&mr, &pr); @@ -464,9 +456,6 @@ static void UpdateAllVols(UaContext* ua) PoolDbRecord pr; MediaDbRecord mr; - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - if (!ua->db->GetPoolIds(ua->jcr, &num_pools, &ids)) { ua->ErrorMsg(_("Error obtaining pool ids. ERR=%s\n"), ua->db->strerror()); return; @@ -572,9 +561,6 @@ static bool UpdateVolume(UaContext* ua) PoolDbRecord pr; MediaDbRecord mr; - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - if ((j = FindArgWithValue(ua, kw[i])) > 0) { /* If all from pool don't select a media record */ if (i != AllFromPool && !SelectMediaDbr(ua, &mr)) { return false; } @@ -648,10 +634,6 @@ static bool UpdateVolume(UaContext* ua) MediaDbRecord mr; StorageDbRecord sr; - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); - memset(&sr, 0, sizeof(sr)); - StartPrompt(ua, _("Parameters to modify:\n")); AddPrompt(ua, _("Volume Status")); /* 0 */ AddPrompt(ua, _("Volume Retention Period")); /* 1 */ @@ -903,7 +885,6 @@ static bool UpdatePool(UaContext* ua) pool = get_pool_resource(ua); if (!pool) { return false; } - memset(&pr, 0, sizeof(pr)); bstrncpy(pr.Name, pool->resource_name_, sizeof(pr.Name)); if (!GetPoolDbr(ua, &pr)) { return false; } @@ -915,7 +896,7 @@ static bool UpdatePool(UaContext* ua) ua->ErrorMsg(_("UpdatePoolRecord returned %d. ERR=%s\n"), id, ua->db->strerror()); } - ua->db->FillQuery(query, BareosDb::SQL_QUERY_list_pool, + ua->db->FillQuery(query, BareosDb::SQL_QUERY::list_pool, edit_int64(pr.PoolId, ed1)); ua->db->ListSqlQuery(ua->jcr, query.c_str(), ua->send, HORZ_LIST, true); ua->InfoMsg(_("Pool DB record updated from resource.\n")); @@ -1042,6 +1023,7 @@ static void UpdateSlots(UaContext* ua) bool have_enabled; int i; + if (!OpenClientDb(ua)) { return; } store.store = get_storage_resource(ua, true, true); if (!store.store) { return; } @@ -1086,7 +1068,6 @@ static void UpdateSlots(UaContext* ua) /* * Walk through the list updating the media records */ - memset(&mr, 0, sizeof(mr)); foreach_dlist (vl, vol_list->contents) { if (vl->bareos_slot_number > max_slots) { ua->WarningMsg(_("Slot %d greater than max %d ignored.\n"), @@ -1176,26 +1157,26 @@ static void UpdateSlots(UaContext* ua) } DbUnlock(ua->db); } + { + MediaDbRecord mr; + mr.InChanger = 1; + SetStorageidInMr(store.store, &mr); - memset(&mr, 0, sizeof(mr)); - mr.InChanger = 1; - SetStorageidInMr(store.store, &mr); - - /* - * Any slot not visited gets it Inchanger flag reset. - */ - DbLock(ua->db); - for (i = 1; i <= max_slots; i++) { - if (BitIsSet(i - 1, slot_list)) { - /* - * Set InChanger to zero for this Slot - */ - mr.Slot = i; - ua->db->MakeInchangerUnique(ua->jcr, &mr); + /* + * Any slot not visited gets it Inchanger flag reset. + */ + DbLock(ua->db); + for (i = 1; i <= max_slots; i++) { + if (BitIsSet(i - 1, slot_list)) { + /* + * Set InChanger to zero for this Slot + */ + mr.Slot = i; + ua->db->MakeInchangerUnique(ua->jcr, &mr); + } } + DbUnlock(ua->db); } - DbUnlock(ua->db); - bail_out: if (vol_list) { StorageReleaseVolList(store.store, vol_list); } free(slot_list); @@ -1221,7 +1202,6 @@ void UpdateSlotsFromVolList(UaContext* ua, char* slot_list) { vol_list_t* vl; - MediaDbRecord mr; if (!OpenClientDb(ua)) { return; } @@ -1247,7 +1227,7 @@ void UpdateSlotsFromVolList(UaContext* ua, /* * Set InChanger to zero for this Slot */ - memset(&mr, 0, sizeof(mr)); + MediaDbRecord mr; mr.Slot = vl->bareos_slot_number; mr.InChanger = 1; mr.MediaId = 0; /* Get by VolumeName */ @@ -1329,7 +1309,6 @@ void UpdateInchangerForExport(UaContext* ua, char* slot_list) { vol_list_t* vl; - MediaDbRecord mr; if (!OpenClientDb(ua)) { return; } @@ -1355,7 +1334,7 @@ void UpdateInchangerForExport(UaContext* ua, /* * Set InChanger to zero for this Slot */ - memset(&mr, 0, sizeof(mr)); + MediaDbRecord mr; mr.Slot = vl->bareos_slot_number; mr.InChanger = 1; mr.MediaId = 0; /* Get by VolumeName */ diff --git a/core/src/dird/vbackup.cc b/core/src/dird/vbackup.cc index 7e75c3d9a4c..161a558880b 100644 --- a/core/src/dird/vbackup.cc +++ b/core/src/dird/vbackup.cc @@ -3,7 +3,7 @@ Copyright (C) 2008-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -229,8 +229,7 @@ bool DoNativeVbackup(JobControlRecord* jcr) */ p = strchr(jobids, ','); /* find oldest jobid */ if (p) { *p = '\0'; } - - memset(&jcr->previous_jr, 0, sizeof(jcr->previous_jr)); + jcr->previous_jr = JobDbRecord{}; jcr->previous_jr.JobId = str_to_int64(jobids); Dmsg1(10, "Previous JobId=%s\n", jobids); @@ -262,7 +261,7 @@ bool DoNativeVbackup(JobControlRecord* jcr) p = jobids; } - memset(&jcr->previous_jr, 0, sizeof(jcr->previous_jr)); + jcr->previous_jr = JobDbRecord{}; jcr->previous_jr.JobId = str_to_int64(p); Dmsg1(10, "Previous JobId=%s\n", p); @@ -391,7 +390,6 @@ void NativeVbackupCleanup(JobControlRecord* jcr, int TermCode, int JobLevel) PoolMem query(PM_MESSAGE); Dmsg2(100, "Enter backup_cleanup %d %c\n", TermCode, TermCode); - memset(&cr, 0, sizeof(cr)); switch (jcr->JobStatus) { case JS_Terminated: diff --git a/core/src/dird/verify.cc b/core/src/dird/verify.cc index e32759a116f..04305f9e3e2 100644 --- a/core/src/dird/verify.cc +++ b/core/src/dird/verify.cc @@ -614,7 +614,6 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId) int do_Digest = CRYPTO_DIGEST_NONE; int32_t file_index = 0; - memset(&fdbr, 0, sizeof(fdbr)); fd = jcr->file_bsock; fdbr.JobId = JobId; jcr->FileIndex = 0; diff --git a/core/src/filed/dir_cmd.cc b/core/src/filed/dir_cmd.cc index d7f0cdcd275..7a9d2785b64 100644 --- a/core/src/filed/dir_cmd.cc +++ b/core/src/filed/dir_cmd.cc @@ -60,7 +60,10 @@ #if defined(WIN32_VSS) #include "win32/findlib/win32.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop #endif namespace filedaemon { diff --git a/core/src/filed/fd_plugins.cc b/core/src/filed/fd_plugins.cc index 141579bebdc..bcab309f629 100644 --- a/core/src/filed/fd_plugins.cc +++ b/core/src/filed/fd_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-2018 Bareos GmbH & Co. KG + 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 @@ -346,7 +346,6 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, { bEvent event; char* name = NULL; - int i; int len = 0; bool call_if_canceled = false; restore_object_pkt* rop; @@ -429,6 +428,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, * See if we need to trigger the loaded plugins in reverse order. */ if (reverse) { + int i{}; foreach_alist_rindex (i, ctx, plugin_ctx_list) { if (!for_thIsPlugin(ctx->plugin, name, len)) { Dmsg2(debuglevel, "Not for this plugin name=%s NULL=%d\n", name, @@ -442,6 +442,7 @@ bRC GeneratePluginEvent(JobControlRecord* jcr, } } } else { + int i{}; foreach_alist_index (i, ctx, plugin_ctx_list) { if (!for_thIsPlugin(ctx->plugin, name, len)) { Dmsg2(debuglevel, "Not for this plugin name=%s NULL=%d\n", name, @@ -1674,7 +1675,6 @@ static void DumpFdPlugins(FILE* fp) { DumpPlugins(fd_plugin_list, fp); } void LoadFdPlugins(const char* plugin_dir, alist* plugin_names) { Plugin* plugin; - int i; if (!plugin_dir) { Dmsg0(debuglevel, "plugin dir is NULL\n"); @@ -1707,6 +1707,7 @@ void LoadFdPlugins(const char* plugin_dir, alist* plugin_names) /* * Verify that the plugin is acceptable, and print information about it. */ + int i{0}; foreach_alist_index (i, plugin, fd_plugin_list) { Dmsg1(debuglevel, "Loaded plugin: %s\n", plugin->file); } @@ -1805,7 +1806,6 @@ static inline bpContext* instantiate_plugin(JobControlRecord* jcr, */ void NewPlugins(JobControlRecord* jcr) { - int i, num; Plugin* plugin; if (!fd_plugin_list) { @@ -1815,6 +1815,7 @@ void NewPlugins(JobControlRecord* jcr) if (jcr->IsJobCanceled()) { return; } + int num; num = fd_plugin_list->size(); if (num == 0) { Dmsg0(debuglevel, "No plugins loaded\n"); @@ -1824,6 +1825,8 @@ void NewPlugins(JobControlRecord* jcr) 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); + + int i{}; foreach_alist_index (i, plugin, fd_plugin_list) { /* * Start a new instance of each plugin diff --git a/core/src/filed/fd_plugins.h b/core/src/filed/fd_plugins.h index b5aab743a33..41e78c0b320 100644 --- a/core/src/filed/fd_plugins.h +++ b/core/src/filed/fd_plugins.h @@ -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-2016 Bareos GmbH & Co. KG + 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 @@ -53,7 +53,10 @@ #include #ifdef HAVE_WIN32 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop #endif #ifdef FILE_DAEMON diff --git a/core/src/filed/filed.cc b/core/src/filed/filed.cc index 6bac1ee0bb5..68ddbcf8677 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-2018 Bareos GmbH & Co. KG + 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 @@ -103,7 +103,6 @@ int main(int argc, char* argv[]) char* uid = NULL; char* gid = NULL; - start_heap = sbrk(0); setlocale(LC_ALL, ""); bindtextdomain("bareos", LOCALEDIR); textdomain("bareos"); diff --git a/core/src/filed/filed.h b/core/src/filed/filed.h index 7f7647c8b85..7c8c37b6af3 100644 --- a/core/src/filed/filed.h +++ b/core/src/filed/filed.h @@ -3,7 +3,7 @@ Copyright (C) 2001-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2014 Bareos GmbH & Co. KG + 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 @@ -32,7 +32,10 @@ #define FILE_DAEMON 1 #include "filed_conf.h" #ifdef HAVE_WIN32 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop #endif #include "include/jcr.h" #include "lib/breg.h" diff --git a/core/src/filed/filed_globals.cc b/core/src/filed/filed_globals.cc index 36c2125624d..ff29876274c 100644 --- a/core/src/filed/filed_globals.cc +++ b/core/src/filed/filed_globals.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-2018 Bareos GmbH & Co. KG + 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 @@ -33,7 +33,6 @@ bool restore_only_mode = false; struct ClientResource; ClientResource* me = nullptr; -void* start_heap = nullptr; char* configfile = nullptr; } /* namespace filedaemon */ diff --git a/core/src/filed/fileset.cc b/core/src/filed/fileset.cc index 979d8cd86c9..2ba249954a1 100644 --- a/core/src/filed/fileset.cc +++ b/core/src/filed/fileset.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-2016 Bareos GmbH & Co. KG + 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 @@ -79,7 +79,8 @@ bool InitFileset(JobControlRecord* jcr) ff = jcr->ff; if (ff->fileset) { return false; } fileset = (findFILESET*)malloc(sizeof(findFILESET)); - memset(fileset, 0, sizeof(findFILESET)); + *fileset = findFILESET{}; + ff->fileset = fileset; fileset->state = state_none; fileset->include_list.init(1, true); diff --git a/core/src/filed/restore.cc b/core/src/filed/restore.cc index 0fdbe332365..a67e026151f 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-2018 Bareos GmbH & Co. KG + 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 @@ -427,7 +427,6 @@ void DoRestore(JobControlRecord* jcr) int non_support_crypto = 0; int non_support_xattr = 0; - memset(&rctx, 0, sizeof(rctx)); rctx.jcr = jcr; sd = jcr->store_bsock; diff --git a/core/src/filed/restore.h b/core/src/filed/restore.h index 3c3e84c2138..f4f3cc7cc7a 100644 --- a/core/src/filed/restore.h +++ b/core/src/filed/restore.h @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2018 Bareos GmbH & Co. KG + Copyright (C) 2018-2019 Bareos GmbH & Co. KG Copyright (C) 2009 Free Software Foundation Europe e.V. This program is Free Software; you can redistribute it and/or @@ -38,39 +38,38 @@ struct DelayedDataStream { struct RestoreCipherContext { CIPHER_CONTEXT* cipher; uint32_t block_size; - POOLMEM* buf; /* Pointer to descryption buffer */ int32_t buf_len; /* Count of bytes currently in buf */ int32_t packet_len; /* Total bytes in packet */ }; +/* clang-format off */ struct r_ctx { - JobControlRecord* jcr; - int32_t stream; /* stream less new bits */ - int32_t prev_stream; /* previous stream */ - int32_t full_stream; /* full stream including new bits */ - int32_t comp_stream; /* last compressed stream found. needed only to restore - encrypted compressed backup */ - BareosWinFilePacket bfd; /* File content */ - uint64_t fileAddr; /* file write address */ - uint32_t size; /* Size of file */ - char flags[FOPTS_BYTES]; /* Options for ExtractData() */ - BareosWinFilePacket forkbfd; /* Alternative data stream */ - uint64_t fork_addr; /* Write address for alternative stream */ - int64_t fork_size; /* Size of alternate stream */ - char fork_flags[FOPTS_BYTES]; /* Options for ExtractData() */ - int32_t type; /* file type FT_ */ - Attributes* attr; /* Pointer to attributes */ - bool extract; /* set when extracting */ - alist* delayed_streams; /* streams that should be restored as last */ - - SIGNATURE* sig; /* Cryptographic signature (if any) for file */ - CRYPTO_SESSION* cs; /* Cryptographic session data (if any) for file */ - RestoreCipherContext - cipher_ctx; /* Cryptographic restore context (if any) for file */ - RestoreCipherContext fork_cipher_ctx; /* Cryptographic restore context (if - any) for alternative stream */ + JobControlRecord* jcr{nullptr}; + int32_t stream{0}; /* stream less new bits */ + int32_t prev_stream{0}; /* previous stream */ + int32_t full_stream{0}; /* full stream including new bits */ + int32_t comp_stream{0}; /* last compressed stream found. needed only to + restore encrypted compressed backup */ + BareosWinFilePacket bfd; /* File content */ + uint64_t fileAddr{0}; /* file write address */ + uint32_t size{0}; /* Size of file */ + char flags[FOPTS_BYTES]{}; /* Options for ExtractData() */ + BareosWinFilePacket forkbfd; /* Alternative data stream */ + uint64_t fork_addr{0}; /* Write address for alternative stream */ + int64_t fork_size{0}; /* Size of alternate stream */ + char fork_flags[FOPTS_BYTES]{0}; /* Options for ExtractData() */ + 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 */ + 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 */ + RestoreCipherContext fork_cipher_ctx{0}; /* Cryptographic restore context (if any) + for alternative stream */ }; +/* clang-format on */ void DoRestore(JobControlRecord* jcr); void FreeSession(r_ctx& rctx); diff --git a/core/src/filed/status.cc b/core/src/filed/status.cc index 881dbd71130..361b62ac582 100644 --- a/core/src/filed/status.cc +++ b/core/src/filed/status.cc @@ -60,7 +60,10 @@ static char DotStatusJob[] = "JobId=%d JobStatus=%c JobErrors=%d\n"; static int privs = 0; #endif #ifdef WIN32_VSS +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop #define VSS " VSS" #else #define VSS "" @@ -81,7 +84,7 @@ static void ListStatusHeader(StatusPacket* sp) int len; char dt[MAX_TIME_LENGTH]; PoolMem msg(PM_MESSAGE); - char b1[32], b2[32], b3[32], b4[32], b5[35]; + char b1[32]; #if defined(HAVE_WIN32) char buf[300]; #endif diff --git a/core/src/findlib/bfile.cc b/core/src/findlib/bfile.cc index 83ea9a9073c..15d219500f6 100644 --- a/core/src/findlib/bfile.cc +++ b/core/src/findlib/bfile.cc @@ -3,7 +3,7 @@ Copyright (C) 2003-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -983,11 +983,7 @@ boffset_t blseek(BareosWinFilePacket* bfd, boffset_t offset, int whence) * * =============================================================== */ -void binit(BareosWinFilePacket* bfd) -{ - memset(bfd, 0, sizeof(BareosWinFilePacket)); - bfd->fid = -1; -} +void binit(BareosWinFilePacket* bfd) { bfd->fid = -1; } bool have_win32_api() { return false; /* no can do */ } diff --git a/core/src/findlib/bfile.h b/core/src/findlib/bfile.h index 12307e4ab7b..12f687544c5 100644 --- a/core/src/findlib/bfile.h +++ b/core/src/findlib/bfile.h @@ -3,7 +3,7 @@ Copyright (C) 2003-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -41,16 +41,16 @@ #define WIN32_BACKUP_DATA 1 typedef struct _BWIN32_STREAM_ID { - int32_t dwStreamId = 0; - int32_t dwStreamAttributes = 0; - int64_t Size = 0; - int32_t dwStreamNameSize = 0; + int32_t dwStreamId; + int32_t dwStreamAttributes; + int64_t Size; + int32_t dwStreamNameSize; } BWIN32_STREAM_ID, *LPBWIN32_STREAM_ID; typedef struct _PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT { - int64_t liNextHeader = 0; - bool bIsInData = false; + int64_t liNextHeader; + bool bIsInData; BWIN32_STREAM_ID header_stream; } PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT; @@ -72,11 +72,11 @@ enum /* In bfile.c */ /* Basic Win32 low level I/O file packet */ +/* clang-format off */ struct BareosWinFilePacket { - bool use_backup_api = false; /**< set if using BackupRead/Write */ - bool encrypted = - false; /**< set if using ReadEncryptedFileRaw/WriteEncryptedFileRaw */ - int mode = 0; /**< set if file is open */ + bool use_backup_api = false; /**< set if using BackupRead/Write */ + bool encrypted = false; /**< set if using ReadEncryptedFileRaw/WriteEncryptedFileRaw */ + int mode = BF_CLOSED; /**< set if file is open */ HANDLE fh = INVALID_HANDLE_VALUE; /**< Win32 file handle */ int fid = 0; /**< fd if doing Unix style */ LPVOID lpContext = nullptr; /**< BackupRead/Write context */ @@ -87,13 +87,13 @@ struct BareosWinFilePacket { int BErrNo = 0; /**< errno */ boffset_t offset = 0; /**< Delta offset */ JobControlRecord* jcr = nullptr; /**< jcr for editing job codes */ - PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT - win32DecompContext; /**< context for decomposition of win32 backup streams */ - int use_backup_decomp = - 0; /**< set if using BackupRead Stream Decomposition */ + PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT win32DecompContext{0}; /**< context for decomposition + of win32 backup streams */ + int use_backup_decomp = 0; /**< set if using BackupRead Stream Decomposition */ bool reparse_point = false; /**< set if reparse point */ bool cmd_plugin = false; /**< set if we have a command plugin */ }; +/* clang-format on */ HANDLE BgetHandle(BareosWinFilePacket* bfd); @@ -107,21 +107,21 @@ HANDLE BgetHandle(BareosWinFilePacket* bfd); */ /* Basic Unix low level I/O file packet */ +/* clang-format off */ struct BareosWinFilePacket { - int fid = 0; /**< file id on Unix */ - int flags_ = 0; /**< open flags */ - int BErrNo = 0; /**< errno */ - int32_t lerror = 0; /**< not used - simplies Win32 builds */ - boffset_t offset{0}; /**< Delta offset */ - JobControlRecord* jcr = nullptr; /**< jcr for editing job codes */ - PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT - win32DecompContext; /**< context for decomposition of win32 backup streams - */ - int use_backup_decomp = - 0; /**< set if using BackupRead Stream Decomposition */ - bool reparse_point = false; /**< not used in Unix */ - bool cmd_plugin = false; /**< set if we have a command plugin */ + int fid{0}; /**< file id on Unix */ + int flags_{0}; /**< open flags */ + int BErrNo{0}; /**< errno */ + int32_t lerror{0}; /**< not used - simplies Win32 builds */ + boffset_t offset{0}; /**< Delta offset */ + JobControlRecord* jcr{nullptr}; /**< jcr for editing job codes */ + PROCESS_WIN32_BACKUPAPIBLOCK_CONTEXT win32DecompContext{0}; /**< context for decomposition + of win32 backup streams */ + int use_backup_decomp{0}; /**< set if using BackupRead Stream Decomposition */ + bool reparse_point{false}; /**< not used in Unix */ + bool cmd_plugin{false}; /**< set if we have a command plugin */ }; +/* clang-format on */ #endif diff --git a/core/src/findlib/find.cc b/core/src/findlib/find.cc index 0e8f0d76afc..75519c22335 100644 --- a/core/src/findlib/find.cc +++ b/core/src/findlib/find.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-2016 Bareos GmbH & Co. KG + 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 @@ -57,7 +57,8 @@ FindFilesPacket* init_find_files() FindFilesPacket* ff; ff = (FindFilesPacket*)malloc(sizeof(FindFilesPacket)); - memset(ff, 0, sizeof(FindFilesPacket)); + FindFilesPacket empty_ff; + *ff = empty_ff; ff->sys_fname = GetPoolMemory(PM_FNAME); @@ -471,7 +472,7 @@ findIncludeExcludeItem* allocate_new_incexe(void) findIncludeExcludeItem* incexe; incexe = (findIncludeExcludeItem*)malloc(sizeof(findIncludeExcludeItem)); - memset(incexe, 0, sizeof(findIncludeExcludeItem)); + *incexe = findIncludeExcludeItem{}; incexe->opts_list.init(1, true); incexe->name_list.init(); incexe->plugin_list.init(); @@ -545,7 +546,7 @@ findFOPTS* start_options(FindFilesPacket* ff) if (state != state_options) { ff->fileset->state = state_options; findFOPTS* fo = (findFOPTS*)malloc(sizeof(findFOPTS)); - memset(fo, 0, sizeof(findFOPTS)); + *fo = findFOPTS{}; fo->regex.init(1, true); fo->regexdir.init(1, true); fo->regexfile.init(1, true); @@ -571,7 +572,7 @@ void NewOptions(FindFilesPacket* ff, findIncludeExcludeItem* incexe) findFOPTS* fo; fo = (findFOPTS*)malloc(sizeof(findFOPTS)); - memset(fo, 0, sizeof(findFOPTS)); + *fo = findFOPTS{}; fo->regex.init(1, true); fo->regexdir.init(1, true); fo->regexfile.init(1, true); diff --git a/core/src/findlib/find.h b/core/src/findlib/find.h index 1957d250480..e691b30d6a8 100644 --- a/core/src/findlib/find.h +++ b/core/src/findlib/find.h @@ -3,7 +3,7 @@ Copyright (C) 2001-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -102,9 +102,9 @@ typedef enum } b_sz_match_type; struct s_sz_matching { - b_sz_match_type type; - uint64_t begin_size; - uint64_t end_size; + b_sz_match_type type{size_match_none}; + uint64_t begin_size{}; + uint64_t end_size{}; }; struct s_included_file { @@ -133,28 +133,29 @@ struct s_excluded_file { * File options structure */ struct findFOPTS { - char flags[FOPTS_BYTES]; /**< Backup options */ - uint32_t Encryption_cipher; /**< Encryption cipher forced by fileset */ - uint32_t Compress_algo; /**< Compression algorithm. 4 letters stored as an - integer */ - int Compress_level; /**< Compression level */ - int StripPath; /**< Strip path count */ - struct s_sz_matching* size_match; /**< Perform size matching ? */ - b_fileset_shadow_type shadow_type; /**< Perform fileset shadowing check ? */ - char VerifyOpts[MAX_OPTS]; /**< Verify options */ - 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 */ + char flags[FOPTS_BYTES]{}; /**< Backup options */ + uint32_t Encryption_cipher{}; /**< Encryption cipher forced by fileset */ + uint32_t Compress_algo{}; /**< Compression algorithm. 4 letters stored as an + integer */ + int Compress_level{}; /**< Compression level */ + int StripPath{}; /**< Strip path count */ + struct s_sz_matching* size_match; /**< Perform size matching ? */ + b_fileset_shadow_type shadow_type{ + check_shadow_none}; /**< Perform fileset shadowing check ? */ + char VerifyOpts[MAX_OPTS]{}; /**< Verify options */ + 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 */ }; /** @@ -182,9 +183,9 @@ struct findFILESET { * OSX resource fork. */ struct HfsPlusInfo { - unsigned long length; /**< Mandatory field */ - char fndrinfo[32]; /**< Finder Info */ - off_t rsrclength; /**< Size of resource fork */ + unsigned long length{0}; /**< Mandatory field */ + char fndrinfo[32]{}; /**< Finder Info */ + off_t rsrclength{0}; /**< Size of resource fork */ }; /** @@ -209,82 +210,83 @@ struct CurLink { * Definition of the FindFiles packet passed as the * first argument to the FindFiles callback subroutine. */ +/* clang-format off */ struct FindFilesPacket { - char* top_fname; /**< Full filename before descending */ - char* fname; /**< Full filename */ - char* link; /**< Link if file linked */ - char* object_name; /**< Object name */ - char* object; /**< Restore object */ - char* plugin; /**< Current Options{Plugin=} name */ - POOLMEM* sys_fname; /**< System filename */ - POOLMEM* fname_save; /**< Save when stripping path */ - POOLMEM* link_save; /**< Save when stripping path */ - POOLMEM* ignoredir_fname; /**< Used to ignore directories */ - char* digest; /**< Set to file digest when the file is a hardlink */ - struct stat statp; /**< Stat packet */ - uint32_t digest_len; /**< Set to the digest len when the file is a hardlink*/ - int32_t digest_stream; /**< Set to digest type when the file is hardlink */ - int32_t FileIndex; /**< FileIndex of this file */ - int32_t LinkFI; /**< FileIndex of main hard linked file */ - int32_t delta_seq; /**< Delta Sequence number */ - int32_t object_index; /**< Object index */ - int32_t object_len; /**< Object length */ - int32_t object_compression; /**< Type of compression for object */ - int type; /**< FT_ type from above */ - int ff_errno; /**< Errno */ - BareosWinFilePacket bfd; /**< Bareos file descriptor */ - time_t save_time; /**< Start of incremental time */ - bool accurate_found; /**< Found in the accurate hash (valid after - CheckChanges()) */ - bool dereference; /**< Follow links (not implemented) */ - bool null_output_device; /**< Using null output device */ - bool incremental; /**< Incremental save */ - bool no_read; /**< Do not read this file when using Plugin */ - char VerifyOpts[MAX_OPTS]; - char AccurateOpts[MAX_OPTS]; - char BaseJobOpts[MAX_OPTS]; - struct s_included_file* included_files_list; - struct s_excluded_file* excluded_files_list; - struct s_excluded_file* excluded_paths_list; - findFILESET* fileset; + char* top_fname{nullptr}; /**< Full filename before descending */ + char* fname{nullptr}; /**< Full filename */ + char* link{nullptr}; /**< Link if file linked */ + char* object_name{nullptr}; /**< Object name */ + char* object{nullptr}; /**< Restore object */ + char* plugin{nullptr}; /**< Current Options{Plugin=} name */ + POOLMEM* sys_fname{nullptr}; /**< System filename */ + POOLMEM* fname_save{nullptr}; /**< Save when stripping path */ + POOLMEM* link_save{nullptr}; /**< Save when stripping path */ + POOLMEM* ignoredir_fname{nullptr}; /**< Used to ignore directories */ + char* digest{nullptr}; /**< Set to file digest when the file is a hardlink */ + struct stat statp{}; /**< Stat packet */ + uint32_t digest_len{0}; /**< Set to the digest len when the file is a hardlink*/ + int32_t digest_stream{0}; /**< Set to digest type when the file is hardlink */ + int32_t FileIndex{0}; /**< FileIndex of this file */ + int32_t LinkFI{0}; /**< FileIndex of main hard linked file */ + int32_t delta_seq{0}; /**< Delta Sequence number */ + int32_t object_index{0}; /**< Object index */ + int32_t object_len{0}; /**< Object length */ + int32_t object_compression{0}; /**< Type of compression for object */ + int type{0}; /**< FT_ type from above */ + int ff_errno{0}; /**< Errno */ + BareosWinFilePacket bfd; /**< Bareos file descriptor */ + time_t save_time{0}; /**< Start of incremental time */ + bool accurate_found{false}; /**< Found in the accurate hash (valid after + CheckChanges()) */ + bool dereference{false}; /**< Follow links (not implemented) */ + bool null_output_device{false}; /**< Using null output device */ + bool incremental{false}; /**< Incremental save */ + bool no_read{false}; /**< Do not read this file when using Plugin */ + char VerifyOpts[MAX_OPTS]{}; + char AccurateOpts[MAX_OPTS]{}; + char BaseJobOpts[MAX_OPTS]{}; + struct s_included_file* included_files_list{nullptr}; + struct s_excluded_file* excluded_files_list{nullptr}; + struct s_excluded_file* excluded_paths_list{nullptr}; + findFILESET* fileset{nullptr}; int (*FileSave)(JobControlRecord*, FindFilesPacket*, - bool); /**< User's callback */ + bool){}; /**< User's callback */ int (*PluginSave)(JobControlRecord*, FindFilesPacket*, - bool); /**< User's callback */ + bool){}; /**< User's callback */ bool (*CheckFct)( JobControlRecord*, - FindFilesPacket*); /**< Optionnal user fct to check file changes */ + FindFilesPacket*){}; /**< Optional user fct to check file changes */ /* * Values set by AcceptFile while processing Options */ - char flags[FOPTS_BYTES]; /**< Backup options */ - uint32_t Compress_algo; /**< Compression algorithm. 4 letters stored as an - integer */ - int Compress_level; /**< Compression level */ - int StripPath; /**< Strip path count */ - struct s_sz_matching* size_match; /**< Perform size matching ? */ - bool cmd_plugin; /**< Set if we have a command plugin */ - bool opt_plugin; /**< Set if we have an option plugin */ - alist fstypes; /**< Allowed file system types */ - alist drivetypes; /**< Allowed drive types */ + char flags[FOPTS_BYTES]{}; /**< Backup options */ + uint32_t Compress_algo{0}; /**< Compression algorithm. 4 letters stored as an integer */ + int Compress_level{0}; /**< Compression level */ + int StripPath{0}; /**< Strip path count */ + 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 */ /* * List of all hard linked files found */ - htable* linkhash; /**< Hard linked files */ - struct CurLink* linked; /**< Set if this file is hard linked */ + htable* linkhash{nullptr}; /**< Hard linked files */ + struct CurLink* linked{nullptr}; /**< Set if this file is hard linked */ /* * Darwin specific things. * To avoid clutter, we always include rsrc_bfd and volhas_attrlist. */ BareosWinFilePacket rsrc_bfd; /**< Fd for resource forks */ - bool volhas_attrlist; /**< Volume supports getattrlist() */ - struct HfsPlusInfo hfsinfo; /**< Finder Info and resource fork size */ + bool volhas_attrlist{false}; /**< Volume supports getattrlist() */ + HfsPlusInfo hfsinfo; /**< Finder Info and resource fork size */ }; +/* clang-format on */ FindFilesPacket* init_find_files(); void SetFindOptions(FindFilesPacket* ff, bool incremental, time_t mtime); diff --git a/core/src/findlib/find_one.cc b/core/src/findlib/find_one.cc index bb32b6f88c5..53b05aa8d54 100644 --- a/core/src/findlib/find_one.cc +++ b/core/src/findlib/find_one.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-2016 Bareos GmbH & Co. KG + 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 @@ -61,7 +61,8 @@ static inline FindFilesPacket* new_dir_ff_pkt(FindFilesPacket* ff_pkt) FindFilesPacket* dir_ff_pkt; dir_ff_pkt = (FindFilesPacket*)malloc(sizeof(FindFilesPacket)); - memcpy(dir_ff_pkt, ff_pkt, sizeof(FindFilesPacket)); + + *dir_ff_pkt = *ff_pkt; dir_ff_pkt->fname = strdup(ff_pkt->fname); dir_ff_pkt->link = strdup(ff_pkt->link); dir_ff_pkt->sys_fname = GetPoolMemory(PM_FNAME); diff --git a/core/src/findlib/match.cc b/core/src/findlib/match.cc index bf7e089c664..54b651c06ec 100644 --- a/core/src/findlib/match.cc +++ b/core/src/findlib/match.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2008 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -567,7 +567,7 @@ bool ParseSizeMatch(const char* size_match_pattern, /* * Empty the matching arguments. */ - memset(size_matching, 0, sizeof(struct s_sz_matching)); + *size_matching = s_sz_matching{}; /* * See if the size is a range e.g. there is a - in the diff --git a/core/src/findlib/xattr.cc b/core/src/findlib/xattr.cc index 66088613942..3b8d6c1b803 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-2016 Bareos GmbH & Co. KG + 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 @@ -1371,7 +1371,7 @@ static BxattrExitCode generic_parse_xattr_streams(JobControlRecord* jcr, char* content, uint32_t content_length) { - xattr_t* current_xattr; + xattr_t* current_xattr = nullptr; alist* xattr_value_list; BxattrExitCode retval = BxattrExitCode::kError; diff --git a/core/src/include/baconfig.h b/core/src/include/baconfig.h index e8c426dc0a1..bf9c3a7ca17 100644 --- a/core/src/include/baconfig.h +++ b/core/src/include/baconfig.h @@ -100,8 +100,6 @@ void InitWinAPIWrapper(); #define OSDependentInit() InitWinAPIWrapper() -#define sbrk(x) 0 - #define ClearThreadId(x) memset(&(x), 0, sizeof(x)) #else /* HAVE_WIN32 */ diff --git a/core/src/include/jcr.h b/core/src/include/jcr.h index 68a650b524a..281f997bddc 100644 --- a/core/src/include/jcr.h +++ b/core/src/include/jcr.h @@ -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-2018 Bareos GmbH & Co. KG + 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 @@ -44,10 +44,10 @@ #ifdef DIRECTOR_DAEMON #include "cats/cats.h" #include "dird/client_connection_handshake_mode.h" +typedef struct s_tree_root TREE_ROOT; #endif #include "lib/alist.h" -#include "lib/tree.h" #include "lib/volume_session_info.h" class dlist; @@ -289,18 +289,18 @@ struct Resources { /* clang-format off */ struct CompressionContext { - POOLMEM* deflate_buffer = nullptr; /**< Buffer used for deflation (compression) */ - POOLMEM* inflate_buffer = nullptr; /**< Buffer used for inflation (decompression) */ - uint32_t deflate_buffer_size = 0; /**< Length of deflation buffer */ - uint32_t inflate_buffer_size = 0; /**< Length of inflation buffer */ + POOLMEM* deflate_buffer{nullptr}; /**< Buffer used for deflation (compression) */ + POOLMEM* inflate_buffer{nullptr}; /**< Buffer used for inflation (decompression) */ + uint32_t deflate_buffer_size{0}; /**< Length of deflation buffer */ + uint32_t inflate_buffer_size{0}; /**< Length of inflation buffer */ struct { #ifdef HAVE_LIBZ - void* pZLIB = nullptr; /**< ZLIB compression session data */ + void* pZLIB{nullptr}; /**< ZLIB compression session data */ #endif #ifdef HAVE_LZO - void* pLZO = nullptr; /**< LZO compression session data */ + void* pLZO{nullptr}; /**< LZO compression session data */ #endif - void* pZFAST = nullptr; /**< FASTLZ compression session data */ + void* pZFAST{nullptr}; /**< FASTLZ compression session data */ } workset; }; /* clang-format on */ diff --git a/core/src/lib/alist.h b/core/src/lib/alist.h index 69c0c3a9d67..bca1c07a24f 100644 --- a/core/src/lib/alist.h +++ b/core/src/lib/alist.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2012 Free Software Foundation Europe e.V. - Copyright (C) 2016-2016 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -40,34 +40,44 @@ * Loop var through each member of list using an decreasing index. */ #ifdef HAVE_TYPEOF -#define foreach_alist(var, list) \ +#define foreach_alist(var, list) \ + for ((var) = list ? (typeof((var)))(list)->first() : 0; (var); \ + (var) = (typeof(var))(list)->next()) + +#define foreach_alist_null(var, list) \ for ((var) = list ? (typeof((var)))(list)->first() : NULL; (var); \ (var) = (typeof(var))(list)->next()) -#define foreach_alist_index(inx, var, list) \ - for ((inx) = 0; list ? ((var) = (typeof((var)))(list)->get((inx))) : NULL; \ +#define foreach_alist_index(inx, var, list) \ + for ((inx) = 0; \ + (list != NULL) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \ (inx)++) -#define foreach_alist_rindex(inx, var, list) \ - for (list ? (inx) = ((list)->size() - 1) : 0; \ - list ? ((var) = (typeof((var)))(list)->get((inx))) : NULL; (inx)--) +#define foreach_alist_rindex(inx, var, list) \ + for ((list != NULL) ? (inx) = ((list)->size() - 1) : 0; \ + (list != NULL) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \ + (inx)--) #else -#define foreach_alist(var, list) \ - for (list ? (*((void**)&(var)) = (void*)((list)->first())) : NULL; (var); \ +#define foreach_alist(var, list) \ + for (list ? (*((void**)&(var)) = (void*)((list)->first())) : 0; (var); \ (*((void**)&(var)) = (void*)((list)->next()))) +#define foreach_alist_null(var, list) \ + for (list ? (*((void**)&(var)) = (void*)((list)->first())) : NULL; (var); \ + (*((void**)&(var)) = (void*)((list)->next()))) -#define foreach_alist_index(inx, var, list) \ - for ((inx) = 0; \ - list ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) : NULL; \ +#define foreach_alist_index(inx, var, list) \ + for ((inx) = 0; \ + (list != NULL) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \ + : 0; \ (inx)++) -#define foreach_alist_rindex(inx, var, list) \ - for (list ? (inx) = ((list)->size() - 1) : 0; \ - list ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) : NULL; \ +#define foreach_alist_rindex(inx, var, list) \ + for ((list != NULL) ? (inx) = ((list)->size() - 1) : 0; \ + (list != NULL) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \ + : 0; \ (inx)--) - #endif /** diff --git a/core/src/lib/crypto_openssl.cc b/core/src/lib/crypto_openssl.cc index 441a85c5e96..9ef5ebb2b22 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-2018 Bareos GmbH & Co. KG + 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 @@ -211,8 +211,14 @@ IMPLEMENT_STACK_OF(RecipientInfo) #else /* Openssl Version >= 1.1 */ + +/* ignore the suggest-override warnings caused by following DEFINE_STACK_OF() */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" DEFINE_STACK_OF(SignerInfo) DEFINE_STACK_OF(RecipientInfo) +#pragma GCC diagnostic pop + #define M_ASN1_OCTET_STRING_free(a) ASN1_STRING_free((ASN1_STRING*)a) #define M_ASN1_OCTET_STRING_cmp(a, b) \ diff --git a/core/src/lib/dlist.h b/core/src/lib/dlist.h index 38d74d1fa04..bce4decca5e 100644 --- a/core/src/lib/dlist.h +++ b/core/src/lib/dlist.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2004-2010 Free Software Foundation Europe e.V. - Copyright (C) 2016-2016 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -49,12 +49,18 @@ * Loop var through each member of list */ #ifdef HAVE_TYPEOF -#define foreach_dlist(var, list) \ - for ((var) = NULL; list ? ((var) = (typeof(var))(list)->next(var)) : NULL;) +#define foreach_dlist(var, list) \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wconversion-null\"") for ( \ + (var) = NULL; list ? ((var) = (typeof(var))(list)->next(var)) \ + : NULL;) _Pragma("GCC diagnostic pop") #else -#define foreach_dlist(var, list) \ - for ((var) = NULL; \ - list ? (*((void**)&(var)) = (void*)((list)->next(var))) : NULL;) +#define foreach_dlist(var, list) \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wconversion-null\"") for ( \ + (var) = NULL; \ + list ? (*((void**)&(var)) = (void*)((list)->next(var))) : NULL;) \ + _Pragma("GCC diagnostic pop") #endif class dlist { diff --git a/core/src/lib/htable.cc b/core/src/lib/htable.cc index b7eb7016bea..7cff8978619 100644 --- a/core/src/lib/htable.cc +++ b/core/src/lib/htable.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2014-2014 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -40,6 +40,7 @@ * Kern Sibbald, July MMIII */ +#include #include "include/hostconfig.h" #ifdef HAVE_HPUX_OS @@ -256,7 +257,7 @@ void htable::stats() printf("buckets=%d num_items=%d max_items=%d\n", buckets, num_items, max_items); printf("max hits in a bucket = %d\n", max); - printf("total bytes malloced = %lld\n", (long long int)total_size); + printf("total bytes malloced = %" PRIu64 "\n", total_size); printf("total blocks malloced = %d\n", blocks); } diff --git a/core/src/lib/htable.h b/core/src/lib/htable.h index 7f8691eb7a8..c47252f7099 100644 --- a/core/src/lib/htable.h +++ b/core/src/lib/htable.h @@ -109,6 +109,7 @@ class htable { void grow_table(); /* Grow the table */ public: + htable() = default; htable(void* item, void* link, int tsize = 31, diff --git a/core/src/lib/mntent_cache.cc b/core/src/lib/mntent_cache.cc index 911dd1378cf..3aa5cd3c890 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-2013 Bareos GmbH & Co. KG + 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 @@ -153,7 +153,8 @@ static mntent_cache_entry_t* add_mntent_mapping(uint32_t dev, mntent_cache_entry_t* mce; mce = (mntent_cache_entry_t*)malloc(sizeof(mntent_cache_entry_t)); - memset(mce, 0, sizeof(mntent_cache_entry_t)); + mntent_cache_entry_t empty_mntent_cache_entry; + *mce = empty_mntent_cache_entry; mce->dev = dev; mce->special = strdup(special); mce->mountpoint = strdup(mountpoint); diff --git a/core/src/lib/mntent_cache.h b/core/src/lib/mntent_cache.h index f3fc03967ca..d6a8d9847bb 100644 --- a/core/src/lib/mntent_cache.h +++ b/core/src/lib/mntent_cache.h @@ -3,7 +3,7 @@ Copyright (C) 2009-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2013 Bareos GmbH & Co. KG + 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 @@ -47,14 +47,14 @@ struct mntent_cache_entry_t { dlink link; - uint32_t dev; - char* special; - char* mountpoint; - char* fstype; - char* mntopts; - int reference_count; - bool validated; - bool destroyed; + uint32_t dev{0}; + char* special{nullptr}; + char* mountpoint{nullptr}; + char* fstype{nullptr}; + char* mntopts{nullptr}; + int reference_count{0}; + bool validated{false}; + bool destroyed{false}; }; mntent_cache_entry_t* find_mntent_mapping(uint32_t dev); diff --git a/core/src/lib/plugins.cc b/core/src/lib/plugins.cc index d5eeb5b722b..7479a130756 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-2013 Bareos GmbH & Co. KG + 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 @@ -343,8 +343,8 @@ bool LoadPlugins(void* binfo, */ void UnloadPlugins(alist* plugin_list) { - int i; - Plugin* plugin; + int i{}; + Plugin* plugin{}; if (!plugin_list) { return; } foreach_alist_index (i, plugin, plugin_list) { @@ -372,8 +372,9 @@ void UnloadPlugin(alist* plugin_list, Plugin* plugin, int index) int ListPlugins(alist* plugin_list, PoolMem& msg) { - int i, len = 0; - Plugin* plugin; + int i{}; + int len{}; + Plugin* plugin{}; if (plugin_list && plugin_list->size() > 0) { PmStrcpy(msg, "Plugin Info:\n"); @@ -439,8 +440,8 @@ void DbgPrintPluginAddHook(dbg_print_plugin_hook_t* fct) void DumpPlugins(alist* plugin_list, FILE* fp) { - int i; - Plugin* plugin; + int i{}; + Plugin* plugin{}; fprintf(fp, "Attempt to dump plugins. Hook count=%d\n", dbg_plugin_hook_count); diff --git a/core/src/lib/rblist.h b/core/src/lib/rblist.h index e7424a80d78..56e8590289b 100644 --- a/core/src/lib/rblist.h +++ b/core/src/lib/rblist.h @@ -57,19 +57,27 @@ struct rblink { }; class rblist { + public: + rblist() = default; + rblist(void* item, rblink* link); + ~rblist(void) { destroy(); } + void* insert(void* item, int compare(void* item1, void* item2)); + int size(void) const; + bool empty(void) const; + void* first(void); + void* next(void* item); + void* search(void* item, int compare(void* item1, void* item2)); + void remove(void* item); + + private: void* head = nullptr; int16_t loffset = 0; uint32_t num_items = 0; bool down = false; void LeftRotate(void* item); void RightRotate(void* item); - - public: - rblist(void* item, rblink* link); - rblist(void); - ~rblist(void) { destroy(); } - void init(void* item, rblink* link); void SetParent(void* item, void* parent); + void init(void* item, rblink* link); void SetLeft(void* item, void* left); void SetRight(void* item, void* right); void SetRed(void* item, bool red); @@ -77,14 +85,7 @@ class rblist { void* left(const void* item) const; void* right(const void* item) const; bool red(const void* item) const; - void* insert(void* item, int compare(void* item1, void* item2)); - void* search(void* item, int compare(void* item1, void* item2)); - void* first(void); - void* next(void* item); void* any(void* item); - void remove(void* item); - bool empty(void) const; - int size(void) const; void destroy(void); }; @@ -106,9 +107,6 @@ inline void rblist::init(void* item, rblink* link) inline rblist::rblist(void* item, rblink* link) { init(item, link); } -/* Constructor with link at head of item */ -inline rblist::rblist(void) : head(0), loffset(0), num_items(0) {} - inline void rblist::SetParent(void* item, void* parent) { ((rblink*)(((char*)item) + loffset))->parent = parent; diff --git a/core/src/lib/thread_list.h b/core/src/lib/thread_list.h index f164e23aba9..ce44230f889 100644 --- a/core/src/lib/thread_list.h +++ b/core/src/lib/thread_list.h @@ -28,7 +28,7 @@ class ConfigurationParser; struct ThreadListPrivate; class ThreadList { - friend class CleanupOwnThreadAndNotify; + friend struct CleanupOwnThreadAndNotify; public: ThreadList(); diff --git a/core/src/lib/tree.cc b/core/src/lib/tree.cc index 4c370ab2321..463af55bb4d 100644 --- a/core/src/lib/tree.cc +++ b/core/src/lib/tree.cc @@ -37,7 +37,8 @@ static TREE_NODE* search_and_insert_tree_node(char* fname, int type, TREE_ROOT* root, TREE_NODE* parent); -static char* tree_alloc(TREE_ROOT* root, int size); +template +static T* tree_alloc(TREE_ROOT* root, int size); /* * NOTE !!!!! we turn off Debug messages for performance reasons. @@ -64,7 +65,7 @@ static void MallocBuf(TREE_ROOT* root, int size) mem->next = root->mem; root->mem = mem; mem->mem = mem->first; - mem->rem = (char*)mem + size - mem->mem; + mem->rem = (char*)mem + size - (char*)mem->mem; Dmsg2(200, "malloc buf size=%d rem=%d\n", size, mem->rem); } @@ -82,8 +83,8 @@ TREE_ROOT* new_tree(int count) if (count < 1000) { /* minimum tree size */ count = 1000; } - root = (TREE_ROOT*)malloc(sizeof(TREE_ROOT)); - memset(root, 0, sizeof(TREE_ROOT)); + root = static_cast(malloc(sizeof(TREE_ROOT))); + root = new (root) TREE_ROOT(); /* * Assume filename + node = 40 characters average length @@ -108,8 +109,8 @@ static TREE_NODE* new_tree_node(TREE_ROOT* root) { TREE_NODE* node; int size = sizeof(TREE_NODE); - node = (TREE_NODE*)tree_alloc(root, size); - memset(node, 0, size); + node = tree_alloc(root, size); + node = new (node) TREE_NODE(); node->delta_seq = -1; return node; } @@ -121,14 +122,14 @@ static void FreeTreeNode(TREE_ROOT* root) { int asize = BALIGN(sizeof(TREE_NODE)); root->mem->rem += asize; - root->mem->mem -= asize; + root->mem->mem = (char*)root->mem->mem - asize; } void TreeRemoveNode(TREE_ROOT* root, TREE_NODE* node) { int asize = BALIGN(sizeof(TREE_NODE)); node->parent->child.remove(node); - if ((root->mem->mem - asize) == (char*)node) { + if (((char*)root->mem->mem - asize) == (char*)node) { FreeTreeNode(root); } else { Dmsg0(0, "Can't release tree node\n"); @@ -140,9 +141,10 @@ void TreeRemoveNode(TREE_ROOT* root, TREE_NODE* node) * Keep the pointers properly aligned by allocating * sizes that are aligned. */ -static char* tree_alloc(TREE_ROOT* root, int size) +template +static T* tree_alloc(TREE_ROOT* root, int size) { - char* buf; + T* buf; int asize = BALIGN(size); if (root->mem->rem < asize) { @@ -155,8 +157,8 @@ static char* tree_alloc(TREE_ROOT* root, int size) MallocBuf(root, mb_size); } root->mem->rem -= asize; - buf = root->mem->mem; - root->mem->mem += asize; + buf = static_cast(root->mem->mem); + root->mem->mem = (char*)root->mem->mem + asize; return buf; } @@ -195,7 +197,7 @@ void TreeAddDeltaPart(TREE_ROOT* root, int32_t FileIndex) { struct delta_list* elt = - (struct delta_list*)tree_alloc(root, sizeof(struct delta_list)); + tree_alloc(root, sizeof(struct delta_list)); elt->next = node->delta_list; elt->JobId = JobId; @@ -356,7 +358,7 @@ static TREE_NODE* search_and_insert_tree_node(char* fname, * It was not found, but is now inserted */ node->fname_len = strlen(fname); - node->fname = tree_alloc(root, node->fname_len + 1); + node->fname = tree_alloc(root, node->fname_len + 1); strcpy(node->fname, fname); node->parent = parent; node->type = type; diff --git a/core/src/lib/tree.h b/core/src/lib/tree.h index 645eeed2de1..0239c77a6a4 100644 --- a/core/src/lib/tree.h +++ b/core/src/lib/tree.h @@ -42,7 +42,7 @@ struct s_mem { struct s_mem* next; /* next buffer */ int rem; /* remaining bytes */ - char* mem; /* memory pointer */ + void* mem; /* memory pointer */ char first[1]; /* first byte */ }; @@ -68,15 +68,25 @@ struct delta_list { * there is one for each file. */ struct s_tree_node { + s_tree_node() + : type{false} + , extract{false} + , extract_dir{false} + , hard_link{false} + , soft_link{false} + , inserted{false} + , loaded{false} + { + } /* KEEP sibling as the first member to avoid having to * do initialization of child */ rblink sibling; rblist child; - char* fname; /* file name */ - int32_t FileIndex; /* file index */ - uint32_t JobId; /* JobId */ - int32_t delta_seq; /* current delta sequence */ - uint16_t fname_len; /* filename length */ + char* fname{}; /* file name */ + int32_t FileIndex{}; /* file index */ + uint32_t JobId{}; /* JobId */ + int32_t delta_seq{}; /* current delta sequence */ + uint16_t fname_len{}; /* filename length */ unsigned int type : 8; /* node type */ unsigned int extract : 1; /* extract item */ unsigned int extract_dir : 1; /* extract dir entry only */ @@ -84,44 +94,53 @@ struct s_tree_node { unsigned int soft_link : 1; /* set if is soft link */ unsigned int inserted : 1; /* set when node newly inserted */ unsigned int loaded : 1; /* set when the dir is in the tree */ - struct s_tree_node* parent; - struct s_tree_node* next; /* next hash of FileIndex */ - struct delta_list* delta_list; /* delta parts for this node */ - uint64_t fhinfo; /* NDMP Fh_info */ - uint64_t fhnode; /* NDMP Fh_node */ + struct s_tree_node* parent{}; + struct s_tree_node* next{}; /* next hash of FileIndex */ + struct delta_list* delta_list{}; /* delta parts for this node */ + uint64_t fhinfo{}; /* NDMP Fh_info */ + uint64_t fhnode{}; /* NDMP Fh_node */ }; typedef struct s_tree_node TREE_NODE; struct s_tree_root { + s_tree_root() + : type{false} + , extract{false} + , extract_dir{false} + , have_link{false} + , inserted{false} + , loaded{false} + { + } /* KEEP sibling as the first member to avoid having to * do initialization of child */ - rblink sibling; + rblink sibling{}; rblist child; - const char* fname; /* file name */ - int32_t FileIndex; /* file index */ - uint32_t JobId; /* JobId */ - int32_t delta_seq; /* current delta sequence */ - uint16_t fname_len; /* filename length */ + const char* fname{}; /* file name */ + int32_t FileIndex{}; /* file index */ + uint32_t JobId{}; /* JobId */ + int32_t delta_seq{}; /* current delta sequence */ + uint16_t fname_len{}; /* filename length */ unsigned int type : 8; /* node type */ unsigned int extract : 1; /* extract item */ unsigned int extract_dir : 1; /* extract dir entry only */ unsigned int have_link : 1; /* set if have hard link */ unsigned int inserted : 1; /* set when newly inserted */ unsigned int loaded : 1; /* set when the dir is in the tree */ - struct s_tree_node* parent; - struct s_tree_node* next; /* next hash of FileIndex */ - struct delta_list* delta_list; /* delta parts for this node */ + struct s_tree_node* parent{}; + struct s_tree_node* next{}; /* next hash of FileIndex */ + struct delta_list* delta_list{}; /* delta parts for this node */ /* The above ^^^ must be identical to a TREE_NODE structure */ - struct s_tree_node* first; /* first entry in the tree */ - struct s_tree_node* last; /* last entry in tree */ - struct s_mem* mem; /* tree memory */ - uint32_t total_size; /* total bytes allocated */ - uint32_t blocks; /* total mallocs */ - int cached_path_len; /* length of cached path */ - char* cached_path; /* cached current path */ - TREE_NODE* cached_parent; /* cached parent for above path */ - htable hardlinks; /* references to first occurence of hardlinks */ + struct s_tree_node* first{}; /* first entry in the tree */ + struct s_tree_node* last{}; /* last entry in tree */ + struct s_mem* mem{}; /* tree memory */ + uint32_t total_size{}; /* total bytes allocated */ + uint32_t blocks{}; /* total mallocs */ + int cached_path_len{}; /* length of cached path */ + char* cached_path{}; /* cached current path */ + TREE_NODE* cached_parent{}; /* cached parent for above path */ + htable hardlinks; /* references to first occurence of hardlinks */ }; typedef struct s_tree_root TREE_ROOT; diff --git a/core/src/lib/var.cc b/core/src/lib/var.cc index 05891f12d8d..bea578ee33c 100644 --- a/core/src/lib/var.cc +++ b/core/src/lib/var.cc @@ -2249,7 +2249,7 @@ var_rc_t var_create(var_t** pvar) if ((var = (var_t*)malloc(sizeof(var_t))) == NULL) return VAR_RC(VAR_ERR_OUT_OF_MEMORY); memset(var, 0, sizeof(var_t)); - var_config(var, VAR_CONFIG_SYNTAX, &var_syntax_default); + var_config(var, var_config_t::VAR_CONFIG_SYNTAX, &var_syntax_default); *pvar = var; return VAR_OK; } @@ -2272,7 +2272,7 @@ var_rc_t var_config(var_t* var, var_config_t mode, ...) va_start(ap, mode); switch (mode) { - case VAR_CONFIG_SYNTAX: { + case var_config_t::VAR_CONFIG_SYNTAX: { var_syntax_t* s; s = (var_syntax_t*)va_arg(ap, void*); if (s == NULL) { @@ -2300,7 +2300,7 @@ var_rc_t var_config(var_t* var, var_config_t mode, ...) } break; } - case VAR_CONFIG_CB_VALUE: { + case var_config_t::VAR_CONFIG_CB_VALUE: { var_cb_value_t fct; void* ctx; @@ -2310,7 +2310,7 @@ var_rc_t var_config(var_t* var, var_config_t mode, ...) var->cb_value_ctx = ctx; break; } - case VAR_CONFIG_CB_OPERATION: { + case var_config_t::VAR_CONFIG_CB_OPERATION: { var_cb_operation_t fct; void* ctx; diff --git a/core/src/lib/var.h b/core/src/lib/var.h index ab55b9d38c9..00ec3e503bf 100644 --- a/core/src/lib/var.h +++ b/core/src/lib/var.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2009 Free Software Foundation Europe e.V. - Copyright (C) 2016-2016 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -100,12 +100,12 @@ typedef enum struct var_st; typedef struct var_st var_t; -typedef enum +enum class var_config_t { VAR_CONFIG_SYNTAX, VAR_CONFIG_CB_VALUE, VAR_CONFIG_CB_OPERATION -} var_config_t; +}; typedef struct { char escape; /* default: '\' */ diff --git a/core/src/plugins/dird/python-dir.cc b/core/src/plugins/dird/python-dir.cc index 21d65894cf5..5a24df63885 100644 --- a/core/src/plugins/dird/python-dir.cc +++ b/core/src/plugins/dird/python-dir.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2014 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -27,8 +27,14 @@ * Python Director Plugin program */ +#if defined(HAVE_WIN32) #include "include/bareos.h" #include +#else +#include +#include "include/bareos.h" +#endif + #include "dird/dird.h" #if (PY_VERSION_HEX < 0x02060000) diff --git a/core/src/plugins/filed/example-plugin-fd.cc b/core/src/plugins/filed/example-plugin-fd.cc index 2006d014983..6964681507e 100644 --- a/core/src/plugins/filed/example-plugin-fd.cc +++ b/core/src/plugins/filed/example-plugin-fd.cc @@ -13,6 +13,7 @@ #define BUILD_PLUGIN #define BUILDING_DLL /* required for Windows plugin */ +#include #include "include/bareos.h" #include "filed/fd_plugins.h" @@ -173,10 +174,11 @@ static bRC handlePluginEvent(bpContext* ctx, bEvent* event, void* value) printf("plugin: BackupEnd\n"); break; case bEventLevel: - printf("plugin: JobLevel=%c %ld\n", (int)(int64_t)value, (int64_t)value); + printf("plugin: JobLevel=%c %" PRId64 "\n", (int)(int64_t)value, + (int64_t)value); break; case bEventSince: - printf("plugin: since=%ld\n", (int64_t)value); + printf("plugin: since=%" PRId64 "\n", (int64_t)value); break; case bEventStartRestoreJob: printf("plugin: StartRestoreJob\n"); diff --git a/core/src/plugins/filed/python-fd.cc b/core/src/plugins/filed/python-fd.cc index 6a7e0afc843..4d3ba084318 100644 --- a/core/src/plugins/filed/python-fd.cc +++ b/core/src/plugins/filed/python-fd.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2015 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -28,8 +28,14 @@ */ #define BUILD_PLUGIN +#if defined(HAVE_WIN32) #include "include/bareos.h" #include +#else +#include +#include "include/bareos.h" +#endif + #include "filed/fd_plugins.h" #include "plugins/filed/fd_common.h" #include "python-fd.h" @@ -1562,8 +1568,7 @@ static inline bool PySavePacketToNative(PySavePacket* pSavePkt, char* buf; if (p_ctx->object_name) { free(p_ctx->object_name); } - p_ctx->object_name = - strdup(PyString_AsString(pSavePkt->object_name)); + p_ctx->object_name = strdup(PyString_AsString(pSavePkt->object_name)); sp->object_name = p_ctx->object_name; sp->object_len = pSavePkt->object_len; diff --git a/core/src/plugins/stored/python-sd.cc b/core/src/plugins/stored/python-sd.cc index 1926c159d4b..85f0c0280e8 100644 --- a/core/src/plugins/stored/python-sd.cc +++ b/core/src/plugins/stored/python-sd.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2011-2014 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -26,8 +26,13 @@ * @file * Python Storage daemon Plugin program */ +#if defined(HAVE_WIN32) #include "include/bareos.h" #include +#else +#include +#include "include/bareos.h" +#endif #include "stored/stored.h" using namespace storagedaemon; diff --git a/core/src/qt-tray-monitor/tray_conf.cc b/core/src/qt-tray-monitor/tray_conf.cc index 0afedfe03a3..1f4d1896bd0 100644 --- a/core/src/qt-tray-monitor/tray_conf.cc +++ b/core/src/qt-tray-monitor/tray_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-2016 Bareos GmbH & Co. KG + 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 @@ -336,7 +336,7 @@ static bool SaveResource(int type, ResourceItem* items, int pass) } if (!error) { - BareosResource* new_resource; + BareosResource* new_resource = nullptr; switch (type) { case R_MONITOR: new_resource = res_monitor; diff --git a/core/src/stored/askdir.cc b/core/src/stored/askdir.cc index 18b0671f801..942beaeb171 100644 --- a/core/src/stored/askdir.cc +++ b/core/src/stored/askdir.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-2016 Bareos GmbH & Co. KG + 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 @@ -93,7 +93,6 @@ static bool DoGetVolumeInfo(DeviceControlRecord* dcr) { JobControlRecord* jcr = dcr->jcr; BareosSocket* dir = jcr->dir_bsock; - VolumeCatalogInfo vol; int n; int32_t InChanger; @@ -103,7 +102,7 @@ static bool DoGetVolumeInfo(DeviceControlRecord* dcr) Mmsg(jcr->errmsg, _("Network error on BnetRecv in req_vol_info.\n")); return false; } - memset(&vol, 0, sizeof(vol)); + VolumeCatalogInfo vol; Dmsg1(debuglevel, "msg); n = sscanf(dir->msg, OK_media, vol.VolCatName, &vol.VolCatJobs, &vol.VolCatFiles, &vol.VolCatBlocks, &vol.VolCatBytes, diff --git a/core/src/stored/backends/cephfs_device.h b/core/src/stored/backends/cephfs_device.h index 8148475d6d3..da65aee135f 100644 --- a/core/src/stored/backends/cephfs_device.h +++ b/core/src/stored/backends/cephfs_device.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2014-2014 Planets Communications B.V. - Copyright (C) 2014-2014 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -47,13 +47,15 @@ class cephfs_device : public Device { /* * Interface from Device */ - int d_close(int); - int d_open(const char* pathname, int flags, int mode); - int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL); - boffset_t d_lseek(DeviceControlRecord* dcr, boffset_t offset, int whence); - ssize_t d_read(int fd, void* buffer, size_t count); - ssize_t d_write(int fd, const void* buffer, size_t count); - bool d_truncate(DeviceControlRecord* dcr); + int d_close(int) override; + int d_open(const char* pathname, int flags, int mode) override; + int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL) override; + boffset_t d_lseek(DeviceControlRecord* dcr, + boffset_t offset, + int whence) override; + ssize_t d_read(int fd, void* buffer, size_t count) override; + ssize_t d_write(int fd, const void* buffer, size_t count) override; + bool d_truncate(DeviceControlRecord* dcr) override; }; } /* namespace storagedaemon */ diff --git a/core/src/stored/backends/gfapi_device.h b/core/src/stored/backends/gfapi_device.h index fb23871c9e0..86c4178ada7 100644 --- a/core/src/stored/backends/gfapi_device.h +++ b/core/src/stored/backends/gfapi_device.h @@ -56,13 +56,15 @@ class gfapi_device : public Device { /* * Interface from Device */ - int d_close(int); - int d_open(const char* pathname, int flags, int mode); - int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL); - boffset_t d_lseek(DeviceControlRecord* dcr, boffset_t offset, int whence); - ssize_t d_read(int fd, void* buffer, size_t count); - ssize_t d_write(int fd, const void* buffer, size_t count); - bool d_truncate(DeviceControlRecord* dcr); -}; + int d_close(int) override; + int d_open(const char* pathname, int flags, int mode) override; + int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL) override; + boffset_t d_lseek(DeviceControlRecord* dcr, + boffset_t offset, + int whence) override; + ssize_t d_read(int fd, void* buffer, size_t count) override; + ssize_t d_write(int fd, const void* buffer, size_t count) override; + bool d_truncate(DeviceControlRecord* dcr) override; +} override; } /* namespace storagedaemon */ #endif /* BAREOS_STORED_BACKENDS_GFAPI_DEVICE_H_ */ diff --git a/core/src/stored/backends/rados_device.h b/core/src/stored/backends/rados_device.h index a9c7b2927f8..2fa63d6003b 100644 --- a/core/src/stored/backends/rados_device.h +++ b/core/src/stored/backends/rados_device.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2014-2016 Planets Communications B.V. - Copyright (C) 2014-2016 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -100,13 +100,15 @@ class rados_device : public Device { /* * Interface from Device */ - int d_close(int); - int d_open(const char* pathname, int flags, int mode); - int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL); - boffset_t d_lseek(DeviceControlRecord* dcr, boffset_t offset, int whence); - ssize_t d_read(int fd, void* buffer, size_t count); - ssize_t d_write(int fd, const void* buffer, size_t count); - bool d_truncate(DeviceControlRecord* dcr); + int d_close(int) override; + int d_open(const char* pathname, int flags, int mode) override; + int d_ioctl(int fd, ioctl_req_t request, char* mt = NULL) override; + boffset_t d_lseek(DeviceControlRecord* dcr, + boffset_t offset, + int whence) override; + ssize_t d_read(int fd, void* buffer, size_t count) override; + ssize_t d_write(int fd, const void* buffer, size_t count) override; + bool d_truncate(DeviceControlRecord* dcr) override; }; } /* namespace storagedaemon */ #endif /* BAREOS_STORED_BACKENDS_RADOS_DEVICE_H_ */ diff --git a/core/src/stored/bcopy.cc b/core/src/stored/bcopy.cc index 09b99e4928f..9658c3142dd 100644 --- a/core/src/stored/bcopy.cc +++ b/core/src/stored/bcopy.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -52,7 +52,7 @@ using namespace storagedaemon; /* Forward referenced functions */ static void GetSessionRecord(Device* dev, DeviceRecord* rec, - SESSION_LABEL* sessrec); + Session_Label* sessrec); static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec); @@ -67,7 +67,7 @@ static bool list_records = false; static uint32_t records = 0; static uint32_t jobs = 0; static DeviceBlock* out_block; -static SESSION_LABEL sessrec; +static Session_Label sessrec; static void usage() { @@ -357,10 +357,10 @@ static bool RecordCb(DeviceControlRecord* in_dcr, DeviceRecord* rec) static void GetSessionRecord(Device* dev, DeviceRecord* rec, - SESSION_LABEL* sessrec) + Session_Label* sessrec) { const char* rtype; - memset(sessrec, 0, sizeof(SESSION_LABEL)); + *sessrec = Session_Label{}; switch (rec->FileIndex) { case PRE_LABEL: rtype = _("Fresh Volume Label"); diff --git a/core/src/stored/bextract.cc b/core/src/stored/bextract.cc index df3a68bb820..927f5b2bb99 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-2016 Bareos GmbH & Co. KG + 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 @@ -424,8 +424,9 @@ static void DoExtract(char* devname) jcr->buf_size = DEFAULT_NETWORK_BUFFER_SIZE; SetupDecompressionBuffers(jcr, &decompress_buf_size); + if (decompress_buf_size > 0) { - memset(&jcr->compress, 0, sizeof(CompressionContext)); + jcr->compress = CompressionContext{}; jcr->compress.inflate_buffer = GetMemory(decompress_buf_size); jcr->compress.inflate_buffer_size = decompress_buf_size; } diff --git a/core/src/stored/bls.cc b/core/src/stored/bls.cc index 55905e682a2..6da1a28707c 100644 --- a/core/src/stored/bls.cc +++ b/core/src/stored/bls.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-2017 Bareos GmbH & Co. KG + 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 @@ -60,7 +60,7 @@ static void do_ls(char* fname); static void do_close(JobControlRecord* jcr); static void GetSessionRecord(Device* dev, DeviceRecord* rec, - SESSION_LABEL* sessrec); + Session_Label* sessrec); static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec); static Device* dev; @@ -70,7 +70,7 @@ static bool list_blocks = false; static bool list_jobs = false; static DeviceRecord* rec; static JobControlRecord* jcr; -static SESSION_LABEL sessrec; +static Session_Label sessrec; static uint32_t num_files = 0; static Attributes* attr; @@ -447,10 +447,11 @@ static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec) static void GetSessionRecord(Device* dev, DeviceRecord* rec, - SESSION_LABEL* sessrec) + Session_Label* sessrec) { const char* rtype; - memset(sessrec, 0, sizeof(SESSION_LABEL)); + Session_Label empty_Session_Label; + *sessrec = empty_Session_Label; jcr->JobId = 0; switch (rec->FileIndex) { case PRE_LABEL: diff --git a/core/src/stored/bscan.cc b/core/src/stored/bscan.cc index e2013f0b519..556831c57e9 100644 --- a/core/src/stored/bscan.cc +++ b/core/src/stored/bscan.cc @@ -71,16 +71,16 @@ static bool CreateFileAttributesRecord(BareosDb* db, DeviceRecord* rec); static bool CreateMediaRecord(BareosDb* db, MediaDbRecord* mr, - VOLUME_LABEL* vl); + Volume_Label* vl); static bool UpdateMediaRecord(BareosDb* db, MediaDbRecord* mr); static bool CreatePoolRecord(BareosDb* db, PoolDbRecord* pr); static JobControlRecord* CreateJobRecord(BareosDb* db, JobDbRecord* mr, - SESSION_LABEL* label, + Session_Label* label, DeviceRecord* rec); static bool UpdateJobRecord(BareosDb* db, JobDbRecord* mr, - SESSION_LABEL* elabel, + Session_Label* elabel, DeviceRecord* rec); static bool CreateClientRecord(BareosDb* db, ClientDbRecord* cr); static bool CreateFilesetRecord(BareosDb* db, FileSetDbRecord* fsr); @@ -106,8 +106,8 @@ static FileSetDbRecord fsr; static RestoreObjectDbRecord rop; static AttributesDbRecord ar; static FileDbRecord fr; -static SESSION_LABEL label; -static SESSION_LABEL elabel; +static Session_Label label; +static Session_Label elabel; static Attributes* attr; static time_t lasttime = 0; @@ -446,12 +446,19 @@ static void do_scan() { attr = new_attr(bjcr); - memset(&ar, 0, sizeof(ar)); - memset(&pr, 0, sizeof(pr)); - memset(&jr, 0, sizeof(jr)); - memset(&cr, 0, sizeof(cr)); - memset(&fsr, 0, sizeof(fsr)); - memset(&fr, 0, sizeof(fr)); + AttributesDbRecord ar_emtpy; + PoolDbRecord pr_empty; + JobDbRecord jr_empty; + ClientDbRecord cr_empty; + FileSetDbRecord fsr_empty; + FileDbRecord fr_empty; + + ar = ar_emtpy; + pr = pr_empty; + jr = jr_empty; + cr = cr_empty; + fsr = fsr_empty; + fr = fr_empty; /* * Detach bscan's jcr as we are not a real Job on the tape @@ -582,7 +589,7 @@ static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec) /* * Check Media Info */ - memset(&mr, 0, sizeof(mr)); + mr = MediaDbRecord{}; bstrncpy(mr.VolumeName, dev->VolHdr.VolumeName, sizeof(mr.VolumeName)); mr.PoolId = pr.PoolId; num_media++; @@ -642,7 +649,7 @@ static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec) ignored_msgs = 0; } UnserSessionLabel(&label, rec); - memset(&jr, 0, sizeof(jr)); + jr = JobDbRecord{}; bstrncpy(jr.Job, label.Job, sizeof(jr.Job)); if (db->GetJobRecord(bjcr, &jr)) { /* @@ -1119,7 +1126,7 @@ static bool CreateFileAttributesRecord(BareosDb* db, /** * For each Volume we see, we create a Medium record */ -static bool CreateMediaRecord(BareosDb* db, MediaDbRecord* mr, VOLUME_LABEL* vl) +static bool CreateMediaRecord(BareosDb* db, MediaDbRecord* mr, Volume_Label* vl) { struct date_time dt; struct tm tm; @@ -1277,7 +1284,7 @@ static bool CreateFilesetRecord(BareosDb* db, FileSetDbRecord* fsr) */ static JobControlRecord* CreateJobRecord(BareosDb* db, JobDbRecord* jr, - SESSION_LABEL* label, + Session_Label* label, DeviceRecord* rec) { JobControlRecord* mjcr; @@ -1337,7 +1344,7 @@ static JobControlRecord* CreateJobRecord(BareosDb* db, */ static bool UpdateJobRecord(BareosDb* db, JobDbRecord* jr, - SESSION_LABEL* elabel, + Session_Label* elabel, DeviceRecord* rec) { struct date_time dt; @@ -1459,7 +1466,6 @@ static bool CreateJobmediaRecord(BareosDb* db, JobControlRecord* mjcr) dcr->EndFile = dev->EndFile; dcr->VolMediaId = dev->VolCatInfo.VolMediaId; - memset(&jmr, 0, sizeof(jmr)); jmr.JobId = mjcr->JobId; jmr.MediaId = mr.MediaId; jmr.FirstIndex = dcr->VolFirstIndex; diff --git a/core/src/stored/bsr.cc b/core/src/stored/bsr.cc index a88109d9bfd..8bd396b1f35 100644 --- a/core/src/stored/bsr.cc +++ b/core/src/stored/bsr.cc @@ -50,7 +50,7 @@ const int dbglevel = 500; /* Forward references */ static int MatchVolume(BootStrapRecord* bsr, BsrVolume* volume, - VOLUME_LABEL* volrec, + Volume_Label* volrec, bool done); static int MatchSesstime(BootStrapRecord* bsr, BsrSessionTime* sesstime, @@ -61,23 +61,23 @@ static int MatchSessid(BootStrapRecord* bsr, DeviceRecord* rec); static int MatchClient(BootStrapRecord* bsr, BsrClient* client, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done); static int MatchJob(BootStrapRecord* bsr, BsrJob* job, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done); static int MatchJobType(BootStrapRecord* bsr, BsrJobType* job_type, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done); static int MatchJobLevel(BootStrapRecord* bsr, BsrJoblevel* job_level, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done); static int MatchJobid(BootStrapRecord* bsr, BsrJobid* jobid, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done); static int MatchFindex(BootStrapRecord* bsr, BsrFileIndex* findex, @@ -97,8 +97,8 @@ static int MatchStream(BootStrapRecord* bsr, bool done); static int MatchAll(BootStrapRecord* bsr, DeviceRecord* rec, - VOLUME_LABEL* volrec, - SESSION_LABEL* sessrec, + Volume_Label* volrec, + Session_Label* sessrec, bool done, JobControlRecord* jcr); static int MatchBlockSesstime(BootStrapRecord* bsr, @@ -209,8 +209,8 @@ static int MatchFileregex(BootStrapRecord* bsr, */ int MatchBsr(BootStrapRecord* bsr, DeviceRecord* rec, - VOLUME_LABEL* volrec, - SESSION_LABEL* sessrec, + Volume_Label* volrec, + Session_Label* sessrec, JobControlRecord* jcr) { int status; @@ -407,8 +407,8 @@ bool IsThisBsrDone(BootStrapRecord* bsr, DeviceRecord* rec) */ static int MatchAll(BootStrapRecord* bsr, DeviceRecord* rec, - VOLUME_LABEL* volrec, - SESSION_LABEL* sessrec, + Volume_Label* volrec, + Session_Label* sessrec, bool done, JobControlRecord* jcr) { @@ -537,7 +537,7 @@ static int MatchAll(BootStrapRecord* bsr, static int MatchVolume(BootStrapRecord* bsr, BsrVolume* volume, - VOLUME_LABEL* volrec, + Volume_Label* volrec, bool done) { if (!volume) { return 0; /* Volume must match */ } @@ -551,7 +551,7 @@ static int MatchVolume(BootStrapRecord* bsr, static int MatchClient(BootStrapRecord* bsr, BsrClient* client, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done) { if (!client) { return 1; /* no specification matches all */ } @@ -562,7 +562,7 @@ static int MatchClient(BootStrapRecord* bsr, static int MatchJob(BootStrapRecord* bsr, BsrJob* job, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done) { if (!job) { return 1; /* no specification matches all */ } @@ -573,7 +573,7 @@ static int MatchJob(BootStrapRecord* bsr, static int MatchJobType(BootStrapRecord* bsr, BsrJobType* job_type, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done) { if (!job_type) { return 1; /* no specification matches all */ } @@ -584,7 +584,7 @@ static int MatchJobType(BootStrapRecord* bsr, static int MatchJobLevel(BootStrapRecord* bsr, BsrJoblevel* job_level, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done) { if (!job_level) { return 1; /* no specification matches all */ } @@ -597,7 +597,7 @@ static int MatchJobLevel(BootStrapRecord* bsr, static int MatchJobid(BootStrapRecord* bsr, BsrJobid* jobid, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool done) { if (!jobid) { return 1; /* no specification matches all */ } diff --git a/core/src/stored/btape.cc b/core/src/stored/btape.cc index 44f68563a8f..459ba409663 100644 --- a/core/src/stored/btape.cc +++ b/core/src/stored/btape.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-2016 Bareos GmbH & Co. KG + 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 @@ -2098,7 +2098,6 @@ static void statcmd() */ static void fillcmd() { - DeviceRecord rec; DeviceBlock* block = dcr->block; char ec1[50], ec2[50]; char buf1[100], buf2[100]; @@ -2187,7 +2186,7 @@ static void fillcmd() } Pmsg0(-1, _("Wrote Start of Session label.\n")); - memset(&rec, 0, sizeof(rec)); + DeviceRecord rec; rec.data = GetMemory(100000); /* max record size */ rec.data_len = REC_SIZE; diff --git a/core/src/stored/dev.cc b/core/src/stored/dev.cc index ef7122ba0a5..834bcccccc8 100644 --- a/core/src/stored/dev.cc +++ b/core/src/stored/dev.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-2016 Bareos GmbH & Co. KG + 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 @@ -1000,7 +1000,7 @@ void Device::SetUnload() void Device::ClearVolhdr() { Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName); - memset(&VolHdr, 0, sizeof(VolHdr)); + VolHdr = Volume_Label{}; setVolCatInfo(false); } @@ -1065,7 +1065,7 @@ bool Device::close(DeviceControlRecord* dcr) EndFile = EndBlock = 0; open_mode = 0; ClearVolhdr(); - memset(&VolCatInfo, 0, sizeof(VolCatInfo)); + VolCatInfo = VolumeCatalogInfo{}; if (tid) { StopThreadTimer(tid); tid = 0; diff --git a/core/src/stored/dev.h b/core/src/stored/dev.h index 007b5a72de4..f99b46b94e0 100644 --- a/core/src/stored/dev.h +++ b/core/src/stored/dev.h @@ -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-2018 Bareos GmbH & Co. KG + 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 @@ -232,43 +232,43 @@ struct VolumeCatalogInfo { /* * Media info for the current Volume */ - uint32_t VolCatJobs = 0; /**< number of jobs on this Volume */ - uint32_t VolCatFiles = 0; /**< Number of files */ - uint32_t VolCatBlocks = 0; /**< Number of blocks */ - uint64_t VolCatBytes = 0; /**< Number of bytes written */ - uint32_t VolCatMounts = 0; /**< Number of mounts this volume */ - uint32_t VolCatErrors = 0; /**< Number of errors this volume */ - uint32_t VolCatWrites = 0; /**< Number of writes this volume */ - uint32_t VolCatReads = 0; /**< Number of reads this volume */ - uint64_t VolCatRBytes = 0; /**< Number of bytes read */ - uint32_t VolCatRecycles = 0; /**< Number of recycles this volume */ - uint32_t EndFile = 0; /**< Last file number */ - uint32_t EndBlock = 0; /**< Last block number */ - int32_t LabelType = 0; /**< Bareos/ANSI/IBM */ - int32_t Slot = 0; /**< >0=Slot loaded, 0=nothing, -1=unknown */ - uint32_t VolCatMaxJobs = 0; /**< Maximum Jobs to write to volume */ - uint32_t VolCatMaxFiles = 0; /**< Maximum files to write to volume */ - uint64_t VolCatMaxBytes = 0; /**< Max bytes to write to volume */ - uint64_t VolCatCapacityBytes = 0; /**< capacity estimate */ - btime_t VolReadTime = 0; /**< time spent reading */ - btime_t VolWriteTime = 0; /**< time spent writing this Volume */ - int64_t VolMediaId = 0; /**< MediaId */ - utime_t VolFirstWritten = 0; /**< Time of first write */ - utime_t VolLastWritten = 0; /**< Time of last write */ - bool InChanger = false; /**< Set if vol in current magazine */ - bool is_valid = false; /**< set if this data is valid */ - char VolCatStatus[20]{0}; /**< Volume status */ + uint32_t VolCatJobs{0}; /**< number of jobs on this Volume */ + uint32_t VolCatFiles{0}; /**< Number of files */ + uint32_t VolCatBlocks{0}; /**< Number of blocks */ + uint64_t VolCatBytes{0}; /**< Number of bytes written */ + uint32_t VolCatMounts{0}; /**< Number of mounts this volume */ + uint32_t VolCatErrors{0}; /**< Number of errors this volume */ + uint32_t VolCatWrites{0}; /**< Number of writes this volume */ + uint32_t VolCatReads{0}; /**< Number of reads this volume */ + uint64_t VolCatRBytes{0}; /**< Number of bytes read */ + uint32_t VolCatRecycles{0}; /**< Number of recycles this volume */ + uint32_t EndFile{0}; /**< Last file number */ + uint32_t EndBlock{0}; /**< Last block number */ + int32_t LabelType{0}; /**< Bareos/ANSI/IBM */ + int32_t Slot{0}; /**< >0=Slot loaded, 0=nothing, -1=unknown */ + uint32_t VolCatMaxJobs{0}; /**< Maximum Jobs to write to volume */ + uint32_t VolCatMaxFiles{0}; /**< Maximum files to write to volume */ + uint64_t VolCatMaxBytes{0}; /**< Max bytes to write to volume */ + uint64_t VolCatCapacityBytes{0}; /**< capacity estimate */ + btime_t VolReadTime{0}; /**< time spent reading */ + btime_t VolWriteTime{0}; /**< time spent writing this Volume */ + int64_t VolMediaId{0}; /**< MediaId */ + utime_t VolFirstWritten{0}; /**< Time of first write */ + utime_t VolLastWritten{0}; /**< Time of last write */ + bool InChanger{false}; /**< Set if vol in current magazine */ + bool is_valid{false}; /**< set if this data is valid */ + char VolCatStatus[20]{0}; /**< Volume status */ char VolCatName[MAX_NAME_LENGTH]{0}; /**< Desired volume to mount */ char VolEncrKey[MAX_NAME_LENGTH]{ - 0}; /**< Encryption Key needed to read the media - */ - uint32_t VolMinBlocksize = 0; /**< Volume Minimum Blocksize */ - uint32_t VolMaxBlocksize = 0; /**< Volume Maximum Blocksize */ + 0}; /**< Encryption Key needed to read the media + */ + uint32_t VolMinBlocksize{0}; /**< Volume Minimum Blocksize */ + uint32_t VolMaxBlocksize{0}; /**< Volume Maximum Blocksize */ }; struct BlockSizes { - uint32_t max_block_size = 0; - uint32_t min_block_size = 0; + uint32_t max_block_size{0}; + uint32_t min_block_size{0}; }; class DeviceResource; /* Forward reference Device resource defined in @@ -296,13 +296,13 @@ class Device { protected: int fd_ = -1; /**< File descriptor */ private: - int blocked_ = 0; /**< Set if we must wait (i.e. change tape) */ - int count_ = 0; /**< Mutex use count -- DEBUG only */ - int num_reserved_ = 0; /**< Counter of device reservations */ - slot_number_t slot_ = 0; /**< Slot loaded in drive or -1 if none */ - pthread_t pid_ = 0; /**< Thread that locked -- DEBUG only */ - bool unload_ = false; /**< Set when Volume must be unloaded */ - bool load_ = false; /**< Set when Volume must be loaded */ + int blocked_{0}; /**< Set if we must wait (i.e. change tape) */ + int count_{0}; /**< Mutex use count -- DEBUG only */ + int num_reserved_{0}; /**< Counter of device reservations */ + slot_number_t slot_{0}; /**< Slot loaded in drive or -1 if none */ + pthread_t pid_{0}; /**< Thread that locked -- DEBUG only */ + bool unload_{false}; /**< Set when Volume must be unloaded */ + bool load_{false}; /**< Set when Volume must be loaded */ public: Device() = default; @@ -368,7 +368,7 @@ class Device { btimer_t* tid = nullptr; /**< Timer id */ VolumeCatalogInfo VolCatInfo; /**< Volume Catalog Information */ - VOLUME_LABEL VolHdr; /**< Actual volume label */ + Volume_Label VolHdr; /**< Actual volume label */ char pool_name[MAX_NAME_LENGTH]{0}; /**< Pool name */ char pool_type[MAX_NAME_LENGTH]{0}; /**< Pool type */ diff --git a/core/src/stored/label.cc b/core/src/stored/label.cc index ef51c584ea4..0ff2e67eb13 100644 --- a/core/src/stored/label.cc +++ b/core/src/stored/label.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-2016 Bareos GmbH & Co. KG + 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 @@ -317,7 +317,6 @@ static bool WriteVolumeLabelToBlock(DeviceControlRecord* dcr) Dmsg0(130, "write Label in WriteVolumeLabelToBlock()\n"); - memset(&rec, 0, sizeof(rec)); rec.data = GetMemory(SER_LENGTH_Volume_Label); EmptyBlock(block); /* Volume label always at beginning */ @@ -586,8 +585,10 @@ void CreateVolumeLabel(Device* dev, const char* VolName, const char* PoolName) dev->VolHdr.HostName[0] = 0; } bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg)); - sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE); - sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__); + snprintf(dev->VolHdr.ProgVersion, sizeof(dev->VolHdr.ProgVersion), + "Ver. %.32s %.12s", VERSION, BDATE); + snprintf(dev->VolHdr.ProgDate, sizeof(dev->VolHdr.ProgDate), "Build %s %s", + __DATE__, __TIME__); dev->SetLabeled(); /* set has Bareos label */ if (debug_level >= 90) { DumpVolumeLabel(dev); } } @@ -781,7 +782,7 @@ bool UnserVolumeLabel(Device* dev, DeviceRecord* rec) return true; } -bool UnserSessionLabel(SESSION_LABEL* label, DeviceRecord* rec) +bool UnserSessionLabel(Session_Label* label, DeviceRecord* rec) { ser_declare; @@ -903,7 +904,7 @@ static void DumpSessionLabel(DeviceRecord* rec, const char* type) int dbl; struct date_time dt; struct tm tm; - SESSION_LABEL label; + Session_Label label; char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30]; UnserSessionLabel(&label, rec); @@ -1031,7 +1032,7 @@ void DumpLabelRecord(Device* dev, DeviceRecord* rec, bool verbose) break; } } else { - SESSION_LABEL label; + Session_Label label; char dt[50]; switch (rec->FileIndex) { case SOS_LABEL: diff --git a/core/src/stored/label.h b/core/src/stored/label.h index 2594b1e08da..f728ce3d361 100644 --- a/core/src/stored/label.h +++ b/core/src/stored/label.h @@ -39,7 +39,7 @@ bool WriteSessionLabel(DeviceControlRecord* dcr, int label); void DumpVolumeLabel(Device* dev); void DumpLabelRecord(Device* dev, DeviceRecord* rec, bool verbose); bool UnserVolumeLabel(Device* dev, DeviceRecord* rec); -bool UnserSessionLabel(SESSION_LABEL* label, DeviceRecord* rec); +bool UnserSessionLabel(Session_Label* label, DeviceRecord* rec); bool WriteNewVolumeLabelToDev(DeviceControlRecord* dcr, const char* VolName, const char* PoolName, diff --git a/core/src/stored/mac.cc b/core/src/stored/mac.cc index c80268434c8..f103cfdab45 100644 --- a/core/src/stored/mac.cc +++ b/core/src/stored/mac.cc @@ -138,8 +138,8 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) Dmsg0(200, "Found first SOS_LABEL and adopting job info\n"); found_first_sos_label = true; - static SESSION_LABEL the_label; - static SESSION_LABEL* label = &the_label; + static Session_Label the_label; + static Session_Label* label = &the_label; struct date_time dt; struct tm tm; diff --git a/core/src/stored/match_bsr.h b/core/src/stored/match_bsr.h index 4b6d5d16371..a8f4dd71902 100644 --- a/core/src/stored/match_bsr.h +++ b/core/src/stored/match_bsr.h @@ -25,8 +25,8 @@ namespace storagedaemon { int MatchBsr(BootStrapRecord* bsr, DeviceRecord* rec, - VOLUME_LABEL* volrec, - SESSION_LABEL* sesrec, + Volume_Label* volrec, + Session_Label* sesrec, JobControlRecord* jcr); int MatchBsrBlock(BootStrapRecord* bsr, DeviceBlock* block); void PositionBsrBlock(BootStrapRecord* bsr, DeviceBlock* block); diff --git a/core/src/stored/mount.cc b/core/src/stored/mount.cc index 301a7e27e5f..30afb66a98a 100644 --- a/core/src/stored/mount.cc +++ b/core/src/stored/mount.cc @@ -3,7 +3,7 @@ Copyright (C) 2002-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2016 Bareos GmbH & Co. KG + 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 @@ -157,11 +157,12 @@ bool DeviceControlRecord::MountNextWriteVolume() switch (AutoloadDevice(dcr, true /* writing */, NULL)) { case -3: Dmsg0(100, "Wait after a unsuccessful tape load (empty slot).\n"); - Jmsg0(jcr, M_WARNING, 0, "Wait after a unsuccessful tape load (empty slot).\n"); + Jmsg0(jcr, M_WARNING, 0, + "Wait after a unsuccessful tape load (empty slot).\n"); Bmicrosleep(1, 0); dev->VolHdr.VolumeName[0] = 0; - dev->vol=0; /* do not free volume here because in this case - * it is still on the vol_list chain for another job */ + dev->vol = 0; /* do not free volume here because in this case + * it is still on the vol_list chain for another job */ goto mount_next_vol; break; case -2: @@ -883,7 +884,8 @@ void DeviceControlRecord::ReleaseVolume() FreeVolume(dev); dev->block_num = dev->file = 0; dev->EndBlock = dev->EndFile = 0; - memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo)); + VolumeCatalogInfo empty_VolumeCatalogInfo; + dev->VolCatInfo = empty_VolumeCatalogInfo; dev->ClearVolhdr(); /* diff --git a/core/src/stored/ndmp_tape.cc b/core/src/stored/ndmp_tape.cc index b5626ba4453..e1f227d65bb 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-2016 Bareos GmbH & Co. KG + 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 @@ -1368,7 +1368,8 @@ extern "C" void* ndmp_thread_server(void* arg) memset(pfds, 0, sizeof(struct pollfd) * nfds); nfds = 0; - foreach_alist (fd_ptr, &sockfds) { + foreach_alist_null(fd_ptr, &sockfds) + { pfds[nfds].fd = fd_ptr->fd; pfds[nfds].events |= POLL_IN; nfds++; @@ -1411,7 +1412,8 @@ extern "C" void* ndmp_thread_server(void* arg) } cnt = 0; - foreach_alist (fd_ptr, &sockfds) { + foreach_alist_null(fd_ptr, &sockfds) + { if (pfds[cnt++].revents & POLLIN) { #endif /* diff --git a/core/src/stored/read_record.cc b/core/src/stored/read_record.cc index 5ecb7f95de8..d0278560962 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-2016 Bareos GmbH & Co. KG + 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 @@ -49,12 +49,12 @@ static const int debuglevel = 500; static void HandleSessionRecord(Device* dev, DeviceRecord* rec, - SESSION_LABEL* sessrec) + Session_Label* sessrec) { const char* rtype; char buf[100]; - - memset(sessrec, 0, sizeof(SESSION_LABEL)); + static const Session_Label empty_SESSION_LABEL{}; + *sessrec = empty_SESSION_LABEL; switch (rec->FileIndex) { case PRE_LABEL: rtype = _("Fresh Volume Label"); @@ -122,7 +122,8 @@ READ_CTX* new_read_context(void) READ_CTX* rctx; rctx = (READ_CTX*)malloc(sizeof(READ_CTX)); - memset(rctx, 0, sizeof(READ_CTX)); + READ_CTX empty_READ_CTX; + *rctx = empty_READ_CTX; rctx->recs = new dlist(rec, &rec->link); return rctx; @@ -187,7 +188,7 @@ void ReadContextSetRecord(DeviceControlRecord* dcr, READ_CTX* rctx) * Any fatal error sets the status bool to false. */ bool ReadNextBlockFromDevice(DeviceControlRecord* dcr, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec), bool mount_cb(DeviceControlRecord* dcr), diff --git a/core/src/stored/read_record.h b/core/src/stored/read_record.h index 4b26da8246a..1b9ca68b2c3 100644 --- a/core/src/stored/read_record.h +++ b/core/src/stored/read_record.h @@ -29,7 +29,7 @@ READ_CTX* new_read_context(void); void FreeReadContext(READ_CTX* rctx); void ReadContextSetRecord(DeviceControlRecord* dcr, READ_CTX* rctx); bool ReadNextBlockFromDevice(DeviceControlRecord* dcr, - SESSION_LABEL* sessrec, + Session_Label* sessrec, bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec), bool mount_cb(DeviceControlRecord* dcr), diff --git a/core/src/stored/record.cc b/core/src/stored/record.cc index 10a31cde9ce..9d9a2ca4d94 100644 --- a/core/src/stored/record.cc +++ b/core/src/stored/record.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2001-2012 Free Software Foundation Europe e.V. - Copyright (C) 2016-2016 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -523,7 +523,7 @@ DeviceRecord* new_record(bool with_data) DeviceRecord* rec; rec = (DeviceRecord*)GetPoolMemory(PM_RECORD); - memset(rec, 0, sizeof(DeviceRecord)); + *rec = DeviceRecord{}; if (with_data) { rec->data = GetPoolMemory(PM_MESSAGE); rec->own_mempool = true; diff --git a/core/src/stored/record.h b/core/src/stored/record.h index 3f6ecadb93b..368a156ac78 100644 --- a/core/src/stored/record.h +++ b/core/src/stored/record.h @@ -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-2016 Bareos GmbH & Co. KG + 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 @@ -119,25 +119,25 @@ struct DeviceRecord { /**< * File and Block are always returned during reading and writing records. */ - uint32_t File; /**< File number */ - uint32_t Block; /**< Block number */ - uint32_t VolSessionId; /**< Sequential id within this session */ - uint32_t VolSessionTime; /**< Session start time */ - int32_t FileIndex; /**< Sequential file number */ - int32_t Stream; /**< Full Stream number with high bits */ - int32_t maskedStream; /**< Masked Stream without high bits */ - uint32_t data_len; /**< Current record length */ - uint32_t remainder; /**< Remaining bytes to read/write */ - char state_bits[REC_STATE_BYTES]; /**< State bits */ - rec_state state; /**< State of WriteRecordToBlock */ - BootStrapRecord* bsr; /**< Pointer to bsr that matched */ - POOLMEM* data; /**< Record data. This MUST be a memory pool item */ - int32_t match_stat; /**< BootStrapRecord match status */ - uint32_t last_VolSessionId; /**< Used in sequencing FI for Vbackup */ - uint32_t last_VolSessionTime; - int32_t last_FileIndex; - int32_t last_Stream; /**< Used in SD-SD replication */ - bool own_mempool; /**< Do we own the POOLMEM pointed to in data ? */ + uint32_t File{0}; /**< File number */ + uint32_t Block{0}; /**< Block number */ + uint32_t VolSessionId{0}; /**< Sequential id within this session */ + uint32_t VolSessionTime{0}; /**< Session start time */ + int32_t FileIndex{0}; /**< Sequential file number */ + int32_t Stream{0}; /**< Full Stream number with high bits */ + int32_t maskedStream{0}; /**< Masked Stream without high bits */ + uint32_t data_len{0}; /**< Current record length */ + uint32_t remainder{0}; /**< Remaining bytes to read/write */ + char state_bits[REC_STATE_BYTES]{}; /**< State bits */ + rec_state state{st_none}; /**< State of WriteRecordToBlock */ + BootStrapRecord* bsr{nullptr}; /**< Pointer to bsr that matched */ + POOLMEM* data{nullptr}; /**< Record data. This MUST be a memory pool item */ + int32_t match_stat{0}; /**< BootStrapRecord match status */ + uint32_t last_VolSessionId{0}; /**< Used in sequencing FI for Vbackup */ + uint32_t last_VolSessionTime{0}; + int32_t last_FileIndex{0}; + int32_t last_Stream{0}; /**< Used in SD-SD replication */ + bool own_mempool{false}; /**< Do we own the POOLMEM pointed to in data ? */ }; /* @@ -165,38 +165,38 @@ struct Volume_Label { * in the Device buffer, but are not actually written * to the tape. */ - int32_t LabelType = 0; /**< This is written in header only */ - uint32_t LabelSize = 0; /**< length of serialized label */ + int32_t LabelType{}; /**< This is written in header only */ + uint32_t LabelSize{}; /**< length of serialized label */ /* * The items below this line are stored on * the tape */ - char Id[32]{0}; /**< Bareos Immortal ... */ + char Id[32]{}; /**< Bareos Immortal ... */ - uint32_t VerNum = 0; /**< Label version number */ + uint32_t VerNum{}; /**< Label version number */ /* VerNum <= 10 */ - float64_t label_date = 0.0; /**< Date tape labeled */ - float64_t label_time = 0.0; /**< Time tape labeled */ + float64_t label_date{}; /**< Date tape labeled */ + float64_t label_time{}; /**< Time tape labeled */ /* VerNum >= 11 */ - btime_t label_btime = 0; /**< tdate tape labeled */ - btime_t write_btime = 0; /**< tdate tape written */ + btime_t label_btime{}; /**< tdate tape labeled */ + btime_t write_btime{}; /**< tdate tape written */ /* Unused with VerNum >= 11 */ - float64_t write_date = 0.0; /**< Date this label written */ - float64_t write_time = 0.0; /**< Time this label written */ - - char VolumeName[MAX_NAME_LENGTH]{0}; /**< Volume name */ - char PrevVolumeName[MAX_NAME_LENGTH]{0}; /**< Previous Volume Name */ - char PoolName[MAX_NAME_LENGTH]{0}; /**< Pool name */ - char PoolType[MAX_NAME_LENGTH]{0}; /**< Pool type */ - char MediaType[MAX_NAME_LENGTH]{0}; /**< Type of this media */ - - char HostName[MAX_NAME_LENGTH]{0}; /**< Host name of writing computer */ - char LabelProg[50]{0}; /**< Label program name */ - char ProgVersion[50]{0}; /**< Program version */ - char ProgDate[50]{0}; /**< Program build date/time */ + float64_t write_date{}; /**< Date this label written */ + float64_t write_time{}; /**< Time this label written */ + + char VolumeName[MAX_NAME_LENGTH]{}; /**< Volume name */ + char PrevVolumeName[MAX_NAME_LENGTH]{}; /**< Previous Volume Name */ + char PoolName[MAX_NAME_LENGTH]{}; /**< Pool name */ + char PoolType[MAX_NAME_LENGTH]{}; /**< Pool type */ + char MediaType[MAX_NAME_LENGTH]{}; /**< Type of this media */ + + char HostName[MAX_NAME_LENGTH]{}; /**< Host name of writing computer */ + char LabelProg[50]{}; /**< Label program name */ + char ProgVersion[50]{}; /**< Program version */ + char ProgDate[50]{}; /**< Program build date/time */ }; #define SER_LENGTH_Volume_Label \ @@ -204,63 +204,61 @@ struct Volume_Label { #define SER_LENGTH_Session_Label \ 1024 /**< max serialised length of session label */ -typedef struct Volume_Label VOLUME_LABEL; - /** * Session Start/End Label * This record is at the beginning and end of each session */ struct Session_Label { - char Id[32]{0}; /**< Bareos Immortal ... */ + char Id[32]{}; /**< Bareos Immortal ... */ - uint32_t VerNum = 0; /**< Label version number */ + uint32_t VerNum{}; /**< Label version number */ - uint32_t JobId = 0; /**< Job id */ - uint32_t VolumeIndex = 0; /**< Sequence no of volume for this job */ + uint32_t JobId{}; /**< Job id */ + uint32_t VolumeIndex{}; /**< Sequence no of volume for this job */ /* VerNum >= 11 */ - btime_t write_btime = 0; /**< Tdate this label written */ + btime_t write_btime{}; /**< Tdate this label written */ /* VerNum < 11 */ - float64_t write_date = 0.0; /**< Date this label written */ + float64_t write_date{}; /**< Date this label written */ /* Unused VerNum >= 11 */ - float64_t write_time = 0.0; /**< Time this label written */ - - char PoolName[MAX_NAME_LENGTH]{0}; /**< Pool name */ - char PoolType[MAX_NAME_LENGTH]{0}; /**< Pool type */ - char JobName[MAX_NAME_LENGTH]{0}; /**< base Job name */ - char ClientName[MAX_NAME_LENGTH]{0}; - char Job[MAX_NAME_LENGTH]{0}; /**< Unique name of this Job */ - char FileSetName[MAX_NAME_LENGTH]{0}; - char FileSetMD5[MAX_NAME_LENGTH]{0}; - uint32_t JobType = 0; - uint32_t JobLevel = 0; + float64_t write_time{}; /**< Time this label written */ + + char PoolName[MAX_NAME_LENGTH]{}; /**< Pool name */ + char PoolType[MAX_NAME_LENGTH]{}; /**< Pool type */ + char JobName[MAX_NAME_LENGTH]{}; /**< base Job name */ + char ClientName[MAX_NAME_LENGTH]{}; + char Job[MAX_NAME_LENGTH]{}; /**< Unique name of this Job */ + char FileSetName[MAX_NAME_LENGTH]{}; + char FileSetMD5[MAX_NAME_LENGTH]{}; + uint32_t JobType{}; + uint32_t JobLevel{}; /* The remainder are part of EOS label only */ - uint32_t JobFiles = 0; - uint64_t JobBytes = 0; - uint32_t StartBlock = 0; - uint32_t EndBlock = 0; - uint32_t StartFile = 0; - uint32_t EndFile = 0; - uint32_t JobErrors = 0; - uint32_t JobStatus = 0; /**< Job status */ + uint32_t JobFiles{}; + uint64_t JobBytes{}; + uint32_t StartBlock{}; + uint32_t EndBlock{}; + uint32_t StartFile{}; + uint32_t EndFile{}; + uint32_t JobErrors{}; + uint32_t JobStatus{}; /**< Job status */ }; -typedef struct Session_Label SESSION_LABEL; #define SERIAL_BUFSIZE 1024 /**< Volume serialisation buffer size */ /** * Read context used to keep track of what is processed or not. */ +/* clang-format off */ struct Read_Context { - DeviceRecord* rec = nullptr; /**< Record currently being processed */ - 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 */ + DeviceRecord* rec = nullptr; /**< Record currently being processed */ + 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 */ }; +/* clang-format on */ struct DelayedDataStream { int32_t stream = 0; /**< stream less new bits */ diff --git a/core/src/stored/reserve.cc b/core/src/stored/reserve.cc index d4a4a8526db..100219cf107 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-2016 Bareos GmbH & Co. KG + Copyright (C) 2016-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 @@ -406,7 +406,8 @@ static bool IsVolInAutochanger(ReserveContext& rctx, VolumeReservationItem* vol) * Find resource, and make sure we were able to open it */ if (bstrcmp(rctx.device_name, changer->resource_name_)) { - Dmsg1(debuglevel, "Found changer device %s\n", vol->dev->device->resource_name_); + Dmsg1(debuglevel, "Found changer device %s\n", + vol->dev->device->resource_name_); return true; } Dmsg1(debuglevel, "Incorrect changer device %s\n", changer->resource_name_); @@ -425,7 +426,7 @@ bool FindSuitableDeviceForJob(JobControlRecord* jcr, ReserveContext& rctx) { bool ok = false; DirectorStorage* store; - char* device_name; + char* device_name = nullptr; alist* dirstore; DeviceControlRecord* dcr = jcr->dcr; @@ -572,8 +573,8 @@ int SearchResForDevice(ReserveContext& rctx) * Look through Autochangers first */ foreach_res (changer, R_AUTOCHANGER) { - Dmsg2(debuglevel, "Try match changer res=%s, wanted %s\n", changer->resource_name_, - rctx.device_name); + Dmsg2(debuglevel, "Try match changer res=%s, wanted %s\n", + changer->resource_name_, rctx.device_name); /* * Find resource, and make sure we were able to open it */ @@ -582,7 +583,8 @@ int SearchResForDevice(ReserveContext& rctx) * Try each device in this AutoChanger */ foreach_alist (rctx.device, changer->device) { - Dmsg1(debuglevel, "Try changer device %s\n", rctx.device->resource_name_); + Dmsg1(debuglevel, "Try changer device %s\n", + rctx.device->resource_name_); if (!rctx.device->autoselect) { Dmsg1(100, "Device %s not autoselect skipped.\n", rctx.device->resource_name_); @@ -601,7 +603,8 @@ int SearchResForDevice(ReserveContext& rctx) rctx.device->resource_name_, rctx.jcr->dcr->dev->NumReserved()); } else { Dmsg2(debuglevel, "Device %s reserved=%d for read.\n", - rctx.device->resource_name_, rctx.jcr->read_dcr->dev->NumReserved()); + rctx.device->resource_name_, + rctx.jcr->read_dcr->dev->NumReserved()); } return status; } @@ -613,8 +616,8 @@ int SearchResForDevice(ReserveContext& rctx) */ if (!rctx.autochanger_only) { foreach_res (rctx.device, R_DEVICE) { - Dmsg2(debuglevel, "Try match res=%s wanted %s\n", rctx.device->resource_name_, - rctx.device_name); + Dmsg2(debuglevel, "Try match res=%s wanted %s\n", + rctx.device->resource_name_, rctx.device_name); /* * Find resource, and make sure we were able to open it @@ -632,7 +635,8 @@ int SearchResForDevice(ReserveContext& rctx) rctx.device->resource_name_, rctx.jcr->dcr->dev->NumReserved()); } else { Dmsg2(debuglevel, "Device %s reserved=%d for read.\n", - rctx.device->resource_name_, rctx.jcr->read_dcr->dev->NumReserved()); + rctx.device->resource_name_, + rctx.jcr->read_dcr->dev->NumReserved()); } return status; } @@ -661,10 +665,12 @@ int SearchResForDevice(ReserveContext& rctx) */ if (rctx.store->append == SD_APPEND) { Dmsg2(debuglevel, "Device %s reserved=%d for append.\n", - rctx.device->resource_name_, rctx.jcr->dcr->dev->NumReserved()); + rctx.device->resource_name_, + rctx.jcr->dcr->dev->NumReserved()); } else { Dmsg2(debuglevel, "Device %s reserved=%d for read.\n", - rctx.device->resource_name_, rctx.jcr->read_dcr->dev->NumReserved()); + rctx.device->resource_name_, + rctx.jcr->read_dcr->dev->NumReserved()); } return status; } diff --git a/core/src/stored/sd_stats.cc b/core/src/stored/sd_stats.cc index 0229bda0f12..c0320884dff 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-2016 Bareos GmbH & Co. KG + 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 @@ -54,49 +54,49 @@ static pthread_cond_t wait_for_next_run = PTHREAD_COND_INITIALIZER; struct device_statistic { dlink link; - bool collected; - utime_t timestamp; - btime_t DevReadTime; - btime_t DevWriteTime; - uint64_t DevWriteBytes; - uint64_t DevReadBytes; - uint64_t spool_size; - int num_waiting; - int num_writers; - DBId_t MediaId; - uint64_t VolCatBytes; - uint64_t VolCatFiles; - uint64_t VolCatBlocks; + bool collected{false}; + utime_t timestamp{0}; + btime_t DevReadTime{0}; + btime_t DevWriteTime{0}; + uint64_t DevWriteBytes{0}; + uint64_t DevReadBytes{0}; + uint64_t spool_size{0}; + int num_waiting{0}; + int num_writers{0}; + DBId_t MediaId{0}; + uint64_t VolCatBytes{0}; + uint64_t VolCatFiles{0}; + uint64_t VolCatBlocks{0}; }; struct device_tapealert { dlink link; - utime_t timestamp; - uint64_t flags; + utime_t timestamp{0}; + uint64_t flags{0}; }; struct device_statistics { dlink link; - char DevName[MAX_NAME_LENGTH]; - struct device_statistic* cached; - dlist* statistics; - dlist* tapealerts; + char DevName[MAX_NAME_LENGTH]{}; + struct device_statistic* cached{nullptr}; + dlist* statistics{nullptr}; + dlist* tapealerts{nullptr}; }; struct job_statistic { dlink link; - bool collected; - utime_t timestamp; - uint32_t JobFiles; - uint64_t JobBytes; - char* DevName; + bool collected{false}; + utime_t timestamp{0}; + uint32_t JobFiles{0}; + uint64_t JobBytes{0}; + char* DevName{nullptr}; }; struct job_statistics { dlink link; - uint32_t JobId; - struct job_statistic* cached; - dlist* statistics; + uint32_t JobId{0}; + struct job_statistic* cached{nullptr}; + dlist* statistics{nullptr}; }; static dlist* device_statistics = NULL; @@ -129,7 +129,8 @@ void UpdateDeviceTapealert(const char* devname, uint64_t flags, utime_t now) if (!found) { dev_stats = (struct device_statistics*)malloc(sizeof(struct device_statistics)); - memset(dev_stats, 0, sizeof(struct device_statistics)); + struct device_statistics empty_device_statistics; + *dev_stats = empty_device_statistics; bstrncpy(dev_stats->DevName, devname, sizeof(dev_stats->DevName)); P(mutex); @@ -142,7 +143,8 @@ void UpdateDeviceTapealert(const char* devname, uint64_t flags, utime_t now) */ tape_alert = (struct device_tapealert*)malloc(sizeof(struct device_tapealert)); - memset(tape_alert, 0, sizeof(struct device_tapealert)); + struct device_tapealert empty_device_tapealert; + *tape_alert = empty_device_tapealert; tape_alert->timestamp = now; tape_alert->flags = flags; @@ -195,7 +197,8 @@ static inline void UpdateDeviceStatistics(const char* devname, } else if (!found) { dev_stats = (struct device_statistics*)malloc(sizeof(struct device_statistics)); - memset(dev_stats, 0, sizeof(struct device_statistics)); + struct device_statistics empty_device_statistics; + *dev_stats = empty_device_statistics; bstrncpy(dev_stats->DevName, devname, sizeof(dev_stats->DevName)); P(mutex); @@ -207,7 +210,9 @@ static inline void UpdateDeviceStatistics(const char* devname, * Add a new set of statistics. */ dev_stat = (struct device_statistic*)malloc(sizeof(struct device_statistic)); - memset(dev_stat, 0, sizeof(struct device_statistic)); + + struct device_statistic empty_device_statistic; + *dev_stat = empty_device_statistic; dev_stat->timestamp = now; dev_stat->DevReadTime = dev->DevReadTime; @@ -281,7 +286,8 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) } } else if (!found) { job_stats = (struct job_statistics*)malloc(sizeof(struct job_statistics)); - memset(job_stats, 0, sizeof(struct job_statistics)); + struct job_statistics empty_job_statistics; + *job_stats = empty_job_statistics; job_stats->JobId = jcr->JobId; P(mutex); @@ -293,7 +299,8 @@ void UpdateJobStatistics(JobControlRecord* jcr, utime_t now) * Add a new set of statistics. */ job_stat = (struct job_statistic*)malloc(sizeof(struct job_statistic)); - memset(job_stat, 0, sizeof(struct job_statistic)); + struct job_statistic empty_job_statistic; + *job_stat = empty_job_statistic; job_stat->timestamp = now; job_stat->JobFiles = jcr->JobFiles; diff --git a/core/src/stored/stored.cc b/core/src/stored/stored.cc index f4e650a7347..b446d70088f 100644 --- a/core/src/stored/stored.cc +++ b/core/src/stored/stored.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-2016 Bareos GmbH & Co. KG + 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 @@ -133,7 +133,6 @@ int main(int argc, char* argv[]) char* uid = NULL; char* gid = NULL; - start_heap = sbrk(0); setlocale(LC_ALL, ""); bindtextdomain("bareos", LOCALEDIR); textdomain("bareos"); diff --git a/core/src/stored/stored_globals.cc b/core/src/stored/stored_globals.cc index 4d4fd4fa899..b06547b6f6d 100644 --- a/core/src/stored/stored_globals.cc +++ b/core/src/stored/stored_globals.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-2016 Bareos GmbH & Co. KG + 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 @@ -30,7 +30,6 @@ ConfigurationParser* my_config = nullptr; StorageResource* me; char* configfile; -void* start_heap = nullptr; bool init_done = false; uint32_t vol_session_time; diff --git a/core/src/stored/vol_mgr.cc b/core/src/stored/vol_mgr.cc index 12a04aea1c9..9efdf5c587e 100644 --- a/core/src/stored/vol_mgr.cc +++ b/core/src/stored/vol_mgr.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2013 Free Software Foundation Europe e.V. - Copyright (C) 2015-2016 Bareos GmbH & Co. KG + Copyright (C) 2015-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 @@ -190,8 +190,6 @@ void RemoveReadVolume(JobControlRecord* jcr, const char* VolumeName) VolumeReservationItem vol, *fvol; LockReadVolumes(); - - memset(&vol, 0, sizeof(vol)); vol.vol_name = strdup(VolumeName); vol.SetJobid(jcr->JobId); @@ -230,8 +228,6 @@ static VolumeReservationItem* find_read_volume(const char* VolumeName) * Do not lock reservations here */ LockReadVolumes(); - - memset(&vol, 0, sizeof(vol)); vol.vol_name = strdup(VolumeName); /* @@ -283,9 +279,10 @@ static VolumeReservationItem* new_vol_item(DeviceControlRecord* dcr, const char* VolumeName) { VolumeReservationItem* vol; + VolumeReservationItem emptyVol; vol = (VolumeReservationItem*)malloc(sizeof(VolumeReservationItem)); - memset(vol, 0, sizeof(VolumeReservationItem)); + *vol = emptyVol; vol->vol_name = strdup(VolumeName); if (dcr) { vol->dev = dcr->dev; @@ -529,7 +526,7 @@ VolumeReservationItem* reserve_volume(DeviceControlRecord* dcr, dcr->SetDev(vol->dev); /* temp point to other dev */ slot = GetAutochangerLoadedSlot(dcr); /* get slot on other drive */ dcr->SetDev(dev); /* restore dev */ - vol->SetSlotNumber(slot); /* save slot */ + vol->SetSlotNumber(slot); /* save slot */ vol->dev->SetUnload(); /* unload the other drive */ vol->SetSwapping(); /* swap from other drive */ dev->swap_dev = vol->dev; /* remember to get this vol */ diff --git a/core/src/stored/vol_mgr.h b/core/src/stored/vol_mgr.h index 630c590233c..3a5f1bb11ff 100644 --- a/core/src/stored/vol_mgr.h +++ b/core/src/stored/vol_mgr.h @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2000-2013 Free Software Foundation Europe e.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + 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 @@ -62,17 +62,17 @@ void ReadVolWalkEnd(VolumeReservationItem* vol); * Volume reservation class -- see vol_mgr.c and reserve.c */ class VolumeReservationItem { - bool swapping_; /**< set when swapping to another drive */ - bool in_use_; /**< set when volume reserved or in use */ - bool reading_; /**< set when reading */ - slot_number_t slot_; /**< slot of swapping volume */ - uint32_t JobId_; /**< JobId for read volumes */ - volatile int32_t use_count_; /**< Use count */ - pthread_mutex_t mutex_; /**< Vol muntex */ + bool swapping_{false}; /**< set when swapping to another drive */ + bool in_use_{false}; /**< set when volume reserved or in use */ + bool reading_{false}; /**< set when reading */ + slot_number_t slot_{0}; /**< slot of swapping volume */ + uint32_t JobId_{0}; /**< JobId for read volumes */ + volatile int32_t use_count_{0}; /**< Use count */ + pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Vol muntex */ public: dlink link; - char* vol_name; /**< Volume name */ - Device* dev; /**< Pointer to device to which we are attached */ + char* vol_name{nullptr}; /**< Volume name */ + Device* dev{nullptr}; /**< Pointer to device to which we are attached */ void InitMutex() { pthread_mutex_init(&mutex_, NULL); } void DestroyMutex() { pthread_mutex_destroy(&mutex_); } diff --git a/core/src/tests/bsock_mock.h b/core/src/tests/bsock_mock.h index 7c6856f6c8b..c70265c9e36 100644 --- a/core/src/tests/bsock_mock.h +++ b/core/src/tests/bsock_mock.h @@ -37,11 +37,18 @@ #include +#ifdef __GNUC__ +#ifndef __clang__ +/* ignore the suggest-override warnings caused by MOCK_METHODx */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsuggest-override" +#endif +#endif + class BareosSocketMock : public BareosSocket { public: BareosSocketMock() : BareosSocket() {} ~BareosSocketMock(); - MOCK_METHOD0(clone, BareosSocket*()); MOCK_METHOD9(connect, bool(JobControlRecord*, @@ -83,7 +90,11 @@ class BareosSocketMock : public BareosSocket { utime_t, int*)); }; - +#ifdef __GNUC__ +#ifndef __clang__ +#pragma GCC diagnostic pop +#endif +#endif /* define a gmock action that fills bsock->msg so we can recv() a message */ ACTION_P2(BareosSocket_Recv, bsock, msg) { diff --git a/core/src/tests/bsock_test.cc b/core/src/tests/bsock_test.cc index dd3c2266b03..ab848d764a3 100644 --- a/core/src/tests/bsock_test.cc +++ b/core/src/tests/bsock_test.cc @@ -1,7 +1,7 @@ /* BAREOS® - Backup Archiving REcovery Open Sourced - Copyright (C) 2018-2018 Bareos GmbH & Co. KG + Copyright (C) 2018-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 @@ -47,9 +47,6 @@ #define CLIENT_AS_A_THREAD 0 -class UaContext { - BareosSocket* UA_sock; -}; class StorageResource; #include "dird/authenticate.h" diff --git a/core/src/tests/htable_test.cc b/core/src/tests/htable_test.cc index 07901e9246b..00f6ad3a5af 100644 --- a/core/src/tests/htable_test.cc +++ b/core/src/tests/htable_test.cc @@ -2,7 +2,7 @@ BAREOS® - Backup Archiving REcovery Open Sourced Copyright (C) 2003-2011 Free Software Foundation Europe e.V. - Copyright (C) 2014-2017 Bareos GmbH & Co. KG + Copyright (C) 2014-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 @@ -88,12 +88,3 @@ struct RbListJobControlRecord { char* buf; }; -static int RblistCompare(void* item1, void* item2) -{ - RbListJobControlRecord *jcr1, *jcr2; - int comp; - jcr1 = (RbListJobControlRecord*)item1; - jcr2 = (RbListJobControlRecord*)item2; - comp = strcmp(jcr1->buf, jcr2->buf); - return comp; -} diff --git a/core/src/tests/ndmp_address_translate_test.cc b/core/src/tests/ndmp_address_translate_test.cc index 489ecde2648..a9bd669a5cd 100644 --- a/core/src/tests/ndmp_address_translate_test.cc +++ b/core/src/tests/ndmp_address_translate_test.cc @@ -26,7 +26,7 @@ #include "dird/storage.h" -void print_smc_aa(directordaemon::smc_element_address_assignment smc_elem_aa) +void print_smc_aa(smc_element_address_assignment smc_elem_aa) { printf("drive: %d, %d\n", smc_elem_aa.dte_addr, smc_elem_aa.dte_count); printf("robot: %d, %d\n", smc_elem_aa.mte_addr, smc_elem_aa.mte_count); @@ -34,7 +34,7 @@ void print_smc_aa(directordaemon::smc_element_address_assignment smc_elem_aa) printf("slot: %d, %d\n\n", smc_elem_aa.se_addr, smc_elem_aa.se_count); } -void init_smc_aa(directordaemon::smc_element_address_assignment& smc_elem_aa) +void init_smc_aa(smc_element_address_assignment& smc_elem_aa) { // Tapes Drives smc_elem_aa.dte_addr = 1000; @@ -81,7 +81,7 @@ SLOT ADDR TEST(ndmp, slot_to_element_addr) { - directordaemon::smc_element_address_assignment smc_elem_aa; + smc_element_address_assignment smc_elem_aa; init_smc_aa(smc_elem_aa); print_smc_aa(smc_elem_aa); @@ -136,7 +136,7 @@ TEST(ndmp, slot_to_element_addr) TEST(ndmp, element_addr_to_slot) { - directordaemon::smc_element_address_assignment smc_elem_aa; + smc_element_address_assignment smc_elem_aa; init_smc_aa(smc_elem_aa); print_smc_aa(smc_elem_aa); diff --git a/core/src/tests/test_dir_plugins.cc b/core/src/tests/test_dir_plugins.cc index 2e0f0910b20..334d4c0ad49 100644 --- a/core/src/tests/test_dir_plugins.cc +++ b/core/src/tests/test_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-2016 Bareos GmbH & Co. KG + 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 @@ -56,8 +56,11 @@ TEST(dir, dir_plugins) InitMsg(NULL, NULL); OSDependentInit(); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" + char* cwd = getcwd(plugin_dir, sizeof(plugin_dir) - 1); +#pragma GCC diagnostic pop - getcwd(plugin_dir, sizeof(plugin_dir) - 1); LoadDirPlugins(plugin_dir, NULL); jcr1->JobId = 111; diff --git a/core/src/tests/test_fd_plugins.cc b/core/src/tests/test_fd_plugins.cc index 5aa756ee755..8cd32c55fd7 100644 --- a/core/src/tests/test_fd_plugins.cc +++ b/core/src/tests/test_fd_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-2018 Bareos GmbH & Co. KG + 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 @@ -98,7 +98,11 @@ TEST(fd, fd_plugins) OSDependentInit(); - getcwd(plugin_dir, sizeof(plugin_dir) - 1); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" + char* cwd = getcwd(plugin_dir, sizeof(plugin_dir) - 1); +#pragma GCC diagnostic pop + LoadFdPlugins(plugin_dir, NULL); jcr1->JobId = 111; diff --git a/core/src/tests/test_sd_plugins.cc b/core/src/tests/test_sd_plugins.cc index 602a160f1e0..df9c0604888 100644 --- a/core/src/tests/test_sd_plugins.cc +++ b/core/src/tests/test_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-2016 Bareos GmbH & Co. KG + 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 @@ -45,7 +45,11 @@ TEST(sd, sd_plugins) OSDependentInit(); - getcwd(plugin_dir, sizeof(plugin_dir) - 1); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" + char* cwd = getcwd(plugin_dir, sizeof(plugin_dir) - 1); +#pragma GCC diagnostic pop + LoadSdPlugins(plugin_dir, NULL); jcr1->JobId = 111; diff --git a/core/src/win32/compat/compat.cc b/core/src/win32/compat/compat.cc index abb0ba123f8..c52c61355f3 100644 --- a/core/src/win32/compat/compat.cc +++ b/core/src/win32/compat/compat.cc @@ -39,7 +39,10 @@ #include "lib/bsignal.h" #include "lib/daemon.h" #include "lib/berrno.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop /** * Sanity check to make sure FILE_ATTRIBUTE_VALID_FLAGS is always smaller @@ -3416,11 +3419,11 @@ Bpipe* OpenBpipe(char* prog, int wait, const char* mode, bool dup_stderr) int kill(int pid, int signal) { int rval = 0; - if (!TerminateProcess((HANDLE)pid, (UINT)signal)) { + if (!TerminateProcess((HANDLE)(UINT_PTR)pid, (UINT)signal)) { rval = -1; errno = b_errno_win32; } - CloseHandle((HANDLE)pid); + CloseHandle((HANDLE)(UINT_PTR)pid); return rval; } diff --git a/core/src/win32/filed/vss_generic.cc b/core/src/win32/filed/vss_generic.cc index 3b7825890ee..090bd95682d 100644 --- a/core/src/win32/filed/vss_generic.cc +++ b/core/src/win32/filed/vss_generic.cc @@ -108,7 +108,10 @@ class IXMLDOMDocument; #define VSS_ERROR_OBJECT_ALREADY_EXISTS 0x8004230D +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vss.h" +#pragma GCC diagnostic pop using namespace filedaemon; diff --git a/core/src/win32/plugins/filed/mssqlvdi-fd.cc b/core/src/win32/plugins/filed/mssqlvdi-fd.cc index d8199cfae11..4e3a95c42e8 100644 --- a/core/src/win32/plugins/filed/mssqlvdi-fd.cc +++ b/core/src/win32/plugins/filed/mssqlvdi-fd.cc @@ -36,9 +36,12 @@ /** * Microsoft® MSSQL Virtual Device Interface (VDI) */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunknown-pragmas" #include "vdi.h" #include "vdierror.h" #include "vdiguid.h" +#pragma GCC diagnostic pop /** * Microsoft® ActiveX® Data Objects diff --git a/vmware/vadp_dumper/CMakeLists.txt b/vmware/vadp_dumper/CMakeLists.txt index 0a693d3ba67..79960bc9bb5 100644 --- a/vmware/vadp_dumper/CMakeLists.txt +++ b/vmware/vadp_dumper/CMakeLists.txt @@ -1,9 +1,9 @@ include_directories(/usr/lib/vmware-vix-disklib/include) link_directories(/usr/lib/vmware-vix-disklib/lib64) add_executable(bareos_vadp_dumper - bareos_vadp_dumper.cpp - cbuf.cpp - copy_thread.cpp + bareos_vadp_dumper.cc + cbuf.cc + copy_thread.cc ) target_link_libraries(bareos_vadp_dumper vixDiskLib diff --git a/vmware/vadp_dumper/bareos_vadp_dumper.cc b/vmware/vadp_dumper/bareos_vadp_dumper.cc new file mode 100644 index 00000000000..346117eab0e --- /dev/null +++ b/vmware/vadp_dumper/bareos_vadp_dumper.cc @@ -0,0 +1,1765 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2014-2019 Bareos GmbH & Co. KG + Copyright (C) 2015-2015 Planets Communications B.V. + + 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, which is + listed in the file LICENSE.vadp. + + 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. +*/ +/* + * VADP Dumper - vStorage APIs for Data Protection Dumper program. + * + */ +#include +#include +#include +#include +#include +#include +#include + +#include "copy_thread.h" + +#include + +/* + * json_array_foreach macro was added in jansson version 2.5 + * we can compile also against an lower version if we define it ourselves. + */ +#if JANSSON_VERSION_HEX < 0x020500 +#define json_array_foreach(array, index, value) \ + for (index = 0; index < json_array_size(array) && \ + (value = json_array_get(array, index)); \ + index++) +#endif + +#include + +#define VIXDISKLIB_VERSION_MAJOR 6 +#define VIXDISKLIB_VERSION_MINOR 5 + +#define VSPHERE_DEFAULT_ADMIN_PORT 0 + +/* + * VixDiskLib does all processing in sectors of 512 bytes. + */ +#define DEFAULT_SECTOR_SIZE VIXDISKLIB_SECTOR_SIZE + +/* + * In each call to the VixDiskLib read/write this number of sectors. + * e.g. 512 means 256 Kb per call (e.g. 512 x 512 bytes) + */ +#define SECTORS_PER_CALL 1024 + +#define MIN(a, b) ((a) < b) ? (a) : (b) +#define MAX(a, b) ((a) > b) ? (a) : (b) + +#define CON_PARAMS_KEY "ConnParams" +#define CON_PARAMS_VM_MOREF_KEY "VmMoRef" +#define CON_PARAMS_HOST_KEY "VsphereHostName" +#define CON_PARAMS_THUMBPRINT_KEY "VsphereThumbPrint" +#define CON_PARAMS_USERNAME_KEY "VsphereUsername" +#define CON_PARAMS_PASSWORD_KEY "VspherePassword" +#define CON_PARAMS_SNAPSHOT_MOREF_KEY "VsphereSnapshotMoRef" + +#define DISK_PARAMS_KEY "DiskParams" +#define DISK_PARAMS_DISK_PATH_KEY "diskPath" + +#define CBT_DISKCHANGEINFO_KEY "DiskChangeInfo" +#define CBT_DISK_SIZE "length" +#define CBT_CHANGEDAREA_KEY "changedArea" +#define CBT_CHANGEDAREA_START_KEY "start" +#define CBT_CHANGEDAREA_LENGTH_KEY "length" +#define CBT_START_OFFSET "startOffset" + +#define BAREOSMAGIC 0x12122012u +#define PROTOCOL_VERSION 1 + +#define BAREOS_VADPDUMPER_IDENTITY "BareosVADPDumper" + +struct disk_type { + const char* type; + VixDiskLibDiskType vadp_type; +}; + +static struct disk_type disk_types[] = { + {"monolithic_sparse", VIXDISKLIB_DISK_MONOLITHIC_SPARSE}, + {"monolithic_flat", VIXDISKLIB_DISK_MONOLITHIC_FLAT}, + {"split_sparse", VIXDISKLIB_DISK_SPLIT_SPARSE}, + {"split_flat", VIXDISKLIB_DISK_SPLIT_FLAT}, + {"vmfs_flat", VIXDISKLIB_DISK_VMFS_FLAT}, + {"optimized", VIXDISKLIB_DISK_STREAM_OPTIMIZED}, + {"vmfs_thin", VIXDISKLIB_DISK_VMFS_THIN}, + {"vmfs_sparse", VIXDISKLIB_DISK_VMFS_SPARSE}, + {NULL, VIXDISKLIB_DISK_UNKNOWN}}; + +/* + * Generic identification structure, 128 bytes with padding. + * This includes a protocol version. + */ +struct runtime_disk_info_encoding { + uint32_t start_magic; + uint32_t protocol_version; + uint64_t absolute_disk_length; + uint64_t absolute_start_offset; + uint32_t bios_cylinders; + uint32_t bios_heads; + uint32_t bios_sectors; + uint32_t phys_cylinders; + uint32_t phys_heads; + uint32_t phys_sectors; + uint64_t phys_capacity; + uint32_t adapter_type; + uint32_t padding[16]; + uint32_t end_magic; +}; +const int rdie_size = sizeof(struct runtime_disk_info_encoding); + +/* + * Disk Meta data structure, + * Encodes what follows e.g. meta_key and meta_data. + * e.g. [META_META_DATA] [META_DATA_KEY] [META_DATA] ... + */ +struct runtime_meta_data_encoding { + uint32_t start_magic; + uint32_t meta_key_length; + uint32_t meta_data_length; + uint32_t end_magic; +}; +const int rmde_size = sizeof(struct runtime_meta_data_encoding); + +/* + * Changed Block Tracking structure. + * Encodes the CBT data followed by the actual data. + * e.g. [CBT] [DATA] ... + */ +struct runtime_cbt_encoding { + uint32_t start_magic; + uint64_t start_offset; + uint64_t offset_length; + uint32_t end_magic; +}; +const int rce_size = sizeof(struct runtime_cbt_encoding); + +static bool cleanup_on_start = false; +static bool cleanup_on_disconnect = false; +static bool save_metadata = false; +static bool verbose = false; +static bool check_size = true; +static bool create_disk = false; +static bool local_vmdk = false; +static bool multi_threaded = false; +static bool restore_meta_data = false; +static uint64_t sectors_per_call = SECTORS_PER_CALL; +static uint64_t absolute_start_offset = 0; +static char* vmdk_disk_name = NULL; +static char* raw_disk_name = NULL; +static int raw_disk_fd = -1; +static char* force_transport = NULL; +static char* disktype = NULL; +static VixDiskLibConnectParams cnxParams; +static VixDiskLibConnection connection = NULL; +static VixDiskLibHandle read_diskHandle = NULL; +static VixDiskLibHandle write_diskHandle = NULL; +static VixDiskLibInfo* info = NULL; +static json_t* json_config = NULL; +static int exit_code = 1; + +/* + * Encode the VDDK VixDiskLibInfo into an internal representation. + */ +static inline void fill_runtime_disk_info_encoding( + struct runtime_disk_info_encoding* rdie) +{ + memset(rdie, 0, rdie_size); + + rdie->protocol_version = PROTOCOL_VERSION; + rdie->start_magic = BAREOSMAGIC; + rdie->end_magic = BAREOSMAGIC; + + if (info->biosGeo.cylinders > 0) { + rdie->bios_cylinders = info->biosGeo.cylinders; + } else { + rdie->bios_cylinders = info->physGeo.cylinders; + } + if (info->biosGeo.heads > 0) { + rdie->bios_heads = info->biosGeo.heads; + } else { + rdie->bios_heads = info->physGeo.heads; + } + if (info->biosGeo.sectors > 0) { + rdie->bios_sectors = info->biosGeo.sectors; + } else { + rdie->bios_sectors = info->physGeo.sectors; + } + + rdie->phys_cylinders = info->physGeo.cylinders; + rdie->phys_heads = info->physGeo.heads; + rdie->phys_sectors = info->physGeo.sectors; + + rdie->phys_capacity = info->capacity; + rdie->adapter_type = info->adapterType; +} + +/* + * Dump the important content of the internal disk representation for verbose + * mode. + */ +static inline void dump_runtime_disk_info_encoding( + struct runtime_disk_info_encoding* rdie) +{ + fprintf(stderr, "Protocol version = %u\n", rdie->protocol_version); + fprintf(stderr, "Absolute disk length = %lu\n", rdie->absolute_disk_length); + fprintf(stderr, "Absolute start offset = %lu\n", rdie->absolute_start_offset); + fprintf(stderr, "BIOS geometry (%u cyl, %u heads, %u sectors)\n", + rdie->bios_cylinders, rdie->bios_heads, rdie->bios_sectors); + fprintf(stderr, "PHYS geometry (%u cyl, %u heads, %u sectors)\n", + rdie->phys_cylinders, rdie->phys_heads, rdie->phys_sectors); + fprintf(stderr, "Physical capacity %lu\n", rdie->phys_capacity); + fprintf(stderr, "Adapter Type %u\n", rdie->adapter_type); +} + +/* + * Validate the disk sizes from the internal disk representation to the current + * VDMK settings. + */ +static inline char validate_runtime_disk_info_encoding( + struct runtime_disk_info_encoding* rdie) +{ + if (info->biosGeo.cylinders > 0 && + info->biosGeo.cylinders < rdie->bios_cylinders) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u BIOS " + "cylinders original had %u\n", + info->biosGeo.cylinders, rdie->bios_cylinders); + goto bail_out; + } + + if (info->biosGeo.heads > 0 && info->biosGeo.heads < rdie->bios_heads) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u BIOS heads " + "original had %u\n", + info->biosGeo.heads, rdie->bios_heads); + goto bail_out; + } + + if (info->biosGeo.cylinders > 0 && + info->biosGeo.cylinders < rdie->bios_cylinders) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u BIOS " + "sectors original had %u\n", + info->biosGeo.sectors, rdie->bios_sectors); + goto bail_out; + } + + if (info->physGeo.cylinders < rdie->phys_cylinders) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u PHYS " + "cylinders original had %u\n", + info->physGeo.cylinders, rdie->phys_cylinders); + goto bail_out; + } + + if (info->physGeo.heads < rdie->phys_heads) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u PHYS heads " + "original had %u\n", + info->biosGeo.heads, rdie->phys_heads); + goto bail_out; + } + + if (info->physGeo.cylinders < rdie->phys_cylinders) { + fprintf(stderr, + "[validate_runtime_disk_info_encoding] New disk has %u PHYS " + "sectors original had %u\n", + info->biosGeo.sectors, rdie->phys_sectors); + goto bail_out; + } + + return 1; + +bail_out: + return 0; +} + +/* + * Writer function that handles partial writes. + */ +static inline size_t robust_writer(int fd, void* buffer, int size) +{ + size_t total_bytes = 0; + size_t cnt = 0; + + do { + cnt = write(fd, (char*)buffer + total_bytes, size); + if (cnt > 0) { + size -= cnt; + total_bytes += cnt; + } else if (cnt < 0) { + total_bytes = -1; + goto bail_out; + } + } while (size > 0 && cnt > 0); + +bail_out: + return total_bytes; +} + +/* + * Reader function that handles partial reads. + */ +static inline size_t robust_reader(int fd, void* buffer, int size) +{ + size_t total_bytes = 0; + size_t cnt = 0; + + do { + cnt = read(fd, (char*)buffer + total_bytes, size); + if (cnt > 0) { + size -= cnt; + total_bytes += cnt; + } else if (cnt < 0) { + total_bytes = -1; + goto bail_out; + } + } while (size > 0 && cnt > 0); + +bail_out: + return total_bytes; +} + +/* + * VDDK helper functions. + */ +static void LogFunction(const char* fmt, va_list args) +{ + if (verbose) { + fprintf(stderr, "Log: "); + vfprintf(stderr, fmt, args); + } +} + +static void WarningFunction(const char* fmt, va_list args) +{ + fprintf(stderr, "Warning: "); + vfprintf(stderr, fmt, args); +} + +static void PanicFunction(const char* fmt, va_list args) +{ + fprintf(stderr, "Log: "); + vfprintf(stderr, fmt, args); + exit_code = 10; + exit(exit_code); +} + +static inline void cleanup_cnxParams() +{ + if (cnxParams.vmxSpec) { + free(cnxParams.vmxSpec); + cnxParams.vmxSpec = NULL; + } + + if (cnxParams.serverName) { + free(cnxParams.serverName); + cnxParams.serverName = NULL; + } + + if (cnxParams.creds.uid.userName) { + free(cnxParams.creds.uid.userName); + cnxParams.creds.uid.userName = NULL; + } + + if (cnxParams.creds.uid.password) { + free(cnxParams.creds.uid.password); + cnxParams.creds.uid.password = NULL; + } + + if (cnxParams.thumbPrint) { + free(cnxParams.thumbPrint); + cnxParams.thumbPrint = NULL; + } +} + +static inline void cleanup_vixdisklib() +{ + uint32_t numCleanedUp, numRemaining; + + VixDiskLib_Cleanup(&cnxParams, &numCleanedUp, &numRemaining); +} + +/* + * Generic cleanup function. + */ +static void cleanup(void) +{ + VixError err; + + if (info) { VixDiskLib_FreeInfo(info); } + + if (read_diskHandle) { + VixDiskLib_Close(read_diskHandle); + read_diskHandle = NULL; + } + + if (write_diskHandle) { + VixDiskLib_Close(write_diskHandle); + write_diskHandle = NULL; + } + + if (connection) { + VixDiskLib_Disconnect(connection); + if (cleanup_on_disconnect) { cleanup_vixdisklib(); } + } + + if (!local_vmdk) { + err = VixDiskLib_EndAccess(&cnxParams, BAREOS_VADPDUMPER_IDENTITY); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to End Access: %s [%lu]\n", error_txt, err); + VixDiskLib_FreeErrorText(error_txt); + } + } + + if (raw_disk_fd != -1) { + if (verbose) { fprintf(stderr, "Log: RAWFILE: Closing RAW file\n"); } + close(raw_disk_fd); + } + + cleanup_cnxParams(); + + VixDiskLib_Exit(); + + if (json_config) { json_decref(json_config); } + + if (vmdk_disk_name) { free(vmdk_disk_name); } + + if (force_transport) { free(force_transport); } + + if (disktype) { free(disktype); } + + _exit(exit_code); +} + +/* + * Convert the configured disktype to the right VADP type. + */ +static inline VixDiskLibDiskType lookup_disktype() +{ + int cnt; + + for (cnt = 0; disk_types[cnt].type; cnt++) { + if (!strcasecmp(disk_types[cnt].type, disktype)) { break; } + } + + if (!disk_types[cnt].type) { + fprintf(stderr, "Unknown disktype %s\n", disktype); + exit(1); + } + + return disk_types[cnt].vadp_type; +} + +/* + * Connect using VDDK to a VSPHERE server. + */ +static inline void do_vixdisklib_connect(const char* key, + json_t* connect_params, + bool readonly, + bool need_snapshot_moref) +{ + int succeeded = 0; + VixError err; + const char* snapshot_moref = NULL; + + memset(&cnxParams, 0, sizeof(cnxParams)); + + err = VixDiskLib_InitEx(VIXDISKLIB_VERSION_MAJOR, VIXDISKLIB_VERSION_MINOR, + LogFunction, WarningFunction, PanicFunction, + "/usr/lib/vmware-vix-disklib", NULL); + + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to initialize vixdisklib %s [%lu]\n", error_txt, + err); + VixDiskLib_FreeErrorText(error_txt); + goto bail_out; + } + + /* + * Start extracting the wanted information from the JSON passed in. + */ + if (!local_vmdk) { + json_t* object; + + object = json_object_get(connect_params, CON_PARAMS_VM_MOREF_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CON_PARAMS_VM_MOREF_KEY, key); + goto bail_out; + } + cnxParams.vmxSpec = strdup(json_string_value(object)); + if (!cnxParams.vmxSpec) { + fprintf(stderr, "Failed to allocate memory for holding %s\n", + CON_PARAMS_VM_MOREF_KEY); + goto bail_out; + } + + object = json_object_get(connect_params, CON_PARAMS_HOST_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CON_PARAMS_HOST_KEY, key); + goto bail_out; + } + cnxParams.serverName = strdup(json_string_value(object)); + if (!cnxParams.serverName) { + fprintf(stderr, "Failed to allocate memory for holding %s\n", + CON_PARAMS_HOST_KEY); + goto bail_out; + } + + object = json_object_get(connect_params, CON_PARAMS_THUMBPRINT_KEY); + if (object) { + cnxParams.thumbPrint = strdup(json_string_value(object)); + if (!cnxParams.thumbPrint) { + fprintf(stderr, "Failed to allocate memory for holding %s\n", + CON_PARAMS_USERNAME_KEY); + goto bail_out; + } + } + + object = json_object_get(connect_params, CON_PARAMS_USERNAME_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CON_PARAMS_USERNAME_KEY, key); + goto bail_out; + } + cnxParams.credType = VIXDISKLIB_CRED_UID; + cnxParams.creds.uid.userName = strdup(json_string_value(object)); + if (!cnxParams.creds.uid.userName) { + fprintf(stderr, "Failed to allocate memory for holding %s\n", + CON_PARAMS_USERNAME_KEY); + goto bail_out; + } + + + object = json_object_get(connect_params, CON_PARAMS_PASSWORD_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CON_PARAMS_PASSWORD_KEY, key); + goto bail_out; + } + cnxParams.creds.uid.password = strdup(json_string_value(object)); + if (!cnxParams.creds.uid.password) { + fprintf(stderr, "Failed to allocate memory for holding %s\n", + CON_PARAMS_PASSWORD_KEY); + goto bail_out; + } + cnxParams.port = VSPHERE_DEFAULT_ADMIN_PORT; + + if (need_snapshot_moref) { + object = json_object_get(connect_params, CON_PARAMS_SNAPSHOT_MOREF_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CON_PARAMS_SNAPSHOT_MOREF_KEY, key); + goto bail_out; + } + snapshot_moref = json_string_value(object); + } + + if (!local_vmdk) { + err = VixDiskLib_PrepareForAccess(&cnxParams, BAREOS_VADPDUMPER_IDENTITY); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to Prepare For Access: %s [%lu]\n", error_txt, + err); + VixDiskLib_FreeErrorText(error_txt); + } + } + } + + err = VixDiskLib_ConnectEx(&cnxParams, (readonly) ? TRUE : FALSE, + snapshot_moref, force_transport, &connection); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to connect to %s : %s [%lu]\n", + cnxParams.serverName, error_txt, err); + VixDiskLib_FreeErrorText(error_txt); + goto bail_out; + } + + /* + * Register our exit handler. + */ + atexit(cleanup); + + succeeded = 1; + +bail_out: + if (!succeeded) { exit(1); } +} + +/* + * Open a VMDK using VDDK. + */ +static inline void do_vixdisklib_open(const char* key, + const char* disk_name, + json_t* disk_params, + bool readonly, + bool getdiskinfo, + VixDiskLibHandle* diskHandle) +{ + int succeeded = 0; + VixError err; + const char* disk_path; + uint32_t flags; + + if (!disk_name) { + json_t* object; + + /* + * Start extracting the wanted information from the JSON passed in. + */ + object = json_object_get(disk_params, DISK_PARAMS_DISK_PATH_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + DISK_PARAMS_DISK_PATH_KEY, key); + goto bail_out; + } + + disk_path = json_string_value(object); + } else { + disk_path = vmdk_disk_name; + } + + flags = 0; + if (readonly) { flags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY; } + + err = VixDiskLib_Open(connection, disk_path, flags, diskHandle); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to open %s : %s [%lu]\n", disk_path, error_txt, + err); + VixDiskLib_FreeErrorText(error_txt); + goto bail_out; + } + + if (getdiskinfo) { + /* + * See how big the logical disk is. + */ + err = VixDiskLib_GetInfo(*diskHandle, &info); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to get Logical Disk Info for %s, %s [%lu]\n", + disk_path, error_txt, err); + VixDiskLib_FreeErrorText(error_txt); + goto bail_out; + } + } + + if (verbose) { + fprintf(stderr, "Selected transport method: %s\n", + VixDiskLib_GetTransportMode(*diskHandle)); + } + + succeeded = 1; + +bail_out: + if (!succeeded) { exit(1); } +} + +/* + * Create a VMDK using VDDK. + */ +static inline void do_vixdisklib_create(const char* key, + const char* disk_name, + json_t* disk_params, + uint64_t absolute_disk_length) +{ + int succeeded = 0; + VixError err; + const char* disk_path; + VixDiskLibCreateParams createParams; + + if (!local_vmdk) { + fprintf(stderr, "Cannot create a remote disk via VADP\n"); + goto bail_out; + } + + if (!disk_name) { + json_t* object; + + /* + * Start extracting the wanted information from the JSON passed in. + */ + object = json_object_get(disk_params, DISK_PARAMS_DISK_PATH_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + DISK_PARAMS_DISK_PATH_KEY, key); + goto bail_out; + } + + disk_path = json_string_value(object); + } else { + disk_path = vmdk_disk_name; + } + + createParams.adapterType = VIXDISKLIB_ADAPTER_SCSI_BUSLOGIC; + createParams.capacity = (absolute_disk_length / VIXDISKLIB_SECTOR_SIZE); + if (disktype) { + createParams.diskType = lookup_disktype(); + } else { + createParams.diskType = VIXDISKLIB_DISK_MONOLITHIC_SPARSE; + } + createParams.hwVersion = 7; /* for ESX(i)4 */ + err = VixDiskLib_Create(connection, disk_path, &createParams, NULL, NULL); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to create Logical Disk for %s, %s [%lu]\n", + disk_path, error_txt, err); + VixDiskLib_FreeErrorText(error_txt); + goto bail_out; + } + + succeeded = 1; + +bail_out: + if (!succeeded) { exit(1); } +} + +/* + * Read data from a VMDK using the VDDP functions. + */ +static size_t read_from_vmdk(size_t sector_offset, size_t nbyte, void* buf) +{ + VixError err; + + err = VixDiskLib_Read(read_diskHandle, sector_offset, + nbyte / DEFAULT_SECTOR_SIZE, (uint8*)buf); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "VMDK Read error: %s [%lu]\n", error_txt, err); + return -1; + } + + return nbyte; +} + +/* + * Write data to a VMDK using the VDDP functions. + */ +static size_t write_to_vmdk(size_t sector_offset, size_t nbyte, void* buf) +{ + VixError err; + + err = VixDiskLib_Write(write_diskHandle, sector_offset, + nbyte / DEFAULT_SECTOR_SIZE, (uint8*)buf); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "VMDK Write error: %s [%lu]\n", error_txt, err); + return -1; + } + + return nbyte; +} + +/* + * Read data from a stream using the robust reader function. + */ +static size_t read_from_stream(size_t sector_offset, size_t nbyte, void* buf) +{ + return robust_reader(STDOUT_FILENO, buf, nbyte); +} + +/* + * Write data from a stream using the robust reader function. + */ +static size_t write_to_stream(size_t sector_offset, size_t nbyte, void* buf) +{ + /* + * Should we clone to rawdevice ? + */ + if (raw_disk_fd != -1) { robust_writer(raw_disk_fd, buf, nbyte); } + + /* + * Should we clone to new VMDK file ? + */ + if (write_diskHandle) { + VixError err; + + err = VixDiskLib_Write(write_diskHandle, sector_offset, + nbyte / DEFAULT_SECTOR_SIZE, (uint8*)buf); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "VMDK Write error: %s [%lu]\n", error_txt, err); + } + } + + return robust_writer(STDOUT_FILENO, buf, nbyte); +} + +/* + * Encode the disk info of the disk saved into the backup output stream. + */ +static inline bool save_disk_info(const char* key, + json_t* cbt, + uint64_t* absolute_disk_length) +{ + bool retval = false; + struct runtime_disk_info_encoding rdie; + json_t* object; + + fill_runtime_disk_info_encoding(&rdie); + + object = json_object_get(cbt, CBT_DISK_SIZE); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CBT_DISK_SIZE, key); + goto bail_out; + } + + rdie.absolute_disk_length = json_integer_value(object); + + object = json_object_get(cbt, CBT_START_OFFSET); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CBT_START_OFFSET, key); + goto bail_out; + } + + rdie.absolute_start_offset = json_integer_value(object); + + /* + * Save the absolute offset we should use. + */ + absolute_start_offset = rdie.absolute_start_offset; + + if (robust_writer(STDOUT_FILENO, (char*)&rdie, rdie_size) != rdie_size) { + fprintf(stderr, + "Failed to write runtime_disk_info_encoding structure to output " + "datastream\n"); + goto bail_out; + } + + retval = true; + if (absolute_disk_length) { + *absolute_disk_length = rdie.absolute_disk_length; + } + +bail_out: + return retval; +} + +/* + * Decode the disk info of the disk restored from the backup input stream. + */ +static inline bool process_disk_info(bool validate_only, json_t* value) +{ + struct runtime_disk_info_encoding rdie; + + memset(&rdie, 0, rdie_size); + if (robust_reader(STDIN_FILENO, (char*)&rdie, rdie_size) != rdie_size) { + fprintf(stderr, "Failed to read a valid runtime_disk_info_encoding\n"); + goto bail_out; + } + + if (rdie.start_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_disk_info_encoding] Failed to find valid MAGIC start " + "marker read %u should have been %u\n", + rdie.start_magic, BAREOSMAGIC); + goto bail_out; + } + + if (rdie.end_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_disk_info_encoding] Failed to find valid MAGIC end " + "marker read %u should have been %u\n", + rdie.end_magic, BAREOSMAGIC); + goto bail_out; + } + + if (verbose) { dump_runtime_disk_info_encoding(&rdie); } + + if (create_disk && !validate_only) { + do_vixdisklib_create(DISK_PARAMS_KEY, vmdk_disk_name, value, + rdie.absolute_disk_length); + do_vixdisklib_open(DISK_PARAMS_KEY, vmdk_disk_name, value, false, true, + &write_diskHandle); + + if (!write_diskHandle) { + fprintf( + stderr, + "Cannot process restore data as no VixDiskLib disk handle opened\n"); + goto bail_out; + } + } + + /* + * Validate that things make sense to restore on the opened VMDK. + */ + if (!validate_only && check_size) { + if (!validate_runtime_disk_info_encoding(&rdie)) { + fprintf(stderr, + "[runtime_disk_info_encoding] Invalid disk geometry for " + "restoring to this volume\n"); + goto bail_out; + } + } + + /* + * Save the absolute offset we should use. + */ + absolute_start_offset = rdie.absolute_start_offset; + + return true; + +bail_out: + return false; +} + +/* + * Read a specific meta data key and encode it into the output stream. + */ +static inline bool read_meta_data_key(char* key) +{ + bool retval = false; + VixError err; + size_t requiredLen; + char* buffer = NULL; + struct runtime_meta_data_encoding rmde; + + if (verbose) { fprintf(stderr, "Processing metadata key %s\n", key); } + + err = VixDiskLib_ReadMetadata(read_diskHandle, key, NULL, 0, &requiredLen); + if (err != VIX_OK && err != VIX_E_BUFFER_TOOSMALL) { return false; } + + buffer = (char*)malloc(requiredLen); + if (!buffer) { + fprintf( + stderr, + "Failed to allocate memory for holding metadata keys, exiting ...\n"); + return false; + } + + err = + VixDiskLib_ReadMetadata(read_diskHandle, key, buffer, requiredLen, NULL); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, + "Failed to read metadata for key %s : %s [%lu] exiting ...\n", key, + error_txt, err); + goto bail_out; + } + + /* + * Should we clone metadata to new VMDK file ? + */ + if (write_diskHandle) { + VixError err; + + err = VixDiskLib_WriteMetadata(write_diskHandle, key, buffer); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, + "Failed to write metadata for key %s : %s [%lu] exiting ...\n", + key, error_txt, err); + goto bail_out; + } + } + + rmde.start_magic = BAREOSMAGIC; + rmde.end_magic = BAREOSMAGIC; + rmde.meta_key_length = strlen(key) + 1; + rmde.meta_data_length = requiredLen + 1; + + if (robust_writer(STDOUT_FILENO, &rmde, rmde_size) != rmde_size) { + fprintf(stderr, + "Failed to write runtime_meta_data_encoding structure to output " + "datastream\n"); + goto bail_out; + } + + if (robust_writer(STDOUT_FILENO, key, rmde.meta_key_length) != + rmde.meta_key_length) { + fprintf(stderr, "Failed to write meta data key to output datastream\n"); + goto bail_out; + } + + if (robust_writer(STDOUT_FILENO, buffer, rmde.meta_data_length) != + rmde.meta_data_length) { + fprintf(stderr, "Failed to write meta data to output datastream\n"); + goto bail_out; + } + + retval = true; + +bail_out: + if (buffer) { free(buffer); } + + return retval; +} + +/* + * Read all meta data from a disk and encode it into the output stream. + */ +static inline bool save_meta_data() +{ + bool retval = false; + VixError err; + size_t requiredLen; + char* bp; + char* buffer = NULL; + struct runtime_meta_data_encoding rmde; + + /* + * See if we are actually saving all meta data or should only write the META + * data end marker. + */ + if (save_metadata) { + err = VixDiskLib_GetMetadataKeys(read_diskHandle, NULL, 0, &requiredLen); + if (err != VIX_OK && err != VIX_E_BUFFER_TOOSMALL) { return false; } + + buffer = (char*)malloc(requiredLen); + if (!buffer) { + fprintf( + stderr, + "Failed to allocate memory for holding metadata keys, exiting ...\n"); + return false; + } + + err = + VixDiskLib_GetMetadataKeys(read_diskHandle, buffer, requiredLen, NULL); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, "Failed to read metadata keys : %s [%lu] exiting ...\n", + error_txt, err); + goto bail_out; + } + + bp = buffer; + while (*bp) { + if (!read_meta_data_key(bp)) { goto bail_out; } + + bp += strlen(bp) + 1; + } + } + + /* + * Write an META data end marker. + * e.g. metadata header with key and data length == 0 + */ + rmde.start_magic = BAREOSMAGIC; + rmde.end_magic = BAREOSMAGIC; + rmde.meta_key_length = 0; + rmde.meta_data_length = 0; + + if (robust_writer(STDOUT_FILENO, &rmde, rmde_size) != rmde_size) { + fprintf(stderr, + "Failed to write runtime_meta_data_encoding structure to output " + "datastream\n"); + goto bail_out; + } + + retval = true; + +bail_out: + if (buffer) { free(buffer); } + + return retval; +} + +/* + * Read a backup stream from STDIN and process its metadata. + * Stop processing when we encounter the special end of metadata tag + * which is when the meta_key_length and meta_data_length are zero. + */ +static inline bool process_meta_data(bool validate_only) +{ + struct runtime_meta_data_encoding rmde; + char* key = NULL; + char* buffer = NULL; + + while (1) { + if (robust_reader(STDIN_FILENO, &rmde, rmde_size) != rmde_size) { + fprintf(stderr, + "Failed to read runtime_meta_data_encoding structure from input " + "datastream\n"); + goto bail_out; + } + + if (rmde.start_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_meta_data_encoding] Failed to find valid MAGIC start " + "marker read %u should have been %u\n", + rmde.start_magic, BAREOSMAGIC); + goto bail_out; + } + + if (rmde.end_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_meta_data_encoding] Failed to find valid MAGIC end " + "marker read %u should have been %u\n", + rmde.end_magic, BAREOSMAGIC); + goto bail_out; + } + + /* + * See if we processed the last meta data item. + */ + if (rmde.meta_key_length == 0 && rmde.meta_data_length == 0) { break; } + + key = (char*)malloc(rmde.meta_key_length); + if (!key) { + fprintf(stderr, "Failed to allocate %d bytes for meta data key\n", + rmde.meta_key_length); + goto bail_out; + } + + if (robust_reader(STDIN_FILENO, key, rmde.meta_key_length) != + rmde.meta_key_length) { + fprintf(stderr, "Failed to read meta data key from input datastream\n"); + goto bail_out; + } + + buffer = (char*)malloc(rmde.meta_data_length); + if (!key) { + fprintf(stderr, "Failed to allocate %d bytes for meta data\n", + rmde.meta_data_length); + goto bail_out; + } + + if (robust_reader(STDIN_FILENO, buffer, rmde.meta_data_length) != + rmde.meta_data_length) { + fprintf(stderr, "Failed to read meta data from input datastream\n"); + goto bail_out; + } + + if (verbose) { + fprintf(stderr, "Meta data key %s, value %s\n", key, buffer); + } + + if (!validate_only && restore_meta_data) { + VixError err; + + err = VixDiskLib_WriteMetadata(write_diskHandle, key, buffer); + if (VIX_FAILED(err)) { + char* error_txt; + + error_txt = VixDiskLib_GetErrorText(err, NULL); + fprintf(stderr, + "Failed to write metadata for key %s : %s [%lu] exiting ...\n", + key, error_txt, err); + goto bail_out; + } + } + + free(key); + free(buffer); + } + + return true; + +bail_out: + return false; +} + +/* + * Process the Change Block Tracking information and write the wanted sectors + * to the output stream. We self encode the data using a prefix header that + * describes the data that is encoded after it including a MAGIC key and the + * actual CBT information e.g. start of the read sectors and the number of + * sectors that follow. + */ +static inline bool process_cbt(const char* key, json_t* cbt) +{ + bool retval = false; + size_t index; + json_t *object, *array_element, *start, *length; + uint64_t start_offset, offset_length, current_offset, max_offset; + uint64_t sector_offset, sectors_to_read; + uint8 buf[DEFAULT_SECTOR_SIZE * SECTORS_PER_CALL]; + struct runtime_cbt_encoding rce; + + if (!read_diskHandle) { + fprintf(stderr, + "Cannot process CBT data as no VixDiskLib disk handle opened\n"); + goto bail_out; + } + + object = json_object_get(cbt, CBT_CHANGEDAREA_KEY); + if (!object) { + fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", + CBT_CHANGEDAREA_KEY, key); + goto bail_out; + } + + /* + * Iterate over each element of the JSON array and get the "start" and + * "length" member. + */ + rce.start_magic = BAREOSMAGIC; + rce.end_magic = BAREOSMAGIC; + json_array_foreach(object, index, array_element) + { + /* + * Get the two members we are interested in. + */ + start = json_object_get(array_element, CBT_CHANGEDAREA_START_KEY); + length = json_object_get(array_element, CBT_CHANGEDAREA_LENGTH_KEY); + + if (!start || !length) { continue; } + + start_offset = json_integer_value(start); + offset_length = json_integer_value(length); + + if (verbose) { + fprintf(stderr, "start = %lu\n", start_offset); + fprintf(stderr, "length = %lu\n", offset_length); + fprintf(stderr, "nr length = %lu\n", offset_length / DEFAULT_SECTOR_SIZE); + fflush(stderr); + } + + rce.start_offset = start_offset; + rce.offset_length = offset_length; + + /* + * Write the CBT info into the output stream. + */ + if (robust_writer(STDOUT_FILENO, (char*)&rce, rce_size) != rce_size) { + fprintf(stderr, + "Failed to write runtime_cbt_encoding structure to output " + "datastream\n"); + goto bail_out; + } + + if (raw_disk_fd != -1) { + lseek(raw_disk_fd, start_offset, SEEK_SET); + if (verbose) { + fprintf(stderr, "Log: RAWFILE: Adusting seek position in file\n"); + } + } + + /* + * Calculate the start offset and read as many sectors as defined by the + * length element of the JSON structure. + */ + current_offset = absolute_start_offset + start_offset; + max_offset = current_offset + offset_length; + sector_offset = current_offset / DEFAULT_SECTOR_SIZE; + while (current_offset < max_offset) { + /* + * The number of sectors to read is the minimum of either the total number + * of sectors still available in this CBT range or the upper setting + * specified in the sectors_per_call variable. + */ + sectors_to_read = + MIN(sectors_per_call, (offset_length / DEFAULT_SECTOR_SIZE)); + + if (multi_threaded) { + if (!send_to_copy_thread(sector_offset, + sectors_to_read * DEFAULT_SECTOR_SIZE)) { + goto bail_out; + } + } else { + if (read_from_vmdk(sector_offset, sectors_to_read * DEFAULT_SECTOR_SIZE, + buf) != sectors_to_read * DEFAULT_SECTOR_SIZE) { + fprintf(stderr, "Read error on VMDK\n"); + goto bail_out; + } + + if (write_to_stream(sector_offset, + sectors_to_read * DEFAULT_SECTOR_SIZE, + buf) != sectors_to_read * DEFAULT_SECTOR_SIZE) { + fprintf(stderr, "Failed to write data to output datastream\n"); + goto bail_out; + } + } + + /* + * Calculate the new offsets for a next run. + */ + current_offset += (sectors_to_read * DEFAULT_SECTOR_SIZE); + sector_offset += sectors_to_read; + offset_length -= sectors_to_read * DEFAULT_SECTOR_SIZE; + } + + if (multi_threaded) { flush_copy_thread(); } + + if (verbose) { fflush(stderr); } + } + + if (multi_threaded) { cleanup_copy_thread(); } + + retval = true; + +bail_out: + if (read_diskHandle) { + VixDiskLib_Close(read_diskHandle); + read_diskHandle = NULL; + } + + return retval; +} + +/* + * Read a backup stream from STDIN and process it. When validate_only is set + * to true we only try to process the data but do not actually write it back + * to the VMDK. + */ +static inline bool process_restore_stream(bool validate_only, json_t* value) +{ + bool retval = false; + size_t cnt; + uint64_t current_offset, max_offset; + uint64_t sector_offset, sectors_to_read; + uint8 buf[DEFAULT_SECTOR_SIZE * SECTORS_PER_CALL]; + struct runtime_cbt_encoding rce; + + if (!create_disk && !validate_only) { + do_vixdisklib_open(DISK_PARAMS_KEY, vmdk_disk_name, value, false, true, + &write_diskHandle); + + if (!write_diskHandle) { + fprintf( + stderr, + "Cannot process restore data as no VixDiskLib disk handle opened\n"); + goto bail_out; + } + } + + /* + * Setup multi threading if requested. + */ + if (!validate_only && multi_threaded) { + if (!setup_copy_thread(read_from_stream, write_to_vmdk)) { + fprintf(stderr, "Failed to initialize multithreading\n"); + goto bail_out; + } + } + + /* + * Process the disk info data. + */ + if (!process_disk_info(validate_only, value)) { goto bail_out; } + + /* + * Process the disk meta data, + */ + if (!process_meta_data(validate_only)) { goto bail_out; } + + memset(&rce, 0, rce_size); + while (robust_reader(STDIN_FILENO, (char*)&rce, rce_size) == rce_size) { + if (rce.start_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_cbt_encoding] Failed to find valid MAGIC start marker " + "read %u should have been %u\n", + rce.start_magic, BAREOSMAGIC); + goto bail_out; + } + + if (rce.end_magic != BAREOSMAGIC) { + fprintf(stderr, + "[runtime_cbt_encoding] Failed to find valid MAGIC end marker " + "read %u should have been %u\n", + rce.end_magic, BAREOSMAGIC); + goto bail_out; + } + + if (verbose) { + fprintf(stderr, "start = %lu\n", rce.start_offset); + fprintf(stderr, "length = %lu\n", rce.offset_length); + fprintf(stderr, "nr length = %lu\n", + rce.offset_length / DEFAULT_SECTOR_SIZE); + fflush(stderr); + } + + current_offset = absolute_start_offset + rce.start_offset; + max_offset = current_offset + rce.offset_length; + sector_offset = current_offset / DEFAULT_SECTOR_SIZE; + while (current_offset < max_offset) { + /* + * The number of sectors to read is the minimum of either the total number + * of sectors still available in this CBT range or the upper setting + * specified in the sectors_per_call variable. + */ + sectors_to_read = + MIN(sectors_per_call, (rce.offset_length / DEFAULT_SECTOR_SIZE)); + + if (!validate_only && multi_threaded) { + if (!send_to_copy_thread(sector_offset, + sectors_to_read * DEFAULT_SECTOR_SIZE)) { + goto bail_out; + } + } else { + cnt = robust_reader(STDIN_FILENO, (char*)buf, + sectors_to_read * DEFAULT_SECTOR_SIZE); + if (cnt != sectors_to_read * DEFAULT_SECTOR_SIZE) { goto bail_out; } + + if (!validate_only) { + if (write_to_vmdk(sector_offset, cnt, buf) != cnt) { goto bail_out; } + } + } + + /* + * Calculate the new offsets for a next run. + */ + current_offset += (sectors_to_read * DEFAULT_SECTOR_SIZE); + sector_offset += sectors_to_read; + rce.offset_length -= sectors_to_read * DEFAULT_SECTOR_SIZE; + } + + if (multi_threaded) { flush_copy_thread(); } + + memset(&rce, 0, rce_size); + } + + if (multi_threaded) { cleanup_copy_thread(); } + + retval = true; + +bail_out: + if (write_diskHandle) { + VixDiskLib_Close(write_diskHandle); + write_diskHandle = NULL; + } + + return retval; +} + +/* + * All work to this program is passed in using a JSON work file which has the + * needed information to perform the wanted operation. This function loads the + * JSON data into memory using the Jansson JSON library. + */ +static inline void process_json_work_file(const char* json_work_file) +{ + json_error_t error; + + /* + * Open the JSON work file. + */ + json_config = json_load_file(json_work_file, JSON_DECODE_ANY, &error); + if (!json_config) { + fprintf(stderr, + "Failed to parse JSON config %s [%s at line %d column %d]\n", + json_work_file, error.text, error.line, error.column); + exit(1); + } + + if (verbose) { + /* + * Dump the internal parsed data in pretty print format. + */ + json_dumpf(json_config, stderr, + JSON_PRESERVE_ORDER | JSON_COMPACT | JSON_INDENT(3)); + fprintf(stderr, "\n"); + fflush(stderr); + } +} + +/* + * Worker function that performs the dump operation of the program. + */ +static inline bool dump_vmdk_stream(const char* json_work_file) +{ + json_t* value; + uint64_t absolute_disk_length = 0; + + process_json_work_file(json_work_file); + + value = json_object_get(json_config, CON_PARAMS_KEY); + if (!value) { + fprintf(stderr, "Failed to find %s in JSON definition\n", CON_PARAMS_KEY); + exit(1); + } + + do_vixdisklib_connect(CON_PARAMS_KEY, value, true, true); + + if (cleanup_on_start) { cleanup_vixdisklib(); } + + value = json_object_get(json_config, DISK_PARAMS_KEY); + if (!value) { + fprintf(stderr, "Failed to find %s in JSON definition\n", DISK_PARAMS_KEY); + exit(1); + } + + do_vixdisklib_open(DISK_PARAMS_KEY, NULL, value, true, true, + &read_diskHandle); + + value = json_object_get(json_config, CBT_DISKCHANGEINFO_KEY); + if (!value) { + fprintf(stderr, "Failed to find %s in JSON definition\n", + CBT_DISKCHANGEINFO_KEY); + exit(1); + } + + /* + * Setup multi threading if requested. + */ + if (multi_threaded) { + if (!setup_copy_thread(read_from_vmdk, write_to_stream)) { + fprintf(stderr, "Failed to initialize multithreading\n"); + exit(1); + } + } + + if (!save_disk_info(CBT_DISKCHANGEINFO_KEY, value, &absolute_disk_length)) { + exit(1); + } + + /* + * See if we are requested to clone the content to a new VMDK. + * save_disk_info() initializes absolute_disk_length. + */ + if (vmdk_disk_name) { + if (create_disk) { + do_vixdisklib_create(NULL, vmdk_disk_name, value, absolute_disk_length); + } + + do_vixdisklib_open(NULL, vmdk_disk_name, value, false, false, + &write_diskHandle); + } + + if (!save_meta_data()) { exit(1); } + + /* + * See if we are requested to clone the content to a rawdevice. + */ + if (raw_disk_name) { + if (verbose) { fprintf(stderr, "Log: RAWFILE: Trying to open RAW file\n"); } + + if ((raw_disk_fd = open(raw_disk_name, O_WRONLY | O_TRUNC)) == -1) { + fprintf(stderr, "Error: Failed to open the RAW DISK FILE\n"); + exit(1); + } + } + + return process_cbt(CBT_DISKCHANGEINFO_KEY, value); +} + +/* + * Worker function that performs the restore operation of the program. + */ +static inline bool restore_vmdk_stream(const char* json_work_file) +{ + json_t* value; + + process_json_work_file(json_work_file); + + value = json_object_get(json_config, CON_PARAMS_KEY); + if (!value) { + fprintf(stderr, "Failed to find %s in JSON definition\n", CON_PARAMS_KEY); + exit(1); + } + + if (cleanup_on_start) { cleanup_vixdisklib(); } + + do_vixdisklib_connect(CON_PARAMS_KEY, value, false, false); + + value = json_object_get(json_config, DISK_PARAMS_KEY); + if (!value) { + fprintf(stderr, "Failed to find %s in JSON definition\n", DISK_PARAMS_KEY); + exit(1); + } + + return process_restore_stream(false, value); +} + +/* + * Worker function that performs the show operation of the program. + */ +static inline int show_backup_stream() +{ + return process_restore_stream(true, NULL); +} + +static void signal_handler(int sig) +{ + exit_code = sig; + exit(sig); +} + +void usage(const char* program_name) +{ + fprintf(stderr, + "Usage: %s [-d ] [-f force_transport] [-s " + "sectors_per_call] [-t disktype] [-CcDlMmRSv] dump | " + "restore | show\n", + program_name); + fprintf(stderr, "Where:\n"); + fprintf(stderr, " -C - Create local VMDK\n"); + fprintf(stderr, " -c - Don't check size of VMDK\n"); + fprintf(stderr, " -D - Cleanup on Disconnect\n"); + fprintf(stderr, " -d - Specify local VMDK name\n"); + fprintf(stderr, " -f - Specify forced transport method\n"); + fprintf(stderr, " -h - This help text\n"); + fprintf(stderr, " -l - Write to a local VMDK\n"); + fprintf(stderr, " -M - Save metadata of VMDK on dump action\n"); + fprintf(stderr, " -m - Use multithreading\n"); + fprintf(stderr, " -r - RAW Image disk name\n"); + fprintf(stderr, " -R - Restore metadata of VMDK on restore action\n"); + fprintf(stderr, " -S - Cleanup on Start\n"); + fprintf(stderr, " -s - Sectors to read per call to VDDK\n"); + fprintf(stderr, " -t - Disktype to create for local VMDK\n"); + fprintf(stderr, " -v - Verbose output\n"); + fprintf(stderr, " -? - This help text\n"); + exit(1); +} + +int main(int argc, char** argv) +{ + bool retval = false; + const char* program_name; + int ch; + + program_name = argv[0]; + while ((ch = getopt(argc, argv, "CcDd:r:f:hlMmRSs:t:v?")) != -1) { + switch (ch) { + case 'C': + create_disk = true; + /* + * If we create the disk we should not check for the size as that won't + * match. + */ + check_size = false; + break; + case 'c': + check_size = false; + break; + case 'D': + cleanup_on_disconnect = true; + break; + case 'd': + vmdk_disk_name = strdup(optarg); + if (!vmdk_disk_name) { + fprintf(stderr, + "Failed to allocate memory to hold diskname, exiting ...\n"); + exit(1); + } + break; + case 'r': + raw_disk_name = strdup(optarg); + if (!raw_disk_name) { + fprintf( + stderr, + "Failed to allocate memory to hold rawdiskname, exiting ...\n"); + exit(1); + } + break; + case 'f': + force_transport = strdup(optarg); + if (!force_transport) { + fprintf(stderr, + "Failed to allocate memory to hold forced transport, exiting " + "...\n"); + exit(1); + } + break; + case 'l': + local_vmdk = true; + break; + case 'M': + save_metadata = true; + break; + case 'm': + multi_threaded = true; + break; + case 'R': + restore_meta_data = true; + break; + case 'S': + cleanup_on_start = true; + break; + case 's': + sectors_per_call = atoi(optarg); + break; + case 't': + disktype = strdup(optarg); + if (!disktype) { + fprintf(stderr, + "Failed to allocate memory to hold disktype, exiting ...\n"); + exit(1); + } + break; + case 'v': + verbose = true; + break; + case 'h': + case '?': + default: + usage(program_name); + break; + } + } + + /* + * Install signal handlers for the most important signals. + */ + signal(SIGHUP, signal_handler); + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + argc -= optind; + argv += optind; + + if (argc <= 0) { usage(program_name); } + + if (strcasecmp(argv[0], "dump") == 0) { + if (argc <= 1) { usage(program_name); } + + retval = dump_vmdk_stream(argv[1]); + } else if (strcasecmp(argv[0], "restore") == 0) { + if (argc <= 1) { usage(program_name); } + + retval = restore_vmdk_stream(argv[1]); + } else if (strcasecmp(argv[0], "show") == 0) { + retval = show_backup_stream(); + } else { + fprintf(stderr, "Unknown action %s\n", argv[1]); + } + + if (retval) { exit_code = 0; } + + exit(exit_code); +} diff --git a/vmware/vadp_dumper/bareos_vadp_dumper.cpp b/vmware/vadp_dumper/bareos_vadp_dumper.cpp deleted file mode 100644 index 7ee1ba3e952..00000000000 --- a/vmware/vadp_dumper/bareos_vadp_dumper.cpp +++ /dev/null @@ -1,1712 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2014-2015 Bareos GmbH & Co. KG - Copyright (C) 2015-2015 Planets Communications B.V. - - 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, which is - listed in the file LICENSE.vadp. - - 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. -*/ -/* - * VADP Dumper - vStorage APIs for Data Protection Dumper program. - * - * Marco van Wieringen, July 2014 - * Renout Gerrits, Oct 2017, added thumbprint - */ -#include -#include -#include -#include -#include -#include -#include - -#include "copy_thread.hpp" - -#include - -/* - * json_array_foreach macro was added in jansson version 2.5 - * we can compile also against an lower version if we define it ourselves. - */ -#if JANSSON_VERSION_HEX < 0x020500 -#define json_array_foreach(array, index, value) \ - for(index = 0; \ - index < json_array_size(array) && (value = json_array_get(array, index)); \ - index++) -#endif - -#include - -#define VIXDISKLIB_VERSION_MAJOR 6 -#define VIXDISKLIB_VERSION_MINOR 5 - -#define VSPHERE_DEFAULT_ADMIN_PORT 0 - -/* - * VixDiskLib does all processing in sectors of 512 bytes. - */ -#define DEFAULT_SECTOR_SIZE VIXDISKLIB_SECTOR_SIZE - -/* - * In each call to the VixDiskLib read/write this number of sectors. - * e.g. 512 means 256 Kb per call (e.g. 512 x 512 bytes) - */ -#define SECTORS_PER_CALL 1024 - -#define MIN(a, b) ((a) < b) ? (a) : (b) -#define MAX(a, b) ((a) > b) ? (a) : (b) - -#define CON_PARAMS_KEY "ConnParams" -#define CON_PARAMS_VM_MOREF_KEY "VmMoRef" -#define CON_PARAMS_HOST_KEY "VsphereHostName" -#define CON_PARAMS_THUMBPRINT_KEY "VsphereThumbPrint" -#define CON_PARAMS_USERNAME_KEY "VsphereUsername" -#define CON_PARAMS_PASSWORD_KEY "VspherePassword" -#define CON_PARAMS_SNAPSHOT_MOREF_KEY "VsphereSnapshotMoRef" - -#define DISK_PARAMS_KEY "DiskParams" -#define DISK_PARAMS_DISK_PATH_KEY "diskPath" - -#define CBT_DISKCHANGEINFO_KEY "DiskChangeInfo" -#define CBT_DISK_SIZE "length" -#define CBT_CHANGEDAREA_KEY "changedArea" -#define CBT_CHANGEDAREA_START_KEY "start" -#define CBT_CHANGEDAREA_LENGTH_KEY "length" -#define CBT_START_OFFSET "startOffset" - -#define BAREOSMAGIC 0x12122012 -#define PROTOCOL_VERSION 1 - -#define BAREOS_VADPDUMPER_IDENTITY "BareosVADPDumper" - -struct disk_type { - const char *type; - VixDiskLibDiskType vadp_type; -}; - -static struct disk_type disk_types[] = { - { "monolithic_sparse", VIXDISKLIB_DISK_MONOLITHIC_SPARSE }, - { "monolithic_flat", VIXDISKLIB_DISK_MONOLITHIC_FLAT }, - { "split_sparse", VIXDISKLIB_DISK_SPLIT_SPARSE }, - { "split_flat", VIXDISKLIB_DISK_SPLIT_FLAT }, - { "vmfs_flat", VIXDISKLIB_DISK_VMFS_FLAT }, - { "optimized", VIXDISKLIB_DISK_STREAM_OPTIMIZED }, - { "vmfs_thin", VIXDISKLIB_DISK_VMFS_THIN }, - { "vmfs_sparse", VIXDISKLIB_DISK_VMFS_SPARSE }, - { NULL, VIXDISKLIB_DISK_UNKNOWN } -}; - -/* - * Generic identification structure, 128 bytes with padding. - * This includes a protocol version. - */ -struct runtime_disk_info_encoding { - uint32_t start_magic; - uint32_t protocol_version; - uint64_t absolute_disk_length; - uint64_t absolute_start_offset; - uint32_t bios_cylinders; - uint32_t bios_heads; - uint32_t bios_sectors; - uint32_t phys_cylinders; - uint32_t phys_heads; - uint32_t phys_sectors; - uint64_t phys_capacity; - uint32_t adapter_type; - uint32_t padding[16]; - uint32_t end_magic; -}; -const int rdie_size = sizeof(struct runtime_disk_info_encoding); - -/* - * Disk Meta data structure, - * Encodes what follows e.g. meta_key and meta_data. - * e.g. [META_META_DATA] [META_DATA_KEY] [META_DATA] ... - */ -struct runtime_meta_data_encoding { - uint32_t start_magic; - uint32_t meta_key_length; - uint32_t meta_data_length; - uint32_t end_magic; -}; -const int rmde_size = sizeof(struct runtime_meta_data_encoding); - -/* - * Changed Block Tracking structure. - * Encodes the CBT data followed by the actual data. - * e.g. [CBT] [DATA] ... - */ -struct runtime_cbt_encoding { - uint32_t start_magic; - uint64_t start_offset; - uint64_t offset_length; - uint32_t end_magic; -}; -const int rce_size = sizeof(struct runtime_cbt_encoding); - -static bool cleanup_on_start = false; -static bool cleanup_on_disconnect = false; -static bool save_metadata = false; -static bool verbose = false; -static bool check_size = true; -static bool create_disk = false; -static bool local_vmdk = false; -static bool multi_threaded = false; -static bool restore_meta_data = false; -static int sectors_per_call = SECTORS_PER_CALL; -static uint64_t absolute_start_offset = 0; -static char *vmdk_disk_name = NULL; -static char *raw_disk_name = NULL; -static int raw_disk_fd = -1; -static char *force_transport = NULL; -static char *disktype = NULL; -static VixDiskLibConnectParams cnxParams; -static VixDiskLibConnection connection = NULL; -static VixDiskLibHandle read_diskHandle = NULL; -static VixDiskLibHandle write_diskHandle = NULL; -static VixDiskLibInfo *info = NULL; -static json_t *json_config = NULL; -static int exit_code = 1; - -/* - * Encode the VDDK VixDiskLibInfo into an internal representation. - */ -static inline void fill_runtime_disk_info_encoding(struct runtime_disk_info_encoding *rdie) -{ - memset(rdie, 0, rdie_size); - - rdie->protocol_version = PROTOCOL_VERSION; - rdie->start_magic = BAREOSMAGIC; - rdie->end_magic = BAREOSMAGIC; - - if (info->biosGeo.cylinders > 0) { - rdie->bios_cylinders = info->biosGeo.cylinders; - } else { - rdie->bios_cylinders = info->physGeo.cylinders; - } - if (info->biosGeo.heads > 0) { - rdie->bios_heads = info->biosGeo.heads; - } else { - rdie->bios_heads = info->physGeo.heads; - } - if (info->biosGeo.sectors > 0) { - rdie->bios_sectors = info->biosGeo.sectors; - } else { - rdie->bios_sectors = info->physGeo.sectors; - } - - rdie->phys_cylinders = info->physGeo.cylinders; - rdie->phys_heads = info->physGeo.heads; - rdie->phys_sectors = info->physGeo.sectors; - - rdie->phys_capacity = info->capacity; - rdie->adapter_type = info->adapterType; -} - -/* - * Dump the important content of the internal disk representation for verbose mode. - */ -static inline void dump_runtime_disk_info_encoding(struct runtime_disk_info_encoding *rdie) -{ - fprintf(stderr, "Protocol version = %lld\n", rdie->protocol_version); - fprintf(stderr, "Absolute disk length = %lld\n", rdie->absolute_disk_length); - fprintf(stderr, "Absolute start offset = %lld\n", rdie->absolute_start_offset); - fprintf(stderr, "BIOS geometry (%ld cyl, %ld heads, %ld sectors)\n", - rdie->bios_cylinders, rdie->bios_heads, rdie->bios_sectors); - fprintf(stderr, "PHYS geometry (%ld cyl, %ld heads, %ld sectors)\n", - rdie->phys_cylinders, rdie->phys_heads, rdie->phys_sectors); - fprintf(stderr, "Physical capacity %lld\n", rdie->phys_capacity); - fprintf(stderr, "Adapter Type %ld\n", rdie->adapter_type); -} - -/* - * Validate the disk sizes from the internal disk representation to the current VDMK settings. - */ -static inline char validate_runtime_disk_info_encoding(struct runtime_disk_info_encoding *rdie) -{ - if (info->biosGeo.cylinders > 0 && info->biosGeo.cylinders < rdie->bios_cylinders) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld BIOS cylinders original had %ld\n", - info->biosGeo.cylinders, rdie->bios_cylinders); - goto bail_out; - } - - if (info->biosGeo.heads > 0 && info->biosGeo.heads < rdie->bios_heads) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld BIOS heads original had %ld\n", - info->biosGeo.heads, rdie->bios_heads); - goto bail_out; - } - - if (info->biosGeo.cylinders > 0 && info->biosGeo.cylinders < rdie->bios_cylinders) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld BIOS sectors original had %ld\n", - info->biosGeo.sectors, rdie->bios_sectors); - goto bail_out; - } - - if (info->physGeo.cylinders < rdie->phys_cylinders) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld PHYS cylinders original had %ld\n", - info->physGeo.cylinders, rdie->phys_cylinders); - goto bail_out; - } - - if (info->physGeo.heads < rdie->phys_heads) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld PHYS heads original had %ld\n", - info->biosGeo.heads, rdie->phys_heads); - goto bail_out; - } - - if (info->physGeo.cylinders < rdie->phys_cylinders) { - fprintf(stderr, "[validate_runtime_disk_info_encoding] New disk has %ld PHYS sectors original had %ld\n", - info->biosGeo.sectors, rdie->phys_sectors); - goto bail_out; - } - - return 1; - -bail_out: - return 0; -} - -/* - * Writer function that handles partial writes. - */ -static inline size_t robust_writer(int fd, void *buffer, int size) -{ - size_t total_bytes = 0; - size_t cnt = 0; - - do { - cnt = write(fd, (char *)buffer + total_bytes, size); - if (cnt > 0) { - size -= cnt; - total_bytes += cnt; - } else if (cnt < 0) { - total_bytes = -1; - goto bail_out; - } - } while (size > 0 && cnt > 0); - -bail_out: - return total_bytes; -} - -/* - * Reader function that handles partial reads. - */ -static inline size_t robust_reader(int fd, void *buffer, int size) -{ - size_t total_bytes = 0; - size_t cnt = 0; - - do { - cnt = read(fd, (char *)buffer + total_bytes, size); - if (cnt > 0) { - size -= cnt; - total_bytes += cnt; - } else if (cnt < 0) { - total_bytes = -1; - goto bail_out; - } - } while (size > 0 && cnt > 0); - -bail_out: - return total_bytes; -} - -/* - * VDDK helper functions. - */ -static void LogFunction(const char *fmt, va_list args) -{ - if (verbose) { - fprintf(stderr, "Log: "); - vfprintf(stderr, fmt, args); - } -} - -static void WarningFunction(const char *fmt, va_list args) -{ - fprintf(stderr, "Warning: "); - vfprintf(stderr, fmt, args); -} - -static void PanicFunction(const char *fmt, va_list args) -{ - fprintf(stderr, "Log: "); - vfprintf(stderr, fmt, args); - exit_code = 10; - exit(exit_code); -} - -static inline void cleanup_cnxParams() -{ - if (cnxParams.vmxSpec) { - free(cnxParams.vmxSpec); - cnxParams.vmxSpec = NULL; - } - - if (cnxParams.serverName) { - free(cnxParams.serverName); - cnxParams.serverName = NULL; - } - - if (cnxParams.creds.uid.userName) { - free(cnxParams.creds.uid.userName); - cnxParams.creds.uid.userName = NULL; - } - - if (cnxParams.creds.uid.password) { - free(cnxParams.creds.uid.password); - cnxParams.creds.uid.password = NULL; - } - - if (cnxParams.thumbPrint) { - free(cnxParams.thumbPrint); - cnxParams.thumbPrint = NULL; - } -} - -static inline void cleanup_vixdisklib() -{ - uint32_t numCleanedUp, numRemaining; - - VixDiskLib_Cleanup(&cnxParams, &numCleanedUp, &numRemaining); -} - -/* - * Generic cleanup function. - */ -static void cleanup(void) -{ - VixError err; - - if (info) { - VixDiskLib_FreeInfo(info); - } - - if (read_diskHandle) { - VixDiskLib_Close(read_diskHandle); - read_diskHandle = NULL; - } - - if (write_diskHandle) { - VixDiskLib_Close(write_diskHandle); - write_diskHandle = NULL; - } - - if (connection) { - VixDiskLib_Disconnect(connection); - if (cleanup_on_disconnect) { - cleanup_vixdisklib(); - } - } - - if (!local_vmdk) { - err = VixDiskLib_EndAccess(&cnxParams, BAREOS_VADPDUMPER_IDENTITY); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to End Access: %s [%d]\n", error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - } - } - - if (raw_disk_fd != -1) { - if (verbose) { - fprintf(stderr, "Log: RAWFILE: Closing RAW file\n"); - } - close(raw_disk_fd); - } - - cleanup_cnxParams(); - - VixDiskLib_Exit(); - - if (json_config) { - json_decref(json_config); - } - - if (vmdk_disk_name) { - free(vmdk_disk_name); - } - - if (force_transport) { - free(force_transport); - } - - if (disktype) { - free(disktype); - } - - _exit(exit_code); -} - -/* - * Convert the configured disktype to the right VADP type. - */ -static inline VixDiskLibDiskType lookup_disktype() -{ - int cnt; - - for (cnt = 0; disk_types[cnt].type; cnt++) { - if (!strcasecmp(disk_types[cnt].type, disktype)) { - break; - } - } - - if (!disk_types[cnt].type) { - fprintf(stderr, "Unknown disktype %s\n", disktype); - exit(1); - } - - return disk_types[cnt].vadp_type; -} - -/* - * Connect using VDDK to a VSPHERE server. - */ -static inline void do_vixdisklib_connect(const char *key, json_t *connect_params, - bool readonly, bool need_snapshot_moref) -{ - int succeeded = 0; - VixError err; - const char *snapshot_moref = NULL; - - memset(&cnxParams, 0, sizeof(cnxParams)); - - err = VixDiskLib_InitEx(VIXDISKLIB_VERSION_MAJOR, - VIXDISKLIB_VERSION_MINOR, - LogFunction, - WarningFunction, - PanicFunction, - "/usr/lib/vmware-vix-disklib", - NULL); - - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to initialize vixdisklib %s [%d]\n", error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - goto bail_out; - } - - /* - * Start extracting the wanted information from the JSON passed in. - */ - if (!local_vmdk) { - json_t *object; - - object = json_object_get(connect_params, CON_PARAMS_VM_MOREF_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CON_PARAMS_VM_MOREF_KEY, key); - goto bail_out; - } - cnxParams.vmxSpec = strdup(json_string_value(object)); - if (!cnxParams.vmxSpec) { - fprintf(stderr, "Failed to allocate memory for holding %s\n", CON_PARAMS_VM_MOREF_KEY); - goto bail_out; - } - - object = json_object_get(connect_params, CON_PARAMS_HOST_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CON_PARAMS_HOST_KEY, key); - goto bail_out; - } - cnxParams.serverName = strdup(json_string_value(object)); - if (!cnxParams.serverName) { - fprintf(stderr, "Failed to allocate memory for holding %s\n", CON_PARAMS_HOST_KEY); - goto bail_out; - } - - object = json_object_get(connect_params, CON_PARAMS_THUMBPRINT_KEY); - if (object) { - - cnxParams.thumbPrint = strdup(json_string_value(object)); - if (!cnxParams.thumbPrint) { - fprintf(stderr, "Failed to allocate memory for holding %s\n", CON_PARAMS_USERNAME_KEY); - goto bail_out; - } - } - - object = json_object_get(connect_params, CON_PARAMS_USERNAME_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CON_PARAMS_USERNAME_KEY, key); - goto bail_out; - } - cnxParams.credType = VIXDISKLIB_CRED_UID; - cnxParams.creds.uid.userName = strdup(json_string_value(object)); - if (!cnxParams.creds.uid.userName) { - fprintf(stderr, "Failed to allocate memory for holding %s\n", CON_PARAMS_USERNAME_KEY); - goto bail_out; - } - - - object = json_object_get(connect_params, CON_PARAMS_PASSWORD_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CON_PARAMS_PASSWORD_KEY, key); - goto bail_out; - } - cnxParams.creds.uid.password = strdup(json_string_value(object)); - if (!cnxParams.creds.uid.password) { - fprintf(stderr, "Failed to allocate memory for holding %s\n", CON_PARAMS_PASSWORD_KEY); - goto bail_out; - } - cnxParams.port = VSPHERE_DEFAULT_ADMIN_PORT; - - if (need_snapshot_moref) { - object = json_object_get(connect_params, CON_PARAMS_SNAPSHOT_MOREF_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CON_PARAMS_SNAPSHOT_MOREF_KEY, key); - goto bail_out; - } - snapshot_moref = json_string_value(object); - } - - if (!local_vmdk) { - err = VixDiskLib_PrepareForAccess(&cnxParams, BAREOS_VADPDUMPER_IDENTITY); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to Prepare For Access: %s [%d]\n", error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - } - } - } - - err = VixDiskLib_ConnectEx(&cnxParams, (readonly) ? TRUE : FALSE, snapshot_moref, force_transport, &connection); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to connect to %s : %s [%d]\n", cnxParams.serverName, error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - goto bail_out; - } - - /* - * Register our exit handler. - */ - atexit(cleanup); - - succeeded = 1; - -bail_out: - if (!succeeded) { - exit(1); - } -} - -/* - * Open a VMDK using VDDK. - */ -static inline void do_vixdisklib_open(const char *key, const char *disk_name, json_t *disk_params, - bool readonly, bool getdiskinfo, VixDiskLibHandle *diskHandle) -{ - int succeeded = 0; - VixError err; - const char *disk_path; - uint32_t flags; - - if (!disk_name) { - json_t *object; - - /* - * Start extracting the wanted information from the JSON passed in. - */ - object = json_object_get(disk_params, DISK_PARAMS_DISK_PATH_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", DISK_PARAMS_DISK_PATH_KEY, key); - goto bail_out; - } - - disk_path = json_string_value(object); - } else { - disk_path = vmdk_disk_name; - } - - flags = 0; - if (readonly) { - flags |= VIXDISKLIB_FLAG_OPEN_READ_ONLY; - } - - err = VixDiskLib_Open(connection, disk_path, flags, diskHandle); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to open %s : %s [%d]\n", disk_path, error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - goto bail_out; - } - - if (getdiskinfo) { - /* - * See how big the logical disk is. - */ - err = VixDiskLib_GetInfo(*diskHandle, &info); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to get Logical Disk Info for %s, %s [%d]\n", disk_path, error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - goto bail_out; - } - } - - if (verbose) { - fprintf(stderr, "Selected transport method: %s\n", VixDiskLib_GetTransportMode(*diskHandle)); - } - - succeeded = 1; - -bail_out: - if (!succeeded) { - exit(1); - } -} - -/* - * Create a VMDK using VDDK. - */ -static inline void do_vixdisklib_create(const char *key, const char *disk_name, - json_t *disk_params, uint64_t absolute_disk_length) -{ - int succeeded = 0; - VixError err; - const char *disk_path; - VixDiskLibCreateParams createParams; - - if (!local_vmdk) { - fprintf(stderr, "Cannot create a remote disk via VADP\n"); - goto bail_out; - } - - if (!disk_name) { - json_t *object; - - /* - * Start extracting the wanted information from the JSON passed in. - */ - object = json_object_get(disk_params, DISK_PARAMS_DISK_PATH_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", DISK_PARAMS_DISK_PATH_KEY, key); - goto bail_out; - } - - disk_path = json_string_value(object); - } else { - disk_path = vmdk_disk_name; - } - - createParams.adapterType = VIXDISKLIB_ADAPTER_SCSI_BUSLOGIC; - createParams.capacity = (absolute_disk_length / VIXDISKLIB_SECTOR_SIZE); - if (disktype) { - createParams.diskType = lookup_disktype(); - } else { - createParams.diskType = VIXDISKLIB_DISK_MONOLITHIC_SPARSE; - } - createParams.hwVersion = 7; /* for ESX(i)4 */ - err = VixDiskLib_Create(connection, disk_path, &createParams, NULL, NULL); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to create Logical Disk for %s, %s [%d]\n", disk_path, error_txt, err); - VixDiskLib_FreeErrorText(error_txt); - goto bail_out; - } - - succeeded = 1; - -bail_out: - if (!succeeded) { - exit(1); - } -} - -/* - * Read data from a VMDK using the VDDP functions. - */ -static size_t read_from_vmdk(size_t sector_offset, size_t nbyte, void *buf) -{ - VixError err; - - err = VixDiskLib_Read(read_diskHandle, sector_offset, nbyte / DEFAULT_SECTOR_SIZE, (uint8 *)buf); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "VMDK Read error: %s [%d]\n", error_txt, err); - return -1; - } - - return nbyte; -} - -/* - * Write data to a VMDK using the VDDP functions. - */ -static size_t write_to_vmdk(size_t sector_offset, size_t nbyte, void *buf) -{ - VixError err; - - err = VixDiskLib_Write(write_diskHandle, sector_offset, nbyte / DEFAULT_SECTOR_SIZE, (uint8 *)buf); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "VMDK Write error: %s [%d]\n", error_txt, err); - return -1; - } - - return nbyte; -} - -/* - * Read data from a stream using the robust reader function. - */ -static size_t read_from_stream(size_t sector_offset, size_t nbyte, void *buf) -{ - return robust_reader(STDOUT_FILENO, buf, nbyte); -} - -/* - * Write data from a stream using the robust reader function. - */ -static size_t write_to_stream(size_t sector_offset, size_t nbyte, void *buf) -{ - /* - * Should we clone to rawdevice ? - */ - if (raw_disk_fd != -1) { - robust_writer(raw_disk_fd, buf, nbyte); - } - - /* - * Should we clone to new VMDK file ? - */ - if (write_diskHandle) { - VixError err; - - err = VixDiskLib_Write(write_diskHandle, sector_offset, nbyte / DEFAULT_SECTOR_SIZE, (uint8 *)buf); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "VMDK Write error: %s [%d]\n", error_txt, err); - } - } - - return robust_writer(STDOUT_FILENO, buf, nbyte); -} - -/* - * Encode the disk info of the disk saved into the backup output stream. - */ -static inline bool save_disk_info(const char *key, json_t *cbt, uint64_t *absolute_disk_length) -{ - bool retval = false; - struct runtime_disk_info_encoding rdie; - json_t *object; - - fill_runtime_disk_info_encoding(&rdie); - - object = json_object_get(cbt, CBT_DISK_SIZE); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CBT_DISK_SIZE, key); - goto bail_out; - } - - rdie.absolute_disk_length = json_integer_value(object); - - object = json_object_get(cbt, CBT_START_OFFSET); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CBT_START_OFFSET, key); - goto bail_out; - } - - rdie.absolute_start_offset = json_integer_value(object); - - /* - * Save the absolute offset we should use. - */ - absolute_start_offset = rdie.absolute_start_offset; - - if (robust_writer(STDOUT_FILENO, (char *)&rdie, rdie_size) != rdie_size) { - fprintf(stderr, "Failed to write runtime_disk_info_encoding structure to output datastream\n"); - goto bail_out; - } - - retval = true; - if (absolute_disk_length) { - *absolute_disk_length = rdie.absolute_disk_length; - } - -bail_out: - return retval; -} - -/* - * Decode the disk info of the disk restored from the backup input stream. - */ -static inline bool process_disk_info(bool validate_only, json_t *value) -{ - struct runtime_disk_info_encoding rdie; - - memset(&rdie, 0, rdie_size); - if (robust_reader(STDIN_FILENO, (char *)&rdie, rdie_size) != rdie_size) { - fprintf(stderr, "Failed to read a valid runtime_disk_info_encoding\n"); - goto bail_out; - } - - if (rdie.start_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_disk_info_encoding] Failed to find valid MAGIC start marker read %lld should have been %lld\n", rdie.start_magic, BAREOSMAGIC); - goto bail_out; - } - - if (rdie.end_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_disk_info_encoding] Failed to find valid MAGIC end marker read %lld should have been %lld\n", rdie.end_magic, BAREOSMAGIC); - goto bail_out; - } - - if (verbose) { - dump_runtime_disk_info_encoding(&rdie); - } - - if (create_disk && !validate_only) { - do_vixdisklib_create(DISK_PARAMS_KEY, vmdk_disk_name, value, rdie.absolute_disk_length); - do_vixdisklib_open(DISK_PARAMS_KEY, vmdk_disk_name, value, false, true, &write_diskHandle); - - if (!write_diskHandle) { - fprintf(stderr, "Cannot process restore data as no VixDiskLib disk handle opened\n"); - goto bail_out; - } - } - - /* - * Validate that things make sense to restore on the opened VMDK. - */ - if (!validate_only && check_size) { - if (!validate_runtime_disk_info_encoding(&rdie)) { - fprintf(stderr, "[runtime_disk_info_encoding] Invalid disk geometry for restoring to this volume\n"); - goto bail_out; - } - } - - /* - * Save the absolute offset we should use. - */ - absolute_start_offset = rdie.absolute_start_offset; - - return true; - -bail_out: - return false; -} - -/* - * Read a specific meta data key and encode it into the output stream. - */ -static inline bool read_meta_data_key(char *key) -{ - bool retval = false; - VixError err; - size_t requiredLen; - char *buffer = NULL; - struct runtime_meta_data_encoding rmde; - - if (verbose) { - fprintf(stderr, "Processing metadata key %s\n", key); - } - - err = VixDiskLib_ReadMetadata(read_diskHandle, key, NULL, 0, &requiredLen); - if (err != VIX_OK && err != VIX_E_BUFFER_TOOSMALL) { - return false; - } - - buffer = (char *)malloc(requiredLen); - if (!buffer) { - fprintf(stderr, "Failed to allocate memory for holding metadata keys, exiting ...\n"); - return false; - } - - err = VixDiskLib_ReadMetadata(read_diskHandle, key, buffer, requiredLen, NULL); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to read metadata for key %s : %s [%d] exiting ...\n", key, error_txt, err); - goto bail_out; - } - - /* - * Should we clone metadata to new VMDK file ? - */ - if (write_diskHandle) { - VixError err; - - err = VixDiskLib_WriteMetadata(write_diskHandle, key, buffer); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to write metadata for key %s : %s [%d] exiting ...\n", key, error_txt, err); - goto bail_out; - } - } - - rmde.start_magic = BAREOSMAGIC; - rmde.end_magic = BAREOSMAGIC; - rmde.meta_key_length = strlen(key) + 1; - rmde.meta_data_length = requiredLen + 1; - - if (robust_writer(STDOUT_FILENO, &rmde, rmde_size) != rmde_size) { - fprintf(stderr, "Failed to write runtime_meta_data_encoding structure to output datastream\n"); - goto bail_out; - } - - if (robust_writer(STDOUT_FILENO, key, rmde.meta_key_length) != rmde.meta_key_length) { - fprintf(stderr, "Failed to write meta data key to output datastream\n"); - goto bail_out; - } - - if (robust_writer(STDOUT_FILENO, buffer, rmde.meta_data_length) != rmde.meta_data_length) { - fprintf(stderr, "Failed to write meta data to output datastream\n"); - goto bail_out; - } - - retval = true; - -bail_out: - if (buffer) { - free(buffer); - } - - return retval; -} - -/* - * Read all meta data from a disk and encode it into the output stream. - */ -static inline bool save_meta_data() -{ - bool retval = false; - VixError err; - size_t requiredLen; - char *bp; - char *buffer = NULL; - struct runtime_meta_data_encoding rmde; - - /* - * See if we are actually saving all meta data or should only write the META data end marker. - */ - if (save_metadata) { - err = VixDiskLib_GetMetadataKeys(read_diskHandle, NULL, 0, &requiredLen); - if (err != VIX_OK && err != VIX_E_BUFFER_TOOSMALL) { - return false; - } - - buffer = (char *)malloc(requiredLen); - if (!buffer) { - fprintf(stderr, "Failed to allocate memory for holding metadata keys, exiting ...\n"); - return false; - } - - err = VixDiskLib_GetMetadataKeys(read_diskHandle, buffer, requiredLen, NULL); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to read metadata keys : %s [%d] exiting ...\n", error_txt, err); - goto bail_out; - } - - bp = buffer; - while (*bp) { - if (!read_meta_data_key(bp)) { - goto bail_out; - } - - bp += strlen(bp) + 1; - } - } - - /* - * Write an META data end marker. - * e.g. metadata header with key and data length == 0 - */ - rmde.start_magic = BAREOSMAGIC; - rmde.end_magic = BAREOSMAGIC; - rmde.meta_key_length = 0; - rmde.meta_data_length = 0; - - if (robust_writer(STDOUT_FILENO, &rmde, rmde_size) != rmde_size) { - fprintf(stderr, "Failed to write runtime_meta_data_encoding structure to output datastream\n"); - goto bail_out; - } - - retval = true; - -bail_out: - if (buffer) { - free(buffer); - } - - return retval; -} - -/* - * Read a backup stream from STDIN and process its metadata. - * Stop processing when we encounter the special end of metadata tag - * which is when the meta_key_length and meta_data_length are zero. - */ -static inline bool process_meta_data(bool validate_only) -{ - struct runtime_meta_data_encoding rmde; - char *key = NULL; - char *buffer = NULL; - - while (1) { - if (robust_reader(STDIN_FILENO, &rmde, rmde_size) != rmde_size) { - fprintf(stderr, "Failed to read runtime_meta_data_encoding structure from input datastream\n"); - goto bail_out; - } - - if (rmde.start_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_meta_data_encoding] Failed to find valid MAGIC start marker read %lld should have been %lld\n", rmde.start_magic, BAREOSMAGIC); - goto bail_out; - } - - if (rmde.end_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_meta_data_encoding] Failed to find valid MAGIC end marker read %lld should have been %lld\n", rmde.end_magic, BAREOSMAGIC); - goto bail_out; - } - - /* - * See if we processed the last meta data item. - */ - if (rmde.meta_key_length == 0 && rmde.meta_data_length == 0) { - break; - } - - key = (char *)malloc(rmde.meta_key_length); - if (!key) { - fprintf(stderr, "Failed to allocate %d bytes for meta data key\n", rmde.meta_key_length); - goto bail_out; - } - - if (robust_reader(STDIN_FILENO, key, rmde.meta_key_length) != rmde.meta_key_length) { - fprintf(stderr, "Failed to read meta data key from input datastream\n"); - goto bail_out; - } - - buffer = (char *)malloc(rmde.meta_data_length); - if (!key) { - fprintf(stderr, "Failed to allocate %d bytes for meta data\n", rmde.meta_data_length); - goto bail_out; - } - - if (robust_reader(STDIN_FILENO, buffer, rmde.meta_data_length) != rmde.meta_data_length) { - fprintf(stderr, "Failed to read meta data from input datastream\n"); - goto bail_out; - } - - if (verbose) { - fprintf(stderr, "Meta data key %s, value %s\n", key, buffer); - } - - if (!validate_only && restore_meta_data) { - VixError err; - - err = VixDiskLib_WriteMetadata(write_diskHandle, key, buffer); - if (VIX_FAILED(err)) { - char *error_txt; - - error_txt = VixDiskLib_GetErrorText(err, NULL); - fprintf(stderr, "Failed to write metadata for key %s : %s [%d] exiting ...\n", key, error_txt, err); - goto bail_out; - } - } - - free(key); - free(buffer); - } - - return true; - -bail_out: - return false; -} - -/* - * Process the Change Block Tracking information and write the wanted sectors - * to the output stream. We self encode the data using a prefix header that describes - * the data that is encoded after it including a MAGIC key and the actual CBT information - * e.g. start of the read sectors and the number of sectors that follow. - */ -static inline bool process_cbt(const char *key, json_t *cbt) -{ - bool retval = false; - size_t index; - json_t *object, *array_element, *start, *length; - uint64_t start_offset, offset_length, current_offset, max_offset; - uint64_t sector_offset, sectors_to_read; - uint8 buf[DEFAULT_SECTOR_SIZE * SECTORS_PER_CALL]; - struct runtime_cbt_encoding rce; - - if (!read_diskHandle) { - fprintf(stderr, "Cannot process CBT data as no VixDiskLib disk handle opened\n"); - goto bail_out; - } - - object = json_object_get(cbt, CBT_CHANGEDAREA_KEY); - if (!object) { - fprintf(stderr, "Failed to find %s in JSON definition of object %s\n", CBT_CHANGEDAREA_KEY, key); - goto bail_out; - } - - /* - * Iterate over each element of the JSON array and get the "start" and "length" member. - */ - rce.start_magic = BAREOSMAGIC; - rce.end_magic = BAREOSMAGIC; - json_array_foreach(object, index, array_element) { - /* - * Get the two members we are interested in. - */ - start = json_object_get(array_element, CBT_CHANGEDAREA_START_KEY); - length = json_object_get(array_element, CBT_CHANGEDAREA_LENGTH_KEY); - - if (!start || !length) { - continue; - } - - start_offset = json_integer_value(start); - offset_length = json_integer_value(length); - - if (verbose) { - fprintf(stderr, "start = %lld\n", start_offset); - fprintf(stderr, "length = %lld\n", offset_length); - fprintf(stderr, "nr length = %lld\n", offset_length / DEFAULT_SECTOR_SIZE); - fflush(stderr); - } - - rce.start_offset = start_offset; - rce.offset_length = offset_length; - - /* - * Write the CBT info into the output stream. - */ - if (robust_writer(STDOUT_FILENO, (char *)&rce, rce_size) != rce_size) { - fprintf(stderr, "Failed to write runtime_cbt_encoding structure to output datastream\n"); - goto bail_out; - } - - if (raw_disk_fd != -1) { - lseek(raw_disk_fd, start_offset, SEEK_SET); - if (verbose) { - fprintf(stderr, "Log: RAWFILE: Adusting seek position in file\n"); - } - } - - /* - * Calculate the start offset and read as many sectors as defined by the - * length element of the JSON structure. - */ - current_offset = absolute_start_offset + start_offset; - max_offset = current_offset + offset_length; - sector_offset = current_offset / DEFAULT_SECTOR_SIZE; - while (current_offset < max_offset) { - /* - * The number of sectors to read is the minimum of either the total number - * of sectors still available in this CBT range or the upper setting specified - * in the sectors_per_call variable. - */ - sectors_to_read = MIN(sectors_per_call, (offset_length / DEFAULT_SECTOR_SIZE)); - - if (multi_threaded) { - if (!send_to_copy_thread(sector_offset, sectors_to_read * DEFAULT_SECTOR_SIZE)) { - goto bail_out; - } - } else { - if (read_from_vmdk(sector_offset, sectors_to_read * DEFAULT_SECTOR_SIZE, - buf) != sectors_to_read * DEFAULT_SECTOR_SIZE) { - fprintf(stderr, "Read error on VMDK\n"); - goto bail_out; - } - - if (write_to_stream(sector_offset, sectors_to_read * DEFAULT_SECTOR_SIZE, - buf) != sectors_to_read * DEFAULT_SECTOR_SIZE) { - fprintf(stderr, "Failed to write data to output datastream\n"); - goto bail_out; - } - } - - /* - * Calculate the new offsets for a next run. - */ - current_offset += (sectors_to_read * DEFAULT_SECTOR_SIZE); - sector_offset += sectors_to_read; - offset_length -= sectors_to_read * DEFAULT_SECTOR_SIZE; - } - - if (multi_threaded) { - flush_copy_thread(); - } - - if (verbose) { - fflush(stderr); - } - } - - if (multi_threaded) { - cleanup_copy_thread(); - } - - retval = true; - -bail_out: - if (read_diskHandle) { - VixDiskLib_Close(read_diskHandle); - read_diskHandle = NULL; - } - - return retval; -} - -/* - * Read a backup stream from STDIN and process it. When validate_only is set - * to true we only try to process the data but do not actually write it back - * to the VMDK. - */ -static inline bool process_restore_stream(bool validate_only, json_t *value) -{ - bool retval = false; - size_t cnt; - uint64_t current_offset, max_offset; - uint64_t sector_offset, sectors_to_read; - uint8 buf[DEFAULT_SECTOR_SIZE * SECTORS_PER_CALL]; - struct runtime_cbt_encoding rce; - - if (!create_disk && !validate_only) { - do_vixdisklib_open(DISK_PARAMS_KEY, vmdk_disk_name, value, false, true, &write_diskHandle); - - if (!write_diskHandle) { - fprintf(stderr, "Cannot process restore data as no VixDiskLib disk handle opened\n"); - goto bail_out; - } - } - - /* - * Setup multi threading if requested. - */ - if (!validate_only && multi_threaded) { - if (!setup_copy_thread(read_from_stream, write_to_vmdk)) { - fprintf(stderr, "Failed to initialize multithreading\n"); - goto bail_out; - } - } - - /* - * Process the disk info data. - */ - if (!process_disk_info(validate_only, value)) { - goto bail_out; - } - - /* - * Process the disk meta data, - */ - if (!process_meta_data(validate_only)) { - goto bail_out; - } - - memset(&rce, 0, rce_size); - while (robust_reader(STDIN_FILENO, (char *)&rce, rce_size) == rce_size) { - if (rce.start_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_cbt_encoding] Failed to find valid MAGIC start marker read %lld should have been %lld\n", rce.start_magic, BAREOSMAGIC); - goto bail_out; - } - - if (rce.end_magic != BAREOSMAGIC) { - fprintf(stderr, "[runtime_cbt_encoding] Failed to find valid MAGIC end marker read %lld should have been %lld\n", rce.end_magic, BAREOSMAGIC); - goto bail_out; - } - - if (verbose) { - fprintf(stderr, "start = %lld\n", rce.start_offset); - fprintf(stderr, "length = %lld\n", rce.offset_length); - fprintf(stderr, "nr length = %lld\n", rce.offset_length / DEFAULT_SECTOR_SIZE); - fflush(stderr); - } - - current_offset = absolute_start_offset + rce.start_offset; - max_offset = current_offset + rce.offset_length; - sector_offset = current_offset / DEFAULT_SECTOR_SIZE; - while (current_offset < max_offset) { - /* - * The number of sectors to read is the minimum of either the total number - * of sectors still available in this CBT range or the upper setting specified - * in the sectors_per_call variable. - */ - sectors_to_read = MIN(sectors_per_call, (rce.offset_length / DEFAULT_SECTOR_SIZE)); - - if (!validate_only && multi_threaded) { - if (!send_to_copy_thread(sector_offset, sectors_to_read * DEFAULT_SECTOR_SIZE)) { - goto bail_out; - } - } else { - cnt = robust_reader(STDIN_FILENO, (char *)buf, sectors_to_read * DEFAULT_SECTOR_SIZE); - if (cnt != sectors_to_read * DEFAULT_SECTOR_SIZE) { - goto bail_out; - } - - if (!validate_only) { - if (write_to_vmdk(sector_offset, cnt, buf) != cnt) { - goto bail_out; - } - } - } - - /* - * Calculate the new offsets for a next run. - */ - current_offset += (sectors_to_read * DEFAULT_SECTOR_SIZE); - sector_offset += sectors_to_read; - rce.offset_length -= sectors_to_read * DEFAULT_SECTOR_SIZE; - } - - if (multi_threaded) { - flush_copy_thread(); - } - - memset(&rce, 0, rce_size); - } - - if (multi_threaded) { - cleanup_copy_thread(); - } - - retval = true; - -bail_out: - if (write_diskHandle) { - VixDiskLib_Close(write_diskHandle); - write_diskHandle = NULL; - } - - return retval; -} - -/* - * All work to this program is passed in using a JSON work file which has the needed - * information to perform the wanted operation. This function loads the JSON data into - * memory using the Jansson JSON library. - */ -static inline void process_json_work_file(const char *json_work_file) -{ - json_error_t error; - - /* - * Open the JSON work file. - */ - json_config = json_load_file(json_work_file, JSON_DECODE_ANY, &error); - if (!json_config) { - fprintf(stderr, "Failed to parse JSON config %s [%s at line %d column %d]\n", - json_work_file, error.text, error.line, error.column); - exit(1); - } - - if (verbose) { - /* - * Dump the internal parsed data in pretty print format. - */ - json_dumpf(json_config, stderr, JSON_PRESERVE_ORDER | JSON_COMPACT | JSON_INDENT(3)); - fprintf(stderr, "\n"); - fflush(stderr); - } -} - -/* - * Worker function that performs the dump operation of the program. - */ -static inline bool dump_vmdk_stream(const char *json_work_file) -{ - json_t *value; - uint64_t absolute_disk_length = 0; - - process_json_work_file(json_work_file); - - value = json_object_get(json_config, CON_PARAMS_KEY); - if (!value) { - fprintf(stderr, "Failed to find %s in JSON definition\n", CON_PARAMS_KEY); - exit(1); - } - - do_vixdisklib_connect(CON_PARAMS_KEY, value, true, true); - - if (cleanup_on_start) { - cleanup_vixdisklib(); - } - - value = json_object_get(json_config, DISK_PARAMS_KEY); - if (!value) { - fprintf(stderr, "Failed to find %s in JSON definition\n", DISK_PARAMS_KEY); - exit(1); - } - - do_vixdisklib_open(DISK_PARAMS_KEY, NULL, value, true, true, &read_diskHandle); - - value = json_object_get(json_config, CBT_DISKCHANGEINFO_KEY); - if (!value) { - fprintf(stderr, "Failed to find %s in JSON definition\n", CBT_DISKCHANGEINFO_KEY); - exit(1); - } - - /* - * Setup multi threading if requested. - */ - if (multi_threaded) { - if (!setup_copy_thread(read_from_vmdk, write_to_stream)) { - fprintf(stderr, "Failed to initialize multithreading\n"); - exit(1); - } - } - - if (!save_disk_info(CBT_DISKCHANGEINFO_KEY, value, &absolute_disk_length)) { - exit(1); - } - - /* - * See if we are requested to clone the content to a new VMDK. - * save_disk_info() initializes absolute_disk_length. - */ - if (vmdk_disk_name) { - if (create_disk) { - do_vixdisklib_create(NULL, vmdk_disk_name, value, absolute_disk_length); - } - - do_vixdisklib_open(NULL, vmdk_disk_name, value, false, false, &write_diskHandle); - } - - if (!save_meta_data()) { - exit(1); - } - - /* - * See if we are requested to clone the content to a rawdevice. - */ - if (raw_disk_name) { - if (verbose) { - fprintf(stderr, "Log: RAWFILE: Trying to open RAW file\n"); - } - - if ((raw_disk_fd = open(raw_disk_name, O_WRONLY | O_TRUNC)) == -1) { - fprintf(stderr, "Error: Failed to open the RAW DISK FILE\n"); - exit(1); - } - } - - return process_cbt(CBT_DISKCHANGEINFO_KEY, value); -} - -/* - * Worker function that performs the restore operation of the program. - */ -static inline bool restore_vmdk_stream(const char *json_work_file) -{ - json_t *value; - - process_json_work_file(json_work_file); - - value = json_object_get(json_config, CON_PARAMS_KEY); - if (!value) { - fprintf(stderr, "Failed to find %s in JSON definition\n", CON_PARAMS_KEY); - exit(1); - } - - if (cleanup_on_start) { - cleanup_vixdisklib(); - } - - do_vixdisklib_connect(CON_PARAMS_KEY, value, false, false); - - value = json_object_get(json_config, DISK_PARAMS_KEY); - if (!value) { - fprintf(stderr, "Failed to find %s in JSON definition\n", DISK_PARAMS_KEY); - exit(1); - } - - return process_restore_stream(false, value); -} - -/* - * Worker function that performs the show operation of the program. - */ -static inline int show_backup_stream() -{ - return process_restore_stream(true, NULL); -} - -static void signal_handler(int sig) -{ - exit_code = sig; - exit(sig); -} - -void usage(const char *program_name) -{ - fprintf(stderr, "Usage: %s [-d ] [-f force_transport] [-s sectors_per_call] [-t disktype] [-CcDlMmRSv] dump | restore | show\n", program_name); - fprintf(stderr, "Where:\n"); - fprintf(stderr, " -C - Create local VMDK\n"); - fprintf(stderr, " -c - Don't check size of VMDK\n"); - fprintf(stderr, " -D - Cleanup on Disconnect\n"); - fprintf(stderr, " -d - Specify local VMDK name\n"); - fprintf(stderr, " -f - Specify forced transport method\n"); - fprintf(stderr, " -h - This help text\n"); - fprintf(stderr, " -l - Write to a local VMDK\n"); - fprintf(stderr, " -M - Save metadata of VMDK on dump action\n"); - fprintf(stderr, " -m - Use multithreading\n"); - fprintf(stderr, " -r - RAW Image disk name\n"); - fprintf(stderr, " -R - Restore metadata of VMDK on restore action\n"); - fprintf(stderr, " -S - Cleanup on Start\n"); - fprintf(stderr, " -s - Sectors to read per call to VDDK\n"); - fprintf(stderr, " -t - Disktype to create for local VMDK\n"); - fprintf(stderr, " -v - Verbose output\n"); - fprintf(stderr, " -? - This help text\n"); - exit(1); -} - -int main(int argc, char **argv) -{ - bool retval; - const char *program_name; - int ch; - - program_name = argv[0]; - while ((ch = getopt(argc, argv, "CcDd:r:f:hlMmRSs:t:v?")) != -1) { - switch (ch) { - case 'C': - create_disk = true; - /* - * If we create the disk we should not check for the size as that won't match. - */ - check_size = false; - break; - case 'c': - check_size = false; - break; - case 'D': - cleanup_on_disconnect = true; - break; - case 'd': - vmdk_disk_name = strdup(optarg); - if (!vmdk_disk_name) { - fprintf(stderr, "Failed to allocate memory to hold diskname, exiting ...\n"); - exit(1); - } - break; - case 'r': - raw_disk_name = strdup(optarg); - if (!raw_disk_name) { - fprintf(stderr, "Failed to allocate memory to hold rawdiskname, exiting ...\n"); - exit(1); - } - break; - case 'f': - force_transport = strdup(optarg); - if (!force_transport) { - fprintf(stderr, "Failed to allocate memory to hold forced transport, exiting ...\n"); - exit(1); - } - break; - case 'l': - local_vmdk = true; - break; - case 'M': - save_metadata = true; - break; - case 'm': - multi_threaded = true; - break; - case 'R': - restore_meta_data = true; - break; - case 'S': - cleanup_on_start = true; - break; - case 's': - sectors_per_call = atoi(optarg); - break; - case 't': - disktype = strdup(optarg); - if (!disktype) { - fprintf(stderr, "Failed to allocate memory to hold disktype, exiting ...\n"); - exit(1); - } - break; - case 'v': - verbose = true; - break; - case 'h': - case '?': - default: - usage(program_name); - break; - } - } - - /* - * Install signal handlers for the most important signals. - */ - signal(SIGHUP, signal_handler); - signal(SIGINT, signal_handler); - signal(SIGTERM, signal_handler); - - argc -= optind; - argv += optind; - - if (argc <= 0) { - usage(program_name); - } - - if (strcasecmp(argv[0], "dump") == 0) { - if (argc <= 1) { - usage(program_name); - } - - retval = dump_vmdk_stream(argv[1]); - } else if (strcasecmp(argv[0], "restore") == 0) { - if (argc <= 1) { - usage(program_name); - } - - retval = restore_vmdk_stream(argv[1]); - } else if (strcasecmp(argv[0], "show") == 0) { - retval = show_backup_stream(); - } else { - fprintf(stderr, "Unknown action %s\n", argv[1]); - } - - if (retval) { - exit_code = 0; - } - - exit(exit_code); -} diff --git a/vmware/vadp_dumper/cbuf.cc b/vmware/vadp_dumper/cbuf.cc new file mode 100644 index 00000000000..6113d69f43c --- /dev/null +++ b/vmware/vadp_dumper/cbuf.cc @@ -0,0 +1,166 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + 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.vadp. + + 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. +*/ +/* + * Marco van Wieringen, August 2013. + */ + +/* + * Circular buffer used for producer/consumer problem with pthreads. + */ +#include +#include "cbuf.h" + +/* + * Initialize a new circular buffer. + */ +int circbuf::init() +{ + if (pthread_mutex_init(&m_lock, NULL) != 0) { return -1; } + + if (pthread_cond_init(&m_notfull, NULL) != 0) { + pthread_mutex_destroy(&m_lock); + return -1; + } + + if (pthread_cond_init(&m_notempty, NULL) != 0) { + pthread_cond_destroy(&m_notfull); + pthread_mutex_destroy(&m_lock); + return -1; + } + + m_next_in = 0; + m_next_out = 0; + m_size = 0; + m_capacity = QSIZE; + + return 0; +} + +/* + * Destroy a circular buffer. + */ +void circbuf::destroy() +{ + pthread_cond_destroy(&m_notempty); + pthread_cond_destroy(&m_notfull); + pthread_mutex_destroy(&m_lock); +} + +/* + * Enqueue a new item into the circular buffer. + */ +int circbuf::enqueue(void* data) +{ + if (pthread_mutex_lock(&m_lock) != 0) { return -1; } + + /* + * Wait while the buffer is full. + */ + while (full()) { pthread_cond_wait(&m_notfull, &m_lock); } + m_data[m_next_in++] = data; + m_size++; + m_next_in %= m_capacity; + + /* + * Let a waiting consumer know there is data. + */ + pthread_cond_signal(&m_notempty); + + pthread_mutex_unlock(&m_lock); + + return 0; +} + +/* + * Dequeue an item from the circular buffer. + */ +void* circbuf::dequeue() +{ + void* data; + + if (pthread_mutex_lock(&m_lock) != 0) { return NULL; } + + /* + * Wait while there is nothing in the buffer + */ + while (empty() && !m_flush) { pthread_cond_wait(&m_notempty, &m_lock); } + + /* + * When we are requested to flush and there is no data left return NULL. + */ + if (empty() && m_flush) { + m_flush = false; + pthread_mutex_unlock(&m_lock); + + return NULL; + } + + data = m_data[m_next_out++]; + m_size--; + m_next_out %= m_capacity; + + /* + * Let a waiting producer know there is room. + */ + pthread_cond_signal(&m_notfull); + + pthread_mutex_unlock(&m_lock); + + return data; +} + +/* + * Make sure there is a free next slot available on the circular buffer. + * So the next enqueue will not block but we block now until one is available. + */ +int circbuf::next_slot() +{ + if (pthread_mutex_lock(&m_lock) != 0) { return -1; } + + /* + * Wait while the buffer is full. + */ + while (full()) { pthread_cond_wait(&m_notfull, &m_lock); } + + pthread_mutex_unlock(&m_lock); + + return m_next_in; +} + +/* + * Flush the circular buffer. Any waiting consumer will be wakened and will + * see we are in flush state. + */ +int circbuf::flush() +{ + if (pthread_mutex_lock(&m_lock) != 0) { return -1; } + + m_flush = true; + + /* + * Let a waiting consumer know there will be no more data. + */ + pthread_cond_signal(&m_notempty); + + pthread_mutex_unlock(&m_lock); + + return 0; +} diff --git a/vmware/vadp_dumper/cbuf.cpp b/vmware/vadp_dumper/cbuf.cpp deleted file mode 100644 index 4d958e29fda..00000000000 --- a/vmware/vadp_dumper/cbuf.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2013-2013 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.vadp. - - 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. -*/ -/* - * Marco van Wieringen, August 2013. - */ - -/* - * Circular buffer used for producer/consumer problem with pthreads. - */ -#include -#include "cbuf.hpp" - -/* - * Initialize a new circular buffer. - */ -int circbuf::init() -{ - if (pthread_mutex_init(&m_lock, NULL) != 0) { - return -1; - } - - if (pthread_cond_init(&m_notfull, NULL) != 0) { - pthread_mutex_destroy(&m_lock); - return -1; - } - - if (pthread_cond_init(&m_notempty, NULL) != 0) { - pthread_cond_destroy(&m_notfull); - pthread_mutex_destroy(&m_lock); - return -1; - } - - m_next_in = 0; - m_next_out = 0; - m_size = 0; - m_capacity = QSIZE; - - return 0; -} - -/* - * Destroy a circular buffer. - */ -void circbuf::destroy() -{ - pthread_cond_destroy(&m_notempty); - pthread_cond_destroy(&m_notfull); - pthread_mutex_destroy(&m_lock); -} - -/* - * Enqueue a new item into the circular buffer. - */ -int circbuf::enqueue(void *data) -{ - if (pthread_mutex_lock(&m_lock) != 0) { - return -1; - } - - /* - * Wait while the buffer is full. - */ - while (full()) { - pthread_cond_wait(&m_notfull, &m_lock); - } - m_data[m_next_in++] = data; - m_size++; - m_next_in %= m_capacity; - - /* - * Let a waiting consumer know there is data. - */ - pthread_cond_signal(&m_notempty); - - pthread_mutex_unlock(&m_lock); - - return 0; -} - -/* - * Dequeue an item from the circular buffer. - */ -void *circbuf::dequeue() -{ - void *data; - - if (pthread_mutex_lock(&m_lock) != 0) { - return NULL; - } - - /* - * Wait while there is nothing in the buffer - */ - while (empty() && !m_flush) { - pthread_cond_wait(&m_notempty, &m_lock); - } - - /* - * When we are requested to flush and there is no data left return NULL. - */ - if (empty() && m_flush) { - m_flush = false; - pthread_mutex_unlock(&m_lock); - - return NULL; - } - - data = m_data[m_next_out++]; - m_size--; - m_next_out %= m_capacity; - - /* - * Let a waiting producer know there is room. - */ - pthread_cond_signal(&m_notfull); - - pthread_mutex_unlock(&m_lock); - - return data; -} - -/* - * Make sure there is a free next slot available on the circular buffer. - * So the next enqueue will not block but we block now until one is available. - */ -int circbuf::next_slot() -{ - if (pthread_mutex_lock(&m_lock) != 0) { - return -1; - } - - /* - * Wait while the buffer is full. - */ - while (full()) { - pthread_cond_wait(&m_notfull, &m_lock); - } - - pthread_mutex_unlock(&m_lock); - - return m_next_in; -} - -/* - * Flush the circular buffer. Any waiting consumer will be wakened and will - * see we are in flush state. - */ -int circbuf::flush() -{ - if (pthread_mutex_lock(&m_lock) != 0) { - return -1; - } - - m_flush = true; - - /* - * Let a waiting consumer know there will be no more data. - */ - pthread_cond_signal(&m_notempty); - - pthread_mutex_unlock(&m_lock); - - return 0; -} diff --git a/vmware/vadp_dumper/cbuf.hpp b/vmware/vadp_dumper/cbuf.h similarity index 100% rename from vmware/vadp_dumper/cbuf.hpp rename to vmware/vadp_dumper/cbuf.h diff --git a/vmware/vadp_dumper/copy_thread.cc b/vmware/vadp_dumper/copy_thread.cc new file mode 100644 index 00000000000..15cbb0e5046 --- /dev/null +++ b/vmware/vadp_dumper/copy_thread.cc @@ -0,0 +1,254 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2014-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.vadp. + + 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. +*/ +/* + * Marco van Wieringen, August 2014. + */ + +/* + * Copy thread used for producer/consumer problem with pthreads. + */ +#include +#include +#include +#include +#include +#include + +#include +#include "copy_thread.h" + +static CP_THREAD_CTX* cp_thread; + +/* + * Copy thread cancel handler. + */ +static void copy_cleanup_thread(void* data) +{ + CP_THREAD_CTX* context = (CP_THREAD_CTX*)data; + + pthread_mutex_unlock(&context->lock); +} + +/* + * Actual copy thread that copies data. + */ +static void* copy_thread(void* data) +{ + CP_THREAD_SAVE_DATA* save_data; + CP_THREAD_CTX* context = (CP_THREAD_CTX*)data; + + if (pthread_mutex_lock(&context->lock) != 0) { goto bail_out; } + + /* + * When we get canceled make sure we run the cleanup function. + */ + pthread_cleanup_push(copy_cleanup_thread, data); + + while (1) { + size_t cnt; + + /* + * Wait for the moment we are supposed to start. + * We are signalled by the restore thread. + */ + pthread_cond_wait(&context->start, &context->lock); + context->started = true; + + pthread_mutex_unlock(&context->lock); + + /* + * Dequeue an item from the circular buffer. + */ + save_data = (CP_THREAD_SAVE_DATA*)context->cb->dequeue(); + + while (save_data) { + cnt = cp_thread->output_function(save_data->sector_offset, + save_data->data_len, save_data->data); + if (cnt < save_data->data_len) { return NULL; } + save_data = (CP_THREAD_SAVE_DATA*)context->cb->dequeue(); + } + + /* + * Need to synchronize the main thread and this one so the main thread + * cannot miss the conditional signal. + */ + if (pthread_mutex_lock(&context->lock) != 0) { goto bail_out; } + + /* + * Signal the main thread we flushed the data. + */ + pthread_cond_signal(&context->flush); + + context->started = false; + context->flushed = true; + } + + pthread_cleanup_pop(1); + +bail_out: + return NULL; +} + +/* + * Create a copy thread. + */ +bool setup_copy_thread(IO_FUNCTION* input_function, + IO_FUNCTION* output_function) +{ + int nr_save_elements; + CP_THREAD_CTX* new_context; + + new_context = (CP_THREAD_CTX*)malloc(sizeof(CP_THREAD_CTX)); + new_context->started = false; + new_context->flushed = false; + new_context->cb = new circbuf; + + nr_save_elements = new_context->cb->capacity(); + new_context->save_data = (CP_THREAD_SAVE_DATA*)malloc( + nr_save_elements * sizeof(CP_THREAD_SAVE_DATA)); + memset(new_context->save_data, 0, + nr_save_elements * sizeof(CP_THREAD_SAVE_DATA)); + new_context->nr_save_elements = nr_save_elements; + + new_context->input_function = input_function; + new_context->output_function = output_function; + + if (pthread_mutex_init(&new_context->lock, NULL) != 0) { goto bail_out; } + + if (pthread_cond_init(&new_context->start, NULL) != 0) { + pthread_mutex_destroy(&new_context->lock); + goto bail_out; + } + + if (pthread_create(&new_context->thread_id, NULL, copy_thread, + (void*)new_context) != 0) { + pthread_cond_destroy(&new_context->start); + pthread_mutex_destroy(&new_context->lock); + goto bail_out; + } + + cp_thread = new_context; + return true; + +bail_out: + free(new_context->save_data); + delete new_context->cb; + free(new_context); + + return false; +} + +/* + * Read a new piece of data via the input_function and put it onto the circular + * buffer. + */ +bool send_to_copy_thread(size_t sector_offset, size_t nbyte) +{ + int slotnr; + circbuf* cb = cp_thread->cb; + CP_THREAD_SAVE_DATA* save_data; + + /* + * Find out which next slot will be used on the Circular Buffer. + * The method will block when the circular buffer is full until a slot is + * available. + */ + slotnr = cb->next_slot(); + save_data = &cp_thread->save_data[slotnr]; + + /* + * If this is the first time we use this slot we need to allocate some memory. + */ + if (!save_data->data) { save_data->data = malloc(nbyte + 1); } + + save_data->data_len = + cp_thread->input_function(sector_offset, nbyte, save_data->data); + if (save_data->data_len < 0) { return false; } + save_data->sector_offset = sector_offset; + + cb->enqueue(save_data); + + /* + * Signal the copy thread its time to start if it didn't start yet. + */ + if (!cp_thread->started) { pthread_cond_signal(&cp_thread->start); } + + return true; +} + +/* + * Flush the copy thread. + */ +void flush_copy_thread() +{ + CP_THREAD_CTX* context = cp_thread; + + if (pthread_mutex_lock(&context->lock) != 0) { return; } + + /* + * In essence the flush should work in one shot but be a bit more + * conservative. + */ + while (!context->flushed) { + /* + * Tell the copy thread to flush out all data. + */ + context->cb->flush(); + + /* + * Wait for the copy thread to say it flushed the data out. + */ + pthread_cond_wait(&context->flush, &context->lock); + } + + context->flushed = false; + + pthread_mutex_unlock(&context->lock); +} + +/* + * Cleanup all data allocated for the copy thread. + */ +void cleanup_copy_thread() +{ + int slotnr; + + /* + * Stop the copy thread. + */ + if (!pthread_equal(cp_thread->thread_id, pthread_self())) { + pthread_cancel(cp_thread->thread_id); + pthread_join(cp_thread->thread_id, NULL); + } + + /* + * Free all data allocated along the way. + */ + for (slotnr = 0; slotnr < cp_thread->nr_save_elements; slotnr++) { + if (cp_thread->save_data[slotnr].data) { + free(cp_thread->save_data[slotnr].data); + } + } + free(cp_thread->save_data); + delete cp_thread->cb; + free(cp_thread); + cp_thread = NULL; +} diff --git a/vmware/vadp_dumper/copy_thread.cpp b/vmware/vadp_dumper/copy_thread.cpp deleted file mode 100644 index e0b79c2d7d7..00000000000 --- a/vmware/vadp_dumper/copy_thread.cpp +++ /dev/null @@ -1,260 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2014-2014 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.vadp. - - 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. -*/ -/* - * Marco van Wieringen, August 2014. - */ - -/* - * Copy thread used for producer/consumer problem with pthreads. - */ -#include -#include -#include -#include -#include -#include - -#include -#include "copy_thread.hpp" - -static CP_THREAD_CTX *cp_thread; - -/* - * Copy thread cancel handler. - */ -static void copy_cleanup_thread(void *data) -{ - CP_THREAD_CTX *context = (CP_THREAD_CTX *)data; - - pthread_mutex_unlock(&context->lock); -} - -/* - * Actual copy thread that copies data. - */ -static void *copy_thread(void *data) -{ - CP_THREAD_SAVE_DATA *save_data; - CP_THREAD_CTX *context = (CP_THREAD_CTX *)data; - - if (pthread_mutex_lock(&context->lock) != 0) { - goto bail_out; - } - - /* - * When we get canceled make sure we run the cleanup function. - */ - pthread_cleanup_push(copy_cleanup_thread, data); - - while (1) { - size_t cnt; - - /* - * Wait for the moment we are supposed to start. - * We are signalled by the restore thread. - */ - pthread_cond_wait(&context->start, &context->lock); - context->started = true; - - pthread_mutex_unlock(&context->lock); - - /* - * Dequeue an item from the circular buffer. - */ - save_data = (CP_THREAD_SAVE_DATA *)context->cb->dequeue(); - - while (save_data) { - cnt = cp_thread->output_function(save_data->sector_offset, save_data->data_len, save_data->data); - if (cnt < save_data->data_len) { - return NULL; - } - save_data = (CP_THREAD_SAVE_DATA *)context->cb->dequeue(); - } - - /* - * Need to synchronize the main thread and this one so the main thread cannot miss the conditional signal. - */ - if (pthread_mutex_lock(&context->lock) != 0) { - goto bail_out; - } - - /* - * Signal the main thread we flushed the data. - */ - pthread_cond_signal(&context->flush); - - context->started = false; - context->flushed = true; - } - - pthread_cleanup_pop(1); - -bail_out: - return NULL; -} - -/* - * Create a copy thread. - */ -bool setup_copy_thread(IO_FUNCTION *input_function, IO_FUNCTION *output_function) -{ - int nr_save_elements; - CP_THREAD_CTX *new_context; - - new_context = (CP_THREAD_CTX *)malloc(sizeof(CP_THREAD_CTX)); - new_context->started = false; - new_context->flushed = false; - new_context->cb = new circbuf; - - nr_save_elements = new_context->cb->capacity(); - new_context->save_data = (CP_THREAD_SAVE_DATA *)malloc(nr_save_elements * sizeof(CP_THREAD_SAVE_DATA)); - memset(new_context->save_data, 0, nr_save_elements * sizeof(CP_THREAD_SAVE_DATA)); - new_context->nr_save_elements = nr_save_elements; - - new_context->input_function = input_function; - new_context->output_function = output_function; - - if (pthread_mutex_init(&new_context->lock, NULL) != 0) { - goto bail_out; - } - - if (pthread_cond_init(&new_context->start, NULL) != 0) { - pthread_mutex_destroy(&new_context->lock); - goto bail_out; - } - - if (pthread_create(&new_context->thread_id, NULL, copy_thread, (void *)new_context) != 0) { - pthread_cond_destroy(&new_context->start); - pthread_mutex_destroy(&new_context->lock); - goto bail_out; - } - - cp_thread = new_context; - return true; - -bail_out: - free(new_context->save_data); - delete new_context->cb; - free(new_context); - - return false; -} - -/* - * Read a new piece of data via the input_function and put it onto the circular buffer. - */ -bool send_to_copy_thread(size_t sector_offset, size_t nbyte) -{ - int slotnr; - circbuf *cb = cp_thread->cb; - CP_THREAD_SAVE_DATA *save_data; - - /* - * Find out which next slot will be used on the Circular Buffer. - * The method will block when the circular buffer is full until a slot is available. - */ - slotnr = cb->next_slot(); - save_data = &cp_thread->save_data[slotnr]; - - /* - * If this is the first time we use this slot we need to allocate some memory. - */ - if (!save_data->data) { - save_data->data = malloc(nbyte + 1); - } - - save_data->data_len = cp_thread->input_function(sector_offset, nbyte, save_data->data); - if (save_data->data_len < 0) { - return false; - } - save_data->sector_offset = sector_offset; - - cb->enqueue(save_data); - - /* - * Signal the copy thread its time to start if it didn't start yet. - */ - if (!cp_thread->started) { - pthread_cond_signal(&cp_thread->start); - } - - return true; -} - -/* - * Flush the copy thread. - */ -void flush_copy_thread() -{ - CP_THREAD_CTX *context = cp_thread; - - if (pthread_mutex_lock(&context->lock) != 0) { - return; - } - - /* - * In essence the flush should work in one shot but be a bit more conservative. - */ - while (!context->flushed) { - /* - * Tell the copy thread to flush out all data. - */ - context->cb->flush(); - - /* - * Wait for the copy thread to say it flushed the data out. - */ - pthread_cond_wait(&context->flush, &context->lock); - } - - context->flushed = false; - - pthread_mutex_unlock(&context->lock); -} - -/* - * Cleanup all data allocated for the copy thread. - */ -void cleanup_copy_thread() -{ - int slotnr; - - /* - * Stop the copy thread. - */ - if (!pthread_equal(cp_thread->thread_id, pthread_self())) { - pthread_cancel(cp_thread->thread_id); - pthread_join(cp_thread->thread_id, NULL); - } - - /* - * Free all data allocated along the way. - */ - for (slotnr = 0; slotnr < cp_thread->nr_save_elements; slotnr++) { - if (cp_thread->save_data[slotnr].data) { - free(cp_thread->save_data[slotnr].data); - } - } - free(cp_thread->save_data); - delete cp_thread->cb; - free(cp_thread); - cp_thread = NULL; -} diff --git a/vmware/vadp_dumper/copy_thread.h b/vmware/vadp_dumper/copy_thread.h new file mode 100644 index 00000000000..0ab035d7554 --- /dev/null +++ b/vmware/vadp_dumper/copy_thread.h @@ -0,0 +1,57 @@ +/* + BAREOS® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2014-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.vadp. + + 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. +*/ +/* + * Marco van Wieringen, August 2014. + */ + +/* + * Copy thread used for producer/consumer problem with pthreads. + */ +#include "cbuf.h" + +typedef size_t(IO_FUNCTION)(size_t sector_offset, size_t nbyte, void* buf); + +struct CP_THREAD_SAVE_DATA { + size_t sector_offset; /* Sector offset where to write data */ + size_t data_len; /* Length of Data */ + void* data; /* Data */ +}; + +struct CP_THREAD_CTX { + int nr_save_elements; /* Number of save items in save_data */ + CP_THREAD_SAVE_DATA* + save_data; /* To save data (cached structure build during restore) */ + circbuf* cb; /* Circular buffer for passing work to copy thread */ + bool started; /* Copy thread consuming data */ + bool flushed; /* Copy thread flushed data */ + pthread_t thread_id; /* Id of copy thread */ + pthread_mutex_t lock; /* Lock the structure */ + pthread_cond_t start; /* Start consuming data from the Circular buffer */ + pthread_cond_t flush; /* Flush data from the Circular buffer */ + IO_FUNCTION* input_function; /* IO function that performs the output I/O */ + IO_FUNCTION* output_function; /* IO function that performs the output I/O */ +}; + +bool setup_copy_thread(IO_FUNCTION* input_function, + IO_FUNCTION* output_function); +bool send_to_copy_thread(size_t sector_offset, size_t nbyte); +void flush_copy_thread(); +void cleanup_copy_thread(); diff --git a/vmware/vadp_dumper/copy_thread.hpp b/vmware/vadp_dumper/copy_thread.hpp deleted file mode 100644 index 70a1a9cc9a5..00000000000 --- a/vmware/vadp_dumper/copy_thread.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - BAREOS® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2014-2014 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.vadp. - - 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. -*/ -/* - * Marco van Wieringen, August 2014. - */ - -/* - * Copy thread used for producer/consumer problem with pthreads. - */ -#include "cbuf.hpp" - -typedef size_t (IO_FUNCTION)(size_t sector_offset, size_t nbyte, void *buf); - -struct CP_THREAD_SAVE_DATA { - size_t sector_offset; /* Sector offset where to write data */ - size_t data_len; /* Length of Data */ - void *data; /* Data */ -}; - -struct CP_THREAD_CTX { - int nr_save_elements; /* Number of save items in save_data */ - CP_THREAD_SAVE_DATA *save_data; /* To save data (cached structure build during restore) */ - circbuf *cb; /* Circular buffer for passing work to copy thread */ - bool started; /* Copy thread consuming data */ - bool flushed; /* Copy thread flushed data */ - pthread_t thread_id; /* Id of copy thread */ - pthread_mutex_t lock; /* Lock the structure */ - pthread_cond_t start; /* Start consuming data from the Circular buffer */ - pthread_cond_t flush; /* Flush data from the Circular buffer */ - IO_FUNCTION *input_function; /* IO function that performs the output I/O */ - IO_FUNCTION *output_function; /* IO function that performs the output I/O */ -}; - -bool setup_copy_thread(IO_FUNCTION *input_function, IO_FUNCTION *output_function); -bool send_to_copy_thread(size_t sector_offset, size_t nbyte); -void flush_copy_thread(); -void cleanup_copy_thread();