mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
Fix mangled JSON output by backup command
jsonstatus wrote the JSON output without synchronization to the stdio_wrapper which caused mangling between different status lines. Use the Print and Error methods of termstatus instead which use a central goroutine to synchronize output.
This commit is contained in:
parent
af20015725
commit
ef70a2fcb3
@ -1,6 +1,7 @@
|
|||||||
package jsonstatus
|
package jsonstatus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
@ -74,6 +75,20 @@ func NewBackup(term *termstatus.Terminal, verbosity uint) *Backup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toJSONString(status interface{}) string {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
json.NewEncoder(buf).Encode(status)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Backup) print(status interface{}) {
|
||||||
|
b.term.Print(toJSONString(status))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Backup) error(status interface{}) {
|
||||||
|
b.term.Error(toJSONString(status))
|
||||||
|
}
|
||||||
|
|
||||||
// Run regularly updates the status lines. It should be called in a separate
|
// Run regularly updates the status lines. It should be called in a separate
|
||||||
// goroutine.
|
// goroutine.
|
||||||
func (b *Backup) Run(ctx context.Context) error {
|
func (b *Backup) Run(ctx context.Context) error {
|
||||||
@ -162,13 +177,13 @@ func (b *Backup) update(total, processed counter, errors uint, currentFiles map[
|
|||||||
}
|
}
|
||||||
sort.Strings(status.CurrentFiles)
|
sort.Strings(status.CurrentFiles)
|
||||||
|
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(status)
|
b.print(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScannerError is the error callback function for the scanner, it prints the
|
// ScannerError is the error callback function for the scanner, it prints the
|
||||||
// error in verbose mode and returns nil.
|
// error in verbose mode and returns nil.
|
||||||
func (b *Backup) ScannerError(item string, fi os.FileInfo, err error) error {
|
func (b *Backup) ScannerError(item string, fi os.FileInfo, err error) error {
|
||||||
json.NewEncoder(b.StdioWrapper.Stderr()).Encode(errorUpdate{
|
b.error(errorUpdate{
|
||||||
MessageType: "error",
|
MessageType: "error",
|
||||||
Error: err,
|
Error: err,
|
||||||
During: "scan",
|
During: "scan",
|
||||||
@ -179,7 +194,7 @@ func (b *Backup) ScannerError(item string, fi os.FileInfo, err error) error {
|
|||||||
|
|
||||||
// Error is the error callback function for the archiver, it prints the error and returns nil.
|
// Error is the error callback function for the archiver, it prints the error and returns nil.
|
||||||
func (b *Backup) Error(item string, fi os.FileInfo, err error) error {
|
func (b *Backup) Error(item string, fi os.FileInfo, err error) error {
|
||||||
json.NewEncoder(b.StdioWrapper.Stderr()).Encode(errorUpdate{
|
b.error(errorUpdate{
|
||||||
MessageType: "error",
|
MessageType: "error",
|
||||||
Error: err,
|
Error: err,
|
||||||
During: "archival",
|
During: "archival",
|
||||||
@ -233,7 +248,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
if current.Type == "dir" {
|
if current.Type == "dir" {
|
||||||
if previous == nil {
|
if previous == nil {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "new",
|
Action: "new",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -250,7 +265,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
|
|
||||||
if previous.Equals(*current) {
|
if previous.Equals(*current) {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "unchanged",
|
Action: "unchanged",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -261,7 +276,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
b.summary.Unlock()
|
b.summary.Unlock()
|
||||||
} else {
|
} else {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "modified",
|
Action: "modified",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -284,7 +299,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
|
|
||||||
if previous == nil {
|
if previous == nil {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "new",
|
Action: "new",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -300,7 +315,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
|
|
||||||
if previous.Equals(*current) {
|
if previous.Equals(*current) {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "unchanged",
|
Action: "unchanged",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -311,7 +326,7 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc
|
|||||||
b.summary.Unlock()
|
b.summary.Unlock()
|
||||||
} else {
|
} else {
|
||||||
if b.v >= 3 {
|
if b.v >= 3 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "verbose_status",
|
MessageType: "verbose_status",
|
||||||
Action: "modified",
|
Action: "modified",
|
||||||
Item: item,
|
Item: item,
|
||||||
@ -335,7 +350,7 @@ func (b *Backup) ReportTotal(item string, s archiver.ScanStats) {
|
|||||||
|
|
||||||
if item == "" {
|
if item == "" {
|
||||||
if b.v >= 2 {
|
if b.v >= 2 {
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(verboseUpdate{
|
b.print(verboseUpdate{
|
||||||
MessageType: "status",
|
MessageType: "status",
|
||||||
Action: "scan_finished",
|
Action: "scan_finished",
|
||||||
Duration: time.Since(b.start).Seconds(),
|
Duration: time.Since(b.start).Seconds(),
|
||||||
@ -351,7 +366,7 @@ func (b *Backup) ReportTotal(item string, s archiver.ScanStats) {
|
|||||||
// Finish prints the finishing messages.
|
// Finish prints the finishing messages.
|
||||||
func (b *Backup) Finish(snapshotID restic.ID) {
|
func (b *Backup) Finish(snapshotID restic.ID) {
|
||||||
close(b.finished)
|
close(b.finished)
|
||||||
json.NewEncoder(b.StdioWrapper.Stdout()).Encode(summaryOutput{
|
b.print(summaryOutput{
|
||||||
MessageType: "summary",
|
MessageType: "summary",
|
||||||
FilesNew: b.summary.Files.New,
|
FilesNew: b.summary.Files.New,
|
||||||
FilesChanged: b.summary.Files.Changed,
|
FilesChanged: b.summary.Files.Changed,
|
||||||
|
Loading…
Reference in New Issue
Block a user