[Libguestfs] [PATCH] file: Better support for read-only files

Richard W.M. Jones rjones at redhat.com
Tue Mar 2 15:01:36 UTC 2021


On Tue, Mar 02, 2021 at 08:44:04AM -0600, Eric Blake wrote:
> Previously, attempts to use nbdkit without -r in order to visit a
> read-only image failed to open the image (in fact, a single read-only
> file in an otherwise usable directory makes "nbdkit file dir=. --run
> 'nbdinfo --list'" fail to print anything in libnbd 1.6).  But a saner
> approach is to try a fallback to a readonly fd if a read-write fd
> fails.
> 
> The windows code compiles, but I wasn't able to test it as thoroughly
> as the Unix code.

> +static int
> +wintfile_can_write (void *handle)
> +{
> +  struct handle *h = handle;
> +  return !h->is_readonly;
> +}

Actually it doesn't because there's a misspelling.

I modified the patch s/wintfile/winfile/, but the newly added test
still fails on Wine with:

  nbdkit: file[1]: debug: file: flush
  nbdkit: file[1]: error: Z:\home\rjones\d\nbdkit-windows\tests\file-readonly.img: FlushFileBuffers: 5
  nbdkit: file[1]: debug: sending error reply: Input/output error
  ...
  libnbd: debug: nbdsh: nbd_flush: leave: error="nbd_flush: flush: command failed: Input/output error"
  ...
  Traceback (most recent call last):
    File "/usr/lib64/python3.9/site-packages/nbdsh.py", line 119, in shell
      exec(c, d, d)
    File "<string>", line 9, in <module>
    File "/usr/lib64/python3.9/site-packages/nbd.py", line 1608, in flush
      return libnbdmod.flush(self._o, flags)
  nbd.Error: nbd_flush: flush: command failed: Input/output error (EIO)

My guess is that Win32 doesn't let you flush a read-only file!
The actual error is ERROR_ACCESS_DENIED (== 5).

The attached patch fixes both issues.

Rest of the patch is fine, so ACK.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
-------------- next part --------------
>From 69bba02f382eb757db4a74136beb9cf45d715842 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Tue, 2 Mar 2021 14:59:35 +0000
Subject: [PATCH] UPDATE: "file: Better support for read-only files"

---
 plugins/file/winfile.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/plugins/file/winfile.c b/plugins/file/winfile.c
index ec186255..04018bfa 100644
--- a/plugins/file/winfile.c
+++ b/plugins/file/winfile.c
@@ -198,7 +198,18 @@ winfile_open (int readonly)
 }
 
 static int
-wintfile_can_write (void *handle)
+winfile_can_write (void *handle)
+{
+  struct handle *h = handle;
+  return !h->is_readonly;
+}
+
+/* Windows cannot flush on a read-only file.  It returns
+ * ERROR_ACCESS_DENIED.  Therefore don't advertise flush if the handle
+ * is r/o.
+ */
+static int
+winfile_can_flush (void *handle)
 {
   struct handle *h = handle;
   return !h->is_readonly;
@@ -442,6 +453,7 @@ static struct nbdkit_plugin plugin = {
 
   .open              = winfile_open,
   .can_write         = winfile_can_write,
+  .can_flush         = winfile_can_flush,
   .can_trim          = winfile_can_trim,
   .can_zero          = winfile_can_zero,
   .can_extents       = winfile_can_extents,
-- 
2.29.0.rc2



More information about the Libguestfs mailing list