| 1 |
[](https://crates.io/crates/dirs) |
| 2 |
[](https://docs.rs/dirs/) |
| 3 |
 |
| 4 |
[](https://travis-ci.org/dirs-dev/dirs-rs) |
| 5 |
[](https://ci.appveyor.com/project/soc/dirs-rs/branch/master) |
| 6 |
 |
| 7 |
|
| 8 |
# `dirs` |
| 9 |
|
| 10 |
## Introduction |
| 11 |
|
| 12 |
- a tiny low-level library with a minimal API |
| 13 |
- that provides the platform-specific, user-accessible locations |
| 14 |
- for retrieving and storing configuration, cache and other data |
| 15 |
- on Linux, Redox, Windows (≥ Vista), macOS and other platforms. |
| 16 |
|
| 17 |
The library provides the location of these directories by leveraging the mechanisms defined by |
| 18 |
- the [XDG base directory](https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) and |
| 19 |
the [XDG user directory](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) specifications on Linux and Redox |
| 20 |
- the [Known Folder](https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457.aspx) API on Windows |
| 21 |
- the [Standard Directories](https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW6) |
| 22 |
guidelines on macOS |
| 23 |
|
| 24 |
## Platforms |
| 25 |
|
| 26 |
This library is written in Rust, and supports Linux, Redox, macOS and Windows. |
| 27 |
Other platforms are also supported; they use the Linux conventions. |
| 28 |
|
| 29 |
The minimal required version of Rust is 1.13 except for Redox, where the minimum Rust version |
| 30 |
depends on the [`redox_users`](https://crates.io/crates/redox_users) crate. |
| 31 |
|
| 32 |
It's mid-level sister library, _directories_, is available for Rust ([directories-rs](https://github.com/dirs-dev/directories-rs)) |
| 33 |
and on the JVM ([directories-jvm](https://github.com/dirs-dev/directories-jvm)). |
| 34 |
|
| 35 |
## Usage |
| 36 |
|
| 37 |
#### Dependency |
| 38 |
|
| 39 |
Add the library as a dependency to your project by inserting |
| 40 |
|
| 41 |
```toml |
| 42 |
dirs = "4.0" |
| 43 |
``` |
| 44 |
|
| 45 |
into the `[dependencies]` section of your Cargo.toml file. |
| 46 |
|
| 47 |
If you are upgrading from version 2, please read the [section on breaking changes](#3) first. |
| 48 |
|
| 49 |
#### Example |
| 50 |
|
| 51 |
Library run by user Alice: |
| 52 |
|
| 53 |
```rust |
| 54 |
extern crate dirs; |
| 55 |
|
| 56 |
dirs::home_dir(); |
| 57 |
// Lin: Some(/home/alice) |
| 58 |
// Win: Some(C:\Users\Alice) |
| 59 |
// Mac: Some(/Users/Alice) |
| 60 |
|
| 61 |
dirs::audio_dir(); |
| 62 |
// Lin: Some(/home/alice/Music) |
| 63 |
// Win: Some(C:\Users\Alice\Music) |
| 64 |
// Mac: Some(/Users/Alice/Music) |
| 65 |
|
| 66 |
dirs::config_dir(); |
| 67 |
// Lin: Some(/home/alice/.config) |
| 68 |
// Win: Some(C:\Users\Alice\AppData\Roaming) |
| 69 |
// Mac: Some(/Users/Alice/Library/Application Support) |
| 70 |
|
| 71 |
dirs::executable_dir(); |
| 72 |
// Lin: Some(/home/alice/.local/bin) |
| 73 |
// Win: None |
| 74 |
// Mac: None |
| 75 |
``` |
| 76 |
|
| 77 |
## Design Goals |
| 78 |
|
| 79 |
- The _dirs_ library is a low-level crate designed to provide the paths to standard directories |
| 80 |
as defined by operating systems rules or conventions.<br/> |
| 81 |
If your requirements are more complex, e. g. computing cache, config, etc. paths for specific |
| 82 |
applications or projects, consider using [directories](https://github.com/dirs-dev/directories-rs) |
| 83 |
instead. |
| 84 |
- This library does not create directories or check for their existence. The library only provides |
| 85 |
information on what the path to a certain directory _should_ be.<br/> |
| 86 |
How this information is used is a decision that developers need to make based on the requirements |
| 87 |
of each individual application. |
| 88 |
- This library is intentionally focused on providing information on user-writable directories only, |
| 89 |
as there is no discernible benefit in returning a path that points to a user-level, writable |
| 90 |
directory on one operating system, but a system-level, read-only directory on another.<br/> |
| 91 |
The confusion and unexpected failure modes of such an approach would be immense. |
| 92 |
- `executable_dir` is specified to provide the path to a user-writable directory for binaries.<br/> |
| 93 |
As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. |
| 94 |
- `font_dir` is specified to provide the path to a user-writable directory for fonts.<br/> |
| 95 |
As such a directory only exists on Linux and macOS, it returns `None` on Windows. |
| 96 |
- `runtime_dir` is specified to provide the path to a directory for non-essential runtime data. |
| 97 |
It is required that this directory is created when the user logs in, is only accessible by the |
| 98 |
user itself, is deleted when the user logs out, and supports all filesystem features of the |
| 99 |
operating system.<br/> |
| 100 |
As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. |
| 101 |
|
| 102 |
## Features |
| 103 |
|
| 104 |
**If you want to compute the location of cache, config or data directories for your own application or project, |
| 105 |
use `ProjectDirs` of the [directories](https://github.com/dirs-dev/directories-rs) project instead.** |
| 106 |
|
| 107 |
| Function name | Value on Linux/Redox | Value on Windows | Value on macOS | |
| 108 |
| ---------------- | ---------------------------------------------------------------------- | --------------------------------- | ------------------------------------------- | |
| 109 |
| `home_dir` | `Some($HOME)` | `Some({FOLDERID_Profile})` | `Some($HOME)` | |
| 110 |
| `cache_dir` | `Some($XDG_CACHE_HOME)` or `Some($HOME`/.cache`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Caches`)` | |
| 111 |
| `config_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` | |
| 112 |
| `data_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` | |
| 113 |
| `data_local_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Application Support`)` | |
| 114 |
| `executable_dir` | `Some($XDG_BIN_HOME)` or `Some($HOME`/.local/bin`)` | `None` | `None` | |
| 115 |
| `preference_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Preferences`)` | |
| 116 |
| `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` | |
| 117 |
| `state_dir` | `Some($XDG_STATE_HOME)` or `Some($HOME`/.local/state`)` | `None` | `None` | |
| 118 |
| `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` | |
| 119 |
| `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` | |
| 120 |
| `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` | |
| 121 |
| `download_dir` | `Some(XDG_DOWNLOAD_DIR)` or `None` | `Some({FOLDERID_Downloads})` | `Some($HOME`/Downloads/`)` | |
| 122 |
| `font_dir` | `Some($XDG_DATA_HOME`/fonts/`)` or `Some($HOME`/.local/share/fonts/`)` | `None` | `Some($HOME`/Library/Fonts/`)` | |
| 123 |
| `picture_dir` | `Some(XDG_PICTURES_DIR)` or `None` | `Some({FOLDERID_Pictures})` | `Some($HOME`/Pictures/`)` | |
| 124 |
| `public_dir` | `Some(XDG_PUBLICSHARE_DIR)` or `None` | `Some({FOLDERID_Public})` | `Some($HOME`/Public/`)` | |
| 125 |
| `template_dir` | `Some(XDG_TEMPLATES_DIR)` or `None` | `Some({FOLDERID_Templates})` | `None` | |
| 126 |
| `video_dir` | `Some(XDG_VIDEOS_DIR)` or `None` | `Some({FOLDERID_Videos})` | `Some($HOME`/Movies/`)` | |
| 127 |
|
| 128 |
## Comparison |
| 129 |
|
| 130 |
There are other crates in the Rust ecosystem that try similar or related things. |
| 131 |
Here is an overview of them, combined with ratings on properties that guided the design of this crate. |
| 132 |
|
| 133 |
Please take this table with a grain of salt: a different crate might very well be more suitable for your specific use case. |
| 134 |
(Of course _my_ crate achieves _my_ design goals better than other crates, which might have had different design goals.) |
| 135 |
|
| 136 |
| Library | Status | Lin | Mac | Win |Base|User|Proj|Conv| |
| 137 |
| --------------------------------------------------------- | -------------- |:---:|:---:|:---:|:--:|:--:|:--:|:--:| |
| 138 |
| [app_dirs](https://crates.io/crates/app_dirs) | Unmaintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ | |
| 139 |
| [app_dirs2](https://crates.io/crates/app_dirs2) | Maintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ | |
| 140 |
| **dirs** | **Developed** | ✔ | ✔ | ✔ | ✔ | ✔ | ✖ | ✔ | |
| 141 |
| [directories](https://crates.io/crates/directories) | Developed | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |
| 142 |
| [s_app_dir](https://crates.io/crates/s_app_dir) | Unmaintained? | ✔ | ✖ | 🞈 | ✖ | ✖ | 🞈 | ✖ | |
| 143 |
| [standard_paths](https://crates.io/crates/standard_paths) | Maintained | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ | |
| 144 |
| [xdg](https://crates.io/crates/xdg) | Maintained | ✔ | ✖ | ✖ | ✔ | ✖ | ✔ | 🞈 | |
| 145 |
| [xdg-basedir](https://crates.io/crates/xdg-basedir) | Unmaintained? | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 | |
| 146 |
| [xdg-rs](https://crates.io/crates/xdg-rs) | Obsolete | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 | |
| 147 |
|
| 148 |
- Lin: Linux support |
| 149 |
- Mac: macOS support |
| 150 |
- Win: Windows support |
| 151 |
- Base: Supports [generic base directories](https://github.com/dirs-dev/directories-rs#basedirs) |
| 152 |
- User: Supports [user directories](https://github.com/dirs-dev/directories-rs#userdirs) |
| 153 |
- Proj: Supports [project-specific base directories](https://github.com/dirs-dev/directories-rs#projectdirs) |
| 154 |
- Conv: Follows naming conventions of the operating system it runs on |
| 155 |
|
| 156 |
## Build |
| 157 |
|
| 158 |
It's possible to cross-compile this library if the necessary toolchains are installed with rustup. |
| 159 |
This is helpful to ensure a change hasn't broken code on a different platform. |
| 160 |
|
| 161 |
The following commands will build this library on Linux, macOS and Windows: |
| 162 |
|
| 163 |
``` |
| 164 |
cargo build --target=x86_64-unknown-linux-gnu |
| 165 |
cargo build --target=x86_64-pc-windows-gnu |
| 166 |
cargo build --target=x86_64-apple-darwin |
| 167 |
cargo build --target=x86_64-unknown-redox |
| 168 |
``` |
| 169 |
|
| 170 |
## Changelog |
| 171 |
|
| 172 |
### 4 |
| 173 |
|
| 174 |
- **BREAKING CHANGE** The behavior of `executable_dir` has been adjusted to not depend on `$XDG_DATA_HOME`. |
| 175 |
Code, which assumed that setting the `$XDG_DATA_HOME` environment variable also impacted `executable_dir` if |
| 176 |
the `$XDG_BIN_HOME` environment variable was not set, requires adjustment. |
| 177 |
- Add support for `XDG_STATE_HOME`. |
| 178 |
|
| 179 |
### 3 |
| 180 |
|
| 181 |
- **BREAKING CHANGE** The behavior of `config_dir` on macOS has been adjusted |
| 182 |
(thanks to [everyone involved](https://github.com/dirs-dev/directories-rs/issues/62)): |
| 183 |
- The existing `config_dir` function has been changed to return the `Application Support` |
| 184 |
directory on macOS, as suggested by Apple documentation. |
| 185 |
- The behavior of the `config_dir` function on non-macOS platforms has not been changed. |
| 186 |
- If you have used the `config_dir` function to store files, it may be necessary to write code |
| 187 |
that migrates the files to the new location on macOS.<br/> |
| 188 |
(Alternative: change uses of the `config_dir` function to uses of the `preference_dir` function |
| 189 |
to retain the old behavior.) |
| 190 |
- The newly added `preference_dir` function returns the `Preferences` directory on macOS now, |
| 191 |
which – according to Apple documentation – shall only be used to store .plist files using |
| 192 |
Apple-proprietary APIs. |
| 193 |
– `preference_dir` and `config_dir` behave identical on non-macOS platforms. |
| 194 |
|
| 195 |
### 2 |
| 196 |
|
| 197 |
**BREAKING CHANGE** The behavior of deactivated, missing or invalid [_XDG User Dirs_](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) |
| 198 |
entries on Linux has been improved (contributed by @tmiasko, thank you!): |
| 199 |
|
| 200 |
- Version 1 returned the user's home directory (`Some($HOME)`) for such faulty entries, except for a faulty `XDG_DESKTOP_DIR` entry which returned (`Some($HOME/Desktop)`). |
| 201 |
- Version 2 returns `None` for such entries. |
| 202 |
|
| 203 |
## License |
| 204 |
|
| 205 |
Licensed under either of |
| 206 |
|
| 207 |
* Apache License, Version 2.0 |
| 208 |
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 209 |
* MIT license |
| 210 |
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 211 |
|
| 212 |
at your option. |
| 213 |
|
| 214 |
## Contribution |
| 215 |
|
| 216 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 217 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 218 |
dual licensed as above, without any additional terms or conditions. |