diff mbox series

[15/21] libmultipath: update_bindings_file: use a single write()

Message ID 20230901180235.23980-16-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipath-tools: user-friendly names rework | expand

Commit Message

Martin Wilck Sept. 1, 2023, 6:02 p.m. UTC
From: Martin Wilck <mwilck@suse.com>

Save code and syscalls by assembling the content in memory first.
write() may return less bytes written than expected. Deal with it.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/alias.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

Comments

Benjamin Marzinski Sept. 6, 2023, 10:45 p.m. UTC | #1
On Fri, Sep 01, 2023 at 08:02:28PM +0200, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
> 
> Save code and syscalls by assembling the content in memory first.
> write() may return less bytes written than expected. Deal with it.
> 
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
>  libmultipath/alias.c | 26 +++++++++++++++++---------
>  1 file changed, 17 insertions(+), 9 deletions(-)
> 
> diff --git a/libmultipath/alias.c b/libmultipath/alias.c
> index 76d852f..c26f37c 100644
> --- a/libmultipath/alias.c
> +++ b/libmultipath/alias.c
> @@ -118,22 +118,30 @@ static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
>  static int write_bindings_file(const Bindings *bindings, int fd)
>  {
>  	struct binding *bnd;
> -	STRBUF_ON_STACK(line);
> +	STRBUF_ON_STACK(content);
>  	int i;
> +	size_t len;
>  
> -	if (write(fd, BINDINGS_FILE_HEADER, sizeof(BINDINGS_FILE_HEADER) - 1)
> -	    != sizeof(BINDINGS_FILE_HEADER) - 1)
> +	if (__append_strbuf_str(&content, BINDINGS_FILE_HEADER,
> +				sizeof(BINDINGS_FILE_HEADER) - 1) == -1)
>  		return -1;
>  
>  	vector_foreach_slot(bindings, bnd, i) {
> -		int len;
> +		if (print_strbuf(&content, "%s %s\n",
> +					bnd->alias, bnd->wwid) < 0)
> +			return -1;
> +	}
> +	len = get_strbuf_len(&content);
> +	while (len > 0) {
> +		ssize_t n = write(fd, get_strbuf_str(&content), len);
>  
> -		if ((len = print_strbuf(&line, "%s %s\n",
> -					bnd->alias, bnd->wwid)) < 0)
> +		if (n < 0)
> +			return n;
> +		else if (n == 0) {
> +			condlog(2, "%s: short write", __func__);
>  			return -1;
> -		if (write(fd, get_strbuf_str(&line), len) != len)
> -			return -1;
> -		truncate_strbuf(&line, 0);
> +		}
> +		len -= n;
>  	}
>  	return 0;
>  }
> -- 
> 2.41.0
--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel
diff mbox series

Patch

diff --git a/libmultipath/alias.c b/libmultipath/alias.c
index 76d852f..c26f37c 100644
--- a/libmultipath/alias.c
+++ b/libmultipath/alias.c
@@ -118,22 +118,30 @@  static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
 static int write_bindings_file(const Bindings *bindings, int fd)
 {
 	struct binding *bnd;
-	STRBUF_ON_STACK(line);
+	STRBUF_ON_STACK(content);
 	int i;
+	size_t len;
 
-	if (write(fd, BINDINGS_FILE_HEADER, sizeof(BINDINGS_FILE_HEADER) - 1)
-	    != sizeof(BINDINGS_FILE_HEADER) - 1)
+	if (__append_strbuf_str(&content, BINDINGS_FILE_HEADER,
+				sizeof(BINDINGS_FILE_HEADER) - 1) == -1)
 		return -1;
 
 	vector_foreach_slot(bindings, bnd, i) {
-		int len;
+		if (print_strbuf(&content, "%s %s\n",
+					bnd->alias, bnd->wwid) < 0)
+			return -1;
+	}
+	len = get_strbuf_len(&content);
+	while (len > 0) {
+		ssize_t n = write(fd, get_strbuf_str(&content), len);
 
-		if ((len = print_strbuf(&line, "%s %s\n",
-					bnd->alias, bnd->wwid)) < 0)
+		if (n < 0)
+			return n;
+		else if (n == 0) {
+			condlog(2, "%s: short write", __func__);
 			return -1;
-		if (write(fd, get_strbuf_str(&line), len) != len)
-			return -1;
-		truncate_strbuf(&line, 0);
+		}
+		len -= n;
 	}
 	return 0;
 }