From nobody Mon Feb 9 02:51:52 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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=1594151237; cv=none; d=zohomail.com; s=zohoarc; b=bGZmeym2a//B23pu/iTQy++Z6ZApFkqTaL34mtesnX5spWIyRkA2cFyecE+YzF+YPgCL37iqycVii50munJaD6NUtVZIFEU3Z1a2GhF5KXAmJLt7yOskxDEv+q5htOI2NjEuSgBXNVs5iqtweLplEn2I0H4OYLl5ntvcmCAy7s4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151237; 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=GJP+4yrEpdiBDkZUo9k3hGdF88hY+dXSA/iPni5FSJA=; b=Yd/OtxPp1IBXKtzGfrMLAPL81IxQMAV0UKdXmTQeW2JHlkDYHL3L3c6rFp+syHwjq7amW8ON03NxhaE3JUo2iZhDCiRm9UuEoOY3G+PgIe04b4hEg3nE7Fo5ndy94i6xADFBIloK9aNptENWJkYUns00izakUMg300+7OO3ENSM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1594151237986914.3131670785499; Tue, 7 Jul 2020 12:47:17 -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-513-_O8Szm8yMreegxwps-1lLw-1; Tue, 07 Jul 2020 15:47:14 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 15797EC1B2; Tue, 7 Jul 2020 19:47:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E9D2A78469; Tue, 7 Jul 2020 19:47:06 +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 850C31806B0D; Tue, 7 Jul 2020 19:47:06 +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 067Jkmih031504 for ; Tue, 7 Jul 2020 15:46:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 351ED797F0; Tue, 7 Jul 2020 19:46:48 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id A913F797EF for ; Tue, 7 Jul 2020 19:46:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151236; 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=GJP+4yrEpdiBDkZUo9k3hGdF88hY+dXSA/iPni5FSJA=; b=fNDkCQlsJYgUdkXye81zcnjAFCBDhBcOKFO5v2COAIYujI6UztskTt5LzMdC8+SRAwHgCd jMxrk8qs4AEOkWKzJ+9d0Z9EYAfsVFGSzD87JKMcgQ//fd/OZ3JDw9QChiUsuDzp/HxxzK gRDeqYZh48j23xx8aeknnsPg3vsTZDc= X-MC-Unique: _O8Szm8yMreegxwps-1lLw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 09/17] virfile: Introduce virFileInDataDetectZeroes() Date: Tue, 7 Jul 2020 21:46:27 +0200 Message-Id: <22acfe6aa6e2499cbb3de0b94c02ed6698afd301.1594150891.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.11 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..a35d9ccb7a 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 *bytes =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; + } + + bytes =3D g_new0(char, buflen); + + if ((r =3D saferead(fd, bytes, buflen)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to read from file")); + goto cleanup; + } + + *inData =3D !virStringIsNull(bytes, r); + *length =3D r; + 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