Skip to content

Commit

Permalink
Fixed fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Roest committed Aug 3, 2020
1 parent 796e48b commit 532088e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 80 deletions.
36 changes: 20 additions & 16 deletions src/dspfs/api.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::user::PublicUser;
use crate::fs::hash::Hash;
use crate::user::PublicUser;

use std::path::Path;
use uuid::Uuid;
use anyhow::Result;
use crate::fs::file::SimpleFile;
use anyhow::Result;
use async_trait::async_trait;
use std::collections::{HashSet, BTreeSet};
use std::time::SystemTime;
use std::fs::DirEntry;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashSet};
use std::ffi::OsString;
use serde::{Serialize,Deserialize};
use std::fs::DirEntry;
use std::path::Path;
use std::time::SystemTime;
use uuid::Uuid;

#[derive(Serialize,Deserialize,Debug,Clone, Eq, PartialEq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
pub struct LocalFile {
name: OsString,
modtime: SystemTime,
Expand All @@ -37,7 +37,7 @@ impl LocalFile {
pub trait Api {
/// Equivalent of `git init`. Creates a new group with only you in it.
async fn init_group(&self, path: &Path) -> Result<Uuid>;

/// Equivalent of `git add`. Adds a file to the group and makes it visible for others in the group.
async fn add_file(&self, guuid: Uuid, path: &Path) -> Result<SimpleFile>;

Expand All @@ -54,13 +54,20 @@ pub trait Api {
async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result<HashSet<LocalFile>>;

/// gets a certain level of filetree as seen by another user
async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>>;
async fn get_files(
&self,
guuid: Uuid,
user: &PublicUser,
path: &Path,
) -> Result<HashSet<SimpleFile>>;

/// requests a file from other users in the group
async fn request_file(&self, hash: Hash, to: &Path) -> Result<()>;

/// Lists current download/uploads.
async fn status(&self) {todo!()}
async fn status(&self) {
todo!()
}

/// Refreshes internal state.
/// may do any of the following things:
Expand All @@ -69,12 +76,9 @@ pub trait Api {
/// * Check for new files from other users.
async fn refresh(&self);


// TODO:
async fn add_folder(&self, guuid: Uuid, path: &Path) -> Result<()> {
async fn add_folder(&self, _guuid: Uuid, path: &Path) -> Result<()> {
assert!(path.is_dir());
todo!()
}
}


101 changes: 64 additions & 37 deletions src/dspfs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
use crate::dspfs::builder::DspfsBuilder;
use crate::dspfs::client::Client;
use crate::dspfs::server::{Server, ServerHandle};
use crate::fs::file::File;
use crate::fs::file::SimpleFile;
use crate::fs::group::StoredGroup;
use crate::fs::hash::Hash;
use crate::global_store::{SharedStore, Store};
use crate::user::{PrivateUser, PublicUser};
use anyhow::{Result, Context};
use anyhow::{Context, Result};
use api::Api;
use api::LocalFile;
use async_trait::async_trait;
use log::*;
use std::collections::{HashMap, HashSet, BTreeSet};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fs;
use std::mem;
use std::path::Path;
use uuid::Uuid;
use crate::fs::file::SimpleFile;
use crate::fs::file::File;
use crate::fs::hash::Hash;
use async_trait::async_trait;
use api::LocalFile;
use api::Api;

pub mod api;
pub mod builder;
pub mod client;
pub mod server;
pub mod api;

pub struct Dspfs<S: Store + 'static> {
pub(self) store: SharedStore<S>,
Expand Down Expand Up @@ -63,7 +63,6 @@ impl<S: Store> Dspfs<S> {
}
}


#[async_trait]
impl<S: Store> Api for Dspfs<S> {
async fn init_group(&self, path: &Path) -> Result<Uuid> {
Expand All @@ -72,7 +71,7 @@ impl<S: Store> Api for Dspfs<S> {
if !group.dspfs_folder().exists() {
// New folder
fs::create_dir_all(group.dspfs_folder())?;
let uuid = group.uuid.clone();
let uuid = group.uuid;
self.store.write().await.add_group(group)?;

Ok(uuid)
Expand All @@ -82,81 +81,109 @@ impl<S: Store> Api for Dspfs<S> {
}

async fn add_file(&self, guuid: Uuid, path: &Path) -> Result<SimpleFile> {
let mut group = self.store.read().await.get_group(guuid)?
.context("There is no group with this uuid.")?.reload(self.store.clone())?;
let mut group = self
.store
.read()
.await
.get_group(guuid)?
.context("There is no group with this uuid.")?
.reload(self.store.clone())?;

let us = self.store.read().await.get_self_user()?.context("")?;

let mut location = group.dspfs_root().to_path_buf();
location.push(path);

if !location.is_file() {
return Err(anyhow::anyhow!("Path does not point to a file"))
return Err(anyhow::anyhow!("Path does not point to a file"));
}

let file = File::new(location).await
.context("Indexing file failed")?;
let file = File::new(location).await.context("Indexing file failed")?;

let simple_file = file.simplify();

group.add_file(&us, file).await.context("Adding file to group failed")?;
group
.add_file(&us, file)
.await
.context("Adding file to group failed")?;

Ok(simple_file)
}

async fn join_group(&self, guuid: Uuid, bootstrap_user: &PublicUser) -> Result<()> {
async fn join_group(&self, _guuid: Uuid, _bootstrap_user: &PublicUser) -> Result<()> {
unimplemented!("procrastination is life")
}

async fn list_files(&self, guuid: Uuid) -> Result<HashSet<SimpleFile>> {
let group = self.store.read().await.get_group(guuid)?
.context("There is no group with this uuid.")?.reload(self.store.clone())?;
let group = self
.store
.read()
.await
.get_group(guuid)?
.context("There is no group with this uuid.")?
.reload(self.store.clone())?;

let f = group.list_files().await?
.into_iter()
.map(|f| f.simplify());
let f = group.list_files().await?.into_iter().map(|f| f.simplify());

Ok(f.collect())
}

async fn get_users(&self, guuid: Uuid) -> Result<BTreeSet<PublicUser>> {
let group = self.store.read().await.get_group(guuid)?
let group = self
.store
.read()
.await
.get_group(guuid)?
.context("There is no group with this uuid.")?;

Ok(group.users)
}

async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result<HashSet<LocalFile>> {
let group = self.store.read().await.get_group(guuid)?
let group = self
.store
.read()
.await
.get_group(guuid)?
.context("There is no group with this uuid.")?;

let mut location = group.dspfs_root().to_path_buf();
location.push(path);

let set = location.read_dir().context("Reading directory failed")?
.map(|i| i.map(|direntry| {
LocalFile::from_direntry(direntry)
})).flatten().collect::<Result<_>>()?;
let set = location
.read_dir()
.context("Reading directory failed")?
.map(|i| i.map(LocalFile::from_direntry))
.flatten()
.collect::<Result<_>>()?;

Ok(set)
}

async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>> {
let group = self.store.read().await.get_group(guuid)?
.context("There is no group with this uuid.")?.reload(self.store.clone())?;

let files = group.get_files_from_user(user, path).await;
async fn get_files(
&self,
guuid: Uuid,
user: &PublicUser,
path: &Path,
) -> Result<HashSet<SimpleFile>> {
let group = self
.store
.read()
.await
.get_group(guuid)?
.context("There is no group with this uuid.")?
.reload(self.store.clone())?;

let _files = group.get_files_from_user(user, path).await;

todo!()
}

async fn request_file(&self, hash: Hash, to: &Path) -> Result<()> {
async fn request_file(&self, _hash: Hash, _to: &Path) -> Result<()> {
unimplemented!()
}

async fn refresh(&self) {
unimplemented!()
}
}


3 changes: 1 addition & 2 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ impl File {
}
}


/// TODO: maybe avoid cloning everything here
pub fn simplify(&self) -> SimpleFile {
SimpleFile {
path: self.path.clone(),
hash: self.hash.clone(),
users: self.users.clone(),
file_size: self.file_size.clone(),
file_size: self.file_size,
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/fs/group/heed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ impl GroupStore for HeedGroupStore {
fn list_files(&self) -> Result<Vec<File>> {
let rtxn = self.env.read_txn()?;

let files = self.files.iter(&rtxn)?
let files = self
.files
.iter(&rtxn)?
.map(|i| i.map(|(_hash, file)| file))
.flatten()
.collect();
Expand All @@ -88,8 +90,7 @@ impl GroupStore for HeedGroupStore {
.filetrees
.get(&rtxn, user)
.context("error getting filetree from db")?
.context("no filetree found for this user")?
)
.context("no filetree found for this user")?)
}

fn delete_file(&mut self, user: &PublicUser, file: &File) -> Result<()> {
Expand Down
34 changes: 16 additions & 18 deletions src/fs/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ mod heed;
mod store;

use crate::fs::file::{File, SimpleFile};
use crate::fs::filetree::FileTree;
use crate::fs::group::heed::HeedGroupStore;
use crate::fs::group::store::{GroupStore, SharedGroupStore};
use crate::fs::hash::Hash;
use crate::global_store::{SharedStore, Store};
use crate::user::PublicUser;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashSet};
use std::io::SeekFrom;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand All @@ -17,8 +19,6 @@ use tokio::fs;
use tokio::io::AsyncReadExt;
use tokio::sync::RwLock;
use uuid::Uuid;
use std::collections::{BTreeSet, HashSet};
use crate::fs::filetree::FileTree;

/// A *StoredGroup* is a reduced version of a [Group], which can safely be stored in a database.
/// For documentation on what a DSPFS *Group* is, refer to the documentation of [Group].
Expand Down Expand Up @@ -221,30 +221,28 @@ impl<S: Store> Group<S> {

/// Returns all files in the directory pointed to by path, from a certain user.
/// This function only returns the files directly in that folder, and not recursively.
pub async fn get_files_from_user(&self, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>> {
pub async fn get_files_from_user(
&self,
user: &PublicUser,
path: &Path,
) -> Result<HashSet<SimpleFile>> {
let filetree = self.group_store.read().await.get_filetree(user)?;

let dir = filetree.find(path)
.context("no file exists at this path")?;
let dir = filetree.find(path).context("no file exists at this path")?;

Ok(match dir {
FileTree::Leaf { file, .. } => {
let mut hs = HashSet::new();
hs.insert(file.simplify());
hs
},
FileTree::Node { children, .. } => {
children.iter()
.map(|node| match node {
FileTree::Leaf { file, .. } => {
file.simplify()
},
FileTree::Node { .. } => {
todo!()
},
})
.collect()
},
}
FileTree::Node { children, .. } => children
.iter()
.map(|node| match node {
FileTree::Leaf { file, .. } => file.simplify(),
FileTree::Node { .. } => todo!(),
})
.collect(),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fs/group/store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::fs::file::File;
use crate::fs::filetree::FileTree;
use crate::fs::hash::Hash;
use crate::user::PublicUser;
use anyhow::Result;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::fs::filetree::FileTree;

/// Thread safe group store
pub type SharedGroupStore = Arc<RwLock<Box<dyn GroupStore>>>;
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,5 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Run program



Ok(())
}
Loading

0 comments on commit 532088e

Please sign in to comment.