From 89bd6c4dbb49ed8e6a540bb934ac7ac1ded1940e Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Sun, 18 Mar 2012 14:46:37 -0400 Subject: Metadata collection is gone After some more thinking I decided not to use metadata collection. It seems to be an overhead. Patch does following: * Replaces metadata collection in file context structure with standard file stats * Removes all operations against old metadata collection * Defines new flags for data to collect * Creates a function that consolidates common operations between open and reopen functions. --- ini/ini_config_priv.h | 11 ++--- ini/ini_configobj.h | 20 +++++++++ ini/ini_fileobj.c | 122 +++++++++++++++++++++----------------------------- 3 files changed, 76 insertions(+), 77 deletions(-) diff --git a/ini/ini_config_priv.h b/ini/ini_config_priv.h index 1880c3f..84742c7 100644 --- a/ini/ini_config_priv.h +++ b/ini/ini_config_priv.h @@ -22,6 +22,9 @@ #ifndef INI_CONFIG_PRIV_H #define INI_CONFIG_PRIV_H +#include +#include +#include #include "collection.h" /* Configuration object */ @@ -45,21 +48,19 @@ struct ini_cfgfile { char *filename; /* File stream */ FILE *file; - /* File descriptor that is passed in */ - int fd; /* Error level */ int error_level; /* Collision flags - define how to merge things */ uint32_t collision_flags; - /* Collision flags - define how to merge things */ + /* What meta data to collect */ uint32_t metadata_flags; /**********************/ /* Internal variables */ /**********************/ /* Collection of errors detected during parsing */ struct collection_item *error_list; - /* Metadata about the file */ - struct collection_item *metadata; + /* File stats */ + struct stat file_stats; /* Count of error lines */ unsigned count; }; diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h index 3321bd3..8984402 100644 --- a/ini/ini_configobj.h +++ b/ini/ini_configobj.h @@ -82,6 +82,26 @@ /** @brief Size of the error array. */ #define ERR_MAXPARSE ERR_DUPSECTION +/** + * @} + */ + +/** + * @defgroup metacollect Constants that define what meta data to collect + * + * Constants in this section define what meta data to collect + * + * + * @{ + */ +/** @brief Do not collect any data. */ +#define INI_META_NONE 0 +/** @brief Collect file stats. */ +#define INI_META_STATS 1 + +/** + * @} + */ /** * @defgroup collisionflags Flags that define collision resolution logic. diff --git a/ini/ini_fileobj.c b/ini/ini_fileobj.c index d667887..48730fc 100644 --- a/ini/ini_fileobj.c +++ b/ini/ini_fileobj.c @@ -99,7 +99,6 @@ void ini_config_file_destroy(struct ini_cfgfile *file_ctx) if(file_ctx) { free(file_ctx->filename); col_destroy_collection(file_ctx->error_list); - col_destroy_collection(file_ctx->metadata); if(file_ctx->file) fclose(file_ctx->file); free(file_ctx); } @@ -107,6 +106,48 @@ void ini_config_file_destroy(struct ini_cfgfile *file_ctx) TRACE_FLOW_EXIT(); } +/* Internal common initialization part */ +static int common_file_init(struct ini_cfgfile *file_ctx) +{ + int error = EOK; + + TRACE_FLOW_ENTRY(); + + /* Open file */ + TRACE_INFO_STRING("File", file_ctx->filename); + errno = 0; + file_ctx->file = fopen(file_ctx->filename, "r"); + if (!(file_ctx->file)) { + error = errno; + TRACE_ERROR_NUMBER("Failed to open file", error); + return error; + } + + /* Create internal collections */ + error = col_create_collection(&(file_ctx->error_list), + INI_ERROR, + COL_CLASS_INI_PERROR); + if (error) { + TRACE_ERROR_NUMBER("Failed to create error list", error); + return error; + } + + /* Collect stats */ + if (file_ctx->metadata_flags & INI_META_STATS) { + errno = 0; + if (fstat(fileno(file_ctx->file), + &(file_ctx->file_stats)) < 0) { + error = errno; + TRACE_ERROR_NUMBER("Failed to get file stats.", error); + return error; + } + } + else memset(&(file_ctx->file_stats), 0, sizeof(struct stat)); + + TRACE_FLOW_EXIT(); + return EOK; +} + /* Create a file object for parsing a config file */ int ini_config_file_open(const char *filename, int error_level, @@ -138,16 +179,9 @@ int ini_config_file_open(const char *filename, return error; } - new_ctx->filename = NULL; new_ctx->file = NULL; new_ctx->error_list = NULL; - new_ctx->metadata = NULL; - - /* TBD - decide whether we actually need an FD. - It will be done when we move the metadata - processing into this function. */ - new_ctx->fd = -1; /* Store flags */ new_ctx->error_level = error_level; @@ -175,39 +209,14 @@ int ini_config_file_open(const char *filename, return error; } - /* Open file */ - TRACE_INFO_STRING("File", new_ctx->filename); - errno = 0; - new_ctx->file = fopen(new_ctx->filename, "r"); - if (!(new_ctx->file)) { - error = errno; - TRACE_ERROR_NUMBER("Failed to open file", error); - ini_config_file_destroy(new_ctx); - return error; - } - - /* Create internal collections */ - error = col_create_collection(&(new_ctx->error_list), - INI_ERROR, - COL_CLASS_INI_PERROR); - if (error) { - TRACE_ERROR_NUMBER("Failed to create error list", error); - ini_config_file_destroy(new_ctx); - return error; - } - - error = col_create_collection(&(new_ctx->metadata), - INI_METADATA, - COL_CLASS_INI_META); - if (error) { - TRACE_ERROR_NUMBER("Failed to create metadata collection", error); + /* Do common init */ + error = common_file_init(new_ctx); + if(error) { + TRACE_ERROR_NUMBER("Failed to do common init", error); ini_config_file_destroy(new_ctx); return error; } - - /* TBD - Add metadata processing here */ - *file_ctx = new_ctx; TRACE_FLOW_EXIT(); return error; @@ -238,12 +247,6 @@ int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in, new_ctx->file = NULL; new_ctx->error_list = NULL; - new_ctx->metadata = NULL; - - /* TBD - decide whether we actually need an FD. - It will be done when we move the metadata - processing into this function. */ - new_ctx->fd = -1; /* Store flags */ new_ctx->error_level = file_ctx_in->error_level; @@ -261,39 +264,14 @@ int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in, return error; } - /* Open file */ - TRACE_INFO_STRING("File", new_ctx->filename); - errno = 0; - new_ctx->file = fopen(new_ctx->filename, "r"); - if (!(new_ctx->file)) { - error = errno; - TRACE_ERROR_NUMBER("Failed to open file", error); - ini_config_file_destroy(new_ctx); - return error; - } - - /* Create internal collections */ - error = col_create_collection(&(new_ctx->error_list), - INI_ERROR, - COL_CLASS_INI_PERROR); - if (error) { - TRACE_ERROR_NUMBER("Failed to create error list", error); - ini_config_file_destroy(new_ctx); - return error; - } - - error = col_create_collection(&(new_ctx->metadata), - INI_METADATA, - COL_CLASS_INI_META); - if (error) { - TRACE_ERROR_NUMBER("Failed to create metadata collection", error); + /* Do common init */ + error = common_file_init(new_ctx); + if(error) { + TRACE_ERROR_NUMBER("Failed to do common init", error); ini_config_file_destroy(new_ctx); return error; } - - /* TBD - Add metadata processing here */ - *file_ctx_out = new_ctx; TRACE_FLOW_EXIT(); return error; -- cgit