diff --git a/src/modules/db_flatstore/db_flatstore.c b/src/modules/db_flatstore/db_flatstore.c index e4052eccfc3..02d95af349d 100644 --- a/src/modules/db_flatstore/db_flatstore.c +++ b/src/modules/db_flatstore/db_flatstore.c @@ -97,7 +97,7 @@ str flat_suffix = STR_STATIC_INIT(".log"); * This variable holds the timestamp of the last file rotation request * received through the management interface. */ -time_t* flat_rotate; +time_t *flat_rotate; /** Timestamp of last file rotation. @@ -107,71 +107,66 @@ time_t flat_local_timestamp; /* Flatstore database module interface */ -static cmd_export_t cmds[] = { - {"db_uri", (cmd_function)flat_uri, 0, 0, 0, 0}, - {"db_con", (cmd_function)flat_con, 0, 0, 0, 0}, - {"db_cmd", (cmd_function)flat_cmd, 0, 0, 0, 0}, - {"db_put", (cmd_function)flat_put, 0, 0, 0, 0}, - {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0} -}; +static cmd_export_t cmds[] = {{"db_uri", (cmd_function)flat_uri, 0, 0, 0, 0}, + {"db_con", (cmd_function)flat_con, 0, 0, 0, 0}, + {"db_cmd", (cmd_function)flat_cmd, 0, 0, 0, 0}, + {"db_put", (cmd_function)flat_put, 0, 0, 0, 0}, + {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}}; /* Exported parameters */ -static param_export_t params[] = { - {"flush", PARAM_INT, &flat_flush}, - {"encode_delimiter", PARAM_INT, &encode_delimiter}, - {"field_delimiter", PARAM_STR, &flat_delimiter}, - {"record_delimiter", PARAM_STR, &flat_record_delimiter}, - {"escape_char", PARAM_STR, &flat_escape}, - {"file_suffix", PARAM_STR, &flat_suffix}, - {0, 0, 0} -}; +static param_export_t params[] = {{"flush", PARAM_INT, &flat_flush}, + {"encode_delimiter", PARAM_INT, &encode_delimiter}, + {"field_delimiter", PARAM_STR, &flat_delimiter}, + {"record_delimiter", PARAM_STR, &flat_record_delimiter}, + {"escape_char", PARAM_STR, &flat_escape}, + {"file_suffix", PARAM_STR, &flat_suffix}, {0, 0, 0}}; struct module_exports exports = { - "db_flatstore", /* module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* exported functions */ - params, /* exported parameters */ - flat_rpc, /* RPC method exports */ - 0, /* exported pseudo-variables */ - 0, /* response handling function */ - mod_init, /* module initialization function */ - child_init, /* per-child init function */ - mod_destroy /* module destroy function */ + "db_flatstore", /* module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* exported functions */ + params, /* exported parameters */ + flat_rpc, /* RPC method exports */ + 0, /* exported pseudo-variables */ + 0, /* response handling function */ + mod_init, /* module initialization function */ + child_init, /* per-child init function */ + mod_destroy /* module destroy function */ }; int mod_register(char *path, int *dlflags, void *p1, void *p2) { - if(db_api_init()<0) + if(db_api_init() < 0) return -1; return 0; } static int mod_init(void) { - if (flat_delimiter.len != 1) { + if(flat_delimiter.len != 1) { ERR("flatstore: Parameter 'field_delimiter' " "must be exactly one character long.\n"); return -1; } - if (flat_record_delimiter.len != 1) { + if(flat_record_delimiter.len != 1) { ERR("flatstore: Parameter 'record_delimiter' " "must be exactly one character long.\n"); return -1; } - if (flat_escape.len != 1) { + if(flat_escape.len != 1) { ERR("flatstore: Parameter 'escape_char' " "must be exaactly one character long.\n"); return -1; } - flat_rotate = (time_t*)shm_malloc(sizeof(time_t)); - if (!flat_rotate) { + flat_rotate = (time_t *)shm_malloc(sizeof(time_t)); + if(!flat_rotate) { SHM_MEM_ERROR; return -1; } @@ -186,8 +181,10 @@ static int mod_init(void) static void mod_destroy(void) { km_mod_destroy(); - if (flat_pid.s) free(flat_pid.s); - if (flat_rotate) shm_free(flat_rotate); + if(flat_pid.s) + free(flat_pid.s); + if(flat_rotate) + shm_free(flat_rotate); } @@ -200,26 +197,26 @@ static void mod_destroy(void) */ static int child_init(int rank) { - char* tmp; + char *tmp; unsigned int v; - if(rank==PROC_INIT) + if(rank == PROC_INIT) return 0; km_child_init(rank); - if (rank <= 0) { + if(rank <= 0) { v = -rank; } else { v = rank - PROC_MIN; } - if ((tmp = int2str(v, &flat_pid.len)) == NULL) { + if((tmp = int2str(v, &flat_pid.len)) == NULL) { BUG("flatstore: Error while converting process id to number\n"); return -1; } - if ((flat_pid.s = strdup(tmp)) == NULL) { + if((flat_pid.s = strdup(tmp)) == NULL) { ERR("flatstore: No memory left\n"); return -1; } @@ -228,4 +225,3 @@ static int child_init(int rank) } /** @} */ - diff --git a/src/modules/db_flatstore/db_flatstore.h b/src/modules/db_flatstore/db_flatstore.h index b832f900fd1..7f6bb1ebbeb 100644 --- a/src/modules/db_flatstore/db_flatstore.h +++ b/src/modules/db_flatstore/db_flatstore.h @@ -35,14 +35,14 @@ #include "../../core/str.h" #include -extern str flat_pid; -extern int flat_flush; -extern str flat_record_delimiter; -extern str flat_delimiter; -extern str flat_escape; -extern str flat_suffix; -extern time_t* flat_rotate; -extern time_t flat_local_timestamp; +extern str flat_pid; +extern int flat_flush; +extern str flat_record_delimiter; +extern str flat_delimiter; +extern str flat_escape; +extern str flat_suffix; +extern time_t *flat_rotate; +extern time_t flat_local_timestamp; /** @} */ diff --git a/src/modules/db_flatstore/flat_cmd.c b/src/modules/db_flatstore/flat_cmd.c index 3b0ecb01919..ae7b7bf166a 100644 --- a/src/modules/db_flatstore/flat_cmd.c +++ b/src/modules/db_flatstore/flat_cmd.c @@ -43,45 +43,47 @@ * @param cmd A pointer to generic db_cmd command being freed. * @param payload A pointer to flat_cmd structure to be freed. */ -static void flat_cmd_free(db_cmd_t* cmd, struct flat_cmd* payload) +static void flat_cmd_free(db_cmd_t *cmd, struct flat_cmd *payload) { db_drv_free(&payload->gen); pkg_free(payload); } -int flat_cmd(db_cmd_t* cmd) +int flat_cmd(db_cmd_t *cmd) { - struct flat_cmd* fcmd; - db_con_t* con; + struct flat_cmd *fcmd; + db_con_t *con; - if (cmd->type != DB_PUT) { + if(cmd->type != DB_PUT) { ERR("flatstore: The driver supports PUT operation only.\n"); return -1; } - if (DB_FLD_EMPTY(cmd->vals)) { + if(DB_FLD_EMPTY(cmd->vals)) { ERR("flatstore: PUT command with no values encountered\n"); return -1; } - fcmd = (struct flat_cmd*)pkg_malloc(sizeof(struct flat_cmd)); - if (fcmd == NULL) { + fcmd = (struct flat_cmd *)pkg_malloc(sizeof(struct flat_cmd)); + if(fcmd == NULL) { PKG_MEM_ERROR; return -1; } memset(fcmd, '\0', sizeof(struct flat_cmd)); - if (db_drv_init(&fcmd->gen, flat_cmd_free) < 0) goto error; + if(db_drv_init(&fcmd->gen, flat_cmd_free) < 0) + goto error; /* FIXME */ con = cmd->ctx->con[db_payload_idx]; - if (flat_open_table(&fcmd->file_index, con, &cmd->table) < 0) goto error; + if(flat_open_table(&fcmd->file_index, con, &cmd->table) < 0) + goto error; DB_SET_PAYLOAD(cmd, fcmd); return 0; - error: - if (fcmd) { +error: + if(fcmd) { DB_SET_PAYLOAD(cmd, NULL); db_drv_free(&fcmd->gen); pkg_free(fcmd); @@ -90,13 +92,13 @@ int flat_cmd(db_cmd_t* cmd) } -int flat_put(db_res_t* res, db_cmd_t* cmd) +int flat_put(db_res_t *res, db_cmd_t *cmd) { - struct flat_cmd* fcmd; - struct flat_con* fcon; - db_con_t* con; + struct flat_cmd *fcmd; + struct flat_con *fcon; + db_con_t *con; int i; - FILE* f; + FILE *f; char delims[4], *s; size_t len; @@ -106,14 +108,14 @@ int flat_put(db_res_t* res, db_cmd_t* cmd) fcon = DB_GET_PAYLOAD(con); f = fcon->file[fcmd->file_index].f; - if (f == NULL) { + if(f == NULL) { ERR("flatstore: Cannot write, file handle not open\n"); return -1; } - if (flat_local_timestamp < *flat_rotate) { + if(flat_local_timestamp < *flat_rotate) { flat_con_disconnect(con); - if (flat_con_connect(con) < 0) { + if(flat_con_connect(con) < 0) { ERR("flatstore: Error while rotating files\n"); return -1; } @@ -121,91 +123,104 @@ int flat_put(db_res_t* res, db_cmd_t* cmd) } for(i = 0; !DB_FLD_EMPTY(cmd->vals) && !DB_FLD_LAST(cmd->vals[i]); i++) { - if (i) { - if (fprintf(f, "%c", flat_delimiter.s[0]) < 0) goto error; + if(i) { + if(fprintf(f, "%c", flat_delimiter.s[0]) < 0) + goto error; } /* TODO: how to distinguish NULL from empty */ - if (cmd->vals[i].flags & DB_NULL) continue; - - switch(cmd->vals[i].type) { - case DB_INT: - if (fprintf(f, "%d", cmd->vals[i].v.int4) < 0) goto error; - break; - - case DB_FLOAT: - if (fprintf(f, "%f", cmd->vals[i].v.flt) < 0) goto error; - break; - - case DB_DOUBLE: - if (fprintf(f, "%f", cmd->vals[i].v.dbl) < 0) goto error; - break; + if(cmd->vals[i].flags & DB_NULL) + continue; - case DB_DATETIME: - if (fprintf(f, "%llu", (unsigned long long)cmd->vals[i].v.time) < 0) - goto error; - break; - - case DB_CSTR: - s = cmd->vals[i].v.cstr; - delims[0] = flat_delimiter.s[0]; - delims[1] = flat_record_delimiter.s[0]; - delims[2] = flat_escape.s[0]; - delims[3] = '\0'; - while (*s) { - len = strcspn(s, delims); - if (fprintf(f, "%.*s", (int)len, s) < 0) goto error; - s += len; - if (*s) { - /* FIXME: do not use the escaped value for easier parsing */ - if (fprintf(f, "%c%c", flat_escape.s[0], *s) < 0) goto error; - s++; + switch(cmd->vals[i].type) { + case DB_INT: + if(fprintf(f, "%d", cmd->vals[i].v.int4) < 0) + goto error; + break; + + case DB_FLOAT: + if(fprintf(f, "%f", cmd->vals[i].v.flt) < 0) + goto error; + break; + + case DB_DOUBLE: + if(fprintf(f, "%f", cmd->vals[i].v.dbl) < 0) + goto error; + break; + + case DB_DATETIME: + if(fprintf(f, "%llu", (unsigned long long)cmd->vals[i].v.time) + < 0) + goto error; + break; + + case DB_CSTR: + s = cmd->vals[i].v.cstr; + delims[0] = flat_delimiter.s[0]; + delims[1] = flat_record_delimiter.s[0]; + delims[2] = flat_escape.s[0]; + delims[3] = '\0'; + while(*s) { + len = strcspn(s, delims); + if(fprintf(f, "%.*s", (int)len, s) < 0) + goto error; + s += len; + if(*s) { + /* FIXME: do not use the escaped value for easier parsing */ + if(fprintf(f, "%c%c", flat_escape.s[0], *s) < 0) + goto error; + s++; + } } - } - break; - - case DB_STR: - case DB_BLOB: - /* FIXME: rewrite */ - s = cmd->vals[i].v.lstr.s; - len = cmd->vals[i].v.lstr.len; - while (len > 0) { - char *c; - for (c = s; len > 0 && - *c != flat_delimiter.s[0] && - *c != flat_record_delimiter.s[0] && - *c != flat_escape.s[0]; - c++, len--); - if (fprintf(f, "%.*s", (int)(c-s), s) < 0) goto error; - s = c; - if (len > 0) { - if (fprintf(f, "%c%c", flat_escape.s[0], *s) < 0) goto error; - s++; - len--; + break; + + case DB_STR: + case DB_BLOB: + /* FIXME: rewrite */ + s = cmd->vals[i].v.lstr.s; + len = cmd->vals[i].v.lstr.len; + while(len > 0) { + char *c; + for(c = s; len > 0 && *c != flat_delimiter.s[0] + && *c != flat_record_delimiter.s[0] + && *c != flat_escape.s[0]; + c++, len--) + ; + if(fprintf(f, "%.*s", (int)(c - s), s) < 0) + goto error; + s = c; + if(len > 0) { + if(fprintf(f, "%c%c", flat_escape.s[0], *s) < 0) + goto error; + s++; + len--; + } } - } - break; + break; - case DB_BITMAP: - if (fprintf(f, "%u", cmd->vals[i].v.bitmap) < 0) goto error; - break; + case DB_BITMAP: + if(fprintf(f, "%u", cmd->vals[i].v.bitmap) < 0) + goto error; + break; - default: - BUG("flatstore: Unsupported field type %d\n", cmd->vals[i].type); - return -1; + default: + BUG("flatstore: Unsupported field type %d\n", + cmd->vals[i].type); + return -1; } } - if (fprintf(f, "%c", flat_record_delimiter.s[0]) < 0) goto error; + if(fprintf(f, "%c", flat_record_delimiter.s[0]) < 0) + goto error; - if (flat_flush && (fflush(f) != 0)) { + if(flat_flush && (fflush(f) != 0)) { ERR("flatstore: Error while flushing file: %s\n", strerror(errno)); return -1; } return 0; - error: +error: ERR("flastore: Error while writing data to file\n"); return -1; } diff --git a/src/modules/db_flatstore/flat_cmd.h b/src/modules/db_flatstore/flat_cmd.h index 7a35f9be551..3dc3ad9431f 100644 --- a/src/modules/db_flatstore/flat_cmd.h +++ b/src/modules/db_flatstore/flat_cmd.h @@ -39,7 +39,8 @@ * This data structure extends the generic data structure db_cmd in the * database API with data specific to the flatstore driver. */ -struct flat_cmd { +struct flat_cmd +{ db_drv_t gen; /**< Generic part of the data structure (must be first) */ int file_index; }; @@ -52,7 +53,7 @@ struct flat_cmd { * @param cmd A generic db_cmd structure to which the newly created flat_cmd * structure will be attached. */ -int flat_cmd(db_cmd_t* cmd); +int flat_cmd(db_cmd_t *cmd); /** The main execution function in the flat driver. @@ -66,7 +67,7 @@ int flat_cmd(db_cmd_t* cmd); * @retval A negative number if the database server failed to execute command * @retval A positive number if there was an error on client side (SER) */ -int flat_put(db_res_t* res, db_cmd_t* cmd); +int flat_put(db_res_t *res, db_cmd_t *cmd); /** @} */ diff --git a/src/modules/db_flatstore/flat_con.c b/src/modules/db_flatstore/flat_con.c index 76665b2fede..8bb862783a4 100644 --- a/src/modules/db_flatstore/flat_con.c +++ b/src/modules/db_flatstore/flat_con.c @@ -48,23 +48,28 @@ * @param con A generic db_con connection structure. * @param payload Flatstore specific payload to be freed. */ -static void flat_con_free(db_con_t* con, struct flat_con* payload) +static void flat_con_free(db_con_t *con, struct flat_con *payload) { int i; - if (!payload) return; + if(!payload) + return; /* delete the structure only if there are no more references * to it in the connection pool */ - if (db_pool_remove((db_pool_entry_t*)payload) == 0) return; - + if(db_pool_remove((db_pool_entry_t *)payload) == 0) + return; + db_pool_entry_free(&payload->gen); - if (payload->file) { + if(payload->file) { for(i = 0; i < payload->n; i++) { - if (payload->file[i].filename) pkg_free(payload->file[i].filename); - if (payload->file[i].table.s) pkg_free(payload->file[i].table.s); - if (payload->file[i].f) fclose(payload->file[i].f); + if(payload->file[i].filename) + pkg_free(payload->file[i].filename); + if(payload->file[i].table.s) + pkg_free(payload->file[i].table.s); + if(payload->file[i].f) + fclose(payload->file[i].f); } pkg_free(payload->file); } @@ -72,36 +77,37 @@ static void flat_con_free(db_con_t* con, struct flat_con* payload) } -int flat_con(db_con_t* con) +int flat_con(db_con_t *con) { - struct flat_con* fcon; + struct flat_con *fcon; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ - fcon = (struct flat_con*)db_pool_get(con->uri); - if (fcon) { + fcon = (struct flat_con *)db_pool_get(con->uri); + if(fcon) { DBG("flatstore: A handle to %.*s found in the connection pool\n", - STR_FMT(&con->uri->body)); + STR_FMT(&con->uri->body)); goto found; } - fcon = (struct flat_con*)pkg_malloc(sizeof(struct flat_con)); - if (fcon == NULL) { + fcon = (struct flat_con *)pkg_malloc(sizeof(struct flat_con)); + if(fcon == NULL) { PKG_MEM_ERROR; goto error; } memset(fcon, '\0', sizeof(struct flat_con)); - if (db_pool_entry_init(&fcon->gen, flat_con_free, con->uri) < 0) goto error; + if(db_pool_entry_init(&fcon->gen, flat_con_free, con->uri) < 0) + goto error; + + DBG("flastore: Preparing new file handles to files in %.*s\n", + STR_FMT(&con->uri->body)); - DBG("flastore: Preparing new file handles to files in %.*s\n", - STR_FMT(&con->uri->body)); - /* Put the newly created flatstore connection into the pool */ - db_pool_put((struct db_pool_entry*)fcon); + db_pool_put((struct db_pool_entry *)fcon); DBG("flatstore: Handle stored in connection pool\n"); - found: +found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ @@ -110,8 +116,8 @@ int flat_con(db_con_t* con) con->disconnect = flat_con_disconnect; return 0; - error: - if (fcon) { +error: + if(fcon) { db_pool_entry_free(&fcon->gen); pkg_free(fcon); } @@ -119,58 +125,60 @@ int flat_con(db_con_t* con) } -int flat_con_connect(db_con_t* con) +int flat_con_connect(db_con_t *con) { - struct flat_con* fcon; + struct flat_con *fcon; int i; - + fcon = DB_GET_PAYLOAD(con); - + /* Do not reconnect already connected connections */ - if (fcon->flags & FLAT_OPENED) return 0; + if(fcon->flags & FLAT_OPENED) + return 0; - DBG("flatstore: Opening handles to files in '%.*s'\n", - STR_FMT(&con->uri->body)); + DBG("flatstore: Opening handles to files in '%.*s'\n", + STR_FMT(&con->uri->body)); /* FIXME: Make sure the directory exists, is accessible, * and we can create files there */ - DBG("flatstore: Directory '%.*s' opened successfully\n", - STR_FMT(&con->uri->body)); + DBG("flatstore: Directory '%.*s' opened successfully\n", + STR_FMT(&con->uri->body)); for(i = 0; i < fcon->n; i++) { - if (fcon->file[i].f) { + if(fcon->file[i].f) { fclose(fcon->file[i].f); } fcon->file[i].f = fopen(fcon->file[i].filename, "a"); - if (fcon->file[i].f == NULL) { - ERR("flatstore: Error while opening file handle to '%s': %s\n", - fcon->file[i].filename, strerror(errno)); + if(fcon->file[i].f == NULL) { + ERR("flatstore: Error while opening file handle to '%s': %s\n", + fcon->file[i].filename, strerror(errno)); return -1; } } fcon->flags |= FLAT_OPENED; return 0; - } -void flat_con_disconnect(db_con_t* con) +void flat_con_disconnect(db_con_t *con) { - struct flat_con* fcon; + struct flat_con *fcon; int i; fcon = DB_GET_PAYLOAD(con); - if ((fcon->flags & FLAT_OPENED) == 0) return; + if((fcon->flags & FLAT_OPENED) == 0) + return; - DBG("flatstore: Closing handles to files in '%.*s'\n", - STR_FMT(&con->uri->body)); + DBG("flatstore: Closing handles to files in '%.*s'\n", + STR_FMT(&con->uri->body)); for(i = 0; i < fcon->n; i++) { - if (fcon->file[i].f == NULL) continue; + if(fcon->file[i].f == NULL) + continue; fclose(fcon->file[i].f); fcon->file[i].f = NULL; } @@ -180,84 +188,83 @@ void flat_con_disconnect(db_con_t* con) /* returns a pkg_malloc'ed file name */ -static char* get_filename(str* dir, str* name) +static char *get_filename(str *dir, str *name) { - char* buf, *p; - int buf_len, total_len; + char *buf, *p; + int buf_len, total_len; - buf_len = pathmax(); + buf_len = pathmax(); - total_len = dir->len + 1 /* / */ + - name->len + 1 /* _ */+ - flat_pid.len + - flat_suffix.len + 1 /* \0 */; + total_len = dir->len + 1 /* / */ + name->len + 1 /* _ */ + flat_pid.len + + flat_suffix.len + 1 /* \0 */; - if (buf_len < total_len) { - ERR("flatstore: The path is too long (%d and PATHMAX is %d)\n", - total_len, buf_len); - return 0; - } + if(buf_len < total_len) { + ERR("flatstore: The path is too long (%d and PATHMAX is %d)\n", + total_len, buf_len); + return 0; + } - if ((buf = pkg_malloc(buf_len)) == NULL) { - PKG_MEM_ERROR; - return 0; - } - p = buf; + if((buf = pkg_malloc(buf_len)) == NULL) { + PKG_MEM_ERROR; + return 0; + } + p = buf; - memcpy(p, dir->s, dir->len); - p += dir->len; + memcpy(p, dir->s, dir->len); + p += dir->len; - *p++ = '/'; + *p++ = '/'; - memcpy(p, name->s, name->len); - p += name->len; + memcpy(p, name->s, name->len); + p += name->len; - *p++ = '_'; + *p++ = '_'; - memcpy(p, flat_pid.s, flat_pid.len); - p += flat_pid.len; + memcpy(p, flat_pid.s, flat_pid.len); + p += flat_pid.len; - memcpy(p, flat_suffix.s, flat_suffix.len); - p += flat_suffix.len; + memcpy(p, flat_suffix.s, flat_suffix.len); + p += flat_suffix.len; - *p = '\0'; - return buf; + *p = '\0'; + return buf; } - -int flat_open_table(int* idx, db_con_t* con, str* name) +int flat_open_table(int *idx, db_con_t *con, str *name) { - struct flat_uri* furi; - struct flat_con* fcon; - struct flat_file* new; + struct flat_uri *furi; + struct flat_con *fcon; + struct flat_file *new; int i; - char* filename, *table; + char *filename, *table; new = NULL; filename = NULL; table = NULL; fcon = DB_GET_PAYLOAD(con); furi = DB_GET_PAYLOAD(con->uri); - + for(i = 0; i < fcon->n; i++) { - if (name->len == fcon->file[i].table.len && - !strncmp(name->s, fcon->file[i].table.s, name->len)) + if(name->len == fcon->file[i].table.len + && !strncmp(name->s, fcon->file[i].table.s, name->len)) break; } - if (fcon->n == i) { + if(fcon->n == i) { /* Perform operations that can fail first (before resizing * fcon->file, so that we can fail gracefully if one of the * operations fail. */ - if ((filename = get_filename(&furi->path, name)) == NULL) + if((filename = get_filename(&furi->path, name)) == NULL) goto no_mem; - if ((table = pkg_malloc(name->len)) == NULL) goto no_mem; + if((table = pkg_malloc(name->len)) == NULL) + goto no_mem; memcpy(table, name->s, name->len); new = pkg_realloc(fcon->file, sizeof(struct flat_file) * (fcon->n + 1)); - if (new == NULL) goto no_mem; + if(new == NULL) + goto no_mem; fcon->file = new; new = new + fcon->n; /* Advance to the new (last) element */ @@ -268,28 +275,30 @@ int flat_open_table(int* idx, db_con_t* con, str* name) new->filename = filename; /* Also open the file if we are connected already */ - if (fcon->flags & FLAT_OPENED) { - if ((new->f = fopen(new->filename, "a")) == NULL) { - ERR("flatstore: Error while opening file handle to '%s': %s\n", - new->filename, strerror(errno)); + if(fcon->flags & FLAT_OPENED) { + if((new->f = fopen(new->filename, "a")) == NULL) { + ERR("flatstore: Error while opening file handle to '%s': %s\n", + new->filename, strerror(errno)); return -1; - } + } } else { new->f = NULL; } - + *idx = fcon->n - 1; } else { *idx = i; } - DBG("flatstore: Handle to file '%s' opened successfully\n", - fcon->file[*idx].filename); + DBG("flatstore: Handle to file '%s' opened successfully\n", + fcon->file[*idx].filename); return 0; - no_mem: +no_mem: PKG_MEM_ERROR; - if (filename) pkg_free(filename); - if (table) pkg_free(table); + if(filename) + pkg_free(filename); + if(table) + pkg_free(table); return -1; } diff --git a/src/modules/db_flatstore/flat_con.h b/src/modules/db_flatstore/flat_con.h index f5f5a8f8e17..f2c24f3381e 100644 --- a/src/modules/db_flatstore/flat_con.h +++ b/src/modules/db_flatstore/flat_con.h @@ -41,15 +41,17 @@ /** * Per-connection flags for flatstore connections. */ -enum flat_con_flags { - FLAT_OPENED = (1 << 0), /**< Handle opened successfully */ +enum flat_con_flags +{ + FLAT_OPENED = (1 << 0), /**< Handle opened successfully */ }; -struct flat_file { - char* filename; /**< Name of file within the directory */ - str table; /**< Table name the file belongs to */ - FILE* f; /**< File handle of the file */ +struct flat_file +{ + char *filename; /**< Name of file within the directory */ + str table; /**< Table name the file belongs to */ + FILE *f; /**< File handle of the file */ }; @@ -60,11 +62,12 @@ struct flat_file { * file handles to files in that directory. The file handles are then used * from commands to write data in them. */ -struct flat_con { - db_pool_entry_t gen; /**< Generic part of the structure */ - struct flat_file* file; - int n; /**< Size of the file array */ - unsigned int flags; /**< Flags */ +struct flat_con +{ + db_pool_entry_t gen; /**< Generic part of the structure */ + struct flat_file *file; + int n; /**< Size of the file array */ + unsigned int flags; /**< Flags */ }; @@ -75,16 +78,16 @@ struct flat_con { * @retval 0 on success * @retval A negative number on error */ -int flat_con(db_con_t* con); +int flat_con(db_con_t *con); -int flat_con_connect(db_con_t* con); +int flat_con_connect(db_con_t *con); -void flat_con_disconnect(db_con_t* con); +void flat_con_disconnect(db_con_t *con); -int flat_open_table(int *idx, db_con_t* con, str* name); +int flat_open_table(int *idx, db_con_t *con, str *name); /** @} */ diff --git a/src/modules/db_flatstore/flat_rpc.c b/src/modules/db_flatstore/flat_rpc.c index 075c409df44..933720fe4be 100644 --- a/src/modules/db_flatstore/flat_rpc.c +++ b/src/modules/db_flatstore/flat_rpc.c @@ -40,22 +40,20 @@ * request, it will be carried out next time SER attempts to write new data * into the file. */ -static void rotate(rpc_t* rpc, void* c) +static void rotate(rpc_t *rpc, void *c) { *km_flat_rotate = time(0); *flat_rotate = time(0); } -static const char* flat_rotate_doc[2] = { - "Close and reopen flatrotate files during log rotation.", - 0 -}; +static const char *flat_rotate_doc[2] = { + "Close and reopen flatrotate files during log rotation.", 0}; rpc_export_t flat_rpc[] = { - {"flatstore.rotate", rotate, flat_rotate_doc, 0}, - {0, 0, 0, 0}, + {"flatstore.rotate", rotate, flat_rotate_doc, 0}, + {0, 0, 0, 0}, }; /** @} */ diff --git a/src/modules/db_flatstore/flat_uri.c b/src/modules/db_flatstore/flat_uri.c index 175e9e6e845..1ea2eacd070 100644 --- a/src/modules/db_flatstore/flat_uri.c +++ b/src/modules/db_flatstore/flat_uri.c @@ -36,29 +36,32 @@ #include -static void flat_uri_free(db_uri_t* uri, struct flat_uri* payload) +static void flat_uri_free(db_uri_t *uri, struct flat_uri *payload) { - if (payload == NULL) return; - if (payload->path.s) free(payload->path.s); + if(payload == NULL) + return; + if(payload->path.s) + free(payload->path.s); db_drv_free(&payload->drv); pkg_free(payload); } -int flat_uri(db_uri_t* uri) +int flat_uri(db_uri_t *uri) { - struct flat_uri* furi; + struct flat_uri *furi; - if ((furi = (struct flat_uri*)pkg_malloc(sizeof(*furi))) == NULL) { + if((furi = (struct flat_uri *)pkg_malloc(sizeof(*furi))) == NULL) { ERR("flatstore: No memory left\n"); return -1; } memset(furi, '\0', sizeof(*furi)); - if (db_drv_init(&furi->drv, flat_uri_free) < 0) goto error; + if(db_drv_init(&furi->drv, flat_uri_free) < 0) + goto error; - if ((furi->path.s = get_abs_pathname(NULL, &uri->body)) == NULL) { + if((furi->path.s = get_abs_pathname(NULL, &uri->body)) == NULL) { ERR("flatstore: Error while obtaining absolute pathname for '%.*s'\n", - STR_FMT(&uri->body)); + STR_FMT(&uri->body)); goto error; } furi->path.len = strlen(furi->path.s); @@ -66,13 +69,14 @@ int flat_uri(db_uri_t* uri) DB_SET_PAYLOAD(uri, furi); return 0; - error: - if (furi) { - if (furi->path.s) pkg_free(furi->path.s); +error: + if(furi) { + if(furi->path.s) + pkg_free(furi->path.s); db_drv_free(&furi->drv); pkg_free(furi); } - return -1; + return -1; } /** @} */ diff --git a/src/modules/db_flatstore/flat_uri.h b/src/modules/db_flatstore/flat_uri.h index 17f15d4a88a..0b4e8ccf654 100644 --- a/src/modules/db_flatstore/flat_uri.h +++ b/src/modules/db_flatstore/flat_uri.h @@ -38,10 +38,11 @@ * db_uri structures in the database API in SER. The structure is used to * convert relative pathnames in flatstore URIs to absolute. */ -struct flat_uri { +struct flat_uri +{ db_drv_t drv; /** Absolute pathname to the database directory, zero terminated */ - str path; + str path; }; @@ -52,7 +53,7 @@ struct flat_uri { * @retval 0 on success * @retval A negative number on error. */ -int flat_uri(db_uri_t* uri); +int flat_uri(db_uri_t *uri); /** @} */ diff --git a/src/modules/db_flatstore/km_flat_con.c b/src/modules/db_flatstore/km_flat_con.c index e91b2c3c02c..10e7ce1ea81 100644 --- a/src/modules/db_flatstore/km_flat_con.c +++ b/src/modules/db_flatstore/km_flat_con.c @@ -32,29 +32,29 @@ #define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1) /* returns a pkg_malloc'ed file name */ -static char* get_name(struct flat_id* id) +static char *get_name(struct flat_id *id) { - char* buf; + char *buf; int buf_len; - char* num, *ptr; + char *num, *ptr; int num_len; int total_len; - buf_len=pathmax(); - if (!id) { + buf_len = pathmax(); + if(!id) { LM_ERR("invalid parameter value\n"); return 0; } - total_len=id->dir.len+1 /* / */+id->table.len+1 /* _ */+ - FILE_SUFFIX_LEN+1 /* \0 */; /* without pid*/ - if (buf_lendir.len + 1 /* / */ + id->table.len + + 1 /* _ */ + FILE_SUFFIX_LEN + 1 /* \0 */; /* without pid*/ + if(buf_len < total_len) { + LM_ERR("the path is too long (%d and PATHMAX is %d)\n", total_len, + buf_len); return 0; } - - buf=pkg_malloc(buf_len); - if (buf==0){ + + buf = pkg_malloc(buf_len); + if(buf == 0) { PKG_MEM_ERROR; return 0; } @@ -69,11 +69,12 @@ static char* get_name(struct flat_id* id) ptr += id->table.len; *ptr++ = '_'; - + num = int2str(km_flat_pid, &num_len); - if (buf_len<(total_len+num_len)){ + if(buf_len < (total_len + num_len)) { LM_ERR("the path is too long (%d and PATHMAX is" - " %d)\n", total_len+num_len, buf_len); + " %d)\n", + total_len + num_len, buf_len); pkg_free(buf); return 0; } @@ -88,30 +89,30 @@ static char* get_name(struct flat_id* id) } -struct flat_con* flat_new_connection(struct flat_id* id) +struct flat_con *flat_new_connection(struct flat_id *id) { - char* fn; + char *fn; - struct flat_con* res; + struct flat_con *res; - if (!id) { + if(!id) { LM_ERR("invalid parameter value\n"); return 0; } - res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con)); - if (!res) { + res = (struct flat_con *)pkg_malloc(sizeof(struct flat_con)); + if(!res) { PKG_MEM_ERROR; return 0; } memset(res, 0, sizeof(struct flat_con)); res->ref = 1; - + res->id = id; fn = get_name(id); - if (fn==0){ + if(fn == 0) { LM_ERR("get_name() failed\n"); pkg_free(res); return 0; @@ -119,12 +120,12 @@ struct flat_con* flat_new_connection(struct flat_id* id) res->file = fopen(fn, "a"); pkg_free(fn); /* we don't need fn anymore */ - if (!res->file) { + if(!res->file) { LM_ERR(" %s\n", strerror(errno)); pkg_free(res); return 0; } - + return res; } @@ -132,11 +133,13 @@ struct flat_con* flat_new_connection(struct flat_id* id) /* * Close the connection and release memory */ -void flat_free_connection(struct flat_con* con) +void flat_free_connection(struct flat_con *con) { - if (!con) return; - if (con->id) free_flat_id(con->id); - if (con->file) { + if(!con) + return; + if(con->id) + free_flat_id(con->id); + if(con->file) { fclose(con->file); } pkg_free(con); @@ -146,21 +149,21 @@ void flat_free_connection(struct flat_con* con) /* * Reopen a connection */ -int flat_reopen_connection(struct flat_con* con) +int flat_reopen_connection(struct flat_con *con) { - char* fn; + char *fn; - if (!con) { + if(!con) { LM_ERR("invalid parameter value\n"); return -1; } - if (con->file) { + if(con->file) { fclose(con->file); con->file = 0; fn = get_name(con->id); - if (fn == 0) { + if(fn == 0) { LM_ERR("failed to get_name\n"); return -1; } @@ -168,7 +171,7 @@ int flat_reopen_connection(struct flat_con* con) con->file = fopen(fn, "a"); pkg_free(fn); - if (!con->file) { + if(!con->file) { LM_ERR("invalid parameter value\n"); return -1; } diff --git a/src/modules/db_flatstore/km_flat_con.h b/src/modules/db_flatstore/km_flat_con.h index b794072d4af..1063c6e5972 100644 --- a/src/modules/db_flatstore/km_flat_con.h +++ b/src/modules/db_flatstore/km_flat_con.h @@ -27,37 +27,38 @@ #include #include "km_flat_id.h" -struct flat_con { - struct flat_id* id; /* Connection identifier */ - int ref; /* Reference count */ - FILE* file; /* File descriptor structure */ - struct flat_con* next; /* Next connection in the pool */ +struct flat_con +{ + struct flat_id *id; /* Connection identifier */ + int ref; /* Reference count */ + FILE *file; /* File descriptor structure */ + struct flat_con *next; /* Next connection in the pool */ }; /* * Some convenience wrappers */ -#define CON_FILE(db_con) (((struct flat_con*)((db_con)->tail))->file) +#define CON_FILE(db_con) (((struct flat_con *)((db_con)->tail))->file) /* * Create a new connection structure, * open the MySQL connection and set reference count to 1 */ -struct flat_con* flat_new_connection(struct flat_id* id); +struct flat_con *flat_new_connection(struct flat_id *id); /* * Close the connection and release memory */ -void flat_free_connection(struct flat_con* con); +void flat_free_connection(struct flat_con *con); /* * Reopen a connection */ -int flat_reopen_connection(struct flat_con* con); +int flat_reopen_connection(struct flat_con *con); #endif /* _KM_FLAT_CON_H */ diff --git a/src/modules/db_flatstore/km_flat_id.c b/src/modules/db_flatstore/km_flat_id.c index c39f8ce17f9..413132e0d4e 100644 --- a/src/modules/db_flatstore/km_flat_id.c +++ b/src/modules/db_flatstore/km_flat_id.c @@ -26,23 +26,22 @@ #include "km_flat_id.h" - /* * Create a new connection identifier */ -struct flat_id* new_flat_id(char* dir, char* table) +struct flat_id *new_flat_id(char *dir, char *table) { - struct flat_id* ptr; - char* t; + struct flat_id *ptr; + char *t; int t_len; - if (!dir || !table) { + if(!dir || !table) { LM_ERR("invalid parameter(s)\n"); return 0; } - ptr = (struct flat_id*)pkg_malloc(sizeof(struct flat_id)); - if (!ptr) { + ptr = (struct flat_id *)pkg_malloc(sizeof(struct flat_id)); + if(!ptr) { PKG_MEM_ERROR; return 0; } @@ -50,8 +49,8 @@ struct flat_id* new_flat_id(char* dir, char* table) /* alloc memory for the table name */ t_len = strlen(table); - t = (char*)pkg_malloc(t_len+1); - if (!t) { + t = (char *)pkg_malloc(t_len + 1); + if(!t) { PKG_MEM_ERROR; pkg_free(ptr); return 0; @@ -72,14 +71,19 @@ struct flat_id* new_flat_id(char* dir, char* table) /* * Compare two connection identifiers */ -unsigned char cmp_flat_id(struct flat_id* id1, struct flat_id* id2) +unsigned char cmp_flat_id(struct flat_id *id1, struct flat_id *id2) { - if (!id1 || !id2) return 0; - if (id1->dir.len != id2->dir.len) return 0; - if (id1->table.len != id2->table.len) return 0; + if(!id1 || !id2) + return 0; + if(id1->dir.len != id2->dir.len) + return 0; + if(id1->table.len != id2->table.len) + return 0; - if (memcmp(id1->dir.s, id2->dir.s, id1->dir.len)) return 0; - if (memcmp(id1->table.s, id2->table.s, id1->table.len)) return 0; + if(memcmp(id1->dir.s, id2->dir.s, id1->dir.len)) + return 0; + if(memcmp(id1->table.s, id2->table.s, id1->table.len)) + return 0; return 1; } @@ -87,10 +91,11 @@ unsigned char cmp_flat_id(struct flat_id* id1, struct flat_id* id2) /* * Free a connection identifier */ -void free_flat_id(struct flat_id* id) +void free_flat_id(struct flat_id *id) { - if (!id) return; - if (id->table.s) + if(!id) + return; + if(id->table.s) pkg_free(id->table.s); pkg_free(id); } diff --git a/src/modules/db_flatstore/km_flat_id.h b/src/modules/db_flatstore/km_flat_id.h index 98d09c8a9b1..e0c6711bfc7 100644 --- a/src/modules/db_flatstore/km_flat_id.h +++ b/src/modules/db_flatstore/km_flat_id.h @@ -26,8 +26,9 @@ #include "../../core/str.h" -struct flat_id { - str dir; /* Database directory */ +struct flat_id +{ + str dir; /* Database directory */ str table; /* Name of table */ }; @@ -35,19 +36,19 @@ struct flat_id { /* * Create a new connection identifier */ -struct flat_id* new_flat_id(char* dir, char* table); +struct flat_id *new_flat_id(char *dir, char *table); /* * Compare two connection identifiers */ -unsigned char cmp_flat_id(struct flat_id* id1, struct flat_id* id2); +unsigned char cmp_flat_id(struct flat_id *id1, struct flat_id *id2); /* * Free a connection identifier */ -void free_flat_id(struct flat_id* id); +void free_flat_id(struct flat_id *id); #endif /* _KM_FLAT_ID_H */ diff --git a/src/modules/db_flatstore/km_flat_pool.c b/src/modules/db_flatstore/km_flat_pool.c index de135267ed8..f1e23ad3a87 100644 --- a/src/modules/db_flatstore/km_flat_pool.c +++ b/src/modules/db_flatstore/km_flat_pool.c @@ -27,7 +27,7 @@ /* The head of the pool */ -static struct flat_con* pool = 0; +static struct flat_con *pool = 0; /* * Pid of the process that added the last @@ -37,37 +37,37 @@ static struct flat_con* pool = 0; static int pool_pid; - /* * Get a connection from the pool, reuse existing * if possible, otherwise create a new one */ -struct flat_con* flat_get_connection(char* dir, char* table) +struct flat_con *flat_get_connection(char *dir, char *table) { - struct flat_id* id; - struct flat_con* ptr; + struct flat_id *id; + struct flat_con *ptr; int pid; - if (!dir || !table) { + if(!dir || !table) { LM_ERR("invalid parameter value\n"); return 0; } pid = getpid(); - if (pool && (pool_pid != pid)) { + if(pool && (pool_pid != pid)) { LM_ERR("inherited open database connections, " - "this is not a good idea\n"); + "this is not a good idea\n"); return 0; } pool_pid = pid; id = new_flat_id(dir, table); - if (!id) return 0; + if(!id) + return 0; ptr = pool; - while (ptr) { - if (cmp_flat_id(id, ptr->id)) { + while(ptr) { + if(cmp_flat_id(id, ptr->id)) { LM_DBG("connection found in the pool\n"); ptr->ref++; free_flat_id(id); @@ -78,7 +78,7 @@ struct flat_con* flat_get_connection(char* dir, char* table) LM_DBG("connection not found in the pool\n"); ptr = flat_new_connection(id); - if (!ptr) { + if(!ptr) { free_flat_id(id); return 0; } @@ -94,14 +94,15 @@ struct flat_con* flat_get_connection(char* dir, char* table) * in the pool if ref count != 0, otherwise it * will be deleted completely */ -void flat_release_connection(struct flat_con* con) +void flat_release_connection(struct flat_con *con) { - struct flat_con* ptr; + struct flat_con *ptr; - if (!con) return; + if(!con) + return; - if (con->ref > 1) { - /* There are still other users, just + if(con->ref > 1) { + /* There are still other users, just * decrease the reference count and return */ LM_DBG("connection still kept in the pool\n"); @@ -111,18 +112,19 @@ void flat_release_connection(struct flat_con* con) LM_DBG("removing connection from the pool\n"); - if (pool == con) { + if(pool == con) { pool = pool->next; } else { ptr = pool; while(ptr) { - if (ptr->next == con) break; + if(ptr->next == con) + break; ptr = ptr->next; } - if (!ptr) { + if(!ptr) { LM_ERR("weird, connection not found in the pool\n"); } else { - /* Remove the connection from the pool */ + /* Remove the connection from the pool */ ptr->next = con->next; } } @@ -136,11 +138,11 @@ void flat_release_connection(struct flat_con* con) */ int flat_rotate_logs(void) { - struct flat_con* ptr; + struct flat_con *ptr; ptr = pool; while(ptr) { - if (flat_reopen_connection(ptr)) { + if(flat_reopen_connection(ptr)) { return -1; } ptr = ptr->next; diff --git a/src/modules/db_flatstore/km_flat_pool.h b/src/modules/db_flatstore/km_flat_pool.h index b305010a2ee..dcdf1db477c 100644 --- a/src/modules/db_flatstore/km_flat_pool.h +++ b/src/modules/db_flatstore/km_flat_pool.h @@ -29,7 +29,7 @@ * Get a connection from the pool, reuse existing * if possible, otherwise create a new one */ -struct flat_con* flat_get_connection(char* dir, char* table); +struct flat_con *flat_get_connection(char *dir, char *table); /* @@ -37,7 +37,7 @@ struct flat_con* flat_get_connection(char* dir, char* table); * in the pool if ref count != 0, otherwise it * will be deleted completely */ -void flat_release_connection(struct flat_con* con); +void flat_release_connection(struct flat_con *con); /* diff --git a/src/modules/db_flatstore/km_flatstore.c b/src/modules/db_flatstore/km_flatstore.c index b933479ad0e..5d9d7d84cd2 100644 --- a/src/modules/db_flatstore/km_flatstore.c +++ b/src/modules/db_flatstore/km_flatstore.c @@ -32,9 +32,9 @@ #include "db_flatstore.h" -static int parse_flat_url(const str* url, str* path) +static int parse_flat_url(const str *url, str *path) { - if (!url || !url->s || !path) { + if(!url || !url->s || !path) { LM_ERR("invalid parameter value\n"); return -1; } @@ -44,17 +44,16 @@ static int parse_flat_url(const str* url, str* path) } - /* * Initialize database module * No function should be called before this */ -db1_con_t* flat_db_init(const str* url) +db1_con_t *flat_db_init(const str *url) { - db1_con_t* res; - str* path; + db1_con_t *res; + str *path; - if (!url || !url->s) { + if(!url || !url->s) { LM_ERR("invalid parameter value\n"); return 0; } @@ -67,15 +66,17 @@ db1_con_t* flat_db_init(const str* url) /* as the table (path) is a substring of the received str, we need to * allocate a separate str struct for it -bogdan */ - res = pkg_malloc(sizeof(db1_con_t)+sizeof(struct flat_con*)+sizeof(str)); - if (!res) { + res = pkg_malloc( + sizeof(db1_con_t) + sizeof(struct flat_con *) + sizeof(str)); + if(!res) { PKG_MEM_ERROR; return 0; } - memset(res, 0, sizeof(db1_con_t) + sizeof(struct flat_con*) + sizeof(str)); - path = (str*)(((char*)res) + sizeof(db1_con_t) + sizeof(struct flat_con*)); + memset(res, 0, sizeof(db1_con_t) + sizeof(struct flat_con *) + sizeof(str)); + path = (str *)(((char *)res) + sizeof(db1_con_t) + + sizeof(struct flat_con *)); - if (parse_flat_url(url, path) < 0) { + if(parse_flat_url(url, path) < 0) { pkg_free(res); return 0; } @@ -89,48 +90,48 @@ db1_con_t* flat_db_init(const str* url) * Store name of table that will be used by * subsequent database functions */ -int flat_use_table(db1_con_t* h, const str* t) +int flat_use_table(db1_con_t *h, const str *t) { - struct flat_con* con; + struct flat_con *con; - if (!h || !t || !t->s) { + if(!h || !t || !t->s) { LM_ERR("invalid parameter value\n"); return -1; } - if (CON_TABLE(h)->s != t->s) { - if (CON_TAIL(h)) { + if(CON_TABLE(h)->s != t->s) { + if(CON_TAIL(h)) { /* Decrement the reference count * of the connection but do not remove * it from the connection pool */ - con = (struct flat_con*)CON_TAIL(h); + con = (struct flat_con *)CON_TAIL(h); con->ref--; } - CON_TAIL(h) = (unsigned long) - flat_get_connection((char*)CON_TABLE(h)->s, (char*)t->s); - if (!CON_TAIL(h)) { + CON_TAIL(h) = (unsigned long)flat_get_connection( + (char *)CON_TABLE(h)->s, (char *)t->s); + if(!CON_TAIL(h)) { return -1; } } - + return 0; } -void flat_db_close(db1_con_t* h) +void flat_db_close(db1_con_t *h) { - struct flat_con* con; + struct flat_con *con; - if (!h) { + if(!h) { LM_ERR("invalid parameter value\n"); return; } - con = (struct flat_con*)CON_TAIL(h); + con = (struct flat_con *)CON_TAIL(h); - if (con) { + if(con) { flat_release_connection(con); } pkg_free(h); @@ -144,103 +145,103 @@ void flat_db_close(db1_con_t* h) * v: values of the keys * n: number of key=value pairs */ -int flat_db_insert(const db1_con_t* h, const db_key_t* k, const db_val_t* v, - const int n) +int flat_db_insert( + const db1_con_t *h, const db_key_t *k, const db_val_t *v, const int n) { - FILE* f; + FILE *f; int i; int l; char *s, *p; - if (km_local_timestamp < *km_flat_rotate) { + if(km_local_timestamp < *km_flat_rotate) { flat_rotate_logs(); km_local_timestamp = *km_flat_rotate; } f = CON_FILE(h); - if (!f) { + if(!f) { LM_ERR("uninitialized connection\n"); return -1; } for(i = 0; i < n; i++) { switch(VAL_TYPE(v + i)) { - case DB1_INT: - fprintf(f, "%d", VAL_INT(v + i)); - break; - - case DB1_UINT: - fprintf(f, "%u", VAL_UINT(v + i)); - break; - - case DB1_BIGINT: - fprintf(f, "%lld", VAL_BIGINT(v + i)); - break; - - case DB1_UBIGINT: - fprintf(f, "%llu", VAL_UBIGINT(v + i)); - break; - - case DB1_DOUBLE: - fprintf(f, "%f", VAL_DOUBLE(v + i)); - break; - - case DB1_STRING: - fprintf(f, "%s", VAL_STRING(v + i)); - break; - - case DB1_STR: - if (!encode_delimiter) { - fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s); - } else { - s = VAL_STR(v + i).s; - l = VAL_STR(v + i).len; - while (l--) { - if (*s == *flat_delimiter.s) { - fprintf(f, "%%%02X", (*s & 0xff) ); - } else { - fprintf(f, "%c", *s); + case DB1_INT: + fprintf(f, "%d", VAL_INT(v + i)); + break; + + case DB1_UINT: + fprintf(f, "%u", VAL_UINT(v + i)); + break; + + case DB1_BIGINT: + fprintf(f, "%lld", VAL_BIGINT(v + i)); + break; + + case DB1_UBIGINT: + fprintf(f, "%llu", VAL_UBIGINT(v + i)); + break; + + case DB1_DOUBLE: + fprintf(f, "%f", VAL_DOUBLE(v + i)); + break; + + case DB1_STRING: + fprintf(f, "%s", VAL_STRING(v + i)); + break; + + case DB1_STR: + if(!encode_delimiter) { + fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s); + } else { + s = VAL_STR(v + i).s; + l = VAL_STR(v + i).len; + while(l--) { + if(*s == *flat_delimiter.s) { + fprintf(f, "%%%02X", (*s & 0xff)); + } else { + fprintf(f, "%c", *s); + } + ++s; } - ++s; } - } - break; - - case DB1_DATETIME: - fprintf(f, "%llu", (unsigned long long)VAL_TIME(v + i)); - break; - - case DB1_BLOB: - l = VAL_BLOB(v+i).len; - s = p = VAL_BLOB(v+i).s; - while (l--) { - if ( !(isprint((int)*s) && *s != '\\' && *s != '|')) { - fprintf(f,"%.*s\\x%02X",(int)(s-p),p,(*s & 0xff)); - p = s+1; + break; + + case DB1_DATETIME: + fprintf(f, "%llu", (unsigned long long)VAL_TIME(v + i)); + break; + + case DB1_BLOB: + l = VAL_BLOB(v + i).len; + s = p = VAL_BLOB(v + i).s; + while(l--) { + if(!(isprint((int)*s) && *s != '\\' && *s != '|')) { + fprintf(f, "%.*s\\x%02X", (int)(s - p), p, (*s & 0xff)); + p = s + 1; + } + ++s; } - ++s; - } - if (p!=s) - fprintf(f,"%.*s",(int)(s-p),p); - break; - - case DB1_BITMAP: - fprintf(f, "%u", VAL_BITMAP(v + i)); - break; - - default: - LM_ERR("val type [%d] not supported", VAL_TYPE(v + i)); - return -1; + if(p != s) + fprintf(f, "%.*s", (int)(s - p), p); + break; + + case DB1_BITMAP: + fprintf(f, "%u", VAL_BITMAP(v + i)); + break; + + default: + LM_ERR("val type [%d] not supported", VAL_TYPE(v + i)); + return -1; } - if (i < (n - 1)) { + if(i < (n - 1)) { fprintf(f, "%c", *km_flat_delimiter); } } fprintf(f, "\n"); - if (flat_flush) { + if(flat_flush) { fflush(f); } diff --git a/src/modules/db_flatstore/km_flatstore.h b/src/modules/db_flatstore/km_flatstore.h index 6461f30a10c..8ea94b19e1e 100644 --- a/src/modules/db_flatstore/km_flatstore.h +++ b/src/modules/db_flatstore/km_flatstore.h @@ -34,17 +34,17 @@ extern int encode_delimiter; * Initialize database module * No function should be called before this */ -db1_con_t* flat_db_init(const str* _url); +db1_con_t *flat_db_init(const str *_url); /* * Store name of table that will be used by * subsequent database functions */ -int flat_use_table(db1_con_t* h, const str* t); +int flat_use_table(db1_con_t *h, const str *t); -void flat_db_close(db1_con_t* h); +void flat_db_close(db1_con_t *h); /* @@ -54,8 +54,8 @@ void flat_db_close(db1_con_t* h); * v: values of the keys * n: number of key=value pairs */ -int flat_db_insert(const db1_con_t* h, const db_key_t* k, const db_val_t* v, - const int n); +int flat_db_insert( + const db1_con_t *h, const db_key_t *k, const db_val_t *v, const int n); #endif /* _KM_FLATSTORE_H */ diff --git a/src/modules/db_flatstore/km_flatstore_mod.c b/src/modules/db_flatstore/km_flatstore_mod.c index 67d5912660c..d08f9fc826c 100644 --- a/src/modules/db_flatstore/km_flatstore_mod.c +++ b/src/modules/db_flatstore/km_flatstore_mod.c @@ -42,14 +42,14 @@ int km_flat_pid; /* * Delimiter delimiting columns */ -char* km_flat_delimiter = "|"; +char *km_flat_delimiter = "|"; /* * Timestamp of the last log rotation request from * the FIFO interface */ -time_t* km_flat_rotate; +time_t *km_flat_rotate; time_t km_local_timestamp; @@ -57,47 +57,44 @@ time_t km_local_timestamp; * Flatstore database module interface */ static cmd_export_t cmds[] = { - {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0} -}; + {"db_bind_api", (cmd_function)db_flat_bind_api, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}}; /* * Exported parameters */ -static param_export_t params[] = { - {0, 0, 0} -}; +static param_export_t params[] = {{0, 0, 0}}; static rpc_export_t k_rpc_methods[]; struct module_exports km_exports = { - "db_flatstore", /* module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* exported functions */ - params, /* exported parameters */ - 0, /* RPC method exports */ - 0, /* exported pseudo-variables */ - 0, /* response handling function */ - km_mod_init, /* module initialization function */ - km_child_init, /* per-child init function */ - km_mod_destroy /* module destroy function */ + "db_flatstore", /* module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* exported functions */ + params, /* exported parameters */ + 0, /* RPC method exports */ + 0, /* exported pseudo-variables */ + 0, /* response handling function */ + km_mod_init, /* module initialization function */ + km_child_init, /* per-child init function */ + km_mod_destroy /* module destroy function */ }; int km_mod_init(void) { - if (rpc_register_array(k_rpc_methods)!=0) { + if(rpc_register_array(k_rpc_methods) != 0) { LM_ERR("failed to register RPC commands\n"); return -1; } - if (strlen(km_flat_delimiter) != 1) { + if(strlen(km_flat_delimiter) != 1) { LM_ERR("delimiter has to be exactly one character\n"); return -1; } - km_flat_rotate = (time_t*)shm_malloc(sizeof(time_t)); - if (!km_flat_rotate) { + km_flat_rotate = (time_t *)shm_malloc(sizeof(time_t)); + if(!km_flat_rotate) { SHM_MEM_ERROR; return -1; } @@ -111,14 +108,15 @@ int km_mod_init(void) void km_mod_destroy(void) { - if (km_flat_rotate) shm_free(km_flat_rotate); + if(km_flat_rotate) + shm_free(km_flat_rotate); } int km_child_init(int rank) { - if (rank <= 0) { - km_flat_pid = - rank; + if(rank <= 0) { + km_flat_pid = -rank; } else { km_flat_pid = rank - PROC_MIN; } @@ -127,23 +125,22 @@ int km_child_init(int rank) int db_flat_bind_api(db_func_t *dbb) { - if(dbb==NULL) + if(dbb == NULL) return -1; memset(dbb, 0, sizeof(db_func_t)); - dbb->use_table = flat_use_table; - dbb->init = flat_db_init; - dbb->close = flat_db_close; - dbb->insert = flat_db_insert; + dbb->use_table = flat_use_table; + dbb->init = flat_db_init; + dbb->close = flat_db_close; + dbb->insert = flat_db_insert; return 0; } /* rpc function documentation */ static const char *rpc_k_rotate_doc[2] = { - "Close and reopen flatrotate files during log rotation.", 0 -}; + "Close and reopen flatrotate files during log rotation.", 0}; /* rpc function implementations */ static void rpc_k_rotate(rpc_t *rpc, void *c) @@ -153,7 +150,6 @@ static void rpc_k_rotate(rpc_t *rpc, void *c) } static rpc_export_t k_rpc_methods[] = { - {"flatstore.k_rotate", rpc_k_rotate, rpc_k_rotate_doc, 0}, - {0, 0, 0, 0}, + {"flatstore.k_rotate", rpc_k_rotate, rpc_k_rotate_doc, 0}, + {0, 0, 0, 0}, }; - diff --git a/src/modules/db_flatstore/km_flatstore_mod.h b/src/modules/db_flatstore/km_flatstore_mod.h index 0a4d7071946..3a510f7e244 100644 --- a/src/modules/db_flatstore/km_flatstore_mod.h +++ b/src/modules/db_flatstore/km_flatstore_mod.h @@ -37,14 +37,14 @@ extern int km_flat_pid; /* * Delmiter delimiting columns */ -extern char* km_flat_delimiter; +extern char *km_flat_delimiter; /* * The timestamp of log rotation request from * the FIFO interface */ -extern time_t* km_flat_rotate; +extern time_t *km_flat_rotate; /*