From 6a900a750311a0aec2aa627468f7abe0f57d57cd Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Wed, 31 Mar 2010 19:21:35 -0400
Subject: [PATCH] [INI] Addinig content to the metadata

This patch implements function that collects
stats and saves them in the ACCESS section
inside metadata.
---
 common/ini/ini_config.c   |    5 ++-
 common/ini/ini_config.h   |   18 +++----
 common/ini/ini_metadata.c |  127 ++++++++++++++++++++++++++++++++++++++++++++-
 common/ini/ini_metadata.h |    3 +-
 4 files changed, 140 insertions(+), 13 deletions(-)

diff --git a/common/ini/ini_config.c b/common/ini/ini_config.c
index 9015590f5be80abe9a97a9c4374e90c7a842590e..76b456e62664e2dd051a41be0d1cd596a180385d 100644
--- a/common/ini/ini_config.c
+++ b/common/ini/ini_config.c
@@ -717,7 +717,10 @@ int config_from_fd_with_metadata(const char *application,
     }
 
     /* Collect meta data before actually parsing the file */
-    error = collect_metadata(metaflags, metadata, config_file);
+    error = collect_metadata(metaflags,
+                             metadata,
+                             config_file,
+                             config_filename);
     if(error) {
         TRACE_ERROR_NUMBER("Failed to collect metadata", error);
         return error;
diff --git a/common/ini/ini_config.h b/common/ini/ini_config.h
index bd2bdd998d9e948bcbc2f99fc469b8ad6eb5ad32..c0f82d0f14a8ce48b7c01e0fd2adbf75d5555d9c 100644
--- a/common/ini/ini_config.h
+++ b/common/ini/ini_config.h
@@ -382,10 +382,16 @@ struct parse_error {
 #define INI_META_KEY_PERM     "perm"
 
 /**
- * @brief The value for this key will store INI file creation time stamp.
+ * @brief The value for this key will store INI file device ID.
  *
  */
-#define INI_META_KEY_CREATED     "created"
+#define INI_META_KEY_DEV     "dev"
+
+/**
+ * @brief The value for this key will store INI file inode number.
+ *
+ */
+#define INI_META_KEY_INODE     "inode"
 
 /**
  * @brief The value for this key will store INI file modification time stamp.
@@ -430,14 +436,6 @@ struct parse_error {
  */
 #define INI_META_KEY_READ_ERROR     "read_error"
 
-/**
- * @brief The value for this key will store read error message if any.
- *
- * If file was opened by caller first but this section was requested
- * the key will no be present. Also the key will no exist if no error
- * occured.
- */
-#define INI_META_KEY_READ_ERRMSG     "err_msg"
 
 /**
  * @}
diff --git a/common/ini/ini_metadata.c b/common/ini/ini_metadata.c
index 630de699b9726cb5e285fb33fb8aff7c8640d46c..a5d5109fc5b840c84b9678045c5193afd5599873 100644
--- a/common/ini/ini_metadata.c
+++ b/common/ini/ini_metadata.c
@@ -20,6 +20,10 @@
 */
 
 #define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
 #include "config.h"
 #include "collection.h"
 #include "collection_tools.h"
@@ -85,11 +89,132 @@ int prepare_metadata(uint32_t metaflags,
 /* Collect metadata for the file */
 int collect_metadata(uint32_t metaflags,
                      struct collection_item **metadata,
-                     FILE *config_file)
+                     FILE *config_file,
+                     const char *config_filename)
 {
     int error = EOK;
+    struct collection_item *metasec = NULL;
+    int filedes;
+    struct stat file_stats;
 
     TRACE_FLOW_STRING("collect_metadata", "Entry");
+    /* Check and create section for file error if needed */
+    if (metaflags & INI_META_SEC_ACCESS_FLAG) {
+        /* Create ACCESS collection */
+        error = col_create_collection(&metasec,
+                                      INI_META_SEC_ACCESS,
+                                      COL_CLASS_INI_SECTION);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to create access section.", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        filedes = fileno(config_file);
+
+        /* Collect statistics */
+        errno = 0;
+        if (fstat(filedes, &file_stats) < 0) {
+            error = errno;
+            TRACE_ERROR_NUMBER("Failed to get statistics.", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* Record statistics */
+        /* UID */
+        error = col_add_int_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_UID,
+                                     file_stats.st_uid);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save uid", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* GID */
+        error = col_add_int_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_GID,
+                                     file_stats.st_gid);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save gid", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* PERMISSIONS */
+        error = col_add_unsigned_property(metasec,
+                                          NULL,
+                                          INI_META_KEY_PERM,
+                                          file_stats.st_mode);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save permissions", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* Modification time stamp */
+        error = col_add_int_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_MODIFIED,
+                                     file_stats.st_mtime);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save modification time", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* Name */
+        error = col_add_str_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_NAME,
+                                     config_filename,
+                                     0);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save file name", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* Device  ID */
+        error = col_add_int_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_DEV,
+                                     file_stats.st_dev);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save inode", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* i-node */
+        error = col_add_int_property(metasec,
+                                     NULL,
+                                     INI_META_KEY_INODE,
+                                     file_stats.st_ino);
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save inode", error);
+            col_destroy_collection(metasec);
+            return error;
+        }
+
+        /* Add section to metadata */
+        error = col_add_collection_to_collection(
+                                    *metadata,
+                                    NULL,
+                                    NULL,
+                                    metasec,
+                                    COL_ADD_MODE_REFERENCE);
+
+        col_destroy_collection(metasec);
+
+        if (error) {
+            TRACE_ERROR_NUMBER("Failed to save file name", error);
+            return error;
+        }
+    }
 
     TRACE_FLOW_STRING("collect_metadata", "Exit");
     return error;
diff --git a/common/ini/ini_metadata.h b/common/ini/ini_metadata.h
index 2839453a66b35b286636ebdd2bf39f5358556623..7d766277ec563b92d22e351df2363fe14040063e 100644
--- a/common/ini/ini_metadata.h
+++ b/common/ini/ini_metadata.h
@@ -35,7 +35,8 @@ int prepare_metadata(uint32_t metaflags,
 /* Collect metadata for the file */
 int collect_metadata(uint32_t metaflags,
                      struct collection_item **metadata,
-                     FILE *config_file);
+                     FILE *config_file,
+                     const char *config_filename);
 
 
 
-- 
1.5.5.6

