2006-09-02 20:23:48 +04:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
|
|
|
* Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
|
|
|
|
* Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
|
|
|
|
* Copyright (C) 2006 Mike McCormack
|
|
|
|
* Copyright (C) 2006 Christian Couder
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
#include "quote.h"
|
|
|
|
|
|
|
|
/* Get a trace file descriptor from GIT_TRACE env variable. */
|
|
|
|
static int get_trace_fd(int *need_close)
|
|
|
|
{
|
|
|
|
char *trace = getenv("GIT_TRACE");
|
|
|
|
|
2006-10-14 18:05:25 +04:00
|
|
|
if (!trace || !strcmp(trace, "") ||
|
|
|
|
!strcmp(trace, "0") || !strcasecmp(trace, "false"))
|
2006-09-02 20:23:48 +04:00
|
|
|
return 0;
|
|
|
|
if (!strcmp(trace, "1") || !strcasecmp(trace, "true"))
|
|
|
|
return STDERR_FILENO;
|
|
|
|
if (strlen(trace) == 1 && isdigit(*trace))
|
|
|
|
return atoi(trace);
|
2007-11-26 01:29:03 +03:00
|
|
|
if (is_absolute_path(trace)) {
|
2006-09-02 20:23:48 +04:00
|
|
|
int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Could not open '%s' for tracing: %s\n"
|
|
|
|
"Defaulting to tracing on stderr...\n",
|
|
|
|
trace, strerror(errno));
|
|
|
|
return STDERR_FILENO;
|
|
|
|
}
|
|
|
|
*need_close = 1;
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2009-02-25 04:22:20 +03:00
|
|
|
fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace);
|
2006-09-02 20:23:48 +04:00
|
|
|
fprintf(stderr, "If you want to trace into a file, "
|
|
|
|
"then please set GIT_TRACE to an absolute pathname "
|
|
|
|
"(starting with /).\n");
|
|
|
|
fprintf(stderr, "Defaulting to tracing on stderr...\n");
|
|
|
|
|
|
|
|
return STDERR_FILENO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char err_msg[] = "Could not trace into fd given by "
|
|
|
|
"GIT_TRACE environment variable";
|
|
|
|
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
void trace_printf(const char *fmt, ...)
|
2006-09-02 20:23:48 +04:00
|
|
|
{
|
2007-09-20 02:42:13 +04:00
|
|
|
struct strbuf buf;
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_list ap;
|
|
|
|
int fd, len, need_close = 0;
|
2006-09-02 20:23:48 +04:00
|
|
|
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
fd = get_trace_fd(&need_close);
|
2006-09-02 20:23:48 +04:00
|
|
|
if (!fd)
|
|
|
|
return;
|
|
|
|
|
2007-11-13 11:22:44 +03:00
|
|
|
strbuf_init(&buf, 64);
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_start(ap, fmt);
|
2007-09-20 02:42:13 +04:00
|
|
|
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_end(ap);
|
2007-09-20 02:42:13 +04:00
|
|
|
if (len >= strbuf_avail(&buf)) {
|
|
|
|
strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (len >= strbuf_avail(&buf))
|
|
|
|
die("broken vsnprintf");
|
|
|
|
}
|
|
|
|
strbuf_setlen(&buf, len);
|
|
|
|
|
|
|
|
write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
|
|
|
|
strbuf_release(&buf);
|
2006-09-02 20:23:48 +04:00
|
|
|
|
|
|
|
if (need_close)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2007-12-03 07:51:50 +03:00
|
|
|
void trace_argv_printf(const char **argv, const char *fmt, ...)
|
2006-09-02 20:23:48 +04:00
|
|
|
{
|
2007-09-20 02:42:13 +04:00
|
|
|
struct strbuf buf;
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_list ap;
|
|
|
|
int fd, len, need_close = 0;
|
2006-09-02 20:23:48 +04:00
|
|
|
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
fd = get_trace_fd(&need_close);
|
2006-09-02 20:23:48 +04:00
|
|
|
if (!fd)
|
|
|
|
return;
|
|
|
|
|
2007-11-13 11:22:44 +03:00
|
|
|
strbuf_init(&buf, 64);
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_start(ap, fmt);
|
2007-09-20 02:42:13 +04:00
|
|
|
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
|
nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf.
* move nfvasprintf into imap-send.c back, and let it work on a 8k buffer,
and die() in case of overflow. It should be enough for imap commands, if
someone cares about imap-send, he's welcomed to fix it properly.
* replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf
logic, it's one place, we'll live with it.
To ease the change, output_buffer string list is replaced with a strbuf ;)
* rework trace.c to call vsnprintf itself. It's used to format strerror()s
and git command names, it should never be more than a few octets long, let
it work on a 8k static buffer with vsnprintf or die loudly.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
2007-09-20 12:43:11 +04:00
|
|
|
va_end(ap);
|
2007-09-20 02:42:13 +04:00
|
|
|
if (len >= strbuf_avail(&buf)) {
|
|
|
|
strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (len >= strbuf_avail(&buf))
|
|
|
|
die("broken vsnprintf");
|
|
|
|
}
|
|
|
|
strbuf_setlen(&buf, len);
|
2006-09-02 20:23:48 +04:00
|
|
|
|
2007-12-03 07:51:50 +03:00
|
|
|
sq_quote_argv(&buf, argv, 0);
|
2007-09-20 02:42:13 +04:00
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
|
|
|
|
strbuf_release(&buf);
|
2006-09-02 20:23:48 +04:00
|
|
|
|
|
|
|
if (need_close)
|
|
|
|
close(fd);
|
|
|
|
}
|