diff --git a/Cargo.lock b/Cargo.lock index f8058e3..29d96b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,9 +34,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "4cd2405b3ac1faab2990b74d728624cd9fd115651fcecc7c2d8daf01376275ba" dependencies = [ "anstyle", "anstyle-parse", @@ -103,9 +103,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.21.5" +version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "bitflags" @@ -172,9 +172,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.12" +version = "4.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d" +checksum = "80932e03c33999b9235edb8655bc9df3204adc9887c2f95b50cb1deb9fd54253" dependencies = [ "clap_builder", "clap_derive", @@ -182,9 +182,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.12" +version = "4.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9" +checksum = "d6c0db58c659eef1c73e444d298c27322a1b52f6927d2ad470c0c0f96fa7b8fa" dependencies = [ "anstream", "anstyle", @@ -201,7 +201,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -245,9 +245,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "cpufeatures" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" dependencies = [ "libc", ] @@ -403,9 +403,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", "libc", @@ -420,9 +420,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" -version = "0.3.22" +version = "0.3.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178" +checksum = "b553656127a00601c8ae5590fcfdc118e4083a7924b6cf4ffc1ea4b99dc429d7" dependencies = [ "bytes", "fnv", @@ -542,9 +542,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.58" +version = "0.1.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -597,9 +597,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" dependencies = [ "wasm-bindgen", ] @@ -612,9 +612,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.151" +version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" [[package]] name = "linux-raw-sys" @@ -762,7 +762,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -797,9 +797,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.5" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae9cee2a55a544be8b89dc6848072af97a20f2422603c10865be2a42b580fff5" +checksum = "1f200d8d83c44a45b21764d1916299752ca035d15ecd46faca3e9a2a2bf6ad06" dependencies = [ "memchr", "thiserror", @@ -808,9 +808,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.5" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81d78524685f5ef2a3b3bd1cafbc9fcabb036253d9b1463e726a91cd16e2dfc2" +checksum = "bcd6ab1236bbdb3a49027e920e693192ebfe8913f6d60e294de57463a493cfde" dependencies = [ "pest", "pest_generator", @@ -818,22 +818,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.5" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68bd1206e71118b5356dae5ddc61c8b11e28b09ef6a31acbd15ea48a28e0c227" +checksum = "2a31940305ffc96863a735bef7c7994a00b325a7138fdbc5bda0f1a0476d3275" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "pest_meta" -version = "2.7.5" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c747191d4ad9e4a4ab9c8798f1e82a39affe7ef9648390b7e5548d18e099de6" +checksum = "a7ff62f5259e53b78d1af898941cdcdccfae7385cf7d793a6e55de5d05bb4b7d" dependencies = [ "once_cell", "pest", @@ -866,18 +866,18 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.71" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -967,9 +967,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.28" +version = "0.38.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" dependencies = [ "bitflags 2.4.1", "errno", @@ -1018,29 +1018,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" dependencies = [ "itoa", "ryu", @@ -1061,7 +1061,7 @@ dependencies = [ [[package]] name = "seva" -version = "0.1.2" +version = "0.1.3" dependencies = [ "bytes", "chrono", @@ -1111,9 +1111,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.2" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" +checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e" [[package]] name = "socket2" @@ -1144,9 +1144,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.43" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -1189,22 +1189,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -1297,7 +1297,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -1426,9 +1426,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1436,24 +1436,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.39" +version = "0.4.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" +checksum = "bde2032aeb86bdfaecc8b261eef3cba735cc426c1f3a3416d1e0791be95fc461" dependencies = [ "cfg-if", "js-sys", @@ -1463,9 +1463,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1473,28 +1473,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "web-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +checksum = "58cd2333b6e0be7a39605f0e255892fd7418a682d8da8fe042fe25128794d2ed" dependencies = [ "js-sys", "wasm-bindgen", @@ -1524,11 +1524,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-core" -version = "0.51.1" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 230519f..d477f2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "seva" -version = "0.1.2" +version = "0.1.3" edition = "2021" authors = ["Matt Gathu "] description = "Simple directory http server inspired by Python's http.server" diff --git a/src/errors.rs b/src/errors.rs index 2fbfad6..c42faec 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -15,8 +15,6 @@ pub enum SevaError { ParsingError(#[from] ParsingError), #[error(transparent)] TimeError(#[from] SystemTimeError), - #[error("errors that can never happen")] - Infallible, #[error(transparent)] RenderError(#[from] handlebars::RenderError), #[error(transparent)] diff --git a/src/fs.rs b/src/fs.rs index c424ac9..7dea2cb 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,4 +1,4 @@ -use std::{fs::Metadata, time::SystemTime}; +use std::{fs::Metadata, path::PathBuf, time::SystemTime}; use chrono::{DateTime, Utc}; use serde::Serialize; @@ -8,39 +8,30 @@ use crate::errors::{ParsingError, Result}; #[derive(Debug, Serialize)] pub struct DirEntry { pub name: String, - pub file_type: EntryType, + pub kind: EntryType, pub ext: Option, pub modified: DateTime, pub created: DateTime, pub size: u64, + pub path: PathBuf, } impl DirEntry { - pub fn dt(t: SystemTime) -> Result> { - let secs = t.duration_since(SystemTime::UNIX_EPOCH)?.as_secs(); - let dt = DateTime::::from_timestamp(secs as i64, 0); - match dt { - Some(dt) => Ok(dt), - None => { - Err(ParsingError::DateTime("date conversion failed".to_owned()) - .into()) - } - } - } - pub fn from_metadata(meta: Metadata, name: &str) -> Result { + pub fn from_metadata(meta: Metadata, name: &str, path: PathBuf) -> Result { let ext = name.rsplit_once('.').map(|(_, e)| e.to_string()); - let file_type = EntryType::from(meta.file_type()); - let name = if file_type == EntryType::Dir && !name.ends_with('/') { + let kind = EntryType::from(meta.file_type()); + let name = if kind == EntryType::Dir && !name.ends_with('/') { format!("{}/", name) } else { name.to_string() }; Ok(Self { name, - file_type, + kind, ext, - modified: Self::dt(meta.modified()?)?, - created: Self::dt(meta.created()?)?, + modified: dt(meta.modified()?)?, + created: dt(meta.created()?)?, size: meta.len(), + path, }) } } @@ -60,3 +51,14 @@ impl From for EntryType { } } } + +fn dt(t: SystemTime) -> Result> { + let secs = t.duration_since(SystemTime::UNIX_EPOCH)?.as_secs(); + let dt = DateTime::::from_timestamp(secs as i64, 0); + match dt { + Some(dt) => Ok(dt), + None => { + Err(ParsingError::DateTime("date conversion failed".to_owned()).into()) + } + } +} diff --git a/src/main.rs b/src/main.rs index a76171c..93a3090 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod fs; mod http; mod mime; mod server; +mod utils; /// A simple directory http server. /// @@ -45,7 +46,7 @@ struct Args { log_level: Level, /// Timeout duration in seconds - #[arg(long, default_value = "60")] + #[arg(long, default_value = "10")] timeout: u64, } diff --git a/src/server.rs b/src/server.rs index f30c386..dda2d4f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,7 +4,6 @@ use std::{ io::{self, Cursor, Read, Seek, Write}, net::{SocketAddr, TcpListener, TcpStream}, path::PathBuf, - str::FromStr, sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -27,6 +26,7 @@ use crate::{ StatusCode, }, mime::MimeType, + utils::t, }; const MAX_URI_LEN: usize = 64000; @@ -70,12 +70,15 @@ impl HttpServer { ) -> Result { debug!("binding to {host} on port: {port}"); let listener = TcpListener::bind((host, port))?; - listener.set_nonblocking(true)?; + t!("listener: set non-blocking", listener.set_nonblocking(true))?; let shutdown = Arc::new(AtomicBool::new(false)); let s = shutdown.clone(); if !test_mode { - ctrlc::set_handler(move || s.store(true, Ordering::SeqCst))?; + t!( + "ctrlc::set_handler", + ctrlc::set_handler(move || s.store(true, Ordering::SeqCst)) + )?; } Ok(Self { dir, @@ -95,8 +98,14 @@ impl HttpServer { loop { match self.listener.accept() { Ok((stream, client_addr)) => { - stream.set_read_timeout(Some(self.timeout))?; - stream.set_write_timeout(Some(self.timeout))?; + t!( + "set read timeout", + stream.set_read_timeout(Some(self.timeout)) + )?; + t!( + "set write timeout", + stream.set_write_timeout(Some(self.timeout)) + )?; let dir = self.dir.clone(); let mut handler = RequestHandler::new(stream, client_addr, dir, self.timeout); @@ -147,7 +156,7 @@ impl RequestHandler { } fn handle(&mut self) -> Result<()> { - match self._handle() { + match t!("RequestHandler::handle", self._handle()) { Ok(_) => { trace!("RequestHandler::handle OK"); } @@ -182,8 +191,8 @@ impl RequestHandler { fn _handle(&mut self) -> Result<()> { debug!("handling stream"); - let req_str = self.read_request()?; - let req = Request::parse(&req_str)?; + let req_str = t!("RequestHandler::read_request", self.read_request())?; + let req = t!("Request::parse", Request::parse(&req_str))?; // check if method is allowed if req.method != HttpMethod::Get && req.method != HttpMethod::Head { @@ -196,7 +205,7 @@ impl RequestHandler { if req_path == "/" || req_path == "/index.html" || req_path.is_empty() { self.send_dir(&req, "/", &self.dir.clone())?; } else if let Some(entry) = self.lookup_path(&req_path)? { - match entry.file_type { + match entry.kind { EntryType::File => { if req.is_partial() { self.send_partial(&req, &entry)? @@ -207,12 +216,7 @@ impl RequestHandler { EntryType::Dir => { if req_path.ends_with('/') { trace!("RequestHandler::_handle send_dir"); - self.send_dir( - &req, - &req_path, - &PathBuf::from_str(&entry.name) - .map_err(|_| SevaError::Infallible)?, - )? + self.send_dir(&req, &req_path, &entry.path)? } else { trace!("RequestHandler::_handle redirect"); self.redirect(&req, &format!("/{}/", req_path))? @@ -255,9 +259,10 @@ impl RequestHandler { } fn send_file(&mut self, req: &Request, entry: &DirEntry) -> Result<()> { + trace!("RequestHandler::send_file req=[{req:?}] entry=[{entry:?}]"); let resp = ResponseBuilder::ok() .headers(self.get_file_headers(entry)) - .body(File::open(&entry.name)?) + .body(t!("send_file File::open", File::open(&entry.path))?) .build(); self.send_response(resp, req)?; @@ -327,9 +332,12 @@ impl RequestHandler { fn lookup_path(&mut self, path: &str) -> Result> { debug!("path lookup: {path}"); let fpath = self.dir.join(path); - debug!("path lookup fpath: {fpath:?}"); - match metadata(fpath) { - Ok(meta) => Ok(Some(DirEntry::from_metadata(meta, path)?)), + debug!("path lookup full path: {fpath:?}"); + match metadata(&fpath) { + Ok(meta) => { + let de = DirEntry::from_metadata(meta, path, fpath)?; + Ok(Some(de)) + } Err(e) => { if e.is_not_found() { Ok(None) @@ -517,7 +525,7 @@ impl RequestHandler { let item = entry?; let meta = item.metadata()?; let name = format!("{}", item.file_name().to_string_lossy()); - let entry = DirEntry::from_metadata(meta, &name)?; + let entry = DirEntry::from_metadata(meta, &name, item.path())?; entries.push(entry); } Ok(entries) diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..e548425 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,18 @@ +/// A utility macro that wraps around Result expressions and +/// emits trace logs. +macro_rules! t { + ($id:expr, $expr:expr $(,)?) => { + match $expr { + Ok(val) => { + trace!("{} ok.", $id); + Ok(val) + } + Err(err) => { + error!("{} failed. Reason: {err:?}", $id); + Err(err) + } + } + }; +} + +pub(crate) use t;