Skip to contentSkip to conversation

prefer xwrite instead of write

PATCH In review Erik Faye-Lund 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 prefer xwrite instead of write

Our xwrite wrapper already deals with a few potential hazards, and are as such more robust. Prefer it instead of write to get the robustness benefits everywhere.

---
With this patch, we don't call write directly any more; all calls goes via the xwrite-wrapper.

 builtin/merge.c    | 2 +-
 streaming.c        | 2 +-
 transport-helper.c | 5 ++---
 3 files changed, 4 insertions(+), 5 deletions(-)
builtin/merge.c +1 -1
index 4941a6c..9383c31 100644
@@ -367,7 +367,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
 			sha1_to_hex(commit->object.sha1));
 		pretty_print_commit(&ctx, commit, &out);
 	}
-	if (write(fd, out.buf, out.len) < 0)
+	if (xwrite(fd, out.buf, out.len) < 0)
 		die_errno(_("Writing SQUASH_MSG"));
 	if (close(fd))
 		die_errno(_("Finishing SQUASH_MSG"));
streaming.c +1 -1
index 9659f18..d7c9f32 100644
@@ -538,7 +538,7 @@ int stream_blob_to_fd(int fd, unsigned const char *sha1, struct stream_filter *f
 			goto close_and_exit;
 	}
 	if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
-		     write(fd, "", 1) != 1))
+		     xwrite(fd, "", 1) != 1))
 		goto close_and_exit;
 	result = 0;
 
transport-helper.c +2 -3
index 2010674..bf64ad7 100644
@@ -1129,9 +1129,8 @@ static int udt_do_write(struct unidirectional_transfer *t)
 		return 0;	/* Nothing to write. */
 
 	transfer_debug("%s is writable", t->dest_name);
-	bytes = write(t->dest, t->buf, t->bufuse);
-	if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
-		errno != EINTR) {
+	bytes = xwrite(t->dest, t->buf, t->bufuse);
+	if (bytes < 0 && errno != EWOULDBLOCK) {
 		error("write(%s) failed: %s", t->dest_name, strerror(errno));
 		return -1;
 	} else if (bytes > 0) {
Signature · 21 lines
1.8.4.msysgit.0

-- 
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#1 lore raw

PATCH 2/2 mingw: remove mingw_write

Since 0b6806b9 ("xread, xwrite: limit size of IO to 8MB"), this wrapper is no longer needed, as read and write are already split into small chunks.

---
 compat/mingw.c | 17 -----------------
 compat/mingw.h |  3 ---
 2 files changed, 20 deletions(-)
compat/mingw.c +0 -17
index fecb98b..e9892f8 100644
@@ -304,23 +304,6 @@ int mingw_open (const char *filename, int oflags, ...)
 	return fd;
 }
 
-#undef write
-ssize_t mingw_write(int fd, const void *buf, size_t count)
-{
-	/*
-	 * While write() calls to a file on a local disk are translated
-	 * into WriteFile() calls with a maximum size of 64KB on Windows
-	 * XP and 256KB on Vista, no such cap is placed on writes to
-	 * files over the network on Windows XP.  Unfortunately, there
-	 * seems to be a limit of 32MB-28KB on X64 and 64MB-32KB on x86;
-	 * bigger writes fail on Windows XP.
-	 * So we cap to a nice 31MB here to avoid write failures over
-	 * the net without changing the number of WriteFile() calls in
-	 * the local case.
-	 */
-	return write(fd, buf, min(count, 31 * 1024 * 1024));
-}
-
 static BOOL WINAPI ctrl_ignore(DWORD type)
 {
 	return TRUE;
compat/mingw.h +0 -3
index 92cd728..e033e72 100644
@@ -180,9 +180,6 @@ int mingw_rmdir(const char *path);
 int mingw_open (const char *filename, int oflags, ...);
 #define open mingw_open
 
-ssize_t mingw_write(int fd, const void *buf, size_t count);
-#define write mingw_write
-
 int mingw_fgetc(FILE *stream);
 #define fgetc mingw_fgetc
 
Signature · 21 lines
1.8.4.msysgit.0

-- 
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
Reviewed-by on v1 1/2
#2 lore raw
In reply to Erik Faye-Lund

Hi,

builtin/merge.c static void squash_message(struct commit *commit, struct commit_list *remotehead · line 371

Quoted 9 lines · Erik Faye-Lund
builtin/merge.c +1 -1
@@ -367,7 +367,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
 			sha1_to_hex(commit->object.sha1));
 		pretty_print_commit(&ctx, commit, &out);
 	}
-	if (write(fd, out.buf, out.len) < 0)
+	if (xwrite(fd, out.buf, out.len) < 0)
 		die_errno(_("Writing SQUASH_MSG"));

Shouldn't this use write_in_full() to avoid a silently truncated result? (*)

[...]

streaming.c int stream_blob_to_fd(int fd, unsigned const char *sha1, struct stream_filter *f · line 541

Quoted 8 lines
streaming.c +1 -1
@@ -538,7 +538,7 @@ int stream_blob_to_fd(int fd, unsigned const char *sha1, struct stream_filter *f
 			goto close_and_exit;
 	}
 	if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
-		     write(fd, "", 1) != 1))
+		     xwrite(fd, "", 1) != 1))

Yeah, if we get EINTR then it's worth retrying.

[...]

transport-helper.c static int udt_do_write(struct unidirectional_transfer *t) · line 1133

Quoted 11 lines
transport-helper.c +2 -3
@@ -1129,9 +1129,8 @@ static int udt_do_write(struct unidirectional_transfer *t)
 		return 0;	/* Nothing to write. */
 
 	transfer_debug("%s is writable", t->dest_name);
-	bytes = write(t->dest, t->buf, t->bufuse);
-	if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
-		errno != EINTR) {
+	bytes = xwrite(t->dest, t->buf, t->bufuse);
+	if (bytes < 0 && errno != EWOULDBLOCK) {

Here the write is limited by BUFFERSIZE, and returning to the outer loop to try another read when the write returns EAGAIN, like the original code does, seems philosophically like the right thing to do.

Luckily we don't use O_NONBLOCK anywhere, so the change shouldn't matter in practice. So although it doesn't do any good, using xwrite here for consistency should be fine.

So my only worry is the (*) above. With that change,

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#3 lore raw
Quoted 4 lines · Jonathan Nieder

Hi,

Erik Faye-Lund wrote:

Quoted 11 lines

builtin/merge.c static void squash_message(struct commit *commit, struct commit_list *remotehead · line 371

Quoted 9 lines
builtin/merge.c +1 -1
@@ -367,7 +367,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
 			sha1_to_hex(commit->object.sha1));
 		pretty_print_commit(&ctx, commit, &out);
 	}
-	if (write(fd, out.buf, out.len) < 0)
+	if (xwrite(fd, out.buf, out.len) < 0)
 		die_errno(_("Writing SQUASH_MSG"));

Shouldn't this use write_in_full() to avoid a silently truncated result? (*)

Meaning this? If so, I think it makes sense.

 builtin/merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/merge.c +1 -1
index 6e108d2..a6a38ee 100644
@@ -367,7 +367,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
 			sha1_to_hex(commit->object.sha1));
 		pretty_print_commit(&ctx, commit, &out);
 	}
-	if (xwrite(fd, out.buf, out.len) < 0)
+	if (write_in_full(fd, out.buf, out.len) != out.len)
 		die_errno(_("Writing SQUASH_MSG"));
 	if (close(fd))
 		die_errno(_("Finishing SQUASH_MSG"));
Quoted 38 lines

[...]

streaming.c int stream_blob_to_fd(int fd, unsigned const char *sha1, struct stream_filter *f · line 541

Quoted 8 lines
streaming.c +1 -1
@@ -538,7 +538,7 @@ int stream_blob_to_fd(int fd, unsigned const char *sha1, struct stream_filter *f
 			goto close_and_exit;
 	}
 	if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
-		     write(fd, "", 1) != 1))
+		     xwrite(fd, "", 1) != 1))

Yeah, if we get EINTR then it's worth retrying.

[...]

transport-helper.c static int udt_do_write(struct unidirectional_transfer *t) · line 1133

Quoted 11 lines
transport-helper.c +2 -3
@@ -1129,9 +1129,8 @@ static int udt_do_write(struct unidirectional_transfer *t)
 		return 0;	/* Nothing to write. */
 
 	transfer_debug("%s is writable", t->dest_name);
-	bytes = write(t->dest, t->buf, t->bufuse);
-	if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
-		errno != EINTR) {
+	bytes = xwrite(t->dest, t->buf, t->bufuse);
+	if (bytes < 0 && errno != EWOULDBLOCK) {

Here the write is limited by BUFFERSIZE, and returning to the outer loop to try another read when the write returns EAGAIN, like the original code does, seems philosophically like the right thing to do.

Luckily we don't use O_NONBLOCK anywhere, so the change shouldn't matter in practice. So although it doesn't do any good, using xwrite here for consistency should be fine.

So my only worry is the (*) above. With that change,

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#4 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 1 line · Jonathan Nieder

Shouldn't this use write_in_full() to avoid a silently truncated result? (*)

Meaning this? If so, I think it makes sense.

[...]

builtin/merge.c static void squash_message(struct commit *commit, struct commit_list *remotehead · line 370

Quoted 2 lines
builtin/merge.c +1 -1
-	if (xwrite(fd, out.buf, out.len) < 0)
+	if (write_in_full(fd, out.buf, out.len) != out.len)

Yes. Either '< 0' or '!= out.len' would work fine here, since write_in_full is defined to always either write the full 'count' bytes or return an error.

Thanks,
Jonathan

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#5 lore raw
Quoted 5 lines · Jonathan Nieder
Quoted 4 lines · Junio C Hamano
Quoted 1 line · Jonathan Nieder

Shouldn't this use write_in_full() to avoid a silently truncated result? (*)

Quoted 8 lines
Quoted 1 line

Meaning this? If so, I think it makes sense.

[...]

builtin/merge.c static void squash_message(struct commit *commit, struct commit_list *remotehead · line 370

Quoted 2 lines
builtin/merge.c +1 -1
-	if (xwrite(fd, out.buf, out.len) < 0)
+	if (write_in_full(fd, out.buf, out.len) != out.len)

Yes. Either '< 0' or '!= out.len' would work fine here, since write_in_full is defined to always either write the full 'count' bytes or return an error.

An unrelated tangent but we may want to fix majority of callers that do not seem to know that ;-)

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#6 lore raw
In reply to Junio C Hamano
Quoted 18 lines · Junio C Hamano
Quoted 4 lines · Jonathan Nieder

Hi,

Erik Faye-Lund wrote:

Quoted 11 lines

builtin/merge.c static void squash_message(struct commit *commit, struct commit_list *remotehead · line 371

Quoted 9 lines
builtin/merge.c +1 -1
@@ -367,7 +367,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
                     sha1_to_hex(commit->object.sha1));
             pretty_print_commit(&ctx, commit, &out);
     }
-    if (write(fd, out.buf, out.len) < 0)
+    if (xwrite(fd, out.buf, out.len) < 0)
             die_errno(_("Writing SQUASH_MSG"));

Shouldn't this use write_in_full() to avoid a silently truncated result? (*)

Quoted 2 lines

Meaning this? If so, I think it makes sense.

Yeah, I think that's better. Thanks, both!

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.