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

refactor!: remove usage of Box<PathBuf> #386

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
6 changes: 1 addition & 5 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
unused_qualifications,
variant_size_differences
)]
#![allow(
box_pointers,
missing_copy_implementations,
missing_debug_implementations
)]
#![allow(missing_copy_implementations, missing_debug_implementations)]

use criterion::{BatchSize, Bencher, Criterion};
use self_encryption::{decrypt_full_set, encrypt, test_helpers::random_bytes};
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
20 changes: 8 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
unused_results
)]
#![allow(
box_pointers,
missing_copy_implementations,
missing_debug_implementations,
variant_size_differences,
Expand Down Expand Up @@ -148,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 @@ -158,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 @@ -261,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 @@ -274,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 @@ -337,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 @@ -367,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 @@ -385,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
3 changes: 2 additions & 1 deletion 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 Expand Up @@ -43,7 +45,6 @@
unused_results
)]
#![allow(
box_pointers,
missing_copy_implementations,
missing_debug_implementations,
variant_size_differences
Expand Down
Loading