From 46804f6c141e96eb9a0dc5a65f6dfe2b75ee8451 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 7 May 2012 15:01:55 -0300 Subject: [RHEL6 qemu-kvm PATCH 2/6] block: Introduce path_has_protocol() function RH-Author: Paolo Bonzini Message-id: <1336402919-26889-2-git-send-email-pbonzini@redhat.com> Patchwork-id: 39612 O-Subject: [RHEL 6.3 qemu-kvm PATCH 1/5] block: Introduce path_has_protocol() function Bugzilla: 818876 RH-Acked-by: Kevin Wolf RH-Acked-by: Laszlo Ersek RH-Acked-by: Eduardo Habkost From: Stefan Hajnoczi The bdrv_find_protocol() function returns NULL if an unknown protocol name is given. It returns the "file" protocol when the filename contains no protocol at all. This makes it difficult to distinguish between paths which contain a protocol and those which do not. Factor out a helper function that tests whether or not a filename has a protocol. The next patch makes use of this function. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf (cherry-pick from upstream commit 9e0b22f4f2c4122bb3544bb44fb7ce2fd7964c12) Conflicts: block.c (gratuitous whitespace differences) --- block.c | 63 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 27 deletions(-) Signed-off-by: Eduardo Habkost --- block.c | 63 ++++++++++++++++++++++++++++++++++++--------------------------- 1 files changed, 36 insertions(+), 27 deletions(-) diff --git a/block.c b/block.c index e05a9c6..ae6c228 100644 --- a/block.c +++ b/block.c @@ -91,6 +91,39 @@ static BlockDriverState *bs_snapshots; /* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist; +#ifdef _WIN32 +static int is_windows_drive_prefix(const char *filename) +{ + return (((filename[0] >= 'a' && filename[0] <= 'z') || + (filename[0] >= 'A' && filename[0] <= 'Z')) && + filename[1] == ':'); +} + +int is_windows_drive(const char *filename) +{ + if (is_windows_drive_prefix(filename) && + filename[2] == '\0') + return 1; + if (strstart(filename, "\\\\.\\", NULL) || + strstart(filename, "//./", NULL)) + return 1; + return 0; +} +#endif + +/* check if the path starts with ":" */ +static int path_has_protocol(const char *path) +{ +#ifdef _WIN32 + if (is_windows_drive(path) || + is_windows_drive_prefix(path)) { + return 0; + } +#endif + + return strchr(path, ':') != NULL; +} + int path_is_absolute(const char *path) { const char *p; @@ -268,26 +301,6 @@ void get_tmp_filename(char *filename, int size) } #endif -#ifdef _WIN32 -static int is_windows_drive_prefix(const char *filename) -{ - return (((filename[0] >= 'a' && filename[0] <= 'z') || - (filename[0] >= 'A' && filename[0] <= 'Z')) && - filename[1] == ':'); -} - -int is_windows_drive(const char *filename) -{ - if (is_windows_drive_prefix(filename) && - filename[2] == '\0') - return 1; - if (strstart(filename, "\\\\.\\", NULL) || - strstart(filename, "//./", NULL)) - return 1; - return 0; -} -#endif - /* * Detect host devices. By convention, /dev/cdrom[N] is always * recognized as a host CDROM. @@ -331,15 +344,11 @@ BlockDriver *bdrv_find_protocol(const char *filename) return drv1; } -#ifdef _WIN32 - if (is_windows_drive(filename) || - is_windows_drive_prefix(filename)) - return bdrv_find_format("file"); -#endif - p = strchr(filename, ':'); - if (!p) { + if (!path_has_protocol(filename)) { return bdrv_find_format("file"); } + p = strchr(filename, ':'); + assert(p != NULL); len = p - filename; if (len > sizeof(protocol) - 1) len = sizeof(protocol) - 1; -- 1.7.3.2