Crates.io | rustfs-utils |
lib.rs | rustfs-utils |
version | 0.0.4 |
created_at | 2025-07-04 23:54:57.829535+00 |
updated_at | 2025-07-05 07:08:24.331549+00 |
description | Utility functions and data structures for RustFS, providing essential features like hashing, compression, and network utilities. |
homepage | https://rustfs.com |
repository | https://github.com/rustfs/rustfs |
max_upload_size | |
id | 1738641 |
size | 228,047 |
Essential utility functions and common tools for RustFS distributed object storage
๐ Documentation
ยท ๐ Bug Reports
ยท ๐ฌ Discussions
RustFS Utils is the utility library for the RustFS distributed object storage system. It provides a comprehensive collection of utility functions, helper tools, and common functionality used across all RustFS modules, including system operations, cryptographic utilities, compression, and cross-platform compatibility tools.
Note: This is a foundational submodule of RustFS that provides essential utility functions for the distributed object storage system. For the complete RustFS experience, please visit the main RustFS repository.
Add this to your Cargo.toml
:
[dependencies]
rustfs-utils = "0.0.3"
# Or with specific features
rustfs-utils = { version = "0.0.3", features = ["compression", "crypto", "network"] }
[dependencies]
rustfs-utils = { version = "0.0.3", features = ["full"] }
Available features:
compression
- Compression and decompression utilitiescrypto
- Cryptographic functions and utilitiesnetwork
- Network-related utilitiespath
- Advanced path manipulation toolssystem
- System monitoring and managementfull
- All features enableduse rustfs_utils::fs::{ensure_dir, atomic_write, safe_remove};
use rustfs_utils::path::{normalize_path, is_subdirectory};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Ensure directory exists
ensure_dir("/path/to/directory")?;
// Atomic file write
atomic_write("/path/to/file.txt", b"Hello, World!")?;
// Path normalization
let normalized = normalize_path("./some/../path/./file.txt");
println!("Normalized: {}", normalized.display());
// Check if path is subdirectory
if is_subdirectory("/safe/path", "/safe/path/subdir") {
println!("Path is safe");
}
Ok(())
}
use rustfs_utils::compress::{compress_data, decompress_data, Algorithm};
fn compression_example() -> Result<(), Box<dyn std::error::Error>> {
let data = b"This is some test data to compress";
// Compress with different algorithms
let gzip_compressed = compress_data(data, Algorithm::Gzip)?;
let zstd_compressed = compress_data(data, Algorithm::Zstd)?;
let lz4_compressed = compress_data(data, Algorithm::Lz4)?;
// Decompress
let decompressed = decompress_data(&gzip_compressed, Algorithm::Gzip)?;
assert_eq!(data, decompressed.as_slice());
println!("Original size: {}", data.len());
println!("Gzip compressed: {}", gzip_compressed.len());
println!("Zstd compressed: {}", zstd_compressed.len());
println!("LZ4 compressed: {}", lz4_compressed.len());
Ok(())
}
use rustfs_utils::crypto::{hash_data, random_bytes, generate_key};
use rustfs_utils::crypto::HashAlgorithm;
fn crypto_example() -> Result<(), Box<dyn std::error::Error>> {
let data = b"Important data to hash";
// Generate hashes
let md5_hash = hash_data(data, HashAlgorithm::MD5)?;
let sha256_hash = hash_data(data, HashAlgorithm::SHA256)?;
let xxhash = hash_data(data, HashAlgorithm::XXHash64)?;
println!("MD5: {}", hex::encode(md5_hash));
println!("SHA256: {}", hex::encode(sha256_hash));
println!("XXHash64: {}", hex::encode(xxhash));
// Generate secure random data
let random_data = random_bytes(32)?;
println!("Random data: {}", hex::encode(random_data));
// Generate cryptographic key
let key = generate_key(256)?; // 256-bit key
println!("Generated key: {}", hex::encode(key));
Ok(())
}
use rustfs_utils::sys::{get_system_info, monitor_resources, DiskUsage};
async fn system_monitoring_example() -> Result<(), Box<dyn std::error::Error>> {
// Get system information
let sys_info = get_system_info().await?;
println!("OS: {} {}", sys_info.os_name, sys_info.os_version);
println!("CPU: {} cores", sys_info.cpu_cores);
println!("Total Memory: {} GB", sys_info.total_memory / 1024 / 1024 / 1024);
// Monitor disk usage
let disk_usage = DiskUsage::for_path("/var/data")?;
println!("Disk space: {} / {} bytes", disk_usage.used, disk_usage.total);
println!("Available: {} bytes", disk_usage.available);
// Monitor resources
let resources = monitor_resources().await?;
println!("CPU Usage: {:.2}%", resources.cpu_percent);
println!("Memory Usage: {:.2}%", resources.memory_percent);
Ok(())
}
use rustfs_utils::net::{resolve_hostname, get_local_ip, is_port_available};
async fn network_example() -> Result<(), Box<dyn std::error::Error>> {
// DNS resolution
let addresses = resolve_hostname("example.com").await?;
for addr in addresses {
println!("Resolved address: {}", addr);
}
// Get local IP
let local_ip = get_local_ip().await?;
println!("Local IP: {}", local_ip);
// Check port availability
if is_port_available(8080).await? {
println!("Port 8080 is available");
} else {
println!("Port 8080 is in use");
}
Ok(())
}
use rustfs_utils::certs::{parse_certificate, validate_certificate_chain, CertificateInfo};
fn certificate_example() -> Result<(), Box<dyn std::error::Error>> {
let cert_pem = include_str!("../test_data/certificate.pem");
// Parse certificate
let cert_info = parse_certificate(cert_pem)?;
println!("Subject: {}", cert_info.subject);
println!("Issuer: {}", cert_info.issuer);
println!("Valid from: {}", cert_info.not_before);
println!("Valid until: {}", cert_info.not_after);
// Validate certificate chain
let ca_certs = vec![/* CA certificates */];
let is_valid = validate_certificate_chain(&cert_info, &ca_certs)?;
if is_valid {
println!("Certificate chain is valid");
} else {
println!("Certificate chain is invalid");
}
Ok(())
}
use rustfs_utils::encoding::{base64_encode, base64_decode, url_encode, url_decode};
fn encoding_example() -> Result<(), Box<dyn std::error::Error>> {
let data = b"Hello, World!";
// Base64 encoding
let encoded = base64_encode(data);
let decoded = base64_decode(&encoded)?;
assert_eq!(data, decoded.as_slice());
// URL encoding
let url = "https://example.com/path with spaces?param=value&other=data";
let encoded_url = url_encode(url);
let decoded_url = url_decode(&encoded_url)?;
assert_eq!(url, decoded_url);
println!("Base64 encoded: {}", encoded);
println!("URL encoded: {}", encoded_url);
Ok(())
}
Utils Architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Public API Layer โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ File System โ Compression โ Crypto โ Network โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ System Info โ Encoding โ Certs โ Path Utils โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Platform Abstraction Layer โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Operating System Integration โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Category | Features | Platform Support |
---|---|---|
File System | Atomic operations, path manipulation | All platforms |
Compression | Gzip, Zstd, LZ4, Brotli | All platforms |
Cryptography | Hashing, random generation, keys | All platforms |
System | Resource monitoring, process management | Linux, macOS, Windows |
Network | DNS, connectivity, interface detection | All platforms |
Run the test suite:
# Run all tests
cargo test
# Run tests for specific features
cargo test --features compression
cargo test --features crypto
cargo test --features network
# Run tests with all features
cargo test --features full
# Run benchmarks
cargo bench
The utils library is optimized for performance:
Operation | Performance | Notes |
---|---|---|
Path Normalization | ~50 ns | Uses efficient string operations |
Base64 Encoding | ~1.2 GB/s | SIMD-optimized implementation |
XXHash64 | ~15 GB/s | Hardware-accelerated when available |
File Copy | ~2 GB/s | Platform-optimized copy operations |
This module is part of the RustFS ecosystem:
For comprehensive documentation, visit:
We welcome contributions! Please see our Contributing Guide for details.
Licensed under the Apache License, Version 2.0. See LICENSE for details.
RustFS is a trademark of RustFS, Inc.
All other trademarks are the property of their respective owners.
Made with ๐ง by the RustFS Team