gulrak / filesystem

An implementation of C++17 std::filesystem for C++11 /C++14/C++17/C++20 on Windows, macOS, Linux and FreeBSD.
MIT License
1.34k stars 173 forks source link

How to get home directory? #84

Closed paulocoutinhox closed 3 years ago

paulocoutinhox commented 3 years ago

Hi,

How I can get home directory abstracting platform?

Thanks.

gulrak commented 3 years ago

Hi!

There is no functionality for getting the home directory in std::filesystem, so I didn't include something like that into ghc::filesystem.

One reason no such functionality exists might be, that there is no portable concept of what to put where in that home directory if to put it there at all or if there even is such a thing as a home directory on a specific platform.

On Windows 10 there is no single place like that, it could be split partly on the local drive, partly on a network share where the profile is stored. The environment variable USERPROFILE might be a place to start with, but be aware that it could contain special characters, so std::getenv might not return something usable if the user name contains international characters. so GetEnvironmentVariableW would be a better way on Windows.

On Unix-like systems the HOME environment variable would be a solution.

Most of the time, the wish comes from searching a way to store and retrieve configuration data, but even when having the home directory, the major platforms have very different conventions as to where configuration data is expected to be. In Windows it could be something like where the environment variable USERPROFILE combined with AppData\Local, AppData\LocalLow or AppData\Roaming depending on rights of the application or if the data should stay local or follow a profile in a domain, appended with a directory for the application, but it also could be the registry.

In Linux it could be some .<application> directory or file in home, but more often now it seems to be some .local/share/<application> directory in the users home, and in macOS it would be Library/Application Support inside the home directory containing the application specific directory with its data.

https://doc.qt.io/qt-5/qstandardpaths.html might be an interesting read on locations.