-
Notifications
You must be signed in to change notification settings - Fork 1
Report an error when pushing conflicting metadata. #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ use cached::instant::SystemTime; | |
| use lazy_static::lazy_static; | ||
| use regex::Regex; | ||
| use std::ffi::OsString; | ||
| use std::path::Path; | ||
| use std::time::UNIX_EPOCH; | ||
| use std::{fs, io, io::Write}; | ||
|
|
||
| lazy_static! { | ||
| static ref ID_REG: Regex = Regex::new(r"^([0-9]{8}-[0-9]{6}-[[:xdigit:]]{8})$").unwrap(); | ||
|
|
@@ -20,10 +22,34 @@ pub fn time_as_num(time: SystemTime) -> f64 { | |
| (time.duration_since(UNIX_EPOCH).unwrap().as_millis() as f64) / 1000.0 | ||
| } | ||
|
|
||
| /// Write a byte slice to disk. | ||
| /// | ||
| /// Succeeds if the file already exists with identical contents. | ||
| /// On the other hand, an AlreadyExists error is returned if the file exists with different contents. | ||
| pub fn write_file_idempotent(path: &Path, contents: &[u8]) -> io::Result<()> { | ||
| match fs::OpenOptions::new() | ||
| .write(true) | ||
| .create_new(true) | ||
| .open(path) | ||
| { | ||
| Ok(mut f) => { | ||
| f.write_all(contents)?; | ||
| } | ||
| Err(err) if err.kind() == io::ErrorKind::AlreadyExists => { | ||
| if fs::read(path)? != contents { | ||
| return Err(err); | ||
| } | ||
|
Comment on lines
+39
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we ok with end-of-line standardisartion between windows/everyone else here? I can't remember what we do in this case - perhaps we dodge it by spitting everything out on one line first?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. outpack_server doesn't ever do any special processing of newline or anything else. Metadata files are an immutable blob of bytes. |
||
| } | ||
| Err(err) => return Err(err), | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::time::Duration; | ||
| use tempfile::tempdir; | ||
|
|
||
| #[test] | ||
| fn can_detect_packet_id() { | ||
|
|
@@ -39,4 +65,42 @@ mod tests { | |
| let res = time_as_num(time); | ||
| assert_eq!(res, 1688033668.123); | ||
| } | ||
|
|
||
| #[test] | ||
| fn write_file_idempotent_writes_to_disk() { | ||
| let base = tempdir().unwrap(); | ||
| let path = base.path().join("hello.txt"); | ||
|
|
||
| write_file_idempotent(&path, b"Hello").unwrap(); | ||
|
|
||
| let contents = fs::read(path).unwrap(); | ||
| assert_eq!(contents, b"Hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn write_file_idempotent_succeeds_if_content_is_identical() { | ||
| let base = tempdir().unwrap(); | ||
| let path = base.path().join("hello.txt"); | ||
|
|
||
| fs::write(&path, b"Hello").unwrap(); | ||
|
|
||
| write_file_idempotent(&path, b"Hello").unwrap(); | ||
|
|
||
| let contents = fs::read(path).unwrap(); | ||
| assert_eq!(contents, b"Hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn write_file_idempotent_succeeds_if_content_is_different() { | ||
| let base = tempdir().unwrap(); | ||
| let path = base.path().join("hello.txt"); | ||
|
|
||
| fs::write(&path, b"Hello").unwrap(); | ||
|
|
||
| let error = write_file_idempotent(&path, b"World").unwrap_err(); | ||
| assert_eq!(error.kind(), io::ErrorKind::AlreadyExists); | ||
|
|
||
| let contents = fs::read(path).unwrap(); | ||
| assert_eq!(contents, b"Hello"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this name, but I also couldn't find a better one that expresses the idea of detecting conflicts.