diff mbox

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

Message ID 1403142363-29482-3-git-send-email-guihc.fnst@cn.fujitsu.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Gui Hecheng June 19, 2014, 1:46 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>
---
 btrfs-image.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

David Sterba June 23, 2014, 2:15 p.m. UTC | #1
On Thu, Jun 19, 2014 at 09:46:02AM +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>
> ---
>  btrfs-image.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/btrfs-image.c b/btrfs-image.c
> index 549722c..b235b87 100644
> --- a/btrfs-image.c
> +++ b/btrfs-image.c
> @@ -2598,8 +2598,16 @@ int main(int argc, char *argv[])
>  out:
>  	if (out == stdout)
>  		fflush(out);
> -	else
> +	else {
>  		fclose(out);
> +		if (ret && create) {
> +			ret = unlink(target);

The previous value is overwritten and in case of unlink() success it's
returned as success of main(), which is not true. So a local variable
has to be used instead.

> +			if (ret)
> +				fprintf(stderr,
> +					"unlink output file failed : %s\n",
> +					strerror(errno));
> +		}
> +	}
>  
>  	return !!ret;
--
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
Gui Hecheng June 24, 2014, 1:41 a.m. UTC | #2
On Mon, 2014-06-23 at 16:15 +0200, David Sterba wrote:
> On Thu, Jun 19, 2014 at 09:46:02AM +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>
> > ---
> >  btrfs-image.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> > 
> > diff --git a/btrfs-image.c b/btrfs-image.c
> > index 549722c..b235b87 100644
> > --- a/btrfs-image.c
> > +++ b/btrfs-image.c
> > @@ -2598,8 +2598,16 @@ int main(int argc, char *argv[])
> >  out:
> >  	if (out == stdout)
> >  		fflush(out);
> > -	else
> > +	else {
> >  		fclose(out);
> > +		if (ret && create) {
> > +			ret = unlink(target);
> 
> The previous value is overwritten and in case of unlink() success it's
> returned as success of main(), which is not true. So a local variable
> has to be used instead.
> 
Yes, I'll rework & resend it soon. Thanks.

> > +			if (ret)
> > +				fprintf(stderr,
> > +					"unlink output file failed : %s\n",
> > +					strerror(errno));
> > +		}
> > +	}
> >  
> >  	return !!ret;


--
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
diff mbox

Patch

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