mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-09-18 18:22:07 +08:00
code to handle options and debugging
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
lib_LTLIBRARIES = libmdb.la
|
||||
libmdb_la_SOURCES= catalog.c mem.c file.c kkd.c table.c data.c dump.c backend.c money.c sargs.c index.c like.c write.c stats.c map.c props.c worktable.c
|
||||
libmdb_la_SOURCES= catalog.c mem.c file.c kkd.c table.c data.c dump.c backend.c money.c sargs.c index.c like.c write.c stats.c map.c props.c worktable.c options.c
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS)
|
||||
LIBS = $(GLIB_LIBS) -lm
|
||||
|
@@ -25,7 +25,6 @@
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
#define MDB_DEBUG_OLE 0
|
||||
#define OFFSET_MASK 0x1fff
|
||||
|
||||
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s);
|
||||
@@ -692,9 +691,7 @@ mdb_ole_read(MdbHandle *mdb, MdbColumn *col, void *ole_ptr, int chunk_size)
|
||||
|
||||
ole_len = mdb_get_int16(ole_ptr, 0);
|
||||
ole_flags = mdb_get_int16(ole_ptr, 2);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("ole len = %d ole flags = %08x\n", ole_len, ole_flags);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"ole len = %d ole flags = %08x", ole_len, ole_flags);
|
||||
|
||||
col->chunk_size = chunk_size;
|
||||
|
||||
@@ -714,9 +711,7 @@ mdb_ole_read(MdbHandle *mdb, MdbColumn *col, void *ole_ptr, int chunk_size)
|
||||
} else if (ole_flags == 0x4000) {
|
||||
col->cur_blob_row = ((char *)ole_ptr)[4];
|
||||
col->cur_blob_pg = mdb_get_int24(ole_ptr, 5);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("ole row = %d ole pg = %ld\n", col->cur_blob_row, col->cur_blob_pg);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"ole row = %d ole pg = %ld", col->cur_blob_row, col->cur_blob_pg);
|
||||
if(mdb_read_alt_pg(mdb, col->cur_blob_pg) != mdb->fmt->pg_size) {
|
||||
/* Failed to read */
|
||||
return 0;
|
||||
@@ -726,14 +721,11 @@ mdb_ole_read(MdbHandle *mdb, MdbColumn *col, void *ole_ptr, int chunk_size)
|
||||
row_stop = mdb_find_end_of_row(mdb, col->cur_blob_row);
|
||||
row_start = mdb_pg_get_int16(mdb, 10 + col->cur_blob_row * 2);
|
||||
len = row_stop - row_start + 1;
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("start %d stop %d len %d\n", row_start, row_stop, len);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"start %d stop %d len %d", row_start, row_stop, len);
|
||||
if (col->bind_ptr) {
|
||||
memcpy(col->bind_ptr, &mdb->pg_buf[row_start], len);
|
||||
#if MDB_DEBUG_OLE
|
||||
buffer_dump(col->bind_ptr, 0, 16);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_OLE))
|
||||
buffer_dump(col->bind_ptr, 0, 16);
|
||||
}
|
||||
/* make sure to swap page back */
|
||||
mdb_swap_pgbuf(mdb);
|
||||
@@ -794,9 +786,7 @@ guint16 len, cur;
|
||||
ole_row = mdb->pg_buf[start+4];
|
||||
|
||||
lval_pg = mdb_pg_get_int24(mdb, start+5);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("Reading LVAL page %06x\n", lval_pg);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"Reading LVAL page %06x", lval_pg);
|
||||
if(mdb_read_alt_pg(mdb, lval_pg) != mdb->fmt->pg_size) {
|
||||
/* Failed to read */
|
||||
return 0;
|
||||
@@ -809,9 +799,7 @@ guint16 len, cur;
|
||||
row_stop = mdb->fmt->pg_size - 1;
|
||||
}
|
||||
row_start = mdb_pg_get_int16(mdb, 10 + ole_row * 2);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("row num %d row start %d row stop %d\n", ole_row, row_start, row_stop);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"row num %d row start %d row stop %d", ole_row, row_start, row_stop);
|
||||
len = row_stop - row_start;
|
||||
if (dest) memcpy(dest, &mdb->pg_buf[row_start], len);
|
||||
/* make sure to swap page back */
|
||||
@@ -820,9 +808,7 @@ guint16 len, cur;
|
||||
} else if (ole_flags == 0x0000) {
|
||||
ole_row = mdb->pg_buf[start+4];
|
||||
lval_pg = mdb_pg_get_int24(mdb, start+5);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("Reading LVAL page %06x\n", lval_pg);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"Reading LVAL page %06x", lval_pg);
|
||||
/* swap the alt and regular page buffers, so we can call get_int16 */
|
||||
mdb_swap_pgbuf(mdb);
|
||||
cur=0;
|
||||
@@ -837,9 +823,7 @@ guint16 len, cur;
|
||||
row_stop = mdb->fmt->pg_size - 1;
|
||||
}
|
||||
row_start = mdb_pg_get_int16(mdb, 10 + ole_row * 2);
|
||||
#if MDB_DEBUG_OLE
|
||||
printf("row num %d row start %d row stop %d\n", ole_row, row_start, row_stop);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_OLE,"row num %d row start %d row stop %d", ole_row, row_start, row_stop);
|
||||
len = row_stop - row_start;
|
||||
if (dest)
|
||||
memcpy(&dest[cur], &mdb->pg_buf[row_start+4],
|
||||
|
@@ -854,13 +854,8 @@ void
|
||||
mdb_index_scan_init(MdbHandle *mdb, MdbTableDef *table)
|
||||
{
|
||||
int i;
|
||||
int use_index=0;
|
||||
char *s;
|
||||
|
||||
if ((s=getenv("MDBOPTS"))) {
|
||||
if (!strcmp(s, "use_index")) use_index++;
|
||||
}
|
||||
if (use_index && mdb_choose_index(table, &i) == MDB_INDEX_SCAN) {
|
||||
if (mdb_get_option(MDB_USE_INDEX) && mdb_choose_index(table, &i) == MDB_INDEX_SCAN) {
|
||||
table->strategy = MDB_INDEX_SCAN;
|
||||
table->scan_idx = g_ptr_array_index (table->indices, i);
|
||||
table->chain = g_malloc0(sizeof(MdbIndexChain));
|
||||
|
@@ -19,20 +19,17 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <mdbtools.h>
|
||||
|
||||
#ifdef DMALLOC
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
//#define MDB_DEBUG_LIKE 1
|
||||
|
||||
int mdb_like_cmp(char *s, char *r)
|
||||
{
|
||||
int i, ret;
|
||||
|
||||
#if MDB_DEBUG_LIKE
|
||||
printf("comparing %s and %s\n", s, r);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_LIKE, "comparing %s and %s", s, r);
|
||||
switch (r[0]) {
|
||||
case '\0':
|
||||
if (s[0]=='\0') {
|
||||
@@ -60,13 +57,9 @@ int i, ret;
|
||||
if (strncmp(s,r,i)) {
|
||||
return 0;
|
||||
} else {
|
||||
#if MDB_DEBUG_LIKE
|
||||
printf("at pos %d comparing %s and %s\n", i, &s[i], &r[i]);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_LIKE, "at pos %d comparing %s and %s", i, &s[i], &r[i]);
|
||||
ret = mdb_like_cmp(&s[i],&r[i]);
|
||||
#if MDB_DEBUG_LIKE
|
||||
printf("returning %d (%s and %s)\n", ret, &s[i], &r[i]);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_LIKE, "returning %d (%s and %s)", ret, &s[i], &r[i]);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
82
src/libmdb/options.c
Normal file
82
src/libmdb/options.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/* MDB Tools - A library for reading MS Access database file
|
||||
* Copyright (C) 2004 Brian Bruns
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <mdbtools.h>
|
||||
|
||||
#ifdef DMALLOC
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
static unsigned long opts;
|
||||
static int optset;
|
||||
|
||||
static void load_options();
|
||||
|
||||
void
|
||||
mdb_debug(int klass, char *fmt, ...)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
va_list ap;
|
||||
|
||||
if (!optset) load_options();
|
||||
if (klass & opts) {
|
||||
va_start(ap, fmt);
|
||||
vfprintf (stdout,fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stdout,"\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
load_options()
|
||||
{
|
||||
char *opt;
|
||||
char *s;
|
||||
|
||||
if (!optset && (s=getenv("MDBOPTS"))) {
|
||||
opt = strtok(s, ":");
|
||||
do {
|
||||
if (!strcmp(opt, "use_index")) opts |= MDB_USE_INDEX;
|
||||
if (!strcmp(opt, "debug_like")) opts |= MDB_DEBUG_LIKE;
|
||||
if (!strcmp(opt, "debug_write")) opts |= MDB_DEBUG_WRITE;
|
||||
if (!strcmp(opt, "debug_usage")) opts |= MDB_DEBUG_USAGE;
|
||||
if (!strcmp(opt, "debug_ole")) opts |= MDB_DEBUG_OLE;
|
||||
if (!strcmp(opt, "debug_all")) {
|
||||
opts |= MDB_DEBUG_LIKE;
|
||||
opts |= MDB_DEBUG_WRITE;
|
||||
}
|
||||
opt = strtok(NULL,":");
|
||||
} while (opt);
|
||||
}
|
||||
optset = 1;
|
||||
}
|
||||
int
|
||||
mdb_get_option(unsigned long optnum)
|
||||
{
|
||||
if (!optset) load_options();
|
||||
return ((opts & optnum) > 0);
|
||||
}
|
@@ -23,7 +23,6 @@
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
#define MDB_DEBUG_USAGE 0
|
||||
|
||||
static gint mdb_col_comparer(MdbColumn *a, MdbColumn *b)
|
||||
{
|
||||
@@ -76,14 +75,11 @@ mdb_read_table(MdbCatalogEntry *entry)
|
||||
table->map_sz = row_end - row_start + 1;
|
||||
table->usage_map = malloc(table->map_sz);
|
||||
memcpy(table->usage_map, &mdb->pg_buf[row_start], table->map_sz);
|
||||
#if MDB_DEBUG_USAGE
|
||||
buffer_dump(mdb->pg_buf, row_start, row_end);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_USAGE))
|
||||
buffer_dump(mdb->pg_buf, row_start, row_end);
|
||||
/* swap back */
|
||||
mdb_swap_pgbuf(mdb);
|
||||
#if MDB_DEBUG_USAGE
|
||||
printf ("usage map found on page %ld rownum %d start %d end %d\n", mdb_pg_get_int24(mdb, fmt->tab_usage_map_offset + 1), rownum, row_start, row_end);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_USAGE,"usage map found on page %ld rownum %d start %d end %d", mdb_pg_get_int24(mdb, fmt->tab_usage_map_offset + 1), rownum, row_start, row_end);
|
||||
|
||||
|
||||
/* now grab the free space page map */
|
||||
@@ -99,9 +95,7 @@ mdb_read_table(MdbCatalogEntry *entry)
|
||||
memcpy(table->free_usage_map, &mdb->pg_buf[row_start], table->freemap_sz);
|
||||
mdb_swap_pgbuf(mdb);
|
||||
#endif
|
||||
#if MDB_DEBUG_USAGE
|
||||
printf ("free map found on page %ld rownum %d start %d end %d\n", mdb_pg_get_int24(mdb, fmt->tab_free_map_offset + 1), rownum, row_start, row_end);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_USAGE,"free map found on page %ld rownum %d start %d end %d\n", mdb_pg_get_int24(mdb, fmt->tab_free_map_offset + 1), rownum, row_start, row_end);
|
||||
|
||||
table->first_data_pg = mdb_pg_get_int16(mdb, fmt->tab_first_dpg_offset);
|
||||
|
||||
|
@@ -25,7 +25,6 @@
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
#define MDB_DEBUG_WRITE 0
|
||||
|
||||
void
|
||||
_mdb_put_int16(unsigned char *buf, guint32 offset, guint32 value)
|
||||
@@ -187,7 +186,7 @@ MdbColumn *col;
|
||||
int i, j;
|
||||
int var_cols = 0, fixed_cols = 0, num_cols, totcols = 0;
|
||||
int var_cols_found, fixed_cols_found, var_entry_pos;
|
||||
int col_start, next_col;
|
||||
int col_start;
|
||||
unsigned char *nullmask;
|
||||
int bitmask_sz;
|
||||
int byte_num, bit_num;
|
||||
@@ -393,9 +392,7 @@ int rows, free_start, free_end;
|
||||
rows = mdb_pg_get_int16(mdb, fmt->row_count_offset);
|
||||
free_start = fmt->row_count_offset + 2 + (rows * 2);
|
||||
free_end = mdb_pg_get_int16(mdb, (fmt->row_count_offset + rows * 2)) -1;
|
||||
#if MDB_DEBUG_WRITE
|
||||
printf("free space left on page = %d\n", free_end - free_start);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_WRITE,"free space left on page = %d", free_end - free_start);
|
||||
return (free_end - free_start + 1);
|
||||
}
|
||||
unsigned char *
|
||||
@@ -435,9 +432,7 @@ mdb_update_indexes(MdbTableDef *table, int num_fields, MdbField *fields, guint32
|
||||
|
||||
for (i=0;i<table->num_idxs;i++) {
|
||||
idx = g_ptr_array_index (table->indices, i);
|
||||
#if MDB_DEBUG_WRITE
|
||||
fprintf(stderr,"Updating %s (%d).\n", idx->name, idx->index_type);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_WRITE,"Updating %s (%d).", idx->name, idx->index_type);
|
||||
if (idx->index_type==1) {
|
||||
mdb_update_index(table, idx, num_fields, fields, pgnum, rownum);
|
||||
}
|
||||
@@ -513,9 +508,9 @@ mdb_insert_row(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
return 0;
|
||||
}
|
||||
new_row_size = mdb_pack_row(table, row_buffer, num_fields, fields);
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(row_buffer, 0, new_row_size-1);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE)) {
|
||||
buffer_dump(row_buffer, 0, new_row_size-1);
|
||||
}
|
||||
pgnum = mdb_map_find_next_freepage(table, new_row_size);
|
||||
if (!pgnum) {
|
||||
fprintf(stderr, "Unable to allocate new page.\n");
|
||||
@@ -524,11 +519,11 @@ mdb_insert_row(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
|
||||
rownum = mdb_add_row_to_pg(table, row_buffer, new_row_size);
|
||||
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
fprintf(stdout, "writing page %d\n", pgnum);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE)) {
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
}
|
||||
mdb_debug(MDB_DEBUG_WRITE, "writing page %d", pgnum);
|
||||
if (!mdb_write_pg(mdb, pgnum)) {
|
||||
fprintf(stderr, "write failed! exiting...\n");
|
||||
exit(1);
|
||||
@@ -608,10 +603,9 @@ int old_row_size, new_row_size, delta, num_fields;
|
||||
|
||||
row_start &= 0x0FFF; /* remove flags */
|
||||
|
||||
#if MDB_DEBUG_WRITE
|
||||
printf("page %lu row %d start %d end %d\n", (unsigned long) table->cur_phys_pg, table->cur_row-1, row_start, row_end);
|
||||
buffer_dump(mdb->pg_buf, row_start, row_end);
|
||||
#endif
|
||||
mdb_debug(MDB_DEBUG_WRITE,"page %lu row %d start %d end %d", (unsigned long) table->cur_phys_pg, table->cur_row-1, row_start, row_end);
|
||||
if (mdb_get_option(MDB_DEBUG_LIKE))
|
||||
buffer_dump(mdb->pg_buf, row_start, row_end);
|
||||
|
||||
for (i=0;i<table->num_cols;i++) {
|
||||
col = g_ptr_array_index(table->columns,i);
|
||||
@@ -622,11 +616,11 @@ int old_row_size, new_row_size, delta, num_fields;
|
||||
}
|
||||
num_fields = mdb_crack_row(table, row_start, row_end, fields);
|
||||
|
||||
#if MDB_DEBUG_WRITE
|
||||
for (i=0;i<num_fields;i++) {
|
||||
printf("col %d %d start %d siz %d\n", i, fields[i].colnum, fields[i].start, fields[i].siz);
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE)) {
|
||||
for (i=0;i<num_fields;i++) {
|
||||
printf("col %d %d start %d siz %d\n", i, fields[i].colnum, fields[i].start, fields[i].siz);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
for (i=0;i<table->num_cols;i++) {
|
||||
col = g_ptr_array_index(table->columns,i);
|
||||
if (col->bind_ptr) {
|
||||
@@ -637,9 +631,8 @@ int old_row_size, new_row_size, delta, num_fields;
|
||||
}
|
||||
|
||||
new_row_size = mdb_pack_row(table, row_buffer, num_fields, fields);
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(row_buffer, 0, new_row_size-1);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE))
|
||||
buffer_dump(row_buffer, 0, new_row_size-1);
|
||||
delta = new_row_size - old_row_size;
|
||||
if ((mdb_pg_get_freespace(mdb) - delta) < 0) {
|
||||
fprintf(stderr, "No space left on this page, update will not occur\n");
|
||||
@@ -660,11 +653,11 @@ guint16 num_rows;
|
||||
int row_start, row_end, row_size;
|
||||
int i, pos;
|
||||
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
printf("updating row %d on page %lu\n", row, (unsigned long) table->cur_phys_pg);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE)) {
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
}
|
||||
mdb_debug(MDB_DEBUG_WRITE,"updating row %d on page %lu", row, (unsigned long) table->cur_phys_pg);
|
||||
new_pg = mdb_new_data_pg(entry);
|
||||
|
||||
num_rows = mdb_pg_get_int16(mdb, fmt->row_count_offset);
|
||||
@@ -703,10 +696,10 @@ int i, pos;
|
||||
g_free(new_pg);
|
||||
|
||||
_mdb_put_int16(mdb->pg_buf, 2, mdb_pg_get_freespace(mdb));
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
#endif
|
||||
if (mdb_get_option(MDB_DEBUG_WRITE)) {
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
}
|
||||
/* drum roll, please */
|
||||
if (!mdb_write_pg(mdb, table->cur_phys_pg)) {
|
||||
fprintf(stderr, "write failed! exiting...\n");
|
||||
|
Reference in New Issue
Block a user