mirror of
https://git.savannah.gnu.org/git/tar.git
synced 2026-04-28 20:27:06 +00:00
Support gnulib-style timestamps in checkpoint logs
* gnulib.modules: Add nstrftime-limited, time_rz. Sort. * src/checkpoint.c: Include <strftime.h>. (format_checkpoint_string): Use nstrftime instead of strftime. Also fix an obscure bug on platforms that lack tm_gmtoff+tm_zone by calling tzalloc on those platforms; if it fails, fall back on gmtime. Also, use fwrite instead of fprintf, since we typically know the length already and this gives us a more-accurate byte count in case there are partial writes.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
|
||||
#include <wordsplit.h>
|
||||
#include <flexmember.h>
|
||||
#include <strftime.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
@@ -306,22 +307,41 @@ format_checkpoint_string (FILE *fp, intmax_t len,
|
||||
{
|
||||
struct timespec ts = current_timespec ();
|
||||
struct tm *tm = localtime (&ts.tv_sec);
|
||||
char const *tmstr = NULL;
|
||||
|
||||
#if HAVE_STRUCT_TM_TM_GMTOFF && HAVE_STRUCT_TM_TM_ZONE
|
||||
/* struct tm has POSIX.1-2024 tm_gmtoff and tm_zone,
|
||||
so nstrftime ignores tz and any tz value will do. */
|
||||
timezone_t tz = 0;
|
||||
#else
|
||||
static timezone_t tz;
|
||||
if (tm && !tz)
|
||||
{
|
||||
tz = tzalloc (getenv ("TZ"));
|
||||
if (!tz)
|
||||
tm = NULL;
|
||||
}
|
||||
#endif
|
||||
/* Keep BUF relatively small, as any text timestamp
|
||||
not fitting into BUF is likely a DoS attack. */
|
||||
char buf[max (SYSINT_BUFSIZE, 256)];
|
||||
|
||||
if (tm)
|
||||
char buf[max (TIMESPEC_STRSIZE_BOUND, 256)];
|
||||
ptrdiff_t buflen =
|
||||
(tm
|
||||
? nstrftime (buf, sizeof buf, arg ? arg : "%c",
|
||||
tm, tz, ts.tv_nsec)
|
||||
: -1);
|
||||
char const *tmstr;
|
||||
idx_t tmstrlen;
|
||||
if (buflen < 0)
|
||||
{
|
||||
buf[0] = '\0';
|
||||
char const *fmt = arg ? arg : "%c";
|
||||
if (strftime (buf, sizeof buf, fmt, tm) != 0 || !buf[0])
|
||||
tmstr = buf;
|
||||
tmstr = code_timespec (ts, buf);
|
||||
tmstrlen = strlen (tmstr);
|
||||
}
|
||||
if (!tmstr)
|
||||
tmstr = timetostr (ts.tv_sec, buf);
|
||||
len = add_printf (len, fprintf (fp, "%s", tmstr));
|
||||
else
|
||||
{
|
||||
tmstr = buf;
|
||||
tmstrlen = buflen;
|
||||
}
|
||||
len = add_printf (len, fwrite (tmstr, 1, tmstrlen, stdout));
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user