Skip to content

Commit d34cf19

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Clean up write_in_full() users
With the new-and-improved write_in_full() semantics, where a partial write simply always returns a real error (and always sets 'errno' when that happens, including for the disk full case), a lot of the callers of write_in_full() were just unnecessarily complex. In particular, there's no reason to ever check for a zero length or return: if the length was zero, we'll return zero, otherwise, if a disk full resulted in the actual write() system call returning zero the write_in_full() logic would have correctly turned that into a negative return value, with 'errno' set to ENOSPC. I really wish every "write_in_full()" user would just check against "<0" now, but this fixes the nasty and stupid ones. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bbaa6c commit d34cf19

File tree

2 files changed

+4
-36
lines changed

2 files changed

+4
-36
lines changed

sha1_file.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,14 +1618,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
16181618

16191619
static int write_buffer(int fd, const void *buf, size_t len)
16201620
{
1621-
ssize_t size;
1622-
1623-
if (!len)
1624-
return 0;
1625-
size = write_in_full(fd, buf, len);
1626-
if (!size)
1627-
return error("file write: disk full");
1628-
if (size < 0)
1621+
if (write_in_full(fd, buf, len) < 0)
16291622
return error("file write error (%s)", strerror(errno));
16301623
return 0;
16311624
}

write_or_die.c

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,7 @@ int write_in_full(int fd, const void *buf, size_t count)
5858

5959
void write_or_die(int fd, const void *buf, size_t count)
6060
{
61-
ssize_t written;
62-
63-
if (!count)
64-
return;
65-
written = write_in_full(fd, buf, count);
66-
if (written == 0)
67-
die("disk full?");
68-
else if (written < 0) {
61+
if (write_in_full(fd, buf, count) < 0) {
6962
if (errno == EPIPE)
7063
exit(0);
7164
die("write error (%s)", strerror(errno));
@@ -74,16 +67,7 @@ void write_or_die(int fd, const void *buf, size_t count)
7467

7568
int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
7669
{
77-
ssize_t written;
78-
79-
if (!count)
80-
return 1;
81-
written = write_in_full(fd, buf, count);
82-
if (written == 0) {
83-
fprintf(stderr, "%s: disk full?\n", msg);
84-
return 0;
85-
}
86-
else if (written < 0) {
70+
if (write_in_full(fd, buf, count) < 0) {
8771
if (errno == EPIPE)
8872
exit(0);
8973
fprintf(stderr, "%s: write error (%s)\n",
@@ -96,16 +80,7 @@ int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
9680

9781
int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
9882
{
99-
ssize_t written;
100-
101-
if (!count)
102-
return 1;
103-
written = write_in_full(fd, buf, count);
104-
if (written == 0) {
105-
fprintf(stderr, "%s: disk full?\n", msg);
106-
return 0;
107-
}
108-
else if (written < 0) {
83+
if (write_in_full(fd, buf, count) < 0) {
10984
fprintf(stderr, "%s: write error (%s)\n",
11085
msg, strerror(errno));
11186
return 0;

0 commit comments

Comments
 (0)