Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information...
commit d34cf19b8987ae0f0806e257edf877238d044747 1 parent 9bbaa6c
Linus Torvalds authored Junio C Hamano committed

Showing 2 changed files with 4 additions and 36 deletions. Show diff stats Hide diff stats

  1. +1 8 sha1_file.c
  2. +3 28 write_or_die.c
9 sha1_file.c
@@ -1618,14 +1618,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
1618 1618
1619 1619 static int write_buffer(int fd, const void *buf, size_t len)
1620 1620 {
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)
1629 1622 return error("file write error (%s)", strerror(errno));
1630 1623 return 0;
1631 1624 }
31 write_or_die.c
@@ -58,14 +58,7 @@ int write_in_full(int fd, const void *buf, size_t count)
58 58
59 59 void write_or_die(int fd, const void *buf, size_t count)
60 60 {
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) {
69 62 if (errno == EPIPE)
70 63 exit(0);
71 64 die("write error (%s)", strerror(errno));
@@ -74,16 +67,7 @@ void write_or_die(int fd, const void *buf, size_t count)
74 67
75 68 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
76 69 {
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) {
87 71 if (errno == EPIPE)
88 72 exit(0);
89 73 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)
96 80
97 81 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
98 82 {
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) {
109 84 fprintf(stderr, "%s: write error (%s)\n",
110 85 msg, strerror(errno));
111 86 return 0;

0 comments on commit d34cf19

Please sign in to comment.
Something went wrong with that request. Please try again.