This patch mini-series supports Git for Windows' default strategy to
determine the current user's home directory by looking at the
environment variable HOME, falling back to HOMEDRIVE and HOMEPATH, and
if these variables are also unset, to USERPROFILE.

This strategy is a quick method to determine the home directory,
certainly quicker than looking at LDAP, even more so when a domain
controller is unreachable and causes long hangs in Cygwin's startup.

This strategy also allows users to override the home directory easily
(e.g. in case that their real home directory is a network share that is
not all that well handled by some commands such as cmd.exe's cd
command).

Sorry for sending out v4 sooooo late...!

Changes since v3:

- Fixed the bug in v2 where `getenv("HOME")` would convert the value to
  a Unix-y path and the `fetch_home_env()` function would then try to
  convert it _again_.

- Disentangled the logic in `fetch_home_env()` instead of doing
  everything in one big, honking, unreadable `if` condition.

- Commented the code in `fetch_home_env()`.

Changes since v2:

- Using `getenv()` and `cygwin_create_path()` instead of the
  `GetEnvironmentVariableW()`/`cygwin_conv_path()` dance

- Adjusted the documentation to drive home that this only affects the
  _current_ user's home directory

- Using the `PUSER_INFO_3` variant of `get_home()`

- Adjusted the commit messages

- Added another patch, to support "ad-hoc cloud accounts"

Johannes Schindelin (3):
  Allow deriving the current user's home directory via the HOME variable
  Respect `db_home` setting even for SYSTEM/Microsoft accounts
  Respect `db_home: env` even when no uid can be determined

 winsup/cygwin/local_includes/cygheap.h |  3 +-
 winsup/cygwin/uinfo.cc                 | 70 ++++++++++++++++++++++++--
 winsup/doc/ntsec.xml                   | 22 ++++++++
 3 files changed, 91 insertions(+), 4 deletions(-)

Range-diff:
1:  6f8fe89d9d ! 1:  7a074997ea Allow deriving the current user's home 
directory via the HOME variable
    @@ winsup/cygwin/uinfo.cc: fetch_from_path (cyg_ldap *pldap, PUSER_INFO_3 
ui, cygps
     +static char *
     +fetch_home_env (void)
     +{
    -+  tmp_pathbuf tp;
    -+  char *p, *q;
    -+  const char *home, *home_drive, *home_path;
    ++  /* If `HOME` is set, prefer it */
    ++  const char *home = getenv ("HOME");
    ++  if (home)
    ++    return strdup (home);
    ++
    ++  /* If `HOME` is unset, fall back to `HOMEDRIVE``HOMEPATH`
    ++     (without a directory separator, as `HOMEPATH` starts with one). */
    ++  const char *home_drive = getenv ("HOMEDRIVE");
    ++  if (home_drive)
    ++    {
    ++      const char *home_path = getenv ("HOMEPATH");
    ++      if (home_path)
    ++  {
    ++    tmp_pathbuf tp;
    ++    char *p = tp.c_get (), *q;
     +
    -+  if ((home = getenv ("HOME"))
    -+      || ((home_drive = getenv ("HOMEDRIVE"))
    -+    && (home_path = getenv ("HOMEPATH"))
     +    // concatenate HOMEDRIVE and HOMEPATH
    -+          && (home = p = tp.c_get ())
    -+    && (q = stpncpy (p, home_drive, NT_MAX_PATH))
    -+          && strlcpy (q, home_path, NT_MAX_PATH - (q - p)))
    -+      || (home = getenv ("USERPROFILE")))
    ++    q = stpncpy (p, home_drive, NT_MAX_PATH);
    ++    strlcpy (q, home_path, NT_MAX_PATH - (q - p));
    ++    return (char *) cygwin_create_path (CCP_WIN_A_TO_POSIX, p);
    ++  }
    ++    }
    ++
    ++  /* If neither `HOME` nor `HOMEDRIVE``HOMEPATH` are set, fall back
    ++     to `USERPROFILE`; In corporate setups, this might point to a
    ++     disconnected network share, hence this is the last fall back. */
    ++  home = getenv ("USERPROFILE");
    ++  if (home)
     +    return (char *) cygwin_create_path (CCP_WIN_A_TO_POSIX, home);
     +
     +  return NULL;
2:  1b4ee89aa7 = 2:  a70c77dc8f Respect `db_home` setting even for 
SYSTEM/Microsoft accounts
3:  4d90319e44 ! 3:  4cd6ae7307 Respect `db_home: env` even when no uid can be 
determined
    @@ winsup/cygwin/uinfo.cc: fetch_from_path (cyg_ldap *pldap, PUSER_INFO_3 
ui, cygps
     +              break;
                  w = wcpncpy (w, dom, we - w);
                  if (w < we)
    -               *w++ = cygheap->pg.nss_separator ()[0];
    +               *w++ = NSS_SEPARATOR_CHAR;
     @@ winsup/cygwin/uinfo.cc: fetch_from_path (cyg_ldap *pldap, PUSER_INFO_3 
ui, cygpsid &sid, PCWSTR str,
              w = wcpncpy (w, name, we - w);
              break;

base-commit: a9a17f5fe51498b182d4a11ac48207b8c7ffe8ec
Published-As: 
https://github.com/dscho/msys2-runtime/releases/tag/home-env-cygwin-v4
Fetch-It-Via: git fetch https://github.com/dscho/msys2-runtime 
home-env-cygwin-v4

--
2.40.0.windows.1

Reply via email to