From 779921f71a75e46649c77652d4a957b1d73d2df1 Mon Sep 17 00:00:00 2001 From: hashmap Date: Thu, 16 Aug 2018 02:11:05 +0200 Subject: [PATCH] Fix compiler warnings (#1361) * env::home_dir is deprecated, compiler suggests to use crate `dirs` * add superficial comments to file util module --- Cargo.lock | 11 +++++++++++ config/Cargo.toml | 1 + config/src/config.rs | 6 ++++-- config/src/lib.rs | 1 + util/src/file.rs | 30 +++++++++++++++++++----------- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0f8c7f6d..7da1eb8dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,6 +336,15 @@ name = "difference" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "dirs" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.3" @@ -561,6 +570,7 @@ dependencies = [ name = "grin_config" version = "0.3.0" dependencies = [ + "dirs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "grin_p2p 0.3.0", "grin_servers 0.3.0", "grin_util 0.3.0", @@ -1980,6 +1990,7 @@ dependencies = [ "checksum cursive 0.8.2-alpha.0 (git+https://github.com/yeastplume/Cursive?tag=grin_integration_1)" = "" "checksum daemonize 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4093d27eb267d617f03c2ee25d4c3ca525b89a76154001954a11984508ffbde5" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" +"checksum dirs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f679c09c1cf5428702cc10f6846c56e4e23420d3a88bcc9335b17c630a7b710b" "checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum encode_unicode 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c088ec0ed2282dcd054f2c124c0327f953563e6c75fdc6ff5141779596289830" diff --git a/config/Cargo.toml b/config/Cargo.toml index f0ae385f8..887ded116 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -9,6 +9,7 @@ publish = false serde = "1" serde_derive = "1" toml = "0.4" +dirs = "1" grin_servers = { path = "../servers" } grin_p2p = { path = "../p2p" } diff --git a/config/src/config.rs b/config/src/config.rs index ee5bea5ea..85329c9de 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -14,6 +14,7 @@ //! Configuration file management +use dirs; use std::env; use std::fs::File; use std::io::Read; @@ -74,7 +75,7 @@ impl GlobalConfig { return Ok(()); } // Then look in {user_home}/.grin - let config_path = env::home_dir(); + let config_path = dirs::home_dir(); if let Some(mut p) = config_path { p.push(GRIN_HOME); p.push(CONFIG_FILE_NAME); @@ -167,7 +168,8 @@ impl GlobalConfig { /// Enable mining pub fn stratum_enabled(&mut self) -> bool { - return self.members + return self + .members .as_mut() .unwrap() .mining_server diff --git a/config/src/lib.rs b/config/src/lib.rs index 8306a84ed..bc1840cc9 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -23,6 +23,7 @@ extern crate serde; #[macro_use] extern crate serde_derive; +extern crate dirs; extern crate toml; extern crate grin_p2p as p2p; diff --git a/util/src/file.rs b/util/src/file.rs index 394f7d7df..78a2ad51a 100644 --- a/util/src/file.rs +++ b/util/src/file.rs @@ -16,16 +16,18 @@ use std::io; use std::path::{Path, PathBuf}; use walkdir::WalkDir; -pub fn delete(path_buf: PathBuf) -> io::Result<()>{ +/// Delete a directory or file +pub fn delete(path_buf: PathBuf) -> io::Result<()> { if path_buf.is_dir() { fs::remove_dir_all(path_buf) } else if path_buf.is_file() { fs::remove_file(path_buf) } else { - Ok(()) - } -} + Ok(()) + } +} +/// Copy directory, create destination if needed pub fn copy_dir_to(src: &Path, dst: &Path) -> io::Result { let mut counter = 0u64; if !dst.is_dir() { @@ -36,15 +38,19 @@ pub fn copy_dir_to(src: &Path, dst: &Path) -> io::Result { let entry = entry_result?; let file_type = entry.file_type()?; let count = copy_to(&entry.path(), &file_type, &dst.join(entry.file_name()))?; - counter +=count; + counter += count; } Ok(counter) } +/// List directory pub fn list_files(path: String) -> Vec { - let mut files_vec: Vec = vec![]; - for entry in WalkDir::new(Path::new(&path)).into_iter().filter_map(|e| e.ok()) { - match entry.file_name().to_str(){ + let mut files_vec: Vec = vec![]; + for entry in WalkDir::new(Path::new(&path)) + .into_iter() + .filter_map(|e| e.ok()) + { + match entry.file_name().to_str() { Some(path_str) => files_vec.push(path_str.to_string()), None => println!("Could not read optional type"), } @@ -54,11 +60,13 @@ pub fn list_files(path: String) -> Vec { fn copy_to(src: &Path, src_type: &fs::FileType, dst: &Path) -> io::Result { if src_type.is_file() { - fs::copy(src,dst) + fs::copy(src, dst) } else if src_type.is_dir() { copy_dir_to(src, dst) } else { - return Err(io::Error::new(io::ErrorKind::Other, format!("Could not copy: {}", src.display()))) + return Err(io::Error::new( + io::ErrorKind::Other, + format!("Could not copy: {}", src.display()), + )); } } -