From f480a1f099cd3eae9ddb971880809ab3ff1e9105 Mon Sep 17 00:00:00 2001 From: piegames Date: Sun, 30 Jul 2023 13:27:28 +0200 Subject: [PATCH] Hack in Windows support --- .github/workflows/ci.yml | 21 +++++++++++++++++---- src/base_directories.rs | 25 ++++++++++++++++++++++++- src/lib.rs | 2 -- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd28d17..d3afd9e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,13 +1,20 @@ name: CI -on: [push, pull_request] +on: + push: + branches: ["*"] + pull_request: + branches: ["*"] env: minrust: 1.36.0 jobs: Lints: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: { os: [ ubuntu-latest, windows-latest ] } steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable @@ -19,7 +26,10 @@ jobs: if: always() Test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: { os: [ ubuntu-latest, windows-latest ] } steps: - uses: actions/checkout@v3 @@ -28,7 +38,10 @@ jobs: - run: cargo test MSRV: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: { os: [ ubuntu-latest, windows-latest ] } steps: - uses: actions/checkout@v3 diff --git a/src/base_directories.rs b/src/base_directories.rs index 31ef2a4..7e7c929 100644 --- a/src/base_directories.rs +++ b/src/base_directories.rs @@ -1,5 +1,6 @@ use std::collections::HashSet; use std::ffi::OsString; +#[cfg(any(unix, target_os = "redox"))] use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::{env, error, fmt, fs, io}; @@ -89,6 +90,7 @@ pub struct BaseDirectories { state_home: Option, data_dirs: Vec, config_dirs: Vec, + #[cfg(any(unix, target_os = "redox"))] runtime_dir: Option, } @@ -184,8 +186,11 @@ impl fmt::Display for Permissions { #[derive(Debug)] enum ErrorKind { HomeMissing, + #[cfg(any(unix, target_os = "redox"))] XdgRuntimeDirInaccessible(PathBuf, io::Error), + #[cfg(any(unix, target_os = "redox"))] XdgRuntimeDirInsecure(PathBuf, Permissions), + #[cfg(any(unix, target_os = "redox"))] XdgRuntimeDirMissing, } @@ -301,6 +306,7 @@ impl BaseDirectories { let config_dirs = env_var("XDG_CONFIG_DIRS") .and_then(abspaths) .unwrap_or(vec![PathBuf::from("/etc/xdg")]); + #[cfg(any(unix, target_os = "redox"))] let runtime_dir = env_var("XDG_RUNTIME_DIR").and_then(abspath); // optional let prefix = PathBuf::from(prefix); @@ -313,11 +319,13 @@ impl BaseDirectories { state_home, data_dirs, config_dirs, + #[cfg(any(unix, target_os = "redox"))] runtime_dir, } } /// Returns the user-specific runtime directory (set by `XDG_RUNTIME_DIR`). + #[cfg(any(unix, target_os = "redox"))] pub fn get_runtime_directory(&self) -> Result<&PathBuf, Error> { if let Some(ref runtime_dir) = self.runtime_dir { // If XDG_RUNTIME_DIR is in the environment but not secure, @@ -343,7 +351,14 @@ impl BaseDirectories { /// Returns `true` if `XDG_RUNTIME_DIR` is available, `false` otherwise. pub fn has_runtime_directory(&self) -> bool { - self.get_runtime_directory().is_ok() + #[cfg(any(unix, target_os = "redox"))] + { + self.get_runtime_directory().is_ok() + } + #[cfg(not(any(unix, target_os = "redox")))] + { + false + } } /// Like [`place_config_file()`](#method.place_config_file), but does @@ -381,6 +396,7 @@ impl BaseDirectories { /// Like [`place_runtime_file()`](#method.place_runtime_file), but does /// not create any directories. /// If `XDG_RUNTIME_DIR` is not available, returns an error. + #[cfg(any(unix, target_os = "redox"))] pub fn get_runtime_file>(&self, path: P) -> io::Result { let runtime_dir = self.get_runtime_directory()?; Ok(runtime_dir.join(self.user_prefix.join(path))) @@ -419,6 +435,7 @@ impl BaseDirectories { /// 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. + #[cfg(any(unix, target_os = "redox"))] pub fn place_runtime_file>(&self, path: P) -> io::Result { write_file(self.get_runtime_directory()?, &self.user_prefix.join(path)) } @@ -504,6 +521,7 @@ impl BaseDirectories { /// 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`. + #[cfg(any(unix, target_os = "redox"))] pub fn find_runtime_file>(&self, path: P) -> Option { let runtime_dir = self.get_runtime_directory().ok()?; read_file( @@ -556,6 +574,7 @@ impl BaseDirectories { /// 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. + #[cfg(any(unix, target_os = "redox"))] pub fn create_runtime_directory>(&self, path: P) -> io::Result { create_directory( Some(self.get_runtime_directory()?), @@ -640,6 +659,7 @@ impl BaseDirectories { /// 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`. + #[cfg(any(unix, target_os = "redox"))] pub fn list_runtime_files>(&self, path: P) -> Vec { if let Ok(runtime_dir) = self.get_runtime_directory() { list_files( @@ -952,6 +972,7 @@ mod test { ("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()), + #[cfg(any(unix, target_os = "redox"))] ("XDG_RUNTIME_DIR", "test_files/runtime-bad".to_string()), ]), ); @@ -1026,6 +1047,7 @@ mod test { } #[test] + #[cfg(any(unix, target_os = "redox"))] fn test_runtime_bad() { let cwd = env::current_dir().unwrap().to_string_lossy().into_owned(); let xd = BaseDirectories::with_env( @@ -1040,6 +1062,7 @@ mod test { } #[test] + #[cfg(any(unix, target_os = "redox"))] fn test_runtime_good() { use std::fs::File; diff --git a/src/lib.rs b/src/lib.rs index 0f7f098..d75cdfc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![cfg(any(unix, target_os = "redox"))] - mod base_directories; pub use crate::base_directories::{ BaseDirectories, Error as BaseDirectoriesError, FileFindIterator,