2020-04-19 02:43:38 +03:00
|
|
|
extern crate base64;
|
2020-04-15 01:55:02 +03:00
|
|
|
extern crate fuse;
|
|
|
|
extern crate libc;
|
|
|
|
extern crate time;
|
2020-04-19 02:43:38 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate chrono;
|
2020-04-15 01:55:02 +03:00
|
|
|
|
2020-04-20 02:57:29 +03:00
|
|
|
|
|
|
|
use time::Timespec;
|
2020-04-15 01:55:02 +03:00
|
|
|
use percent_encoding::percent_decode_str;
|
|
|
|
use serde::Deserialize;
|
2020-04-20 02:57:29 +03:00
|
|
|
use libc::{EIO, ENOENT};
|
|
|
|
use reqwest::{blocking::Client, header::CONTENT_LENGTH};
|
|
|
|
use size_format::SizeFormatterBinary;
|
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, HashMap, HashSet},
|
|
|
|
env,
|
|
|
|
ffi::OsStr,
|
|
|
|
fmt,
|
|
|
|
path::Path,
|
|
|
|
process,
|
2020-04-25 01:11:18 +03:00
|
|
|
thread::sleep,
|
|
|
|
time::Duration,
|
2020-04-20 02:57:29 +03:00
|
|
|
};
|
|
|
|
use fuse::{
|
|
|
|
FileAttr, FileType, Filesystem, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, Request,
|
|
|
|
};
|
|
|
|
|
2020-04-15 01:55:02 +03:00
|
|
|
|
2020-04-19 02:43:38 +03:00
|
|
|
struct Metrics {
|
|
|
|
http_requests: u64,
|
2020-04-20 02:57:29 +03:00
|
|
|
connect_errors: u64,
|
2020-04-19 02:43:38 +03:00
|
|
|
ingress: u64,
|
|
|
|
hit_len_cache: u64,
|
|
|
|
hit_data_cache: u64,
|
|
|
|
miss_len_cache: u64,
|
|
|
|
miss_data_cache: u64,
|
|
|
|
server_addr: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Metrics {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f,
|
2020-04-20 02:57:29 +03:00
|
|
|
"http_requests: {}\nconnect_errors: {}\ningress: {}\nhit_len_cache: {}\nhit_data_cache: {}\nmiss_len_cache: {}\nmiss_data_cache: {}\nserver_addr: {}\n",
|
2020-04-19 02:43:38 +03:00
|
|
|
self.http_requests,
|
2020-04-20 02:57:29 +03:00
|
|
|
self.connect_errors,
|
2020-04-19 02:43:38 +03:00
|
|
|
self.ingress,
|
|
|
|
self.hit_len_cache,
|
|
|
|
self.hit_data_cache,
|
|
|
|
self.miss_len_cache,
|
|
|
|
self.miss_data_cache,
|
|
|
|
self.server_addr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 02:57:29 +03:00
|
|
|
static mut METRICS: Metrics = Metrics {
|
2020-04-19 02:43:38 +03:00
|
|
|
http_requests: 0,
|
2020-04-20 02:57:29 +03:00
|
|
|
connect_errors: 0,
|
2020-04-19 02:43:38 +03:00
|
|
|
ingress: 0,
|
|
|
|
hit_len_cache: 0,
|
|
|
|
hit_data_cache: 0,
|
|
|
|
miss_len_cache: 0,
|
|
|
|
miss_data_cache: 0,
|
|
|
|
server_addr: String::new(),
|
|
|
|
};
|
|
|
|
|
2020-04-15 01:55:02 +03:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
pub struct Track {
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub artist: Option<String>,
|
|
|
|
pub album: Option<String>,
|
|
|
|
pub genre: Option<String>,
|
|
|
|
pub year: Option<i32>,
|
|
|
|
pub format: Option<String>,
|
|
|
|
pub filetype: Option<String>,
|
|
|
|
pub path: Option<String>,
|
2020-04-23 00:20:26 +03:00
|
|
|
pub size: Option<i64>,
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-19 02:43:38 +03:00
|
|
|
const CACHE_HEAD: i64 = 768 * 1024;
|
|
|
|
const MAX_CACHE_SIZE: i64 = 10; // Count
|
2020-04-20 02:57:29 +03:00
|
|
|
static mut HTTP_AUTH: String = String::new();
|
2020-04-15 01:55:02 +03:00
|
|
|
|
|
|
|
fn get_basename(path: Option<&String>) -> Option<String> {
|
|
|
|
let base = match percent_decode_str(path.unwrap().as_str()).decode_utf8() {
|
|
|
|
Ok(path) => {
|
|
|
|
let remote_name = path.into_owned();
|
|
|
|
let basename = Path::new(&remote_name).file_name();
|
|
|
|
match basename {
|
|
|
|
Some(name) => Some(name.to_os_string().into_string().unwrap()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
|
|
|
};
|
|
|
|
base
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
2020-04-17 18:20:20 +03:00
|
|
|
async fn get_tracks(server: &String) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
|
2020-04-19 02:43:38 +03:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
unsafe {
|
|
|
|
let resp = client
|
|
|
|
.get(format!("{}/songs", server).as_str())
|
2020-04-20 02:57:29 +03:00
|
|
|
.header("Authorization", format!("Basic {}", HTTP_AUTH))
|
2020-04-19 02:43:38 +03:00
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json::<Vec<Track>>()
|
|
|
|
.await?;
|
|
|
|
info!("Found {} tracks.", resp.len());
|
|
|
|
Ok(resp)
|
|
|
|
}
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
struct JsonFilesystem {
|
2020-04-17 18:20:20 +03:00
|
|
|
server: String,
|
2020-04-15 01:55:02 +03:00
|
|
|
tree: Vec<Track>,
|
|
|
|
attrs: BTreeMap<u64, FileAttr>,
|
|
|
|
inodes: BTreeMap<String, u64>,
|
2020-04-19 02:43:38 +03:00
|
|
|
buffer_head_index: HashSet<u64>,
|
|
|
|
buffer_head_data: HashMap<u64, Vec<u8>>,
|
2020-04-17 03:13:54 +03:00
|
|
|
buffer_length: BTreeMap<String, i64>,
|
2020-04-19 02:43:38 +03:00
|
|
|
metrics_inode: u64,
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
impl JsonFilesystem {
|
2020-04-17 18:20:20 +03:00
|
|
|
fn new(tree: &Vec<Track>, server: String) -> JsonFilesystem {
|
2020-04-15 01:55:02 +03:00
|
|
|
let mut attrs = BTreeMap::new();
|
|
|
|
let mut inodes = BTreeMap::new();
|
|
|
|
let ts = time::now().to_timespec();
|
2020-04-19 02:43:38 +03:00
|
|
|
let mut total_size: i64 = 0;
|
2020-04-15 01:55:02 +03:00
|
|
|
let attr = FileAttr {
|
|
|
|
ino: 1,
|
|
|
|
size: 0,
|
|
|
|
blocks: 0,
|
|
|
|
atime: ts,
|
|
|
|
mtime: ts,
|
|
|
|
ctime: ts,
|
|
|
|
crtime: ts,
|
|
|
|
kind: FileType::Directory,
|
|
|
|
perm: 0o755,
|
|
|
|
nlink: 0,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
rdev: 0,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
attrs.insert(1, attr);
|
|
|
|
inodes.insert("/".to_string(), 1);
|
|
|
|
for (i, track) in tree.iter().enumerate() {
|
|
|
|
let basename = get_basename(track.path.as_ref()).unwrap().to_string();
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!(
|
|
|
|
"Added inode: {} - {} [{}]",
|
|
|
|
i + 2,
|
|
|
|
basename,
|
2020-04-23 00:20:26 +03:00
|
|
|
track.size.unwrap()
|
2020-04-19 02:43:38 +03:00
|
|
|
);
|
2020-04-23 00:20:26 +03:00
|
|
|
total_size = total_size + track.size.unwrap();
|
2020-04-15 01:55:02 +03:00
|
|
|
let attr = FileAttr {
|
|
|
|
ino: i as u64 + 2,
|
2020-04-23 00:20:26 +03:00
|
|
|
size: track.size.unwrap() as u64,
|
2020-04-15 01:55:02 +03:00
|
|
|
blocks: 0,
|
|
|
|
atime: ts,
|
|
|
|
mtime: ts,
|
|
|
|
ctime: ts,
|
|
|
|
crtime: ts,
|
|
|
|
kind: FileType::RegularFile,
|
|
|
|
perm: 0o644,
|
|
|
|
nlink: 0,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
rdev: 0,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
attrs.insert(attr.ino, attr);
|
|
|
|
inodes.insert(basename.clone(), attr.ino);
|
|
|
|
}
|
2020-04-19 02:43:38 +03:00
|
|
|
// Metrics file
|
|
|
|
let metrics_inode = 2 + tree.len() as u64;
|
|
|
|
let metrics_attr = FileAttr {
|
|
|
|
ino: metrics_inode,
|
|
|
|
size: 4096,
|
|
|
|
blocks: 0,
|
|
|
|
atime: ts,
|
|
|
|
mtime: ts,
|
|
|
|
ctime: ts,
|
|
|
|
crtime: ts,
|
|
|
|
kind: FileType::RegularFile,
|
|
|
|
perm: 0o444,
|
|
|
|
nlink: 0,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
rdev: 0,
|
|
|
|
flags: 0,
|
|
|
|
};
|
|
|
|
attrs.insert(metrics_attr.ino, metrics_attr);
|
|
|
|
inodes.insert("METRICS.TXT".to_string(), metrics_attr.ino);
|
|
|
|
warn!("Len: attrs: {}, ino: {}", attrs.len(), inodes.len());
|
|
|
|
info!(
|
|
|
|
"Filesystem initialized. Size: {} files, {}B in total.",
|
|
|
|
inodes.len(),
|
|
|
|
(SizeFormatterBinary::new(total_size as u64))
|
|
|
|
);
|
2020-04-15 01:55:02 +03:00
|
|
|
JsonFilesystem {
|
2020-04-17 18:20:20 +03:00
|
|
|
server: server,
|
2020-04-15 01:55:02 +03:00
|
|
|
tree: tree.clone(),
|
|
|
|
attrs: attrs,
|
|
|
|
inodes: inodes,
|
2020-04-19 02:43:38 +03:00
|
|
|
buffer_head_data: HashMap::new(),
|
|
|
|
buffer_head_index: HashSet::new(),
|
2020-04-17 03:13:54 +03:00
|
|
|
buffer_length: BTreeMap::new(),
|
2020-04-19 02:43:38 +03:00
|
|
|
metrics_inode: metrics_inode,
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
impl Filesystem for JsonFilesystem {
|
|
|
|
fn getattr(&mut self, _req: &Request, ino: u64, reply: ReplyAttr) {
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("getattr(ino={})", ino);
|
2020-04-15 01:55:02 +03:00
|
|
|
match self.attrs.get(&ino) {
|
|
|
|
Some(attr) => {
|
|
|
|
let ttl = Timespec::new(1, 0);
|
|
|
|
reply.attr(&ttl, attr);
|
|
|
|
}
|
|
|
|
None => reply.error(ENOENT),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEntry) {
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("lookup(parent={}, name={})", parent, name.to_str().unwrap());
|
2020-04-15 01:55:02 +03:00
|
|
|
let inode = match self.inodes.get(name.to_str().unwrap()) {
|
|
|
|
Some(inode) => inode,
|
|
|
|
None => {
|
|
|
|
reply.error(ENOENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match self.attrs.get(inode) {
|
|
|
|
Some(attr) => {
|
|
|
|
let ttl = Timespec::new(1, 0);
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("{:#?}", attr);
|
2020-04-15 01:55:02 +03:00
|
|
|
reply.entry(&ttl, attr, 0);
|
|
|
|
}
|
|
|
|
None => reply.error(ENOENT),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(
|
|
|
|
&mut self,
|
|
|
|
_req: &Request,
|
|
|
|
ino: u64,
|
|
|
|
fh: u64,
|
|
|
|
offset: i64,
|
|
|
|
size: u32,
|
|
|
|
reply: ReplyData,
|
|
|
|
) {
|
2020-04-19 02:43:38 +03:00
|
|
|
// return usage statistics
|
|
|
|
if ino == self.metrics_inode {
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
let metrics_str = format!("{:#?}", METRICS);
|
2020-04-19 02:43:38 +03:00
|
|
|
reply.data(&metrics_str.as_bytes());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleaning cache
|
|
|
|
if self.buffer_head_index.len() > MAX_CACHE_SIZE as usize {
|
|
|
|
let mut iter = self.buffer_head_index.iter().filter(|&x| *x != ino);
|
|
|
|
let old_entry = iter.next().unwrap();
|
|
|
|
self.buffer_head_data.remove(old_entry);
|
|
|
|
let old_entry_copy = old_entry.clone();
|
|
|
|
self.buffer_head_index.remove(&old_entry_copy);
|
|
|
|
let basename = &self.tree[(ino - 2) as usize].path.as_ref();
|
|
|
|
debug!(
|
|
|
|
"{} - Cache dropped for: {} ",
|
|
|
|
ino,
|
|
|
|
get_basename(*basename).unwrap().to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
debug!(
|
|
|
|
"{} - read(ino={}, fh={}, offset={}, size={}) ",
|
|
|
|
ino, ino, fh, offset, size
|
2020-04-15 01:55:02 +03:00
|
|
|
);
|
2020-04-17 17:57:54 +03:00
|
|
|
|
2020-04-15 01:55:02 +03:00
|
|
|
let url = &self.tree[(ino - 2) as usize].path.as_ref().unwrap();
|
2020-04-17 03:13:54 +03:00
|
|
|
let id = &self.tree[(ino - 2) as usize].id.as_ref().unwrap();
|
2020-04-19 02:43:38 +03:00
|
|
|
let full_url = format!("{}{}", self.server, url);
|
2020-04-25 01:11:18 +03:00
|
|
|
let chunk: Vec<u8>;
|
2020-04-17 03:13:54 +03:00
|
|
|
let content_length: i64;
|
2020-04-17 01:07:36 +03:00
|
|
|
let client = Client::new();
|
2020-04-17 03:13:54 +03:00
|
|
|
|
|
|
|
// content_length cache.
|
|
|
|
if self.buffer_length.contains_key(id.as_str()) {
|
|
|
|
content_length = self.buffer_length[id.as_str()];
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("{} - Hit length cache", ino);
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.hit_len_cache += 1;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
2020-04-17 03:13:54 +03:00
|
|
|
} else {
|
2020-04-19 02:43:38 +03:00
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
content_length = match client
|
2020-04-19 02:43:38 +03:00
|
|
|
.head(full_url.as_str())
|
2020-04-20 02:57:29 +03:00
|
|
|
.header("Authorization", format!("Basic {}", HTTP_AUTH))
|
2020-04-19 02:43:38 +03:00
|
|
|
.send()
|
2020-04-20 02:57:29 +03:00
|
|
|
{
|
|
|
|
Ok(content) => {
|
|
|
|
let content_length = match content.headers().get(CONTENT_LENGTH) {
|
|
|
|
Some(header_content) => {
|
|
|
|
header_content.to_str().unwrap().parse::<i64>().unwrap()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
reply.error(EIO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
content_length
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let name = &self.tree[(ino - 2) as usize].path.as_ref();
|
|
|
|
let basename = get_basename(*name).unwrap().to_string();
|
|
|
|
error!("An error fetching file {}. {}", basename, err);
|
|
|
|
METRICS.connect_errors += 1;
|
|
|
|
reply.error(EIO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.http_requests += 1;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
2020-04-17 03:13:54 +03:00
|
|
|
self.buffer_length.insert(id.to_string(), content_length);
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("{} - Miss length cache", ino);
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.miss_len_cache += 1;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
2020-04-17 03:13:54 +03:00
|
|
|
}
|
2020-04-19 02:43:38 +03:00
|
|
|
// Check for API wrong file size here
|
2020-04-17 17:57:54 +03:00
|
|
|
if content_length > offset {
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("{} - Content len {:?} ", ino, content_length);
|
2020-04-18 04:39:06 +03:00
|
|
|
let end_of_chunk = if size - 1 + offset as u32 > content_length as u32 {
|
|
|
|
content_length
|
|
|
|
} else {
|
|
|
|
(size + offset as u32) as i64
|
|
|
|
};
|
|
|
|
let range = format!("bytes={}-{}", offset, end_of_chunk - 1);
|
2020-04-17 17:57:54 +03:00
|
|
|
|
|
|
|
// if it's beginning of file...
|
2020-04-18 04:39:06 +03:00
|
|
|
if end_of_chunk < CACHE_HEAD {
|
2020-04-17 17:57:54 +03:00
|
|
|
// looking for CACHE_HEAD bytes file beginning in cache
|
2020-04-19 02:43:38 +03:00
|
|
|
if self.buffer_head_data.contains_key(&ino) {
|
|
|
|
// Cache found
|
|
|
|
debug!("{} - Hit data cache", ino);
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.hit_data_cache += 1;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
chunk = self.buffer_head_data[&ino][offset as usize..end_of_chunk as usize]
|
2020-04-17 17:57:54 +03:00
|
|
|
.to_vec()
|
|
|
|
.clone();
|
|
|
|
reply.data(&chunk);
|
|
|
|
} else {
|
2020-04-19 02:43:38 +03:00
|
|
|
// Cache doesn't found
|
|
|
|
debug!("{} - Miss data cache", ino);
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.miss_data_cache += 1;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
// Fetch file head (CACHE_HEAD)
|
2020-04-20 02:57:29 +03:00
|
|
|
let response: Vec<u8>;
|
2020-04-19 02:43:38 +03:00
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
response = match client
|
2020-04-19 02:43:38 +03:00
|
|
|
.get(full_url.as_str())
|
|
|
|
.header(
|
|
|
|
"Range",
|
|
|
|
format!(
|
|
|
|
"bytes=0-{}",
|
|
|
|
if CACHE_HEAD > content_length {
|
|
|
|
content_length - 1
|
|
|
|
} else {
|
|
|
|
CACHE_HEAD - 1
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
2020-04-20 02:57:29 +03:00
|
|
|
.header("Authorization", format!("Basic {}", HTTP_AUTH))
|
2020-04-19 02:43:38 +03:00
|
|
|
.send()
|
2020-04-20 02:57:29 +03:00
|
|
|
{
|
|
|
|
Ok(content) => content.bytes().unwrap().to_vec(),
|
|
|
|
Err(err) => {
|
|
|
|
let name = &self.tree[(ino - 2) as usize].path.as_ref();
|
|
|
|
let basename = get_basename(*name).unwrap().to_string();
|
|
|
|
error!("An error fetching file {}. {}", basename, err);
|
|
|
|
METRICS.connect_errors += 1;
|
|
|
|
reply.error(EIO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.http_requests += 1;
|
|
|
|
METRICS.ingress += response.len() as u64;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
// Save cache
|
|
|
|
self.buffer_head_data.insert(ino, response.to_vec());
|
|
|
|
self.buffer_head_index.insert(ino);
|
2020-04-17 17:57:54 +03:00
|
|
|
chunk = response[offset as usize..end_of_chunk as usize].to_vec();
|
|
|
|
reply.data(&chunk);
|
|
|
|
}
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("{} - Chunk len: {:?} ", ino, chunk.len());
|
2020-04-17 17:57:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-04-19 02:43:38 +03:00
|
|
|
// If it isn't a beginning of file don't cache it and fetch over HTTP directly.
|
2020-04-20 02:57:29 +03:00
|
|
|
let response: Vec<u8>;
|
2020-04-19 02:43:38 +03:00
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
response = match client
|
2020-04-19 02:43:38 +03:00
|
|
|
.get(full_url.as_str())
|
|
|
|
.header("Range", &range)
|
2020-04-20 02:57:29 +03:00
|
|
|
.header("Authorization", format!("Basic {}", HTTP_AUTH))
|
2020-04-19 02:43:38 +03:00
|
|
|
.send()
|
2020-04-20 02:57:29 +03:00
|
|
|
{
|
|
|
|
Ok(content) => content.bytes().unwrap().to_vec(),
|
|
|
|
Err(err) => {
|
|
|
|
let name = &self.tree[(ino - 2) as usize].path.as_ref();
|
|
|
|
let basename = get_basename(*name).unwrap().to_string();
|
|
|
|
error!("An error fetching file {}. {}", basename, err);
|
|
|
|
METRICS.connect_errors += 1;
|
|
|
|
reply.error(EIO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.http_requests += 1;
|
|
|
|
METRICS.ingress += response.len() as u64;
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
chunk = response.to_vec().clone();
|
2020-04-17 17:57:54 +03:00
|
|
|
reply.data(&chunk);
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!(
|
|
|
|
"{} - Len: {}, Chunk {} - {}",
|
|
|
|
ino,
|
2020-04-17 17:57:54 +03:00
|
|
|
chunk.len(),
|
|
|
|
offset,
|
2020-04-18 04:39:06 +03:00
|
|
|
offset + chunk.len() as i64
|
2020-04-17 17:57:54 +03:00
|
|
|
);
|
2020-04-15 01:55:02 +03:00
|
|
|
} else {
|
2020-04-19 02:43:38 +03:00
|
|
|
// Wrong filesize detected.
|
|
|
|
warn!(
|
|
|
|
"{} - Wrong offset. Len is {} but offset {}",
|
|
|
|
ino, content_length, offset
|
2020-04-17 17:57:54 +03:00
|
|
|
);
|
|
|
|
reply.data(&[]);
|
|
|
|
}
|
2020-04-17 01:07:36 +03:00
|
|
|
return;
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn readdir(
|
|
|
|
&mut self,
|
|
|
|
_req: &Request,
|
|
|
|
ino: u64,
|
|
|
|
fh: u64,
|
|
|
|
offset: i64,
|
|
|
|
mut reply: ReplyDirectory,
|
|
|
|
) {
|
2020-04-19 02:43:38 +03:00
|
|
|
debug!("readdir(ino={}, fh={}, offset={})", ino, fh, offset);
|
2020-04-15 01:55:02 +03:00
|
|
|
if ino == 1 {
|
|
|
|
if offset == 0 {
|
|
|
|
reply.add(1, 0, FileType::Directory, ".");
|
|
|
|
reply.add(1, 1, FileType::Directory, "..");
|
|
|
|
}
|
|
|
|
for (i, (key, &inode)) in self.inodes.iter().enumerate().skip(offset as usize) {
|
|
|
|
if inode == 1 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reply.add(inode, (i + 1) as i64, FileType::RegularFile, key);
|
|
|
|
}
|
|
|
|
reply.ok();
|
|
|
|
} else {
|
|
|
|
reply.error(ENOENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-04-19 03:33:29 +03:00
|
|
|
env_logger::init();
|
|
|
|
info!("Logger initialized. Set RUST_LOG=[debug,error,info,warn,trace] Default: info");
|
2020-04-15 01:55:02 +03:00
|
|
|
let mountpoint = match env::args().nth(1) {
|
|
|
|
Some(path) => path,
|
|
|
|
None => {
|
2020-04-18 04:39:06 +03:00
|
|
|
println!(
|
|
|
|
"Usage: {} <MOUNTPOINT> <SERVER>",
|
|
|
|
env::args().nth(0).unwrap()
|
|
|
|
);
|
2020-04-15 01:55:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-04-17 18:20:20 +03:00
|
|
|
let server = match env::args().nth(2) {
|
|
|
|
Some(server) => server,
|
|
|
|
None => {
|
2020-04-18 04:39:06 +03:00
|
|
|
println!(
|
|
|
|
"Usage: {} <MOUNTPOINT> <SERVER>",
|
|
|
|
env::args().nth(0).unwrap()
|
|
|
|
);
|
2020-04-17 18:20:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-04-19 02:43:38 +03:00
|
|
|
unsafe {
|
2020-04-20 02:57:29 +03:00
|
|
|
METRICS.server_addr = server.clone();
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
let http_user_var = "HTTP_USER";
|
|
|
|
let http_pass_var = "HTTP_PASS";
|
|
|
|
|
|
|
|
let http_user = match env::var_os(http_user_var) {
|
|
|
|
Some(val) => {
|
|
|
|
info!(
|
|
|
|
"Variable {} is set. Will be used for http auth as user.",
|
|
|
|
http_user_var
|
|
|
|
);
|
|
|
|
val.to_str().unwrap().to_string()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("{} is not defined in the environment.", http_user_var);
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let http_pass = match env::var_os(http_pass_var) {
|
|
|
|
Some(val) => {
|
|
|
|
info!(
|
|
|
|
"Variable {} is set. Will be used for http auth as password.",
|
|
|
|
http_pass_var
|
|
|
|
);
|
|
|
|
val.to_str().unwrap().to_string()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("{} is not defined in the environment.", http_pass_var);
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let mut buf = String::new();
|
|
|
|
buf.push_str(&http_user);
|
|
|
|
buf.push_str(":");
|
|
|
|
buf.push_str(&http_pass);
|
2020-04-20 02:57:29 +03:00
|
|
|
HTTP_AUTH = base64::encode(buf)
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
let lib = match get_tracks(&server) {
|
|
|
|
Ok(library) => library,
|
|
|
|
Err(err) => {
|
2020-04-19 03:33:29 +03:00
|
|
|
error!("Can't fetch library from remote server. Probably server not running or auth failed.");
|
|
|
|
error!(
|
|
|
|
"Provide Basic Auth credentials by setting envs {} and {}",
|
|
|
|
http_user_var, http_pass_var
|
|
|
|
);
|
|
|
|
panic!("Error: {}", err);
|
2020-04-19 02:43:38 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
info!("Remote library host: {}", &server);
|
2020-04-17 18:20:20 +03:00
|
|
|
let fs = JsonFilesystem::new(&lib, server);
|
2020-04-19 03:33:29 +03:00
|
|
|
let options = [
|
|
|
|
"-o",
|
|
|
|
"ro",
|
|
|
|
"-o",
|
|
|
|
"fsname=musfs",
|
|
|
|
"-o",
|
|
|
|
"sync_read",
|
|
|
|
"-o",
|
|
|
|
"auto_unmount",
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(|o| o.as_ref())
|
|
|
|
.collect::<Vec<&OsStr>>();
|
2020-04-19 02:43:38 +03:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"Caching {}B bytes in head of files.",
|
|
|
|
SizeFormatterBinary::new(CACHE_HEAD as u64)
|
|
|
|
);
|
|
|
|
info!("Max cache is {} files.", MAX_CACHE_SIZE);
|
|
|
|
info!("Mount options: {:?}", options);
|
2020-04-25 01:11:18 +03:00
|
|
|
let _mount: fuse::BackgroundSession;
|
2020-04-19 03:33:29 +03:00
|
|
|
unsafe {
|
2020-04-25 01:11:18 +03:00
|
|
|
_mount = fuse::spawn_mount(fs, &mountpoint, &options).expect("Couldn't mount filesystem");
|
2020-04-19 03:33:29 +03:00
|
|
|
}
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
println!("Exitting...");
|
|
|
|
process::exit(0x0000);
|
|
|
|
})
|
|
|
|
.expect("Error setting Ctrl-C handler");
|
2020-04-25 01:11:18 +03:00
|
|
|
loop {
|
|
|
|
sleep(Duration::from_millis(2));
|
|
|
|
}
|
2020-04-15 01:55:02 +03:00
|
|
|
}
|