Crates.io | directories |
lib.rs | directories |
version | 5.0.1 |
source | src |
created_at | 2017-12-18 07:12:55.098997 |
updated_at | 2023-04-30 22:04:03.590681 |
description | A tiny mid-level library that provides platform-specific standard locations of directories for config, cache and other data on Linux, Windows and macOS by leveraging the mechanisms defined by the XDG base/user directory specifications on Linux, the Known Folder API on Windows, and the Standard Directory guidelines on macOS. |
homepage | |
repository | https://github.com/soc/directories-rs |
max_upload_size | |
id | 43503 |
size | 76,247 |
directories
The library provides the location of these directories by leveraging the mechanisms defined by
This library is written in Rust, and supports Linux, Redox, macOS and Windows. Other platforms are also supported; they use the Linux conventions.
dirs, the low-level sister library, is available at dirs-rs.
A version of this library running on the JVM is provided by directories-jvm.
Add the library as a dependency to your project by inserting
directories = "5.0"
into the [dependencies]
section of your Cargo.toml file.
If you are upgrading from version 2, please read the section on breaking changes first.
Library run by user Alice:
extern crate directories;
use directories::{BaseDirs, UserDirs, ProjectDirs};
if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") {
proj_dirs.config_dir();
// Lin: /home/alice/.config/barapp
// Win: C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config
// Mac: /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App
}
if let Some(base_dirs) = BaseDirs::new() {
base_dirs.executable_dir();
// Lin: Some(/home/alice/.local/bin)
// Win: None
// Mac: None
}
if let Some(user_dirs) = UserDirs::new() {
user_dirs.audio_dir();
// Lin: /home/alice/Music
// Win: C:\Users\Alice\Music
// Mac: /Users/Alice/Music
}
BaseDirs::new
, UserDirs::new
or ProjectDirs::from
.executable_dir
is specified to provide the path to a user-writable directory for binaries.None
on macOS and Windows.font_dir
is specified to provide the path to a user-writable directory for fonts.None
on Windows.runtime_dir
is specified to provide the path to a directory for non-essential runtime data.
It is required that this directory is created when the user logs in, is only accessible by the
user itself, is deleted when the user logs out, and supports all filesystem features of the
operating system.None
on macOS and Windows.BaseDirs
The intended use case for BaseDirs
is to query the paths of user-invisible standard directories
that have been defined according to the conventions of the operating system the library is running on.
If you want to compute the location of cache, config or data directories for your own application or project, use ProjectDirs
instead.
Function name | Value on Linux | Value on Windows | Value on macOS |
---|---|---|---|
home_dir |
$HOME |
{FOLDERID_Profile} |
$HOME |
cache_dir |
$XDG_CACHE_HOME or $HOME /.cache |
{FOLDERID_LocalAppData} |
$HOME /Library/Caches |
config_dir |
$XDG_CONFIG_HOME or $HOME /.config |
{FOLDERID_RoamingAppData} |
$HOME /Library/Application Support |
config_local_dir |
$XDG_CONFIG_HOME or $HOME /.config |
{FOLDERID_LocalAppData} |
$HOME /Library/Application Support |
data_dir |
$XDG_DATA_HOME or $HOME /.local/share |
{FOLDERID_RoamingAppData} |
$HOME /Library/Application Support |
data_local_dir |
$XDG_DATA_HOME or $HOME /.local/share |
{FOLDERID_LocalAppData} |
$HOME /Library/Application Support |
executable_dir |
Some($XDG_BIN_HOME) or Some($HOME /.local/bin) |
None |
None |
preference_dir |
$XDG_CONFIG_HOME or $HOME /.config |
{FOLDERID_RoamingAppData} |
$HOME /Library/Preferences |
runtime_dir |
Some($XDG_RUNTIME_DIR) or None |
None |
None |
state_dir |
Some($XDG_STATE_HOME) or Some($HOME /.local/state) |
None |
None |
UserDirs
The intended use case for UserDirs
is to query the paths of user-facing standard directories
that have been defined according to the conventions of the operating system the library is running on.
Function name | Value on Linux | Value on Windows | Value on macOS |
---|---|---|---|
home_dir |
$HOME |
{FOLDERID_Profile} |
$HOME |
audio_dir |
Some(XDG_MUSIC_DIR) or None |
Some({FOLDERID_Music}) |
Some($HOME /Music/) |
desktop_dir |
Some(XDG_DESKTOP_DIR) or None |
Some({FOLDERID_Desktop}) |
Some($HOME /Desktop/) |
document_dir |
Some(XDG_DOCUMENTS_DIR) or None |
Some({FOLDERID_Documents}) |
Some($HOME /Documents/) |
download_dir |
Some(XDG_DOWNLOAD_DIR) or None |
Some({FOLDERID_Downloads}) |
Some($HOME /Downloads/) |
font_dir |
Some($XDG_DATA_HOME /fonts/) or Some($HOME /.local/share/fonts/) |
None |
Some($HOME /Library/Fonts/) |
picture_dir |
Some(XDG_PICTURES_DIR) or None |
Some({FOLDERID_Pictures}) |
Some($HOME /Pictures/) |
public_dir |
Some(XDG_PUBLICSHARE_DIR) or None |
Some({FOLDERID_Public}) |
Some($HOME /Public/) |
template_dir |
Some(XDG_TEMPLATES_DIR) or None |
Some({FOLDERID_Templates}) |
None |
video_dir |
Some(XDG_VIDEOS_DIR) or None |
Some({FOLDERID_Videos}) |
Some($HOME /Movies/) |
ProjectDirs
The intended use case for ProjectDirs
is to compute the location of cache, config or data directories for your own application or project,
which are derived from the standard directories.
Function name | Value on Linux | Value on Windows | Value on macOS |
---|---|---|---|
cache_dir |
$XDG_CACHE_HOME /<project_path> or $HOME /.cache/<project_path> |
{FOLDERID_LocalAppData} /<project_path> /cache |
$HOME /Library/Caches/<project_path> |
config_dir |
$XDG_CONFIG_HOME /<project_path> or $HOME /.config/<project_path> |
{FOLDERID_RoamingAppData} /<project_path> /config |
$HOME /Library/Application Support/<project_path> |
config_local_dir |
$XDG_CONFIG_HOME /<project_path> or $HOME /.config/<project_path> |
{FOLDERID_LocalAppData} /<project_path> /config |
$HOME /Library/Application Support/<project_path> |
data_dir |
$XDG_DATA_HOME /<project_path> or $HOME /.local/share/<project_path> |
{FOLDERID_RoamingAppData} /<project_path> /data |
$HOME /Library/Application Support/<project_path> |
data_local_dir |
$XDG_DATA_HOME /<project_path> or $HOME /.local/share/<project_path> |
{FOLDERID_LocalAppData} /<project_path> /data |
$HOME /Library/Application Support/<project_path> |
preference_dir |
$XDG_CONFIG_HOME /<project_path> or $HOME /.config/<project_path> |
{FOLDERID_RoamingAppData} /<project_path> /config |
$HOME /Library/Preferences/<project_path> |
runtime_dir |
Some($XDG_RUNTIME_DIR /<project_path>) |
None |
None |
state_dir |
Some($XDG_STATE_HOME /<project_path>) or $HOME /.local/state/<project_path> |
None |
None |
The specific value of <project_path>
is computed by the
ProjectDirs::from(qualifier: &str,
organization: &str,
application: &str)
function and varies across operating systems. As an example, calling
ProjectDirs::from("org" /*qualifier*/,
"Baz Corp" /*organization*/,
"Foo Bar-App" /*application*/)
results in the following values:
Value on Linux | Value on Windows | Value on macOS |
---|---|---|
"foobar-app" |
"Baz Corp/Foo Bar-App" |
"org.Baz-Corp.Foo-Bar-App" |
The ProjectDirs::from_path
function allows the creation of ProjectDirs
structs directly from a PathBuf
value.
This argument is used verbatim and is not adapted to operating system standards.
The use of ProjectDirs::from_path
is strongly discouraged, as its results will not follow operating system standards on at least two of three platforms.
There are other crates in the Rust ecosystem that try similar or related things. Here is an overview of them, combined with ratings on properties that guided the design of this crate.
Please take this table with a grain of salt: a different crate might very well be more suitable for your specific use case. (Of course my crate achieves my design goals better than other crates, which might have had different design goals.)
Library | Status | Lin | Mac | Win | Base | User | Proj | Conv |
---|---|---|---|---|---|---|---|---|
app_dirs | Unmaintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ |
app_dirs2 | Maintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ |
dirs | Developed | ✔ | ✔ | ✔ | ✔ | ✔ | ✖ | ✔ |
directories | Developed | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
s_app_dir | Unmaintained? | ✔ | ✖ | 🞈 | ✖ | ✖ | 🞈 | ✖ |
standard_paths | Maintained | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ |
xdg | Maintained | ✔ | ✖ | ✖ | ✔ | ✖ | ✔ | 🞈 |
xdg-basedir | Unmaintained? | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 |
xdg-rs | Obsolete | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 |
It's possible to cross-compile this library if the necessary toolchains are installed with rustup. This is helpful to ensure a change has not broken compilation on a different platform.
The following commands will build this library on Linux, macOS and Windows:
cargo build --target=x86_64-unknown-linux-gnu
cargo build --target=x86_64-pc-windows-gnu
cargo build --target=x86_64-apple-darwin
cargo build --target=x86_64-unknown-redox
dirs-sys
dependency to 0.4.0
.config_local_dir
for non-roaming configuration on Windows. On non-Windows platforms the behavior is identical to config dir
.executable_dir
has been adjusted to not depend on $XDG_DATA_HOME
.
Code, which assumed that setting the $XDG_DATA_HOME
environment variable also impacted executable_dir
if
the $XDG_BIN_HOME
environment variable was not set, requires adjustment.XDG_STATE_HOME
.BaseDirs::config_dir
and ProjectDirs::config_dir
on macOS has been adjusted (thanks to everyone involved):
config_dir
functions have been changed to return the Application Support
directory on macOS, as suggested by Apple documentation.config_dir
functions on non-macOS platforms has not been changed.config_dir
functions to store files, it may be necessary to write code
that migrates the files to the new location on macOS.config_dir
functions to uses of the preference_dir
functions
to retain the old behavior.)BaseDirs::preference_dir
and ProjectDirs::preference_dir
functions returns
the Preferences
directory on macOS now, which – according to Apple documentation – shall only
be used to store .plist files using Apple-proprietary APIs.
– preference_dir
and config_dir
behave identical on non-macOS platforms.BREAKING CHANGE The behavior of deactivated, missing or invalid XDG User Dirs entries on Linux has been improved (contributed by @tmiasko, thank you!):
Some($HOME)
) for such faulty entries, except for a faulty XDG_DESKTOP_DIR
entry which returned (Some($HOME/Desktop)
).None
for such entries.Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.