-
Notifications
You must be signed in to change notification settings - Fork 33
/
wrapper_streamsource.go
168 lines (142 loc) · 4.36 KB
/
wrapper_streamsource.go
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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/term"
)
func (r *resticWrapper) prepareStreamSource() (io.ReadCloser, error) {
if r.profile.Backup != nil && r.profile.Backup.UseStdin {
if len(r.profile.Backup.StdinCommand) > 0 {
return r.prepareCommandStreamSource()
} else {
return r.prepareStdinStreamSource()
}
}
//nolint:nilnil
return nil, nil
}
func (r *resticWrapper) prepareStdinStreamSource() (io.ReadCloser, error) {
clog.Debug("redirecting stdin to the backup")
if r.stdin == nil {
return nil, fmt.Errorf("stdin was already consumed. cannot read it twice")
}
totalBytes := int64(0)
readCloser := &readerWrapper{
reader: r.stdin,
read: func(w *readerWrapper, bytes []byte) (n int, err error) {
n, err = w.reader.Read(bytes)
totalBytes += int64(n)
return
},
close: func(w *readerWrapper) error {
if totalBytes > 0 && r.stdin != nil {
r.stdin = nil
w.close = nil
}
return nil
},
}
return readCloser, nil
}
func (r *resticWrapper) prepareCommandStreamSource() (io.ReadCloser, error) {
clog.Debug("redirecting command output to the backup")
pipeReader, pipeWriter := io.Pipe()
bufferedWriter := bufio.NewWriterSize(pipeWriter, 8*1024)
commandSignals := make(chan os.Signal, 2)
signal.Notify(commandSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
go func() {
defer pipeWriter.Close()
defer bufferedWriter.Flush()
defer signal.Stop(commandSignals)
env := r.getEnvironment(true)
env = append(env, r.getProfileEnvironment()...)
for i, sourceCommand := range r.profile.Backup.StdinCommand {
clog.Debugf("starting 'stdin-command' command %d/%d: %s", i+1, len(r.profile.Backup.StdinCommand), sourceCommand)
rCommand := newShellCommand(sourceCommand, nil, env, r.getShell(), r.dryRun, commandSignals, nil)
rCommand.stdout = bufferedWriter
rCommand.stderr = term.GetErrorOutput()
_, stderr, err := runShellCommand(rCommand)
if err != nil {
// discard unflushed output
bufferedWriter.Reset(pipeWriter)
// push command error to reader
err = newCommandError(rCommand, stderr, fmt.Errorf("'stdin-command' on profile '%s': %w", r.profile.Name, err))
if closeError := pipeWriter.CloseWithError(err); closeError != nil {
clog.Errorf("Failed closing pipe for command '%s' after %w ; close error: %w", sourceCommand, err, closeError)
}
return
}
}
}()
closePipe := func() error {
defer func() {
clog.Debugf("stopping 'stdin-command'")
signal.Stop(commandSignals)
commandSignals <- os.Interrupt
}()
return pipeReader.Close()
}
// read from pipe to ensure the process started and returns content or error before restic is started
var initialReader io.Reader
{
initialBytes := make([]byte, 512)
if n, err := pipeReader.Read(initialBytes); err == nil || err == io.EOF {
clog.Debugf("initial %d bytes successfully read from 'stdin-command'", n)
initialReader = bytes.NewReader(initialBytes[:n])
} else {
_ = closePipe()
return nil, err
}
}
readCloser := &readerWrapper{
reader: io.MultiReader(initialReader, pipeReader),
read: func(w *readerWrapper, bytes []byte) (n int, err error) {
n, err = w.reader.Read(bytes)
// Stopping restic when stream source command fails while producing content
if err != nil && err != io.EOF {
clog.Errorf("interrupting '%s' after stdin read error: %s", r.command, err)
if runtime.GOOS == "windows" {
return // that will close stdin and stops restic
} else if r.sigChan != nil {
r.sigChan <- os.Interrupt
}
// Wait for the signal to arrive before allowing further read from stdin
time.Sleep(750 * time.Millisecond)
}
return
},
close: func(w *readerWrapper) error {
w.close = nil
return closePipe()
},
}
return readCloser, nil
}
type readerWrapper struct {
reader io.Reader
readLock, closeLock sync.Mutex
read func(w *readerWrapper, bytes []byte) (n int, err error)
close func(w *readerWrapper) error
}
func (w *readerWrapper) Read(bytes []byte) (n int, err error) {
w.readLock.Lock()
defer w.readLock.Unlock()
return w.read(w, bytes)
}
func (w *readerWrapper) Close() error {
w.closeLock.Lock()
defer w.closeLock.Unlock()
if w.close != nil {
return w.close(w)
}
return nil
}