[libvirt] [PATCH v4 0/4] Rewrite virGetUser*Directory() functions using g_get_*_dir()

By rewriting virGetUser*Directory() functions using g_get_*_dir() functions allows us to drop all the different implementations we keep, as GLib already takes care of those for us. Changes since v3: https://www.redhat.com/archives/libvir-list/2019-December/msg01123.html - Used g_build_filename() instead of g_strdup_printf(); Changes since v2: https://www.redhat.com/archives/libvir-list/2019-December/msg01064.html - Addressed comments made by Cole about: - virGetUserDirectory() should return $HOME; - virGetUser*Directory() don't append "libvirt" to the returned path on Windows; Changes since v1: https://www.redhat.com/archives/libvir-list/2019-December/msg01055.html - Don't check for the return of g_get_*_dir(), as it cannot be NULL; Fabiano Fidêncio (4): util: Rewrite virGetUserDirectory() using g_get_home_dir() util: Rewrite virGetUserConfigDirectory() using g_get_user_config_dir() util: Rewrite virGetUserCacheDirectory() using g_get_user_cache_dir() util: Rewrite virGetUserRuntimeDirectory() using g_get_user_runtime_dir() src/util/virutil.c | 137 ++++++++++----------------------------------- 1 file changed, 31 insertions(+), 106 deletions(-) -- 2.23.0

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- src/util/virutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/virutil.c b/src/util/virutil.c index ed1f696e37..7008c3119c 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -582,7 +582,7 @@ virGetHostnameQuiet(void) char * virGetUserDirectory(void) { - return virGetUserDirectoryByUID(geteuid()); + return g_strdup(g_get_home_dir()); } -- 2.23.0

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- src/util/virutil.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/src/util/virutil.c b/src/util/virutil.c index 7008c3119c..9d98b0051a 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -586,6 +586,16 @@ virGetUserDirectory(void) } +char *virGetUserConfigDirectory(void) +{ +#ifdef WIN32 + return g_strdup(g_get_user_config_dir()); +#else + return g_build_filename(g_get_user_config_dir(), "libvirt", NULL); +#endif +} + + #ifdef HAVE_GETPWUID_R /* Look up fields from the user database for the given user. On * error, set errno, report the error if not instructed otherwise via @quiet, @@ -754,11 +764,6 @@ static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir) return ret; } -char *virGetUserConfigDirectory(void) -{ - return virGetXDGDirectory("XDG_CONFIG_HOME", ".config"); -} - char *virGetUserCacheDirectory(void) { return virGetXDGDirectory("XDG_CACHE_HOME", ".cache"); @@ -1187,21 +1192,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } -char * -virGetUserConfigDirectory(void) -{ - char *ret; - if (virGetWin32SpecialFolder(CSIDL_LOCAL_APPDATA, &ret) < 0) - return NULL; - - if (!ret) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to determine config directory")); - return NULL; - } - return ret; -} - char * virGetUserCacheDirectory(void) { @@ -1242,15 +1232,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } -char * -virGetUserConfigDirectory(void) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("virGetUserConfigDirectory is not available")); - - return NULL; -} - char * virGetUserCacheDirectory(void) { -- 2.23.0

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- src/util/virutil.c | 57 ++++++++-------------------------------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/src/util/virutil.c b/src/util/virutil.c index 9d98b0051a..db9378fa57 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -596,6 +596,16 @@ char *virGetUserConfigDirectory(void) } +char *virGetUserCacheDirectory(void) +{ +#ifdef WIN32 + return g_strdup(g_get_user_cache_dir()); +#else + return g_build_filename(g_get_user_cache_dir(), "libvirt", NULL); +#endif +} + + #ifdef HAVE_GETPWUID_R /* Look up fields from the user database for the given user. On * error, set errno, report the error if not instructed otherwise via @quiet, @@ -746,29 +756,6 @@ char *virGetUserShell(uid_t uid) } -static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir) -{ - const char *path = getenv(xdgenvname); - char *ret = NULL; - char *home = NULL; - - if (path && path[0]) { - ret = g_strdup_printf("%s/libvirt", path); - } else { - home = virGetUserDirectory(); - if (home) - ret = g_strdup_printf("%s/%s/libvirt", home, xdgdefdir); - } - - VIR_FREE(home); - return ret; -} - -char *virGetUserCacheDirectory(void) -{ - return virGetXDGDirectory("XDG_CACHE_HOME", ".cache"); -} - char *virGetUserRuntimeDirectory(void) { const char *path = getenv("XDG_RUNTIME_DIR"); @@ -1192,21 +1179,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } -char * -virGetUserCacheDirectory(void) -{ - char *ret; - if (virGetWin32SpecialFolder(CSIDL_INTERNET_CACHE, &ret) < 0) - return NULL; - - if (!ret) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to determine config directory")); - return NULL; - } - return ret; -} - char * virGetUserRuntimeDirectory(void) { @@ -1232,15 +1204,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } -char * -virGetUserCacheDirectory(void) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("virGetUserCacheDirectory is not available")); - - return NULL; -} - char * virGetUserRuntimeDirectory(void) { -- 2.23.0

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- src/util/virutil.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/src/util/virutil.c b/src/util/virutil.c index db9378fa57..a28feb3daa 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -606,6 +606,16 @@ char *virGetUserCacheDirectory(void) } +char *virGetUserRuntimeDirectory(void) +{ +#ifdef WIN32 + return g_strdup(g_get_user_runtime_dir()); +#else + return g_build_filename(g_get_user_runtime_dir(), "libvirt", NULL); +#endif +} + + #ifdef HAVE_GETPWUID_R /* Look up fields from the user database for the given user. On * error, set errno, report the error if not instructed otherwise via @quiet, @@ -756,20 +766,6 @@ char *virGetUserShell(uid_t uid) } -char *virGetUserRuntimeDirectory(void) -{ - const char *path = getenv("XDG_RUNTIME_DIR"); - - if (!path || !path[0]) { - return virGetUserCacheDirectory(); - } else { - char *ret; - - ret = g_strdup_printf("%s/libvirt", path); - return ret; - } -} - char *virGetUserName(uid_t uid) { char *ret; @@ -1179,12 +1175,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } -char * -virGetUserRuntimeDirectory(void) -{ - return virGetUserCacheDirectory(); -} - # else /* !HAVE_GETPWUID_R && !WIN32 */ char * virGetUserDirectoryByUID(uid_t uid G_GNUC_UNUSED) @@ -1203,15 +1193,6 @@ virGetUserShell(uid_t uid G_GNUC_UNUSED) return NULL; } - -char * -virGetUserRuntimeDirectory(void) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("virGetUserRuntimeDirectory is not available")); - - return NULL; -} # endif /* ! HAVE_GETPWUID_R && ! WIN32 */ char * -- 2.23.0

On 12/18/19 11:26 AM, Fabiano Fidêncio wrote:
By rewriting virGetUser*Directory() functions using g_get_*_dir() functions allows us to drop all the different implementations we keep, as GLib already takes care of those for us.
Changes since v3: https://www.redhat.com/archives/libvir-list/2019-December/msg01123.html - Used g_build_filename() instead of g_strdup_printf();
Changes since v2: https://www.redhat.com/archives/libvir-list/2019-December/msg01064.html - Addressed comments made by Cole about: - virGetUserDirectory() should return $HOME; - virGetUser*Directory() don't append "libvirt" to the returned path on Windows;
Changes since v1: https://www.redhat.com/archives/libvir-list/2019-December/msg01055.html - Don't check for the return of g_get_*_dir(), as it cannot be NULL;
Fabiano Fidêncio (4): util: Rewrite virGetUserDirectory() using g_get_home_dir() util: Rewrite virGetUserConfigDirectory() using g_get_user_config_dir() util: Rewrite virGetUserCacheDirectory() using g_get_user_cache_dir() util: Rewrite virGetUserRuntimeDirectory() using g_get_user_runtime_dir()
src/util/virutil.c | 137 ++++++++++----------------------------------- 1 file changed, 31 insertions(+), 106 deletions(-)
Reviewed-by: Cole Robinson <crobinso@redhat.com> - Cole
participants (2)
-
Cole Robinson
-
Fabiano Fidêncio