From 02061a58501ca8d1ef26e58e857244fc86ebe1bb Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 14 May 2024 15:13:50 -0700 Subject: [PATCH] wip: use Nix's sqlite db "directly" this might not be a safe assumption, but we're gonna try --- Cargo.lock | 27 +++++++++++++++++++++++++ magic-nix-cache/Cargo.toml | 1 + magic-nix-cache/src/util.rs | 39 ++++++++++++------------------------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8175c8d..ad3c96e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1642,6 +1642,18 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastcdc" version = "3.1.0" @@ -2544,6 +2556,7 @@ dependencies = [ "is_ci", "netrc-rs", "reqwest", + "rusqlite", "serde", "serde_json", "sha2", @@ -3411,6 +3424,20 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rusqlite" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a78046161564f5e7cd9008aff3b2990b3850dc8e0349119b98e8f251e099f24d" +dependencies = [ + "bitflags 2.4.2", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "rust_decimal" version = "1.34.3" diff --git a/magic-nix-cache/Cargo.toml b/magic-nix-cache/Cargo.toml index 6ee4709..3ed022b 100644 --- a/magic-nix-cache/Cargo.toml +++ b/magic-nix-cache/Cargo.toml @@ -33,6 +33,7 @@ uuid = { version = "1.4.0", features = ["serde", "v7", "rand", "std"] } futures = "0.3" async-compression = "0.4" tracing-appender = "0.2.3" +rusqlite = { version = "0.30", features = ["bundled"] } [dependencies.tokio] version = "1.28.0" diff --git a/magic-nix-cache/src/util.rs b/magic-nix-cache/src/util.rs index 2c7e759..ced7180 100644 --- a/magic-nix-cache/src/util.rs +++ b/magic-nix-cache/src/util.rs @@ -9,34 +9,19 @@ use crate::error::Result; /// Returns the list of store paths that are currently present. pub async fn get_store_paths(store: &NixStore) -> Result> { - // FIXME: use the Nix API. - let store_dir = store.store_dir(); - let mut listing = tokio::fs::read_dir(store_dir).await?; - let mut paths = HashSet::new(); - while let Some(entry) = listing.next_entry().await? { - let file_name = entry.file_name(); - let file_name = Path::new(&file_name); + // FIXME(cole-h): update the nix bindings to get the dbdir of the localstore? + let db = + rusqlite::Connection::open("file:/nix/var/nix/db/db.sqlite?immutable=1").expect("FIXME"); - if let Some(extension) = file_name.extension() { - match extension.to_str() { - None | Some("drv") | Some("chroot") => { - tracing::debug!( - "skipping file with weird or uninteresting extension {extension:?}" - ); - continue; - } - _ => {} - } - } + let mut stmt = db.prepare("SELECT path FROM ValidPaths").expect("FIXME"); + let paths = stmt + .query_map([], |row| -> std::result::Result { + Ok(PathBuf::from(row.get::<_, String>(0)?)) + }) + .expect("FIXME") + .into_iter() + .map(|r| r.expect("FIXME")) + .collect::>(); - if let Some(s) = file_name.to_str() { - // Special paths (so far only `.links`) - if s == ".links" { - continue; - } - } - - paths.insert(store_dir.join(file_name)); - } Ok(paths) }