forked from TrueCloudLab/restic
Add tree.Stat()
This commit is contained in:
parent
fe231af7fc
commit
2a97e2b08a
2 changed files with 40 additions and 0 deletions
22
progress.go
22
progress.go
|
@ -1,6 +1,7 @@
|
|||
package restic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -163,3 +164,24 @@ func (s *Stat) Add(other Stat) {
|
|||
s.Files += other.Files
|
||||
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
18
tree.go
|
@ -151,6 +151,24 @@ func (t Tree) Find(name string) (*Node, error) {
|
|||
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) {
|
||||
stat, ok := fi.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
|
|
Loading…
Reference in a new issue