From 40548322acb568c8a41d98e9d7722f1506246485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Fri, 5 Oct 2018 08:26:35 +0200 Subject: [PATCH] wsutil: Add get_profile_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use this in profile_exists() and copy_persconffile_profile(). Change-Id: I48728038b086a38822ef71766b23db8050deb464 Reviewed-on: https://code.wireshark.org/review/30027 Petri-Dish: Stig Bjørlykke Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- debian/libwsutil0.symbols | 1 + wsutil/filesystem.c | 72 +++++++++++++++++++-------------------- wsutil/filesystem.h | 6 ++++ 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index 69efd6eb58..4200b13758 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -79,6 +79,7 @@ libwsutil.so.0 libwsutil0 #MINVER# get_plugins_pers_dir@Base 1.12.0~rc1 get_plugins_pers_dir_with_version@Base 2.5.0 get_positive_int@Base 1.99.0 + get_profile_dir@Base 2.9.0 get_profile_name@Base 1.12.0~rc1 get_profiles_dir@Base 1.12.0~rc1 get_progfile_dir@Base 1.12.0~rc1 diff --git a/wsutil/filesystem.c b/wsutil/filesystem.c index cc75a7e8da..b58477a760 100644 --- a/wsutil/filesystem.c +++ b/wsutil/filesystem.c @@ -1419,40 +1419,50 @@ get_persconffile_dir(const gchar *profilename) return persconffile_profile_dir; } -gboolean -profile_exists(const gchar *profilename, gboolean global) +char * +get_profile_dir(const char *profilename, gboolean is_global) { - gchar *path = NULL, *global_path; + gchar *profile_dir; - if (global) { - /* - * If we're looking up a global profile, we must have a - * profile name. - */ - if (!profilename) - return FALSE; - global_path = get_global_profiles_dir(); - path = g_strdup_printf ("%s%s%s", global_path, - G_DIR_SEPARATOR_S, profilename); - g_free(global_path); - if (test_for_directory (path) == EISDIR) { - g_free (path); - return TRUE; + if (is_global) { + if (profilename && strlen(profilename) > 0 && + strcmp(profilename, DEFAULT_PROFILE) != 0) + { + gchar *global_path = get_global_profiles_dir(); + profile_dir = g_build_filename(global_path, profilename, NULL); + g_free(global_path); + } else { + profile_dir = g_strdup(get_datafile_dir()); } } else { /* * If we didn't supply a profile name, i.e. if profilename is * null, get_persconffile_dir() returns the default profile. */ - path = get_persconffile_dir (profilename); - if (test_for_directory (path) == EISDIR) { - g_free (path); - return TRUE; - } + profile_dir = get_persconffile_dir(profilename); } - g_free (path); - return FALSE; + return profile_dir; +} + +gboolean +profile_exists(const gchar *profilename, gboolean global) +{ + gchar *path = NULL; + gboolean exists; + + /* + * If we're looking up a global profile, we must have a + * profile name. + */ + if (global && !profilename) + return FALSE; + + path = get_profile_dir(profilename, global); + exists = (test_for_directory(path) == EISDIR) ? TRUE : FALSE; + + g_free(path); + return exists; } static int @@ -1673,20 +1683,10 @@ copy_persconffile_profile(const char *toname, const char *fromname, gboolean fro { gchar *from_dir; gchar *to_dir = get_persconffile_dir(toname); - gchar *filename, *from_file, *to_file, *global_path; + gchar *filename, *from_file, *to_file; GList *files, *file; - if (from_global) { - if (strcmp(fromname, DEFAULT_PROFILE) == 0) { - from_dir = get_global_profiles_dir(); - } else { - global_path = get_global_profiles_dir(); - from_dir = g_strdup_printf ("%s%s%s", global_path, G_DIR_SEPARATOR_S, fromname); - g_free(global_path); - } - } else { - from_dir = get_persconffile_dir(fromname); - } + from_dir = get_profile_dir(fromname, from_global); files = g_hash_table_get_keys(profile_files); file = g_list_first(files); diff --git a/wsutil/filesystem.h b/wsutil/filesystem.h index e7c44848ed..3e805bd69d 100644 --- a/wsutil/filesystem.h +++ b/wsutil/filesystem.h @@ -122,6 +122,12 @@ WS_DLL_PUBLIC gboolean has_global_profiles(void); */ WS_DLL_PUBLIC char *get_profiles_dir(void); +/* + * Get the directory used to store configuration files for a given profile. + * Caller must free the returned string. + */ +WS_DLL_PUBLIC char *get_profile_dir(const char *profilename, gboolean is_global); + /* * Create the directory used to store configuration profile directories. */