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
31 struct checkpoint_action
33 struct checkpoint_action
*next
;
34 enum checkpoint_opcode opcode
;
42 /* Checkpointing counter */
43 static unsigned checkpoint
;
45 /* List of checkpoint actions */
46 static struct checkpoint_action
*checkpoint_action
, *checkpoint_action_tail
;
48 static struct checkpoint_action
*
49 alloc_action (enum checkpoint_opcode opcode
)
51 struct checkpoint_action
*p
= xzalloc (sizeof *p
);
52 if (checkpoint_action_tail
)
53 checkpoint_action_tail
->next
= p
;
55 checkpoint_action
= p
;
56 checkpoint_action_tail
= p
;
62 copy_string_unquote (const char *str
)
64 char *output
= xstrdup (str
);
65 size_t len
= strlen (output
);
66 if ((*output
== '"' || *output
== '\'')
67 && output
[len
-1] == *output
)
69 memmove (output
, output
+1, len
-2);
72 unquote_string (output
);
77 checkpoint_compile_action (const char *str
)
79 struct checkpoint_action
*act
;
81 if (strcmp (str
, ".") == 0 || strcmp (str
, "dot") == 0)
82 alloc_action (cop_dot
);
83 else if (strcmp (str
, "bell") == 0)
84 alloc_action (cop_bell
);
85 else if (strcmp (str
, "echo") == 0)
86 alloc_action (cop_echo
);
87 else if (strncmp (str
, "echo=", 5) == 0)
89 act
= alloc_action (cop_echo
);
90 act
->v
.command
= copy_string_unquote (str
+ 5);
92 else if (strncmp (str
, "exec=", 5) == 0)
94 act
= alloc_action (cop_exec
);
95 act
->v
.command
= copy_string_unquote (str
+ 5);
97 else if (strncmp (str
, "ttyout=", 7) == 0)
99 act
= alloc_action (cop_ttyout
);
100 act
->v
.command
= copy_string_unquote (str
+ 7);
102 else if (strncmp (str
, "sleep=", 6) == 0)
105 time_t n
= strtoul (str
+6, &p
, 10);
107 FATAL_ERROR ((0, 0, _("%s: not a valid timeout"), str
));
108 act
= alloc_action (cop_sleep
);
112 FATAL_ERROR ((0, 0, _("%s: unknown checkpoint action"), str
));
116 checkpoint_finish_compile ()
118 if (checkpoint_option
)
120 if (!checkpoint_action
)
121 /* Provide a historical default */
122 checkpoint_compile_action ("echo");
124 else if (checkpoint_action
)
125 /* Otherwise, set default checkpoint rate */
126 checkpoint_option
= DEFAULT_CHECKPOINT
;
130 expand_checkpoint_string (const char *input
, bool do_write
, unsigned cpn
)
132 const char *opstr
= do_write
? gettext ("write") : gettext ("read");
133 size_t opstrlen
= strlen (opstr
);
134 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
135 char *cps
= STRINGIFY_BIGINT (cpn
, uintbuf
);
136 size_t cpslen
= strlen (cps
);
140 size_t outlen
= strlen (input
); /* Initial guess */
142 /* Fix the initial length guess */
143 for (ip
= input
; (ip
= strchr (ip
, '%')) != NULL
; )
148 outlen
+= cpslen
- 2;
152 outlen
+= opstrlen
- 2;
157 output
= xmalloc (outlen
+ 1);
158 for (ip
= input
, op
= output
; *ip
; )
165 op
= stpcpy (op
, cps
);
169 op
= stpcpy (op
, opstr
);
187 run_checkpoint_actions (bool do_write
)
189 struct checkpoint_action
*p
;
192 for (p
= checkpoint_action
; p
; p
= p
->next
)
203 tty
= fopen ("/dev/tty", "w");
214 const char *str
= p
->v
.command
;
218 /* TRANSLATORS: This is a ``checkpoint of write operation'',
219 *not* ``Writing a checkpoint''.
220 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
221 *not* ``Escribiendo un punto de comprobaci@'on'' */
222 str
= gettext ("Write checkpoint %u");
224 /* TRANSLATORS: This is a ``checkpoint of read operation'',
225 *not* ``Reading a checkpoint''.
226 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
227 *not* ``Leyendo un punto de comprobaci@'on'' */
228 str
= gettext ("Read checkpoint %u");
230 tmp
= expand_checkpoint_string (str
, do_write
, checkpoint
);
231 WARN ((0, 0, "%s", tmp
));
238 tty
= fopen ("/dev/tty", "w");
241 char *tmp
= expand_checkpoint_string (p
->v
.command
, do_write
,
243 fprintf (tty
, "%s", tmp
);
254 sys_exec_checkpoint_script (p
->v
.command
,
255 archive_name_cursor
[0],
265 checkpoint_run (bool do_write
)
267 if (checkpoint_option
&& !(++checkpoint
% checkpoint_option
))
268 run_checkpoint_actions (do_write
);