1[![crates.io](https://img.shields.io/crates/v/directories.svg)](https://crates.io/crates/directories) 2[![API documentation](https://docs.rs/directories/badge.svg)](https://docs.rs/directories/) 3![actively developed](https://img.shields.io/badge/maintenance-actively--developed-brightgreen.svg) 4[![TravisCI status](https://img.shields.io/travis/dirs-dev/directories-rs/master.svg?label=Linux/macOS%20build)](https://travis-ci.org/dirs-dev/directories-rs) 5[![AppVeyor status](https://img.shields.io/appveyor/ci/soc/directories-rs/master.svg?label=Windows%20build)](https://ci.appveyor.com/project/soc/directories-rs/branch/master) 6![License: MIT/Apache-2.0](https://img.shields.io/badge/license-MIT%2FApache--2.0-orange.svg) 7 8# `directories` 9 10## Introduction 11 12- a tiny mid-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 17The 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 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 26This library is written in Rust, and supports Linux, Redox, macOS and Windows. 27Other platforms are also supported; they use the Linux conventions. 28 29_dirs_, the low-level sister library, is available at [dirs-rs](https://github.com/soc/dirs-rs). 30 31A version of this library running on the JVM is provided by [directories-jvm](https://github.com/soc/directories-jvm). 32 33## Usage 34 35#### Dependency 36 37Add the library as a dependency to your project by inserting 38 39```toml 40directories = "3.0" 41``` 42 43into the `[dependencies]` section of your Cargo.toml file. 44 45If you are upgrading from version 2, please read the [section on breaking changes](#3) first. 46 47#### Example 48 49Library run by user Alice: 50 51```rust 52extern crate directories; 53use directories::{BaseDirs, UserDirs, ProjectDirs}; 54 55if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { 56 proj_dirs.config_dir(); 57 // Lin: /home/alice/.config/barapp 58 // Win: C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config 59 // Mac: /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App 60} 61 62if let Some(base_dirs) = BaseDirs::new() { 63 base_dirs.executable_dir(); 64 // Lin: Some(/home/alice/.local/bin) 65 // Win: None 66 // Mac: None 67} 68 69if let Some(user_dirs) = UserDirs::new() { 70 user_dirs.audio_dir(); 71 // Lin: /home/alice/Music 72 // Win: C:\Users\Alice\Music 73 // Mac: /Users/Alice/Music 74} 75``` 76 77## Design Goals 78 79- The _directories_ library is designed to provide an accurate snapshot of the system's state at 80 the point of invocation of `BaseDirs::new`, `UserDirs::new` or `ProjectDirs::from`.<br/> 81 Subsequent changes to the state of the system are not reflected in values created prior to such a change. 82- This library does not create directories or check for their existence. The library only provides 83 information on what the path to a certain directory _should_ be.<br/> 84 How this information is used is a decision that developers need to make based on the requirements 85 of each individual application. 86- This library is intentionally focused on providing information on user-writable directories only, 87 as there is no discernible benefit in returning a path that points to a user-level, writable 88 directory on one operating system, but a system-level, read-only directory on another.<br/> 89 The confusion and unexpected failure modes of such an approach would be immense. 90 - `executable_dir` is specified to provide the path to a user-writable directory for binaries.<br/> 91 As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. 92 - `font_dir` is specified to provide the path to a user-writable directory for fonts.<br/> 93 As such a directory only exists on Linux and macOS, it returns `None` on Windows. 94 - `runtime_dir` is specified to provide the path to a directory for non-essential runtime data. 95 It is required that this directory is created when the user logs in, is only accessible by the 96 user itself, is deleted when the user logs out, and supports all filesystem features of the 97 operating system.<br/> 98 As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. 99 100## Features 101 102### `BaseDirs` 103 104The intended use case for `BaseDirs` is to query the paths of user-invisible standard directories 105that have been defined according to the conventions of the operating system the library is running on. 106 107If you want to compute the location of cache, config or data directories for your own application or project, use `ProjectDirs` instead. 108 109| Function name | Value on Linux | Value on Windows | Value on macOS | 110| ---------------- | ----------------------------------------------------------------------------------------------- | --------------------------- | ----------------------------------- | 111| `home_dir` | `$HOME` | `{FOLDERID_Profile}` | `$HOME` | 112| `cache_dir` | `$XDG_CACHE_HOME` or `$HOME`/.cache | `{FOLDERID_LocalAppData}` | `$HOME`/Library/Caches | 113| `config_dir` | `$XDG_CONFIG_HOME` or `$HOME`/.config | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Application Support | 114| `data_dir` | `$XDG_DATA_HOME` or `$HOME`/.local/share | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Application Support | 115| `data_local_dir` | `$XDG_DATA_HOME` or `$HOME`/.local/share | `{FOLDERID_LocalAppData}` | `$HOME`/Library/Application Support | 116| `executable_dir` | `Some($XDG_BIN_HOME`/../bin`)` or `Some($XDG_DATA_HOME`/../bin`)` or `Some($HOME`/.local/bin`)` | `None` | `None` | 117| `preference_dir` | `$XDG_CONFIG_HOME` or `$HOME`/.config | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Preferences | 118| `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` | 119 120### `UserDirs` 121 122The intended use case for `UserDirs` is to query the paths of user-facing standard directories 123that have been defined according to the conventions of the operating system the library is running on. 124 125| Function name | Value on Linux | Value on Windows | Value on macOS | 126| ---------------- | ---------------------------------------------------------------------- | -------------------------------- | ------------------------------ | 127| `home_dir` | `$HOME` | `{FOLDERID_Profile}` | `$HOME` | 128| `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` | 129| `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` | 130| `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` | 131| `download_dir` | `Some(XDG_DOWNLOAD_DIR)` or `None` | `Some({FOLDERID_Downloads})` | `Some($HOME`/Downloads/`)` | 132| `font_dir` | `Some($XDG_DATA_HOME`/fonts/`)` or `Some($HOME`/.local/share/fonts/`)` | `None` | `Some($HOME`/Library/Fonts/`)` | 133| `picture_dir` | `Some(XDG_PICTURES_DIR)` or `None` | `Some({FOLDERID_Pictures})` | `Some($HOME`/Pictures/`)` | 134| `public_dir` | `Some(XDG_PUBLICSHARE_DIR)` or `None` | `Some({FOLDERID_Public})` | `Some($HOME`/Public/`)` | 135| `template_dir` | `Some(XDG_TEMPLATES_DIR)` or `None` | `Some({FOLDERID_Templates})` | `None` | 136| `video_dir` | `Some(XDG_VIDEOS_DIR)` or `None` | `Some({FOLDERID_Videos})` | `Some($HOME`/Movies/`)` | 137 138### `ProjectDirs` 139 140The intended use case for `ProjectDirs` is to compute the location of cache, config or data directories for your own application or project, 141which are derived from the standard directories. 142 143| Function name | Value on Linux | Value on Windows | Value on macOS | 144| ---------------- | ---------------------------------------------------------------------------------- | --------------------------------------------------- | ---------------------------------------------------- | 145| `cache_dir` | `$XDG_CACHE_HOME`/`<project_path>` or `$HOME`/.cache/`<project_path>` | `{FOLDERID_LocalAppData}`/`<project_path>`/cache | `$HOME`/Library/Caches/`<project_path>` | 146| `config_dir` | `$XDG_CONFIG_HOME`/`<project_path>` or `$HOME`/.config/`<project_path>` | `{FOLDERID_RoamingAppData}`/`<project_path>`/config | `$HOME`/Library/Application Support/`<project_path>` | 147| `data_dir` | `$XDG_DATA_HOME`/`<project_path>` or `$HOME`/.local/share/`<project_path>` | `{FOLDERID_RoamingAppData}`/`<project_path>`/data | `$HOME`/Library/Application Support/`<project_path>` | 148| `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>` | 149| `preference_dir` | `$XDG_CONFIG_HOME`/`<project_path>` or `$HOME`/.config/`<project_path>` | `{FOLDERID_RoamingAppData}`/`<project_path>`/config | `$HOME`/Library/Preferences/`<project_path>` | 150| `runtime_dir` | `Some($XDG_RUNTIME_DIR`/`_project_path_)` | `None` | `None` | 151 152The specific value of `<project_path>` is computed by the 153 154 ProjectDirs::from(qualifier: &str, 155 organization: &str, 156 application: &str) 157 158function and varies across operating systems. As an example, calling 159 160 ProjectDirs::from("org" /*qualifier*/, 161 "Baz Corp" /*organization*/, 162 "Foo Bar-App" /*application*/) 163 164results in the following values: 165 166| Value on Linux | Value on Windows | Value on macOS | 167| -------------- | ------------------------ | ---------------------------- | 168| `"foobar-app"` | `"Baz Corp/Foo Bar-App"` | `"org.Baz-Corp.Foo-Bar-App"` | 169 170The `ProjectDirs::from_path` function allows the creation of `ProjectDirs` structs directly from a `PathBuf` value. 171This argument is used verbatim and is not adapted to operating system standards. 172 173The use of `ProjectDirs::from_path` is strongly discouraged, as its results will not follow operating system standards on at least two of three platforms. 174 175## Comparison 176 177There are other crates in the Rust ecosystem that try similar or related things. 178Here is an overview of them, combined with ratings on properties that guided the design of this crate. 179 180Please take this table with a grain of salt: a different crate might very well be more suitable for your specific use case. 181(Of course _my_ crate achieves _my_ design goals better than other crates, which might have had different design goals.) 182 183| Library | Status | Lin | Mac | Win |Base|User|Proj|Conv| 184| --------------------------------------------------------- | -------------- |:---:|:---:|:---:|:--:|:--:|:--:|:--:| 185| [app_dirs](https://crates.io/crates/app_dirs) | Unmaintained | ✔ | ✔ | ✔ | | ✖ | ✔ | ✖ | 186| [app_dirs2](https://crates.io/crates/app_dirs2) | Maintained | ✔ | ✔ | ✔ | | ✖ | ✔ | ✖ | 187| [dirs](https://crates.io/crates/dirs) | Developed | ✔ | ✔ | ✔ | ✔ | ✔ | ✖ | ✔ | 188| **directories** | **Developed** | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | 189| [s_app_dir](https://crates.io/crates/s_app_dir) | Unmaintained? | ✔ | ✖ | | ✖ | ✖ | | ✖ | 190| [standard_paths](https://crates.io/crates/standard_paths) | Maintained | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ | 191| [xdg](https://crates.io/crates/xdg) | Maintained | ✔ | ✖ | ✖ | ✔ | ✖ | ✔ | | 192| [xdg-basedir](https://crates.io/crates/xdg-basedir) | Unmaintained? | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | | 193| [xdg-rs](https://crates.io/crates/xdg-rs) | Obsolete | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | | 194 195- Lin: Linux support 196- Mac: macOS support 197- Win: Windows support 198- Base: Supports [generic base directories](#basedirs) 199- User: Supports [user directories](#userdirs) 200- Proj: Supports [project-specific base directories](#projectdirs) 201- Conv: Follows naming conventions of the operating system it runs on 202 203## Build 204 205It's possible to cross-compile this library if the necessary toolchains are installed with rustup. 206This is helpful to ensure a change has not broken compilation on a different platform. 207 208The following commands will build this library on Linux, macOS and Windows: 209 210``` 211cargo build --target=x86_64-unknown-linux-gnu 212cargo build --target=x86_64-pc-windows-gnu 213cargo build --target=x86_64-apple-darwin 214cargo build --target=x86_64-unknown-redox 215``` 216 217## Changelog 218 219### 3 220 221- **BREAKING CHANGE** The behavior of the `BaseDirs::config_dir` and `ProjectDirs::config_dir` 222 on macOS has been adjusted (thanks to [everyone involved](https://github.com/dirs-dev/directories-rs/issues/62)): 223 - The existing `config_dir` functions have been changed to return the `Application Support` 224 directory on macOS, as suggested by Apple documentation. 225 - The behavior of the `config_dir` functions on non-macOS platforms has not been changed. 226 - If you have used the `config_dir` functions to store files, it may be necessary to write code 227 that migrates the files to the new location on macOS.<br/> 228 (Alternative: change uses of the `config_dir` functions to uses of the `preference_dir` functions 229 to retain the old behavior.) 230- The newly added `BaseDirs::preference_dir` and `ProjectDirs::preference_dir` functions returns 231 the `Preferences` directory on macOS now, which – according to Apple documentation – shall only 232 be used to store .plist files using Apple-proprietary APIs. 233 – `preference_dir` and `config_dir` behave identical on non-macOS platforms. 234 235### 2 236 237The behavior of deactivated, missing or invalid [_XDG User Dirs_](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) 238entries on Linux has been improved (contributed by @tmiasko, thank you!): 239 240- 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)`). 241- Version 2 returns `None` for such entries. 242 243## License 244 245Licensed under either of 246 247 * Apache License, Version 2.0 248 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 249 * MIT license 250 ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 251 252at your option. 253 254## Contribution 255 256Unless you explicitly state otherwise, any contribution intentionally submitted 257for inclusion in the work by you, as defined in the Apache-2.0 license, shall be 258dual licensed as above, without any additional terms or conditions. 259