From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=52290 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PTeMO-0001hN-A3 for qemu-devel@nongnu.org; Fri, 17 Dec 2010 12:46:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PTeLN-0002pb-1S for qemu-devel@nongnu.org; Fri, 17 Dec 2010 12:44:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42546) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PTeLM-0002pS-Nf for qemu-devel@nongnu.org; Fri, 17 Dec 2010 12:43:48 -0500 From: Kevin Wolf Date: Fri, 17 Dec 2010 18:44:17 +0100 Message-Id: <1292607893-13461-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1292607893-13461-1-git-send-email-kwolf@redhat.com> References: <1292607893-13461-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 02/38] block: Introduce path_has_protocol() function List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: anthony@codemonkey.ws Cc: kwolf@redhat.com, qemu-devel@nongnu.org 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 --- block.c | 64 +++++++++++++++++++++++++++++++++++--------------------------- 1 files changed, 36 insertions(+), 28 deletions(-) diff --git a/block.c b/block.c index e7a986c..65fce80 100644 --- a/block.c +++ b/block.c @@ -70,6 +70,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; @@ -244,26 +277,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. @@ -307,16 +320,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.2.3