Skip to content

Commit

Permalink
cache dir size
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblum committed Feb 11, 2019
1 parent d17e4d2 commit a9eb528
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
6 changes: 6 additions & 0 deletions go/client/cmd_status.go
Expand Up @@ -105,6 +105,7 @@ type fstatus struct {
DeviceEKNames []string
LocalDbStats []string
LocalChatDbStats []string
CacheDirSizeInfo []keybase1.DirSizeInfo
}

func (c *CmdStatus) Run() error {
Expand Down Expand Up @@ -218,6 +219,7 @@ func (c *CmdStatus) load() (*fstatus, error) {
status.DeviceEKNames = extStatus.DeviceEkNames
status.LocalDbStats = extStatus.LocalDbStats
status.LocalChatDbStats = extStatus.LocalChatDbStats
status.CacheDirSizeInfo = extStatus.CacheDirSizeInfo

// set anything os-specific:
if err := c.osSpecific(&status); err != nil {
Expand Down Expand Up @@ -317,6 +319,10 @@ func (c *CmdStatus) outputTerminal(status *fstatus) error {
dui.Printf(" %s \n", strings.Join(status.DeviceEKNames, "\n "))
dui.Printf("LocalDbStats:\n%s \n", strings.Join(status.LocalDbStats, "\n"))
dui.Printf("LocalChatDbStats:\n%s \n", strings.Join(status.LocalChatDbStats, "\n"))
dui.Printf("CacheDirSizeInfo:\n")
for _, dirInfo := range status.CacheDirSizeInfo {
dui.Printf("%s: %s\n", dirInfo.Name, dirInfo.HumanSize)
}

c.outputClients(dui, status.Clients)
return nil
Expand Down
3 changes: 3 additions & 0 deletions go/libkb/status.go
Expand Up @@ -133,6 +133,9 @@ func GetExtendedStatus(m MetaContext) (res keybase1.ExtendedStatus, err error) {

res.LocalDbStats = strings.Split(g.LocalDb.Stats(), "\n")
res.LocalChatDbStats = strings.Split(g.LocalChatDb.Stats(), "\n")
if cacheSizeInfo, err := CacheSizeInfo(g); err == nil {
res.CacheDirSizeInfo = cacheSizeInfo
}

return res, nil
}
47 changes: 47 additions & 0 deletions go/libkb/util.go
Expand Up @@ -28,6 +28,7 @@ import (
"time"
"unicode"

humanize "github.com/dustin/go-humanize"
"github.com/keybase/client/go/kbcrypto"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/profiling"
Expand Down Expand Up @@ -981,3 +982,49 @@ func RuntimeGroup() keybase1.RuntimeGroup {
return keybase1.RuntimeGroup_UNKNOWN
}
}

// DirSize walks the file tree the size of the given directory
func DirSize(dirPath string) (size uint64, err error) {
err = filepath.Walk(dirPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += uint64(info.Size())
}
return nil
})
return size, err
}

func CacheSizeInfo(g *GlobalContext) (info []keybase1.DirSizeInfo, err error) {
cacheDir := g.GetCacheDir()
files, err := ioutil.ReadDir(cacheDir)
if err != nil {
return nil, err
}

var totalSize uint64
for _, file := range files {
if !file.IsDir() {
totalSize += uint64(file.Size())
continue
}
dirPath := filepath.Join(cacheDir, file.Name())
size, err := DirSize(dirPath)
if err != nil {
return nil, err
}
totalSize += size
info = append(info, keybase1.DirSizeInfo{
Name: dirPath,
HumanSize: humanize.Bytes(size),
})
}
info = append(info, keybase1.DirSizeInfo{
Name: cacheDir,
HumanSize: humanize.Bytes(totalSize),
})
return info, nil

}
24 changes: 24 additions & 0 deletions go/protocol/keybase1/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions protocol/avdl/keybase1/config.avdl
Expand Up @@ -47,6 +47,12 @@ protocol config {
string desc;
}

record DirSizeInfo {
string name;
string humanSize;
}


record ExtendedStatus {
boolean standalone;
boolean passphraseStreamCached;
Expand All @@ -72,6 +78,7 @@ protocol config {
in the config file. */
array<string> localDbStats;
array<string> localChatDbStats;
array<DirSizeInfo> cacheDirSizeInfo;
}

ExtendedStatus getExtendedStatus(int sessionID);
Expand Down
21 changes: 21 additions & 0 deletions protocol/json/keybase1/config.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion shared/constants/types/rpc-gen.js.flow

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9eb528

Please sign in to comment.