Files
kopia/cli/command_content_list.go
Jarek Kowalski 40510c043d Support for content-level compression (#1076)
* cli: added a flag to create repository with v2 index features

* content: plumb through compression.ID parameter to content.Manager.WriteContent()

* content: expose content.Manager.SupportsContentCompression

This allows object manager to decide whether to create compressed object
or let the content manager do it.

* object: if compression is requested and the repo supports it, pass compression ID to the content manager

* cli: show compression status in 'repository status'

* cli: output compression information in 'content list' and 'content stats'

* content: compression and decompression support

* content: unit tests for compression

* object: compression tests

* testing: added integration tests against v2 index

* testing: run all e2e tests with and without content-level compression

* htmlui: added UI for specifying index format on creation

* cli: additional tests for 'content ls' and 'content stats'

* applied pr suggestions
2021-05-22 05:35:27 -07:00

138 lines
3.3 KiB
Go

package cli
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/stats"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/compression"
"github.com/kopia/kopia/repo/content"
)
type commandContentList struct {
long bool
includeDeleted bool
deletedOnly bool
summary bool
human bool
compression bool
contentRange contentRangeFlags
jo jsonOutput
out textOutput
}
func (c *commandContentList) setup(svc appServices, parent commandParent) {
cmd := parent.Command("list", "List contents").Alias("ls")
cmd.Flag("long", "Long output").Short('l').BoolVar(&c.long)
cmd.Flag("compression", "Compression").Short('c').BoolVar(&c.compression)
cmd.Flag("deleted", "Include deleted content").BoolVar(&c.includeDeleted)
cmd.Flag("deleted-only", "Only show deleted content").BoolVar(&c.deletedOnly)
cmd.Flag("summary", "Summarize the list").Short('s').BoolVar(&c.summary)
cmd.Flag("human", "Human-readable output").Short('h').BoolVar(&c.human)
c.contentRange.setup(cmd)
c.jo.setup(svc, cmd)
c.out.setup(svc)
cmd.Action(svc.directRepositoryReadAction(c.run))
}
func (c *commandContentList) run(ctx context.Context, rep repo.DirectRepository) error {
var jl jsonList
jl.begin(&c.jo)
defer jl.end()
var totalSize stats.CountSum
err := rep.ContentReader().IterateContents(
ctx,
content.IterateOptions{
Range: c.contentRange.contentIDRange(),
IncludeDeleted: c.includeDeleted || c.deletedOnly,
},
func(b content.Info) error {
if c.deletedOnly && !b.GetDeleted() {
return nil
}
totalSize.Add(int64(b.GetPackedLength()))
switch {
case c.jo.jsonOutput:
jl.emit(b)
case c.compression:
c.outputCompressed(b)
case c.long:
c.outputLong(b)
default:
c.out.printStdout("%v\n", b.GetContentID())
}
return nil
})
if err != nil {
return errors.Wrap(err, "error iterating")
}
if c.summary {
count, sz := totalSize.Approximate()
c.out.printStdout("Total: %v contents, %v total size\n",
maybeHumanReadableCount(c.human, int64(count)),
maybeHumanReadableBytes(c.human, sz))
}
return nil
}
func (c *commandContentList) outputLong(b content.Info) {
c.out.printStdout("%v %v %v %v %v+%v%v %v\n",
b.GetContentID(),
b.GetOriginalLength(),
formatTimestamp(b.Timestamp()),
b.GetPackBlobID(),
b.GetPackOffset(),
maybeHumanReadableBytes(c.human, int64(b.GetPackedLength())),
c.deletedInfoString(b),
c.compressionInfoStringString(b),
)
}
func (c *commandContentList) outputCompressed(b content.Info) {
c.out.printStdout("%v length %v packed %v %v %v\n",
b.GetContentID(),
maybeHumanReadableBytes(c.human, int64(b.GetOriginalLength())),
maybeHumanReadableBytes(c.human, int64(b.GetPackedLength())),
c.compressionInfoStringString(b),
c.deletedInfoString(b),
)
}
func (*commandContentList) deletedInfoString(b content.Info) string {
if b.GetDeleted() {
return " (deleted)"
}
return ""
}
func (*commandContentList) compressionInfoStringString(b content.Info) string {
h := b.GetCompressionHeaderID()
if h == content.NoCompression {
return "-"
}
s := string(compression.HeaderIDToName[h])
if s == "" {
s = fmt.Sprintf("compression-%x", h)
}
if b.GetOriginalLength() > 0 {
s += " " + formatCompressionPercentage(int64(b.GetOriginalLength()), int64(b.GetPackedLength()))
}
return s
}