Added constants and functions for getting the effective database path

and cache directory
This commit is contained in:
Nathan Fisher 2023-03-31 11:39:20 -04:00
parent ccd163edf5
commit bbb611c77c
2 changed files with 157 additions and 26 deletions

94
Cargo.lock generated
View File

@ -226,7 +226,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"scratch", "scratch",
"syn 2.0.11", "syn 2.0.12",
] ]
[[package]] [[package]]
@ -243,7 +243,7 @@ checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.11", "syn 2.0.12",
] ]
[[package]] [[package]]
@ -395,9 +395,9 @@ dependencies = [
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.54" version = "0.1.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c17cc76786e99f8d2f055c11159e7f0091c42474dcc3189fbab96072e873e6d" checksum = "716f12fbcfac6ffab0a5e9ec51d0a0ff70503742bb2dc7b99396394c9dc323f0"
dependencies = [ dependencies = [
"android_system_properties", "android_system_properties",
"core-foundation-sys", "core-foundation-sys",
@ -757,7 +757,7 @@ checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.11", "syn 2.0.12",
] ]
[[package]] [[package]]
@ -807,9 +807,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.11" version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40" checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -857,7 +857,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.11", "syn 2.0.12",
] ]
[[package]] [[package]]
@ -1110,9 +1110,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.46.0" version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdacb41e6a96a052c6cb63a144f24900236121c6f63f4f8219fef5977ecb0c25" checksum = "2649ff315bee4c98757f15dac226efe3d81927adbb6e882084bb1ee3e0c330a7"
dependencies = [ dependencies = [
"windows-targets", "windows-targets",
] ]
@ -1123,28 +1123,28 @@ version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.42.2",
"windows_i686_gnu", "windows_i686_gnu 0.42.2",
"windows_i686_msvc", "windows_i686_msvc 0.42.2",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.42.2",
] ]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.42.2" version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" checksum = "2f8996d3f43b4b2d44327cd71b7b0efd1284ab60e6e9d0e8b630e18555d87d3e"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.47.0",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.47.0",
"windows_i686_gnu", "windows_i686_gnu 0.47.0",
"windows_i686_msvc", "windows_i686_msvc 0.47.0",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.47.0",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.47.0",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.47.0",
] ]
[[package]] [[package]]
@ -1153,42 +1153,84 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "831d567d53d4f3cb1db332b68e6e2b6260228eb4d99a777d8b2e8ed794027c90"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a42d54a417c60ce4f0e31661eed628f0fa5aca73448c093ec4d45fab4c51cdf"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1925beafdbb22201a53a483db861a5644123157c1c3cee83323a2ed565d71e3"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a8ef8f2f1711b223947d9b69b596cf5a4e452c930fb58b6fc3fdae7d0ec6b31"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7acaa0c2cf0d2ef99b61c308a0c3dbae430a51b7345dedec470bd8f53f5a3642"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5a0628f71be1d11e17ca4a0e9e15b3a5180f6fbf1c2d55e3ba3f850378052c1"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.42.2" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.47.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d6e62c256dc6d40b8c8707df17df8d774e60e39db723675241e7c15e910bce7"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.4.1" version = "0.4.1"

View File

@ -8,6 +8,8 @@ mod plist;
mod repository; mod repository;
mod version; mod version;
use std::path::PathBuf;
pub use { pub use {
creator::{Creator, Message}, creator::{Creator, Message},
db::Database, db::Database,
@ -19,3 +21,90 @@ pub use {
tar, tar,
version::*, version::*,
}; };
pub const DB: [&str; 4] = ["var", "db", "hpk", "db.ron.zstd"];
pub const CACHE: [&str; 3] = ["var", "cache", "hpk"];
/// Gets the database file path. Takes an optional prefix variable for working
/// with an alternate filesystem root.
/// ## Examples
/// Get the database path for the currently running system:
/// ```Rust
/// let db = hpk::get_db(None);
/// ```
/// Get the database path for a chroot mounted at /mnt/aarch64:
/// ```Rust
/// let chroot = PathBuf::from("/mnt/aarch64");
/// let db = hpk::get_db(Some(chroot));
/// ```
pub fn get_db(prefix: Option<PathBuf>) -> PathBuf {
match prefix {
Some(mut p) => {
DB.iter().for_each(|seg| p.push(seg));
p
},
None => {
let mut p = PathBuf::from("/");
DB.iter().for_each(|seg| p.push(seg));
p
},
}
}
/// Gets the cache directory path. Takes an optional prefix variable for working
/// with an alternate filesystem root.
/// ## Examples
/// Get the cache directory for the currently running system:
/// ```Rust
/// let db = hpk::get_cache(None);
/// ```
/// Get the cache directory for a chroot mounted at /mnt/aarch64:
/// ```Rust
/// let chroot = PathBuf::from("/mnt/aarch64");
/// let db = hpk::get_cache(Some(chroot));
/// ```
pub fn get_cache(prefix: Option<PathBuf>) -> PathBuf {
match prefix {
Some(mut p) => {
CACHE.iter().for_each(|seg| p.push(seg));
p
}
None => {
let mut p = PathBuf::from("/");
CACHE.iter().for_each(|seg| p.push(seg));
p
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn getdb_local() {
let db = get_db(None);
assert_eq!(db, PathBuf::from("/var/db/hpk/db.ron.zstd"));
}
#[test]
fn getdb_chroot() {
let prefix = PathBuf::from("/mnt/aarch64");
let db = get_db(Some(prefix));
assert_eq!(db, PathBuf::from("/mnt/aarch64/var/db/hpk/db.ron.zstd"));
}
#[test]
fn get_cache_local() {
let cache = get_cache(None);
assert_eq!(cache, PathBuf::from("/var/cache/hpk"));
}
#[test]
fn get_cache_chroot() {
let prefix = PathBuf::from("/mnt/aarch64");
let cache = get_cache(Some(prefix));
assert_eq!(cache, PathBuf::from("/mnt/aarch64/var/cache/hpk"));
}
}