Skip to content

upki: add support for system-wide configuration/caching (alt)#121

Open
ctz wants to merge 2 commits into
mainfrom
jbp-system-dirs
Open

upki: add support for system-wide configuration/caching (alt)#121
ctz wants to merge 2 commits into
mainfrom
jbp-system-dirs

Conversation

@ctz
Copy link
Copy Markdown
Member

@ctz ctz commented May 5, 2026

This is an alternative of #106

@ctz ctz requested a review from djc May 5, 2026 17:05
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 5, 2026

Merging this PR will not alter performance

✅ 4 untouched benchmarks


Comparing jbp-system-dirs (1ca91cd) with main (cb0df8d)

Open in CodSpeed

@djc
Copy link
Copy Markdown
Member

djc commented May 5, 2026

Why should this be scoped to Unix only?

@ctz
Copy link
Copy Markdown
Member Author

ctz commented May 5, 2026

Why should this be scoped to Unix only?

Just trying to avoid looking at c:\etc\upki\config.toml on windows, which is -- at best -- not helpful.

@djc
Copy link
Copy Markdown
Member

djc commented May 5, 2026

I think one of my PRs had a global path for Windows too.

@ctz
Copy link
Copy Markdown
Member Author

ctz commented May 6, 2026

Sorry, which one is that?

@djc
Copy link
Copy Markdown
Member

djc commented May 6, 2026

Ah, maybe I never pushed it, but I found this in my git stash stack:

djc-2021 system-dirs upki $ git show stash@{1}
commit ed57832313a15096805019b55135426b3ad5bc70
Merge: 46f51ba e8f9072
Author: Dirkjan Ochtman <dirkjan@ochtman.nl>
Date:   Fri Apr 17 16:50:45 2026 +0200

    WIP on system-dirs: 46f51ba upki: take PathKind for Config construction

diff --cc upki/src/lib.rs
index 6c67473,6c67473..d011502
--- a/upki/src/lib.rs
+++ b/upki/src/lib.rs
@@@ -101,6 -101,6 +101,8 @@@ impl AsRef<Path> for ConfigPath 
  /// What kind of path is being determined.
  #[derive(Clone, Copy, Debug)]
  pub enum PathKind {
++    /// System-wide configuration and data.
++    System,
      /// User-relative configuration and data.
      User,
  }
@@@ -108,17 -108,17 +110,69 @@@
  impl PathKind {
      fn config_dir(self) -> Result<PathBuf, Error> {
          Ok(match self {
++            Self::System => platform::system_config_dir()?,
              Self::User => project_dirs()?.config_dir().to_owned(),
          })
      }
  
      fn cache_dir(self) -> Result<PathBuf, Error> {
          Ok(match self {
++            Self::System => platform::system_cache_dir()?,
              Self::User => project_dirs()?.cache_dir().to_owned(),
          })
      }
  }
  
++#[cfg(target_os = "macos")]
++mod platform {
++    use super::*;
++
++    pub(super) fn system_config_dir() -> Result<PathBuf, Error> {
++        Ok(PathBuf::from("/Library/Application Support").join(PREFIX))
++    }
++
++    pub(super) fn system_cache_dir() -> Result<PathBuf, Error> {
++        Ok(PathBuf::from("/Library/Caches").join(BUNDLE_ID))
++    }
++}
++
++#[cfg(target_os = "linux")]
++mod platform {
++    use super::*;
++
++    pub(super) fn system_config_dir() -> Result<PathBuf, Error> {
++        Ok(PathBuf::from("/etc").join(PREFIX))
++    }
++
++    pub(super) fn system_cache_dir() -> Result<PathBuf, Error> {
++        Ok(PathBuf::from("/var/cache").join(PREFIX))
++    }
++}
++
++#[cfg(target_os = "windows")]
++mod platform {
++    use super::*;
++
++    pub(super) fn system_config_dir() -> Result<PathBuf, Error> {
++        match env::var_os("ProgramData") {
++            Ok(base) => PathBuf::from(base)
++                .join(VENDOR)
++                .join(PREFIX),
++            Err(_) => return Err(Error::NoConfigDirectoryFound),
++        }
++    }
++
++    pub(super) fn system_cache_dir() -> Result<PathBuf, Error> {
++        match env::var_os("ProgramData") {
++            Ok(base) => PathBuf::from(base)
++                .join(VENDOR)
++                .join(PREFIX)
++                .join("cache"),
++            Err(_) => return Err(Error::NoCacheDirectoryFound),
++        }
++    }
++}
++
  /// Errors for the upki library API.
  #[non_exhaustive]
  #[derive(Debug)]
@@@ -181,5 -181,5 +235,8 @@@ fn project_dirs() -> Result<ProjectDirs
      ProjectDirs::from("dev", "rustls", PREFIX).ok_or(Error::NoValidHomeDirectory)
  }
  
++const BUNDLE_ID: &str = "dev.rustls.upki";
++#[cfg(target_os = "windows")]
++const VENDOR: &str = "rustls";
  const PREFIX: &str = "upki";
  const CONFIG_FILE: &str = "config.toml";

Comment thread upki/src/lib.rs Outdated
@ctz ctz force-pushed the jbp-system-dirs branch from 5006ab9 to 1ca91cd Compare May 8, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants