From patchwork Mon Nov 2 09:13:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Guangrong X-Patchwork-Id: 7534901 Return-Path: X-Original-To: patchwork-kvm@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 4D574BEEA4 for ; Mon, 2 Nov 2015 09:20:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 770E520591 for ; Mon, 2 Nov 2015 09:20:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5654720551 for ; Mon, 2 Nov 2015 09:20:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753477AbbKBJUF (ORCPT ); Mon, 2 Nov 2015 04:20:05 -0500 Received: from mga02.intel.com ([134.134.136.20]:20659 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751119AbbKBJTp (ORCPT ); Mon, 2 Nov 2015 04:19:45 -0500 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 02 Nov 2015 01:19:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,234,1444719600"; d="scan'208";a="840963116" Received: from xiaoreal1.sh.intel.com (HELO xiaoreal1.sh.intel.com.sh.intel.com) ([10.239.48.79]) by fmsmga002.fm.intel.com with ESMTP; 02 Nov 2015 01:19:28 -0800 From: Xiao Guangrong To: pbonzini@redhat.com, imammedo@redhat.com Cc: gleb@kernel.org, mtosatti@redhat.com, stefanha@redhat.com, mst@redhat.com, rth@twiddle.net, ehabkost@redhat.com, dan.j.williams@intel.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, vsementsov@virtuozzo.com, eblake@redhat.com, Xiao Guangrong Subject: [PATCH v7 11/35] util: introduce qemu_file_getlength() Date: Mon, 2 Nov 2015 17:13:13 +0800 Message-Id: <1446455617-129562-12-git-send-email-guangrong.xiao@linux.intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> References: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, 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 It is used to get the size of the specified file, also qemu_fd_getlength() is introduced to unify the code with raw_getlength() in block/raw-posix.c Signed-off-by: Xiao Guangrong Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/raw-posix.c | 7 +------ include/qemu/osdep.h | 2 ++ util/osdep.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index 918c756..734e6dd 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1592,18 +1592,13 @@ static int64_t raw_getlength(BlockDriverState *bs) { BDRVRawState *s = bs->opaque; int ret; - int64_t size; ret = fd_open(bs); if (ret < 0) { return ret; } - size = lseek(s->fd, 0, SEEK_END); - if (size < 0) { - return -errno; - } - return size; + return qemu_fd_getlength(s->fd); } #endif diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index dbc17dc..ca4c3fa 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -303,4 +303,6 @@ int qemu_read_password(char *buf, int buf_size); pid_t qemu_fork(Error **errp); size_t qemu_file_get_page_size(const char *mem_path, Error **errp); +int64_t qemu_fd_getlength(int fd); +size_t qemu_file_getlength(const char *file, Error **errp); #endif diff --git a/util/osdep.c b/util/osdep.c index 0092bb6..5a61e19 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -428,3 +428,34 @@ writev(int fd, const struct iovec *iov, int iov_cnt) return readv_writev(fd, iov, iov_cnt, true); } #endif + +int64_t qemu_fd_getlength(int fd) +{ + int64_t size; + + size = lseek(fd, 0, SEEK_END); + if (size < 0) { + return -errno; + } + return size; +} + +size_t qemu_file_getlength(const char *file, Error **errp) +{ + int64_t size; + int fd = qemu_open(file, O_RDONLY); + + if (fd < 0) { + error_setg_file_open(errp, errno, file); + return 0; + } + + size = qemu_fd_getlength(fd); + if (size < 0) { + error_setg_errno(errp, -size, "can't get size of file %s", file); + size = 0; + } + + qemu_close(fd); + return size; +}