From aed6c4ab14de6118cb73a85e06fb6ebabff1094e Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sun, 18 Mar 2007 21:36:28 +0000 Subject: [PATCH] kes Correct return status on db_batch... routines. kes Add call to db_write_batch_file_records() to migration code. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4361 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/cats/cats.h | 12 ---- bacula/src/cats/protos.h | 10 ++-- bacula/src/cats/sql.c | 22 ++++---- bacula/src/cats/sql_create.c | 105 +++++++++++++++++++++-------------- bacula/src/dird/backup.c | 4 +- bacula/src/dird/migrate.c | 2 +- bacula/technotes-2.1 | 4 +- 7 files changed, 83 insertions(+), 76 deletions(-) diff --git a/bacula/src/cats/cats.h b/bacula/src/cats/cats.h index fa67e07cc0..85a1a29103 100644 --- a/bacula/src/cats/cats.h +++ b/bacula/src/cats/cats.h @@ -171,9 +171,6 @@ struct B_DB { #define sql_fetch_field(x) my_sqlite_fetch_field(x) #define sql_num_fields(x) ((x)->ncolumn) #define SQL_ROW char** -#define sql_batch_start(x) db_batch_start(x) -#define sql_batch_end(x,y) db_batch_end(x,y) -#define sql_batch_insert(x,y) db_batch_insert(x,y) #define sql_batch_lock_path_query my_sqlite_batch_lock_query #define sql_batch_lock_filename_query my_sqlite_batch_lock_query #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query @@ -301,9 +298,6 @@ struct B_DB { #define sql_fetch_field(x) my_sqlite_fetch_field(x) #define sql_num_fields(x) ((x)->ncolumn) #define SQL_ROW char** -#define sql_batch_start(x) db_batch_start(x) -#define sql_batch_end(x,y) db_batch_end(x,y) -#define sql_batch_insert(x,y) db_batch_insert(x,y) #define sql_batch_lock_path_query my_sqlite_batch_lock_query #define sql_batch_lock_filename_query my_sqlite_batch_lock_query #define sql_batch_unlock_tables_query my_sqlite_batch_unlock_query @@ -386,9 +380,6 @@ struct B_DB { #define sql_fetch_field(x) mysql_fetch_field((x)->result) #define sql_num_fields(x) (int)mysql_num_fields((x)->result) #define sql_batch_start(x) db_batch_start(x) -#define sql_batch_end(x,y) db_batch_end(x,y) -#define sql_batch_insert(x,y) db_batch_insert(x,y) -#define sql_batch_lock_path_query my_mysql_batch_lock_path_query #define sql_batch_lock_filename_query my_mysql_batch_lock_filename_query #define sql_batch_unlock_tables_query my_mysql_batch_unlock_tables_query #define sql_batch_fill_filename_query my_mysql_batch_fill_filename_query @@ -504,9 +495,6 @@ extern char* my_pg_batch_fill_path_query; #define sql_field_seek(x, y) my_postgresql_field_seek((x), (y)) #define sql_fetch_field(x) my_postgresql_fetch_field(x) #define sql_num_fields(x) ((x)->num_fields) -#define sql_batch_start(x) my_postgresql_batch_start(x) -#define sql_batch_end(x,y) my_postgresql_batch_end(x,y) -#define sql_batch_insert(x,y) my_postgresql_batch_insert(x,y) #define sql_lock_table(x,y) my_postgresql_lock_table(x, y) #define sql_unlock_table(x,y) my_postgresql_unlock_table(x) #define sql_batch_lock_path_query my_pg_batch_lock_path_query diff --git a/bacula/src/cats/protos.h b/bacula/src/cats/protos.h index 4b29aed94d..ef11adddcb 100644 --- a/bacula/src/cats/protos.h +++ b/bacula/src/cats/protos.h @@ -56,7 +56,7 @@ int db_int64_handler(void *ctx, int num_fields, char **row); /* create.c */ -int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar); +bool db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar); bool db_create_job_record(JCR *jcr, B_DB *db, JOB_DBR *jr); int db_create_media_record(JCR *jcr, B_DB *db, MEDIA_DBR *media_dbr); int db_create_client_record(JCR *jcr, B_DB *db, CLIENT_DBR *cr); @@ -67,10 +67,10 @@ int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr); bool db_create_device_record(JCR *jcr, B_DB *mdb, DEVICE_DBR *dr); bool db_create_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr); bool db_create_mediatype_record(JCR *jcr, B_DB *mdb, MEDIATYPE_DBR *mr); -int db_create_batch_file_record(JCR *jcr); -int db_batch_start(B_DB *mdb); -int db_batch_end(B_DB *mdb, const char *error); -int db_batch_insert(B_DB *mdb, ATTR_DBR *ar); +bool db_write_batch_file_records(JCR *jcr); +bool db_batch_start(B_DB *mdb); +bool db_batch_end(B_DB *mdb, const char *error); +bool db_batch_insert(B_DB *mdb, ATTR_DBR *ar); /* delete.c */ int db_delete_pool_record(JCR *jcr, B_DB *db, POOL_DBR *pool_dbr); diff --git a/bacula/src/cats/sql.c b/bacula/src/cats/sql.c index 3b2fef0b3f..79296d5d00 100644 --- a/bacula/src/cats/sql.c +++ b/bacula/src/cats/sql.c @@ -1,17 +1,7 @@ -/* - * Bacula Catalog Database interface routines - * - * Almost generic set of SQL database interface routines - * (with a little more work) - * - * Kern Sibbald, March 2000 - * - * Version $Id$ - */ /* Bacula® - The Network Backup Solution - Copyright (C) 2000-2006 Free Software Foundation Europe e.V. + Copyright (C) 2000-2007 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -35,6 +25,16 @@ (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, Switzerland, email:ftf@fsfeurope.org. */ +/* + * Bacula Catalog Database interface routines + * + * Almost generic set of SQL database interface routines + * (with a little more work) + * + * Kern Sibbald, March 2000 + * + * Version $Id$ + */ /* The following is necessary so that we do not include * the dummy external definition of B_DB. diff --git a/bacula/src/cats/sql_create.c b/bacula/src/cats/sql_create.c index 282aede043..e833715975 100644 --- a/bacula/src/cats/sql_create.c +++ b/bacula/src/cats/sql_create.c @@ -680,7 +680,11 @@ bool db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr) * - then insert the join between the temp, filename and path tables into file. */ -int db_batch_start(B_DB *mdb) +/* + * Returns 1 if OK + * 0 if failed + */ +bool db_batch_start(B_DB *mdb) { return sql_query(mdb, " CREATE TEMPORARY TABLE batch " @@ -689,10 +693,14 @@ int db_batch_start(B_DB *mdb) " path blob, " " name blob, " " lstat tinyblob, " - " md5 tinyblob) "); + " md5 tinyblob) ") == 0; } -int db_batch_insert(B_DB *mdb, ATTR_DBR *ar) +/* + * Returns 1 if OK + * 0 if failed + */ +bool db_batch_insert(B_DB *mdb, ATTR_DBR *ar) { size_t len; char *digest; @@ -714,75 +722,79 @@ int db_batch_insert(B_DB *mdb, ATTR_DBR *ar) ar->FileIndex, edit_int64(ar->JobId,ed1), mdb->path, mdb->fname, ar->attr, digest); - sql_query(mdb, mdb->cmd); - - return mdb->status; + return sql_query(mdb, mdb->cmd) == 0; } /* set error to something to abort operation */ -int db_batch_end(B_DB *mdb, const char *error) +/* + * Returns 1 if OK + * 0 if failed + */ +bool db_batch_end(B_DB *mdb, const char *error) { Dmsg0(50, "db_batch_end started"); if (mdb) { mdb->status = 0; - return mdb->status; } - return 0; + return true; } -int db_create_batch_file_record(JCR *jcr) +/* + * Returns 1 if OK + * 0 if failed + */ +bool db_write_batch_file_records(JCR *jcr) { Dmsg0(50,"db_create_file_record : no files"); if (!jcr->db_batch) { /* no files to backup ? */ Dmsg0(50,"db_create_file_record : no files\n"); - return 0; + return true; } - if (sql_batch_end(jcr->db_batch, NULL)) { + if (!db_batch_end(jcr->db_batch, NULL)) { Jmsg(jcr, M_FATAL, 0, "Bad batch end %s\n", jcr->db_batch->errmsg); - return 1; + return false; + } + + if (job_canceled(jcr)) { + return false; } /* we have to lock tables */ - if (sql_query(jcr->db_batch, sql_batch_lock_path_query)) - { + if (sql_query(jcr->db_batch, sql_batch_lock_path_query) != 0) { Jmsg(jcr, M_FATAL, 0, "Can't lock Path table %s\n", jcr->db_batch->errmsg); - return 1; + return false; } - if (sql_query(jcr->db_batch, sql_batch_fill_path_query)) - { + if (sql_query(jcr->db_batch, sql_batch_fill_path_query) != 0) { Jmsg(jcr, M_FATAL, 0, "Can't fill Path table %s\n",jcr->db_batch->errmsg); sql_query(jcr->db_batch, sql_batch_unlock_tables_query); - return 1; + return false; } - if (sql_query(jcr->db_batch, sql_batch_unlock_tables_query)) - { + if (sql_query(jcr->db_batch, sql_batch_unlock_tables_query) !=0) { Jmsg(jcr, M_FATAL, 0, "Can't unlock Path table %s\n", jcr->db_batch->errmsg); - return 1; + return false; } /* we have to lock tables */ - if (sql_query(jcr->db_batch, sql_batch_lock_filename_query)) - { + if (sql_query(jcr->db_batch, sql_batch_lock_filename_query) != 0) { Jmsg(jcr, M_FATAL, 0, "Can't lock Filename table %s\n", jcr->db_batch->errmsg); - return 1; + return false; } - if (sql_query(jcr->db_batch, sql_batch_fill_filename_query)) - { + if (sql_query(jcr->db_batch, sql_batch_fill_filename_query) != 0) { Jmsg(jcr,M_FATAL,0,"Can't fill Filename table %s\n",jcr->db_batch->errmsg); sql_query(jcr->db_batch, sql_batch_unlock_tables_query); - return 1; + return false; } - if (sql_query(jcr->db_batch, sql_batch_unlock_tables_query)) { + if (sql_query(jcr->db_batch, sql_batch_unlock_tables_query) != 0) { Jmsg(jcr, M_FATAL, 0, "Can't unlock Filename table %s\n", jcr->db_batch->errmsg); - return 1; + return false; } if (sql_query(jcr->db_batch, @@ -791,15 +803,15 @@ int db_create_batch_file_record(JCR *jcr) " Filename.FilenameId,batch.LStat, batch.MD5 " " FROM batch " " JOIN Path ON (batch.Path = Path.Path) " - " JOIN Filename ON (batch.Name = Filename.Name) ")) + " JOIN Filename ON (batch.Name = Filename.Name) ") != 0) { Jmsg(jcr, M_FATAL, 0, "Can't fill File table %s\n", jcr->db_batch->errmsg); - return 1; + return false; } sql_query(jcr->db_batch, "DROP TABLE batch"); - return 0; + return true; } #ifdef HAVE_BATCH_FILE_INSERT @@ -812,7 +824,7 @@ int db_create_batch_file_record(JCR *jcr) * how many times it occurs. This is this subroutine, we separate * the file and the path and fill temporary tables with this three records. */ -int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) +bool db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) { Dmsg1(dbglevel, "Fname=%s\n", ar->fname); Dmsg0(dbglevel, "put_file_into_catalog\n"); @@ -833,10 +845,13 @@ int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) if (jcr->db_batch) { Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db_batch)); } - return 0; + return false; } - sql_batch_start(jcr->db_batch); + if (!db_batch_start(jcr->db_batch)) { + Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db_batch)); + return false; + } } B_DB *bdb = jcr->db_batch; @@ -857,13 +872,13 @@ int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) /* if (jcr->changes > 100000) { - sql_batch_end(bdb, NULL); - sql_batch_start(bdb); + db_batch_end(bdb, NULL); + db_batch_start(bdb); jcr->changes = 0; } */ - return (sql_batch_insert(bdb, ar) == 0); + return db_batch_insert(bdb, ar); } #else /* ! HAVE_BATCH_FILE_INSERT */ @@ -877,9 +892,8 @@ int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) * how many times it occurs. This is this subroutine, we separate * the file and the path and create three database records. */ -int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) +bool db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) { - db_lock(mdb); Dmsg1(dbglevel, "Fname=%s\n", ar->fname); Dmsg0(dbglevel, "put_file_into_catalog\n"); @@ -916,11 +930,11 @@ int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) Dmsg3(dbglevel, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId); db_unlock(mdb); - return 1; + return true; bail_out: db_unlock(mdb); - return 0; + return false; } @@ -1081,6 +1095,11 @@ static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) return ar->FilenameId > 0; } +bool db_write_batch_file_records(JCR *jcr) +{ + return true; +} + #endif /* ! HAVE_BATCH_FILE_INSERT */ #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */ diff --git a/bacula/src/dird/backup.c b/bacula/src/dird/backup.c index 65d96641bc..9ff2db7c6c 100644 --- a/bacula/src/dird/backup.c +++ b/bacula/src/dird/backup.c @@ -233,9 +233,7 @@ bool do_backup(JCR *jcr) /* Pickup Job termination data */ stat = wait_for_job_termination(jcr); -#ifdef HAVE_BATCH_FILE_INSERT - db_create_batch_file_record(jcr); /* used by bulk batch file insert */ -#endif + db_write_batch_file_records(jcr); /* used by bulk batch file insert */ if (stat == JS_Terminated) { backup_cleanup(jcr, stat); return true; diff --git a/bacula/src/dird/migrate.c b/bacula/src/dird/migrate.c index f6b369d5a4..33a744488a 100644 --- a/bacula/src/dird/migrate.c +++ b/bacula/src/dird/migrate.c @@ -377,8 +377,8 @@ bool do_migration(JCR *jcr) /* Pickup Job termination data */ /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */ wait_for_storage_daemon_termination(jcr); - set_jcr_job_status(jcr, jcr->SDJobStatus); + db_write_batch_file_records(jcr); /* used by bulk batch file insert */ if (jcr->JobStatus != JS_Terminated) { return false; } diff --git a/bacula/technotes-2.1 b/bacula/technotes-2.1 index e8495420d9..001ac7f802 100644 --- a/bacula/technotes-2.1 +++ b/bacula/technotes-2.1 @@ -2,7 +2,9 @@ General: 18Mar07 -ebl Fix AfterJob timming +kes Correct return status on db_batch... routines. +kes Add call to db_write_batch_file_records() to migration code. +ebl Fix AfterJob timing kes Move the checking of the database in initializion of the Director to after we drop privileges. This avoids the need for both root and bacula access to the DB. -- 2.39.5