Skip to content

Commit 00b7cbf

Browse files
committed
copy.c: make copy_fd() report its status silently
When copy_fd() function encounters errors, it emits error messages itself, which makes it impossible for callers to take responsibility for reporting errors, especially when they want to ignore certain errors. Move the error reporting to its callers in preparation. - copy_file() and copy_file_with_time() by indirection get their own calls to error(). - hold_lock_file_for_append(), when told to die on error, used to exit(128) relying on the error message from copy_fd(), but now it does its own die() instead. Note that the callers that do not pass LOCK_DIE_ON_ERROR need to be adjusted for this change, but fortunately there is none ;-) - filter_buffer_or_fd() has its own error() already, in addition to the message from copy_fd(), so this will change the output but arguably in a better way. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdf96a2 commit 00b7cbf

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,13 @@ extern const char *git_mailmap_blob;
14821482
extern void maybe_flush_or_die(FILE *, const char *);
14831483
__attribute__((format (printf, 2, 3)))
14841484
extern void fprintf_or_die(FILE *, const char *fmt, ...);
1485+
1486+
#define COPY_READ_ERROR (-2)
1487+
#define COPY_WRITE_ERROR (-3)
14851488
extern int copy_fd(int ifd, int ofd);
14861489
extern int copy_file(const char *dst, const char *src, int mode);
14871490
extern int copy_file_with_time(const char *dst, const char *src, int mode);
1491+
14881492
extern void write_or_die(int fd, const void *buf, size_t count);
14891493
extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
14901494
extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);

copy.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ int copy_fd(int ifd, int ofd)
77
ssize_t len = xread(ifd, buffer, sizeof(buffer));
88
if (!len)
99
break;
10-
if (len < 0) {
11-
return error("copy-fd: read returned %s",
12-
strerror(errno));
13-
}
10+
if (len < 0)
11+
return COPY_READ_ERROR;
1412
if (write_in_full(ofd, buffer, len) < 0)
15-
return error("copy-fd: write returned %s",
16-
strerror(errno));
13+
return COPY_WRITE_ERROR;
1714
}
1815
return 0;
1916
}
@@ -43,6 +40,14 @@ int copy_file(const char *dst, const char *src, int mode)
4340
return fdo;
4441
}
4542
status = copy_fd(fdi, fdo);
43+
switch (status) {
44+
case COPY_READ_ERROR:
45+
error("copy-fd: read returned %s", strerror(errno));
46+
break;
47+
case COPY_WRITE_ERROR:
48+
error("copy-fd: write returned %s", strerror(errno));
49+
break;
50+
}
4651
close(fdi);
4752
if (close(fdo) != 0)
4853
return error("%s: close error: %s", dst, strerror(errno));

lockfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
206206
int save_errno = errno;
207207

208208
if (flags & LOCK_DIE_ON_ERROR)
209-
exit(128);
209+
die("failed to prepare '%s' for appending", path);
210210
close(orig_fd);
211211
rollback_lock_file(lk);
212212
errno = save_errno;

0 commit comments

Comments
 (0)