From b3f52e56b55e01b45d0bc709259600084caddbf4 Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Mon, 4 Dec 2017 17:42:47 +0100
Subject: [PATCH] Migrate to xdg (#626)

Replaces the app-dirs crate with the xdg crate
---
 Cargo.lock                 | 38 +++---------------------------------
 Cargo.toml                 |  3 +--
 src/lib.rs                 |  2 +-
 src/main.rs                |  2 +-
 src/shell/mod.rs           | 40 ++++++++++++++++++--------------------
 src/shell/plugins/mod.rs   | 26 ++++++++++++++-----------
 src/shell/variables/mod.rs | 16 +++++++--------
 7 files changed, 47 insertions(+), 80 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 87cceff6..cfaa9a02 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -16,17 +16,6 @@ name = "ansi_term"
 version = "0.10.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
-[[package]]
-name = "app_dirs"
-version = "1.1.1"
-source = "git+https://github.com/redox-os/app-dirs-rs.git#6283614ca1f3d3008bd4d258a2972c8c2f8f9c43"
-dependencies = [
- "ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
 [[package]]
 name = "atty"
 version = "0.2.3"
@@ -197,7 +186,6 @@ name = "ion-shell"
 version = "1.0.5"
 dependencies = [
  "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "app_dirs 1.1.1 (git+https://github.com/redox-os/app-dirs-rs.git)",
  "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "calculate 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "errno 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -219,6 +207,7 @@ dependencies = [
  "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "users 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "xdg 2.1.0 (git+https://github.com/jD91mZM2/rust-xdg?branch=redox)",
 ]
 
 [[package]]
@@ -273,15 +262,6 @@ dependencies = [
  "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
-[[package]]
-name = "ole32-sys"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
 [[package]]
 name = "ord_subset"
 version = "3.1.0"
@@ -351,15 +331,6 @@ name = "serde"
 version = "1.0.21"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
-[[package]]
-name = "shell32-sys"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
 [[package]]
 name = "smallstring"
 version = "0.1.2"
@@ -501,13 +472,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 [[package]]
 name = "xdg"
 version = "2.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
+source = "git+https://github.com/jD91mZM2/rust-xdg?branch=redox#5435baa3ac09a00c38fa931c6e7b929353e3286e"
 
 [metadata]
 "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
 "checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455"
 "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
-"checksum app_dirs 1.1.1 (git+https://github.com/redox-os/app-dirs-rs.git)" = "<none>"
 "checksum atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "21e50800ec991574876040fff8ee46b136a53e985286fbe6a3bdfe6421b78860"
 "checksum backtrace 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8709cc7ec06f6f0ae6c2c7e12f6ed41540781f72b488d83734978295ceae182e"
 "checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661"
@@ -535,7 +505,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum libloading 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f92926a9a4ba7aeeb01f5fba3f0d577147243b6e7fa8261c219cd1d6fbe3b1c"
 "checksum liner 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e21c5e49e5d73f18124fd3c751a0823f754c5ae7193cb9e8aa467b03b161241c"
 "checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
-"checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c"
 "checksum ord_subset 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc65e6e24476e1a5baed530f60b918ff7925539aaae1d59ab51113c991321c40"
 "checksum permutate 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53b7d5b19a715ffab38693a9dd44b067fdfa2b18eef65bd93562dfe507022fae"
 "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
@@ -547,7 +516,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e"
 "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
 "checksum serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6eda663e865517ee783b0891a3f6eb3a253e0b0dabb46418969ee9635beadd9e"
-"checksum shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72f20b8f3c060374edb8046591ba28f62448c369ccbdc7b02075103fb3a9e38d"
 "checksum smallstring 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "30950abdb5b38f56a0e181ae56ed64a539b64fa77ea6325147203dc7faeb087f"
 "checksum smallvec 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4f8266519bc1d17d0b5b16f6c21295625d562841c708f6376f49028a43e9c11e"
 "checksum smallvec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4f357e8cd37bf8822e1b964e96fd39e2cb5a0424f8aaa284ccaccc2162411c"
@@ -569,4 +537,4 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
 "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
-"checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61"
+"checksum xdg 2.1.0 (git+https://github.com/jD91mZM2/rust-xdg?branch=redox)" = "<none>"
diff --git a/Cargo.toml b/Cargo.toml
index edcec834..b126d843 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,9 +39,8 @@ regex = "0.2"
 smallstring = "0.1"
 smallvec = "0.4"
 unicode-segmentation = "1.2"
+xdg = { git = "https://github.com/jD91mZM2/rust-xdg", branch = "redox" }
 
-[dependencies.app_dirs]
-git = "https://github.com/redox-os/app-dirs-rs.git"
 [profile.release]
 lto = true
 panic = "abort"
diff --git a/src/lib.rs b/src/lib.rs
index 51c699fc..6dfe8eb4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,7 +5,7 @@
 #![feature(pointer_methods)]
 #![feature(getpid)]
 
-extern crate app_dirs;
+extern crate xdg;
 #[macro_use]
 extern crate bitflags;
 extern crate calc;
diff --git a/src/main.rs b/src/main.rs
index 4bada477..d41134fd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,7 +9,7 @@
 // #![feature(alloc_system)]
 // extern crate alloc_system;
 
-extern crate app_dirs;
+extern crate xdg;
 #[macro_use]
 extern crate bitflags;
 extern crate calc;
diff --git a/src/shell/mod.rs b/src/shell/mod.rs
index 9722f401..05fea73e 100644
--- a/src/shell/mod.rs
+++ b/src/shell/mod.rs
@@ -30,13 +30,12 @@ use self::job_control::{BackgroundProcess, JobControl};
 use self::pipe_exec::PipelineExecution;
 use self::status::*;
 use self::variables::Variables;
-use app_dirs::{app_root, AppDataType, AppInfo};
 use builtins::{BuiltinMap, BUILTINS};
 use fnv::FnvHashMap;
 use liner::Context;
-use parser::{ArgumentSplitter, Expander, Select};
 use parser::Terminator;
 use parser::pipelines::Pipeline;
+use parser::{ArgumentSplitter, Expander, Select};
 use smallvec::SmallVec;
 use std::env;
 use std::fs::File;
@@ -45,11 +44,12 @@ use std::iter::FromIterator;
 use std::ops::Deref;
 use std::path::Path;
 use std::process;
-use std::sync::{Arc, Mutex};
 use std::sync::atomic::Ordering;
+use std::sync::{Arc, Mutex};
 use std::time::SystemTime;
 use sys;
 use types::*;
+use xdg::BaseDirectories;
 
 #[allow(dead_code)]
 #[derive(Debug, Fail)]
@@ -194,25 +194,23 @@ impl<'a> Shell {
 
     /// Evaluates the source init file in the user's home directory.
     pub fn evaluate_init_file(&mut self) {
-        match app_root(
-            AppDataType::UserConfig,
-            &AppInfo { name:   "ion", author: "Redox OS Developers" },
-        ) {
-            Ok(mut initrc) => {
-                initrc.push("initrc");
-                if initrc.exists() {
-                    if let Err(err) = self.execute_script(&initrc) {
-                        eprintln!("ion: {}", err);
-                    }
-                } else {
-                    eprintln!("ion: creating initrc file at {:?}", initrc);
-                    if let Err(why) = File::create(initrc) {
-                        eprintln!("ion: could not create initrc file: {}", why);
-                    }
-                }
+        let base_dirs = match BaseDirectories::with_prefix("ion") {
+            Ok(base_dirs) => base_dirs,
+            Err(err) => {
+                eprintln!("ion: unable to get base directory: {}", err);
+                return;
             }
-            Err(why) => {
-                eprintln!("ion: unable to get config root: {}", why);
+        };
+        match base_dirs.find_config_file("initrc") {
+            Some(initrc) => {
+                if let Err(err) = self.execute_script(&initrc) {
+                    eprintln!("ion: {}", err);
+                }
+            },
+            None => {
+                if let Err(err) = base_dirs.place_config_file("initrc") {
+                    eprintln!("ion: could not create initrc file: {}", err);
+                }
             }
         }
     }
diff --git a/src/shell/plugins/mod.rs b/src/shell/plugins/mod.rs
index 162d6085..de55cb49 100644
--- a/src/shell/plugins/mod.rs
+++ b/src/shell/plugins/mod.rs
@@ -6,20 +6,24 @@ mod string;
 pub(crate) use self::library_iter::*;
 pub(crate) use self::string::StringError;
 
-use app_dirs::{app_root, AppDataType, AppInfo};
 use std::path::PathBuf;
+use xdg::BaseDirectories;
 
 pub(crate) fn config_dir() -> Option<PathBuf> {
-    match app_root(
-        AppDataType::UserConfig,
-        &AppInfo { name:   "ion", author: "Redox OS Developers" },
-    ) {
-        Ok(mut path) => {
-            path.push("plugins");
-            Some(path)
-        }
-        Err(why) => {
-            eprintln!("ion: unable to get config directory: {:?}", why);
+    match BaseDirectories::with_prefix("ion") {
+        Ok(base_dirs) => {
+            match base_dirs.create_config_directory("plugins") {
+                Ok(mut path) => {
+                    Some(path)
+                },
+                Err(err) => {
+                    eprintln!("ion: unable to create config directory: {:?}", err);
+                    None
+                }
+            }
+        },
+        Err(err) => {
+            eprintln!("ion: unable to get config directory: {:?}", err);
             None
         }
     }
diff --git a/src/shell/variables/mod.rs b/src/shell/variables/mod.rs
index 6de31d5b..c2ea6c11 100644
--- a/src/shell/variables/mod.rs
+++ b/src/shell/variables/mod.rs
@@ -2,18 +2,18 @@ use super::colors::Colors;
 use super::directory_stack::DirectoryStack;
 use super::plugins::namespaces::{self, StringNamespace};
 use super::status::{FAILURE, SUCCESS};
-use app_dirs::{app_root, AppDataType, AppInfo};
 use fnv::FnvHashMap;
 use liner::Context;
 use std::env;
 use std::io::{self, BufRead};
-use sys::{self, getpid, is_root};
 use sys::variables as self_sys;
+use sys::{self, getpid, is_root};
 use types::{
     Array, ArrayVariableContext, HashMap, HashMapVariableContext, Identifier, Key, Value,
     VariableContext,
 };
 use unicode_segmentation::UnicodeSegmentation;
+use xdg::BaseDirectories;
 
 lazy_static! {
     static ref STRING_NAMESPACES: FnvHashMap<Identifier, StringNamespace> = namespaces::collect();
@@ -43,13 +43,11 @@ impl Default for Variables {
         map.insert("PID".into(), pid.into());
 
         // Initialize the HISTFILE variable
-        if let Ok(mut home_path) = app_root(
-            AppDataType::UserData,
-            &AppInfo { name:   "ion", author: "Redox OS Developers" },
-        ) {
-            home_path.push("history");
-            map.insert("HISTFILE".into(), home_path.to_str().unwrap_or("?").into());
-            map.insert("HISTFILE_ENABLED".into(), "1".into());
+        if let Ok(base_dirs) = BaseDirectories::with_prefix("ion") {
+            if let Ok(mut path) = base_dirs.place_data_file("history") {
+                map.insert("HISTFILE".into(), path.to_str().unwrap_or("?").into());
+                map.insert("HISTFILE_ENABLED".into(), "1".into());
+            }
         }
 
         // Initialize the PWD (Present Working Directory) variable
-- 
GitLab