From nobody Mon Feb 9 02:51:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593772330; cv=none; d=zohomail.com; s=zohoarc; b=DQG6fYjkCKRDjt70JkbKx2QOgaM4fd/sUBgxEeVbltpoHnmAzr+vgBmkqrpdrErmPrkUBKrEOEvhW9TTVQoQgBfXfqg/7bM8lH8azRqDh/eai5PMAzVsgBuhLmlGKUCCNanSYMb2iTzjPLL9AIG9RelwR3MC8O2KVlHP6sLbyz8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593772330; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/pvhm1s87vc/iYhopuxeQDEf85bjKWtbuvNobhYPiAQ=; b=GJ7oXAD45xDM6hV6cLgDWJuBYbnXeeCb1BRNim7HJc5xeflun+WJVh1P5ukV0sH36UZAm1sEADOh5MQlqdJAfwdO2zrGx/mNoIudsA2E30b7wBbKL29qWiMck8UmO6L65u1gMIBALwdh8/qoTwbv4ULVV31v0IDqyt8SEUtVnt8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1593772330612862.3329850148357; Fri, 3 Jul 2020 03:32:10 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-275-qYiStJR9NxanLz8pTqLXTQ-1; Fri, 03 Jul 2020 06:32:07 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 87A2D64AD0; Fri, 3 Jul 2020 10:32:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 565FA79242; Fri, 3 Jul 2020 10:32:00 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E20E06C9C7; Fri, 3 Jul 2020 10:31:55 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 063AT4Yn016974 for ; Fri, 3 Jul 2020 06:29:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4C72D7BD43; Fri, 3 Jul 2020 10:29:04 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.194.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id BFE0879242 for ; Fri, 3 Jul 2020 10:29:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593772329; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/pvhm1s87vc/iYhopuxeQDEf85bjKWtbuvNobhYPiAQ=; b=Lf4Wz2+oV6lkVuUmJ7MegCp4xu1Ll3pw6J6Q9nbqSPlcnG0P6LSh7CRDxawt0VD3Y+1fyJ in8DOo4qc0f8GJJV+ey0xrnMmFCp4SIsFot01005FUTWxzckGC3HG+E2EJjATMTznbrshQ Ez5O69vTJSAqn9m4AxYrnyVWcHN7iFY= X-MC-Unique: qYiStJR9NxanLz8pTqLXTQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/9] virfile: Introduce virFileInDataDetectZeroes() Date: Fri, 3 Jul 2020 12:28:44 +0200 Message-Id: <5e40db80ed2e4e1954955b6571748e02075319ef.1593771693.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" For given file descriptor determine if the current position it is in plus 1MiB (arbitrary chosen value) consists solely from zero bytes or not. This is a block device friendly version of virFileInData(). Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 67 ++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 4 +++ 3 files changed, 72 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1d80aeb833..6b5a751788 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2070,6 +2070,7 @@ virFileGetMountSubtree; virFileGetXAttr; virFileGetXAttrQuiet; virFileInData; +virFileInDataDetectZeroes; virFileIsCDROM; virFileIsDir; virFileIsExecutable; diff --git a/src/util/virfile.c b/src/util/virfile.c index c034df5931..18b5096b88 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4064,6 +4064,73 @@ virFileInData(int fd G_GNUC_UNUSED, #endif /* !HAVE_DECL_SEEK_HOLE */ =20 =20 +#define DETECT_ZEORES_BLOCK_SIZE (1 * 1024 * 1024) + +/* + * virFileInDataDetectZeroes: + * @fd: file to check + * @inData: true if current position in the @fd is in data section + * @length: amount of bytes until the end of the current section + * + * This behaves exactly like virFileInData() except it doesn't use SEEK_DA= TA + * and SEEK_HOLE rather than a buffer into which it reads data from @fd and + * detects if the buffer is full of zeroes. Therefore, it is safe to use on + * special files (e.g. block devices). On the other hand it sees only + * DETECT_ZEORES_BLOCK_SIZE ahead. + * + * Returns: 0 on success, + * -1 otherwise. + */ +int +virFileInDataDetectZeroes(int fd, + int *inData, + long long *length) +{ + const size_t buflen =3D DETECT_ZEORES_BLOCK_SIZE; + g_autofree char *buf =3D NULL; + off_t cur; + ssize_t r; + int ret =3D -1; + + /* Get current position */ + cur =3D lseek(fd, 0, SEEK_CUR); + if (cur =3D=3D (off_t) -1) { + virReportSystemError(errno, "%s", + _("Unable to get current position in file")); + goto cleanup; + } + + buf =3D g_new0(char, buflen); + + if ((r =3D saferead(fd, buf, buflen)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to read from file")); + goto cleanup; + } + + *inData =3D !virStringIsNull(buf, r); + *length =3D MIN(r, buflen); + ret =3D 0; + + cleanup: + /* At any rate, reposition back to where we started. */ + if (cur !=3D (off_t) -1) { + int theerrno =3D errno; + + if (lseek(fd, cur, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, "%s", + _("unable to restore position in file")); + ret =3D -1; + if (theerrno =3D=3D 0) + theerrno =3D errno; + } + + errno =3D theerrno; + } + return ret; +} + + /** * virFileReadValueInt: * @value: pointer to int to be filled in with the value diff --git a/src/util/virfile.h b/src/util/virfile.h index 7a92364a5c..9a5eade609 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -352,6 +352,10 @@ int virFileInData(int fd, int *inData, long long *length); =20 +int virFileInDataDetectZeroes(int fd, + int *inData, + long long *length); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(virFileWrapperFd, virFileWrapperFdFree); =20 int virFileGetXAttr(const char *path, --=20 2.26.2