]>
Dogcows Code - chaz/tar/blob - src/checkpoint.c
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/>. */
22 #include "wordsplit.h"
23 #include <sys/ioctl.h>
24 #include "fprintftime.h"
26 enum checkpoint_opcode
37 struct checkpoint_action
39 struct checkpoint_action
*next
;
40 enum checkpoint_opcode opcode
;
48 /* Checkpointing counter */
49 static unsigned checkpoint
;
51 /* List of checkpoint actions */
52 static struct checkpoint_action
*checkpoint_action
, *checkpoint_action_tail
;
54 static struct checkpoint_action
*
55 alloc_action (enum checkpoint_opcode opcode
)
57 struct checkpoint_action
*p
= xzalloc (sizeof *p
);
58 if (checkpoint_action_tail
)
59 checkpoint_action_tail
->next
= p
;
61 checkpoint_action
= p
;
62 checkpoint_action_tail
= p
;
68 copy_string_unquote (const char *str
)
70 char *output
= xstrdup (str
);
71 size_t len
= strlen (output
);
72 if ((*output
== '"' || *output
== '\'')
73 && output
[len
-1] == *output
)
75 memmove (output
, output
+1, len
-2);
78 unquote_string (output
);
83 checkpoint_compile_action (const char *str
)
85 struct checkpoint_action
*act
;
87 if (strcmp (str
, ".") == 0 || strcmp (str
, "dot") == 0)
88 alloc_action (cop_dot
);
89 else if (strcmp (str
, "bell") == 0)
90 alloc_action (cop_bell
);
91 else if (strcmp (str
, "echo") == 0)
92 alloc_action (cop_echo
);
93 else if (strncmp (str
, "echo=", 5) == 0)
95 act
= alloc_action (cop_echo
);
96 act
->v
.command
= copy_string_unquote (str
+ 5);
98 else if (strncmp (str
, "exec=", 5) == 0)
100 act
= alloc_action (cop_exec
);
101 act
->v
.command
= copy_string_unquote (str
+ 5);
103 else if (strncmp (str
, "ttyout=", 7) == 0)
105 act
= alloc_action (cop_ttyout
);
106 act
->v
.command
= copy_string_unquote (str
+ 7);
108 else if (strncmp (str
, "sleep=", 6) == 0)
111 time_t n
= strtoul (str
+6, &p
, 10);
113 FATAL_ERROR ((0, 0, _("%s: not a valid timeout"), str
));
114 act
= alloc_action (cop_sleep
);
117 else if (strcmp (str
, "totals") == 0)
118 alloc_action (cop_totals
);
120 FATAL_ERROR ((0, 0, _("%s: unknown checkpoint action"), str
));
124 checkpoint_finish_compile (void)
126 if (checkpoint_option
)
128 if (!checkpoint_action
)
129 /* Provide a historical default */
130 checkpoint_compile_action ("echo");
132 else if (checkpoint_action
)
133 /* Otherwise, set default checkpoint rate */
134 checkpoint_option
= DEFAULT_CHECKPOINT
;
137 static const char *checkpoint_total_format
[] = {
148 ws
.ws_col
= ws
.ws_row
= 0;
149 if ((ioctl (fileno (fp
), TIOCGWINSZ
, (char *) &ws
) < 0) || ws
.ws_col
== 0)
151 const char *col
= getenv ("COLUMNS");
153 return strtol (col
, NULL
, 10);
161 getarg (const char *input
, const char ** endp
, char **argbuf
, size_t *arglen
)
165 char *p
= strchr (input
+ 1, '}');
168 size_t n
= p
- input
;
172 *argbuf
= xrealloc (*argbuf
, *arglen
);
175 memcpy (*argbuf
, input
+ 1, n
);
186 static int tty_cleanup
;
188 static const char *def_format
= "%s: %t: %T%*\r";
191 format_checkpoint_string (FILE *fp
, size_t len
,
192 const char *input
, bool do_write
,
195 const char *opstr
= do_write
? gettext ("write") : gettext ("read");
196 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
197 char *cps
= STRINGIFY_BIGINT (cpn
, uintbuf
);
200 static char *argbuf
= NULL
;
201 static size_t arglen
= 0;
207 /* TRANSLATORS: This is a "checkpoint of write operation",
208 *not* "Writing a checkpoint".
209 E.g. in Spanish "Punto de comprobaci@'on de escritura",
210 *not* "Escribiendo un punto de comprobaci@'on" */
211 input
= gettext ("Write checkpoint %u");
213 /* TRANSLATORS: This is a "checkpoint of read operation",
214 *not* "Reading a checkpoint".
215 E.g. in Spanish "Punto de comprobaci@'on de lectura",
216 *not* "Leyendo un punto de comprobaci@'on" */
217 input
= gettext ("Read checkpoint %u");
220 for (ip
= input
; *ip
; ip
++)
226 arg
= getarg (ip
, &ip
, &argbuf
, &arglen
);
238 len
+= format_checkpoint_string (fp
, len
, def_format
, do_write
,
249 len
+= strlen (opstr
);
253 len
+= fprintf (fp
, "%.0f", compute_duration ());
258 len
+= format_total_stats (fp
, checkpoint_total_format
, ',', 0);
265 const char *fmt
= arg
? arg
: "%c";
267 gettimeofday (&tv
, NULL
);
268 tm
= localtime (&tv
.tv_sec
);
269 len
+= fprintftime (fp
, fmt
, tm
, 0, tv
.tv_usec
* 1000);
275 int w
= arg
? strtoul (arg
, NULL
, 10) : getwidth (fp
);
276 for (; w
> len
; len
++)
305 static FILE *tty
= NULL
;
308 run_checkpoint_actions (bool do_write
)
310 struct checkpoint_action
*p
;
312 for (p
= checkpoint_action
; p
; p
= p
->next
)
323 tty
= fopen ("/dev/tty", "w");
333 int n
= fprintf (stderr
, "%s: ", program_name
);
334 format_checkpoint_string (stderr
, n
, p
->v
.command
, do_write
,
336 fputc ('\n', stderr
);
342 tty
= fopen ("/dev/tty", "w");
344 format_checkpoint_string (tty
, 0, p
->v
.command
, do_write
,
353 sys_exec_checkpoint_script (p
->v
.command
,
354 archive_name_cursor
[0],
360 print_total_stats ();
366 finish_checkpoint_actions (void)
368 struct checkpoint_action
*p
;
370 for (p
= checkpoint_action
; p
; p
= p
->next
)
375 if (tty
&& tty_cleanup
)
377 int w
= getwidth (tty
);
392 checkpoint_run (bool do_write
)
394 if (checkpoint_option
&& !(++checkpoint
% checkpoint_option
))
395 run_checkpoint_actions (do_write
);
399 checkpoint_finish (void)
401 if (checkpoint_option
)
402 finish_checkpoint_actions ();
This page took 0.0515679999999999 seconds and 5 git commands to generate.