From patchwork Fri Jun 26 06:38:04 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 32517 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n5Q6cCBf017216 for ; Fri, 26 Jun 2009 06:38:13 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751034AbZFZGiG (ORCPT ); Fri, 26 Jun 2009 02:38:06 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751359AbZFZGiF (ORCPT ); Fri, 26 Jun 2009 02:38:05 -0400 Received: from e37.co.us.ibm.com ([32.97.110.158]:45314 "EHLO e37.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751005AbZFZGiD (ORCPT ); Fri, 26 Jun 2009 02:38:03 -0400 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e37.co.us.ibm.com (8.13.1/8.13.1) with ESMTP id n5Q6bNJn001686 for ; Fri, 26 Jun 2009 00:37:23 -0600 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.2) with ESMTP id n5Q6c6b8253534 for ; Fri, 26 Jun 2009 00:38:06 -0600 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n5Q6c5jB024588 for ; Fri, 26 Jun 2009 00:38:06 -0600 Received: from [9.65.27.211] (sig-9-65-27-211.mts.ibm.com [9.65.27.211]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n5Q6c4Kc024542; Fri, 26 Jun 2009 00:38:05 -0600 Subject: rev1 [PATCH] support colon in filenames From: Ram Pai Reply-To: linuxram@us.ibm.com To: kvm-devel , qemu-devel@nongnu.org Cc: Anthony Liguori , Kevin Wolf In-Reply-To: <4A434009.5010009@redhat.com> References: <1245862739.6278.7.camel@localhost> <1245866233.6278.17.camel@localhost> <4A434009.5010009@redhat.com> Organization: IBM Corporation Date: Thu, 25 Jun 2009 23:38:04 -0700 Message-Id: <1245998284.6278.99.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.24.3 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 user to espace colon characters. For example the above filename can now be expressed either as 'scsi\:0' or as file:scsi:0 anything following the "file:" tag is interpreted verbatim. However if "file:" tag is omitted then any colon characters in the string must be escaped using backslash. Here are couple of examples: scsi\:0\:abc is a local file scsi:0:abc http\://myweb is a local file by name http://myweb file:scsi:0:abc is a local file scsi:0:abc file:http://myweb is a local file by name http://myweb Changelog w.r.t to iteration 1: 1) removes flexibility added to nbd semantics eg -- nbd:\::9999 2) introduce the "file:" protocol to indicate local file NOTE: no code changes are needed to handle commas in a filename. As always a comma has to be escaped by a preceding comma. Signed-off-by: Ram Pai ----------------------------------------------------------------------- block.c | 16 ++++++---------- block/raw-posix.c | 30 +++++++++++++++++++++++------- cutils.c | 26 ++++++++++++++++++++++++++ qemu-common.h | 1 + 4 files changed, 56 insertions(+), 17 deletions(-) --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/block.c b/block.c index aca5a6d..0064e22 100644 --- a/block.c +++ b/block.c @@ -225,22 +225,18 @@ static BlockDriver *find_protocol(const char *filename) { BlockDriver *drv1; char protocol[128]; - int len; - const char *p; + const char *f; + 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) - return bdrv_find_format("raw"); - len = p - filename; - if (len > sizeof(protocol) - 1) - len = sizeof(protocol) - 1; - memcpy(protocol, filename, len); - protocol[len] = '\0'; + f = fill_token(protocol, len, filename, ':'); + if (*f != ':' || !strcmp(protocol, "file")) + return bdrv_find_format("raw"); + for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) diff --git a/block/raw-posix.c b/block/raw-posix.c index 41bfa37..03d6581 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -124,6 +124,22 @@ 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]; + const char *f; + va_list ap; + va_start(ap, flags); + + if (!strstart(filename, "file:", &f)) { + fill_token(myfile, PATH_MAX, filename, '\0'); + return open(myfile, flags, ap); + } else { + return open(f, flags, ap); + } +} + + static int raw_open_common(BlockDriverState *bs, const char *filename, int flags) { @@ -151,7 +167,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 +860,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 +1001,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 +1053,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 +1149,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 +1255,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 +1415,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..f6d5bf5 100644 --- a/cutils.c +++ b/cutils.c @@ -24,6 +24,32 @@ #include "qemu-common.h" #include "host-utils.h" +/* + * fill buffer 'buff' with the contents of the string 'str' delimited by + * the character 'c'. If delimiter not found in 'len' bytes of the string + * assume '\0' as the default delimiter. + * Return pointer to the delimiting character + */ +const char *fill_token(char *buf, int len, const char *str, char c) +{ + const char *p=str; + char *q=buf; + + while (p < str+len) { + if (*p == c) + break; + if (*p == '\\' ) { + p++; + if (*p == '\0') + break; + } + *q++ = *p++; + } + *q='\0'; + return p; +} + + void pstrcpy(char *buf, int buf_size, const char *str) { int c; diff --git a/qemu-common.h b/qemu-common.h index 2dcb224..8916502 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 */ +const char *fill_token(char *buf, int buf_size, const char *str, char); 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);