123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- /* Checkpoint management for tar.
- Copyright 2007-2023 Free Software Foundation, Inc.
- This file is part of GNU tar.
- GNU tar 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 3 of the License, or
- (at your option) any later version.
- GNU tar 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, see <http://www.gnu.org/licenses/>. */
- #include <system.h>
- #include "common.h"
- #include "wordsplit.h"
- #include <sys/ioctl.h>
- #include <termios.h>
- #include "fprintftime.h"
- #include <signal.h>
- enum checkpoint_opcode
- {
- cop_dot,
- cop_bell,
- cop_echo,
- cop_ttyout,
- cop_sleep,
- cop_exec,
- cop_totals,
- cop_wait
- };
- struct checkpoint_action
- {
- struct checkpoint_action *next;
- enum checkpoint_opcode opcode;
- union
- {
- time_t time;
- char *command;
- int signal;
- } v;
- };
- /* Checkpointing counter */
- static unsigned checkpoint;
- /* List of checkpoint actions */
- static struct checkpoint_action *checkpoint_action, *checkpoint_action_tail;
- /* State of the checkpoint system */
- enum {
- CHKP_INIT, /* Needs initialization */
- CHKP_COMPILE, /* Actions are being compiled */
- CHKP_RUN /* Actions are being run */
- };
- static int checkpoint_state;
- /* Blocked signals */
- static sigset_t sigs;
- static struct checkpoint_action *
- alloc_action (enum checkpoint_opcode opcode)
- {
- struct checkpoint_action *p = xzalloc (sizeof *p);
- if (checkpoint_action_tail)
- checkpoint_action_tail->next = p;
- else
- checkpoint_action = p;
- checkpoint_action_tail = p;
- p->opcode = opcode;
- return p;
- }
- static char *
- copy_string_unquote (const char *str)
- {
- char *output = xstrdup (str);
- size_t len = strlen (output);
- if ((*output == '"' || *output == '\'')
- && len > 1 && output[len-1] == *output)
- {
- memmove (output, output+1, len-2);
- output[len-2] = 0;
- }
- unquote_string (output);
- return output;
- }
- void
- checkpoint_compile_action (const char *str)
- {
- struct checkpoint_action *act;
- if (checkpoint_state == CHKP_INIT)
- {
- sigemptyset (&sigs);
- checkpoint_state = CHKP_COMPILE;
- }
-
- if (strcmp (str, ".") == 0 || strcmp (str, "dot") == 0)
- alloc_action (cop_dot);
- else if (strcmp (str, "bell") == 0)
- alloc_action (cop_bell);
- else if (strcmp (str, "echo") == 0)
- alloc_action (cop_echo);
- else if (strncmp (str, "echo=", 5) == 0)
- {
- act = alloc_action (cop_echo);
- act->v.command = copy_string_unquote (str + 5);
- }
- else if (strncmp (str, "exec=", 5) == 0)
- {
- act = alloc_action (cop_exec);
- act->v.command = copy_string_unquote (str + 5);
- }
- else if (strncmp (str, "ttyout=", 7) == 0)
- {
- act = alloc_action (cop_ttyout);
- act->v.command = copy_string_unquote (str + 7);
- }
- else if (strncmp (str, "sleep=", 6) == 0)
- {
- char *p;
- time_t n = strtoul (str+6, &p, 10);
- if (*p)
- FATAL_ERROR ((0, 0, _("%s: not a valid timeout"), str));
- act = alloc_action (cop_sleep);
- act->v.time = n;
- }
- else if (strcmp (str, "totals") == 0)
- alloc_action (cop_totals);
- else if (strncmp (str, "wait=", 5) == 0)
- {
- act = alloc_action (cop_wait);
- act->v.signal = decode_signal (str + 5);
- sigaddset (&sigs, act->v.signal);
- }
- else
- FATAL_ERROR ((0, 0, _("%s: unknown checkpoint action"), str));
- }
- void
- checkpoint_finish_compile (void)
- {
- if (checkpoint_state == CHKP_INIT
- && checkpoint_option
- && !checkpoint_action)
- {
- /* Provide a historical default */
- checkpoint_compile_action ("echo");
- }
- if (checkpoint_state == CHKP_COMPILE)
- {
- sigprocmask (SIG_BLOCK, &sigs, NULL);
- if (!checkpoint_option)
- /* set default checkpoint rate */
- checkpoint_option = DEFAULT_CHECKPOINT;
- checkpoint_state = CHKP_RUN;
- }
- }
- static const char *checkpoint_total_format[] = {
- "R",
- "W",
- "D"
- };
- static long
- getwidth (FILE *fp)
- {
- char const *columns;
- #ifdef TIOCGWINSZ
- struct winsize ws;
- if (ioctl (fileno (fp), TIOCGWINSZ, &ws) == 0 && 0 < ws.ws_col)
- return ws.ws_col;
- #endif
- columns = getenv ("COLUMNS");
- if (columns)
- {
- long int col = strtol (columns, NULL, 10);
- if (0 < col)
- return col;
- }
- return 80;
- }
- static char *
- getarg (const char *input, const char ** endp, char **argbuf, size_t *arglen)
- {
- if (input[0] == '{')
- {
- char *p = strchr (input + 1, '}');
- if (p)
- {
- size_t n = p - input;
- if (n > *arglen)
- {
- *arglen = n;
- *argbuf = xrealloc (*argbuf, *arglen);
- }
- n--;
- memcpy (*argbuf, input + 1, n);
- (*argbuf)[n] = 0;
- *endp = p + 1;
- return *argbuf;
- }
- }
- *endp = input;
- return NULL;
- }
- static int tty_cleanup;
- static const char *def_format =
- "%{%Y-%m-%d %H:%M:%S}t: %ds, %{read,wrote}T%*\r";
- static int
- format_checkpoint_string (FILE *fp, size_t len,
- const char *input, bool do_write,
- unsigned cpn)
- {
- const char *opstr = do_write ? gettext ("write") : gettext ("read");
- char uintbuf[UINTMAX_STRSIZE_BOUND];
- char *cps = STRINGIFY_BIGINT (cpn, uintbuf);
- const char *ip;
- static char *argbuf = NULL;
- static size_t arglen = 0;
- char *arg = NULL;
- if (!input)
- {
- if (do_write)
- /* TRANSLATORS: This is a "checkpoint of write operation",
- *not* "Writing a checkpoint".
- E.g. in Spanish "Punto de comprobaci@'on de escritura",
- *not* "Escribiendo un punto de comprobaci@'on" */
- input = gettext ("Write checkpoint %u");
- else
- /* TRANSLATORS: This is a "checkpoint of read operation",
- *not* "Reading a checkpoint".
- E.g. in Spanish "Punto de comprobaci@'on de lectura",
- *not* "Leyendo un punto de comprobaci@'on" */
- input = gettext ("Read checkpoint %u");
- }
- for (ip = input; *ip; ip++)
- {
- if (*ip == '%')
- {
- if (*++ip == '{')
- {
- arg = getarg (ip, &ip, &argbuf, &arglen);
- if (!arg)
- {
- fputc ('%', fp);
- fputc (*ip, fp);
- len += 2;
- continue;
- }
- }
- switch (*ip)
- {
- case 'c':
- len += format_checkpoint_string (fp, len, def_format, do_write,
- cpn);
- break;
- case 'u':
- fputs (cps, fp);
- len += strlen (cps);
- break;
- case 's':
- fputs (opstr, fp);
- len += strlen (opstr);
- break;
- case 'd':
- len += fprintf (fp, "%.0f", compute_duration ());
- break;
- case 'T':
- {
- const char **fmt = checkpoint_total_format, *fmtbuf[3];
- struct wordsplit ws;
- compute_duration ();
- if (arg)
- {
- ws.ws_delim = ",";
- if (wordsplit (arg, &ws, WRDSF_NOVAR | WRDSF_NOCMD |
- WRDSF_QUOTE | WRDSF_DELIM))
- ERROR ((0, 0, _("cannot split string '%s': %s"),
- arg, wordsplit_strerror (&ws)));
- else
- {
- int i;
- for (i = 0; i < ws.ws_wordc; i++)
- fmtbuf[i] = ws.ws_wordv[i];
- for (; i < 3; i++)
- fmtbuf[i] = NULL;
- fmt = fmtbuf;
- }
- }
- len += format_total_stats (fp, fmt, ',', 0);
- if (arg)
- wordsplit_free (&ws);
- }
- break;
- case 't':
- {
- struct timeval tv;
- struct tm *tm;
- const char *fmt = arg ? arg : "%c";
- gettimeofday (&tv, NULL);
- tm = localtime (&tv.tv_sec);
- len += fprintftime (fp, fmt, tm, 0, tv.tv_usec * 1000);
- }
- break;
- case '*':
- {
- long w = arg ? strtol (arg, NULL, 10) : getwidth (fp);
- for (; w > len; len++)
- fputc (' ', fp);
- }
- break;
- default:
- fputc ('%', fp);
- fputc (*ip, fp);
- len += 2;
- break;
- }
- arg = NULL;
- }
- else
- {
- fputc (*ip, fp);
- if (*ip == '\r')
- {
- len = 0;
- tty_cleanup = 1;
- }
- else
- len++;
- }
- }
- fflush (fp);
- return len;
- }
- static FILE *tty = NULL;
- static void
- run_checkpoint_actions (bool do_write)
- {
- struct checkpoint_action *p;
- for (p = checkpoint_action; p; p = p->next)
- {
- switch (p->opcode)
- {
- case cop_dot:
- fputc ('.', stdlis);
- fflush (stdlis);
- break;
- case cop_bell:
- if (!tty)
- tty = fopen ("/dev/tty", "w");
- if (tty)
- {
- fputc ('\a', tty);
- fflush (tty);
- }
- break;
- case cop_echo:
- {
- int n = fprintf (stderr, "%s: ", program_name);
- format_checkpoint_string (stderr, n, p->v.command, do_write,
- checkpoint);
- fputc ('\n', stderr);
- }
- break;
- case cop_ttyout:
- if (!tty)
- tty = fopen ("/dev/tty", "w");
- if (tty)
- format_checkpoint_string (tty, 0, p->v.command, do_write,
- checkpoint);
- break;
- case cop_sleep:
- sleep (p->v.time);
- break;
- case cop_exec:
- sys_exec_checkpoint_script (p->v.command,
- archive_name_cursor[0],
- checkpoint);
- break;
- case cop_totals:
- compute_duration ();
- print_total_stats ();
- break;
- case cop_wait:
- {
- int n;
- sigwait (&sigs, &n);
- }
- }
- }
- }
- void
- checkpoint_flush_actions (void)
- {
- struct checkpoint_action *p;
- for (p = checkpoint_action; p; p = p->next)
- {
- switch (p->opcode)
- {
- case cop_ttyout:
- if (tty && tty_cleanup)
- {
- long w = getwidth (tty);
- while (w--)
- fputc (' ', tty);
- fputc ('\r', tty);
- fflush (tty);
- }
- break;
- default:
- /* nothing */;
- }
- }
- }
- void
- checkpoint_run (bool do_write)
- {
- if (checkpoint_option && !(++checkpoint % checkpoint_option))
- run_checkpoint_actions (do_write);
- }
- void
- checkpoint_finish (void)
- {
- if (checkpoint_option)
- {
- checkpoint_flush_actions ();
- if (tty)
- fclose (tty);
- }
- }
|