You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get UserProgramFiles with KF_FLAG_DONT_VERIFY for test
In the `gix-path` tests, `ProgramFilesPaths::obtain_envlessly()`
gets the expected user program files directory path `pf_user` by
retrieving the path of the `UserProgramFiles` known folder. This is
usually the best way to get that path. But like the other program
files paths, we only use the known folders system to get this path
in the test suite. In the code under test, we obtain this and other
program files paths from environment variables, so that production
code remains reasonably simple, without excessive or long-building
dependencies.
However, unlike the other program files paths, there is no
environment variable specifically for the user program files path.
Instead, there is an environment variable for the user's local
application data directory, `LocalAppData`. It is expected that the
user's program files path, if any, is the `Programs` subdirectory
of the directory given by the `LocalAppData` environment variable.
For the global (systemwide) program files directories, it is not
safe to guess their locations if they are absent. This is because
they are expected to be subdirectories of the root directory of the
system drive, and limited user accounts are capable of creating new
directories in the root directory of the system drive on most
Windows systems. (See CVE-2024-43785 on how this can go wrong.)
However, for the user's program files directory, it is safe to
guess its location, so long as one guesses it inside a directory
the user is expected to have full control of and that other users
without administrative powers are expected not to have the ability
to create directories inside. That is the case when guessing that
it is the `Programs` subdirectory of the user's local application
data directory.
Therefore, to support Git for Windows installations in the user's
program files directory even when `git.exe` cannot be found in a
`PATH` search, we shall search in the default locations where such
an installation would be. We shall do this without first checking
if the per-user program files directory they are inside exists yet,
since that extra step is not necessary for safety.
(Also, unlike nonexistent global program files directories, which
are never likely to come into existence short of installing a
different version of the operating system, a nonexistent user
program files directory is simply one that has not yet been created
due to no need for it. It is created the first time an application
tries to install there.)
However, this raises the question of how we test that this path (to
where the user program files directory either alredy exists or
would be created) is found correctly by comparing the
environment-variables-based result in the code under test to the
known-folders-based findings in the test. Two approaches come to
mind:
1. We could use the known folders system to find a directory higher
up, and then append the further component(s). But this would
effectively repeat logic between the code and their tests. It
would also make the test suite unable to identify failures if,
in some future version of the operating system, the user program
files directory is practically able to exist an other places.
2. We could retrieve the `UserProgramFiles` known folder entry.
This requires passing the `KF_FLAG_DONT_VERIFY` known folders
flag, without which we get an error if the directory doesn't
exist yet. However, we have been accessing known folders
information through the `known-folders` library, which doesn't
allow known folders flags other than `KF_FLAG_DEFAULT` to be
passed to `SHGetKnownFolderPath` Windows API function.
Here, we go with way (2). The `windows` crate is already a test
dependency of `gix-path`. This uses it to call that function with
an explicit flag argument, so we can pass `KF_FLAG_DONT_VERIFY`.
0 commit comments