short i/o: clean up the naming for the write_{in,or}_xxx family
We recently introduced a write_in_full() which would either write the specified object or emit an error message and fail. In order to fix the read side we now want to introduce a read_in_full() but without an error emit. This patch cleans up the naming of this family of calls: 1) convert the existing write_or_whine() to write_or_whine_pipe() to better indicate its pipe specific nature, 2) convert the existing write_in_full() calls to write_or_whine() to better indicate its nature, 3) introduce a write_in_full() providing a write or fail semantic, and 4) convert write_or_whine() and write_or_whine_pipe() to use write_in_full(). 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:57 UTC
e08140568a131bcd26e64a0bc8188040847be998
4 files changed
+48
-35
cache.h
+2
-1
@@ -433,9 +433,10 @@ extern char *git_log_output_encoding;
433
434
extern int copy_fd(int ifd, int ofd);
435
extern void read_or_die(int fd, void *buf, size_t count);
436
-extern int write_in_full(int fd, const void *buf, size_t count, const char *);
436
+extern int write_in_full(int fd, const void *buf, size_t count);
437
extern void write_or_die(int fd, const void *buf, size_t count);
438
extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
439
+extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);
440
441
/* pager.c */
442
extern void setup_pager(void);
send-pack.c
+2
-2
@@ -65,14 +65,14 @@ static int pack_objects(int fd, struct ref *refs)
65
memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
66
buf[0] = '^';
67
buf[41] = '\n';
68
- if (!write_in_full(pipe_fd[1], buf, 42,
68
+ if (!write_or_whine(pipe_fd[1], buf, 42,
69
"send-pack: send refs"))
70
break;
71
}
72
if (!is_null_sha1(refs->new_sha1)) {
73
memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
74
buf[40] = '\n';
75
- if (!write_in_full(pipe_fd[1], buf, 41,
75
+ if (!write_or_whine(pipe_fd[1], buf, 41,
76
"send-pack: send refs"))
77
break;
78
}
trace.c
+2
-2
@@ -101,7 +101,7 @@ void trace_printf(const char *format, ...)
101
nfvasprintf(&trace_str, format, rest);
102
va_end(rest);
103
104
- write_or_whine(fd, trace_str, strlen(trace_str), err_msg);
104
+ write_or_whine_pipe(fd, trace_str, strlen(trace_str), err_msg);
105
106
free(trace_str);
107
@@ -139,7 +139,7 @@ void trace_argv_printf(const char **argv, int count, const char *format, ...)
139
strncpy(trace_str + format_len, argv_str, argv_len);
140
strcpy(trace_str + trace_len - 1, "\n");
141
142
- write_or_whine(fd, trace_str, trace_len, err_msg);
142
+ write_or_whine_pipe(fd, trace_str, trace_len, err_msg);
143
144
free(argv_str);
145
free(format_str);
write_or_die.c
+42
-30
@@ -35,49 +35,61 @@ void write_or_die(int fd, const void *buf, size_t count)
35
}
36
}
37
38
-int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
38
+int write_in_full(int fd, const void *buf, size_t count)
39
{
40
const char *p = buf;
41
- ssize_t written;
41
+ ssize_t total = 0;
42
+ ssize_t wcount = 0;
43
44
while (count > 0) {
44
- written = xwrite(fd, p, count);
45
- if (written == 0) {
46
- fprintf(stderr, "%s: disk full?\n", msg);
47
- return 0;
48
- }
49
- else if (written < 0) {
50
- if (errno == EPIPE)
51
- exit(0);
52
- fprintf(stderr, "%s: write error (%s)\n",
53
- msg, strerror(errno));
54
- return 0;
45
+ wcount = xwrite(fd, p, count);
46
+ if (wcount <= 0) {
47
+ if (total)
48
+ return total;
49
+ else
50
+ return wcount;
51
}
56
- count -= written;
57
- p += written;
52
+ count -= wcount;
53
+ p += wcount;
54
+ total += wcount;
55
+ }
56
+
57
+ return wcount;
58
+}
59
+
60
+int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
61
+{
62
+ ssize_t written;
63
+
64
+ written = write_in_full(fd, buf, count);
65
+ if (written == 0) {
66
+ fprintf(stderr, "%s: disk full?\n", msg);
67
+ return 0;
68
+ }
69
+ else if (written < 0) {
70
+ if (errno == EPIPE)
71
+ exit(0);
72
+ fprintf(stderr, "%s: write error (%s)\n",
73
+ msg, strerror(errno));
74
+ return 0;
75
}
76
77
return 1;
78
}
79
63
-int write_in_full(int fd, const void *buf, size_t count, const char *msg)
80
+int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
81
{
65
- const char *p = buf;
82
ssize_t written;
83
68
- while (count > 0) {
69
- written = xwrite(fd, p, count);
70
- if (written == 0) {
71
- fprintf(stderr, "%s: disk full?\n", msg);
72
- return 0;
73
- }
74
- else if (written < 0) {
75
- fprintf(stderr, "%s: write error (%s)\n",
76
- msg, strerror(errno));
77
- return 0;
78
- }
79
- count -= written;
80
- p += written;
84
+ written = write_in_full(fd, buf, count);
85
+ if (written == 0) {
86
+ fprintf(stderr, "%s: disk full?\n", msg);
87
+ return 0;
88
+ }
89
+ else if (written < 0) {
90
+ fprintf(stderr, "%s: write error (%s)\n",
91
+ msg, strerror(errno));
92
+ return 0;
93
}
94
95
return 1;