diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-04-27 08:59:39 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-03 14:00:37 -0300 |
| commit | 9756ed37cc1e095cfc0c48dbd5748758a09324c3 (patch) | |
| tree | 7167570866415fae6edf03337892228fc9a98540 | |
| parent | 9f2adbe2baa2d7a0e1cefab77091cf68b485e944 (diff) | |
| download | mullvadvpn-9756ed37cc1e095cfc0c48dbd5748758a09324c3.tar.xz mullvadvpn-9756ed37cc1e095cfc0c48dbd5748758a09324c3.zip | |
Use `rpc_file_path` from `mullvad-ipc-client`
| -rw-r--r-- | mullvad-daemon/src/rpc_address_file.rs | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/mullvad-daemon/src/rpc_address_file.rs b/mullvad-daemon/src/rpc_address_file.rs index 84ec176f85..2280c6377f 100644 --- a/mullvad-daemon/src/rpc_address_file.rs +++ b/mullvad-daemon/src/rpc_address_file.rs @@ -2,8 +2,13 @@ use std::fs::{self, File, OpenOptions}; use std::io::{self, Write}; use std::path::{Path, PathBuf}; +use mullvad_ipc_client::rpc_file_path; + error_chain! { errors { + UnknownFilePath { + description("Failed to find path for RPC connection info file") + } WriteFailed(path: PathBuf) { description("Failed to write RPC connection info to file") display("Failed to write RPC connection info to {}", path.to_string_lossy()) @@ -15,46 +20,34 @@ error_chain! { } } -#[cfg(unix)] -lazy_static! { - /// The path to the file where we write the RPC connection info - static ref RPC_ADDRESS_FILE_PATH: PathBuf = Path::new("/tmp").join(".mullvad_rpc_address"); -} - -#[cfg(not(unix))] -lazy_static! { - /// The path to the file where we write the RPC connection info - static ref RPC_ADDRESS_FILE_PATH: PathBuf = { - let windows_directory = ::std::env::var_os("WINDIR").unwrap(); - PathBuf::from(windows_directory).join("Temp").join(".mullvad_rpc_address") - }; -} - - /// Writes down the RPC connection info to some API to a file. pub fn write(rpc_address: &str, shared_secret: &str) -> Result<()> { // Avoids opening an existing file owned by another user and writing sensitive data to it. remove()?; - open_file(RPC_ADDRESS_FILE_PATH.as_path()) + let file_path = rpc_file_path().chain_err(|| ErrorKind::UnknownFilePath)?; + + open_file(&file_path) .and_then(|mut file| write!(file, "{}\n{}\n", rpc_address, shared_secret)) - .chain_err(|| ErrorKind::WriteFailed(RPC_ADDRESS_FILE_PATH.to_owned()))?; + .chain_err(|| ErrorKind::WriteFailed(file_path.clone()))?; debug!( "Wrote RPC connection info to {}", - RPC_ADDRESS_FILE_PATH.to_string_lossy() + file_path.to_string_lossy() ); Ok(()) } /// Removes the RPC file, if it exists. pub fn remove() -> Result<()> { - if let Err(error) = fs::remove_file(RPC_ADDRESS_FILE_PATH.as_path()) { + let file_path = rpc_file_path().chain_err(|| ErrorKind::UnknownFilePath)?; + + if let Err(error) = fs::remove_file(&file_path) { if error.kind() == io::ErrorKind::NotFound { // No previously existing file Ok(()) } else { - Err(error).chain_err(|| ErrorKind::RemoveFailed(RPC_ADDRESS_FILE_PATH.to_owned())) + Err(error).chain_err(|| ErrorKind::RemoveFailed(file_path)) } } else { Ok(()) |
