Add arg parser.

This commit is contained in:
Alexandr Bogomyakov
2020-04-25 22:20:32 +03:00
parent 824ccbb366
commit 3446e7f3d5
2 changed files with 43 additions and 35 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "musfuse" name = "musfuse"
version = "0.5.0" version = "0.6.0"
authors = ["AB <ultradesu@hexor.ru>"] authors = ["AB <ultradesu@hexor.ru>"]
edition = "2018" edition = "2018"
@ -9,6 +9,7 @@ edition = "2018"
reqwest = { version = "0.10", features = ["json", "blocking"] } reqwest = { version = "0.10", features = ["json", "blocking"] }
tokio = { version = "0.2", features = ["full"] } tokio = { version = "0.2", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
clap = {version = "2.33", features = ["yaml"]}
serde_json = "1.0" serde_json = "1.0"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"
fuse = "0.3.1" fuse = "0.3.1"

View File

@ -1,4 +1,5 @@
extern crate base64; extern crate base64;
extern crate clap;
extern crate fuse; extern crate fuse;
extern crate libc; extern crate libc;
extern crate time; extern crate time;
@ -6,12 +7,15 @@ extern crate time;
extern crate log; extern crate log;
extern crate chrono; extern crate chrono;
use clap::{App, Arg};
use time::Timespec; use env_logger::Env;
use percent_encoding::percent_decode_str; use fuse::{
use serde::Deserialize; FileAttr, FileType, Filesystem, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, Request,
};
use libc::{EIO, ENOENT}; use libc::{EIO, ENOENT};
use percent_encoding::percent_decode_str;
use reqwest::{blocking::Client, header::CONTENT_LENGTH}; use reqwest::{blocking::Client, header::CONTENT_LENGTH};
use serde::Deserialize;
use size_format::SizeFormatterBinary; use size_format::SizeFormatterBinary;
use std::{ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
@ -23,10 +27,11 @@ use std::{
thread::sleep, thread::sleep,
time::Duration, time::Duration,
}; };
use fuse::{ use time::Timespec;
FileAttr, FileType, Filesystem, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, Request,
};
const CACHE_HEAD: i64 = 768 * 1024; // bytes from beginning of each file cached
const MAX_CACHE_SIZE: i64 = 10; // Count of files cached
static mut HTTP_AUTH: String = String::new();
struct Metrics { struct Metrics {
http_requests: u64, http_requests: u64,
@ -80,10 +85,6 @@ pub struct Track {
pub size: Option<i64>, pub size: Option<i64>,
} }
const CACHE_HEAD: i64 = 768 * 1024;
const MAX_CACHE_SIZE: i64 = 10; // Count
static mut HTTP_AUTH: String = String::new();
fn get_basename(path: Option<&String>) -> Option<String> { fn get_basename(path: Option<&String>) -> Option<String> {
let base = match percent_decode_str(path.unwrap().as_str()).decode_utf8() { let base = match percent_decode_str(path.unwrap().as_str()).decode_utf8() {
Ok(path) => { Ok(path) => {
@ -200,7 +201,6 @@ impl JsonFilesystem {
}; };
attrs.insert(metrics_attr.ino, metrics_attr); attrs.insert(metrics_attr.ino, metrics_attr);
inodes.insert("METRICS.TXT".to_string(), metrics_attr.ino); inodes.insert("METRICS.TXT".to_string(), metrics_attr.ino);
warn!("Len: attrs: {}, ino: {}", attrs.len(), inodes.len());
info!( info!(
"Filesystem initialized. Size: {} files, {}B in total.", "Filesystem initialized. Size: {} files, {}B in total.",
inodes.len(), inodes.len(),
@ -484,28 +484,35 @@ impl Filesystem for JsonFilesystem {
} }
fn main() { fn main() {
env_logger::init(); env_logger::from_env(Env::default().default_filter_or("info")).init();
info!("Logger initialized. Set RUST_LOG=[debug,error,info,warn,trace] Default: info"); info!("Logger initialized. Set RUST_LOG=[debug,error,info,warn,trace] Default: info");
let mountpoint = match env::args().nth(1) { let cli_args = App::new("mus-fuse")
Some(path) => path, .version(env!("CARGO_PKG_VERSION"))
None => { .author(env!("CARGO_PKG_AUTHORS"))
println!( .about("Mount FUSE filesystem with your own remote library.")
"Usage: {} <MOUNTPOINT> <SERVER>", .arg(
env::args().nth(0).unwrap() Arg::with_name("server")
); .short("s")
return; .long("server")
} .value_name("SERVER")
}; .help("Sets a server hosting your library")
let server = match env::args().nth(2) { .required(true)
Some(server) => server, .takes_value(true),
None => { )
println!( .arg(
"Usage: {} <MOUNTPOINT> <SERVER>", Arg::with_name("mountpoint")
env::args().nth(0).unwrap() .short("m")
); .long("mountpoint")
return; .value_name("MOUNT POINT")
} .help("Mount point for library.")
}; .required(true)
.takes_value(true),
)
.get_matches();
let server = cli_args.value_of("server").unwrap().to_string();
let mountpoint = cli_args.value_of("mountpoint").unwrap().to_string();
unsafe { unsafe {
METRICS.server_addr = server.clone(); METRICS.server_addr = server.clone();
} }
@ -588,6 +595,6 @@ fn main() {
}) })
.expect("Error setting Ctrl-C handler"); .expect("Error setting Ctrl-C handler");
loop { loop {
sleep(Duration::from_millis(2)); sleep(Duration::from_millis(300));
} }
} }