From f94fcea14b237db897bf97eee1eb631d4ff67c63 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Mon, 23 Apr 2018 19:56:01 -0700 Subject: [PATCH] moved block cache to a subdirectory --- block/block_cache.go | 9 ++++++--- cli/command_repository_status.go | 34 ++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/block/block_cache.go b/block/block_cache.go index 947824844..3edb43104 100644 --- a/block/block_cache.go +++ b/block/block_cache.go @@ -3,6 +3,7 @@ import ( "context" "os" + "path/filepath" "time" "github.com/kopia/kopia/storage/filesystem" @@ -31,13 +32,15 @@ func newBlockCache(ctx context.Context, st storage.Storage, caching CachingOptio return nullBlockCache{st}, nil } - if _, err := os.Stat(caching.CacheDirectory); os.IsNotExist(err) { - if err := os.MkdirAll(caching.CacheDirectory, 0700); err != nil { + blockCacheDir := filepath.Join(caching.CacheDirectory, "blocks") + + if _, err := os.Stat(blockCacheDir); os.IsNotExist(err) { + if err := os.MkdirAll(blockCacheDir, 0700); err != nil { return nil, err } } cacheStorage, err := filesystem.New(context.Background(), &filesystem.Options{ - Path: caching.CacheDirectory, + Path: blockCacheDir, }) if err != nil { return nil, err diff --git a/cli/command_repository_status.go b/cli/command_repository_status.go index 59cb11443..1734f4c18 100644 --- a/cli/command_repository_status.go +++ b/cli/command_repository_status.go @@ -5,6 +5,7 @@ "encoding/json" "fmt" "io/ioutil" + "path/filepath" "reflect" "github.com/kopia/kopia/internal/scrubber" @@ -18,15 +19,11 @@ func runStatusCommand(ctx context.Context, rep *repo.Repository) error { fmt.Printf("Config file: %v\n", rep.ConfigFile) - entries, err := ioutil.ReadDir(rep.CacheDirectory) + fileCount, totalFileSize, err := scanCacheDir(filepath.Join(rep.CacheDirectory, "blocks")) if err != nil { fmt.Printf("Cache directory: %v (error: %v)\n", rep.CacheDirectory, err) } else { - var totalSize int64 - for _, e := range entries { - totalSize += e.Size() - } - fmt.Printf("Cache directory: %v (%v files, %v)\n", rep.CacheDirectory, len(entries), units.BytesStringBase2(totalSize)) + fmt.Printf("Cache directory: %v (%v files, %v)\n", rep.CacheDirectory, fileCount, units.BytesStringBase2(totalFileSize)) } fmt.Println() @@ -64,6 +61,31 @@ func runStatusCommand(ctx context.Context, rep *repo.Repository) error { return nil } +func scanCacheDir(dirname string) (fileCount int, totalFileLength int64, err error) { + entries, err := ioutil.ReadDir(dirname) + if err != nil { + return 0, 0, err + } + + for _, e := range entries { + if e.IsDir() { + subdir := filepath.Join(dirname, e.Name()) + c, l, err2 := scanCacheDir(subdir) + if err2 != nil { + return 0, 0, err2 + } + fileCount += c + totalFileLength += l + continue + } + + fileCount++ + totalFileLength += e.Size() + } + + return +} + func init() { statusCommand.Action(repositoryAction(runStatusCommand)) }