diff mbox

HMP: snapshot_blkdev can not consider //root/sn1 and /root/sn1 as the same file

Message ID 52864A3A.4030601@gmail.com
State New
Headers show

Commit Message

lijun Nov. 15, 2013, 4:22 p.m. UTC
From: Jun Li <junmuzi@gmail.com>

Hi all,

snapshot_blkdev can not consider //root/sn1 and /root/sn1 as the same 
file. When file /root/sn1 is the base file, do snapshot using file 
//root/sn1, qemu consider it as a new file. So this will rewrite the 
base file.

Signed-off-by: Jun Li <junmuzi@gmail.com>

snapshot
@@ -970,6 +972,18 @@ void hmp_snapshot_blkdev(Monitor *mon, c
          return;
      }

+    /* Delete duplicate '/' in filename. */
+    while (*p != '\0') {
+        if (*p == '/') {
+            while (*(p + count++) == '/') {
+                /* do null. */
+            }
+            strcpy((char *)(p + 1), (char *)(p + count - 1));
+            count = 1;
+        }
+        p++;
+    }
+
      mode = reuse ? NEW_IMAGE_MODE_EXISTING : 
NEW_IMAGE_MODE_ABSOLUTE_PATHS;
      qmp_blockdev_snapshot_sync(device, filename, !!format, format,
                                 true, mode, &errp);

Comments

Eric Blake Nov. 15, 2013, 4:55 p.m. UTC | #1
On 11/15/2013 09:22 AM, lijun wrote:
> From: Jun Li <junmuzi@gmail.com>
> 
> Hi all,
> 
> snapshot_blkdev can not consider //root/sn1 and /root/sn1 as the same
> file. When file /root/sn1 is the base file, do snapshot using file
> //root/sn1, qemu consider it as a new file. So this will rewrite the
> base file.

Careful.  On cygwin, /root and //root are two different files (and POSIX
allows this).  While qemu hasn't yet been ported to cygwin, we should
not make the task harder by using incorrect ad-hoc normalization.

> +    /* Delete duplicate '/' in filename. */
> +    while (*p != '\0') {

Rather than hand-rolling a loop, you should use realpath() (guaranteed
by POSIX, but not always portable) or its appropriate glib wrapper,
where we can assume that glib will properly account for // being
distinct on platforms where it matters.
diff mbox

Patch

--- a/hmp.c    2013-11-15 23:15:46.733361130 +0800
+++ b/hmp.c    2013-11-16 00:20:23.972248509 +0800
@@ -957,10 +957,12 @@  void hmp_snapshot_blkdev(Monitor *mon, c
  {
      const char *device = qdict_get_str(qdict, "device");
      const char *filename = qdict_get_try_str(qdict, "snapshot-file");
+    const char *p = filename;
      const char *format = qdict_get_try_str(qdict, "format");
      int reuse = qdict_get_try_bool(qdict, "reuse", 0);
      enum NewImageMode mode;
      Error *errp = NULL;
+    int count = 1;

      if (!filename) {
          /* In the future, if 'snapshot-file' is not specified, the