Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo-credential-libsecret: load libsecret only once #15295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions credential/cargo-credential-libsecret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod linux {
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int};
use std::ptr::{null, null_mut};
use std::sync::OnceLock;

#[allow(non_camel_case_types)]
type gchar = c_char;
Expand Down Expand Up @@ -85,6 +86,21 @@ mod linux {

pub struct LibSecretCredential;

fn get_libsecret() -> Result<&'static Library, Error> {
static LIBSECRET: OnceLock<Library> = OnceLock::new();
// unfortunately `get_or_try_init` is not yet stable
match LIBSECRET.get() {
Some(lib) => Ok(lib),
None => {
let _ = LIBSECRET.set(unsafe { Library::new("libsecret-1.so.0") }.context(
"failed to load libsecret: try installing the `libsecret` \
or `libsecret-1-0` package with the system package manager",
)?);
Ok(LIBSECRET.get().unwrap())
}
}
}

fn label(index_url: &str) -> CString {
CString::new(format!("cargo-registry:{}", index_url)).unwrap()
}
Expand Down Expand Up @@ -114,15 +130,11 @@ mod linux {
) -> Result<CredentialResponse, Error> {
// Dynamically load libsecret to avoid users needing to install
// additional -dev packages when building this provider.
let lib;
let lib = get_libsecret()?;
let secret_password_lookup_sync: Symbol<'_, SecretPasswordLookupSync>;
let secret_password_store_sync: Symbol<'_, SecretPasswordStoreSync>;
let secret_password_clear_sync: Symbol<'_, SecretPasswordClearSync>;
unsafe {
lib = Library::new("libsecret-1.so.0").context(
"failed to load libsecret: try installing the `libsecret` \
or `libsecret-1-0` package with the system package manager",
)?;
secret_password_lookup_sync = lib
.get(b"secret_password_lookup_sync\0")
.map_err(Box::new)?;
Expand Down