mirror of
https://github.com/kopia/kopia.git
synced 2026-01-25 06:48:48 -05:00
Fixes #690 This is a breaking change for folks who are expecting snapshots to fail quickly without writing a snapshot manifest in case of an error. Before this change, any source read failure would cause the entire snapshot to fail (and not write a snapshot manifest as a result), unless `ignoreFileErrors` or `ignoreDirectoryErrors` was set. The new behavior is to continue snapshotting remaining files and directories (this can be disabled by passing `--fail-fast` flag or setting `KOPIA_SNAPSHOT_FAIL_FAST=1` environment variable) and defer returning an error until the very end. After snapshotting we will always attempt to write the snapshot manifest (except when the root of the snapshot itself cannot be opened). In case of a fail-fast error, the manifest will be marked as 'partial' and the directory tree will contain only partial set of files. In case of any errors, the manifest (and each directory object) will list the number if failures and no more than 10 examples of failed files/directories along with their respective errors. Once the snapshot is complete we will return non-zero exit code to the operating system if there were any fatal errors during snapshotting. With this change we are repurposing `ignoreFileErrors` and `ignoreDirectoryErrors` to designate some errors as non-fatal. Non-fatal errors are reported as warnings in the logs and will not cause a non-zero exit code to be returned.
133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/fs"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/object"
|
|
"github.com/kopia/kopia/snapshot/snapshotfs"
|
|
)
|
|
|
|
var (
|
|
lsCommand = app.Command("list", "List a directory stored in repository object.").Alias("ls")
|
|
|
|
lsCommandLong = lsCommand.Flag("long", "Long output").Short('l').Bool()
|
|
lsCommandRecursive = lsCommand.Flag("recursive", "Recursive output").Short('r').Bool()
|
|
lsCommandShowOID = lsCommand.Flag("show-object-id", "Show object IDs").Short('o').Bool()
|
|
lsCommandErrorSummary = lsCommand.Flag("error-summary", "Emit error summary").Default("true").Bool()
|
|
lsCommandPath = lsCommand.Arg("object-path", "Path").Required().String()
|
|
)
|
|
|
|
func runLSCommand(ctx context.Context, rep repo.Repository) error {
|
|
dir, err := snapshotfs.FilesystemDirectoryFromIDWithPath(ctx, rep, *lsCommandPath, false)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to get filesystem directory entry")
|
|
}
|
|
|
|
var prefix string
|
|
if !*lsCommandLong {
|
|
prefix = *lsCommandPath
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
prefix += "/"
|
|
}
|
|
}
|
|
|
|
return listDirectory(ctx, dir, prefix, "")
|
|
}
|
|
|
|
func init() {
|
|
lsCommand.Action(repositoryReaderAction(runLSCommand))
|
|
}
|
|
|
|
func listDirectory(ctx context.Context, d fs.Directory, prefix, indent string) error {
|
|
entries, err := d.Readdir(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error reading directory")
|
|
}
|
|
|
|
for _, e := range entries {
|
|
if err := printDirectoryEntry(ctx, e, prefix, indent); err != nil {
|
|
return errors.Wrap(err, "unable to print directory entry")
|
|
}
|
|
}
|
|
|
|
if dws, ok := d.(fs.DirectoryWithSummary); ok && *lsCommandErrorSummary {
|
|
if ds, _ := dws.Summary(ctx); ds != nil && ds.FatalErrorCount > 0 {
|
|
errorColor.Fprintf(os.Stderr, "\nNOTE: Encountered %v errors while snapshotting this directory:\n\n", ds.FatalErrorCount) //nolint:errcheck
|
|
|
|
for _, e := range ds.FailedEntries {
|
|
errorColor.Fprintf(os.Stderr, "- Error in \"%v\": %v\n", e.EntryPath, e.Error) //nolint:errcheck
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func printDirectoryEntry(ctx context.Context, e fs.Entry, prefix, indent string) error {
|
|
objectID := e.(object.HasObjectID).ObjectID()
|
|
oid := objectID.String()
|
|
col := defaultColor
|
|
|
|
var (
|
|
errorSummary string
|
|
info string
|
|
)
|
|
|
|
if dws, ok := e.(fs.DirectoryWithSummary); ok && *lsCommandErrorSummary {
|
|
if ds, _ := dws.Summary(ctx); ds != nil && ds.FatalErrorCount > 0 {
|
|
errorSummary = fmt.Sprintf(" (%v errors)", ds.FatalErrorCount)
|
|
col = errorColor
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case *lsCommandLong:
|
|
info = fmt.Sprintf(
|
|
"%v %12d %v %-34v %v%v",
|
|
e.Mode(),
|
|
e.Size(),
|
|
formatTimestamp(e.ModTime().Local()),
|
|
oid,
|
|
nameToDisplay(prefix, e),
|
|
errorSummary,
|
|
)
|
|
case *lsCommandShowOID:
|
|
info = fmt.Sprintf("%-34v %v%v", oid, nameToDisplay(prefix, e), errorSummary)
|
|
|
|
default:
|
|
info = fmt.Sprintf("%v%v", nameToDisplay(prefix, e), errorSummary)
|
|
}
|
|
|
|
col.Println(info) //nolint:errcheck
|
|
|
|
if *lsCommandRecursive {
|
|
if subdir, ok := e.(fs.Directory); ok {
|
|
if listerr := listDirectory(ctx, subdir, prefix+e.Name()+"/", indent+" "); listerr != nil {
|
|
return listerr
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func nameToDisplay(prefix string, e fs.Entry) string {
|
|
suffix := ""
|
|
if e.IsDir() {
|
|
suffix = "/"
|
|
}
|
|
|
|
if *lsCommandLong || *lsCommandRecursive {
|
|
return prefix + e.Name() + suffix
|
|
}
|
|
|
|
return e.Name()
|
|
}
|