diff options
| author | troido <troido@protonmail.com> | 2020-04-23 12:53:01 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-23 12:53:01 +0200 |
| commit | 080466200060d2d3ec64bec32a4959fa061b79ce (patch) | |
| tree | fbe602a0777583086b21799028f882d7b63d5c31 /src/util.rs | |
| parent | b41c30fa15aea0b01b8fa30e378d123da046a1e6 (diff) | |
accept authentication messages, and validate registrations
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 6a22031..02c282c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ use std::cmp::{min, max}; + pub fn clamp<T: Ord>(val: T, lower: T, upper: T) -> T{ max(min(val, upper), lower) } @@ -14,6 +15,30 @@ pub fn strip_prefix<'a>(txt: &'a str, prefix: &'a str) -> Option<&'a str> { } } +use std::fs; +use std::path::Path; +use crate::{ + errors::AnyError, + aerr +}; + +pub fn write_file_safe<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<(), AnyError> { + let temppath = path + .as_ref() + .with_file_name( + format!( + "tempfile_{}_{}.tmp", + path.as_ref().file_name().ok_or(aerr!("writing to directory"))?.to_str().unwrap_or("invalid"), + rand::random::<u64>() + ) + ); + fs::write(&temppath, contents)?; + fs::rename(&temppath, path)?; + Ok(()) +} + + + #[macro_export] macro_rules! hashmap { ( $($key:expr => $value:expr ),* ) => {{ |
