diff mbox series

[U-Boot,1/2] tools: env: Refactor write path of flash_io()

Message ID 1520509926-15837-2-git-send-email-alex.kiernan@gmail.com
State Superseded
Delegated to: Tom Rini
Headers show
Series Add atomic write to fw_setenv for environments on filesystems | expand

Commit Message

Alex Kiernan March 8, 2018, 11:52 a.m. UTC
Extract write path of flash_io() into a separate function. This patch
should be a functional no-op.

Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---

 tools/env/fw_env.c | 98 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 53 insertions(+), 45 deletions(-)

Comments

Stefano Babic March 8, 2018, 3:24 p.m. UTC | #1
On 08/03/2018 12:52, Alex Kiernan wrote:
> Extract write path of flash_io() into a separate function. This patch
> should be a functional no-op.
> 
> Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
> ---
> 
>  tools/env/fw_env.c | 98 +++++++++++++++++++++++++++++-------------------------
>  1 file changed, 53 insertions(+), 45 deletions(-)
> 
> diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
> index 0e3e343..2df3504 100644
> --- a/tools/env/fw_env.c
> +++ b/tools/env/fw_env.c
> @@ -1229,68 +1229,76 @@ static int flash_read (int fd)
>  	return 0;
>  }
>  
> -static int flash_io (int mode)
> +static int flash_io_write (int fd_current)
>  {
> -	int fd_current, fd_target, rc, dev_target;
> +	int fd_target, rc, dev_target;
>  
> -	/* dev_current: fd_current, erase_current */
> -	fd_current = open (DEVNAME (dev_current), mode);
> -	if (fd_current < 0) {
> -		fprintf (stderr,
> -			 "Can't open %s: %s\n",
> -			 DEVNAME (dev_current), strerror (errno));
> -		return -1;
> +	if (HaveRedundEnv) {
> +		/* switch to next partition for writing */
> +		dev_target = !dev_current;
> +		/* dev_target: fd_target, erase_target */
> +		fd_target = open (DEVNAME (dev_target), O_RDWR);
> +		if (fd_target < 0) {
> +			fprintf (stderr,
> +				 "Can't open %s: %s\n",
> +				 DEVNAME (dev_target),
> +				 strerror (errno));
> +			rc = -1;
> +			goto exit;
> +		}
> +	} else {
> +		dev_target = dev_current;
> +		fd_target = fd_current;
>  	}
>  
> -	if (mode == O_RDWR) {
> -		if (HaveRedundEnv) {
> -			/* switch to next partition for writing */
> -			dev_target = !dev_current;
> -			/* dev_target: fd_target, erase_target */
> -			fd_target = open (DEVNAME (dev_target), mode);
> -			if (fd_target < 0) {
> -				fprintf (stderr,
> -					 "Can't open %s: %s\n",
> -					 DEVNAME (dev_target),
> -					 strerror (errno));
> -				rc = -1;
> -				goto exit;
> -			}
> -		} else {
> -			dev_target = dev_current;
> -			fd_target = fd_current;
> -		}
> +	rc = flash_write (fd_current, fd_target, dev_target);
>  
> -		rc = flash_write (fd_current, fd_target, dev_target);
> +	if (fsync(fd_current) &&
> +	    !(errno == EINVAL || errno == EROFS)) {
> +		fprintf (stderr,
> +			 "fsync failed on %s: %s\n",
> +			 DEVNAME (dev_current), strerror (errno));
> +	}
>  
> -		if (fsync(fd_current) &&
> +	if (HaveRedundEnv) {
> +		if (fsync(fd_target) &&
>  		    !(errno == EINVAL || errno == EROFS)) {
>  			fprintf (stderr,
>  				 "fsync failed on %s: %s\n",
>  				 DEVNAME (dev_current), strerror (errno));
>  		}
>  
> -		if (HaveRedundEnv) {
> -			if (fsync(fd_target) &&
> -			    !(errno == EINVAL || errno == EROFS)) {
> -				fprintf (stderr,
> -					 "fsync failed on %s: %s\n",
> -					 DEVNAME (dev_current), strerror (errno));
> -			}
> -
> -			if (close (fd_target)) {
> -				fprintf (stderr,
> -					"I/O error on %s: %s\n",
> -					DEVNAME (dev_target),
> -					strerror (errno));
> -				rc = -1;
> -			}
> +		if (close (fd_target)) {
> +			fprintf (stderr,
> +				 "I/O error on %s: %s\n",
> +				 DEVNAME (dev_target),
> +				 strerror (errno));
> +			rc = -1;
>  		}
> +	}
> +exit:
> +	return rc;
> +}
> +
> +static int flash_io (int mode)
> +{
> +	int fd_current, rc;
> +
> +	/* dev_current: fd_current, erase_current */
> +	fd_current = open (DEVNAME (dev_current), mode);
> +	if (fd_current < 0) {
> +		fprintf (stderr,
> +			 "Can't open %s: %s\n",
> +			 DEVNAME (dev_current), strerror (errno));
> +		return -1;
> +	}
> +
> +	if (mode == O_RDWR) {
> +		rc = flash_io_write(fd_current);
>  	} else {
>  		rc = flash_read (fd_current);
>  	}
>  
> -exit:
>  	if (close (fd_current)) {
>  		fprintf (stderr,
>  			 "I/O error on %s: %s\n",
> 

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 0e3e343..2df3504 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -1229,68 +1229,76 @@  static int flash_read (int fd)
 	return 0;
 }
 
-static int flash_io (int mode)
+static int flash_io_write (int fd_current)
 {
-	int fd_current, fd_target, rc, dev_target;
+	int fd_target, rc, dev_target;
 
-	/* dev_current: fd_current, erase_current */
-	fd_current = open (DEVNAME (dev_current), mode);
-	if (fd_current < 0) {
-		fprintf (stderr,
-			 "Can't open %s: %s\n",
-			 DEVNAME (dev_current), strerror (errno));
-		return -1;
+	if (HaveRedundEnv) {
+		/* switch to next partition for writing */
+		dev_target = !dev_current;
+		/* dev_target: fd_target, erase_target */
+		fd_target = open (DEVNAME (dev_target), O_RDWR);
+		if (fd_target < 0) {
+			fprintf (stderr,
+				 "Can't open %s: %s\n",
+				 DEVNAME (dev_target),
+				 strerror (errno));
+			rc = -1;
+			goto exit;
+		}
+	} else {
+		dev_target = dev_current;
+		fd_target = fd_current;
 	}
 
-	if (mode == O_RDWR) {
-		if (HaveRedundEnv) {
-			/* switch to next partition for writing */
-			dev_target = !dev_current;
-			/* dev_target: fd_target, erase_target */
-			fd_target = open (DEVNAME (dev_target), mode);
-			if (fd_target < 0) {
-				fprintf (stderr,
-					 "Can't open %s: %s\n",
-					 DEVNAME (dev_target),
-					 strerror (errno));
-				rc = -1;
-				goto exit;
-			}
-		} else {
-			dev_target = dev_current;
-			fd_target = fd_current;
-		}
+	rc = flash_write (fd_current, fd_target, dev_target);
 
-		rc = flash_write (fd_current, fd_target, dev_target);
+	if (fsync(fd_current) &&
+	    !(errno == EINVAL || errno == EROFS)) {
+		fprintf (stderr,
+			 "fsync failed on %s: %s\n",
+			 DEVNAME (dev_current), strerror (errno));
+	}
 
-		if (fsync(fd_current) &&
+	if (HaveRedundEnv) {
+		if (fsync(fd_target) &&
 		    !(errno == EINVAL || errno == EROFS)) {
 			fprintf (stderr,
 				 "fsync failed on %s: %s\n",
 				 DEVNAME (dev_current), strerror (errno));
 		}
 
-		if (HaveRedundEnv) {
-			if (fsync(fd_target) &&
-			    !(errno == EINVAL || errno == EROFS)) {
-				fprintf (stderr,
-					 "fsync failed on %s: %s\n",
-					 DEVNAME (dev_current), strerror (errno));
-			}
-
-			if (close (fd_target)) {
-				fprintf (stderr,
-					"I/O error on %s: %s\n",
-					DEVNAME (dev_target),
-					strerror (errno));
-				rc = -1;
-			}
+		if (close (fd_target)) {
+			fprintf (stderr,
+				 "I/O error on %s: %s\n",
+				 DEVNAME (dev_target),
+				 strerror (errno));
+			rc = -1;
 		}
+	}
+exit:
+	return rc;
+}
+
+static int flash_io (int mode)
+{
+	int fd_current, rc;
+
+	/* dev_current: fd_current, erase_current */
+	fd_current = open (DEVNAME (dev_current), mode);
+	if (fd_current < 0) {
+		fprintf (stderr,
+			 "Can't open %s: %s\n",
+			 DEVNAME (dev_current), strerror (errno));
+		return -1;
+	}
+
+	if (mode == O_RDWR) {
+		rc = flash_io_write(fd_current);
 	} else {
 		rc = flash_read (fd_current);
 	}
 
-exit:
 	if (close (fd_current)) {
 		fprintf (stderr,
 			 "I/O error on %s: %s\n",