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 <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2015-05-19 10:55:16 -07:00
Родитель fdf96a20ac
Коммит 00b7cbfcb3
3 изменённых файлов: 16 добавлений и 7 удалений

Просмотреть файл

@ -1482,9 +1482,13 @@ extern const char *git_mailmap_blob;
extern void maybe_flush_or_die(FILE *, const char *); extern void maybe_flush_or_die(FILE *, const char *);
__attribute__((format (printf, 2, 3))) __attribute__((format (printf, 2, 3)))
extern void fprintf_or_die(FILE *, const char *fmt, ...); extern void fprintf_or_die(FILE *, const char *fmt, ...);
#define COPY_READ_ERROR (-2)
#define COPY_WRITE_ERROR (-3)
extern int copy_fd(int ifd, int ofd); extern int copy_fd(int ifd, int ofd);
extern int copy_file(const char *dst, const char *src, int mode); extern int copy_file(const char *dst, const char *src, int mode);
extern int copy_file_with_time(const char *dst, const char *src, int mode); extern int copy_file_with_time(const char *dst, const char *src, int mode);
extern void write_or_die(int fd, const void *buf, size_t count); extern void write_or_die(int fd, const void *buf, size_t count);
extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg); extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg); extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);

17
copy.c
Просмотреть файл

@ -7,13 +7,10 @@ int copy_fd(int ifd, int ofd)
ssize_t len = xread(ifd, buffer, sizeof(buffer)); ssize_t len = xread(ifd, buffer, sizeof(buffer));
if (!len) if (!len)
break; break;
if (len < 0) { if (len < 0)
return error("copy-fd: read returned %s", return COPY_READ_ERROR;
strerror(errno));
}
if (write_in_full(ofd, buffer, len) < 0) if (write_in_full(ofd, buffer, len) < 0)
return error("copy-fd: write returned %s", return COPY_WRITE_ERROR;
strerror(errno));
} }
return 0; return 0;
} }
@ -43,6 +40,14 @@ int copy_file(const char *dst, const char *src, int mode)
return fdo; return fdo;
} }
status = copy_fd(fdi, fdo); status = copy_fd(fdi, fdo);
switch (status) {
case COPY_READ_ERROR:
error("copy-fd: read returned %s", strerror(errno));
break;
case COPY_WRITE_ERROR:
error("copy-fd: write returned %s", strerror(errno));
break;
}
close(fdi); close(fdi);
if (close(fdo) != 0) if (close(fdo) != 0)
return error("%s: close error: %s", dst, strerror(errno)); return error("%s: close error: %s", dst, strerror(errno));

Просмотреть файл

@ -206,7 +206,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
int save_errno = errno; int save_errno = errno;
if (flags & LOCK_DIE_ON_ERROR) if (flags & LOCK_DIE_ON_ERROR)
exit(128); die("failed to prepare '%s' for appending", path);
close(orig_fd); close(orig_fd);
rollback_lock_file(lk); rollback_lock_file(lk);
errno = save_errno; errno = save_errno;