mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
zsh reinit fixes (#477)
* reset command now initiates and completes async so there is feedback that something is happening when it takes a long time * switch from standard rpc to rpciter * checkpoint on reinit -- stream output, stats packet, logging to cmd pty, new endBytes for EOF * make generic versions of endbytes scanner and channel output funcs * update bash to use more modern state parsing (tricks learned from zsh) * verbose mode, fix stats output message * add a diff when verbose mode is on
This commit is contained in:
@@ -10,7 +10,9 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
mathrand "math/rand"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -552,3 +554,60 @@ func StrArrayToMap(sarr []string) map[string]bool {
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func AppendNonZeroRandomBytes(b []byte, randLen int) []byte {
|
||||
if randLen <= 0 {
|
||||
return b
|
||||
}
|
||||
numAdded := 0
|
||||
for numAdded < randLen {
|
||||
rn := mathrand.Intn(256)
|
||||
if rn > 0 && rn < 256 { // exclude 0, also helps to suppress security warning to have a guard here
|
||||
b = append(b, byte(rn))
|
||||
numAdded++
|
||||
}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// returns (isEOF, error)
|
||||
func CopyWithEndBytes(outputBuf *bytes.Buffer, reader io.Reader, endBytes []byte) (bool, error) {
|
||||
buf := make([]byte, 4096)
|
||||
for {
|
||||
n, err := reader.Read(buf)
|
||||
if n > 0 {
|
||||
outputBuf.Write(buf[:n])
|
||||
obytes := outputBuf.Bytes()
|
||||
if bytes.HasSuffix(obytes, endBytes) {
|
||||
outputBuf.Truncate(len(obytes) - len(endBytes))
|
||||
return (err == io.EOF), nil
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
return true, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// does *not* close outputCh on EOF or error
|
||||
func CopyToChannel(outputCh chan<- []byte, reader io.Reader) error {
|
||||
buf := make([]byte, 4096)
|
||||
for {
|
||||
n, err := reader.Read(buf)
|
||||
if n > 0 {
|
||||
// copy so client can use []byte without it being overwritten
|
||||
bufCopy := make([]byte, n)
|
||||
copy(bufCopy, buf[:n])
|
||||
outputCh <- bufCopy
|
||||
}
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user