Source

git / write_or_die.c

Full commit
Rene Scharfe 7230e6d 

Theodore Ts'o 06f59e9 

































Johannes Sixt b2f5e26 





Theodore Ts'o 06f59e9 
Thomas Rast d824cbb 
Theodore Ts'o 06f59e9 


Linus Torvalds 4c81b03 


Thomas Rast d824cbb 
Linus Torvalds 4c81b03 


Andy Whitcroft 93822c2 
Christian Couder 7cf6720 
Linus Torvalds d34cf19 
Andy Whitcroft 93822c2 

Thomas Rast d824cbb 
Andy Whitcroft e081405 




Linus Torvalds d34cf19 
Andy Whitcroft e081405 




Christian Couder 7cf6720 



Andy Whitcroft 825cee7 
Andy Whitcroft e081405 
Andy Whitcroft 825cee7 
Linus Torvalds d34cf19 
Andy Whitcroft e081405 


Andy Whitcroft 825cee7 


#include "cache.h"

/*
 * Some cases use stdio, but want to flush after the write
 * to get error handling (and to get better interactive
 * behaviour - not buffering excessively).
 *
 * Of course, if the flush happened within the write itself,
 * we've already lost the error code, and cannot report it any
 * more. So we just ignore that case instead (and hope we get
 * the right error code on the flush).
 *
 * If the file handle is stdout, and stdout is a file, then skip the
 * flush entirely since it's not needed.
 */
void maybe_flush_or_die(FILE *f, const char *desc)
{
	static int skip_stdout_flush = -1;
	struct stat st;
	char *cp;

	if (f == stdout) {
		if (skip_stdout_flush < 0) {
			cp = getenv("GIT_FLUSH");
			if (cp)
				skip_stdout_flush = (atoi(cp) == 0);
			else if ((fstat(fileno(stdout), &st) == 0) &&
				 S_ISREG(st.st_mode))
				skip_stdout_flush = 1;
			else
				skip_stdout_flush = 0;
		}
		if (skip_stdout_flush && !ferror(f))
			return;
	}
	if (fflush(f)) {
		/*
		 * On Windows, EPIPE is returned only by the first write()
		 * after the reading end has closed its handle; subsequent
		 * write()s return EINVAL.
		 */
		if (errno == EPIPE || errno == EINVAL)
			exit(0);
		die_errno("write failure on '%s'", desc);
	}
}

void fsync_or_die(int fd, const char *msg)
{
	if (fsync(fd) < 0) {
		die_errno("fsync error on '%s'", msg);
	}
}

void write_or_die(int fd, const void *buf, size_t count)
{
	if (write_in_full(fd, buf, count) < 0) {
		if (errno == EPIPE)
			exit(0);
		die_errno("write error");
	}
}

int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
{
	if (write_in_full(fd, buf, count) < 0) {
		if (errno == EPIPE)
			exit(0);
		fprintf(stderr, "%s: write error (%s)\n",
			msg, strerror(errno));
		return 0;
	}

	return 1;
}

int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
{
	if (write_in_full(fd, buf, count) < 0) {
		fprintf(stderr, "%s: write error (%s)\n",
			msg, strerror(errno));
		return 0;
	}

	return 1;
}