All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ram Pai <linuxram@us.ibm.com>
To: kvm-devel <kvm@vger.kernel.org>
Cc: Anthony Liguori <aliguori@us.ibm.com>
Subject: [PATCH] support colon in filenames
Date: Wed, 24 Jun 2009 09:58:59 -0700	[thread overview]
Message-ID: <1245862739.6278.7.camel@localhost> (raw)

Problem: It is impossible to feed filenames with the character colon because 
qemu interprets such names as a protocol. For example  a filename scsi:0,
is interpreted as a protocol by name "scsi".

This patch allows users to espace colon characters. For example the above filename
can now be expressed as 'scsi\:0'

Here are couple of examples:

ndb:\::9999  is treated as a ndb protocol with a hostname ':' on port 9999
scsi\:0\:abc is a local file scsi:0:abc
http\://myweb is a local file by name http://myweb
nbd\::localhost:2558  is a protocol by name nbd: 

Signed-off-by: Ram Pai <linuxram@us.ibm.com>
-----------------------------------------------------------------------


 block.c           |   26 +++++++++++++++++---------
 block/nbd.c       |   19 ++++++++++++++-----
 block/raw-posix.c |   24 +++++++++++++++++-------
 cutils.c          |   22 ++++++++++++++++++++++
 qemu-common.h     |    1 +
 vl.c              |    3 +--
 6 files changed, 72 insertions(+), 23 deletions(-)

diff --git a/block.c b/block.c
index aca5a6d..80bded9 100644
--- a/block.c
+++ b/block.c
@@ -225,22 +225,30 @@ static BlockDriver *find_protocol(const char *filename)
 {
     BlockDriver *drv1;
     char protocol[128];
-    int len;
-    const char *p;
+    char *p = protocol; 
+    const char *f=filename;
+    int len = strnlen(filename, 128);
 
 #ifdef _WIN32
     if (is_windows_drive(filename) ||
         is_windows_drive_prefix(filename))
         return bdrv_find_format("raw");
 #endif
-    p = strchr(filename, ':');
-    if (!p)
+    while ( f < filename+len ) {
+	if ( *f == ':' )
+		break;
+	if ( *f == '\\') {
+		f++;
+		if ( *f == '\0')
+			break;
+	}
+	*p++ = *f++;
+    }
+    *p='\0';
+
+    if (*f != ':')
         return bdrv_find_format("raw");
-    len = p - filename;
-    if (len > sizeof(protocol) - 1)
-        len = sizeof(protocol) - 1;
-    memcpy(protocol, filename, len);
-    protocol[len] = '\0';
+
     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
         if (drv1->protocol_name &&
             !strcmp(drv1->protocol_name, protocol))
diff --git a/block/nbd.c b/block/nbd.c
index 47d4778..a011cc7 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -64,18 +64,27 @@ static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
 
     } else {
         uint16_t port;
-        char *p, *r;
+        char *q, *p, *r;
         char hostname[128];
 
         pstrcpy(hostname, 128, host);
 
-        p = strchr(hostname, ':');
-        if (p == NULL)
+	q=p=hostname;
+	while ( p < hostname+128 ) {
+		if (*p == ':')
+			break;
+		if ( *p == '\\' )  {
+			p++;
+			if (*p =='\0') 
+				break;
+		}
+		*q++=*p++;
+	}
+        if (*p != ':')
             return -EINVAL;
 
-        *p = '\0';
+	*q='\0';
         p++;
-
         port = strtol(p, &r, 0);
         if (r == p)
             return -EINVAL;
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 41bfa37..98ede17 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -124,6 +124,16 @@ static int fd_open(BlockDriverState *bs);
 static int cdrom_reopen(BlockDriverState *bs);
 #endif
 
+static int _open(const char *filename, int flags, ...)
+{
+	char myfile[PATH_MAX];
+   	va_list ap;
+   	va_start(ap, flags);
+	return  open(esc_string(myfile, PATH_MAX, filename),
+				flags, ap);
+}
+
+
 static int raw_open_common(BlockDriverState *bs, const char *filename,
         int flags)
 {
@@ -151,7 +161,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename,
         s->open_flags |= O_DSYNC;
 
     s->fd = -1;
-    fd = open(filename, s->open_flags, 0644);
+    fd = _open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;
         if (ret == -EROFS)
@@ -844,7 +854,7 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)
         options++;
     }
 
-    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
+    fd = _open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
               0644);
     if (fd < 0)
         return -EIO;
@@ -985,7 +995,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
         if ( bsdPath[ 0 ] != '\0' ) {
             strcat(bsdPath,"s0");
             /* some CDs don't have a partition 0 */
-            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
+            fd = _open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
             if (fd < 0) {
                 bsdPath[strlen(bsdPath)-1] = '1';
             } else {
@@ -1037,7 +1047,7 @@ static int fd_open(BlockDriverState *bs)
 #endif
             return -EIO;
         }
-        s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
+        s->fd = _open(bs->filename, s->open_flags & ~O_NONBLOCK);
         if (s->fd < 0) {
             s->fd_error_time = qemu_get_clock(rt_clock);
             s->fd_got_error = 1;
@@ -1133,7 +1143,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options)
         options++;
     }
 
-    fd = open(filename, O_WRONLY | O_BINARY);
+    fd = _open(filename, O_WRONLY | O_BINARY);
     if (fd < 0)
         return -EIO;
 
@@ -1239,7 +1249,7 @@ static int floppy_eject(BlockDriverState *bs, int eject_flag)
         close(s->fd);
         s->fd = -1;
     }
-    fd = open(bs->filename, s->open_flags | O_NONBLOCK);
+    fd = _open(bs->filename, s->open_flags | O_NONBLOCK);
     if (fd >= 0) {
         if (ioctl(fd, FDEJECT, 0) < 0)
             perror("FDEJECT");
@@ -1399,7 +1409,7 @@ static int cdrom_reopen(BlockDriverState *bs)
      */
     if (s->fd >= 0)
         close(s->fd);
-    fd = open(bs->filename, s->open_flags, 0644);
+    fd = _open(bs->filename, s->open_flags, 0644);
     if (fd < 0) {
         s->fd = -1;
         return -EIO;
diff --git a/cutils.c b/cutils.c
index 6ea0c49..63c196d 100644
--- a/cutils.c
+++ b/cutils.c
@@ -24,6 +24,28 @@
 #include "qemu-common.h"
 #include "host-utils.h"
 
+/*
+ * prune escape character '\'
+ */
+char *esc_string(char *buf, int buf_size, const char *str)
+{
+        const char *p=str;
+        int len = strlen(str);
+        char *q=buf;
+
+	len = (buf_size < len) ? buf_size: len;
+        while (p < str+len) {
+                if (*p == '\\') {
+                        p++;
+			if (*p == '\0')
+				break;
+		}
+                *q++ = *p++;
+        }
+        *q='\0';
+	return buf;
+}
+
 void pstrcpy(char *buf, int buf_size, const char *str)
 {
     int c;
diff --git a/qemu-common.h b/qemu-common.h
index 2dcb224..1e510dc 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -104,6 +104,7 @@ void qemu_get_timedate(struct tm *tm, int offset);
 int qemu_timedate_diff(struct tm *tm);
 
 /* cutils.c */
+char *esc_string(char *buf, int buf_size, const char *str);
 void pstrcpy(char *buf, int buf_size, const char *str);
 char *pstrcat(char *buf, int buf_size, const char *s);
 int strstart(const char *str, const char *val, const char **ptr);
diff --git a/vl.c b/vl.c
index 7278999..5d7b024 100644
--- a/vl.c
+++ b/vl.c
@@ -2583,8 +2583,7 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
     else if (cache == 3) /* not specified */
         bdrv_flags |= BDRV_O_CACHE_DEF;
     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
-        fprintf(stderr, "qemu: could not open disk image %s\n",
-                        file);
+        fprintf(stderr, "qemu: could not open disk image %s\n", file);
         return -1;
     }
     if (bdrv_key_required(bdrv))

 


             reply	other threads:[~2009-06-24 16:58 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-24 16:58 Ram Pai [this message]
2009-06-24 17:08 ` [PATCH] support colon in filenames Balbir Singh
2009-06-24 17:30   ` Ram Pai
2009-06-24 18:31     ` Balbir Singh
2009-06-24 17:26 ` Amit Shah
2009-06-24 17:27   ` Amit Shah
2009-06-24 17:57 ` Ram Pai
2009-06-24 17:57   ` [Qemu-devel] " Ram Pai
2009-06-25  9:14   ` Kevin Wolf
2009-06-25  9:14     ` [Qemu-devel] " Kevin Wolf
2009-06-25 17:52     ` Ram Pai
2009-06-25 17:52       ` [Qemu-devel] " Ram Pai
2009-06-26  6:53       ` Kevin Wolf
2009-06-26  6:53         ` [Qemu-devel] " Kevin Wolf
2009-06-26  6:38     ` rev1 " Ram Pai
2009-06-26  6:38       ` [Qemu-devel] " Ram Pai
2009-06-26  7:45       ` Kevin Wolf
2009-06-26  7:45         ` [Qemu-devel] " Kevin Wolf
2009-06-27  0:41         ` rev2 " Ram Pai
2009-06-27  0:41           ` [Qemu-devel] " Ram Pai
2009-07-02  5:08           ` [PATCH] rev3: " Ram Pai
2009-07-02  5:08             ` [Qemu-devel] " Ram Pai
2009-07-02  8:52             ` Kevin Wolf
2009-07-02 12:52               ` Anthony Liguori
2009-07-02 13:18                 ` Kevin Wolf
2009-07-08  8:30                   ` [PATCH] rev4: " Ram Pai
2009-07-08  8:30                     ` [Qemu-devel] " Ram Pai
2009-07-08 15:05                     ` Jan Kiszka
2009-07-08 15:05                       ` [Qemu-devel] " Jan Kiszka
2009-07-10 13:31                     ` Anthony Liguori
2009-07-10 13:31                       ` [Qemu-devel] " Anthony Liguori
2009-07-15  7:51                     ` [PATCH] rev5: " Ram Pai
2009-07-15  7:51                       ` [Qemu-devel] " Ram Pai
2009-07-15  9:30                       ` Jan Kiszka
2009-07-15  9:30                         ` [Qemu-devel] " Jan Kiszka
2009-07-15 17:03                         ` Ram Pai
2009-07-15 17:03                           ` [Qemu-devel] " Ram Pai
2009-07-15 18:20                           ` Jamie Lokier
2009-07-15 18:20                             ` Jamie Lokier
2009-07-15 18:44                             ` Ram Pai
2009-07-15 18:44                               ` Ram Pai
2009-07-15 21:04                               ` qcow2 relative paths (was: [PATCH] rev5: support colon in filenames) Jamie Lokier
2009-07-15 21:04                                 ` [Qemu-devel] " Jamie Lokier
2009-07-15 21:14                                 ` qcow2 relative paths Jan Kiszka
2009-07-15 21:14                                   ` [Qemu-devel] " Jan Kiszka
2009-07-16  2:28                                 ` qcow2 relative paths (was: [PATCH] rev5: support colon in filenames) Ram Pai
2009-07-16  2:28                                   ` [Qemu-devel] " Ram Pai
2009-07-16  7:38                                   ` qcow2 relative paths Kevin Wolf
2009-07-16  7:38                                     ` [Qemu-devel] " Kevin Wolf
2009-07-16  7:51                                     ` Ram Pai
2009-07-16  7:51                                       ` [Qemu-devel] " Ram Pai
2009-07-16  7:39                                 ` [PATCH] rev6: support colon in filenames Ram Pai
2009-07-16  7:39                                   ` [Qemu-devel] " Ram Pai
2009-07-17 23:17                                   ` [PATCH] rev7: " Ram Pai
2009-07-17 23:17                                     ` [Qemu-devel] " Ram Pai
2009-07-21 12:42                                     ` Kevin Wolf
2009-07-21 12:42                                       ` [Qemu-devel] " Kevin Wolf
2009-08-06  6:27                                       ` Ram Pai
2009-08-06  6:27                                         ` [Qemu-devel] " Ram Pai
2009-08-06  6:47                                         ` [PATCH] rev8: " Ram Pai
2009-08-06  6:47                                           ` [Qemu-devel] " Ram Pai
2009-07-15 15:04                       ` [Qemu-devel] [PATCH] rev5: " Blue Swirl
2009-07-15 15:04                         ` Blue Swirl
2009-07-15 15:14                         ` Anthony Liguori
2009-07-15 15:14                           ` Anthony Liguori
2009-07-15 15:29                           ` Blue Swirl
2009-07-15 15:29                             ` Blue Swirl
2009-07-15 15:40                             ` Anthony Liguori
2009-07-15 15:40                               ` Anthony Liguori
2009-07-15 16:42                               ` Kevin Wolf
2009-07-15 16:42                                 ` Kevin Wolf
2009-07-15 17:47                               ` Michael S. Tsirkin
2009-07-15 17:47                                 ` Michael S. Tsirkin
2009-07-16 10:57                               ` Amit Shah
2009-07-16 10:57                                 ` Amit Shah
2009-07-16 13:43                               ` Markus Armbruster
2009-07-16 13:43                                 ` Markus Armbruster
2009-07-16 14:10                                 ` Anthony Liguori
2009-07-16 14:10                                   ` Anthony Liguori
2009-07-16 15:13                                   ` Gerd Hoffmann
2009-07-16 15:13                                     ` Gerd Hoffmann
2009-07-16 15:12                                 ` Gerd Hoffmann
2009-07-16 15:12                                   ` Gerd Hoffmann
2009-07-15 15:34                           ` Kevin Wolf
2009-07-15 15:34                             ` Kevin Wolf
2009-07-15 15:41                             ` Anthony Liguori
2009-07-15 15:41                               ` Anthony Liguori
2009-07-15 15:52                           ` Paul Brook
2009-07-15 15:52                             ` Paul Brook
2009-07-15 16:03                           ` Gerd Hoffmann
2009-07-15 16:03                             ` Gerd Hoffmann
2009-07-15 16:08                             ` Paul Brook
2009-07-15 16:08                               ` Paul Brook
2009-07-16  7:39                         ` Ram Pai
2009-07-16  7:39                           ` Ram Pai
2009-07-16  7:43                           ` Kevin Wolf
2009-07-16  7:43                             ` Kevin Wolf
2009-07-15 18:14                   ` [Qemu-devel] [PATCH] rev3: " Jamie Lokier
2009-07-15 20:54                     ` Jan Kiszka
2009-07-15 21:36                       ` Jamie Lokier
2009-07-15 21:42                         ` Jan Kiszka
2009-07-15 22:00                           ` Jamie Lokier
2009-07-15 22:16                           ` Anthony Liguori
2009-07-15 22:16                             ` Anthony Liguori
2009-07-15 22:39                             ` Jamie Lokier
2009-07-15 22:39                               ` Jamie Lokier
2009-07-15 22:41                               ` Anthony Liguori
2009-07-15 22:41                                 ` Anthony Liguori
2009-07-15 22:51                                 ` Jamie Lokier
2009-07-15 22:51                                   ` Jamie Lokier
2009-07-16  0:03                                   ` Anthony Liguori
2009-07-16  0:03                                     ` Anthony Liguori
2009-07-16  7:20                                     ` Jan Kiszka
2009-07-16  7:20                                       ` Jan Kiszka
2009-07-16  7:16                             ` Jan Kiszka
2009-07-16  7:16                               ` Jan Kiszka
2009-07-16  8:01                     ` Kevin Wolf
2009-07-16 23:53                     ` Paul Brook
2009-07-16 23:53                       ` Paul Brook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1245862739.6278.7.camel@localhost \
    --to=linuxram@us.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.