From 19153cf9113c73f110f1f40d37cbf232f056fa51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Wed, 10 Oct 2018 14:33:40 +0200 Subject: [PATCH] wsutil: Add config_file_exists_with_entries() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The purpose of this function is to check if a configuration file exists and has at least one entry which is not a comment. Use this when building the list of profiles where the user can copy configuration from, to avoid listing profiles with empty files or files with only comments. Change-Id: If45f52025959818fb1213ffac488cd59441e9fce Reviewed-on: https://code.wireshark.org/review/30113 Petri-Dish: Stig Bjørlykke Tested-by: Petri Dish Buildbot Reviewed-by: Peter Wu --- debian/libwsutil0.symbols | 1 + ui/qt/widgets/copy_from_profile_menu.cpp | 2 +- wsutil/filesystem.c | 30 ++++++++++++++++++++++++ wsutil/filesystem.h | 6 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index 4200b13758..88464bcc5a 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -10,6 +10,7 @@ libwsutil.so.0 libwsutil0 #MINVER# cmdarg_err@Base 1.99.0 cmdarg_err_cont@Base 1.99.0 cmdarg_err_init@Base 1.99.0 + config_file_exists_with_entries@Base 2.9.0 copy_file_binary_mode@Base 1.12.0~rc1 copy_persconffile_profile@Base 1.12.0~rc1 crc11_307_noreflect_noxor@Base 1.10.0 diff --git a/ui/qt/widgets/copy_from_profile_menu.cpp b/ui/qt/widgets/copy_from_profile_menu.cpp index 36a07dd614..6c91e6b1de 100644 --- a/ui/qt/widgets/copy_from_profile_menu.cpp +++ b/ui/qt/widgets/copy_from_profile_menu.cpp @@ -25,7 +25,7 @@ CopyFromProfileMenu::CopyFromProfileMenu(QString filename) : profile_def *profile = (profile_def *) fl_entry->data; char *profile_dir = get_profile_dir(profile->name, profile->is_global); char *file_name = g_build_filename(profile_dir, filename_.toUtf8().constData(), NULL); - if (file_exists(file_name) && strcmp(profile_name, profile->name) != 0) { + if ((strcmp(profile_name, profile->name) != 0) && config_file_exists_with_entries(file_name, '#')) { if (profile->is_global && !globals_started) { if (have_profiles_) { addSeparator(); diff --git a/wsutil/filesystem.c b/wsutil/filesystem.c index b58477a760..323cfd01b8 100644 --- a/wsutil/filesystem.c +++ b/wsutil/filesystem.c @@ -2024,6 +2024,36 @@ file_exists(const char *fname) } } +gboolean config_file_exists_with_entries(const char *fname, char comment_char) +{ + gboolean start_of_line = TRUE; + gboolean has_entries = FALSE; + FILE *file; + int c; + + if (!fname) { + return FALSE; + } + + if ((file = ws_fopen(fname, "r")) == NULL) { + return FALSE; + } + + do { + c = ws_getc_unlocked(file); + if (start_of_line && c != comment_char && !g_ascii_isspace(c) && g_ascii_isprint(c)) { + has_entries = TRUE; + break; + } + if (c == '\n' || !g_ascii_isspace(c)) { + start_of_line = (c == '\n'); + } + } while (c != EOF); + + fclose(file); + return has_entries; +} + /* * Check that the from file is not the same as to file * We do it here so we catch all cases ... diff --git a/wsutil/filesystem.h b/wsutil/filesystem.h index 3e805bd69d..e00794899e 100644 --- a/wsutil/filesystem.h +++ b/wsutil/filesystem.h @@ -286,6 +286,12 @@ WS_DLL_PUBLIC int test_for_fifo(const char *); */ WS_DLL_PUBLIC gboolean file_exists(const char *fname); +/* + * Check if file is existing and has text entries which does not start + * with the comment character. + */ +WS_DLL_PUBLIC gboolean config_file_exists_with_entries(const char *fname, char comment_char); + /* * Check if two filenames are identical (with absolute and relative paths). */