[v3,3/4] btrfs-progs: delete invalid output file when btrfs-image failed
diff mbox

Message ID 1403579805-7832-1-git-send-email-guihc.fnst@cn.fujitsu.com
State Accepted
Headers show

Commit Message

Gui Hecheng June 24, 2014, 3:16 a.m. UTC
When btrfs-image failed to create an image, the invalid output file
had better be deleted to prevent being used mistakenly in the future.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
---
changelog
        v1->v2: use a new local variable to avoid return value overwritten
	v2->v3: fix patch format problem: tailing whitespace
---
 btrfs-image.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

David Sterba June 24, 2014, 8:47 a.m. UTC | #1
On Tue, Jun 24, 2014 at 11:16:45AM +0800, Gui Hecheng wrote:
> When btrfs-image failed to create an image, the invalid output file
> had better be deleted to prevent being used mistakenly in the future.
> 
> Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
> ---
> changelog
>         v1->v2: use a new local variable to avoid return value overwritten
> 	v2->v3: fix patch format problem: tailing whitespace

Thanks, both v3 patches added to integration.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/btrfs-image.c b/btrfs-image.c
index 9e5b8b3..1a3e6ca 100644
--- a/btrfs-image.c
+++ b/btrfs-image.c
@@ -2608,8 +2608,18 @@  int main(int argc, char *argv[])
 out:
 	if (out == stdout)
 		fflush(out);
-	else
+	else {
 		fclose(out);
+		if (ret && create) {
+			int unlink_ret;
+
+			unlink_ret = unlink(target);
+			if (unlink_ret)
+				fprintf(stderr,
+					"unlink output file failed : %s\n",
+					strerror(errno));
+		}
+	}
 
 	return !!ret;
 }