Add tree.Stat()

This commit is contained in:
Alexander Neumann 2015-01-04 20:11:32 +01:00
parent fe231af7fc
commit 2a97e2b08a
2 changed files with 40 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package restic package restic
import ( import (
"fmt"
"sync" "sync"
"time" "time"
) )
@ -163,3 +164,24 @@ func (s *Stat) Add(other Stat) {
s.Files += other.Files s.Files += other.Files
s.Other += other.Other s.Other += other.Other
} }
func (s Stat) String() string {
b := float64(s.Bytes)
var str string
switch {
case s.Bytes > 1<<40:
str = fmt.Sprintf("%.3f TiB", b/(1<<40))
case s.Bytes > 1<<30:
str = fmt.Sprintf("%.3f GiB", b/(1<<30))
case s.Bytes > 1<<20:
str = fmt.Sprintf("%.3f MiB", b/(1<<20))
case s.Bytes > 1<<10:
str = fmt.Sprintf("%.3f KiB", b/(1<<10))
default:
str = fmt.Sprintf("%dB", s.Bytes)
}
return fmt.Sprintf("Stat(%d files, %d dirs, %d other, %v)",
s.Files, s.Dirs, s.Other, str)
}

18
tree.go
View file

@ -151,6 +151,24 @@ func (t Tree) Find(name string) (*Node, error) {
return node, err return node, err
} }
func (t Tree) Stat() Stat {
s := Stat{}
for _, n := range t {
switch n.Type {
case "file":
s.Files++
s.Bytes += n.Size
case "dir":
s.Dirs++
s.Add(n.Tree.Stat())
default:
s.Other++
}
}
return s
}
func (node *Node) fill_extra(path string, fi os.FileInfo) (err error) { func (node *Node) fill_extra(path string, fi os.FileInfo) (err error) {
stat, ok := fi.Sys().(*syscall.Stat_t) stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok { if !ok {