Tage Johansson
2023-Aug-20 14:16 UTC
[Libguestfs] [libnbd PATCH v8 04/10] rust: Make it possible to run examples with a URI
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
Eric Blake
2023-Aug-22 21:45 UTC
[Libguestfs] [libnbd PATCH v8 04/10] rust: Make it possible to run examples with a URI
On Sun, Aug 20, 2023 at 02:16:23PM +0000, Tage Johansson wrote:> 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(-)> +++ 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'As $uri is likely to contain the shell glob character '?', we are best writing this as 'cargo ... -- "$uri"' to set a good example about how to avoid rare file name expansion interference. I made that change in place, as well as tweaking ocaml/examples/get_size.ml where you copied from. For 1-4, Reviewed-by: Eric Blake <eblake at redhat.com> and I've gone ahead and pushed them (55fec56c..e5563c0d), in order to see the effect on CI. For the rest of the series, I would like to spend a bit more time reviewing. -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Seemingly Similar Threads
- [libnbd PATCH v8 04/10] rust: Make it possible to run examples with a URI
- [libnbd PATCH v7 0/9] Rust Bindings for Libnbd
- [libnbd PATCH 0/2] (Attempt to) fix Rust on BSD-based builds
- [libnbd PATCH] RFC: Add bindings for Rust language
- [PATCH 2/2] Rust bindings: Make it able to publish this crate