Import xdg 2.4.1 upstream upstream/2.4.1
authorDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 4 Apr 2023 07:20:47 +0000 (16:20 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 4 Apr 2023 07:20:47 +0000 (16:20 +0900)
36 files changed:
.cargo_vcs_info.json [new file with mode: 0644]
.gitignore [new file with mode: 0644]
.travis.yml [new file with mode: 0644]
Cargo.toml [new file with mode: 0644]
Cargo.toml.orig [new file with mode: 0644]
LICENSE-APACHE [new file with mode: 0644]
LICENSE-MIT [new file with mode: 0644]
README.md [new file with mode: 0644]
src/lib.rs [new file with mode: 0644]
test_files/runtime-bad/everywhere [new file with mode: 0644]
test_files/runtime-bad/runtime.file [new file with mode: 0644]
test_files/symlinks/config/myapp/default_profile/user_config.file [new file with mode: 0644]
test_files/symlinks/config/myapp/user_config.file [new file with mode: 0644]
test_files/system1/config/both_system_config.file [new file with mode: 0644]
test_files/system1/config/everywhere [new file with mode: 0644]
test_files/system1/config/myapp/default_profile/system1_config.file [new file with mode: 0644]
test_files/system1/config/myapp/system1_config.file [new file with mode: 0644]
test_files/system1/config/system1_config.file [new file with mode: 0644]
test_files/system1/data/both_system_data.file [new file with mode: 0644]
test_files/system1/data/everywhere [new file with mode: 0644]
test_files/system1/data/system1_data.file [new file with mode: 0644]
test_files/system2/config/both_system_config.file [new file with mode: 0644]
test_files/system2/config/everywhere [new file with mode: 0644]
test_files/system2/config/system2_config.file [new file with mode: 0644]
test_files/system2/data/both_system_data.file [new file with mode: 0644]
test_files/system2/data/everywhere [new file with mode: 0644]
test_files/system2/data/system2_data.file [new file with mode: 0644]
test_files/user/cache/everywhere [new file with mode: 0644]
test_files/user/cache/user_cache.file [new file with mode: 0644]
test_files/user/config/everywhere [new file with mode: 0644]
test_files/user/config/myapp/default_profile/user_config.file [new file with mode: 0644]
test_files/user/config/myapp/user_config.file [new file with mode: 0644]
test_files/user/config/user_config.file [new file with mode: 0644]
test_files/user/data/everywhere [new file with mode: 0644]
test_files/user/data/user_data.file [new file with mode: 0644]
test_files/user/runtime/user_runtime.file [new file with mode: 0644]

diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
new file mode 100644 (file)
index 0000000..333e6af
--- /dev/null
@@ -0,0 +1,5 @@
+{
+  "git": {
+    "sha1": "ce43f34c4684601208c645651a63b06d5f5a5cf5"
+  }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..e9e2199
--- /dev/null
@@ -0,0 +1,2 @@
+/target/
+/Cargo.lock
diff --git a/.travis.yml b/.travis.yml
new file mode 100644 (file)
index 0000000..cb5d7bb
--- /dev/null
@@ -0,0 +1,13 @@
+language: rust
+rust:
+  - stable
+env:
+  - secure: "ZmdqljmHk9oOyCJWLKWJ4ayl/7rExqss+1ZIw26zeKontCdWQ9K5NvGmbk69mD9BL07aX1O1jIaSiSrqIqV6f8/BsguADvdBUyqfdLAXQy28we+17xTphmS6vh3uT1ayqGBBhB3Qy/6JnEBp5vFdcMbwSXq02+/MwCp/wbozk4junnJ0Gg0BOK/6FUQBBnlflg1Tm5KS3pZw5H7/7GtNRW4K9ApM6EBHqBhEc8GGemCoh6q2WUabSnaxev1Bql5ZogN7wDrf5Ohpvzqv5wU25aWwRc3eJngGRelHmlGtCS1VpWSZHE4nahWj6nLIsW6Kr620Dy2ZPvNuQ/+OCC/eKNPifRlgMN2Ff1eRz9irQ6sc+KCyDmaMpAA92f1IF7y9AolGKjiTWYxKMT+0g7lbPV+WoE1+pfbsZcyOGGMLxzxxlYxSukgXN55Eqs8ag9jakjP0GtCVU3tL4nO6zyq7+UhTleWPXuTKwG64C7q3UZZUgpayzzKcJdcCdozEwuokF/kI1+o4AiF65uFw+SrQ07okJnUm86p3QhRtNSdTY6hbbLGuELQ5DbikkKoavV8WdjjqViy4tU2WafdJvA67sZvEP4h6eVvAWz+/3uZoAJyXFbfulq40XB+Fzxl/5ionnM9md86UDsX9W64dSVl+2vovj+5Jejm3wQLsuJheVG4="
+after_script:
+  - |
+    if [ "${TRAVIS_BRANCH}" = "master" -a -n "${GH_TOKEN}" ]; then
+      cargo doc
+      git clone https://github.com/davisp/ghp-import
+      ./ghp-import/ghp_import.py -n target/doc
+      git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
+    fi
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644 (file)
index 0000000..bfb28eb
--- /dev/null
@@ -0,0 +1,28 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+name = "xdg"
+version = "2.4.1"
+authors = ["Ben Longbons <b.r.longbons@gmail.com>", "whitequark <whitequark@whitequark.org>"]
+description = "A library for storing and retrieving files according to XDG Base Directory specification"
+homepage = "https://github.com/whitequark/rust-xdg"
+documentation = "https://docs.rs/xdg/"
+readme = "README.md"
+keywords = ["linux", "configuration"]
+license = "Apache-2.0/MIT"
+repository = "https://github.com/whitequark/rust-xdg"
+
+[lib]
+doctest = false
+[dependencies.dirs]
+version = "4.0"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
new file mode 100644 (file)
index 0000000..d517e5d
--- /dev/null
@@ -0,0 +1,20 @@
+[package]
+name = "xdg"
+version = "2.4.1"
+description = "A library for storing and retrieving files according to XDG Base Directory specification"
+homepage = "https://github.com/whitequark/rust-xdg"
+repository = "https://github.com/whitequark/rust-xdg"
+documentation = "https://docs.rs/xdg/"
+readme = "README.md"
+keywords = ["linux", "configuration"]
+license = "Apache-2.0/MIT"
+authors = [
+    "Ben Longbons <b.r.longbons@gmail.com>",
+    "whitequark <whitequark@whitequark.org>"
+]
+
+[lib]
+doctest = false
+
+[dependencies]
+dirs = "4.0"
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644 (file)
index 0000000..16fe87b
--- /dev/null
@@ -0,0 +1,201 @@
+                              Apache License
+                        Version 2.0, January 2004
+                     http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+   "License" shall mean the terms and conditions for use, reproduction,
+   and distribution as defined by Sections 1 through 9 of this document.
+
+   "Licensor" shall mean the copyright owner or entity authorized by
+   the copyright owner that is granting the License.
+
+   "Legal Entity" shall mean the union of the acting entity and all
+   other entities that control, are controlled by, or are under common
+   control with that entity. For the purposes of this definition,
+   "control" means (i) the power, direct or indirect, to cause the
+   direction or management of such entity, whether by contract or
+   otherwise, or (ii) ownership of fifty percent (50%) or more of the
+   outstanding shares, or (iii) beneficial ownership of such entity.
+
+   "You" (or "Your") shall mean an individual or Legal Entity
+   exercising permissions granted by this License.
+
+   "Source" form shall mean the preferred form for making modifications,
+   including but not limited to software source code, documentation
+   source, and configuration files.
+
+   "Object" form shall mean any form resulting from mechanical
+   transformation or translation of a Source form, including but
+   not limited to compiled object code, generated documentation,
+   and conversions to other media types.
+
+   "Work" shall mean the work of authorship, whether in Source or
+   Object form, made available under the License, as indicated by a
+   copyright notice that is included in or attached to the work
+   (an example is provided in the Appendix below).
+
+   "Derivative Works" shall mean any work, whether in Source or Object
+   form, that is based on (or derived from) the Work and for which the
+   editorial revisions, annotations, elaborations, or other modifications
+   represent, as a whole, an original work of authorship. For the purposes
+   of this License, Derivative Works shall not include works that remain
+   separable from, or merely link (or bind by name) to the interfaces of,
+   the Work and Derivative Works thereof.
+
+   "Contribution" shall mean any work of authorship, including
+   the original version of the Work and any modifications or additions
+   to that Work or Derivative Works thereof, that is intentionally
+   submitted to Licensor for inclusion in the Work by the copyright owner
+   or by an individual or Legal Entity authorized to submit on behalf of
+   the copyright owner. For the purposes of this definition, "submitted"
+   means any form of electronic, verbal, or written communication sent
+   to the Licensor or its representatives, including but not limited to
+   communication on electronic mailing lists, source code control systems,
+   and issue tracking systems that are managed by, or on behalf of, the
+   Licensor for the purpose of discussing and improving the Work, but
+   excluding communication that is conspicuously marked or otherwise
+   designated in writing by the copyright owner as "Not a Contribution."
+
+   "Contributor" shall mean Licensor and any individual or Legal Entity
+   on behalf of whom a Contribution has been received by Licensor and
+   subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   copyright license to reproduce, prepare Derivative Works of,
+   publicly display, publicly perform, sublicense, and distribute the
+   Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   (except as stated in this section) patent license to make, have made,
+   use, offer to sell, sell, import, and otherwise transfer the Work,
+   where such license applies only to those patent claims licensable
+   by such Contributor that are necessarily infringed by their
+   Contribution(s) alone or by combination of their Contribution(s)
+   with the Work to which such Contribution(s) was submitted. If You
+   institute patent litigation against any entity (including a
+   cross-claim or counterclaim in a lawsuit) alleging that the Work
+   or a Contribution incorporated within the Work constitutes direct
+   or contributory patent infringement, then any patent licenses
+   granted to You under this License for that Work shall terminate
+   as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+   Work or Derivative Works thereof in any medium, with or without
+   modifications, and in Source or Object form, provided that You
+   meet the following conditions:
+
+   (a) You must give any other recipients of the Work or
+       Derivative Works a copy of this License; and
+
+   (b) You must cause any modified files to carry prominent notices
+       stating that You changed the files; and
+
+   (c) You must retain, in the Source form of any Derivative Works
+       that You distribute, all copyright, patent, trademark, and
+       attribution notices from the Source form of the Work,
+       excluding those notices that do not pertain to any part of
+       the Derivative Works; and
+
+   (d) If the Work includes a "NOTICE" text file as part of its
+       distribution, then any Derivative Works that You distribute must
+       include a readable copy of the attribution notices contained
+       within such NOTICE file, excluding those notices that do not
+       pertain to any part of the Derivative Works, in at least one
+       of the following places: within a NOTICE text file distributed
+       as part of the Derivative Works; within the Source form or
+       documentation, if provided along with the Derivative Works; or,
+       within a display generated by the Derivative Works, if and
+       wherever such third-party notices normally appear. The contents
+       of the NOTICE file are for informational purposes only and
+       do not modify the License. You may add Your own attribution
+       notices within Derivative Works that You distribute, alongside
+       or as an addendum to the NOTICE text from the Work, provided
+       that such additional attribution notices cannot be construed
+       as modifying the License.
+
+   You may add Your own copyright statement to Your modifications and
+   may provide additional or different license terms and conditions
+   for use, reproduction, or distribution of Your modifications, or
+   for any such Derivative Works as a whole, provided Your use,
+   reproduction, and distribution of the Work otherwise complies with
+   the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+   any Contribution intentionally submitted for inclusion in the Work
+   by You to the Licensor shall be under the terms and conditions of
+   this License, without any additional terms or conditions.
+   Notwithstanding the above, nothing herein shall supersede or modify
+   the terms of any separate license agreement you may have executed
+   with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+   names, trademarks, service marks, or product names of the Licensor,
+   except as required for reasonable and customary use in describing the
+   origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+   agreed to in writing, Licensor provides the Work (and each
+   Contributor provides its Contributions) on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+   implied, including, without limitation, any warranties or conditions
+   of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+   PARTICULAR PURPOSE. You are solely responsible for determining the
+   appropriateness of using or redistributing the Work and assume any
+   risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+   whether in tort (including negligence), contract, or otherwise,
+   unless required by applicable law (such as deliberate and grossly
+   negligent acts) or agreed to in writing, shall any Contributor be
+   liable to You for damages, including any direct, indirect, special,
+   incidental, or consequential damages of any character arising as a
+   result of this License or out of the use or inability to use the
+   Work (including but not limited to damages for loss of goodwill,
+   work stoppage, computer failure or malfunction, or any and all
+   other commercial damages or losses), even if such Contributor
+   has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+   the Work or Derivative Works thereof, You may choose to offer,
+   and charge a fee for, acceptance of support, warranty, indemnity,
+   or other liability obligations and/or rights consistent with this
+   License. However, in accepting such obligations, You may act only
+   on Your own behalf and on Your sole responsibility, not on behalf
+   of any other Contributor, and only if You agree to indemnify,
+   defend, and hold each Contributor harmless for any liability
+   incurred by, or claims asserted against, such Contributor by reason
+   of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+   To apply the Apache License to your work, attach the following
+   boilerplate notice, with the fields enclosed by brackets "[]"
+   replaced with your own identifying information. (Don't include
+   the brackets!)  The text should be enclosed in the appropriate
+   comment syntax for the file format. We also recommend that a
+   file or class name and description of purpose be included on the
+   same "printed page" as the copyright notice for easier
+   identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644 (file)
index 0000000..39d4bdb
--- /dev/null
@@ -0,0 +1,25 @@
+Copyright (c) 2014 The Rust Project Developers
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..996c9cd
--- /dev/null
+++ b/README.md
@@ -0,0 +1,33 @@
+rust-xdg
+========
+
+rust-xdg is a library that makes it easy to follow the X Desktop Group
+specifications.
+
+Currently, only [XDG Base Directory][basedir] specification is implemented.
+
+[basedir]: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
+
+Installation
+------------
+
+Add the following to `Cargo.toml`:
+
+```toml
+[dependencies]
+xdg = "^2.1"
+```
+
+Examples
+--------
+
+See [documentation](https://whitequark.github.io/rust-xdg/xdg/).
+
+License
+-------
+
+rust-xdg is distributed under the terms of both the MIT license
+and the Apache License (Version 2.0).
+
+See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT)
+for details.
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644 (file)
index 0000000..2c95961
--- /dev/null
@@ -0,0 +1,1018 @@
+#![cfg(any(unix, target_os = "redox"))]
+
+extern crate dirs;
+
+use std::fmt;
+use std::convert;
+use std::error;
+use std::io;
+use std::env;
+use std::fs;
+use std::path::{Path, PathBuf};
+use std::ffi::OsString;
+
+use std::os::unix::fs::PermissionsExt;
+
+use BaseDirectoriesErrorKind::*;
+use BaseDirectoriesError as Error;
+
+/// BaseDirectories allows to look up paths to configuration, data,
+/// cache and runtime files in well-known locations according to
+/// the [X Desktop Group Base Directory specification][xdg-basedir].
+/// [xdg-basedir]: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
+///
+/// The Base Directory specification defines five kinds of files:
+///
+///   * **Configuration files** store the application's settings and
+///     are often modified during runtime;
+///   * **Data files** store supplementary data, such as graphic assets,
+///     precomputed tables, documentation, or architecture-independent
+///     source code;
+///   * **Cache files** store non-essential, transient data that provides
+///     a runtime speedup;
+///   * **State files** store logs, history, recently used files and application
+///     state (window size, open files, unsaved changes, â€¦);
+///   * **Runtime files** include filesystem objects such are sockets or
+///     named pipes that are used for communication internal to the application.
+///     Runtime files must not be accessible to anyone except current user.
+///
+/// # Examples
+///
+/// To configure paths for application `myapp`:
+///
+/// ```
+/// extern crate xdg;
+/// let xdg_dirs = xdg::BaseDirectories::with_prefix("myapp").unwrap();
+/// ```
+///
+/// To store configuration:
+///
+/// ```
+/// let config_path = xdg_dirs.place_config_file("config.ini")
+///                           .expect("cannot create configuration directory");
+/// let mut config_file = File::create(config_path)?;
+/// write!(&mut config_file, "configured = 1")?;
+/// ```
+///
+/// The `config.ini` file will appear in the proper location for desktop
+/// configuration files, most likely `~/.config/myapp/config.ini`.
+/// The leading directories will be automatically created.
+///
+/// To retrieve supplementary data:
+///
+/// ```
+/// let logo_path = xdg_dirs.find_data_file("logo.png")
+///                         .expect("application data not present");
+/// let mut logo_file = File::open(logo_path)?;
+/// let mut logo = Vec::new();
+/// logo_file.read_to_end(&mut logo)?;
+/// ```
+///
+/// The `logo.png` will be searched in the proper locations for
+/// supplementary data files, most likely `~/.local/share/myapp/logo.png`,
+/// then `/usr/local/share/myapp/logo.png` and `/usr/share/myapp/logo.png`.
+#[derive(Debug, Clone)]
+pub struct BaseDirectories {
+    shared_prefix: PathBuf,
+    user_prefix: PathBuf,
+    data_home: PathBuf,
+    config_home: PathBuf,
+    cache_home: PathBuf,
+    state_home: PathBuf,
+    data_dirs: Vec<PathBuf>,
+    config_dirs: Vec<PathBuf>,
+    runtime_dir: Option<PathBuf>,
+}
+
+pub struct BaseDirectoriesError {
+    kind: BaseDirectoriesErrorKind,
+}
+
+impl BaseDirectoriesError {
+    fn new(kind: BaseDirectoriesErrorKind) -> BaseDirectoriesError {
+        BaseDirectoriesError {
+            kind: kind,
+        }
+    }
+}
+
+impl fmt::Debug for BaseDirectoriesError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.kind.fmt(f)
+    }
+}
+
+impl error::Error for BaseDirectoriesError {
+    fn description(&self) -> &str {
+        match self.kind {
+            HomeMissing => "$HOME must be set",
+            XdgRuntimeDirInaccessible(_, _) =>
+                "$XDG_RUNTIME_DIR must be accessible by the current user",
+            XdgRuntimeDirInsecure(_, _) =>
+                "$XDG_RUNTIME_DIR must be secure: have permissions 0700",
+            XdgRuntimeDirMissing =>
+                "$XDG_RUNTIME_DIR is not set",
+        }
+    }
+    fn cause(&self) -> Option<&dyn error::Error> {
+        match self.kind {
+            XdgRuntimeDirInaccessible(_, ref e) => Some(e),
+            _ => None,
+        }
+    }
+}
+
+impl fmt::Display for BaseDirectoriesError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self.kind {
+            HomeMissing => write!(f, "{}", self.to_string()),
+            XdgRuntimeDirInaccessible(ref dir, ref error) => {
+                write!(f, "$XDG_RUNTIME_DIR (`{}`) must be accessible \
+                           by the current user (error: {})", dir.display(), error)
+            },
+            XdgRuntimeDirInsecure(ref dir, permissions) => {
+                write!(f, "$XDG_RUNTIME_DIR (`{}`) must be secure: must have \
+                           permissions 0o700, got {}", dir.display(), permissions)
+            },
+            XdgRuntimeDirMissing => {
+                write!(f, "$XDG_RUNTIME_DIR must be set")
+            },
+        }
+    }
+}
+
+impl convert::From<BaseDirectoriesError> for io::Error {
+    fn from(error: BaseDirectoriesError) -> io::Error {
+        match error.kind {
+            HomeMissing | XdgRuntimeDirMissing =>
+                io::Error::new(io::ErrorKind::NotFound, error),
+            _ => io::Error::new(io::ErrorKind::Other, error)
+        }
+    }
+
+}
+
+#[derive(Copy, Clone)]
+struct Permissions(u32);
+
+impl fmt::Debug for Permissions {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let Permissions(p) = *self;
+        write!(f, "{:#05o}", p)
+    }
+}
+
+impl fmt::Display for Permissions {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Debug::fmt(self, f)
+    }
+}
+
+#[derive(Debug)]
+enum BaseDirectoriesErrorKind {
+    HomeMissing,
+    XdgRuntimeDirInaccessible(PathBuf, io::Error),
+    XdgRuntimeDirInsecure(PathBuf, Permissions),
+    XdgRuntimeDirMissing,
+}
+
+impl BaseDirectories {
+    /// Reads the process environment, determines the XDG base directories,
+    /// and returns a value that can be used for lookup.
+    /// The following environment variables are examined:
+    ///
+    ///   * `HOME`; if not set: use the same fallback as `dirs::home_dir()`;
+    ///     if still not available: return an error.
+    ///   * `XDG_DATA_HOME`; if not set: assumed to be `$HOME/.local/share`.
+    ///   * `XDG_CONFIG_HOME`; if not set: assumed to be `$HOME/.config`.
+    ///   * `XDG_CACHE_HOME`; if not set: assumed to be `$HOME/.cache`.
+    ///   * `XDG_STATE_HOME`; if not set: assumed to be `$HOME/.local/state`.
+    ///   * `XDG_DATA_DIRS`; if not set: assumed to be `/usr/local/share:/usr/share`.
+    ///   * `XDG_CONFIG_DIRS`; if not set: assumed to be `/etc/xdg`.
+    ///   * `XDG_RUNTIME_DIR`; if not accessible or permissions are not `0700`:
+    ///     record as inaccessible (can be queried with
+    ///     [has_runtime_directory](method.has_runtime_directory)).
+    ///
+    /// As per specification, if an environment variable contains a relative path,
+    /// the behavior is the same as if it was not set.
+    pub fn new() -> Result<BaseDirectories, BaseDirectoriesError> {
+        BaseDirectories::with_env("", "", &|name| env::var_os(name))
+    }
+
+    /// Same as [`new()`](#method.new), but `prefix` is implicitly prepended to
+    /// every path that is looked up.
+    pub fn with_prefix<P>(prefix: P) -> Result<BaseDirectories, BaseDirectoriesError>
+            where P: AsRef<Path> {
+        BaseDirectories::with_env(prefix, "", &|name| env::var_os(name))
+    }
+
+    /// Same as [`with_prefix()`](#method.with_prefix),
+    /// with `profile` also implicitly prepended to every path that is looked up,
+    /// but only for user-specific directories.
+    ///
+    /// This allows each user to have mutliple "profiles" with different user-specific data.
+    ///
+    /// For example:
+    ///
+    /// ```rust
+    /// let dirs = BaseDirectories::with_profile("program-name", "profile-name")
+    ///                            .unwrap();
+    /// dirs.find_data_file("bar.jpg");
+    /// dirs.find_config_file("foo.conf");
+    /// ```
+    ///
+    /// will find `/usr/share/program-name/bar.jpg` (without `profile-name`)
+    /// and `~/.config/program-name/profile-name/foo.conf`.
+    pub fn with_profile<P1, P2>(prefix: P1, profile: P2)
+            -> Result<BaseDirectories, BaseDirectoriesError>
+            where P1: AsRef<Path>, P2: AsRef<Path> {
+        BaseDirectories::with_env(prefix, profile, &|name| env::var_os(name))
+    }
+
+    fn with_env<P1, P2, T: ?Sized>(prefix: P1, profile: P2, env_var: &T)
+            -> Result<BaseDirectories, BaseDirectoriesError>
+            where P1: AsRef<Path>, P2: AsRef<Path>, T: Fn(&str) -> Option<OsString> {
+        BaseDirectories::with_env_impl(prefix.as_ref(), profile.as_ref(), env_var)
+    }
+
+    fn with_env_impl<T: ?Sized>(prefix: &Path, profile: &Path, env_var: &T)
+            -> Result<BaseDirectories, BaseDirectoriesError>
+            where T: Fn(&str) -> Option<OsString> {
+        fn abspath(path: OsString) -> Option<PathBuf> {
+            let path = PathBuf::from(path);
+            if path.is_absolute() {
+                Some(path)
+            } else {
+                None
+            }
+        }
+
+        fn abspaths(paths: OsString) -> Option<Vec<PathBuf>> {
+            let paths = env::split_paths(&paths)
+                            .map(PathBuf::from)
+                            .filter(|ref path| path.is_absolute())
+                            .collect::<Vec<_>>();
+            if paths.is_empty() {
+                None
+            } else {
+                Some(paths)
+            }
+        }
+
+        let home = dirs::home_dir().ok_or(Error::new(HomeMissing))?;
+
+        let data_home   = env_var("XDG_DATA_HOME")
+                              .and_then(abspath)
+                              .unwrap_or(home.join(".local/share"));
+        let config_home = env_var("XDG_CONFIG_HOME")
+                              .and_then(abspath)
+                              .unwrap_or(home.join(".config"));
+        let cache_home  = env_var("XDG_CACHE_HOME")
+                              .and_then(abspath)
+                              .unwrap_or(home.join(".cache"));
+        let state_home  = env_var("XDG_STATE_HOME")
+                              .and_then(abspath)
+                              .unwrap_or(home.join(".local/state"));
+        let data_dirs   = env_var("XDG_DATA_DIRS")
+                              .and_then(abspaths)
+                              .unwrap_or(vec![PathBuf::from("/usr/local/share"),
+                                              PathBuf::from("/usr/share")]);
+        let config_dirs = env_var("XDG_CONFIG_DIRS")
+                              .and_then(abspaths)
+                              .unwrap_or(vec![PathBuf::from("/etc/xdg")]);
+        let runtime_dir = env_var("XDG_RUNTIME_DIR")
+                              .and_then(abspath); // optional
+
+        let prefix = PathBuf::from(prefix);
+        Ok(BaseDirectories {
+            user_prefix: prefix.join(profile),
+            shared_prefix: prefix,
+            data_home,
+            config_home,
+            cache_home,
+            state_home,
+            data_dirs,
+            config_dirs,
+            runtime_dir,
+        })
+    }
+
+    /// Returns the user-specific runtime directory (set by `XDG_RUNTIME_DIR`).
+    pub fn get_runtime_directory(&self) -> Result<&PathBuf, BaseDirectoriesError> {
+        if let Some(ref runtime_dir) = self.runtime_dir {
+            // If XDG_RUNTIME_DIR is in the environment but not secure,
+            // do not allow recovery.
+            fs::read_dir(runtime_dir).map_err(|e| {
+                Error::new(XdgRuntimeDirInaccessible(runtime_dir.clone(), e))
+            })?;
+            let permissions = fs::metadata(runtime_dir).map_err(|e| {
+                Error::new(XdgRuntimeDirInaccessible(runtime_dir.clone(), e))
+            })?.permissions().mode() as u32;
+            if permissions & 0o077 != 0 {
+                Err(Error::new(XdgRuntimeDirInsecure(runtime_dir.clone(),
+                                                     Permissions(permissions))))
+            } else {
+                Ok(&runtime_dir)
+            }
+        } else {
+            Err(Error::new(XdgRuntimeDirMissing))
+        }
+    }
+
+    /// Returns `true` if `XDG_RUNTIME_DIR` is available, `false` otherwise.
+    pub fn has_runtime_directory(&self) -> bool {
+        match self.get_runtime_directory() {
+            Ok(_) => true,
+            _ => false
+        }
+    }
+
+    /// Like [`place_config_file()`](#method.place_config_file), but does
+    /// not create any directories.
+    pub fn get_config_file<P>(&self, path: P) -> PathBuf
+            where P: AsRef<Path> {
+        self.config_home.join(self.user_prefix.join(path))
+    }
+
+    /// Like [`place_data_file()`](#method.place_data_file), but does
+    /// not create any directories.
+    pub fn get_data_file<P>(&self, path: P) -> PathBuf
+            where P: AsRef<Path> {
+        self.data_home.join(self.user_prefix.join(path))
+    }
+
+    /// Like [`place_cache_file()`](#method.place_cache_file), but does
+    /// not create any directories.
+    pub fn get_cache_file<P>(&self, path: P) -> PathBuf
+            where P: AsRef<Path> {
+        self.cache_home.join(self.user_prefix.join(path))
+    }
+
+    /// Like [`place_state_file()`](#method.place_state_file), but does
+    /// not create any directories.
+    pub fn get_state_file<P>(&self, path: P) -> PathBuf
+            where P: AsRef<Path> {
+        self.state_home.join(self.user_prefix.join(path))
+    }
+
+    /// Like [`place_runtime_file()`](#method.place_runtime_file), but does
+    /// not create any directories.
+    /// If `XDG_RUNTIME_DIR` is not available, returns an error.
+    pub fn get_runtime_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        let runtime_dir = self.get_runtime_directory()?;
+        Ok(runtime_dir.join(self.user_prefix.join(path)))
+    }
+
+    /// Given a relative path `path`, returns an absolute path in
+    /// `XDG_CONFIG_HOME` where a configuration file may be stored.
+    /// Leading directories in the returned path are pre-created;
+    /// if that is not possible, an error is returned.
+    pub fn place_config_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        write_file(&self.config_home, self.user_prefix.join(path))
+    }
+
+    /// Like [`place_config_file()`](#method.place_config_file), but for
+    /// a data file in `XDG_DATA_HOME`.
+    pub fn place_data_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        write_file(&self.data_home, self.user_prefix.join(path))
+    }
+
+    /// Like [`place_config_file()`](#method.place_config_file), but for
+    /// a cache file in `XDG_CACHE_HOME`.
+    pub fn place_cache_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        write_file(&self.cache_home, self.user_prefix.join(path))
+    }
+
+    /// Like [`place_config_file()`](#method.place_config_file), but for
+    /// an application state file in `XDG_STATE_HOME`.
+    pub fn place_state_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        write_file(&self.state_home, self.user_prefix.join(path))
+    }
+
+    /// Like [`place_config_file()`](#method.place_config_file), but for
+    /// a runtime file in `XDG_RUNTIME_DIR`.
+    /// If `XDG_RUNTIME_DIR` is not available, returns an error.
+    pub fn place_runtime_file<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        write_file(self.get_runtime_directory()?, self.user_prefix.join(path))
+    }
+
+    /// Given a relative path `path`, returns an absolute path to an existing
+    /// configuration file, or `None`. Searches `XDG_CONFIG_HOME` and then
+    /// `XDG_CONFIG_DIRS`.
+    pub fn find_config_file<P>(&self, path: P) -> Option<PathBuf>
+            where P: AsRef<Path> {
+        read_file(&self.config_home, &self.config_dirs,
+                  &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an iterator yielding absolute
+    /// paths to existing configuration files, in `XDG_CONFIG_DIRS` and
+    /// `XDG_CONFIG_HOME`. Paths are produced in order from lowest priority
+    /// to highest.
+    pub fn find_config_files<P>(&self, path: P) -> FileFindIterator
+            where P: AsRef<Path> {
+        FileFindIterator::new(&self.config_home, &self.config_dirs,
+                    &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an absolute path to an existing
+    /// data file, or `None`. Searches `XDG_DATA_HOME` and then
+    /// `XDG_DATA_DIRS`.
+    pub fn find_data_file<P>(&self, path: P) -> Option<PathBuf>
+            where P: AsRef<Path> {
+        read_file(&self.data_home, &self.data_dirs,
+                  &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an iterator yielding absolute
+    /// paths to existing data files, in `XDG_DATA_DIRS` and
+    /// `XDG_DATA_HOME`. Paths are produced in order from lowest priority
+    /// to highest.
+    pub fn find_data_files<P>(&self, path: P) -> FileFindIterator
+            where P: AsRef<Path> {
+        FileFindIterator::new(&self.data_home, &self.data_dirs,
+                    &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an absolute path to an existing
+    /// cache file, or `None`. Searches `XDG_CACHE_HOME`.
+    pub fn find_cache_file<P>(&self, path: P) -> Option<PathBuf>
+            where P: AsRef<Path> {
+        read_file(&self.cache_home, &Vec::new(),
+                  &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an absolute path to an existing
+    /// application state file, or `None`. Searches `XDG_STATE_HOME`.
+    pub fn find_state_file<P>(&self, path: P) -> Option<PathBuf>
+            where P: AsRef<Path> {
+        read_file(&self.state_home, &Vec::new(),
+                  &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, returns an absolute path to an existing
+    /// runtime file, or `None`. Searches `XDG_RUNTIME_DIR`.
+    /// If `XDG_RUNTIME_DIR` is not available, returns `None`.
+    pub fn find_runtime_file<P>(&self, path: P) -> Option<PathBuf>
+            where P: AsRef<Path> {
+        if let Ok(runtime_dir) = self.get_runtime_directory() {
+            read_file(runtime_dir, &Vec::new(),
+                      &self.user_prefix, &self.shared_prefix, path.as_ref())
+        } else {
+            None
+        }
+    }
+
+    /// Given a relative path `path`, returns an absolute path to a configuration
+    /// directory in `XDG_CONFIG_HOME`. The directory and all directories
+    /// leading to it are created if they did not exist;
+    /// if that is not possible, an error is returned.
+    pub fn create_config_directory<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        create_directory(&self.config_home,
+                         self.user_prefix.join(path))
+    }
+
+    /// Like [`create_config_directory()`](#method.create_config_directory),
+    /// but for a data directory in `XDG_DATA_HOME`.
+    pub fn create_data_directory<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        create_directory(&self.data_home,
+                         self.user_prefix.join(path))
+    }
+
+    /// Like [`create_config_directory()`](#method.create_config_directory),
+    /// but for a cache directory in `XDG_CACHE_HOME`.
+    pub fn create_cache_directory<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        create_directory(&self.cache_home,
+                         self.user_prefix.join(path))
+    }
+
+    /// Like [`create_config_directory()`](#method.create_config_directory),
+    /// but for an application state directory in `XDG_STATE_HOME`.
+    pub fn create_state_directory<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        create_directory(&self.state_home,
+                         self.user_prefix.join(path))
+    }
+
+    /// Like [`create_config_directory()`](#method.create_config_directory),
+    /// but for a runtime directory in `XDG_RUNTIME_DIR`.
+    /// If `XDG_RUNTIME_DIR` is not available, returns an error.
+    pub fn create_runtime_directory<P>(&self, path: P) -> io::Result<PathBuf>
+            where P: AsRef<Path> {
+        create_directory(self.get_runtime_directory()?,
+                         self.user_prefix.join(path))
+    }
+
+    /// Given a relative path `path`, list absolute paths to all files
+    /// in directories with path `path` in `XDG_CONFIG_HOME` and
+    /// `XDG_CONFIG_DIRS`.
+    pub fn list_config_files<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files(&self.config_home, &self.config_dirs,
+                   &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Like [`list_config_files`](#method.list_config_files), but
+    /// only the first occurence of every distinct filename is returned.
+    pub fn list_config_files_once<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files_once(&self.config_home, &self.config_dirs,
+                        &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, lists absolute paths to all files
+    /// in directories with path `path` in `XDG_DATA_HOME` and
+    /// `XDG_DATA_DIRS`.
+    pub fn list_data_files<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files(&self.data_home, &self.data_dirs,
+                   &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Like [`list_data_files`](#method.list_data_files), but
+    /// only the first occurence of every distinct filename is returned.
+    pub fn list_data_files_once<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files_once(&self.data_home, &self.data_dirs,
+                        &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, lists absolute paths to all files
+    /// in directories with path `path` in `XDG_CACHE_HOME`.
+    pub fn list_cache_files<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files(&self.cache_home, &Vec::new(),
+                   &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, lists absolute paths to all files
+    /// in directories with path `path` in `XDG_STATE_HOME`.
+    pub fn list_state_files<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        list_files(&self.state_home, &Vec::new(),
+                   &self.user_prefix, &self.shared_prefix, path.as_ref())
+    }
+
+    /// Given a relative path `path`, lists absolute paths to all files
+    /// in directories with path `path` in `XDG_RUNTIME_DIR`.
+    /// If `XDG_RUNTIME_DIR` is not available, returns an empty `Vec`.
+    pub fn list_runtime_files<P>(&self, path: P) -> Vec<PathBuf>
+            where P: AsRef<Path> {
+        if let Ok(runtime_dir) = self.get_runtime_directory() {
+            list_files(runtime_dir, &Vec::new(),
+                       &self.user_prefix, &self.shared_prefix, path.as_ref())
+        } else {
+            Vec::new()
+        }
+    }
+
+    /// Returns the user-specific data directory (set by `XDG_DATA_HOME`).
+    pub fn get_data_home(&self) -> PathBuf {
+        self.data_home.join(&self.user_prefix)
+    }
+
+    /// Returns the user-specific configuration directory (set by
+    /// `XDG_CONFIG_HOME`).
+    pub fn get_config_home(&self) -> PathBuf {
+        self.config_home.join(&self.user_prefix)
+    }
+
+    /// Returns the user-specific directory for non-essential (cached) data
+    /// (set by `XDG_CACHE_HOME`).
+    pub fn get_cache_home(&self) -> PathBuf {
+        self.cache_home.join(&self.user_prefix)
+    }
+
+    /// Returns the user-specific directory for application state data
+    /// (set by `XDG_STATE_HOME`).
+    pub fn get_state_home(&self) -> PathBuf {
+        self.state_home.join(&self.user_prefix)
+    }
+
+    /// Returns a preference ordered (preferred to less preferred) list of
+    /// supplementary data directories, ordered by preference (set by
+    /// `XDG_DATA_DIRS`).
+    pub fn get_data_dirs(&self) -> Vec<PathBuf> {
+        self.data_dirs.iter().map(|p| p.join(&self.shared_prefix)).collect()
+    }
+
+    /// Returns a preference ordered (preferred to less preferred) list of
+    /// supplementary configuration directories (set by `XDG_CONFIG_DIRS`).
+    pub fn get_config_dirs(&self) -> Vec<PathBuf> {
+        self.config_dirs.iter().map(|p| p.join(&self.shared_prefix)).collect()
+    }
+}
+
+fn write_file<P>(home: &PathBuf, path: P) -> io::Result<PathBuf>
+        where P: AsRef<Path> {
+    match path.as_ref().parent() {
+        Some(parent) => fs::create_dir_all(home.join(parent))?,
+        None => fs::create_dir_all(home)?,
+    }
+    Ok(PathBuf::from(home.join(path.as_ref())))
+}
+
+fn create_directory<P>(home: &PathBuf, path: P) -> io::Result<PathBuf>
+        where P: AsRef<Path> {
+    let full_path = home.join(path.as_ref());
+    fs::create_dir_all(&full_path)?;
+    Ok(full_path)
+}
+
+fn path_exists<P: ?Sized + AsRef<Path>>(path: &P) -> bool {
+    fn inner(path: &Path) -> bool {
+        fs::metadata(path).is_ok()
+    }
+    inner(path.as_ref())
+}
+
+#[cfg(test)]
+fn path_is_dir<P: ?Sized + AsRef<Path>>(path: &P) -> bool {
+    fn inner(path: &Path) -> bool {
+        fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)
+    }
+    inner(path.as_ref())
+}
+
+fn read_file(home: &PathBuf, dirs: &Vec<PathBuf>,
+             user_prefix: &Path, shared_prefix: &Path, path: &Path)
+             -> Option<PathBuf> {
+    let full_path = home.join(user_prefix).join(path);
+    if path_exists(&full_path) {
+        return Some(full_path)
+    }
+    for dir in dirs.iter() {
+        let full_path = dir.join(shared_prefix).join(path);
+        if path_exists(&full_path) {
+            return Some(full_path)
+        }
+    }
+    None
+}
+
+use std::vec::IntoIter as VecIter;
+pub struct FileFindIterator {
+    search_dirs: VecIter<PathBuf>,
+    relpath: PathBuf,
+}
+
+impl FileFindIterator {
+    fn new(home: &PathBuf, dirs: &Vec<PathBuf>,
+           user_prefix: &Path, shared_prefix: &Path, path: &Path)
+           -> FileFindIterator {
+       let mut search_dirs = Vec::new();
+       for dir in dirs.iter().rev() {
+           search_dirs.push(dir.join(shared_prefix));
+       }
+       search_dirs.push(home.join(user_prefix));
+       FileFindIterator {
+           search_dirs: search_dirs.into_iter(),
+           relpath: path.to_path_buf(),
+       }
+   }
+}
+
+impl Iterator for FileFindIterator {
+    type Item = PathBuf;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        loop {
+            let dir = self.search_dirs.next()?;
+            let candidate = dir.join(self.relpath.clone());
+            if path_exists(&candidate) {
+                return Some(candidate)
+            }
+        }
+    }
+}
+
+impl DoubleEndedIterator for FileFindIterator {
+    fn next_back(&mut self) -> Option<Self::Item> {
+        loop {
+            let dir = self.search_dirs.next_back()?;
+            let candidate = dir.join(self.relpath.clone());
+            if path_exists(&candidate) {
+                return Some(candidate)
+            }
+        }
+    }
+}
+
+fn list_files(home: &Path, dirs: &[PathBuf],
+              user_prefix: &Path, shared_prefix: &Path, path: &Path)
+              -> Vec<PathBuf> {
+    fn read_dir(dir: &Path, into: &mut Vec<PathBuf>) {
+        if let Ok(entries) = fs::read_dir(dir) {
+            into.extend(
+                entries
+                .filter_map(|entry| entry.ok())
+                .map(|entry| entry.path()))
+        }
+    }
+    let mut files = Vec::new();
+    read_dir(&home.join(user_prefix).join(path), &mut files);
+    for dir in dirs {
+        read_dir(&dir.join(shared_prefix).join(path), &mut files);
+    }
+    files
+}
+
+fn list_files_once(home: &Path, dirs: &[PathBuf],
+                   user_prefix: &Path, shared_prefix: &Path, path: &Path)
+                   -> Vec<PathBuf> {
+    let mut seen = std::collections::HashSet::new();
+    list_files(home, dirs, user_prefix, shared_prefix, path).into_iter().filter(|path| {
+        match path.clone().file_name() {
+            None => false,
+            Some(filename) => {
+                if seen.contains(filename) {
+                    false
+                } else {
+                    seen.insert(filename.to_owned());
+                    true
+                }
+            }
+        }
+    }).collect::<Vec<_>>()
+}
+
+#[cfg(test)]
+fn make_absolute<P>(path: P) -> PathBuf where P: AsRef<Path> {
+    env::current_dir().unwrap().join(path.as_ref())
+}
+
+#[cfg(test)]
+fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I> where
+    I: Iterator<Item=A> + Clone, J: Iterator<Item=A>, A: PartialEq
+{
+    loop {
+        let mut iter_next = iter.clone();
+        match (iter_next.next(), prefix.next()) {
+            (Some(x), Some(y)) => {
+                if x != y { return None }
+            }
+            (Some(_), None) => return Some(iter),
+            (None, None) => return Some(iter),
+            (None, Some(_)) => return None,
+        }
+        iter = iter_next;
+    }
+}
+
+#[cfg(test)]
+fn make_relative<P>(path: P) -> PathBuf where P: AsRef<Path> {
+    iter_after(path.as_ref().components(), env::current_dir().unwrap().components())
+        .unwrap().as_path().to_owned()
+}
+
+#[cfg(test)]
+fn make_env(vars: Vec<(&'static str, String)>) ->
+        Box<dyn Fn(&str)->Option<OsString>> {
+    return Box::new(move |name| {
+        for &(key, ref value) in vars.iter() {
+            if key == name { return Some(OsString::from(value)) }
+        }
+        None
+    })
+}
+
+#[test]
+fn test_files_exists() {
+    assert!(path_exists("test_files"));
+    assert!(fs::metadata("test_files/runtime-bad")
+                 .unwrap().permissions().mode() & 0o077 != 0);
+}
+
+#[test]
+fn test_bad_environment() {
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", "test_files/user".to_string()),
+            ("XDG_DATA_HOME", "test_files/user/data".to_string()),
+            ("XDG_CONFIG_HOME", "test_files/user/config".to_string()),
+            ("XDG_CACHE_HOME", "test_files/user/cache".to_string()),
+            ("XDG_DATA_DIRS", "test_files/user/data".to_string()),
+            ("XDG_CONFIG_DIRS", "test_files/user/config".to_string()),
+            ("XDG_RUNTIME_DIR", "test_files/runtime-bad".to_string())
+        ])).unwrap();
+    assert_eq!(xd.find_data_file("everywhere"), None);
+    assert_eq!(xd.find_config_file("everywhere"), None);
+    assert_eq!(xd.find_cache_file("everywhere"), None);
+}
+
+#[test]
+fn test_good_environment() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_DATA_HOME", format!("{}/test_files/user/data", cwd)),
+            ("XDG_CONFIG_HOME", format!("{}/test_files/user/config", cwd)),
+            ("XDG_CACHE_HOME", format!("{}/test_files/user/cache", cwd)),
+            ("XDG_DATA_DIRS", format!("{}/test_files/system0/data:{}/test_files/system1/data:{}/test_files/system2/data:{}/test_files/system3/data", cwd, cwd, cwd, cwd)),
+            ("XDG_CONFIG_DIRS", format!("{}/test_files/system0/config:{}/test_files/system1/config:{}/test_files/system2/config:{}/test_files/system3/config", cwd, cwd, cwd, cwd)),
+            // ("XDG_RUNTIME_DIR", format!("{}/test_files/runtime-bad", cwd)),
+        ])).unwrap();
+    assert!(xd.find_data_file("everywhere") != None);
+    assert!(xd.find_config_file("everywhere") != None);
+    assert!(xd.find_cache_file("everywhere") != None);
+
+    let mut config_files = xd.find_config_files("everywhere");
+    assert_eq!(config_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/system2/config/everywhere", cwd))));
+    assert_eq!(config_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/system1/config/everywhere", cwd))));
+    assert_eq!(config_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/user/config/everywhere", cwd))));
+    assert_eq!(config_files.next(), None);
+
+    let mut data_files = xd.find_data_files("everywhere");
+    assert_eq!(data_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/system2/data/everywhere", cwd))));
+    assert_eq!(data_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/system1/data/everywhere", cwd))));
+    assert_eq!(data_files.next(),
+        Some(PathBuf::from(format!("{}/test_files/user/data/everywhere", cwd))));
+    assert_eq!(data_files.next(), None);
+}
+
+#[test]
+fn test_runtime_bad() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_RUNTIME_DIR", format!("{}/test_files/runtime-bad", cwd)),
+        ])).unwrap();
+    assert!(xd.has_runtime_directory() == false);
+}
+
+#[test]
+fn test_runtime_good() {
+    use std::fs::File;
+
+    let test_runtime_dir = make_absolute(&"test_files/runtime-good");
+    let _ = fs::remove_dir_all(&test_runtime_dir);
+    fs::create_dir_all(&test_runtime_dir).unwrap();
+
+    let mut perms = fs::metadata(&test_runtime_dir).unwrap().permissions();
+    perms.set_mode(0o700);
+    fs::set_permissions(&test_runtime_dir, perms).unwrap();
+
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_RUNTIME_DIR", format!("{}/test_files/runtime-good", cwd)),
+        ])).unwrap();
+
+    xd.create_runtime_directory("foo").unwrap();
+    assert!(path_is_dir("test_files/runtime-good/foo"));
+    let w = xd.place_runtime_file("bar/baz").unwrap();
+    assert!(path_is_dir("test_files/runtime-good/bar"));
+    assert!(!path_exists("test_files/runtime-good/bar/baz"));
+    File::create(&w).unwrap();
+    assert!(path_exists("test_files/runtime-good/bar/baz"));
+    assert!(xd.find_runtime_file("bar/baz") == Some(w.clone()));
+    File::open(&w).unwrap();
+    fs::remove_file(&w).unwrap();
+    let root = xd.list_runtime_files(".");
+    let mut root = root.into_iter().map(|p| make_relative(&p)).collect::<Vec<_>>();
+    root.sort();
+    assert_eq!(root,
+               vec![PathBuf::from("test_files/runtime-good/bar"),
+                    PathBuf::from("test_files/runtime-good/foo")]);
+    assert!(xd.list_runtime_files("bar").is_empty());
+    assert!(xd.find_runtime_file("foo/qux").is_none());
+    assert!(xd.find_runtime_file("qux/foo").is_none());
+    assert!(!path_exists("test_files/runtime-good/qux"));
+}
+
+#[test]
+fn test_lists() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_DATA_HOME", format!("{}/test_files/user/data", cwd)),
+            ("XDG_CONFIG_HOME", format!("{}/test_files/user/config", cwd)),
+            ("XDG_CACHE_HOME", format!("{}/test_files/user/cache", cwd)),
+            ("XDG_DATA_DIRS", format!("{}/test_files/system0/data:{}/test_files/system1/data:{}/test_files/system2/data:{}/test_files/system3/data", cwd, cwd, cwd, cwd)),
+            ("XDG_CONFIG_DIRS", format!("{}/test_files/system0/config:{}/test_files/system1/config:{}/test_files/system2/config:{}/test_files/system3/config", cwd, cwd, cwd, cwd)),
+        ])).unwrap();
+
+    let files = xd.list_config_files(".");
+    let mut files = files.into_iter().map(|p| make_relative(&p)).collect::<Vec<_>>();
+    files.sort();
+    assert_eq!(files,
+        [
+            "test_files/system1/config/both_system_config.file",
+            "test_files/system1/config/everywhere",
+            "test_files/system1/config/myapp",
+            "test_files/system1/config/system1_config.file",
+            "test_files/system2/config/both_system_config.file",
+            "test_files/system2/config/everywhere",
+            "test_files/system2/config/system2_config.file",
+            "test_files/user/config/everywhere",
+            "test_files/user/config/myapp",
+            "test_files/user/config/user_config.file",
+        ].iter().map(PathBuf::from).collect::<Vec<_>>());
+
+    let files = xd.list_config_files_once(".");
+    let mut files = files.into_iter().map(|p| make_relative(&p)).collect::<Vec<_>>();
+    files.sort();
+    assert_eq!(files,
+        [
+            "test_files/system1/config/both_system_config.file",
+            "test_files/system1/config/system1_config.file",
+            "test_files/system2/config/system2_config.file",
+            "test_files/user/config/everywhere",
+            "test_files/user/config/myapp",
+            "test_files/user/config/user_config.file",
+        ].iter().map(PathBuf::from).collect::<Vec<_>>());
+}
+
+#[test]
+fn test_get_file() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_DATA_HOME", format!("{}/test_files/user/data", cwd)),
+            ("XDG_CONFIG_HOME", format!("{}/test_files/user/config", cwd)),
+            ("XDG_CACHE_HOME", format!("{}/test_files/user/cache", cwd)),
+            ("XDG_RUNTIME_DIR", format!("{}/test_files/user/runtime", cwd)),
+        ])).unwrap();
+
+    let path = format!("{}/test_files/user/runtime/", cwd);
+    let metadata = fs::metadata(&path).expect("Could not read metadata for runtime directory");
+    let mut perms = metadata.permissions();
+    perms.set_mode(0o700);
+    fs::set_permissions(&path, perms).expect("Could not set permissions for runtime directory");
+
+    let file = xd.get_config_file("myapp/user_config.file");
+    assert_eq!(file, PathBuf::from(&format!("{}/test_files/user/config/myapp/user_config.file", cwd)));
+
+    let file = xd.get_data_file("user_data.file");
+    assert_eq!(file, PathBuf::from(&format!("{}/test_files/user/data/user_data.file", cwd)));
+
+    let file = xd.get_cache_file("user_cache.file");
+    assert_eq!(file, PathBuf::from(&format!("{}/test_files/user/cache/user_cache.file", cwd)));
+
+    let file = xd.get_runtime_file("user_runtime.file").unwrap();
+    assert_eq!(file, PathBuf::from(&format!("{}/test_files/user/runtime/user_runtime.file", cwd)));
+}
+
+#[test]
+fn test_prefix() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("myapp", "", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_CACHE_HOME", format!("{}/test_files/user/cache", cwd)),
+        ])).unwrap();
+    assert_eq!(xd.get_cache_file("cache.db"),
+        PathBuf::from(&format!("{}/test_files/user/cache/myapp/cache.db", cwd)));
+    assert_eq!(xd.place_cache_file("cache.db").unwrap(),
+               PathBuf::from(&format!("{}/test_files/user/cache/myapp/cache.db", cwd)));
+}
+
+#[test]
+fn test_profile() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let xd = BaseDirectories::with_env("myapp", "default_profile", &*make_env(vec![
+            ("HOME", format!("{}/test_files/user", cwd)),
+            ("XDG_CONFIG_HOME", format!("{}/test_files/user/config", cwd)),
+            ("XDG_CONFIG_DIRS", format!("{}/test_files/system1/config", cwd)),
+       ])).unwrap();
+    assert_eq!(xd.find_config_file("system1_config.file").unwrap(),
+               // Does *not* include default_profile
+               PathBuf::from(&format!("{}/test_files/system1/config/myapp/system1_config.file", cwd)));
+    assert_eq!(xd.find_config_file("user_config.file").unwrap(),
+               // Includes default_profile
+               PathBuf::from(&format!("{}/test_files/user/config/myapp/default_profile/user_config.file", cwd)));
+}
+
+/// Ensure that entries in XDG_CONFIG_DIRS can be replaced with symlinks.
+#[test]
+fn test_symlinks() {
+    let cwd = env::current_dir().unwrap().to_string_lossy().into_owned();
+    let symlinks_dir = format!("{}/test_files/symlinks", cwd);
+    let config_dir = format!("{}/config", symlinks_dir);
+    let myapp_dir = format!("{}/myapp", config_dir);
+
+    assert!(path_exists(&myapp_dir));
+    assert!(path_exists(&config_dir));
+    assert!(path_exists(&symlinks_dir));
+
+    let xd = BaseDirectories::with_env(
+        "myapp", "", &*make_env(vec![
+            ("HOME", symlinks_dir),
+            ("XDG_CONFIG_HOME", config_dir),
+        ])
+    ).unwrap();
+    assert_eq!(xd.find_config_file("user_config.file").unwrap(),
+               PathBuf::from(&format!("{}/user_config.file", myapp_dir)));
+}
diff --git a/test_files/runtime-bad/everywhere b/test_files/runtime-bad/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/runtime-bad/runtime.file b/test_files/runtime-bad/runtime.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/symlinks/config/myapp/default_profile/user_config.file b/test_files/symlinks/config/myapp/default_profile/user_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/symlinks/config/myapp/user_config.file b/test_files/symlinks/config/myapp/user_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/config/both_system_config.file b/test_files/system1/config/both_system_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/config/everywhere b/test_files/system1/config/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/config/myapp/default_profile/system1_config.file b/test_files/system1/config/myapp/default_profile/system1_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/config/myapp/system1_config.file b/test_files/system1/config/myapp/system1_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/config/system1_config.file b/test_files/system1/config/system1_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/data/both_system_data.file b/test_files/system1/data/both_system_data.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/data/everywhere b/test_files/system1/data/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system1/data/system1_data.file b/test_files/system1/data/system1_data.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/config/both_system_config.file b/test_files/system2/config/both_system_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/config/everywhere b/test_files/system2/config/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/config/system2_config.file b/test_files/system2/config/system2_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/data/both_system_data.file b/test_files/system2/data/both_system_data.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/data/everywhere b/test_files/system2/data/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/system2/data/system2_data.file b/test_files/system2/data/system2_data.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/cache/everywhere b/test_files/user/cache/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/cache/user_cache.file b/test_files/user/cache/user_cache.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/config/everywhere b/test_files/user/config/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/config/myapp/default_profile/user_config.file b/test_files/user/config/myapp/default_profile/user_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/config/myapp/user_config.file b/test_files/user/config/myapp/user_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/config/user_config.file b/test_files/user/config/user_config.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/data/everywhere b/test_files/user/data/everywhere
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/data/user_data.file b/test_files/user/data/user_data.file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test_files/user/runtime/user_runtime.file b/test_files/user/runtime/user_runtime.file
new file mode 100644 (file)
index 0000000..e69de29