Previously, the examples fetch-first-sector and get-size in rust/examples only accepted a unix socket as argument. This commit makes it possible to provide a URI as well. --- rust/examples/fetch-first-sector.rs | 15 +++++++++++---- rust/examples/get-size.rs | 20 +++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/rust/examples/fetch-first-sector.rs b/rust/examples/fetch-first-sector.rs index 9efb47a..b035ccb 100644 --- a/rust/examples/fetch-first-sector.rs +++ b/rust/examples/fetch-first-sector.rs @@ -7,6 +7,9 @@ //! //! nbdkit -U - floppy . \ //! --run 'cargo run --example fetch-first-sector -- $unixsocket' +//! Or with a URI: +//! nbdkit -U - floppy . \ +//! --run 'cargo run --example fetch-first-sector -- $uri' //! //! The nbdkit floppy plugin creates an MBR disk so the //! first sector is the partition table. @@ -21,11 +24,15 @@ fn main() -> anyhow::Result<()> { if args.len() != 2 { anyhow::bail!("Usage: {:?} socket", args[0]); } - let socket = &args[1]; - // Connect to the NBD server over a - // Unix domain socket. - nbd.connect_unix(socket)?; + // Check if the user provided a URI or a unix socket. + let socket_or_uri = args[1].to_str().unwrap(); + if socket_or_uri.contains("://") { + nbd.connect_uri(socket_or_uri)?; + } else { + // Connect to the NBD server over a Unix domain socket. + nbd.connect_unix(socket_or_uri)?; + } // Read the first sector synchronously. let mut buf = [0; 512]; diff --git a/rust/examples/get-size.rs b/rust/examples/get-size.rs index 7f31df5..7af8e9f 100644 --- a/rust/examples/get-size.rs +++ b/rust/examples/get-size.rs @@ -5,6 +5,12 @@ //! //! nbdkit -U - memory 1M \ //! --run 'cargo run --example get-size -- $unixsocket' +//! Or with a URI: +//! nbdkit -U - memory 1M \ +//! --run 'cargo run --example get-size -- $uri' +//! +//! Or connect over a URI: +//! cargo run --example get-size -- nbd://hostname:port use std::env; @@ -15,15 +21,19 @@ fn main() -> anyhow::Result<()> { if args.len() != 2 { anyhow::bail!("Usage: {:?} socket", args[0]); } - let socket = &args[1]; - // Connect to the NBD server over a - // Unix domain socket. - nbd.connect_unix(socket)?; + // Check if the user provided a URI or a unix socket. + let socket_or_uri = args[1].to_str().unwrap(); + if socket_or_uri.contains("://") { + nbd.connect_uri(socket_or_uri)?; + } else { + // Connect to the NBD server over a Unix domain socket. + nbd.connect_unix(socket_or_uri)?; + } // Read the size in bytes and print it. let size = nbd.get_size()?; - println!("{:?}: size = {size} bytes", socket); + println!("{:?}: size = {size} bytes", socket_or_uri); Ok(()) } -- 2.41.0 _______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs