Rename libarchive-2/ to libarchive/. No need for it to be versioned.
[dragonfly.git] / contrib / libarchive / tar / siginfo.c
1 /*-
2  * Copyright 2008 Colin Percival
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/siginfo.c,v 1.2 2008/05/22 21:08:36 cperciva Exp $");
28
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "bsdtar.h"
36
37 /* Is there a pending SIGINFO or SIGUSR1? */
38 static volatile sig_atomic_t siginfo_received = 0;
39
40 struct siginfo_data {
41         /* What sort of operation are we doing? */
42         char * oper;
43
44         /* What path are we handling? */
45         char * path;
46
47         /* How large is the archive entry? */
48         int64_t size;
49
50         /* Old signal handlers. */
51 #ifdef SIGINFO
52         void (*siginfo_old)(int);
53 #endif
54         void (*sigusr1_old)(int);
55 };
56
57 static void              siginfo_handler(int sig);
58
59 /* Handler for SIGINFO / SIGUSR1. */
60 static void
61 siginfo_handler(int sig)
62 {
63
64         (void)sig; /* UNUSED */
65
66         /* Record that SIGINFO or SIGUSR1 has been received. */
67         siginfo_received = 1;
68 }
69
70 void
71 siginfo_init(struct bsdtar *bsdtar)
72 {
73
74         /* Allocate space for internal structure. */
75         if ((bsdtar->siginfo = malloc(sizeof(struct siginfo_data))) == NULL)
76                 bsdtar_errc(bsdtar, 1, errno, "malloc failed");
77
78         /* Set the strings to NULL so that free() is safe. */
79         bsdtar->siginfo->path = bsdtar->siginfo->oper = NULL;
80
81 #ifdef SIGINFO
82         /* We want to catch SIGINFO, if it exists. */
83         bsdtar->siginfo->siginfo_old = signal(SIGINFO, siginfo_handler);
84 #endif
85         /* ... and treat SIGUSR1 the same way as SIGINFO. */
86         bsdtar->siginfo->sigusr1_old = signal(SIGUSR1, siginfo_handler);
87 }
88
89 void
90 siginfo_setinfo(struct bsdtar *bsdtar, const char * oper, const char * path,
91     int64_t size)
92 {
93
94         /* Free old operation and path strings. */
95         free(bsdtar->siginfo->oper);
96         free(bsdtar->siginfo->path);
97
98         /* Duplicate strings and store entry size. */
99         if ((bsdtar->siginfo->oper = strdup(oper)) == NULL)
100                 bsdtar_errc(bsdtar, 1, errno, "Cannot strdup");
101         if ((bsdtar->siginfo->path = strdup(path)) == NULL)
102                 bsdtar_errc(bsdtar, 1, errno, "Cannot strdup");
103         bsdtar->siginfo->size = size;
104 }
105
106 void
107 siginfo_printinfo(struct bsdtar *bsdtar, off_t progress)
108 {
109
110         /* If there's a signal to handle and we know what we're doing... */
111         if ((siginfo_received == 1) &&
112             (bsdtar->siginfo->path != NULL) &&
113             (bsdtar->siginfo->oper != NULL)) {
114                 if (bsdtar->verbose)
115                         fprintf(stderr, "\n");
116                 if (bsdtar->siginfo->size > 0) {
117                         safe_fprintf(stderr, "%s %s (%ju / %" PRId64 ")",
118                             bsdtar->siginfo->oper, bsdtar->siginfo->path,
119                             (uintmax_t)progress, bsdtar->siginfo->size);
120                 } else {
121                         safe_fprintf(stderr, "%s %s",
122                             bsdtar->siginfo->oper, bsdtar->siginfo->path);
123                 }
124                 if (!bsdtar->verbose)
125                         fprintf(stderr, "\n");
126                 siginfo_received = 0;
127         }
128 }
129
130 void
131 siginfo_done(struct bsdtar *bsdtar)
132 {
133
134 #ifdef SIGINFO
135         /* Restore old SIGINFO handler. */
136         signal(SIGINFO, bsdtar->siginfo->siginfo_old);
137 #endif
138         /* And the old SIGUSR1 handler, too. */
139         signal(SIGUSR1, bsdtar->siginfo->sigusr1_old);
140
141         /* Free strings. */
142         free(bsdtar->siginfo->path);
143         free(bsdtar->siginfo->oper);
144
145         /* Free internal data structure. */
146         free(bsdtar->siginfo);
147 }