
To allow the querying of usage for snapshots, we define a new method on the snapshotter to query the resources in use by a single snapshot. Conversely, it can be said that if the snapshot was deleted, the reported amount of usage would be recovered. There are few problems with this model in the implementation of btrfs that need to be worked out. In btrfs, it is hard to resolve the amount of data usage with the use of quotas but these may report valuables that are incompatible with the model. Signed-off-by: Stephen J Day <stephen.day@docker.com>
43 lines
771 B
Go
43 lines
771 B
Go
// +build !windows
|
|
|
|
package fs
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
)
|
|
|
|
func diskUsage(roots ...string) (Usage, error) {
|
|
type inode struct {
|
|
// TODO(stevvooe): Can probably reduce memory usage by not tracking
|
|
// device, but we can leave this right for now.
|
|
dev, ino uint64
|
|
}
|
|
|
|
var (
|
|
size int64
|
|
inodes = map[inode]struct{}{} // expensive!
|
|
)
|
|
|
|
for _, root := range roots {
|
|
if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
stat := fi.Sys().(*syscall.Stat_t)
|
|
inodes[inode{dev: stat.Dev, ino: stat.Ino}] = struct{}{}
|
|
size += fi.Size()
|
|
return nil
|
|
}); err != nil {
|
|
return Usage{}, err
|
|
}
|
|
}
|
|
|
|
return Usage{
|
|
Inodes: int64(len(inodes)),
|
|
Size: size,
|
|
}, nil
|
|
}
|