如何获取 linux 的已知路径
How to get known paths for linux
Windows 有一个已知路径的概念,其中包含无需硬编码路径即可检索它们的函数:
#include <filesystem>
#include <windows.h>
#include <ShlObj.h>
//...
std::filesystem::path GetAppDataPath() {
namespace FS = std::filesystem;
PWSTR ppszPath = nullptr;
auto hr_path = ::SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &ppszPath);
bool success = SUCCEEDED(hr_path);
if (success) {
auto p = FS::path(ppszPath);
::CoTaskMemFree(ppszPath);
p = FS::canonical(p);
return p;
}
return {};
}
是否有 linux 的等效项?
Linux是一个操作系统内核。它没有用户目录的概念。
有几个 Linux 分布。文件系统结构由发行版决定。大多数发行版都遵循 Linux Foundation 的 POSIX standard, and follow (to varying degree) the Filesystem Hierarchy Standard,这类似于其他类 UNIX 系统的目录结构。也就是说,发行版通常允许用户以非常规配置使用文件系统。例如,他们通常不会强制用户主目录位于 /home
.
下
POSIX 指定一些与此上下文相关的环境变量:
HOME
The system shall initialize this variable at the time of login to be a pathname of the user's home directory.
TMPDIR
This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.
可以在 C++ 中使用 std::getenv
访问环境变量。
在桌面系统上,目录结构在某种程度上也由桌面环境决定,其中有几种可用。 freedesktop.org 为不同桌面环境的互操作性制定了非官方规范。在符合 XDG Base Directory Specification 的 DE 上,应提供以下环境变量:
$XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.
$XDG_DATA_DIRS defines the preference-ordered set of base directories to search for data files in addition to the $XDG_DATA_HOME base directory. The directories in $XDG_DATA_DIRS should be seperated with a colon ':'.
If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
freedesktop.org 还提供了一个实用程序 xdg-user-dirs:
xdg-user-dirs is a tool to help manage "well known" user directories like the desktop folder and the music folder. It also handles localization (i.e. translation) of the filenames.
$(XDG_CONFIG_HOME)/user-dirs.dirs specifies the current set of directories for the user. This file is in a shell format, so its easy to access from a shell script. This file can also be modified by users (manually or via applications) to change the directories used.
因此,在 FOLDERID_RoamingAppData
的情况下,您可能应该根据用例使用 $XDG_x
之一,根据指定回退到相对于 $HOME
的适当默认值。
Windows 有一个已知路径的概念,其中包含无需硬编码路径即可检索它们的函数:
#include <filesystem>
#include <windows.h>
#include <ShlObj.h>
//...
std::filesystem::path GetAppDataPath() {
namespace FS = std::filesystem;
PWSTR ppszPath = nullptr;
auto hr_path = ::SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &ppszPath);
bool success = SUCCEEDED(hr_path);
if (success) {
auto p = FS::path(ppszPath);
::CoTaskMemFree(ppszPath);
p = FS::canonical(p);
return p;
}
return {};
}
是否有 linux 的等效项?
Linux是一个操作系统内核。它没有用户目录的概念。
有几个 Linux 分布。文件系统结构由发行版决定。大多数发行版都遵循 Linux Foundation 的 POSIX standard, and follow (to varying degree) the Filesystem Hierarchy Standard,这类似于其他类 UNIX 系统的目录结构。也就是说,发行版通常允许用户以非常规配置使用文件系统。例如,他们通常不会强制用户主目录位于 /home
.
POSIX 指定一些与此上下文相关的环境变量:
HOME
The system shall initialize this variable at the time of login to be a pathname of the user's home directory.
TMPDIR
This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.
可以在 C++ 中使用 std::getenv
访问环境变量。
在桌面系统上,目录结构在某种程度上也由桌面环境决定,其中有几种可用。 freedesktop.org 为不同桌面环境的互操作性制定了非官方规范。在符合 XDG Base Directory Specification 的 DE 上,应提供以下环境变量:
$XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.
$XDG_DATA_DIRS defines the preference-ordered set of base directories to search for data files in addition to the $XDG_DATA_HOME base directory. The directories in $XDG_DATA_DIRS should be seperated with a colon ':'.
If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
freedesktop.org 还提供了一个实用程序 xdg-user-dirs:
xdg-user-dirs is a tool to help manage "well known" user directories like the desktop folder and the music folder. It also handles localization (i.e. translation) of the filenames.
$(XDG_CONFIG_HOME)/user-dirs.dirs specifies the current set of directories for the user. This file is in a shell format, so its easy to access from a shell script. This file can also be modified by users (manually or via applications) to change the directories used.
因此,在 FOLDERID_RoamingAppData
的情况下,您可能应该根据用例使用 $XDG_x
之一,根据指定回退到相对于 $HOME
的适当默认值。