From 4eb8fbd8e44ff4265d5293b75ac58b663b175b4a Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sun, 25 Dec 2016 14:43:35 +0100 Subject: [PATCH] Tweak remove unneeded changes --- bacula/src/tools/dbcheck.c | 263 ++++++++++++++----------------------- 1 file changed, 96 insertions(+), 167 deletions(-) diff --git a/bacula/src/tools/dbcheck.c b/bacula/src/tools/dbcheck.c index 73ea9920d1..2f2eea7332 100644 --- a/bacula/src/tools/dbcheck.c +++ b/bacula/src/tools/dbcheck.c @@ -49,9 +49,8 @@ typedef struct s_name_ctx { int tot_ids; /* total to process */ } NAME_LIST; -/* - * Global variables - */ +/* Global variables */ + static bool fix = false; static bool batch = false; static BDB *db; @@ -60,14 +59,11 @@ static NAME_LIST name_list; static char buf[20000]; static bool quit = false; static CONFIG *config; -static const char *idx_tmp_name; +static const char *idx_tmp_name; #define MAX_ID_LIST_LEN 10000000 -/* - * Forward referenced functions - */ -static void print_catalog_details(CAT *catalog, const char *working_dir); +/* Forward referenced functions */ static int make_id_list(const char *query, ID_LIST *id_list); static int delete_id_list(const char *query, ID_LIST *id_list); static int make_name_list(const char *query, NAME_LIST *name_list); @@ -90,8 +86,8 @@ static void repair_bad_filenames(); static void do_interactive_mode(); static bool yes_no(const char *prompt); static bool check_idx(const char *col_name); -static bool create_tmp_idx(const char *idx_name, const char *table_name, - const char *col_name); +static bool create_tmp_idx(const char *idx_name, const char *table_name, + const char *col_name); static bool drop_tmp_idx(const char *idx_name, const char *table_name); static int check_idx_handler(void *ctx, int num_fields, char **row); @@ -134,12 +130,12 @@ int main (int argc, char *argv[]) lmgr_init_thread(); my_name_is(argc, argv, "dbcheck"); - init_msg(NULL, NULL); /* setup message handler */ + init_msg(NULL, NULL); /* setup message handler */ memset(&id_list, 0, sizeof(id_list)); memset(&name_list, 0, sizeof(name_list)); - while ((ch = getopt(argc, argv, "bc:C:d:fvB?")) != -1) { + while ((ch = getopt(argc, argv, "bc:C:d:fvB?")) != -1) { switch (ch) { case 'B': print_catalog = true; /* get catalog information from config */ @@ -216,11 +212,23 @@ int main (int argc, char *argv[]) } set_working_directory(director->working_directory); - /* - * Print catalog information and exit (-B) - */ + /* Print catalog information and exit (-B) */ if (print_catalog) { - print_catalog_details(catalog, director->working_directory); + + POOLMEM *catalog_details = get_pool_memory(PM_MESSAGE); + db = db_init_database(NULL, catalog->db_driver, catalog->db_name, catalog->db_user, + catalog->db_password, catalog->db_address, + catalog->db_port, catalog->db_socket, + catalog->db_ssl_key, catalog->db_ssl_cert, catalog->db_ssl_ca, + catalog->db_ssl_capath, catalog->db_ssl_cipher, + catalog->mult_db_connections, + catalog->disable_batch_insert); + if (db) { + printf("%sdb_type=%s\nworking_dir=%s\n", catalog->display(catalog_details), + db_get_engine_name(db), working_directory); + db_close_database(NULL, db); + } + free_pool_memory(catalog_details); exit(0); } @@ -249,9 +257,7 @@ int main (int argc, char *argv[]) usage(); } - /* - * This is needed by SQLite to find the db - */ + /* This is needed by SQLite to find the db */ working_directory = argv[0]; db_name = "bacula"; user = db_name; @@ -291,18 +297,15 @@ int main (int argc, char *argv[]) } /* if (argc >= 2) */ } - /* - * Open database - */ - db = db_init_database(NULL, NULL, db_name, user, password, dbhost, dbport, NULL, dbsslkey, dbsslcert, dbsslca, dbsslcapath, dbsslcipher, false, false); + /* Open database */ + db = db_init_database(NULL, NULL, db_name, user, password, dbhost, + dbport, NULL, dbsslkey, dbsslcert, dbsslca, dbsslcapath, dbsslcipher, false, false); if (!db || !db_open_database(NULL, db)) { Emsg1(M_FATAL, 0, "%s", db_strerror(db)); return 1; } - /* - * Drop temporary index idx_tmp_name if it already exists - */ + /* Drop temporary index idx_tmp_name if it already exists */ drop_tmp_idx("idxPIchk", "File"); if (batch) { @@ -323,9 +326,7 @@ int main (int argc, char *argv[]) do_interactive_mode(); } - /* - * Drop temporary index idx_tmp_name - */ + /* Drop temporary index idx_tmp_name */ drop_tmp_idx("idxPIchk", "File"); if (db) db_close_database(NULL, db); @@ -335,28 +336,6 @@ int main (int argc, char *argv[]) return 0; } -static void print_catalog_details(CAT *catalog, const char *working_dir) -{ - POOLMEM *catalog_details = get_pool_memory(PM_MESSAGE); - - /* - * Instantiate a BDB class and see what db_type gets assigned to it. - */ - db = db_init_database(NULL, catalog->db_driver, catalog->db_name, catalog->db_user, - catalog->db_password, catalog->db_address, - catalog->db_port, catalog->db_socket, - catalog->db_ssl_key, catalog->db_ssl_cert, catalog->db_ssl_ca, - catalog->db_ssl_capath, catalog->db_ssl_cipher, - catalog->mult_db_connections, - catalog->disable_batch_insert); - if (db) { - printf("%sdb_type=%s\nworking_dir=%s\n", catalog->display(catalog_details), - db_get_engine_name(db), working_directory); - db_close_database(NULL, db); - } - free_pool_memory(catalog_details); -} - static void do_interactive_mode() { const char *cmd; @@ -504,10 +483,9 @@ static int print_name_handler(void *ctx, int num_fields, char **row) static int get_name_handler(void *ctx, int num_fields, char **row) { - POOLMEM *name = (POOLMEM *)ctx; - + POOLMEM *buf = (POOLMEM *)ctx; if (row[0]) { - pm_strcpy(&name, row[0]); + pm_strcpy(&buf, row[0]); } return 0; } @@ -669,9 +647,7 @@ static void eliminate_duplicate_filenames() printf(_("Checking for duplicate Filename entries.\n")); - /* - * Make list of duplicated names - */ + /* Make list of duplicated names */ query = "SELECT Name, count(Name) as Count FROM Filename GROUP BY Name " "HAVING count(Name) > 1"; @@ -686,13 +662,9 @@ static void eliminate_duplicate_filenames() return; } if (fix) { - /* - * Loop through list of duplicate names - */ + /* Loop through list of duplicate names */ for (int i=0; i 1) { @@ -704,9 +676,7 @@ static void eliminate_duplicate_filenames() if (verbose) { printf(_("Found %d for: %s\n"), id_list.num_ids, name_list.name[i]); } - /* - * Force all records to use the first id then delete the other ids - */ + /* Force all records to use the first id then delete the other ids */ for (int j=1; j 1"; @@ -751,13 +719,9 @@ static void eliminate_duplicate_paths() return; } if (fix) { - /* - * Loop through list of duplicate names - */ + /* Loop through list of duplicate names */ for (int i=0; i 1) { @@ -769,9 +733,7 @@ static void eliminate_duplicate_paths() if (verbose) { printf(_("Found %d for: %s\n"), id_list.num_ids, name_list.name[i]); } - /* - * Force all records to use the first id then delete the other ids - */ + /* Force all records to use the first id then delete the other ids */ for (int j=1; j 0 && IsPathSeparator(name[len-1]); len--) { } if (len == 0) { @@ -1257,7 +1195,7 @@ static void repair_bad_filenames() } db_sql_query(db, buf, NULL, NULL); } - free_pool_memory(name); + free_pool_memory(name); } } @@ -1301,15 +1239,11 @@ static void repair_bad_paths() if (!db_sql_query(db, buf, get_name_handler, name)) { printf("%s\n", db_strerror(db)); } - /* - * Strip trailing blanks - */ + /* Strip trailing blanks */ for (len=strlen(name); len > 0 && name[len-1]==' '; len--) { name[len-1] = 0; } - /* - * Add trailing slash - */ + /* Add trailing slash */ len = pm_strcat(&name, "/"); db_escape_string(NULL, db, esc_name, name, len); bsnprintf(buf, sizeof(buf), "UPDATE Path SET Path='%s' WHERE PathId=%s", @@ -1319,7 +1253,7 @@ static void repair_bad_paths() } db_sql_query(db, buf, NULL, NULL); } - free_pool_memory(name); + free_pool_memory(name); } } @@ -1367,19 +1301,19 @@ typedef struct s_idx_list { static IDX_LIST idx_list[MAXIDX]; -/* +/* * Called here with each table index to be added to the list */ static int check_idx_handler(void *ctx, int num_fields, char **row) { - /* - * Table | Non_unique | Key_name | Seq_in_index | Column_name |... - * File | 0 | PRIMARY | 1 | FileId |... - */ + /* + * Table | Non_unique | Key_name | Seq_in_index | Column_name |... + * File | 0 | PRIMARY | 1 | FileId |... + */ char *name, *key_name, *col_name; int i, len; int found = false; - + name = (char *)ctx; key_name = row[2]; col_name = row[4]; @@ -1393,9 +1327,7 @@ static int check_idx_handler(void *ctx, int num_fields, char **row) break; } } - /* - * If the new Key_name, add it to the list - */ + /* If the new Key_name, add it to the list */ if (!found) { len = strlen(key_name) + 1; idx_list[i].key_name = (char *)malloc(len); @@ -1419,45 +1351,42 @@ static bool check_idx(const char *col_name) int found = false; const char *query = "SHOW INDEX FROM File"; - switch (db_get_type_index(db)) { - case SQL_TYPE_MYSQL: - memset(&idx_list, 0, sizeof(idx_list)); - if (!db_sql_query(db, query, check_idx_handler, (void *)col_name)) { - printf("%s\n", db_strerror(db)); - } - for (i = 0; (idx_list[i].key_name != NULL) && (i < MAXIDX) ; i++) { - /* - * NOTE : if (idx_list[i].count_key > 1) then index idx_list[i].key_name is "multiple-column" index - */ - if ((idx_list[i].count_key == 1) && (idx_list[i].count_col == 1)) { - /* - * "one column" index over *col_name found - */ - found = true; - } + if (db_get_type_index(db) != SQL_TYPE_MYSQL) { + return true; + } + /* Continue for MySQL */ + memset(&idx_list, 0, sizeof(idx_list)); + if (!db_sql_query(db, query, check_idx_handler, (void *)col_name)) { + printf("%s\n", db_strerror(db)); + } + for (i = 0; (idx_list[i].key_name != NULL) && (i < MAXIDX) ; i++) { + /* + * NOTE : if (idx_list[i].count_key > 1) then index idx_list[i].key_name is "multiple-column" index + */ + if ((idx_list[i].count_key == 1) && (idx_list[i].count_col == 1)) { + /* "one column" index over *col_name found */ + found = true; } - if (found) { - if (verbose) { - printf(_("Ok. Index over the %s column already exists and dbcheck will work faster.\n"), col_name); - } - } else { - printf(_("Note. Index over the %s column not found, that can greatly slow down dbcheck.\n"), col_name); + } + if (found) { + if (verbose) { + printf(_("Ok. Index over the %s column already exists and dbcheck will work faster.\n"), col_name); } - return found; - default: - return true; + } else { + printf(_("Note. Index over the %s column not found, that can greatly slow down dbcheck.\n"), col_name); } + return found; } /* * Create temporary one-column index */ -static bool create_tmp_idx(const char *idx_name, const char *table_name, - const char *col_name) +static bool create_tmp_idx(const char *idx_name, const char *table_name, + const char *col_name) { idx_tmp_name = NULL; printf(_("Create temporary index... This may take some time!\n")); - bsnprintf(buf, sizeof(buf), "CREATE INDEX %s ON %s (%s)", idx_name, table_name, col_name); + bsnprintf(buf, sizeof(buf), "CREATE INDEX %s ON %s (%s)", idx_name, table_name, col_name); if (verbose) { printf("%s\n", buf); } -- 2.39.2