[libvirt] [PATCH] util: Simplify Windows version of virGetUserDirectoryByUID()

Fabiano Fidêncio posted 1 patch 4 years, 4 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20191218175310.1706088-1-fidencio@redhat.com
src/util/virutil.c | 92 +---------------------------------------------
1 file changed, 1 insertion(+), 91 deletions(-)
[libvirt] [PATCH] util: Simplify Windows version of virGetUserDirectoryByUID()
Posted by Fabiano Fidêncio 4 years, 4 months ago
Let's just use the plain g_get_home_dir(), from GLib, instead of
maintaining a code adapted from the GLib's one.

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
---
 src/util/virutil.c | 92 +---------------------------------------------
 1 file changed, 1 insertion(+), 91 deletions(-)

diff --git a/src/util/virutil.c b/src/util/virutil.c
index a28feb3daa..4075047cf9 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -1070,100 +1070,10 @@ virDoesGroupExist(const char *name G_GNUC_UNUSED)
 }
 
 # ifdef WIN32
-/* These methods are adapted from GLib2 under terms of LGPLv2+ */
-static int
-virGetWin32SpecialFolder(int csidl, char **path)
-{
-    char buf[MAX_PATH+1];
-    LPITEMIDLIST pidl = NULL;
-    int ret = 0;
-
-    *path = NULL;
-
-    if (SHGetSpecialFolderLocation(NULL, csidl, &pidl) == S_OK) {
-        if (SHGetPathFromIDList(pidl, buf))
-            *path = g_strdup(buf);
-        CoTaskMemFree(pidl);
-    }
-    return ret;
-}
-
-static int
-virGetWin32DirectoryRoot(char **path)
-{
-    char windowsdir[MAX_PATH];
-
-    *path = NULL;
-
-    if (GetWindowsDirectory(windowsdir, G_N_ELEMENTS(windowsdir))) {
-        const char *tmp;
-        /* Usually X:\Windows, but in terminal server environments
-         * might be an UNC path, AFAIK.
-         */
-        tmp = virFileSkipRoot(windowsdir);
-        if (VIR_FILE_IS_DIR_SEPARATOR(tmp[-1]) &&
-            tmp[-2] != ':')
-            tmp--;
-
-        windowsdir[tmp - windowsdir] = '\0';
-    } else {
-        strcpy(windowsdir, "C:\\");
-    }
-
-    *path = g_strdup(windowsdir);
-    return 0;
-}
-
-
-
 char *
 virGetUserDirectoryByUID(uid_t uid G_GNUC_UNUSED)
 {
-    /* Since Windows lacks setuid binaries, and since we already fake
-     * geteuid(), we can safely assume that this is only called when
-     * querying about the current user */
-    const char *dir;
-    char *ret;
-
-    dir = getenv("HOME");
-
-    /* Only believe HOME if it is an absolute path and exists */
-    if (dir) {
-        if (!virFileIsAbsPath(dir) ||
-            !virFileExists(dir))
-            dir = NULL;
-    }
-
-    /* In case HOME is Unix-style (it happens), convert it to
-     * Windows style.
-     */
-    if (dir) {
-        char *p;
-        while ((p = strchr(dir, '/')) != NULL)
-            *p = '\\';
-    }
-
-    if (!dir)
-        /* USERPROFILE is probably the closest equivalent to $HOME? */
-        dir = getenv("USERPROFILE");
-
-    ret = g_strdup(dir);
-
-    if (!ret &&
-        virGetWin32SpecialFolder(CSIDL_PROFILE, &ret) < 0)
-        return NULL;
-
-    if (!ret &&
-        virGetWin32DirectoryRoot(&ret) < 0)
-        return NULL;
-
-    if (!ret) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Unable to determine home directory"));
-        return NULL;
-    }
-
-    return ret;
+    return g_strdup(g_get_home_dir());
 }
 
 char *
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] util: Simplify Windows version of virGetUserDirectoryByUID()
Posted by Cole Robinson 4 years, 4 months ago
On 12/18/19 12:53 PM, Fabiano Fidêncio wrote:
> Let's just use the plain g_get_home_dir(), from GLib, instead of
> maintaining a code adapted from the GLib's one.
> 
> Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
> ---
>  src/util/virutil.c | 92 +---------------------------------------------
>  1 file changed, 1 insertion(+), 91 deletions(-)
> 
> diff --git a/src/util/virutil.c b/src/util/virutil.c
> index a28feb3daa..4075047cf9 100644
> --- a/src/util/virutil.c
> +++ b/src/util/virutil.c
> @@ -1070,100 +1070,10 @@ virDoesGroupExist(const char *name G_GNUC_UNUSED)
>  }
>  
>  # ifdef WIN32
> -/* These methods are adapted from GLib2 under terms of LGPLv2+ */
> -static int
> -virGetWin32SpecialFolder(int csidl, char **path)
> -{
> -    char buf[MAX_PATH+1];
> -    LPITEMIDLIST pidl = NULL;
> -    int ret = 0;
> -
> -    *path = NULL;
> -
> -    if (SHGetSpecialFolderLocation(NULL, csidl, &pidl) == S_OK) {
> -        if (SHGetPathFromIDList(pidl, buf))
> -            *path = g_strdup(buf);
> -        CoTaskMemFree(pidl);
> -    }
> -    return ret;
> -}
> -
> -static int
> -virGetWin32DirectoryRoot(char **path)
> -{
> -    char windowsdir[MAX_PATH];
> -
> -    *path = NULL;
> -
> -    if (GetWindowsDirectory(windowsdir, G_N_ELEMENTS(windowsdir))) {
> -        const char *tmp;
> -        /* Usually X:\Windows, but in terminal server environments
> -         * might be an UNC path, AFAIK.
> -         */
> -        tmp = virFileSkipRoot(windowsdir);
> -        if (VIR_FILE_IS_DIR_SEPARATOR(tmp[-1]) &&
> -            tmp[-2] != ':')
> -            tmp--;
> -
> -        windowsdir[tmp - windowsdir] = '\0';
> -    } else {
> -        strcpy(windowsdir, "C:\\");
> -    }
> -
> -    *path = g_strdup(windowsdir);
> -    return 0;
> -}
> -
> -
> -
>  char *
>  virGetUserDirectoryByUID(uid_t uid G_GNUC_UNUSED)
>  {
> -    /* Since Windows lacks setuid binaries, and since we already fake
> -     * geteuid(), we can safely assume that this is only called when
> -     * querying about the current user */

Keep this comment, it explains why this function does not abide the
passed in uid argument. With that:

Reviewed-by: Cole Robinson <crobinso@redhat.com>

After this, virFileSkipRoot and virFileIsAbsPath are no longer used, and
after that I think VIR_FILE_IS_DIR_SEPARATOR, maybe check for others.
Should be a follow up patch though

- Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list