short i/o: fix calls to write to use xwrite or write_in_full
We have a number of badly checked write() calls. Often we are expecting write() to write exactly the size we requested or fail, this fails to handle interrupts or short writes. Switch to using the new write_in_full(). Otherwise we at a minimum need to check for EINTR and EAGAIN, where this is appropriate use xwrite(). Note, the changes to config handling are much larger and handled in the next patch in the sequence. Signed-off-by: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Andy Whitcroft committed
Jan 8, 2007 at 15:58 UTC
93822c2239a336e5cb583549071c59202ef6c5b2
20 files changed
+74
-89
builtin-rerere.c
+7
-4
@@ -51,9 +51,11 @@ static int write_rr(struct path_list *rr, int out_fd)
51
int i;
52
for (i = 0; i < rr->nr; i++) {
53
const char *path = rr->items[i].path;
54
- write(out_fd, rr->items[i].util, 40);
55
- write(out_fd, "\t", 1);
56
- write(out_fd, path, strlen(path) + 1);
54
+ int length = strlen(path) + 1;
55
+ if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
56
+ write_in_full(out_fd, "\t", 1) != 1 ||
57
+ write_in_full(out_fd, path, length) != length)
58
+ die("unable to write rerere record");
59
}
60
close(out_fd);
61
return commit_lock_file(&write_lock);
@@ -244,7 +246,8 @@ static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
246
{
247
int i;
248
for (i = 0; i < nbuf; i++)
247
- write(1, ptr[i].ptr, ptr[i].size);
249
+ if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
250
+ return -1;
251
return 0;
252
}
253
builtin-tar-tree.c
+1
-1
@@ -82,7 +82,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
82
if (memcmp(content, "52 comment=", 11))
83
return 1;
84
85
- n = xwrite(1, content + 11, 41);
85
+ n = write_in_full(1, content + 11, 41);
86
if (n < 41)
87
die("git-get-tar-commit-id: write error");
88
commit.c
+4
-2
@@ -249,8 +249,10 @@ int write_shallow_commits(int fd, int use_pack_protocol)
249
if (use_pack_protocol)
250
packet_write(fd, "shallow %s", hex);
251
else {
252
- write(fd, hex, 40);
253
- write(fd, "\n", 1);
252
+ if (write_in_full(fd, hex, 40) != 40)
253
+ break;
254
+ if (write_in_full(fd, "\n", 1) != 1)
255
+ break;
256
}
257
}
258
return count;
daemon.c
+1
-1
@@ -102,7 +102,7 @@ static void logreport(int priority, const char *err, va_list params)
102
buf[buflen++] = '\n';
103
buf[buflen] = '\0';
104
105
- write(2, buf, buflen);
105
+ write_in_full(2, buf, buflen);
106
}
107
108
static void logerror(const char *err, ...)
diff.c
+1
-1
@@ -1389,7 +1389,7 @@ static void prep_temp_blob(struct diff_tempfile *temp,
1389
fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1390
if (fd < 0)
1391
die("unable to create temp-file");
1392
- if (write(fd, blob, size) != size)
1392
+ if (write_in_full(fd, blob, size) != size)
1393
die("unable to write temp-file");
1394
close(fd);
1395
temp->name = temp->tmp_path;
entry.c
+2
-2
@@ -89,7 +89,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
89
return error("git-checkout-index: unable to create file %s (%s)",
90
path, strerror(errno));
91
}
92
- wrote = write(fd, new, size);
92
+ wrote = write_in_full(fd, new, size);
93
close(fd);
94
free(new);
95
if (wrote != size)
@@ -104,7 +104,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
104
return error("git-checkout-index: unable to create "
105
"file %s (%s)", path, strerror(errno));
106
}
107
- wrote = write(fd, new, size);
107
+ wrote = write_in_full(fd, new, size);
108
close(fd);
109
free(new);
110
if (wrote != size)
http-fetch.c
+1
-1
@@ -71,7 +71,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
71
int posn = 0;
72
struct object_request *obj_req = (struct object_request *)data;
73
do {
74
- ssize_t retval = write(obj_req->local,
74
+ ssize_t retval = xwrite(obj_req->local,
75
(char *) ptr + posn, size - posn);
76
if (retval < 0)
77
return posn;
http-push.c
+1
-1
@@ -195,7 +195,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
195
int posn = 0;
196
struct transfer_request *request = (struct transfer_request *)data;
197
do {
198
- ssize_t retval = write(request->local_fileno,
198
+ ssize_t retval = xwrite(request->local_fileno,
199
(char *) ptr + posn, size - posn);
200
if (retval < 0)
201
return posn;
imap-send.c
+1
-1
@@ -236,7 +236,7 @@ socket_read( Socket_t *sock, char *buf, int len )
236
static int
237
socket_write( Socket_t *sock, const char *buf, int len )
238
{
239
- int n = write( sock->fd, buf, len );
239
+ int n = write_in_full( sock->fd, buf, len );
240
if (n != len) {
241
socket_perror( "write", sock, n );
242
close( sock->fd );
index-pack.c
+1
-1
@@ -814,7 +814,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
814
char buf[48];
815
int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
816
report, sha1_to_hex(sha1));
817
- xwrite(1, buf, len);
817
+ write_in_full(1, buf, len);
818
819
/*
820
* Let's just mimic git-unpack-objects here and write
merge-recursive.c
+1
-1
@@ -517,7 +517,7 @@ static int mkdir_p(const char *path, unsigned long mode)
517
static void flush_buffer(int fd, const char *buf, unsigned long size)
518
{
519
while (size > 0) {
520
- long ret = xwrite(fd, buf, size);
520
+ long ret = write_in_full(fd, buf, size);
521
if (ret < 0) {
522
/* Ignore epipe */
523
if (errno == EPIPE)
read-cache.c
+3
-3
@@ -870,7 +870,7 @@ static int ce_write_flush(SHA_CTX *context, int fd)
870
unsigned int buffered = write_buffer_len;
871
if (buffered) {
872
SHA1_Update(context, write_buffer, buffered);
873
- if (write(fd, write_buffer, buffered) != buffered)
873
+ if (write_in_full(fd, write_buffer, buffered) != buffered)
874
return -1;
875
write_buffer_len = 0;
876
}
@@ -919,7 +919,7 @@ static int ce_flush(SHA_CTX *context, int fd)
919
920
/* Flush first if not enough space for SHA1 signature */
921
if (left + 20 > WRITE_BUFFER_SIZE) {
922
- if (write(fd, write_buffer, left) != left)
922
+ if (write_in_full(fd, write_buffer, left) != left)
923
return -1;
924
left = 0;
925
}
@@ -927,7 +927,7 @@ static int ce_flush(SHA_CTX *context, int fd)
927
/* Append the SHA1 signature at the end */
928
SHA1_Final(write_buffer + left, context);
929
left += 20;
930
- return (write(fd, write_buffer, left) != left) ? -1 : 0;
930
+ return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
931
}
932
933
static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
refs.c
+4
-4
@@ -332,7 +332,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master)
332
}
333
lockpath = mkpath("%s.lock", git_HEAD);
334
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
335
- written = write(fd, ref, len);
335
+ written = write_in_full(fd, ref, len);
336
close(fd);
337
if (written != len) {
338
unlink(lockpath);
@@ -968,7 +968,7 @@ static int log_ref_write(struct ref_lock *lock,
968
sha1_to_hex(sha1),
969
committer);
970
}
971
- written = len <= maxlen ? write(logfd, logrec, len) : -1;
971
+ written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
972
free(logrec);
973
close(logfd);
974
if (written != len)
@@ -987,8 +987,8 @@ int write_ref_sha1(struct ref_lock *lock,
987
unlock_ref(lock);
988
return 0;
989
}
990
- if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
991
- write(lock->lock_fd, &term, 1) != 1
990
+ if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
991
+ write_in_full(lock->lock_fd, &term, 1) != 1
992
|| close(lock->lock_fd) < 0) {
993
error("Couldn't write %s", lock->lk->filename);
994
unlock_ref(lock);
sha1_file.c
+6
-13
@@ -1611,20 +1611,13 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
1611
1612
static int write_buffer(int fd, const void *buf, size_t len)
1613
{
1614
- while (len) {
1615
- ssize_t size;
1614
+ ssize_t size;
1615
1617
- size = write(fd, buf, len);
1618
- if (!size)
1619
- return error("file write: disk full");
1620
- if (size < 0) {
1621
- if (errno == EINTR || errno == EAGAIN)
1622
- continue;
1623
- return error("file write error (%s)", strerror(errno));
1624
- }
1625
- len -= size;
1626
- buf = (char *) buf + size;
1627
- }
1616
+ size = write_in_full(fd, buf, len);
1617
+ if (!size)
1618
+ return error("file write: disk full");
1619
+ if (size < 0)
1620
+ return error("file write error (%s)", strerror(errno));
1621
return 0;
1622
}
1623
ssh-fetch.c
+11
-22
@@ -20,22 +20,6 @@ static int fd_out;
20
static unsigned char remote_version;
21
static unsigned char local_version = 1;
22
23
-static ssize_t force_write(int fd, void *buffer, size_t length)
24
-{
25
- ssize_t ret = 0;
26
- while (ret < length) {
27
- ssize_t size = write(fd, (char *) buffer + ret, length - ret);
28
- if (size < 0) {
29
- return size;
30
- }
31
- if (size == 0) {
32
- return ret;
33
- }
34
- ret += size;
35
- }
36
- return ret;
37
-}
38
-
23
static int prefetches;
24
25
static struct object_list *in_transit;
@@ -53,8 +37,9 @@ void prefetch(unsigned char *sha1)
37
node->item = lookup_unknown_object(sha1);
38
*end_of_transit = node;
39
end_of_transit = &node->next;
56
- force_write(fd_out, &type, 1);
57
- force_write(fd_out, sha1, 20);
40
+ /* XXX: what if these writes fail? */
41
+ write_in_full(fd_out, &type, 1);
42
+ write_in_full(fd_out, sha1, 20);
43
prefetches++;
44
}
45
@@ -97,8 +82,10 @@ int fetch(unsigned char *sha1)
82
static int get_version(void)
83
{
84
char type = 'v';
100
- write(fd_out, &type, 1);
101
- write(fd_out, &local_version, 1);
85
+ if (write_in_full(fd_out, &type, 1) != 1 ||
86
+ write_in_full(fd_out, &local_version, 1)) {
87
+ return error("Couldn't request version from remote end");
88
+ }
89
if (xread(fd_in, &remote_version, 1) < 1) {
90
return error("Couldn't read version from remote end");
91
}
@@ -109,8 +96,10 @@ int fetch_ref(char *ref, unsigned char *sha1)
96
{
97
signed char remote;
98
char type = 'r';
112
- write(fd_out, &type, 1);
113
- write(fd_out, ref, strlen(ref) + 1);
99
+ int length = strlen(ref) + 1;
100
+ if (write_in_full(fd_out, &type, 1) != 1 ||
101
+ write_in_full(fd_out, ref, length) != length)
102
+ return -1;
103
104
if (read_in_full(fd_in, &remote, 1) != 1)
105
return -1;
ssh-upload.c
+6
-4
@@ -41,7 +41,8 @@ static int serve_object(int fd_in, int fd_out) {
41
remote = -1;
42
}
43
44
- write(fd_out, &remote, 1);
44
+ if (write_in_full(fd_out, &remote, 1) != 1)
45
+ return 0;
46
47
if (remote < 0)
48
return 0;
@@ -53,7 +54,7 @@ static int serve_version(int fd_in, int fd_out)
54
{
55
if (xread(fd_in, &remote_version, 1) < 1)
56
return -1;
56
- write(fd_out, &local_version, 1);
57
+ write_in_full(fd_out, &local_version, 1);
58
return 0;
59
}
60
@@ -74,10 +75,11 @@ static int serve_ref(int fd_in, int fd_out)
75
76
if (get_ref_sha1(ref, sha1))
77
remote = -1;
77
- write(fd_out, &remote, 1);
78
+ if (write_in_full(fd_out, &remote, 1) != 1)
79
+ return 0;
80
if (remote)
81
return 0;
80
- write(fd_out, sha1, 20);
82
+ write_in_full(fd_out, sha1, 20);
83
return 0;
84
}
85
test-delta.c
+1
-1
@@ -68,7 +68,7 @@ int main(int argc, char *argv[])
68
}
69
70
fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
71
- if (fd < 0 || write(fd, out_buf, out_size) != out_size) {
71
+ if (fd < 0 || write_in_full(fd, out_buf, out_size) != out_size) {
72
perror(argv[4]);
73
return 1;
74
}
unpack-file.c
+1
-1
@@ -17,7 +17,7 @@ static char *create_temp_file(unsigned char *sha1)
17
fd = mkstemp(path);
18
if (fd < 0)
19
die("unable to create temp-file");
20
- if (write(fd, buf, size) != size)
20
+ if (write_in_full(fd, buf, size) != size)
21
die("unable to write temp-file");
22
close(fd);
23
return path;
upload-pack.c
+1
@@ -55,6 +55,7 @@ static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
55
/* emergency quit */
56
fd = 2;
57
if (fd == 2) {
58
+ /* XXX: are we happy to lose stuff here? */
59
xwrite(fd, data, sz);
60
return sz;
61
}
write_or_die.c
+20
-25
@@ -33,45 +33,40 @@ void read_or_die(int fd, void *buf, size_t count)
33
die("read error (%s)", strerror(errno));
34
}
35
36
-void write_or_die(int fd, const void *buf, size_t count)
36
+int write_in_full(int fd, const void *buf, size_t count)
37
{
38
const char *p = buf;
39
- ssize_t written;
39
+ ssize_t total = 0;
40
+ ssize_t written = 0;
41
42
while (count > 0) {
43
written = xwrite(fd, p, count);
43
- if (written == 0)
44
- die("disk full?");
45
- else if (written < 0) {
46
- if (errno == EPIPE)
47
- exit(0);
48
- die("write error (%s)", strerror(errno));
44
+ if (written <= 0) {
45
+ if (total)
46
+ return total;
47
+ else
48
+ return written;
49
}
50
count -= written;
51
p += written;
52
+ total += written;
53
}
54
+
55
+ return total;
56
}
57
55
-int write_in_full(int fd, const void *buf, size_t count)
58
+void write_or_die(int fd, const void *buf, size_t count)
59
{
57
- const char *p = buf;
58
- ssize_t total = 0;
59
- ssize_t wcount = 0;
60
+ ssize_t written;
61
61
- while (count > 0) {
62
- wcount = xwrite(fd, p, count);
63
- if (wcount <= 0) {
64
- if (total)
65
- return total;
66
- else
67
- return wcount;
68
- }
69
- count -= wcount;
70
- p += wcount;
71
- total += wcount;
62
+ written = write_in_full(fd, buf, count);
63
+ if (written == 0)
64
+ die("disk full?");
65
+ else if (written < 0) {
66
+ if (errno == EPIPE)
67
+ exit(0);
68
+ die("write error (%s)", strerror(errno));
69
}
73
-
74
- return wcount;
70
}
71
72
int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)