Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-07-08 12:12:22 +0300
committerJunio C Hamano <gitster@pobox.com>2016-07-08 19:47:29 +0300
commit52563d7ecc8f3f38cb1c0521294c5f6a0a475637 (patch)
tree1e29e32d8955c4c796794773c032b980f673b1dc /wrapper.c
parentee861e0f78367ff0e94feeb42f721ef83ceec251 (diff)
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>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/wrapper.c b/wrapper.c
index 7c126b87c5..b827206a42 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -640,22 +640,28 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
return len;
}
+void write_file_buf(const char *path, const char *buf, size_t len)
+{
+ int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+ if (write_in_full(fd, buf, len) != len)
+ die_errno(_("could not write to %s"), path);
+ if (close(fd))
+ die_errno(_("could not close %s"), path);
+}
+
void write_file(const char *path, const char *fmt, ...)
{
va_list params;
struct strbuf sb = STRBUF_INIT;
- int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
va_start(params, fmt);
strbuf_vaddf(&sb, fmt, params);
va_end(params);
strbuf_complete_line(&sb);
- if (write_in_full(fd, sb.buf, sb.len) != sb.len)
- die_errno(_("could not write to %s"), path);
+
+ write_file_buf(path, sb.buf, sb.len);
strbuf_release(&sb);
- if (close(fd))
- die_errno(_("could not close %s"), path);
}
void sleep_millisec(int millisec)