2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 06:46:34 +00:00

improve fprintf related error handling

This commit is contained in:
Michael Eischer 2024-11-01 17:04:49 +01:00
parent 41fa41b28b
commit 569a117a1d
10 changed files with 43 additions and 33 deletions

View File

@ -351,12 +351,7 @@ func TestBackupExclude(t *testing.T) {
for _, filename := range backupExcludeFilenames { for _, filename := range backupExcludeFilenames {
fp := filepath.Join(datadir, filename) fp := filepath.Join(datadir, filename)
rtest.OK(t, os.MkdirAll(filepath.Dir(fp), 0755)) rtest.OK(t, os.MkdirAll(filepath.Dir(fp), 0755))
rtest.OK(t, os.WriteFile(fp, []byte(filename), 0o666))
f, err := os.Create(fp)
rtest.OK(t, err)
fmt.Fprint(f, filename)
rtest.OK(t, f.Close())
} }
snapshots := make(map[string]struct{}) snapshots := make(map[string]struct{})

View File

@ -39,21 +39,24 @@ func TestCollectTargets(t *testing.T) {
f1, err := os.Create(filepath.Join(dir, "fromfile")) f1, err := os.Create(filepath.Join(dir, "fromfile"))
rtest.OK(t, err) rtest.OK(t, err)
// Empty lines should be ignored. A line starting with '#' is a comment. // Empty lines should be ignored. A line starting with '#' is a comment.
fmt.Fprintf(f1, "\n%s*\n # here's a comment\n", f1.Name()) _, err = fmt.Fprintf(f1, "\n%s*\n # here's a comment\n", f1.Name())
rtest.OK(t, err)
rtest.OK(t, f1.Close()) rtest.OK(t, f1.Close())
f2, err := os.Create(filepath.Join(dir, "fromfile-verbatim")) f2, err := os.Create(filepath.Join(dir, "fromfile-verbatim"))
rtest.OK(t, err) rtest.OK(t, err)
for _, filename := range []string{fooSpace, barStar} { for _, filename := range []string{fooSpace, barStar} {
// Empty lines should be ignored. CR+LF is allowed. // Empty lines should be ignored. CR+LF is allowed.
fmt.Fprintf(f2, "%s\r\n\n", filepath.Join(dir, filename)) _, err = fmt.Fprintf(f2, "%s\r\n\n", filepath.Join(dir, filename))
rtest.OK(t, err)
} }
rtest.OK(t, f2.Close()) rtest.OK(t, f2.Close())
f3, err := os.Create(filepath.Join(dir, "fromfile-raw")) f3, err := os.Create(filepath.Join(dir, "fromfile-raw"))
rtest.OK(t, err) rtest.OK(t, err)
for _, filename := range []string{"baz", "quux"} { for _, filename := range []string{"baz", "quux"} {
fmt.Fprintf(f3, "%s\x00", filepath.Join(dir, filename)) _, err = fmt.Fprintf(f3, "%s\x00", filepath.Join(dir, filename))
rtest.OK(t, err)
} }
rtest.OK(t, err) rtest.OK(t, err)
rtest.OK(t, f3.Close()) rtest.OK(t, f3.Close())

View File

@ -296,7 +296,9 @@ func PrintSnapshotGroupHeader(stdout io.Writer, groupKeyJSON string) error {
} }
// Info // Info
fmt.Fprintf(stdout, "snapshots") if _, err := fmt.Fprintf(stdout, "snapshots"); err != nil {
return err
}
var infoStrings []string var infoStrings []string
if key.Hostname != "" { if key.Hostname != "" {
infoStrings = append(infoStrings, "host ["+key.Hostname+"]") infoStrings = append(infoStrings, "host ["+key.Hostname+"]")
@ -308,11 +310,13 @@ func PrintSnapshotGroupHeader(stdout io.Writer, groupKeyJSON string) error {
infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]") infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]")
} }
if infoStrings != nil { if infoStrings != nil {
fmt.Fprintf(stdout, " for (%s)", strings.Join(infoStrings, ", ")) if _, err := fmt.Fprintf(stdout, " for (%s)", strings.Join(infoStrings, ", ")); err != nil {
return err
}
} }
fmt.Fprintf(stdout, ":\n") _, err = fmt.Fprintf(stdout, ":\n")
return nil return err
} }
// Snapshot helps to print Snapshots as JSON with their ID included. // Snapshot helps to print Snapshots as JSON with their ID included.

View File

@ -308,7 +308,7 @@ func readPasswordTerminal(ctx context.Context, in *os.File, out *os.File, prompt
fd := int(out.Fd()) fd := int(out.Fd())
state, err := term.GetState(fd) state, err := term.GetState(fd)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to get terminal state: %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "unable to get terminal state: %v\n", err)
return "", err return "", err
} }
@ -317,16 +317,22 @@ func readPasswordTerminal(ctx context.Context, in *os.File, out *os.File, prompt
go func() { go func() {
defer close(done) defer close(done)
fmt.Fprint(out, prompt) _, err = fmt.Fprint(out, prompt)
if err != nil {
return
}
buf, err = term.ReadPassword(int(in.Fd())) buf, err = term.ReadPassword(int(in.Fd()))
fmt.Fprintln(out) if err != nil {
return
}
_, err = fmt.Fprintln(out)
}() }()
select { select {
case <-ctx.Done(): case <-ctx.Done():
err := term.Restore(fd, state) err := term.Restore(fd, state)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to restore terminal state: %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "unable to restore terminal state: %v\n", err)
} }
return "", ctx.Err() return "", ctx.Err()
case <-done: case <-done:

View File

@ -13,17 +13,17 @@ import (
func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool { func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool {
if e.path != other.path { if e.path != other.path {
fmt.Fprintf(out, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path) _, _ = fmt.Fprintf(out, "%v: path does not match (%v != %v)\n", e.path, e.path, other.path)
return false return false
} }
if e.fi.Mode() != other.fi.Mode() { if e.fi.Mode() != other.fi.Mode() {
fmt.Fprintf(out, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode()) _, _ = fmt.Fprintf(out, "%v: mode does not match (%v != %v)\n", e.path, e.fi.Mode(), other.fi.Mode())
return false return false
} }
if !sameModTime(e.fi, other.fi) { if !sameModTime(e.fi, other.fi) {
fmt.Fprintf(out, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime()) _, _ = fmt.Fprintf(out, "%v: ModTime does not match (%v != %v)\n", e.path, e.fi.ModTime(), other.fi.ModTime())
return false return false
} }
@ -31,17 +31,17 @@ func (e *dirEntry) equals(out io.Writer, other *dirEntry) bool {
stat2, _ := other.fi.Sys().(*syscall.Stat_t) stat2, _ := other.fi.Sys().(*syscall.Stat_t)
if stat.Uid != stat2.Uid { if stat.Uid != stat2.Uid {
fmt.Fprintf(out, "%v: UID does not match (%v != %v)\n", e.path, stat.Uid, stat2.Uid) _, _ = fmt.Fprintf(out, "%v: UID does not match (%v != %v)\n", e.path, stat.Uid, stat2.Uid)
return false return false
} }
if stat.Gid != stat2.Gid { if stat.Gid != stat2.Gid {
fmt.Fprintf(out, "%v: GID does not match (%v != %v)\n", e.path, stat.Gid, stat2.Gid) _, _ = fmt.Fprintf(out, "%v: GID does not match (%v != %v)\n", e.path, stat.Gid, stat2.Gid)
return false return false
} }
if stat.Nlink != stat2.Nlink { if stat.Nlink != stat2.Nlink {
fmt.Fprintf(out, "%v: Number of links do not match (%v != %v)\n", e.path, stat.Nlink, stat2.Nlink) _, _ = fmt.Fprintf(out, "%v: Number of links do not match (%v != %v)\n", e.path, stat.Nlink, stat2.Nlink)
return false return false
} }

View File

@ -140,7 +140,7 @@ func printExitError(code int, message string) {
return return
} }
} else { } else {
fmt.Fprintf(globalOptions.stderr, "%v\n", message) _, _ = fmt.Fprintf(globalOptions.stderr, "%v\n", message)
} }
} }
@ -152,10 +152,10 @@ func main() {
log.SetOutput(logBuffer) log.SetOutput(logBuffer)
err := feature.Flag.Apply(os.Getenv("RESTIC_FEATURES"), func(s string) { err := feature.Flag.Apply(os.Getenv("RESTIC_FEATURES"), func(s string) {
fmt.Fprintln(os.Stderr, s) _, _ = fmt.Fprintln(os.Stderr, s)
}) })
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) _, _ = fmt.Fprintln(os.Stderr, err)
Exit(1) Exit(1)
} }

View File

@ -106,7 +106,7 @@ func runRESTServer(ctx context.Context, t testing.TB, dir, reqListenAddr string)
matched = true matched = true
} }
} }
fmt.Fprintln(os.Stdout, line) // print all output to console _, _ = fmt.Fprintln(os.Stdout, line) // print all output to console
} }
}() }()

View File

@ -120,7 +120,7 @@ func goroutineNum() int {
runtime.Stack(b, false) runtime.Stack(b, false)
var num int var num int
fmt.Sscanf(string(b), "goroutine %d ", &num) _, _ = fmt.Sscanf(string(b), "goroutine %d ", &num)
return num return num
} }

View File

@ -42,7 +42,7 @@ func (rd *eofDetectReader) Close() error {
msg += fmt.Sprintf(", body: %q", buf) msg += fmt.Sprintf(", body: %q", buf)
} }
fmt.Fprintln(os.Stderr, msg) _, _ = fmt.Fprintln(os.Stderr, msg)
Log("%s: %+v", msg, errors.New("Close()")) Log("%s: %+v", msg, errors.New("Close()"))
} }
return rd.rd.Close() return rd.rd.Close()

View File

@ -212,7 +212,7 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) {
} }
if _, err := io.WriteString(dst, msg.line); err != nil { if _, err := io.WriteString(dst, msg.line); err != nil {
fmt.Fprintf(os.Stderr, "write failed: %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
} }
if flush == nil { if flush == nil {
@ -220,16 +220,18 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) {
} }
if err := flush(); err != nil { if err := flush(); err != nil {
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
} }
case stat := <-t.status: case stat := <-t.status:
for _, line := range stat.lines { for _, line := range stat.lines {
// Ensure that each message ends with exactly one newline. // Ensure that each message ends with exactly one newline.
fmt.Fprintln(t.wr, strings.TrimRight(line, "\n")) if _, err := fmt.Fprintln(t.wr, strings.TrimRight(line, "\n")); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
}
} }
if err := t.wr.Flush(); err != nil { if err := t.wr.Flush(); err != nil {
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
} }
} }
} }