fix: limit cmd log length to 32KB per operation

This commit is contained in:
garethgeorge
2024-04-06 14:10:15 -07:00
parent 3770966111
commit 92d52bed8e
5 changed files with 16 additions and 50 deletions
-41
View File
@@ -1,41 +0,0 @@
package fsutil
import (
"fmt"
"os"
"path"
)
// CollapseSubdirectory recursively moves the subdir up towards parent and removes any (newly empty) directories on the way.
func CollapseSubdirectory(p, subdir string) (string, error) {
if p == subdir {
return p, nil
}
dir := path.Dir(subdir)
ents, err := os.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("read dir %q: %w", dir, err)
}
base := path.Base(subdir)
for _, ent := range ents {
if ent.Name() != base && ent.Name() != "." && ent.Name() != ".." {
return subdir, nil
}
}
if err := os.Rename(subdir, dir+".tmp"); err != nil {
return "", fmt.Errorf("rename %q to %q: %w", subdir, dir+".tmp", err)
}
if err := os.Remove(dir); err != nil {
return "", fmt.Errorf("remove %q: %w", dir, err)
}
if err := os.Rename(dir+".tmp", base); err != nil {
return "", fmt.Errorf("rename %q to %q: %w", dir+".tmp", base, err)
}
return CollapseSubdirectory(p, dir)
}
+3 -3
View File
@@ -1,7 +1,6 @@
package orchestrator
import (
"bytes"
"context"
"errors"
"fmt"
@@ -9,6 +8,7 @@ import (
"time"
v1 "github.com/garethgeorge/backrest/gen/go/v1"
"github.com/garethgeorge/backrest/internal/ioutil"
"github.com/garethgeorge/backrest/internal/oplog"
"github.com/garethgeorge/backrest/pkg/restic"
"github.com/hashicorp/go-multierror"
@@ -62,8 +62,8 @@ func (t *TaskWithOperation) runWithOpAndContext(ctx context.Context, do func(ctx
}()
return WithOperation(t.orch.OpLog, t.op, func() error {
buf := bytes.NewBuffer(nil)
ctx = restic.ContextWithLogger(ctx, buf)
capture := ioutil.NewOutputCapturer(32_000) // 32k of logs
ctx = restic.ContextWithLogger(ctx, capture)
err := do(ctx, t.op)