[syslog-ng] [PATCH 1/2] cfg-lexer: Split cfg_lexer_include_file() into two functions

Gergely Nagy algernon at balabit.hu
Fri Mar 30 11:46:12 CEST 2012


To be able to keep the code simple in the future, split
cfg_lexer_include_file() into two: the intent is that we'll check if
the file exists, and if not, try the same thing as a glob, otherwise
continue as we did before.

This would nest too deep, so split out the current bulk into a
separate function.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 lib/cfg-lexer.c |   59 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 38 insertions(+), 21 deletions(-)

diff --git a/lib/cfg-lexer.c b/lib/cfg-lexer.c
index 6143a27..cfe229f 100644
--- a/lib/cfg-lexer.c
+++ b/lib/cfg-lexer.c
@@ -1,6 +1,6 @@
 /*
- * Copyright (c) 2002-2010 BalaBit IT Ltd, Budapest, Hungary
- * Copyright (c) 1998-2010 Balázs Scheidler
+ * Copyright (c) 2002-2012 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 1998-2012 Balázs Scheidler
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -379,32 +379,18 @@ cfg_lexer_start_next_include(CfgLexer *self)
   return TRUE;
 }
 
-gboolean
-cfg_lexer_include_file(CfgLexer *self, const gchar *filename_)
+static gboolean
+cfg_lexer_include_file_simple(CfgLexer *self, gchar *filename)
 {
-  struct stat st;
   CfgIncludeLevel *level;
-  gchar *filename;
+  struct stat st;
 
-  if (self->include_depth >= MAX_INCLUDE_DEPTH - 1)
+  if (stat(filename, &st) < 0)
     {
-      msg_error("Include file depth is too deep, increase MAX_INCLUDE_DEPTH and recompile",
-                evt_tag_str("filename", filename_),
-                evt_tag_int("depth", self->include_depth),
-                NULL);
+      g_free(filename);
       return FALSE;
     }
 
-  filename = find_file_in_path(cfg_args_get(self->globals, "include-path"), filename_, G_FILE_TEST_EXISTS);
-  if (!filename || stat(filename, &st) < 0)
-    {
-      msg_error("Include file/directory not found",
-                evt_tag_str("filename", filename_),
-                evt_tag_str("include-path", cfg_args_get(self->globals, "include-path")),
-                evt_tag_errno("error", errno),
-                NULL);
-      return FALSE;
-    }
   self->include_depth++;
   level = &self->include_stack[self->include_depth];
   level->include_type = CFGI_FILE;
@@ -488,10 +474,41 @@ cfg_lexer_include_file(CfgLexer *self, const gchar *filename_)
   g_slist_foreach(level->file.files, (GFunc) g_free, NULL);
   g_slist_free(level->file.files);
   level->file.files = NULL;
+
   return FALSE;
 }
 
 gboolean
+cfg_lexer_include_file(CfgLexer *self, const gchar *filename_)
+{
+  struct stat st;
+  CfgIncludeLevel *level;
+  gchar *filename;
+
+  if (self->include_depth >= MAX_INCLUDE_DEPTH - 1)
+    {
+      msg_error("Include file depth is too deep, increase MAX_INCLUDE_DEPTH and recompile",
+                evt_tag_str("filename", filename_),
+                evt_tag_int("depth", self->include_depth),
+                NULL);
+      return FALSE;
+    }
+
+  filename = find_file_in_path(cfg_args_get(self->globals, "include-path"), filename_, G_FILE_TEST_EXISTS);
+  if (!filename || stat(filename, &st) < 0)
+    {
+      msg_error("Include file/directory not found",
+                evt_tag_str("filename", filename_),
+                evt_tag_str("include-path", cfg_args_get(self->globals, "include-path")),
+                evt_tag_errno("error", errno),
+                NULL);
+      return FALSE;
+    }
+  else
+    return cfg_lexer_include_file_simple(self, filename);
+}
+
+gboolean
 cfg_lexer_include_buffer(CfgLexer *self, const gchar *name, gchar *buffer, gsize length)
 {
   CfgIncludeLevel *level;
-- 
1.7.9




More information about the syslog-ng mailing list