From nobody Mon Nov 10 04:24:03 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553635496420392.9378414539634; Tue, 26 Mar 2019 14:24:56 -0700 (PDT) Received: from localhost ([127.0.0.1]:38152 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tYz-0004Wb-EI for importer@patchew.org; Tue, 26 Mar 2019 17:24:53 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tV7-0001OX-CS for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:20:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8tSG-00016E-Jj for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:17:57 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:33831) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8tSG-000160-Ef; Tue, 26 Mar 2019 17:17:56 -0400 Received: by mail-qt1-x844.google.com with SMTP id k2so16479931qtm.1; Tue, 26 Mar 2019 14:17:56 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:6d41:b488:ffd7:8d35:4aac]) by smtp.gmail.com with ESMTPSA id d21sm11735060qtc.91.2019.03.26.14.17.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 26 Mar 2019 14:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aAMblIhkyZ3K7eFe/YT7i45tLeR3QjU4K9ZXpZYwYlo=; b=VxpC1SmuARzSnXkiL/CbZfQGuIIQVipsrz1HZKCl+o4aUxFvl+kymnEtevE4JFOGVO bxBW17aVo10pOtReFf0IX/5C82W6glzgq4yVtGR80J44TzONvQusjGOoxCNJEyM34hBA /KAizwiWcF86ym0YL7Edrrm11EIkrEyEUL2j5oaxNk2CzNmBbsrIZVmJMZ/gj8w+jwgo djpFD6acMvefgM7gzPdHm0lzXe4X3g16+OLmy9aEL/sIn9B4EYmw9v9cgOrGQXAPxvC5 9WxyB9dWlZhNu4C/IjVcV/iJRBaBSqAYujgYw8LFyH6UK0PooY3z4cXYjdF71i83zikg UI2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aAMblIhkyZ3K7eFe/YT7i45tLeR3QjU4K9ZXpZYwYlo=; b=aHQetLMspOUT5V5hpa9UpoWijOCV7obhi3X1J5F78SnXcxwsg5z4CndcBsIfJPDAd4 7n4KerxmK9VU9OOWihmlso2ty3xiESnnHFQGljBMtoQAeGmGYyB9FtTwpVFXk7mKiDZq AMVchUzZIfs3wgnlMdnNzE43D88szbGKk8ouiWzsFwkQsnG/2LuORENggfMpUrBKbTio NOfrthlfzvrrcpg1VK1ZOjwLixPeWPIlPvK2ODxsKexNHOLVg7uMYQZXIBwWJtdKD1px DuZiZcnPGi0/MO3V9cqIu2GYYPrNyUepbK6io0ZNLYMfgQTV/j5ic2T95244zZczp+J+ aLtg== X-Gm-Message-State: APjAAAXrJmevisLtIBaiFa9FTP1u02nrzhRe892GyctRIoLTh5LWwqvQ 58AaoA+PIzFkCB7k4HttjvIMaQAwyog= X-Google-Smtp-Source: APXvYqwNoKyNGJUhli9zt+MM2ZTXVznm1gpa11k1+21hGVkPmzwLaQSEVhM3erqQhiYp567+Xhm3nw== X-Received: by 2002:a0c:9524:: with SMTP id l33mr26890289qvl.41.1553635075826; Tue, 26 Mar 2019 14:17:55 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 26 Mar 2019 18:17:40 -0300 Message-Id: <20190326211742.26140-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190326211742.26140-1-danielhb413@gmail.com> References: <20190326211742.26140-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::844 Subject: [Qemu-devel] [PATCH v3 1/3] block: introducing 'bdrv_co_delete_file' interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Daniel Henrique Barboza , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the 'file' driver in file-posix.c. The implementation is given by 'raw_co_delete_file'. The helper 'bdrv_path_is_regular_file' is being used only in raw_co_delete_file at this moment, but it will be used inside LUKS in a later patch. Foreseeing this future use, let's put it in block.c and make it public. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 11 +++++++++++ block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block.h | 1 + include/block/block_int.h | 6 ++++++ 4 files changed, 46 insertions(+) diff --git a/block.c b/block.c index 0a93ee9ac8..227362b282 100644 --- a/block.c +++ b/block.c @@ -621,6 +621,17 @@ int get_tmp_filename(char *filename, int size) #endif } =20 +/** + * Helper that checks if a given string represents a regular + * local file. + */ +bool bdrv_path_is_regular_file(const char *path) +{ + struct stat st; + + return (stat(path, &st) =3D=3D 0) && S_ISREG(st.st_mode); +} + /* * Detect host devices. By convention, /dev/cdrom[N] is always * recognized as a host CDROM. diff --git a/block/file-posix.c b/block/file-posix.c index d102f3b222..09d84bab37 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2342,6 +2342,33 @@ static int coroutine_fn raw_co_create_opts(const cha= r *filename, QemuOpts *opts, return raw_co_create(&options, errp); } =20 +/** + * Co-routine function that erases a regular file. + */ +static int coroutine_fn raw_co_delete_file(const char *filename, + Error **errp) +{ + int ret; + + /* Skip file: protocol prefix */ + strstart(filename, "file:", &filename); + + if (!bdrv_path_is_regular_file(filename)) { + ret =3D -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", filename); + goto done; + } + + ret =3D unlink(filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", filena= me); + } + +done: + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2867,6 +2894,7 @@ BlockDriver bdrv_file =3D { .bdrv_co_block_status =3D raw_co_block_status, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes =3D raw_co_pwrite_zeroes, + .bdrv_co_delete_file =3D raw_co_delete_file, =20 .bdrv_co_preadv =3D raw_co_preadv, .bdrv_co_pwritev =3D raw_co_pwritev, diff --git a/include/block/block.h b/include/block/block.h index e452988b66..820643f96d 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -363,6 +363,7 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, Blo= ckDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); =20 +bool bdrv_path_is_regular_file(const char *path); =20 typedef struct BdrvCheckResult { int corruptions; diff --git a/include/block/block_int.h b/include/block/block_int.h index 01e855a066..74abb78ce7 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -309,6 +309,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(const char *filename, + Error **errp); + /* * Flushes all data that was already written to the OS all the way dow= n to * the disk (for example file-posix.c calls fsync()). --=20 2.20.1