1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
mod init {
use std::{
io,
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};
static IS_INITIALIZED: AtomicBool = AtomicBool::new(false);
#[derive(Default)]
pub struct Deregister(Vec<(i32, signal_hook::SigId)>);
pub struct AutoDeregister(Deregister);
impl Deregister {
pub fn deregister(self) -> std::io::Result<()> {
if self.0.is_empty() {
return Ok(());
}
static REINSTATE_DEFAULT_BEHAVIOUR: AtomicBool = AtomicBool::new(true);
for (_, hook_id) in &self.0 {
signal_hook::low_level::unregister(*hook_id);
}
IS_INITIALIZED.store(false, Ordering::SeqCst);
if REINSTATE_DEFAULT_BEHAVIOUR
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(false))
.expect("always returns value")
{
for (sig, _) in self.0 {
#[allow(unsafe_code)]
unsafe {
signal_hook::low_level::register(sig, move || {
signal_hook::low_level::emulate_default_handler(sig).ok();
})?;
}
}
}
Ok(())
}
pub fn auto_deregister(self) -> AutoDeregister {
AutoDeregister(self)
}
}
impl Drop for AutoDeregister {
fn drop(&mut self) {
std::mem::take(&mut self.0).deregister().ok();
}
}
pub fn init_handler(interrupt: impl Fn() + Send + Sync + Clone + 'static) -> io::Result<Deregister> {
if IS_INITIALIZED
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(true))
.expect("always returns value")
{
return Err(io::Error::new(io::ErrorKind::Other, "Already initialized"));
}
let mut hooks = Vec::with_capacity(signal_hook::consts::TERM_SIGNALS.len());
for sig in signal_hook::consts::TERM_SIGNALS {
let interrupt = interrupt.clone();
#[allow(unsafe_code)]
unsafe {
let hook_id = signal_hook::low_level::register(*sig, move || {
static INTERRUPT_COUNT: AtomicUsize = AtomicUsize::new(0);
if !super::is_triggered() {
INTERRUPT_COUNT.store(0, Ordering::SeqCst);
}
let msg_idx = INTERRUPT_COUNT.fetch_add(1, Ordering::SeqCst);
if msg_idx == 1 {
git_tempfile::handler::cleanup_tempfiles();
signal_hook::low_level::emulate_default_handler(*sig).ok();
}
interrupt();
super::trigger();
})?;
hooks.push((*sig, hook_id));
}
}
git_tempfile::setup(git_tempfile::SignalHandlerMode::None);
Ok(Deregister(hooks))
}
}
use std::{
io,
sync::atomic::{AtomicBool, Ordering},
};
pub use init::init_handler;
pub struct Iter<I, EFN> {
inner: git_features::interrupt::IterWithErr<'static, I, EFN>,
}
impl<I, EFN, E> Iter<I, EFN>
where
I: Iterator,
EFN: FnOnce() -> E,
{
pub fn new(inner: I, make_err: EFN) -> Self {
Iter {
inner: git_features::interrupt::IterWithErr::new(inner, make_err, &IS_INTERRUPTED),
}
}
pub fn into_inner(self) -> I {
self.inner.inner
}
pub fn inner(&self) -> &I {
&self.inner.inner
}
}
impl<I, EFN, E> Iterator for Iter<I, EFN>
where
I: Iterator,
EFN: FnOnce() -> E,
{
type Item = Result<I::Item, E>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
pub struct Read<R> {
inner: git_features::interrupt::Read<'static, R>,
}
impl<R> Read<R>
where
R: io::Read,
{
pub fn new(read: R) -> Self {
Read {
inner: git_features::interrupt::Read {
inner: read,
should_interrupt: &IS_INTERRUPTED,
},
}
}
pub fn into_inner(self) -> R {
self.inner.inner
}
}
impl<R> io::Read for Read<R>
where
R: io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<R> io::BufRead for Read<R>
where
R: io::BufRead,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
}
fn consume(&mut self, amt: usize) {
self.inner.consume(amt)
}
}
pub static IS_INTERRUPTED: AtomicBool = AtomicBool::new(false);
pub fn is_triggered() -> bool {
IS_INTERRUPTED.load(Ordering::Relaxed)
}
pub fn trigger() {
IS_INTERRUPTED.store(true, Ordering::SeqCst);
}
pub fn reset() {
IS_INTERRUPTED.store(false, Ordering::SeqCst);
}