From 4ab63834abdf68ec979d9afd39c9b4de5a6e4459 Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 29 Sep 2016 17:26:03 +1000 Subject: [PATCH] ckdb - update cmd_dsp --- src/ckdb.h | 2 +- src/ckdb_cmd.c | 72 ++++++++++++++++++++++++++++++++++--------------- src/ckdb_data.c | 6 +++-- src/klist.c | 57 +++++++++++++++++++++++++++++++++++++++ src/klist.h | 2 ++ src/ktree.c | 6 +++-- 6 files changed, 118 insertions(+), 27 deletions(-) diff --git a/src/ckdb.h b/src/ckdb.h index 646a5e68..f8bed5a7 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -58,7 +58,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.7" -#define CKDB_VERSION DB_VERSION"-2.511" +#define CKDB_VERSION DB_VERSION"-2.512" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index e4578fa5..82b2c401 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -5904,45 +5904,73 @@ static char *cmd_dsp(__maybe_unused PGconn *conn, __maybe_unused char *cmd, __maybe_unused K_TREE *trf_root, __maybe_unused bool reload_data) { - __maybe_unused K_ITEM *i_file; - __maybe_unused char reply[1024] = ""; - __maybe_unused size_t siz = sizeof(reply); - LOGDEBUG("%s(): cmd '%s'", __func__, cmd); - // WARNING: This is a gaping security hole - only use in development +#if 1 LOGDEBUG("%s.disabled.dsp", id); return strdup("disabled.dsp"); -/* +#else + // WARNING: This is a gaping security hole - only use in development + K_ITEM *i_file, *i_name, *i_type; + char reply[1024] = "", *fil, *name, *typ; + size_t siz = sizeof(reply); + i_file = require_name(trf_root, "file", 1, NULL, reply, siz); if (!i_file) return strdup(reply); - dsp_ktree(blocks_free, blocks_root, transfer_data(i_file), NULL); + i_name = require_name(trf_root, "name", 1, NULL, reply, siz); + if (!i_name) + return strdup(reply); + + i_type = optional_name(trf_root, "type", 1, NULL, reply, siz); + if (*reply) + return strdup(reply); + + fil = transfer_data(i_file); + name = transfer_data(i_name); + if (i_type) + typ = transfer_data(i_type); + else + typ = "tree"; + + if (strcasecmp(typ, "tree") == 0) { + if (strcasecmp(name, "blocks") == 0) + dsp_ktree(blocks_root, fil, NULL); - dsp_ktree(transfer_free, trf_root, transfer_data(i_file), NULL); + if (strcasecmp(name, "transfer") == 0) + dsp_ktree(trf_root, fil, NULL); - dsp_ktree(paymentaddresses_free, paymentaddresses_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "paymentaddresses") == 0) + dsp_ktree(paymentaddresses_root, fil, NULL); - dsp_ktree(paymentaddresses_create_free, paymentaddresses_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "paymentaddresses_create") == 0) + dsp_ktree(paymentaddresses_root, fil, NULL); - dsp_ktree(sharesummary_free, sharesummary_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "sharesummary") == 0) + dsp_ktree(sharesummary_root, fil, NULL); - dsp_ktree(userstats_free, userstats_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "userstats") == 0) + dsp_ktree(userstats_root, fil, NULL); - dsp_ktree(markersummary_free, markersummary_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "markersummary") == 0) + dsp_ktree(markersummary_root, fil, NULL); - dsp_ktree(workmarkers_free, workmarkers_root, - transfer_data(i_file), NULL); + if (strcasecmp(name, "workmarkers") == 0) + dsp_ktree(workmarkers_root, fil, NULL); + } + + if (strcasecmp(typ, "store") == 0) { + if (strcasecmp(name, "blocks") == 0) + dsp_kstore(blocks_store, fil, NULL); - LOGDEBUG("%s.ok.dsp.file='%s'", id, transfer_data(i_file)); + if (strcasecmp(name, "markersummary") == 0) + dsp_kstore(markersummary_store, fil, NULL); + } + + LOGDEBUG("%s.ok.dsp.file='%s'", id, fil); return strdup("ok.dsp"); -*/ +#endif } static char *cmd_stats(__maybe_unused PGconn *conn, char *cmd, char *id, diff --git a/src/ckdb_data.c b/src/ckdb_data.c index d4bc4461..c24f6a6e 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -856,8 +856,10 @@ void dsp_transfer(K_ITEM *item, FILE *stream) fprintf(stream, "%s() called with (null) item\n", __func__); else { DATA_TRANSFER(t, item); - fprintf(stream, " name='%s' mvalue='%s' malloc=%"PRIu64"\n", - t->name, t->mvalue, t->msiz); + fprintf(stream, " name='%s' mvalue='%s' malloc=%"PRIu64 + " intransient=%c\n", + t->name, t->mvalue, t->msiz, + t->intransient ? 'Y' : 'N'); } } diff --git a/src/klist.c b/src/klist.c index 1741489e..d1f65ae0 100644 --- a/src/klist.c +++ b/src/klist.c @@ -60,6 +60,63 @@ K_LISTS *all_klists; #define CHKITEM(__item, __list) _CHKITEM(__item, __list, "item") +void _dsp_kstore(K_STORE *store, char *filename, char *msg, KLIST_FFL_ARGS) +{ + K_ITEM *item; + FILE *stream; + struct tm tm; + time_t now_t; + char stamp[128]; + + if (!(store->master->dsp_func)) { + quithere(1, "List %s has no dsp_func" KLIST_FFL, + store->master->name, KLIST_FFL_PASS); + } + + now_t = time(NULL); + localtime_r(&now_t, &tm); + snprintf(stamp, sizeof(stamp), + "[%d-%02d-%02d %02d:%02d:%02d]", + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); + + stream = fopen(filename, "ae"); + if (!stream) + { + fprintf(stderr, "%s %s() failed to open '%s' (%d) %s", + stamp, __func__, filename, errno, strerror(errno)); + return; + } + + if (msg) + fprintf(stream, "%s %s\n", stamp, msg); + else + fprintf(stream, "%s Dump of store '%s':\n", stamp, store->master->name); + + if (store->count > 0) + { + K_RLOCK(store->master); + + item = store->head; + while (item) + { + store->master->dsp_func(item, stream); + item = item->next; + } + K_RUNLOCK(store->master); + + fprintf(stream, "End\n\n"); + } + else + fprintf(stream, "Empty kstore\n\n"); + + fclose(stream); +} + static void k_alloc_items(K_LIST *list, KLIST_FFL_ARGS) { K_ITEM *item; diff --git a/src/klist.h b/src/klist.h index d40b199b..eb9e6dc7 100644 --- a/src/klist.h +++ b/src/klist.h @@ -663,6 +663,8 @@ static inline K_ITEM *list_rtail(K_LIST *list) #define STORE_HEAD_NOLOCK(_s) LIST_HEAD_NOLOCK(_s) #define STORE_TAIL_NOLOCK(_s) LIST_TAIL_NOLOCK(_s) +extern void _dsp_kstore(K_STORE *store, char *filename, char *msg, KLIST_FFL_ARGS); +#define dsp_kstore(_store, _file, _msg) _dsp_kstore(_store, _file, _msg, KLIST_FFL_HERE) extern K_STORE *_k_new_store(K_LIST *list, KLIST_FFL_ARGS); #define k_new_store(_list) _k_new_store(_list, KLIST_FFL_HERE) extern K_LIST *_k_new_list(const char *name, size_t siz, int allocate, diff --git a/src/ktree.c b/src/ktree.c index 78b2ef98..5675a5ae 100644 --- a/src/ktree.c +++ b/src/ktree.c @@ -182,8 +182,6 @@ void _dsp_ktree(K_TREE *tree, char *filename, char *msg, KTREE_FFL_ARGS) if (!(tree->master->dsp_func)) FAIL("NULLDSP NULL dsp_func in %s", tree->master->name); - _TREE_READ(tree, true, file, func, line); - now_t = time(NULL); localtime_r(&now_t, &tm); snprintf(stamp, sizeof(stamp), @@ -210,12 +208,16 @@ void _dsp_ktree(K_TREE *tree, char *filename, char *msg, KTREE_FFL_ARGS) if (tree->root->isNil == No) { + K_RLOCK(tree->master); + item = first_in_ktree(tree, ctx); while (item) { tree->master->dsp_func(item, stream); item = next_in_ktree(ctx); } + K_RUNLOCK(tree->master); + fprintf(stream, "End\n\n"); } else