This commit is contained in:
AB
2020-06-05 02:29:07 +03:00
parent 6e6bab7e55
commit 28c6fa93b6
3 changed files with 26 additions and 32 deletions

View File

@ -12,8 +12,8 @@ use polyfuse::{
}; };
use slab::Slab; use slab::Slab;
use crate::http::HTTP;
use std::path::{PathBuf, Path}; use std::path::{Path, PathBuf};
use std::{ use std::{
collections::hash_map::{Entry, HashMap}, collections::hash_map::{Entry, HashMap},
ffi::{OsStr, OsString}, ffi::{OsStr, OsString},
@ -24,7 +24,6 @@ use std::{
}; };
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing_futures::Instrument; use tracing_futures::Instrument;
use crate::http::HTTP;
type Ino = u64; type Ino = u64;
@ -174,7 +173,11 @@ impl MemFS {
}); });
Self { Self {
http: HTTP::new(cfg.server.clone(), cfg.username.clone(), cfg.password.clone()), http: HTTP::new(
cfg.server.clone(),
cfg.username.clone(),
cfg.password.clone(),
),
inodes: Mutex::new(inodes), inodes: Mutex::new(inodes),
f_ino_map: Mutex::new(Vec::new()), f_ino_map: Mutex::new(Vec::new()),
dir_handles: Mutex::default(), dir_handles: Mutex::default(),
@ -426,7 +429,6 @@ impl MemFS {
let mut file_path = self.full_path(op.parent()).await.unwrap(); let mut file_path = self.full_path(op.parent()).await.unwrap();
file_path.push(op.name()); file_path.push(op.name());
self.fetch_remote(file_path, f_inode).await; self.fetch_remote(file_path, f_inode).await;
} }
_ => { _ => {
drop(inode); drop(inode);
@ -613,7 +615,7 @@ impl MemFS {
warn!("inode_mutex {:?}", inode_mutex); warn!("inode_mutex {:?}", inode_mutex);
Some((uri, parent_ino)) Some((uri, parent_ino))
} }
INodeKind::Symlink(_) => Some((uri, parent_ino)) INodeKind::Symlink(_) => Some((uri, parent_ino)),
}; };
ret ret
} }
@ -868,7 +870,6 @@ impl MemFS {
} }
async fn do_read(&self, op: &op::Read<'_>) -> io::Result<impl Reply + Debug> { async fn do_read(&self, op: &op::Read<'_>) -> io::Result<impl Reply + Debug> {
let full_path_mutex = self.f_ino_map.lock().await; let full_path_mutex = self.f_ino_map.lock().await;
let mut counter = 0; let mut counter = 0;
let full_path = loop { let full_path = loop {

View File

@ -1,5 +1,7 @@
extern crate base64; extern crate base64;
use chrono::DateTime;
use reqwest::{header, Client, Error};
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
path::PathBuf, path::PathBuf,
@ -7,14 +9,8 @@ use std::{
thread::sleep, thread::sleep,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use chrono::{DateTime};
use reqwest::{Client, Error, header};
static APP_USER_AGENT: &str = concat!( static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct HTTP { pub struct HTTP {
@ -38,7 +34,7 @@ impl RemoteEntry {
} }
impl HTTP { impl HTTP {
pub fn new(server: String, username: Option<String>, password: Option<String>, ) -> Self { pub fn new(server: String, username: Option<String>, password: Option<String>) -> Self {
let mut headers = header::HeaderMap::new(); let mut headers = header::HeaderMap::new();
match username { match username {
Some(username) => { Some(username) => {
@ -47,19 +43,19 @@ impl HTTP {
_buf.push_str(format!("{}:{}", username, password.as_ref().unwrap()).as_str()); _buf.push_str(format!("{}:{}", username, password.as_ref().unwrap()).as_str());
let creds = base64::encode(_buf); let creds = base64::encode(_buf);
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_str(format!("Basic {}", creds).as_str()).unwrap()); headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(format!("Basic {}", creds).as_str()).unwrap(),
);
} }
None => {}, None => {}
}; };
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.user_agent(APP_USER_AGENT) .user_agent(APP_USER_AGENT)
.default_headers(headers) .default_headers(headers)
.build().unwrap(); .build()
Self { .unwrap();
client, Self { client, server }
server
}
} }
pub async fn list(&self, path: PathBuf) -> Result<Vec<RemoteEntry>, Error> { pub async fn list(&self, path: PathBuf) -> Result<Vec<RemoteEntry>, Error> {
debug!("Fetching path '{}/{}'", self.server, path.display()); debug!("Fetching path '{}/{}'", self.server, path.display());
@ -77,9 +73,11 @@ impl HTTP {
pub async fn read(&self, path: PathBuf, size: usize, offset: usize) -> Result<Vec<u8>, Error> { pub async fn read(&self, path: PathBuf, size: usize, offset: usize) -> Result<Vec<u8>, Error> {
debug!("Reading path '{}/{}'", self.server, path.display()); debug!("Reading path '{}/{}'", self.server, path.display());
let mut headers = header::HeaderMap::new(); let mut headers = header::HeaderMap::new();
let range = format!("bytes={}-{}", offset, {offset+size}); let range = format!("bytes={}-{}", offset, { offset + size });
headers.insert(header::RANGE, header::HeaderValue::from_str(range.as_str()).unwrap()); headers.insert(
header::RANGE,
header::HeaderValue::from_str(range.as_str()).unwrap(),
);
let mut client = &self.client; let mut client = &self.client;
let resp = client let resp = client
@ -93,4 +91,3 @@ impl HTTP {
Ok(resp.to_vec()) Ok(resp.to_vec())
} }
} }

View File

@ -2,7 +2,7 @@ use std::path::PathBuf;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
use env_logger::Env; use env_logger::Env;
use std::{process}; use std::process;
mod config; mod config;
mod filesystem; mod filesystem;
@ -29,7 +29,3 @@ async fn main() -> Result<(), std::io::Error> {
Ok(()) Ok(())
} }
/*
Mkdir { parent: 1, name: "123", mode: 493, umask: 18 }
Mknod { parent: 1, name: "123233", mode: 33188, rdev: 0, umask: 18 }
*/