From patchwork Tue Jan 26 10:38:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8120101 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 80EEEBEEE5 for ; Tue, 26 Jan 2016 10:44:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CCC4D20256 for ; Tue, 26 Jan 2016 10:44:00 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0BBF820166 for ; Tue, 26 Jan 2016 10:44:00 +0000 (UTC) Received: from localhost ([::1]:42955 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO16N-0006Nb-D6 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 26 Jan 2016 05:43:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40647) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO11x-0005I8-8X for qemu-devel@nongnu.org; Tue, 26 Jan 2016 05:39:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aO11s-0007L6-G3 for qemu-devel@nongnu.org; Tue, 26 Jan 2016 05:39:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48234) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO11l-0007Jj-0v; Tue, 26 Jan 2016 05:39:13 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id A1B6C42E5D6; Tue, 26 Jan 2016 10:39:12 +0000 (UTC) Received: from fam-t430.redhat.com (vpn1-5-65.pek2.redhat.com [10.72.5.65]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0QAcRTF016464; Tue, 26 Jan 2016 05:39:05 -0500 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 26 Jan 2016 18:38:13 +0800 Message-Id: <1453804705-7205-5-git-send-email-famz@redhat.com> In-Reply-To: <1453804705-7205-1-git-send-email-famz@redhat.com> References: <1453804705-7205-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, jsnow@redhat.com, Markus Armbruster , mreitz@redhat.com, vsementsov@parallels.com, Stefan Hajnoczi Subject: [Qemu-devel] [RFC PATCH 04/16] block: Move filename_decompose to block.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With the return value decoupled from VMDK, it can be reused by other block code. Signed-off-by: Fam Zheng Reviewed-by: Eric Blake Reviewed-by: John Snow --- block.c | 40 ++++++++++++++++++++++++++++++++++++++++ block/vmdk.c | 40 ---------------------------------------- include/block/block.h | 2 ++ 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/block.c b/block.c index fa6ad1d..78db342 100644 --- a/block.c +++ b/block.c @@ -144,6 +144,46 @@ int path_is_absolute(const char *path) #endif } +int filename_decompose(const char *filename, char *path, char *prefix, + char *postfix, size_t buf_len, Error **errp) +{ + const char *p, *q; + + if (filename == NULL || !strlen(filename)) { + error_setg(errp, "No filename provided"); + return -EINVAL; + } + p = strrchr(filename, '/'); + if (p == NULL) { + p = strrchr(filename, '\\'); + } + if (p == NULL) { + p = strrchr(filename, ':'); + } + if (p != NULL) { + p++; + if (p - filename >= buf_len) { + return -EINVAL; + } + pstrcpy(path, p - filename + 1, filename); + } else { + p = filename; + path[0] = '\0'; + } + q = strrchr(p, '.'); + if (q == NULL) { + pstrcpy(prefix, buf_len, p); + postfix[0] = '\0'; + } else { + if (q - p >= buf_len) { + return -EINVAL; + } + pstrcpy(prefix, q - p + 1, p); + pstrcpy(postfix, buf_len, q); + } + return 0; +} + /* if filename is absolute, just copy it to dest. Otherwise, build a path to it by considering it is relative to base_path. URL are supported. */ diff --git a/block/vmdk.c b/block/vmdk.c index f8f7fcf..505e0c2 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1764,46 +1764,6 @@ exit: return ret; } -static int filename_decompose(const char *filename, char *path, char *prefix, - char *postfix, size_t buf_len, Error **errp) -{ - const char *p, *q; - - if (filename == NULL || !strlen(filename)) { - error_setg(errp, "No filename provided"); - return VMDK_ERROR; - } - p = strrchr(filename, '/'); - if (p == NULL) { - p = strrchr(filename, '\\'); - } - if (p == NULL) { - p = strrchr(filename, ':'); - } - if (p != NULL) { - p++; - if (p - filename >= buf_len) { - return VMDK_ERROR; - } - pstrcpy(path, p - filename + 1, filename); - } else { - p = filename; - path[0] = '\0'; - } - q = strrchr(p, '.'); - if (q == NULL) { - pstrcpy(prefix, buf_len, p); - postfix[0] = '\0'; - } else { - if (q - p >= buf_len) { - return VMDK_ERROR; - } - pstrcpy(prefix, q - p + 1, p); - pstrcpy(postfix, buf_len, q); - } - return VMDK_OK; -} - static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp) { int idx = 0; diff --git a/include/block/block.h b/include/block/block.h index bfb76f8..b9b30cb 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -449,6 +449,8 @@ int bdrv_is_snapshot(BlockDriverState *bs); int path_has_protocol(const char *path); int path_is_absolute(const char *path); +int filename_decompose(const char *filename, char *path, char *prefix, + char *postfix, size_t buf_len, Error **errp); void path_combine(char *dest, int dest_size, const char *base_path, const char *filename);