Skip to content
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

Fixes issue #34: set tmpfile length in async writer #35

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/content/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use async_std::task::{self, Context, JoinHandle, Poll};
use futures::io::AsyncWrite;
use futures::prelude::*;
use memmap2::MmapMut;
// use memmap::MmapMut;
ceejbot marked this conversation as resolved.
Show resolved Hide resolved
use ssri::{Algorithm, Integrity, IntegrityOpts};
use tempfile::NamedTempFile;

Expand Down Expand Up @@ -121,11 +122,12 @@ impl AsyncWriter {
.create(&tmp_path)
.await
.to_internal()?;
let tmpfile = task::spawn_blocking(|| NamedTempFile::new_in(tmp_path))
let mut tmpfile = task::spawn_blocking(|| NamedTempFile::new_in(tmp_path))
.await
.to_internal()?;
let mmap = if let Some(size) = size {
if size <= MAX_MMAP_SIZE {
tmpfile.as_file_mut().set_len(size as u64).to_internal()?;
unsafe { MmapMut::map_mut(tmpfile.as_file()).ok() }
} else {
None
Expand Down
30 changes: 30 additions & 0 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,34 @@ mod tests {
let data = crate::read_sync(&dir, "hello").unwrap();
assert_eq!(data, b"hello");
}

#[test]
fn hash_write_sync() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let original = format!("hello world{}", 5);
let integrity = crate::write_hash_sync(&dir, &original)
.expect("should be able to write a hash synchronously");
let bytes = crate::read_hash_sync(&dir, &integrity)
.expect("should be able to read the data we just wrote");
let result =
String::from_utf8(bytes).expect("we wrote valid utf8 but did not read valid utf8 back");
assert_eq!(result, original, "we did not read back what we wrote");
}

#[async_attributes::test]
async fn hash_write_async() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let original = format!("hello world{}", 12);
let integrity = crate::write_hash(&dir, &original)
.await
.expect("should be able to write a hash asynchronously");
let bytes = crate::read_hash(&dir, &integrity)
.await
.expect("should be able to read back what we wrote");
let result =
String::from_utf8(bytes).expect("we wrote valid utf8 but did not read valid utf8 back");
assert_eq!(result, original, "we did not read back what we wrote");
}
}