]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/bdb.c
Fix Win32 build with new .def files and DLL_IMP_EXP ...
[bacula/bacula] / bacula / src / cats / bdb.c
index 4a68ac03aec7e1c38a7c29f2b49f335e467c5e38..2a262f807577e653a841a6636b1f847e5bfccfc5 100644 (file)
@@ -5,48 +5,52 @@
  *  The purpose of these routines is to ensure that Bacula
  *  can limp along if no real database is loaded on the
  *  system.
- *   
- *    Kern Sibbald, January MMI 
+ *
+ *    Kern Sibbald, January MMI
  *
  *    Version $Id$
  *
  */
-
-
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Bacula® - The Network Backup Solution
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
+   Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
+   You should have received a copy of the GNU 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.
 
- */
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 
 
 /* The following is necessary so that we do not include
  * the dummy external definition of DB.
  */
-#define __SQL_C                      /* indicate that this is sql.c */
+#define __SQL_C                       /* indicate that this is sql.c */
 
 #include "bacula.h"
 #include "cats.h"
 
 #ifdef HAVE_BACULA_DB
 
-/* Forward referenced functions */
-
-extern char *working_directory;
+uint32_t bacula_db_version = 0;
 
 /* List of open databases */
 static BQUEUE db_list = {&db_list, &db_list};
@@ -68,18 +72,32 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 #define DB_CLIENT_FILENAME   "client.db"
 #define DB_FILESET_FILENAME  "fileset.db"
 
+dbid_list::dbid_list() 
+{
+   memset(this, 0, sizeof(dbid_list));
+   max_ids = 1000;
+   DBId = (DBId_t *)malloc(max_ids * sizeof(DBId_t));
+   num_ids = num_seen = tot_ids = 0;
+   PurgedFiles = NULL;
+}
+
+dbid_list::~dbid_list() 
+{ 
+   free(DBId);
+}
+
 static POOLMEM *make_filename(B_DB *mdb, char *name)
 {
    char sep;
    POOLMEM *dbf;
 
    dbf = get_pool_memory(PM_FNAME);
-   if (working_directory[strlen(working_directory)-1] == '/') {
+   if (IsPathSeparator(working_directory[strlen(working_directory)-1])) {
       sep = 0;
    } else {
-      sep = '/'; 
+      sep = '/';
    }
-   Mmsg(&dbf, "%s%c%s-%s", working_directory, sep, mdb->db_name, name);
+   Mmsg(dbf, "%s%c%s-%s", working_directory, sep, mdb->db_name, name);
    return dbf;
 }
 
@@ -95,25 +113,36 @@ int bdb_write_control_file(B_DB *mdb)
    return 1;
 }
 
+/*
+ * Retrieve database type
+ */
+const char *
+db_get_type(void)
+{
+   return "Internal";
+}
+
 /*
  * Initialize database data structure. In principal this should
  * never have errors, or it is really fatal.
  */
 B_DB *
-db_init_database(void *jcr, char *db_name, char *db_user, char *db_password,
-                char *db_address, int db_port, char *db_socket)
+db_init_database(JCR *jcr, char const *db_name, char const *db_user, char const *db_password,
+                 char const *db_address, int db_port, char const *db_socket,
+                 int mult_db_connections)
 {
    B_DB *mdb;
-   P(mutex);                         /* lock DB queue */
+   P(mutex);                          /* lock DB queue */
    /* Look to see if DB already open */
    for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
       if (strcmp(mdb->db_name, db_name) == 0) {
          Dmsg2(200, "DB REopen %d %s\n", mdb->ref_count, db_name);
-        mdb->ref_count++;
-        V(mutex);
-        return mdb;                  /* already open */
+         mdb->ref_count++;
+         V(mutex);
+         return mdb;                  /* already open */
       }
    }
+
    Dmsg0(200, "db_open first time\n");
    mdb = (B_DB *)malloc(sizeof(B_DB));
    memset(mdb, 0, sizeof(B_DB));
@@ -129,8 +158,9 @@ db_init_database(void *jcr, char *db_name, char *db_user, char *db_password,
    Dmsg0(200, "Done db_open_database()\n");
    mdb->cfd = -1;
    V(mutex);
-   Jmsg(jcr, M_WARNING, 0, _("WARNING!!!! The Internal Database is for TESTING ONLY!\n"));
-   Jmsg(jcr, M_WARNING, 0, _("You should use either SQLite or MySQL\n"));
+   Jmsg(jcr, M_WARNING, 0, _("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"));
+   Jmsg(jcr, M_WARNING, 0, _("WARNING!!!! The Internal Database is NOT OPERATIONAL!\n"));
+   Jmsg(jcr, M_WARNING, 0, _("You should use SQLite, PostgreSQL, or MySQL\n"));
 
    return mdb;
 }
@@ -140,7 +170,7 @@ db_init_database(void *jcr, char *db_name, char *db_user, char *db_password,
  * which are returned in the errmsg
  */
 int
-db_open_database(void *jcr, B_DB *mdb)
+db_open_database(JCR *jcr, B_DB *mdb)
 {
    char *dbf;
    int fd, badctl;
@@ -159,18 +189,18 @@ db_open_database(void *jcr, B_DB *mdb)
 
    Dmsg0(200, "make_filename\n");
    dbf = make_filename(mdb, DB_CONTROL_FILENAME);
-   mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600); 
+   mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600);
    free_memory(dbf);
    if (mdb->cfd < 0) {
-      Mmsg2(&mdb->errmsg, _("Unable to open Catalog DB control file %s: ERR=%s\n"), 
-        dbf, strerror(errno));
+      Mmsg2(&mdb->errmsg, _("Unable to open Catalog DB control file %s: ERR=%s\n"),
+         dbf, strerror(errno));
       V(mutex);
       return 0;
    }
    Dmsg0(200, "DB open\n");
    /* See if the file was previously written */
    filend = lseek(mdb->cfd, 0, SEEK_END);
-   if (filend == 0) {                /* No, initialize everything */
+   if (filend == 0) {                 /* No, initialize everything */
       Dmsg0(200, "Init DB files\n");
       memset(&mdb->control, 0, sizeof(mdb->control));
       mdb->control.bdb_version = BDB_VERSION;
@@ -220,12 +250,13 @@ db_open_database(void *jcr, B_DB *mdb)
       Mmsg1(&mdb->errmsg, _("Error reading catalog DB control file. ERR=%s\n"), strerror(errno));
       badctl = 1;
    } else if (mdb->control.bdb_version != BDB_VERSION) {
-      Mmsg2(&mdb->errmsg, _("Error, catalog DB control file wrong version. \
-Wanted %d, got %d\n\
-Please reinitialize the working directory.\n"), 
-        BDB_VERSION, mdb->control.bdb_version);
+      Mmsg2(&mdb->errmsg, _("Error, catalog DB control file wrong version. "
+"Wanted %d, got %d\n"
+"Please reinitialize the working directory.\n"),
+         BDB_VERSION, mdb->control.bdb_version);
       badctl = 1;
    }
+   bacula_db_version = mdb->control.bdb_version;
    if (badctl) {
       V(mutex);
       return 0;
@@ -234,7 +265,7 @@ Please reinitialize the working directory.\n"),
    return 1;
 }
 
-void db_close_database(void *jcr, B_DB *mdb)           
+void db_close_database(JCR *jcr, B_DB *mdb)
 {
    P(mutex);
    mdb->ref_count--;
@@ -242,29 +273,28 @@ void db_close_database(void *jcr, B_DB *mdb)
       qdchain(&mdb->bq);
       /*  close file descriptors */
       if (mdb->cfd >= 0) {
-        close(mdb->cfd);
+         close(mdb->cfd);
       }
       free(mdb->db_name);
       if (mdb->jobfd) {
-        fclose(mdb->jobfd);
+         fclose(mdb->jobfd);
       }
       if (mdb->poolfd) {
-        fclose(mdb->poolfd);
+         fclose(mdb->poolfd);
       }
       if (mdb->mediafd) {
-        fclose(mdb->mediafd);
+         fclose(mdb->mediafd);
       }
       if (mdb->jobmediafd) {
-        fclose(mdb->jobmediafd);
+         fclose(mdb->jobmediafd);
       }
       if (mdb->clientfd) {
-        fclose(mdb->clientfd);
+         fclose(mdb->clientfd);
       }
       if (mdb->filesetfd) {
-        fclose(mdb->filesetfd);
+         fclose(mdb->filesetfd);
       }
-/*    pthread_mutex_destroy(&mdb->mutex); */
-      rwl_destroy(&mdb->lock);      
+      rwl_destroy(&mdb->lock);
       free_pool_memory(mdb->errmsg);
       free_pool_memory(mdb->cmd);
       free_pool_memory(mdb->cached_path);
@@ -273,10 +303,14 @@ void db_close_database(void *jcr, B_DB *mdb)
    V(mutex);
 }
 
+void db_thread_cleanup()
+{ }
+
 
-void db_escape_string(char *snew, char *old, int len)
+void db_escape_string(JCR *jcr, B_DB *db, char *snew, char *old, int len)
 {
-   strcpy(snew, old);
+   memset(snew, 0, len);
+   bstrncpy(snew, old, len);
 }
 
 char *db_strerror(B_DB *mdb)
@@ -284,9 +318,9 @@ char *db_strerror(B_DB *mdb)
    return mdb->errmsg;
 }
 
-int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
+bool db_sql_query(B_DB *mdb, char const *query, DB_RESULT_HANDLER *result_handler, void *ctx)
 {
-   return 1;
+   return true;
 }
 
 /*
@@ -296,15 +330,15 @@ int bdb_open_jobs_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->jobfd) {  
+   if (!mdb->jobfd) {
       dbf = make_filename(mdb, DB_JOBS_FILENAME);
-      mdb->jobfd = fopen(dbf, "r+");
+      mdb->jobfd = fopen(dbf, "r+b");
       if (!mdb->jobfd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        Emsg0(M_FATAL, 0, mdb->errmsg);
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         Emsg0(M_FATAL, 0, mdb->errmsg);
+         free_memory(dbf);
+         return 0;
       }
       free_memory(dbf);
    }
@@ -318,15 +352,15 @@ int bdb_open_jobmedia_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->jobmediafd) {  
+   if (!mdb->jobmediafd) {
       dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
-      mdb->jobmediafd = fopen(dbf, "r+");
+      mdb->jobmediafd = fopen(dbf, "r+b");
       if (!mdb->jobmediafd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        Emsg0(M_FATAL, 0, mdb->errmsg);
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         Emsg0(M_FATAL, 0, mdb->errmsg);
+         free_memory(dbf);
+         return 0;
       }
       free_memory(dbf);
    }
@@ -341,15 +375,15 @@ int bdb_open_pools_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->poolfd) { 
+   if (!mdb->poolfd) {
       dbf = make_filename(mdb, DB_POOLS_FILENAME);
-      mdb->poolfd = fopen(dbf, "r+");
+      mdb->poolfd = fopen(dbf, "r+b");
       if (!mdb->poolfd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        Emsg0(M_FATAL, 0, mdb->errmsg);
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         Emsg0(M_FATAL, 0, mdb->errmsg);
+         free_memory(dbf);
+         return 0;
       }
       Dmsg1(200, "Opened pool file %s\n", dbf);
       free_memory(dbf);
@@ -364,15 +398,15 @@ int bdb_open_client_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->clientfd) {  
+   if (!mdb->clientfd) {
       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
-      mdb->clientfd = fopen(dbf, "r+");
+      mdb->clientfd = fopen(dbf, "r+b");
       if (!mdb->clientfd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        Emsg0(M_FATAL, 0, mdb->errmsg);
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         Emsg0(M_FATAL, 0, mdb->errmsg);
+         free_memory(dbf);
+         return 0;
       }
       free_memory(dbf);
    }
@@ -386,15 +420,15 @@ int bdb_open_fileset_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->filesetfd) {  
+   if (!mdb->filesetfd) {
       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
-      mdb->filesetfd = fopen(dbf, "r+");
+      mdb->filesetfd = fopen(dbf, "r+b");
       if (!mdb->filesetfd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        Emsg0(M_FATAL, 0, mdb->errmsg);
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         Emsg0(M_FATAL, 0, mdb->errmsg);
+         free_memory(dbf);
+         return 0;
       }
       free_memory(dbf);
    }
@@ -410,14 +444,14 @@ int bdb_open_media_file(B_DB *mdb)
 {
    char *dbf;
 
-   if (!mdb->mediafd) {  
+   if (!mdb->mediafd) {
       dbf = make_filename(mdb, DB_MEDIA_FILENAME);
-      mdb->mediafd = fopen(dbf, "r+");
+      mdb->mediafd = fopen(dbf, "r+b");
       if (!mdb->mediafd) {
-         Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n", 
-           dbf, strerror(errno));
-        free_memory(dbf);
-        return 0;
+         Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n",
+            dbf, strerror(errno));
+         free_memory(dbf);
+         return 0;
       }
       free_memory(dbf);
    }
@@ -425,36 +459,47 @@ int bdb_open_media_file(B_DB *mdb)
 }
 
 
-void _db_lock(char *file, int line, B_DB *mdb)
+void _db_lock(const char *file, int line, B_DB *mdb)
 {
    int errstat;
    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
-          strerror(errstat));
+           strerror(errstat));
    }
-}    
+}
 
-void _db_unlock(char *file, int line, B_DB *mdb)
+void _db_unlock(const char *file, int line, B_DB *mdb)
 {
    int errstat;
    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
-          strerror(errstat));
+           strerror(errstat));
    }
-}    
+}
 
 /*
  * Start a transaction. This groups inserts and makes things
- *  much more efficient. Usually started when inserting 
+ *  much more efficient. Usually started when inserting
  *  file attributes.
  */
-void db_start_transaction(void *jcr, B_DB *mdb)
+void db_start_transaction(JCR *jcr, B_DB *mdb)
 {
 }
 
-void db_end_transaction(void *jcr, B_DB *mdb)
+void db_end_transaction(JCR *jcr, B_DB *mdb)
 {
 }
 
+bool db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
+{ return true; }
+
+void
+db_list_pool_records(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr, 
+                     DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
+{ }
+
+int db_int64_handler(void *ctx, int num_fields, char **row)
+{ return 0; }
+
 
 #endif /* HAVE_BACULA_DB */