Skip to content

Commit

Permalink
feat: replace crc32c with crc-rc (#934)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Jan 8, 2025
1 parent 6d9cdbf commit f4c991c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
35 changes: 25 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "0.2.2"
version = "0.2.3"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand All @@ -22,13 +22,13 @@ readme = "README.md"
edition = "2021"

[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.2.2" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.2.2" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.2.2" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.2.2" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.2.2" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.2.2" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.2.2" }
dragonfly-client = { path = "dragonfly-client", version = "0.2.3" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.2.3" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.2.3" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.2.3" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.2.3" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.2.3" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.2.3" }
thiserror = "1.0"
dragonfly-api = "=2.1.3"
reqwest = { version = "0.12.4", features = [
Expand Down Expand Up @@ -61,7 +61,7 @@ rustls-pki-types = "1.10.1"
rustls-pemfile = "2.2.0"
sha2 = "0.10"
blake3 = "1.5.5"
crc32c = "0.6.8"
crc = "3.2.1"
uuid = { version = "1.11", features = ["v4"] }
hex = "0.4"
rocksdb = "0.22.0"
Expand Down
18 changes: 0 additions & 18 deletions dragonfly-client-config/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ fn main() {
let target = env::var("TARGET").unwrap_or_default();
println!("cargo:rustc-env=BUILD_PLATFORM={}", target);

// Get the RUSTFLAGS environment variable.
let mut rustflags = env::var("RUSTFLAGS").unwrap_or_default();

// Set the environment variables for the RUSTFLAGS.
let additional_rustflags = if target.contains("x86_64") {
"-C target-feature=+sse4.2"
} else {
""
};

if !additional_rustflags.is_empty() {
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str(additional_rustflags);
println!("cargo:rustc-env=RUSTFLAGS={}", rustflags);
}

// Set the environment variables for the build time.
if let Ok(build_time) = SystemTime::now().duration_since(UNIX_EPOCH) {
println!("cargo:rustc-env=BUILD_TIMESTAMP={}", build_time.as_secs());
Expand Down
2 changes: 1 addition & 1 deletion dragonfly-client-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ prost-wkt-types.workspace = true
tokio.workspace = true
tokio-util.workspace = true
sha2.workspace = true
crc32c.workspace = true
crc.workspace = true
base16ct.workspace = true
num_cpus = "1.0"
bincode = "1.3.3"
Expand Down
19 changes: 11 additions & 8 deletions dragonfly-client-storage/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

use crc::*;
use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::Result;
Expand Down Expand Up @@ -335,8 +336,9 @@ impl Content {
error!("seek {:?} failed: {}", task_path, err);
})?;

// Copy the piece to the file while updating the CRC32C value.
let mut crc: u32 = 0;
// Copy the piece to the file while updating the CRC32 value.
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
let mut length = 0;
let mut buffer = vec![0; self.config.storage.write_buffer_size];
let mut writer = BufWriter::with_capacity(self.config.storage.write_buffer_size, f);
Expand All @@ -348,7 +350,7 @@ impl Content {
break;
}

crc = crc32c::crc32c_append(crc, &buffer[..n]);
digest.update(&buffer[..n]);
writer.write_all(&buffer[..n]).await?;
length += n as u64;
}
Expand All @@ -357,7 +359,7 @@ impl Content {
// Calculate the hash of the piece.
Ok(WritePieceResponse {
length,
hash: crc.to_string(),
hash: digest.finalize().to_string(),
})
}

Expand Down Expand Up @@ -455,8 +457,9 @@ impl Content {
error!("open {:?} failed: {}", task_path, err);
})?;

// Copy the content to the file while updating the CRC32C value.
let mut crc: u32 = 0;
// Copy the content to the file while updating the CRC32 value.
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
let mut length = 0;
let mut buffer = vec![0; self.config.storage.write_buffer_size];
let mut writer = BufWriter::with_capacity(self.config.storage.write_buffer_size, to_f);
Expand All @@ -468,15 +471,15 @@ impl Content {
break;
}

crc = crc32c::crc32c_append(crc, &buffer[..n]);
digest.update(&buffer[..n]);
writer.write_all(&buffer[..n]).await?;
length += n as u64;
}
writer.flush().await?;

Ok(WritePersistentCacheTaskResponse {
length,
hash: crc.to_string(),
hash: digest.finalize().to_string(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion dragonfly-client-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uuid.workspace = true
hex.workspace = true
openssl.workspace = true
blake3.workspace = true
crc32c.workspace = true
crc.workspace = true
base16ct.workspace = true
base64 = "0.22.1"
crc32fast = "1.4.2"
Expand Down
8 changes: 5 additions & 3 deletions dragonfly-client-util/src/digest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

use crc::*;
use dragonfly_client_core::Result as ClientResult;
use sha2::Digest as Sha2Digest;
use std::fmt;
Expand Down Expand Up @@ -135,17 +136,18 @@ pub fn calculate_file_hash(algorithm: Algorithm, path: &Path) -> ClientResult<Di
let mut reader = std::io::BufReader::new(f);
match algorithm {
Algorithm::Crc32 => {
let mut crc: u32 = 0;
let mut buffer = [0; 4096];
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
loop {
let n = reader.read(&mut buffer)?;
if n == 0 {
break;
}
crc = crc32c::crc32c_append(crc, &buffer[..n]);
digest.update(&buffer[..n]);
}

Ok(Digest::new(algorithm, crc.to_string()))
Ok(Digest::new(algorithm, digest.finalize().to_string()))
}
Algorithm::Blake3 => {
let mut hasher = blake3::Hasher::new();
Expand Down

0 comments on commit f4c991c

Please sign in to comment.