write_file: add pointer+len variant

There are many callsites which could use write_file, but for which it is a little awkward because they have a strbuf or other pointer/len combo. Specifically: 1. write_file() takes a format string, so we have to use "%s" or "%.*s", which are ugly. 2. Using any form of "%s" does not handle embedded NULs in the output. That probably doesn't matter for our call-sites, but it's nicer not to have to worry. 3. It's less efficient; we format into another strbuf just to do the write. That's probably not measurably slow for our uses, but it's simply inelegant. We can fix this by providing a helper to write out the formatted buffer, and just calling it from write_file(). Note that we don't do the usual "complete with a newline" that write_file does. If the caller has their own buffer, there's a reasonable chance they're doing something more complicated than a single line, and they can call strbuf_complete_line() themselves. We could go even further and add strbuf_write_file(), but it doesn't save much: - write_file_buf(path, sb.buf, sb.len); + strbuf_write_file(&sb, path); It would also be somewhat asymmetric with strbuf_read_file, which actually returns errors rather than dying (and the error handling is most of the benefit of write_file() in the first place). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 8, 2016 at 05:12 UTC 52563d7ecc8f3f38cb1c0521294c5f6a0a475637
2 files changed +17 -5
cache.h
+6
@@ -1734,6 +1734,12 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
1734 return write_in_full(fd, str, strlen(str));
1735 }
1736
1737 +/**
1738 + * Open (and truncate) the file at path, write the contents of buf to it,
1739 + * and close it. Dies if any errors are encountered.
1740 + */
1741 +extern void write_file_buf(const char *path, const char *buf, size_t len);
1742 +
1743 extern void write_file(const char *path, const char *fmt, ...);
1744
1745 /* pager.c */
wrapper.c
+11 -5
@@ -640,22 +640,28 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
640 return len;
641 }
642
643 +void write_file_buf(const char *path, const char *buf, size_t len)
644 +{
645 + int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
646 + if (write_in_full(fd, buf, len) != len)
647 + die_errno(_("could not write to %s"), path);
648 + if (close(fd))
649 + die_errno(_("could not close %s"), path);
650 +}
651 +
652 void write_file(const char *path, const char *fmt, ...)
653 {
654 va_list params;
655 struct strbuf sb = STRBUF_INIT;
647 - int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
656
657 va_start(params, fmt);
658 strbuf_vaddf(&sb, fmt, params);
659 va_end(params);
660
661 strbuf_complete_line(&sb);
654 - if (write_in_full(fd, sb.buf, sb.len) != sb.len)
655 - die_errno(_("could not write to %s"), path);
662 +
663 + write_file_buf(path, sb.buf, sb.len);
664 strbuf_release(&sb);
657 - if (close(fd))
658 - die_errno(_("could not close %s"), path);
665 }
666
667 void sleep_millisec(int millisec)