1#include"cache.h" 2#include"run-command.h" 3 4/* 5 * Some cases use stdio, but want to flush after the write 6 * to get error handling (and to get better interactive 7 * behaviour - not buffering excessively). 8 * 9 * Of course, if the flush happened within the write itself, 10 * we've already lost the error code, and cannot report it any 11 * more. So we just ignore that case instead (and hope we get 12 * the right error code on the flush). 13 * 14 * If the file handle is stdout, and stdout is a file, then skip the 15 * flush entirely since it's not needed. 16 */ 17voidmaybe_flush_or_die(FILE*f,const char*desc) 18{ 19static int skip_stdout_flush = -1; 20struct stat st; 21char*cp; 22 23if(f == stdout) { 24if(skip_stdout_flush <0) { 25 cp =getenv("GIT_FLUSH"); 26if(cp) 27 skip_stdout_flush = (atoi(cp) ==0); 28else if((fstat(fileno(stdout), &st) ==0) && 29S_ISREG(st.st_mode)) 30 skip_stdout_flush =1; 31else 32 skip_stdout_flush =0; 33} 34if(skip_stdout_flush && !ferror(f)) 35return; 36} 37if(fflush(f)) { 38check_pipe(errno); 39die_errno("write failure on '%s'", desc); 40} 41} 42 43voidfprintf_or_die(FILE*f,const char*fmt, ...) 44{ 45va_list ap; 46int ret; 47 48va_start(ap, fmt); 49 ret =vfprintf(f, fmt, ap); 50va_end(ap); 51 52if(ret <0) { 53check_pipe(errno); 54die_errno("write error"); 55} 56} 57 58voidfsync_or_die(int fd,const char*msg) 59{ 60if(fsync(fd) <0) { 61die_errno("fsync error on '%s'", msg); 62} 63} 64 65voidwrite_or_die(int fd,const void*buf,size_t count) 66{ 67if(write_in_full(fd, buf, count) <0) { 68check_pipe(errno); 69die_errno("write error"); 70} 71}