1 /* Checkpoint management for tar.
3 Copyright (C) 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>. */
21 enum checkpoint_opcode
29 struct checkpoint_action
31 struct checkpoint_action *next;
32 enum checkpoint_opcode opcode;
40 /* Checkpointing counter */
41 static unsigned checkpoint;
43 /* List of checkpoint actions */
44 static struct checkpoint_action *checkpoint_action, *checkpoint_action_tail;
46 static struct checkpoint_action *
47 alloc_action (enum checkpoint_opcode opcode)
49 struct checkpoint_action *p = xzalloc (sizeof *p);
50 if (checkpoint_action_tail)
51 checkpoint_action_tail->next = p;
53 checkpoint_action = p;
54 checkpoint_action_tail = p;
60 copy_string_unquote (const char *str)
62 char *output = xstrdup (str);
63 size_t len = strlen (output);
64 if ((*output == '"' || *output == '\'')
65 && output[len-1] == *output)
67 memmove (output, output+1, len-2);
70 unquote_string (output);
75 checkpoint_compile_action (const char *str)
77 struct checkpoint_action *act;
79 if (strcmp (str, ".") == 0 || strcmp (str, "dot") == 0)
80 alloc_action (cop_dot);
81 else if (strcmp (str, "echo") == 0)
82 alloc_action (cop_echo);
83 else if (strncmp (str, "echo=", 5) == 0)
85 act = alloc_action (cop_echo);
86 act->v.command = copy_string_unquote (str + 5);
88 else if (strncmp (str, "exec=", 5) == 0)
90 act = alloc_action (cop_exec);
91 act->v.command = copy_string_unquote (str + 5);
93 else if (strncmp (str, "sleep=", 6) == 0)
96 time_t n = strtoul (str+6, &p, 10);
98 FATAL_ERROR ((0, 0, _("%s: not a valid timeout"), str));
99 act = alloc_action (cop_sleep);
103 FATAL_ERROR ((0, 0, _("%s: unknown checkpoint action"), str));
107 checkpoint_finish_compile ()
109 if (checkpoint_option)
111 if (!checkpoint_action)
112 /* Provide a historical default */
113 checkpoint_compile_action ("echo");
115 else if (checkpoint_action)
116 /* Otherwise, set default checkpoint rate */
117 checkpoint_option = DEFAULT_CHECKPOINT;
121 expand_checkpoint_string (const char *input, bool do_write, unsigned cpn)
123 const char *opstr = do_write ? gettext ("write") : gettext ("read");
124 size_t opstrlen = strlen (opstr);
125 char uintbuf[UINTMAX_STRSIZE_BOUND];
126 char *cps = STRINGIFY_BIGINT (cpn, uintbuf);
127 size_t cpslen = strlen (cps);
131 size_t outlen = strlen (input); /* Initial guess */
133 /* Fix the initial length guess */
134 for (ip = input; (ip = strchr (ip, '%')) != NULL; )
139 outlen += cpslen - 2;
143 outlen += opstrlen - 2;
148 output = xmalloc (outlen + 1);
149 for (ip = input, op = output; *ip; )
156 op = stpcpy (op, cps);
160 op = stpcpy (op, opstr);
178 run_checkpoint_actions (bool do_write)
180 struct checkpoint_action *p;
182 for (p = checkpoint_action; p; p = p->next)
194 const char *str = p->v.command;
198 /* TRANSLATORS: This is a ``checkpoint of write operation'',
199 *not* ``Writing a checkpoint''.
200 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
201 *not* ``Escribiendo un punto de comprobaci@'on'' */
202 str = gettext ("Write checkpoint %u");
204 /* TRANSLATORS: This is a ``checkpoint of read operation'',
205 *not* ``Reading a checkpoint''.
206 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
207 *not* ``Leyendo un punto de comprobaci@'on'' */
208 str = gettext ("Read checkpoint %u");
210 tmp = expand_checkpoint_string (str, do_write, checkpoint);
211 WARN ((0, 0, "%s", tmp));
221 sys_exec_checkpoint_script (p->v.command,
222 archive_name_cursor[0],
230 checkpoint_run (bool do_write)
232 if (checkpoint_option && !(++checkpoint % checkpoint_option))
233 run_checkpoint_actions (do_write);