Skip to content

Commit

Permalink
fix clippy errors and warings
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed May 9, 2024
1 parent 94e371e commit 1a45127
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 493 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ You can specify the log level adding the `--log-level` argument to the command l
values: `TRACE`, `DEBUG`, `INFO` (default), `WARN`, `ERROR`.

```bash
--log-level LEVEL
rencfs --log-level LEVEL ...
```

## Start it in docker
Expand Down Expand Up @@ -229,6 +229,14 @@ cargo build --release
cargo run -- --mount-point MOUNT_POINT --data-dir DATA_DIR
```
## Developing inside a Container
See here how to configure for [VsCode](https://code.visualstudio.com/docs/devcontainers/containers)\
And here for [RustRover](https://www.jetbrains.com/help/rust/connect-to-devcontainer.html)
You can use the `.devcontainer` directory from the project to start a container with all the necessary tools to build
and run the app.
# Future
- Plan is to implement it also on macOS and Windows
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed-duplicate-crates = ["syn", "socket2", "windows-sys", "windows-targets", "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", 'windows_x86_64_gnu', "windows_x86_64_gnullvm", "windows_x86_64_msvc", "memoffset", "nix", "parking_lot", "parking_lot_core", "polling", "redox_syscall", "regex-automata", "regex-syntax", "rustix", "sha2", "bitflags", "block-buffer", "crypto-common", "digest", "event-listener", "event-listener-strategy", "fastrand", "futures-lite", "async-io", "async-lock", "heck", "linux-raw-sys"]
10 changes: 5 additions & 5 deletions examples/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use std::sync::Arc;
use secrecy::SecretString;

use rencfs::crypto;
use rencfs::crypto::writer::{CryptoWriter, FileCryptoWriterCallback};
use rencfs::crypto::writer::FileCryptoWriterCallback;
use rencfs::crypto::Cipher;

fn main() -> anyhow::Result<()> {
let password = SecretString::new("password".to_string());
let salt = crypto::hash_secret_string(&password);
let cipher = Cipher::ChaCha20;
let key = Arc::new(crypto::derive_key(&password, &cipher, salt).unwrap());
let key = Arc::new(crypto::derive_key(&password, cipher, salt).unwrap());

let mut args = args();
let path_in = args.next().expect("path_in is missing");
Expand All @@ -39,8 +39,8 @@ fn main() -> anyhow::Result<()> {
}
let mut file = File::open(path_in.clone()).unwrap();
let mut writer = crypto::create_file_writer(
Path::new(&path_out).to_path_buf(),
Path::new(&"/tmp").to_path_buf(),
&Path::new(&path_out).to_path_buf(),
&Path::new(&"/tmp").to_path_buf(),
cipher,
key.clone(),
42_u64,
Expand All @@ -51,7 +51,7 @@ fn main() -> anyhow::Result<()> {
writer.finish().unwrap();

let mut reader = crypto::create_file_reader(
Path::new(&path_out).to_path_buf(),
&Path::new(&path_out).to_path_buf(),
cipher,
key.clone(),
42_u64,
Expand Down
41 changes: 26 additions & 15 deletions src/arc_hashmap.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

type Value<V> = (Arc<V>, Arc<AtomicUsize>);
pub struct ArcHashMap<K, V>
where
K: Eq + Hash,
{
map: Mutex<HashMap<K, (Arc<V>, Arc<AtomicUsize>)>>,
map: Mutex<HashMap<K, Value<V>>>,
}

pub struct Guard<V> {
Expand All @@ -27,28 +28,31 @@ impl<V> Deref for Guard<V> {
type Target = V;

fn deref(&self) -> &Self::Target {
&*self.val
&self.val
}
}

impl<K: Eq + Hash, V> ArcHashMap<K, V> {
pub fn new() -> Self {
ArcHashMap {
impl<K: Eq + Hash, V> Default for ArcHashMap<K, V> {
fn default() -> Self {
Self {
map: Mutex::new(HashMap::new()),
}
}
}

impl<K: Eq + Hash, V> ArcHashMap<K, V> {
pub fn insert(&self, key: K, value: V) -> Guard<V> {
self.purge();
self.get_or_insert_with(key, || value)
}

pub fn get<'a>(&self, key: &K) -> Option<Guard<V>> {
#[allow(clippy::missing_panics_doc)]
pub fn get(&self, key: &K) -> Option<Guard<V>> {
self.purge();
self.get_internal(self.map.lock().unwrap().get(key))
Self::get_internal(self.map.lock().expect("cannot obtain lock").get(key))
}

pub fn get_internal<'a>(&self, v: Option<&(Arc<V>, Arc<AtomicUsize>)>) -> Option<Guard<V>> {
fn get_internal(v: Option<&Value<V>>) -> Option<Guard<V>> {
if let Some((v, rc)) = v {
rc.fetch_add(1, Ordering::SeqCst);
return Some(Guard {
Expand All @@ -59,25 +63,32 @@ impl<K: Eq + Hash, V> ArcHashMap<K, V> {
None
}

#[allow(clippy::missing_panics_doc)]
pub fn get_or_insert_with<F>(&self, key: K, f: F) -> Guard<V>
where
F: FnOnce() -> V,
{
self.purge();
let mut map = self.map.lock().unwrap();
let v = map
.entry(key)
.or_insert_with(|| (Arc::new(f()), Arc::new(AtomicUsize::new(0))));
self.get_internal(Some(v)).unwrap()
let mut map = self.map.lock().expect("cannot obtain lock");
Self::get_internal(Some(
map.entry(key)
.or_insert_with(|| (Arc::new(f()), Arc::new(AtomicUsize::new(0)))),
))
.unwrap()
}

fn purge(&self) {
let mut map = self.map.lock().unwrap();
map.retain(|_, v| v.1.load(Ordering::SeqCst) > 0);
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

#[allow(clippy::missing_panics_doc)]
pub fn len(&self) -> usize {
self.purge();
self.map.lock().unwrap().len()
self.map.lock().expect("cannot obtain lock").len()
}
}
Loading

0 comments on commit 1a45127

Please sign in to comment.