1 /* Checkpoint management for tar.
3 Copyright 2007, 2013 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 enum checkpoint_opcode
33 struct checkpoint_action
35 struct checkpoint_action *next;
36 enum checkpoint_opcode opcode;
44 /* Checkpointing counter */
45 static unsigned checkpoint;
47 /* List of checkpoint actions */
48 static struct checkpoint_action *checkpoint_action, *checkpoint_action_tail;
50 static struct checkpoint_action *
51 alloc_action (enum checkpoint_opcode opcode)
53 struct checkpoint_action *p = xzalloc (sizeof *p);
54 if (checkpoint_action_tail)
55 checkpoint_action_tail->next = p;
57 checkpoint_action = p;
58 checkpoint_action_tail = p;
64 copy_string_unquote (const char *str)
66 char *output = xstrdup (str);
67 size_t len = strlen (output);
68 if ((*output == '"' || *output == '\'')
69 && output[len-1] == *output)
71 memmove (output, output+1, len-2);
74 unquote_string (output);
79 checkpoint_compile_action (const char *str)
81 struct checkpoint_action *act;
83 if (strcmp (str, ".") == 0 || strcmp (str, "dot") == 0)
84 alloc_action (cop_dot);
85 else if (strcmp (str, "bell") == 0)
86 alloc_action (cop_bell);
87 else if (strcmp (str, "echo") == 0)
88 alloc_action (cop_echo);
89 else if (strncmp (str, "echo=", 5) == 0)
91 act = alloc_action (cop_echo);
92 act->v.command = copy_string_unquote (str + 5);
94 else if (strncmp (str, "exec=", 5) == 0)
96 act = alloc_action (cop_exec);
97 act->v.command = copy_string_unquote (str + 5);
99 else if (strncmp (str, "ttyout=", 7) == 0)
101 act = alloc_action (cop_ttyout);
102 act->v.command = copy_string_unquote (str + 7);
104 else if (strncmp (str, "sleep=", 6) == 0)
107 time_t n = strtoul (str+6, &p, 10);
109 FATAL_ERROR ((0, 0, _("%s: not a valid timeout"), str));
110 act = alloc_action (cop_sleep);
114 FATAL_ERROR ((0, 0, _("%s: unknown checkpoint action"), str));
118 checkpoint_finish_compile (void)
120 if (checkpoint_option)
122 if (!checkpoint_action)
123 /* Provide a historical default */
124 checkpoint_compile_action ("echo");
126 else if (checkpoint_action)
127 /* Otherwise, set default checkpoint rate */
128 checkpoint_option = DEFAULT_CHECKPOINT;
132 expand_checkpoint_string (const char *input, bool do_write, unsigned cpn)
134 const char *opstr = do_write ? gettext ("write") : gettext ("read");
135 size_t opstrlen = strlen (opstr);
136 char uintbuf[UINTMAX_STRSIZE_BOUND];
137 char *cps = STRINGIFY_BIGINT (cpn, uintbuf);
138 size_t cpslen = strlen (cps);
142 size_t outlen = strlen (input); /* Initial guess */
144 /* Fix the initial length guess */
145 for (ip = input; (ip = strchr (ip, '%')) != NULL; )
150 outlen += cpslen - 2;
154 outlen += opstrlen - 2;
159 output = xmalloc (outlen + 1);
160 for (ip = input, op = output; *ip; )
167 op = stpcpy (op, cps);
171 op = stpcpy (op, opstr);
189 run_checkpoint_actions (bool do_write)
191 struct checkpoint_action *p;
194 for (p = checkpoint_action; p; p = p->next)
205 tty = fopen ("/dev/tty", "w");
216 const char *str = p->v.command;
220 /* TRANSLATORS: This is a "checkpoint of write operation",
221 *not* "Writing a checkpoint".
222 E.g. in Spanish "Punto de comprobaci@'on de escritura",
223 *not* "Escribiendo un punto de comprobaci@'on" */
224 str = gettext ("Write checkpoint %u");
226 /* TRANSLATORS: This is a "checkpoint of read operation",
227 *not* "Reading a checkpoint".
228 E.g. in Spanish "Punto de comprobaci@'on de lectura",
229 *not* "Leyendo un punto de comprobaci@'on" */
230 str = gettext ("Read checkpoint %u");
232 tmp = expand_checkpoint_string (str, do_write, checkpoint);
233 WARN ((0, 0, "%s", tmp));
240 tty = fopen ("/dev/tty", "w");
243 char *tmp = expand_checkpoint_string (p->v.command, do_write,
245 fprintf (tty, "%s", tmp);
256 sys_exec_checkpoint_script (p->v.command,
257 archive_name_cursor[0],
267 checkpoint_run (bool do_write)
269 if (checkpoint_option && !(++checkpoint % checkpoint_option))
270 run_checkpoint_actions (do_write);