[Pkg-libvirt-commits] [libguestfs] 18/54: fish: small refactor of config reading code

Hilko Bengen bengen at moszumanska.debian.org
Sun Mar 9 11:21:15 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch debian
in repository libguestfs.

commit 8cc180a1ae56096c89c91cda015b11cbc79e8355
Author: Pino Toscano <ptoscano at redhat.com>
Date:   Mon Feb 17 16:21:18 2014 +0100

    fish: small refactor of config reading code
    
    Even though so far there is just one possible setting to read, isolate
    in an own function the code to parse a configuration file and read the
    settings out of it.
    
    Now there's a new config_t handle used every time, but since config_read
    would reset an handle completely, there is no behaviour change.
    
    (cherry picked from commit 5d2b0d1366fc72a79b842ee4bccaa99a16cab754)
---
 fish/config.c | 88 +++++++++++++++++++++--------------------------------------
 1 file changed, 31 insertions(+), 57 deletions(-)

diff --git a/fish/config.c b/fish/config.c
index e9f437a..02d850b 100644
--- a/fish/config.c
+++ b/fish/config.c
@@ -42,87 +42,61 @@ static const char *etc_filename = "/etc/libguestfs-tools.conf";
  * global handle 'g' is opened.
  */
 
-void
-parse_config (void)
+static void
+read_config_from_file (const char *filename)
 {
-  const char *home;
   FILE *fp;
-  config_t conf;
-
-  config_init (&conf);
-
-  /* Try $HOME first. */
-  home = getenv ("HOME");
-  if (home != NULL) {
-    CLEANUP_FREE char *path = NULL;
 
-    if (asprintf (&path, "%s/%s", home, home_filename) == -1) {
-      perror ("asprintf");
-      exit (EXIT_FAILURE);
-    }
+  fp = fopen (filename, "r");
+  if (fp != NULL) {
+    config_t conf;
 
-    fp = fopen (path, "r");
-    if (fp != NULL) {
-      /*
-      if (verbose)
-        fprintf (stderr, "%s: reading configuration from %s\n",
-                 program_name, path);
-      */
-
-      if (config_read (&conf, fp) == CONFIG_FALSE) {
-        fprintf (stderr,
-                 _("%s: %s: line %d: error parsing configuration file: %s\n"),
-                 program_name, path, config_error_line (&conf),
-                 config_error_text (&conf));
-        exit (EXIT_FAILURE);
-      }
-
-      if (fclose (fp) == -1) {
-        perror (path);
-        exit (EXIT_FAILURE);
-      }
-
-      /* Notes:
-       *
-       * (1) It's not obvious from the documentation, that config_read
-       * completely resets the 'conf' structure.  This means we cannot
-       * call config_read twice on the two possible configuration
-       * files, but instead have to copy out settings into our
-       * variables between calls.
-       *
-       * (2) If the next call fails then 'read_only' variable is not
-       * updated.  Failure could happen just because the setting is
-       * missing from the configuration file, so we ignore it here.
-       */
-      config_lookup_bool (&conf, "read_only", &read_only);
-    }
-  }
+    config_init (&conf);
 
-  fp = fopen (etc_filename, "r");
-  if (fp != NULL) {
     /*
     if (verbose)
       fprintf (stderr, "%s: reading configuration from %s\n",
-               program_name, etc_filename);
+               program_name, filename);
     */
 
     if (config_read (&conf, fp) == CONFIG_FALSE) {
       fprintf (stderr,
                _("%s: %s: line %d: error parsing configuration file: %s\n"),
-               program_name, etc_filename, config_error_line (&conf),
+               program_name, filename, config_error_line (&conf),
                config_error_text (&conf));
       exit (EXIT_FAILURE);
     }
 
     if (fclose (fp) == -1) {
-      perror (etc_filename);
+      perror (filename);
       exit (EXIT_FAILURE);
     }
 
     config_lookup_bool (&conf, "read_only", &read_only);
+
+    config_destroy (&conf);
+  }
+}
+
+void
+parse_config (void)
+{
+  const char *home;
+
+  /* Try $HOME first. */
+  home = getenv ("HOME");
+  if (home != NULL) {
+    CLEANUP_FREE char *path = NULL;
+
+    if (asprintf (&path, "%s/%s", home, home_filename) == -1) {
+      perror ("asprintf");
+      exit (EXIT_FAILURE);
+    }
+
+    read_config_from_file (path);
   }
 
-  config_destroy (&conf);
+  read_config_from_file (etc_filename);
 }
 
 #else /* !HAVE_LIBCONFIG */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list