Upgrade to libarchive-2.7.0.
[dragonfly.git] / contrib / libarchive / tar / siginfo.c
blob5f28e230036826f4c9beb0e04386328c01b520ee
1 /*-
2 * Copyright 2008 Colin Percival
3 * All rights reserved.
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.
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.
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 $");
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "bsdtar.h"
37 /* Is there a pending SIGINFO or SIGUSR1? */
38 static volatile sig_atomic_t siginfo_received = 0;
40 struct siginfo_data {
41 /* What sort of operation are we doing? */
42 char * oper;
44 /* What path are we handling? */
45 char * path;
47 /* How large is the archive entry? */
48 int64_t size;
50 /* Old signal handlers. */
51 #ifdef SIGINFO
52 void (*siginfo_old)(int);
53 #endif
54 void (*sigusr1_old)(int);
57 static void siginfo_handler(int sig);
59 /* Handler for SIGINFO / SIGUSR1. */
60 static void
61 siginfo_handler(int sig)
64 (void)sig; /* UNUSED */
66 /* Record that SIGINFO or SIGUSR1 has been received. */
67 siginfo_received = 1;
70 void
71 siginfo_init(struct bsdtar *bsdtar)
74 /* Allocate space for internal structure. */
75 if ((bsdtar->siginfo = malloc(sizeof(struct siginfo_data))) == NULL)
76 bsdtar_errc(bsdtar, 1, errno, "malloc failed");
78 /* Set the strings to NULL so that free() is safe. */
79 bsdtar->siginfo->path = bsdtar->siginfo->oper = NULL;
81 #ifdef SIGINFO
82 /* We want to catch SIGINFO, if it exists. */
83 bsdtar->siginfo->siginfo_old = signal(SIGINFO, siginfo_handler);
84 #endif
85 #ifdef SIGUSR1
86 /* ... and treat SIGUSR1 the same way as SIGINFO. */
87 bsdtar->siginfo->sigusr1_old = signal(SIGUSR1, siginfo_handler);
88 #endif
91 void
92 siginfo_setinfo(struct bsdtar *bsdtar, const char * oper, const char * path,
93 int64_t size)
96 /* Free old operation and path strings. */
97 free(bsdtar->siginfo->oper);
98 free(bsdtar->siginfo->path);
100 /* Duplicate strings and store entry size. */
101 if ((bsdtar->siginfo->oper = strdup(oper)) == NULL)
102 bsdtar_errc(bsdtar, 1, errno, "Cannot strdup");
103 if ((bsdtar->siginfo->path = strdup(path)) == NULL)
104 bsdtar_errc(bsdtar, 1, errno, "Cannot strdup");
105 bsdtar->siginfo->size = size;
108 void
109 siginfo_printinfo(struct bsdtar *bsdtar, off_t progress)
112 /* If there's a signal to handle and we know what we're doing... */
113 if ((siginfo_received == 1) &&
114 (bsdtar->siginfo->path != NULL) &&
115 (bsdtar->siginfo->oper != NULL)) {
116 if (bsdtar->verbose)
117 fprintf(stderr, "\n");
118 if (bsdtar->siginfo->size > 0) {
119 safe_fprintf(stderr, "%s %s (%ju / %" PRId64 ")",
120 bsdtar->siginfo->oper, bsdtar->siginfo->path,
121 (uintmax_t)progress, bsdtar->siginfo->size);
122 } else {
123 safe_fprintf(stderr, "%s %s",
124 bsdtar->siginfo->oper, bsdtar->siginfo->path);
126 if (!bsdtar->verbose)
127 fprintf(stderr, "\n");
128 siginfo_received = 0;
132 void
133 siginfo_done(struct bsdtar *bsdtar)
136 #ifdef SIGINFO
137 /* Restore old SIGINFO handler. */
138 signal(SIGINFO, bsdtar->siginfo->siginfo_old);
139 #endif
140 #ifdef SIGUSR1
141 /* And the old SIGUSR1 handler, too. */
142 signal(SIGUSR1, bsdtar->siginfo->sigusr1_old);
143 #endif
145 /* Free strings. */
146 free(bsdtar->siginfo->path);
147 free(bsdtar->siginfo->oper);
149 /* Free internal data structure. */
150 free(bsdtar->siginfo);