From 04fe236a5fa5fa1fc76b0f7bd7c9232226003bf7 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Tue, 5 Jul 2022 22:54:27 -0700 Subject: [PATCH] fix(cli): fixed snapshot sizes in the snapshot list (#2148) This is caused by a fix where fs.Directory was incorrectly reporting its size == total size of all files in all subdirectories and `snapshot list` was relying on that. Fixes #2144 --- cli/command_snapshot_list.go | 34 ++++++++++++++++++++++--------- cli/command_snapshot_list_test.go | 30 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/cli/command_snapshot_list.go b/cli/command_snapshot_list.go index ffbdc284b..811cd67e8 100644 --- a/cli/command_snapshot_list.go +++ b/cli/command_snapshot_list.go @@ -378,8 +378,24 @@ func (c *commandSnapshotList) entryBits(ctx context.Context, m *snapshot.Manifes bits = append(bits, "incomplete:"+m.IncompleteReason) } + var summary *fs.DirectorySummary + + if dws, ok := ent.(fs.DirectoryWithSummary); ok { + s, err := dws.Summary(ctx) + if err != nil { + log(ctx).Warnw("unable to get directory summary", "name", ent.Name(), "err", err) + } + + summary = s + } + + totalBytes := ent.Size() + if summary != nil { + totalBytes = summary.TotalFileSize + } + bits = append(bits, - maybeHumanReadableBytes(c.snapshotListShowHumanReadable, ent.Size()), + maybeHumanReadableBytes(c.snapshotListShowHumanReadable, totalBytes), ent.Mode().String()) if c.shapshotListShowOwner { bits = append(bits, @@ -399,15 +415,13 @@ func (c *commandSnapshotList) entryBits(ctx context.Context, m *snapshot.Manifes bits = append(bits, deltaBytes(ent.Size()-lastTotalFileSize)) } - if dws, ok := ent.(fs.DirectoryWithSummary); ok { - if s, _ := dws.Summary(ctx); s != nil { - bits = append(bits, - fmt.Sprintf("files:%v", s.TotalFileCount), - fmt.Sprintf("dirs:%v", s.TotalDirCount)) - if s.FatalErrorCount > 0 { - bits = append(bits, fmt.Sprintf("errors:%v", s.FatalErrorCount)) - col = errorColor - } + if summary != nil { + bits = append(bits, + fmt.Sprintf("files:%v", summary.TotalFileCount), + fmt.Sprintf("dirs:%v", summary.TotalDirCount)) + if summary.FatalErrorCount > 0 { + bits = append(bits, fmt.Sprintf("errors:%v", summary.FatalErrorCount)) + col = errorColor } } diff --git a/cli/command_snapshot_list_test.go b/cli/command_snapshot_list_test.go index 161203d12..b482323c1 100644 --- a/cli/command_snapshot_list_test.go +++ b/cli/command_snapshot_list_test.go @@ -47,4 +47,34 @@ func TestSnapshotList(t *testing.T) { for _, s := range snapshots { require.NotEmpty(t, s.RetentionReasons, "expecting retention reason to be set") } + + lines := e.RunAndExpectSuccess(t, "snapshot", "list") + require.Len(t, lines, 5) + + require.Contains(t, lines[1], " 3 B ") + require.Contains(t, lines[1], " files:1 dirs:1 ") + + require.Contains(t, lines[2], " 7 B ") + require.Contains(t, lines[2], " files:2 dirs:1 ") + + require.Contains(t, lines[3], " 8 B ") + require.Contains(t, lines[3], " files:3 dirs:1 ") + + require.Contains(t, lines[4], "+ 1 identical snapshots until") + + lines = e.RunAndExpectSuccess(t, "snapshot", "list", "-l") + require.Len(t, lines, 5) + + require.Contains(t, lines[1], " 3 B ") + require.Contains(t, lines[1], " files:1 dirs:1 ") + + require.Contains(t, lines[2], " 7 B ") + require.Contains(t, lines[2], " files:2 dirs:1 ") + + require.Contains(t, lines[3], " 8 B ") + require.Contains(t, lines[3], " files:3 dirs:1 ") + + // identical snapshot is not coalesced + require.Contains(t, lines[4], " 8 B ") + require.Contains(t, lines[4], " files:3 dirs:1 ") }