Project

General

Profile

Submit #3063 » save_headers_to_dbm_file.patch

htse, 09/24/2017 05:56 PM

View differences:

usr.bin/kcollect/kcollect.c
static void dump_dbm(kcollect_t *ary, size_t count, const char *datafile);
static int str2unix(const char* str, const char* fmt);
static int rec_comparator(const void *c1, const void *c2);
static void load_dbm(const char *datafile, kcollect_t *ary,
static void load_dbm(const char *datafile,
kcollect_t **ret_ary, size_t *counter);
static void dump_fields(kcollect_t *ary);
static void adjust_fields(kcollect_t *ent, const char *fields);
......
* array and counter
*/
if (fromFile) {
load_dbm(dbmFile, ary, &dbmAry, &count);
load_dbm(dbmFile, &dbmAry, &count);
free(ary);
ary = dbmAry;
......
char fmt;
char sbuf[20];
struct tm *tmv;
time_t t;
time_t t;
for (i = count - 1; i >= 2; --i) {
if ((total_count & 15) == 0) {
......
format_output(value, fmt, scale, sbuf);
printf("%s",sbuf);
}
printf("\n");
++total_count;
}
......
datum value;
time_t t;
uint i;
char h0[] = "HEADER 0";
char h1[] = "HEADER 1";
for (i = 2; i < (count - 1); ++i) {
t = ary[i].realtime.tv_sec;
for (i = 0; i < (count); ++i) {
/* first 2 INFO records are special and get 0|1 */
t = (i < 2)? i : ary[i].realtime.tv_sec;
tmv = gmtime(&t);
strftime(buf, sizeof(buf), DISPLAY_FULL_DATE, tmv);
key.dptr = buf;
key.dsize = sizeof(buf);
/* store the first 2 records with their own key */
key.dptr = (t < 2)?((t == 0)? h0:h1): buf;
key.dsize = (t < 2)?sizeof("HEADER 0"):sizeof(buf);
value.dptr = ary[i].data;
value.dsize = sizeof(ary[i].data);
value.dsize = sizeof(uint64_t) * KCOLLECT_ENTRIES;
if (dbm_store(db,key,value,DBM_INSERT) == -1) {
fprintf(stderr,
"[ERR] error storing the value in "
......
*/
static
void
load_dbm(const char* datafile, kcollect_t *ary, kcollect_t **ret_ary,
load_dbm(const char* datafile, kcollect_t **ret_ary,
size_t *counter)
{
DBM * db = dbm_open(datafile,(O_RDONLY),(S_IRUSR|S_IRGRP));
datum key;
datum value;
size_t recCounter = 0;
int headersFound = 0;
if (db == NULL) {
fprintf(stderr,
......
/* with the count allocate enough memory */
if (*ret_ary)
free(*ret_ary);
*ret_ary = malloc(sizeof(kcollect_t) * (recCounter + 2));
*ret_ary = malloc(sizeof(kcollect_t) * (recCounter));
if (*ret_ary == NULL) {
fprintf(stderr,
"[ERR] failed to allocate enough memory to "
......
dbm_close(db);
exit(EXIT_FAILURE);
} else {
/* initialize the first 2 ary records */
uint c;
memcpy((*ret_ary)[0].data, ary[0].data,
sizeof(uint64_t) * KCOLLECT_ENTRIES);
memcpy((*ret_ary)[1].data, ary[1].data,
sizeof(uint64_t) * KCOLLECT_ENTRIES);
uint sc;
/*
* Actual data retrieval but only of recCounter
* records
*/
c = 0;
c = 2;
key = dbm_firstkey(db);
while (key.dptr && c < recCounter) {
value = dbm_fetch(db, key);
if (value.dptr != NULL) {
memcpy((*ret_ary)[2 + c].data,
if(!strcmp(key.dptr,"HEADER 0")) {
sc = 0;
headersFound |= 1;
}
else if(!strcmp(key.dptr,"HEADER 1")) {
sc = 1;
headersFound |= 2;
}
else {
sc = c;
c++;
}
memcpy((*ret_ary)[sc].data,
value.dptr,
sizeof(uint64_t) * KCOLLECT_ENTRIES);
(*ret_ary)[2 + c].realtime.tv_sec =
(*ret_ary)[sc].realtime.tv_sec =
str2unix(key.dptr,
DISPLAY_FULL_DATE);
}
key = dbm_nextkey(db);
++c;
}
}
}
/*
* Set the counter to returned records + first 2 header records,
* Set the counter,
* and sort the non-header records.
*/
*counter = 2 + recCounter;
qsort(&(*ret_ary)[2], recCounter, sizeof(kcollect_t), rec_comparator);
*counter = recCounter;
qsort(&(*ret_ary)[2], recCounter - 2, sizeof(kcollect_t), rec_comparator);
dbm_close(db);
dbm_close(db);
if (headersFound != 3) {
fprintf(stderr, "We could not retrieve all necessary headers, might be the database file is corrupted? (%i)\n", headersFound);
exit(EXIT_FAILURE);
}
}
static void
(3-3/5)