+/*
+ * Lock database, this can be called multiple times by the same
+ * thread without blocking, but must be unlocked the number of
+ * times it was locked.
+ */
+void _db_lock(const char *file, int line, B_DB *mdb)
+{
+ int errstat;
+ if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
+ berrno be;
+ e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+/*
+ * Unlock the database. This can be called multiple times by the
+ * same thread up to the number of times that thread called
+ * db_lock()/
+ */
+void _db_unlock(const char *file, int line, B_DB *mdb)
+{
+ int errstat;
+ if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
+ berrno be;
+ e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+/*
+ * Start a transaction. This groups inserts and makes things
+ * much more efficient. Usually started when inserting
+ * file attributes.
+ */
+void db_start_transaction(JCR *jcr, B_DB *mdb)
+{
+ if (!jcr->attr) {
+ jcr->attr = get_pool_memory(PM_FNAME);
+ }
+ if (!jcr->ar) {
+ jcr->ar = (ATTR_DBR *)malloc(sizeof(ATTR_DBR));
+ }
+
+#ifdef HAVE_SQLITE
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ /* Allow only 10,000 changes per transaction */
+ if (mdb->transaction && mdb->changes > 10000) {
+ db_end_transaction(jcr, mdb);
+ }
+ if (!mdb->transaction) {
+ my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
+ Dmsg0(400, "Start SQLite transaction\n");
+ mdb->transaction = 1;
+ }
+ db_unlock(mdb);
+#endif
+
+/*
+ * This is turned off because transactions break
+ * if multiple simultaneous jobs are run.
+ */
+#ifdef HAVE_POSTGRESQL
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ /* Allow only 25,000 changes per transaction */
+ if (mdb->transaction && mdb->changes > 25000) {
+ db_end_transaction(jcr, mdb);
+ }
+ if (!mdb->transaction) {
+ db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
+ Dmsg0(400, "Start PosgreSQL transaction\n");
+ mdb->transaction = 1;
+ }
+ db_unlock(mdb);
+#endif
+
+#ifdef HAVE_DBI
+ if (db_type == SQL_TYPE_SQLITE) {
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ /* Allow only 10,000 changes per transaction */
+ if (mdb->transaction && mdb->changes > 10000) {
+ db_end_transaction(jcr, mdb);
+ }
+ if (!mdb->transaction) {
+ //my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
+ db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
+ Dmsg0(400, "Start SQLite transaction\n");
+ mdb->transaction = 1;
+ }
+ db_unlock(mdb);
+ } else if (db_type == SQL_TYPE_POSTGRESQL) {
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ /* Allow only 25,000 changes per transaction */
+ if (mdb->transaction && mdb->changes > 25000) {
+ db_end_transaction(jcr, mdb);
+ }
+ if (!mdb->transaction) {
+ db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
+ Dmsg0(400, "Start PosgreSQL transaction\n");
+ mdb->transaction = 1;
+ }
+ db_unlock(mdb);
+ }
+#endif
+}
+
+void db_end_transaction(JCR *jcr, B_DB *mdb)
+{
+ /*
+ * This can be called during thread cleanup and
+ * the db may already be closed. So simply return.
+ */
+ if (!mdb) {
+ return;
+ }
+
+ if (jcr && jcr->cached_attribute) {
+ Dmsg0(400, "Flush last cached attribute.\n");
+ if (!db_create_file_attributes_record(jcr, mdb, jcr->ar)) {
+ Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
+ }
+ jcr->cached_attribute = false;
+ }
+
+#ifdef HAVE_SQLITE
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ if (mdb->transaction) {
+ my_sqlite_query(mdb, "COMMIT"); /* end transaction */
+ mdb->transaction = 0;
+ Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
+ }
+ mdb->changes = 0;
+ db_unlock(mdb);
+#endif
+
+#ifdef HAVE_POSTGRESQL
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ if (mdb->transaction) {
+ db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
+ mdb->transaction = 0;
+ Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
+ }
+ mdb->changes = 0;
+ db_unlock(mdb);
+#endif
+
+#ifdef HAVE_DBI
+ if (db_type == SQL_TYPE_SQLITE) {
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ if (mdb->transaction) {
+ //my_sqlite_query(mdb, "COMMIT"); /* end transaction */
+ db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
+ mdb->transaction = 0;
+ Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
+ }
+ mdb->changes = 0;
+ db_unlock(mdb);
+ } else if (db_type == SQL_TYPE_POSTGRESQL) {
+ if (!mdb->allow_transactions) {
+ return;
+ }
+ db_lock(mdb);
+ if (mdb->transaction) {
+ db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
+ mdb->transaction = 0;
+ Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
+ }
+ mdb->changes = 0;
+ db_unlock(mdb);
+ }
+#endif
+}
+
+/*
+ * Given a full filename, split it into its path
+ * and filename parts. They are returned in pool memory
+ * in the mdb structure.
+ */
+void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname)
+{
+ const char *p, *f;
+
+ /* Find path without the filename.
+ * I.e. everything after the last / is a "filename".
+ * OK, maybe it is a directory name, but we treat it like
+ * a filename. If we don't find a / then the whole name
+ * must be a path name (e.g. c:).
+ */
+ for (p=f=fname; *p; p++) {
+ if (IsPathSeparator(*p)) {
+ f = p; /* set pos of last slash */
+ }
+ }
+ if (IsPathSeparator(*f)) { /* did we find a slash? */
+ f++; /* yes, point to filename */
+ } else { /* no, whole thing must be path name */
+ f = p;
+ }
+
+ /* If filename doesn't exist (i.e. root directory), we
+ * simply create a blank name consisting of a single
+ * space. This makes handling zero length filenames
+ * easier.
+ */
+ mdb->fnl = p - f;
+ if (mdb->fnl > 0) {
+ mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
+ memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
+ mdb->fname[mdb->fnl] = 0;
+ } else {
+ mdb->fname[0] = 0;
+ mdb->fnl = 0;
+ }
+
+ mdb->pnl = f - fname;
+ if (mdb->pnl > 0) {
+ mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
+ memcpy(mdb->path, fname, mdb->pnl);
+ mdb->path[mdb->pnl] = 0;
+ } else {
+ Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
+ Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
+ mdb->path[0] = 0;
+ mdb->pnl = 0;
+ }
+
+ Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
+}
+
+/*
+ * List dashes as part of header for listing SQL results in a table
+ */
+void
+list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
+{
+ SQL_FIELD *field;
+ int i, j;
+
+ sql_field_seek(mdb, 0);
+ send(ctx, "+");
+ for (i = 0; i < sql_num_fields(mdb); i++) {
+ field = sql_fetch_field(mdb);
+ if (!field) {
+ break;
+ }
+ for (j = 0; j < (int)field->max_length + 2; j++) {
+ send(ctx, "-");
+ }
+ send(ctx, "+");
+ }
+ send(ctx, "\n");
+}
+
+/*
+ * If full_list is set, we list vertically, otherwise, we
+ * list on one line horizontally.
+ */
+void
+list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
+{
+ SQL_FIELD *field;
+ SQL_ROW row;
+ int i, col_len, max_len = 0;
+ char buf[2000], ewc[30];
+
+ Dmsg0(800, "list_result starts\n");
+ if (mdb->result == NULL || sql_num_rows(mdb) == 0) {
+ send(ctx, _("No results to list.\n"));
+ return;
+ }
+
+ Dmsg1(800, "list_result starts looking at %d fields\n", sql_num_fields(mdb));
+ /* determine column display widths */
+ sql_field_seek(mdb, 0);
+ for (i = 0; i < sql_num_fields(mdb); i++) {
+ Dmsg1(800, "list_result processing field %d\n", i);
+ field = sql_fetch_field(mdb);
+ if (!field) {
+ break;
+ }
+ col_len = cstrlen(field->name);
+ if (type == VERT_LIST) {
+ if (col_len > max_len) {
+ max_len = col_len;
+ }
+ } else {
+ if (IS_NUM(field->type) && (int)field->max_length > 0) { /* fixup for commas */
+ field->max_length += (field->max_length - 1) / 3;
+ }
+ if (col_len < (int)field->max_length) {
+ col_len = field->max_length;
+ }
+ if (col_len < 4 && !IS_NOT_NULL(field->flags)) {
+ col_len = 4; /* 4 = length of the word "NULL" */
+ }
+ field->max_length = col_len; /* reset column info */
+ }
+ }
+
+ Dmsg0(800, "list_result finished first loop\n");
+ if (type == VERT_LIST) {
+ goto vertical_list;
+ }
+
+ Dmsg1(800, "list_result starts second loop looking at %d fields\n", sql_num_fields(mdb));
+ list_dashes(mdb, send, ctx);
+ send(ctx, "|");
+ sql_field_seek(mdb, 0);
+ for (i = 0; i < sql_num_fields(mdb); i++) {
+ Dmsg1(800, "list_result looking at field %d\n", i);
+ field = sql_fetch_field(mdb);
+ if (!field) {
+ break;
+ }
+ bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, field->name);
+ send(ctx, buf);
+ }
+ send(ctx, "\n");
+ list_dashes(mdb, send, ctx);
+
+ Dmsg1(800, "list_result starts third loop looking at %d fields\n", sql_num_fields(mdb));
+ while ((row = sql_fetch_row(mdb)) != NULL) {
+ sql_field_seek(mdb, 0);
+ send(ctx, "|");
+ for (i = 0; i < sql_num_fields(mdb); i++) {
+ field = sql_fetch_field(mdb);
+ if (!field) {
+ break;
+ }
+ if (row[i] == NULL) {
+ bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, "NULL");
+ } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
+ bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length,
+ add_commas(row[i], ewc));
+ } else {
+ bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, row[i]);
+ }
+ send(ctx, buf);
+ }
+ send(ctx, "\n");
+ }
+ list_dashes(mdb, send, ctx);
+ return;
+
+vertical_list:
+
+ Dmsg1(800, "list_result starts vertical list at %d fields\n", sql_num_fields(mdb));
+ while ((row = sql_fetch_row(mdb)) != NULL) {
+ sql_field_seek(mdb, 0);
+ for (i = 0; i < sql_num_fields(mdb); i++) {
+ field = sql_fetch_field(mdb);
+ if (!field) {
+ break;
+ }
+ if (row[i] == NULL) {
+ bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
+ } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) {
+ bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
+ add_commas(row[i], ewc));
+ } else {
+ bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
+ }
+ send(ctx, buf);
+ }
+ send(ctx, "\n");
+ }
+ return;
+}
+
+/*
+ * Open a new connexion to mdb catalog. This function is used
+ * by batch and accurate mode.
+ */
+bool db_open_batch_connexion(JCR *jcr, B_DB *mdb)
+{
+ int multi_db=false;
+
+#ifdef HAVE_BATCH_FILE_INSERT
+ multi_db=true; /* we force a new connexion only if batch insert is enabled */
+#endif
+
+ if (!jcr->db_batch) {
+ jcr->db_batch = db_init_database(jcr,
+ mdb->db_name,
+ mdb->db_user,
+ mdb->db_password,
+ mdb->db_address,
+ mdb->db_port,
+ mdb->db_socket,
+ multi_db /* multi_db = true when using batch mode */);
+ if (!jcr->db_batch) {
+ Jmsg0(jcr, M_FATAL, 0, "Could not init batch connexion");
+ return false;
+ }
+
+ if (!db_open_database(jcr, jcr->db_batch)) {
+ Mmsg2(&jcr->db_batch->errmsg, _("Could not open database \"%s\": ERR=%s\n"),
+ jcr->db_batch->db_name, db_strerror(jcr->db_batch));
+ Jmsg1(jcr, M_FATAL, 0, "%s", jcr->db_batch->errmsg);
+ return false;
+ }
+ Dmsg3(100, "initdb ref=%d connected=%d db=%p\n", jcr->db_batch->ref_count,
+ jcr->db_batch->connected, jcr->db_batch->db);
+
+ }
+ return true;
+}
+
+/*
+ * !!! WARNING !!! Use this function only when bacula is stopped.
+ * ie, after a fatal signal and before exiting the program
+ * Print information about a B_DB object.
+ */
+void _dbg_print_db(JCR *jcr, FILE *fp)
+{
+ B_DB *mdb = jcr->db;
+
+ if (!mdb) {
+ return;
+ }
+
+ fprintf(fp, "B_DB=%p db_name=%s db_user=%s connected=%i\n",
+ mdb, NPRTB(mdb->db_name), NPRTB(mdb->db_user), mdb->connected);
+ fprintf(fp, "\tcmd=\"%s\" changes=%i\n", NPRTB(mdb->cmd), mdb->changes);
+ if (mdb->lock.valid == RWLOCK_VALID) {
+ fprintf(fp, "\tRWLOCK=%p w_active=%i w_wait=%i\n", &mdb->lock, mdb->lock.w_active, mdb->lock.w_wait);
+#ifndef HAVE_WIN32
+ fprintf(fp, "\t\tthreadid=0x%x mutex=%p\n", (int)mdb->lock.writer_id, &mdb->lock.mutex);
+#endif
+ }
+}
+
+#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/