Skip to content

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

Merged
djc merged 3 commits into
mainfrom
jbp-system-dirs
May 14, 2026
Merged

upki: add support for system-wide configuration/caching (alt)#121
djc merged 3 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 (54ff3de) with main (1aac2dc)

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
@djc djc force-pushed the jbp-system-dirs branch 2 times, most recently from 8a47569 to 07f6bca Compare May 13, 2026 10:02
@djc
Copy link
Copy Markdown
Member

djc commented May 13, 2026

Introduced platform-specific system-wide config directories, and a platform-specific system-wide default cache dir.

@djc djc force-pushed the jbp-system-dirs branch 4 times, most recently from f948070 to b9b13a4 Compare May 13, 2026 11:39
Copy link
Copy Markdown
Member Author

@ctz ctz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

@ctz
Copy link
Copy Markdown
Member Author

ctz commented May 14, 2026

(will rebase shortly)

@ctz ctz force-pushed the jbp-system-dirs branch from b9b13a4 to 54ff3de Compare May 14, 2026 11:08
@djc djc added this pull request to the merge queue May 14, 2026
Merged via the queue into main with commit 8d90273 May 14, 2026
10 checks passed
@djc djc deleted the jbp-system-dirs branch May 14, 2026 11:49
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