Skip to content

Commit

Permalink
Hack in Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Jul 30, 2023
1 parent dea09d9 commit f480a1f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
21 changes: 17 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
25 changes: 24 additions & 1 deletion src/base_directories.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -89,6 +90,7 @@ pub struct BaseDirectories {
state_home: Option<PathBuf>,
data_dirs: Vec<PathBuf>,
config_dirs: Vec<PathBuf>,
#[cfg(any(unix, target_os = "redox"))]
runtime_dir: Option<PathBuf>,
}

Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> io::Result<PathBuf> {
let runtime_dir = self.get_runtime_directory()?;
Ok(runtime_dir.join(self.user_prefix.join(path)))
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> io::Result<PathBuf> {
write_file(self.get_runtime_directory()?, &self.user_prefix.join(path))
}
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf> {
let runtime_dir = self.get_runtime_directory().ok()?;
read_file(
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> io::Result<PathBuf> {
create_directory(
Some(self.get_runtime_directory()?),
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> Vec<PathBuf> {
if let Ok(runtime_dir) = self.get_runtime_directory() {
list_files(
Expand Down Expand Up @@ -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()),
]),
);
Expand Down Expand Up @@ -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(
Expand All @@ -1040,6 +1062,7 @@ mod test {
}

#[test]
#[cfg(any(unix, target_os = "redox"))]
fn test_runtime_good() {
use std::fs::File;

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(any(unix, target_os = "redox"))]

mod base_directories;
pub use crate::base_directories::{
BaseDirectories, Error as BaseDirectoriesError, FileFindIterator,
Expand Down

0 comments on commit f480a1f

Please sign in to comment.