1 use lightning::util::persist::{KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN};
2 use lightning::util::string::PrintableString;
5 pub(crate) fn is_valid_kvstore_str(key: &str) -> bool {
6 key.len() <= KVSTORE_NAMESPACE_KEY_MAX_LEN && key.chars().all(|c| KVSTORE_NAMESPACE_KEY_ALPHABET.contains(c))
9 pub(crate) fn check_namespace_key_validity(
10 primary_namespace: &str, secondary_namespace: &str, key: Option<&str>, operation: &str)
11 -> Result<(), std::io::Error> {
12 if let Some(key) = key {
14 debug_assert!(false, "Failed to {} {}/{}/{}: key may not be empty.", operation,
15 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
16 let msg = format!("Failed to {} {}/{}/{}: key may not be empty.", operation,
17 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
18 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
21 if primary_namespace.is_empty() && !secondary_namespace.is_empty() {
23 "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
25 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
27 "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", operation,
28 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
29 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
32 if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) || !is_valid_kvstore_str(key) {
33 debug_assert!(false, "Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.",
35 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
36 let msg = format!("Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.",
38 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
39 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
42 if primary_namespace.is_empty() && !secondary_namespace.is_empty() {
44 "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
45 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
47 "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
48 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
49 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
51 if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) {
52 debug_assert!(false, "Failed to {} {}/{}: primary namespace and secondary namespace must be valid.",
53 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
54 let msg = format!("Failed to {} {}/{}: primary namespace and secondary namespace must be valid.",
55 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
56 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));