From 0632cce855be8c7a0203e8d10b8ff1e821d618e7 Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Sun, 18 Mar 2012 14:13:00 -0400
Subject: [PATCH 16/24] [INI] Separate close and destroy

Allow closing file without destroying the context.
---
 ini/ini_configobj.h |    5 ++++-
 ini/ini_fileobj.c   |   28 ++++++++++++++++++++++------
 ini/ini_parse_ut.c  |   10 +++++-----
 3 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h
index f0c6882..e728c2e 100644
--- a/ini/ini_configobj.h
+++ b/ini/ini_configobj.h
@@ -213,9 +213,12 @@ int ini_config_file_open(const char *filename,
                          uint32_t metadata_flags,
                          struct ini_cfgfile **file_ctx);
 
-/* Close file context and destroy the object */
+/* Close file context */
 void ini_config_file_close(struct ini_cfgfile *file_ctx);
 
+/* Close file context and destroy the object */
+void ini_config_file_destroy(struct ini_cfgfile *file_ctx);
+
 /* How many errors do we have in the list ? */
 unsigned ini_config_error_count(struct ini_cfgfile *file_ctx);
 
diff --git a/ini/ini_fileobj.c b/ini/ini_fileobj.c
index 7bc599b..09c35e3 100644
--- a/ini/ini_fileobj.c
+++ b/ini/ini_fileobj.c
@@ -75,12 +75,28 @@ static int valid_collision_flags(uint32_t collision_flags)
     return 1;
 }
 
-/* Close file context and destroy the object */
+
+/* Close file but not destroy the object */
 void ini_config_file_close(struct ini_cfgfile *file_ctx)
 {
     TRACE_FLOW_ENTRY();
 
     if(file_ctx) {
+        if(file_ctx->file) {
+            fclose(file_ctx->file);
+            file_ctx->file = NULL;
+        }
+    }
+
+    TRACE_FLOW_EXIT();
+}
+
+/* Close file context and destroy the object */
+void ini_config_file_destroy(struct ini_cfgfile *file_ctx)
+{
+    TRACE_FLOW_ENTRY();
+
+    if(file_ctx) {
         free(file_ctx->filename);
         col_destroy_collection(file_ctx->error_list);
         col_destroy_collection(file_ctx->metadata);
@@ -144,7 +160,7 @@ int ini_config_file_open(const char *filename,
     new_ctx->filename = malloc(PATH_MAX + 1);
     if (!(new_ctx->filename)) {
         error = errno;
-        ini_config_file_close(new_ctx);
+        ini_config_file_destroy(new_ctx);
         TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", error);
         return error;
     }
@@ -155,7 +171,7 @@ int ini_config_file_open(const char *filename,
                                           filename);
     if(error) {
         TRACE_ERROR_NUMBER("Failed to resolve path", error);
-        ini_config_file_close(new_ctx);
+        ini_config_file_destroy(new_ctx);
         return error;
     }
 
@@ -166,7 +182,7 @@ int ini_config_file_open(const char *filename,
     if (!(new_ctx->file)) {
         error = errno;
         TRACE_ERROR_NUMBER("Failed to open file", error);
-        ini_config_file_close(new_ctx);
+        ini_config_file_destroy(new_ctx);
         return error;
     }
 
@@ -176,7 +192,7 @@ int ini_config_file_open(const char *filename,
                                   COL_CLASS_INI_PERROR);
     if (error) {
         TRACE_ERROR_NUMBER("Failed to create error list", error);
-        ini_config_file_close(new_ctx);
+        ini_config_file_destroy(new_ctx);
         return error;
     }
 
@@ -185,7 +201,7 @@ int ini_config_file_open(const char *filename,
                                   COL_CLASS_INI_META);
     if (error) {
         TRACE_ERROR_NUMBER("Failed to create metadata collection", error);
-        ini_config_file_close(new_ctx);
+        ini_config_file_destroy(new_ctx);
         return error;
     }
 
diff --git a/ini/ini_parse_ut.c b/ini/ini_parse_ut.c
index a2bc4dd..5bb3e04 100644
--- a/ini/ini_parse_ut.c
+++ b/ini/ini_parse_ut.c
@@ -91,7 +91,7 @@ int test_one_file(const char *in_filename,
         /* We do not return here intentionally */
     }
 
-    ini_config_file_close(file_ctx);
+    ini_config_file_destroy(file_ctx);
 
     INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
 
@@ -381,13 +381,13 @@ int merge_values_test(void)
                 printf("This is unexpected error %d in mode %d\n", error, mflags[i]);
                 ini_config_destroy(ini_config);
                 simplebuffer_free(sbobj);
-                ini_config_file_close(file_ctx);
+                ini_config_file_destroy(file_ctx);
                 return error;
             }
             /* We do not return here intentionally */
         }
 
-        ini_config_file_close(file_ctx);
+        ini_config_file_destroy(file_ctx);
 
         INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
 
@@ -572,12 +572,12 @@ int merge_section_test(void)
                             error, msecflags[i], mflags[j]);
                     ini_config_destroy(ini_config);
                     simplebuffer_free(sbobj);
-                    ini_config_file_close(file_ctx);
+                    ini_config_file_destroy(file_ctx);
                     return error;
                 }
             }
 
-            ini_config_file_close(file_ctx);
+            ini_config_file_destroy(file_ctx);
 
             INIOUT(col_debug_collection(ini_config->cfg,
                    COL_TRAVERSE_DEFAULT));
-- 
1.7.1

