| /* This Source Code Form is subject to the terms of the Mozilla Public | |
| * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| //! Contains routines for retrieving default config directories, | |
| //! which for linux uses the XDG base directory spec and provides | |
| //! similar abstractions for non-linux platforms. | |
| extern crate xdg; | |
| use std::env; | |
| use std::path::PathBuf; | |
| use std::fs::File; | |
| use std::fs; | |
| /// This method bootstraps all Servo specific directories | |
| /// on linux platform following the XDG base directory spec. | |
| /// during default command line args setup by opts.rs | |
| #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os="windows")))] | |
| pub fn bootstrap_default_dirs() { | |
| let mut xdg_dirs = xdg::BaseDirectories::new().unwrap(); | |
| xdg_dirs.create_config_directory("servo").unwrap(); | |
| xdg_dirs.create_data_directory("servo").unwrap(); | |
| xdg_dirs.create_cache_directory("servo").unwrap(); | |
| xdg_dirs.create_runtime_directory("servo").unwrap(); | |
| } | |
| #[cfg(target_os = "macos")] | |
| pub fn default_config_dir() -> Option<String> { | |
| let mut xdg_dirs = xdg::BaseDirectories::with_prefix("servo").unwrap(); | |
| let mut config_dir = env::home_dir().unwrap(); | |
| config_dir.push("Library"); | |
| config_dir.push("Application Support"); | |
| config_dir.push("Servo"); | |
| let config_dir_str = config_dir.to_str().unwrap().to_owned(); | |
| config_dir_str | |
| } | |
| #[cfg(target_os = "windows")] | |
| pub fn default_config_dir() -> Option<String> { | |
| let mut config_dir = match env::var("APPDATA") { | |
| Ok(appdata_path) => PathBuf::new(appdata_path), | |
| Err(_) => {let mut dir = env::home_dir().unwrap(); | |
| dir.push("Appdata"); | |
| dir.push("Roaming"); | |
| dir | |
| } | |
| }; | |
| config_dir.push("Servo"); | |
| } | |
| #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))] | |
| pub fn default_config_dir() -> Option<String> { | |
| let mut xdg_dirs = xdg::BaseDirectories::with_prefix("servo").unwrap(); | |
| let config_dir_str = xdg_dirs.get_config_home().to_str().unwrap().to_owned(); | |
| Some(config_dir_str) | |
| } | |
| #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))] | |
| pub fn default_data_dir() -> Option<String> { | |
| let mut xdg_dirs = xdg::BaseDirectories::with_prefix("servo").unwrap(); | |
| let data_dir_str = xdg_dirs.get_data_home().to_str().unwrap().to_owned(); | |
| Some(data_dir_str) | |
| } | |
| #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))] | |
| pub fn default_cache_dir() -> Option<String> { | |
| let mut xdg_dirs = xdg::BaseDirectories::with_prefix("servo").unwrap(); | |
| let cache_dir_str = xdg_dirs.get_cache_home().to_str().unwrap().to_owned(); | |
| Some(cache_dir_str) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment