Skip to content

Commit

Permalink
Merge branch 'master' into 1mb
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef authored Sep 23, 2024
2 parents 2731f94 + 7a113a0 commit 6f30eba
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 65 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/auto_merge_prs.yml

This file was deleted.

13 changes: 5 additions & 8 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,11 @@ jobs:
target
key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

# Run cargo tarpaulin & push result to coveralls.io
- name: rust-tarpaulin code coverage check
uses: actions-rs/[email protected]
with:
args: "-v --release"
version: "0.15.0"
out-type: Lcov
timeout: 2000
# Generate code coverage & push result to coveralls.io
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Generate code coverage
run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
- name: Push code coverage results to coveralls.io
uses: coverallsapp/github-action@master
with:
Expand Down
1 change: 0 additions & 1 deletion src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use bytes::Bytes;
use rayon::prelude::*;
use xor_name::XorName;

///
#[derive(Clone)]
pub(crate) struct EncryptionBatch {
pub(crate) raw_chunks: Vec<RawChunk>,
Expand Down
19 changes: 8 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct EncryptedChunk {
#[derive(Clone)]
pub struct StreamSelfEncryptor {
// File path for the encryption target.
file_path: Box<PathBuf>,
file_path: PathBuf,
// List of `(start_position, end_position)` for each chunk for the target file.
batch_positions: Vec<(usize, usize)>,
// Current step (i.e. chunk_index) for encryption
Expand All @@ -157,16 +157,13 @@ pub struct StreamSelfEncryptor {
// Progressing collection of source chunks' names
src_hashes: BTreeMap<usize, XorName>,
// File path to flush encrypted_chunks into.
chunk_dir: Option<Box<PathBuf>>,
chunk_dir: Option<PathBuf>,
}

impl StreamSelfEncryptor {
/// For encryption, return with an intialized streaming encryptor.
/// If a `chunk_dir` is provided, the encrypted_chunks will be written into the specified dir as well.
pub fn encrypt_from_file(
file_path: Box<PathBuf>,
chunk_dir: Option<Box<PathBuf>>,
) -> Result<Self> {
pub fn encrypt_from_file(file_path: PathBuf, chunk_dir: Option<PathBuf>) -> Result<Self> {
let file = File::open(&*file_path)?;
let metadata = file.metadata()?;
let file_size = metadata.len();
Expand Down Expand Up @@ -260,7 +257,7 @@ impl StreamSelfEncryptor {
/// The streaming decryptor to carry out the decryption on fly, chunk by chunk.
pub struct StreamSelfDecryptor {
// File path for the decryption output.
file_path: Box<PathBuf>,
file_path: PathBuf,
// Current step (i.e. chunk_index) for decryption
chunk_index: usize,
// Source hashes of the chunks that collected from the data_map, they shall already be sorted by index.
Expand All @@ -273,7 +270,7 @@ pub struct StreamSelfDecryptor {

impl StreamSelfDecryptor {
/// For decryption, return with an intialized streaming decryptor
pub fn decrypt_to_file(file_path: Box<PathBuf>, data_map: &DataMap) -> Result<Self> {
pub fn decrypt_to_file(file_path: PathBuf, data_map: &DataMap) -> Result<Self> {
let temp_dir = tempdir()?;
let src_hashes = extract_hashes(data_map);

Expand Down Expand Up @@ -336,7 +333,7 @@ impl StreamSelfDecryptor {
// Drain any in-order chunks due to the recent filled in piece.
fn drain_unprocessed(&mut self) -> Result<()> {
while let Some(chunk_name) = self.encrypted_chunks.get(&self.chunk_index) {
let file_path = self.temp_dir.path().join(&hex::encode(chunk_name));
let file_path = self.temp_dir.path().join(hex::encode(chunk_name));
let mut chunk_file = File::open(file_path)?;
let mut chunk_data = Vec::new();
let _ = chunk_file.read_to_end(&mut chunk_data)?;
Expand Down Expand Up @@ -366,7 +363,7 @@ pub fn encrypt_from_file(file_path: &Path, output_dir: &Path) -> Result<(DataMap
let chunk_name = XorName::from_content(&chunk.content);
chunk_names.push(chunk_name);

let file_path = output_dir.join(&hex::encode(chunk_name));
let file_path = output_dir.join(hex::encode(chunk_name));
let mut output_file = File::create(file_path)?;
output_file.write_all(&chunk.content)?;
}
Expand All @@ -384,7 +381,7 @@ pub fn decrypt_from_chunk_files(
let mut encrypted_chunks = Vec::new();
for chunk_info in data_map.infos() {
let chunk_name = chunk_info.dst_hash;
let file_path = chunk_dir.join(&hex::encode(chunk_name));
let file_path = chunk_dir.join(hex::encode(chunk_name));
let mut chunk_file = File::open(file_path)?;
let mut chunk_data = Vec::new();
let _ = chunk_file.read_to_end(&mut chunk_data)?;
Expand Down
10 changes: 4 additions & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ fn test_stream_self_encryptor() -> Result<(), Error> {
create_dir_all(chunk_path.clone())?;

// Encrypt the file using StreamSelfEncryptor
let mut encryptor = StreamSelfEncryptor::encrypt_from_file(
Box::new(file_path),
Some(Box::new(chunk_path.clone())),
)?;
let mut encryptor =
StreamSelfEncryptor::encrypt_from_file(file_path, Some(chunk_path.clone()))?;
let mut encrypted_chunks = Vec::new();
let mut data_map = None;
while let Ok((chunk, map)) = encryptor.next_encryption() {
Expand Down Expand Up @@ -68,7 +66,7 @@ fn test_stream_self_encryptor() -> Result<(), Error> {
}

let mut decryptor =
StreamSelfDecryptor::decrypt_to_file(Box::new(decrypted_file_path.clone()), &data_map)?;
StreamSelfDecryptor::decrypt_to_file(decrypted_file_path.clone(), &data_map)?;
for chunk in encrypted_chunks {
let _ = decryptor.next_encrypted(chunk)?;
}
Expand All @@ -82,7 +80,7 @@ fn test_stream_self_encryptor() -> Result<(), Error> {
// Use the flushed encrypted chunks to recover the file and verify with the original data
let mut flushed_encrypted_chunks = Vec::new();
for chunk_info in data_map.infos() {
let file_path = chunk_path.join(&hex::encode(chunk_info.dst_hash));
let file_path = chunk_path.join(hex::encode(chunk_info.dst_hash));
let mut chunk_file = File::open(file_path)?;
let mut chunk_data = Vec::new();
let _ = chunk_file.read_to_end(&mut chunk_data)?;
Expand Down
2 changes: 2 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

//! Tests for the self-encryption crate
// For explanation of lint checks, run `rustc -W help` or see
// https://github.com/maidsafe/QA/blob/master/Documentation/Rust%20Lint%20Checks.md
#![forbid(
Expand Down

0 comments on commit 6f30eba

Please sign in to comment.