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(namespace: &str, sub_namespace: &str, key: Option<&str>, operation: &str) -> Result<(), std::io::Error> {
10 if let Some(key) = key {
12 debug_assert!(false, "Failed to {} {}/{}/{}: key may not be empty.", operation,
13 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
14 let msg = format!("Failed to {} {}/{}/{}: key may not be empty.", operation,
15 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
16 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
19 if namespace.is_empty() && !sub_namespace.is_empty() {
21 "Failed to {} {}/{}/{}: namespace may not be empty if a non-empty sub-namespace is given.",
23 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
25 "Failed to {} {}/{}/{}: namespace may not be empty if a non-empty sub-namespace is given.", operation,
26 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
27 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
30 if !is_valid_kvstore_str(namespace) || !is_valid_kvstore_str(sub_namespace) || !is_valid_kvstore_str(key) {
31 debug_assert!(false, "Failed to {} {}/{}/{}: namespace, sub-namespace, and key must be valid.",
33 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
34 let msg = format!("Failed to {} {}/{}/{}: namespace, sub-namespace, and key must be valid.",
36 PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key));
37 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
40 if namespace.is_empty() && !sub_namespace.is_empty() {
42 "Failed to {} {}/{}: namespace may not be empty if a non-empty sub-namespace is given.",
43 operation, PrintableString(namespace), PrintableString(sub_namespace));
45 "Failed to {} {}/{}: namespace may not be empty if a non-empty sub-namespace is given.",
46 operation, PrintableString(namespace), PrintableString(sub_namespace));
47 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
49 if !is_valid_kvstore_str(namespace) || !is_valid_kvstore_str(sub_namespace) {
50 debug_assert!(false, "Failed to {} {}/{}: namespace and sub-namespace must be valid.",
51 operation, PrintableString(namespace), PrintableString(sub_namespace));
52 let msg = format!("Failed to {} {}/{}: namespace and sub-namespace must be valid.",
53 operation, PrintableString(namespace), PrintableString(sub_namespace));
54 return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));