From 641dc65e6e44c64c34cab4ed19d827cf237cd5e5 Mon Sep 17 00:00:00 2001 From: Johannes Hertenstein Date: Sun, 7 Oct 2018 14:37:51 +0200 Subject: [PATCH 1/3] Output directory size in cache command --- cmd/restic/cmd_cache.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cmd/restic/cmd_cache.go b/cmd/restic/cmd_cache.go index 04e60908f..ef4edc9b2 100644 --- a/cmd/restic/cmd_cache.go +++ b/cmd/restic/cmd_cache.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "path/filepath" "sort" "time" @@ -29,6 +30,7 @@ The "cache" command allows listing and cleaning local cache directories. type CacheOptions struct { Cleanup bool MaxAge uint + NoSize bool } var cacheOptions CacheOptions @@ -39,6 +41,7 @@ func init() { f := cmdCache.Flags() f.BoolVar(&cacheOptions.Cleanup, "cleanup", false, "remove old cache directories") f.UintVar(&cacheOptions.MaxAge, "max-age", 30, "max age in `days` for cache directories to be considered old") + f.BoolVar(&cacheOptions.NoSize, "no-size", false, "do not output the size of the cache directories") } func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { @@ -92,12 +95,17 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { ID string Last string Old string + Size string } tab.AddColumn("Repo ID", "{{ .ID }}") tab.AddColumn("Last Used", "{{ .Last }}") tab.AddColumn("Old", "{{ .Old }}") + if !opts.NoSize { + tab.AddColumn("Size", "{{ .Size }}") + } + dirs, err := cache.All(cachedir) if err != nil { return err @@ -118,10 +126,20 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { old = "yes" } + var size string + if !opts.NoSize { + bytes, err := dirSize(fmt.Sprintf("%s/%s", cachedir, entry.Name())) + if err != nil { + return err + } + size = fmt.Sprintf("%11s", formatBytes(uint64(bytes))) + } + tab.AddRow(data{ entry.Name()[:10], fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)), old, + size, }) } @@ -130,3 +148,14 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { return nil } + +func dirSize(path string) (int64, error) { + var size int64 + err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + if !info.IsDir() { + size += info.Size() + } + return err + }) + return size, err +} From ed651df19b61e2f9ee397be0ff7cf353b309721c Mon Sep 17 00:00:00 2001 From: Johannes Hertenstein Date: Mon, 8 Oct 2018 15:47:08 +0200 Subject: [PATCH 2/3] Use correct method for joining paths --- cmd/restic/cmd_cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/restic/cmd_cache.go b/cmd/restic/cmd_cache.go index ef4edc9b2..f6823f621 100644 --- a/cmd/restic/cmd_cache.go +++ b/cmd/restic/cmd_cache.go @@ -128,7 +128,7 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { var size string if !opts.NoSize { - bytes, err := dirSize(fmt.Sprintf("%s/%s", cachedir, entry.Name())) + bytes, err := dirSize(filepath.Join(cachedir, entry.Name())) if err != nil { return err } From 277cba4b32f5ded77998699c754821688cd30019 Mon Sep 17 00:00:00 2001 From: Johannes Hertenstein Date: Mon, 8 Oct 2018 15:47:34 +0200 Subject: [PATCH 3/3] Catch errors when walking cache directories --- cmd/restic/cmd_cache.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/restic/cmd_cache.go b/cmd/restic/cmd_cache.go index f6823f621..9a2ebd826 100644 --- a/cmd/restic/cmd_cache.go +++ b/cmd/restic/cmd_cache.go @@ -152,10 +152,15 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error { func dirSize(path string) (int64, error) { var size int64 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + if err != nil || info == nil { + return err + } + if !info.IsDir() { size += info.Size() } - return err + + return nil }) return size, err }