From patchwork Thu Dec 9 11:53:00 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 75009 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 158ABB70A9 for ; Fri, 10 Dec 2010 10:09:45 +1100 (EST) Received: from localhost ([127.0.0.1]:60437 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQmj4-0003N4-Mh for incoming@patchwork.ozlabs.org; Thu, 09 Dec 2010 15:04:26 -0500 Received: from [140.186.70.92] (port=37880 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQf3c-0007vM-IB for qemu-devel@nongnu.org; Thu, 09 Dec 2010 06:53:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PQf3a-0001FA-Ti for qemu-devel@nongnu.org; Thu, 09 Dec 2010 06:53:08 -0500 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:35383) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PQf3a-0001ED-Kk for qemu-devel@nongnu.org; Thu, 09 Dec 2010 06:53:06 -0500 Received: from d06nrmr1307.portsmouth.uk.ibm.com (d06nrmr1307.portsmouth.uk.ibm.com [9.149.38.129]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id oB9Br2p4027853 for ; Thu, 9 Dec 2010 11:53:02 GMT Received: from d06av04.portsmouth.uk.ibm.com (d06av04.portsmouth.uk.ibm.com [9.149.37.216]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oB9Br3mE2601128 for ; Thu, 9 Dec 2010 11:53:03 GMT Received: from d06av04.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id oB9Br1jw009056 for ; Thu, 9 Dec 2010 04:53:01 -0700 Received: from stefanha-thinkpad.manchester-maybrook.uk.ibm.com (dyn-9-174-219-22.manchester-maybrook.uk.ibm.com [9.174.219.22]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id oB9Br1mR009044; Thu, 9 Dec 2010 04:53:01 -0700 From: Stefan Hajnoczi To: Date: Thu, 9 Dec 2010 11:53:00 +0000 Message-Id: <1291895580-2850-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.2.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Kevin Wolf , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2] block: Introduce path_has_protocol() function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- Sorry for this oversight, Windows breakage fixed. v2: * Define Windows drive functions before path_has_protocol() 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;