From 7030e51bb22e62e6f5d63b0faa923d2151336818 Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Sun, 26 Dec 2010 19:21:32 -0500
Subject: [PATCH] [INI] 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     |  121 ++++++++++++++++++++-----------------------------
 3 files changed, 75 insertions(+), 77 deletions(-)

diff --git a/ini/ini_config_priv.h b/ini/ini_config_priv.h
index 1880c3ff4caa91e7ed3a37e445a20294e76d8037..84742c70ceeb5b493b935c635e81c57d5ef487ce 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 <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #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 3321bd3ea8985b9b12f04ab792fbcac307bbbe2e..898440252ef3a547566fe2b5e80753d7971a8e47 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 200c7af9de896c1abd26a57a508f9f540475b19d..c10b4bf954366ddae90f1d06c53acd0332968599 100644
--- a/ini/ini_fileobj.c
+++ b/ini/ini_fileobj.c
@@ -96,7 +96,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);
     }
@@ -104,6 +103,47 @@ 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;
+        }
+    }
+
+    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,
@@ -135,16 +175,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;
@@ -172,39 +205,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;
@@ -236,12 +244,6 @@ int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in,
     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 = file_ctx_in->error_level;
@@ -259,39 +261,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_close(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;
-- 
1.5.5.6

