mirror of
https://codeberg.org/icewind/cube.git
synced 2026-06-03 12:04:10 +02:00
error improvements
This commit is contained in:
parent
7d7c252eea
commit
33e0cb8290
3 changed files with 22 additions and 12 deletions
|
|
@ -130,7 +130,7 @@ impl ExportConfig {
|
||||||
path: self.path.clone(),
|
path: self.path.clone(),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
info!(readonly, size, "go export meta");
|
info!(readonly, size, "opened export");
|
||||||
|
|
||||||
Ok(Export {
|
Ok(Export {
|
||||||
readonly,
|
readonly,
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ impl From<NbdError> for IoError {
|
||||||
impl From<IoError> for NbdError {
|
impl From<IoError> for NbdError {
|
||||||
fn from(value: IoError) -> Self {
|
fn from(value: IoError) -> Self {
|
||||||
match value.kind() {
|
match value.kind() {
|
||||||
ErrorKind::UnexpectedEof => NbdError::Disconnected,
|
ErrorKind::UnexpectedEof | ErrorKind::ConnectionReset => NbdError::Disconnected,
|
||||||
_ => NbdError::Io(value),
|
_ => NbdError::Io(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
30
src/main.rs
30
src/main.rs
|
|
@ -8,6 +8,7 @@ use nbd::server::{handshake, transmission};
|
||||||
use signal_hook::consts::SIGHUP;
|
use signal_hook::consts::SIGHUP;
|
||||||
use signal_hook::iterator::exfiltrator::SignalOnly;
|
use signal_hook::iterator::exfiltrator::SignalOnly;
|
||||||
use signal_hook::iterator::SignalsInfo;
|
use signal_hook::iterator::SignalsInfo;
|
||||||
|
use std::fs::File;
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::thread::spawn;
|
use std::thread::spawn;
|
||||||
|
|
@ -22,18 +23,31 @@ struct Args {
|
||||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(remote = ?stream.peer_addr().ok()))]
|
#[tracing::instrument(skip_all, fields(remote = ?stream.peer_addr().ok()))]
|
||||||
fn handle_client(mut stream: TcpStream, exports: Exports) -> Result<(), NbdError> {
|
fn connect_client(stream: &mut TcpStream, exports: Exports) -> Result<File, NbdError> {
|
||||||
let file = handshake(&mut stream, move |name| {
|
handshake(stream, move |name| {
|
||||||
let export_cfg = exports
|
let export_cfg = exports
|
||||||
.get(name)
|
.get(name)
|
||||||
.ok_or_else(|| HandshakeError::UnknownExport(name.into()))?;
|
.ok_or_else(|| HandshakeError::UnknownExport(name.into()))?;
|
||||||
info!(name = name, export = ?export_cfg, "opening export");
|
info!(name = name, export = ?export_cfg, "opening export");
|
||||||
Ok(export_cfg.export()?)
|
Ok(export_cfg.export()?)
|
||||||
})?;
|
})
|
||||||
|
.map_err(NbdError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, fields(remote = ?stream.peer_addr().ok()))]
|
||||||
|
fn handle_client(mut stream: TcpStream, exports: Exports) {
|
||||||
|
let file = match connect_client(&mut stream, exports) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(e) => {
|
||||||
|
error!(error = ?e, "error while opening export");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
info!("connected");
|
info!("connected");
|
||||||
transmission(&mut stream, file)?;
|
if let Err(e) = transmission(&mut stream, file).map_err(NbdError::from) {
|
||||||
|
error!("{e}");
|
||||||
|
}
|
||||||
info!("disconnected");
|
info!("disconnected");
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
|
@ -64,11 +78,7 @@ fn main() -> Result<()> {
|
||||||
match stream {
|
match stream {
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
let exports = config.exports.clone();
|
let exports = config.exports.clone();
|
||||||
spawn(move || {
|
spawn(move || handle_client(stream, exports));
|
||||||
if let Err(e) = handle_client(stream, exports) {
|
|
||||||
error!("{e}");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let e = Error::Connection(e);
|
let e = Error::Connection(e);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue