Skip to content

Commit

Permalink
Implement fallback for getting the size of a container
Browse files Browse the repository at this point in the history
This moves Driver.Size() to Differ.DiffSize(), removing the empty
implementations in devmapper and dummy, and renaming the one in aufs.

Then we fall back to a container.Changes() implementation in the non-aufs
case.
  • Loading branch information
alexlarsson committed Nov 19, 2013
1 parent e42b574 commit 5d76681
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
14 changes: 14 additions & 0 deletions archive/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ func ChangesDirs(newDir, oldDir string) ([]Change, error) {
return newRoot.Changes(oldRoot), nil
}

func ChangesSize(newDir string, changes []Change) int64 {
var size int64
for _, change := range changes {
if change.Kind == ChangeModify || change.Kind == ChangeAdd {
file := filepath.Join(newDir, change.Path)
fileInfo, _ := os.Lstat(file)
if fileInfo != nil && !fileInfo.IsDir() {
size += fileInfo.Size()
}
}
}
return size
}

func ExportChanges(dir string, changes []Change) (Archive, error) {
files := make([]string, 0)
deletions := make([]string, 0)
Expand Down
27 changes: 19 additions & 8 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flag"
"fmt"
"github.com/dotcloud/docker/archive"
"github.com/dotcloud/docker/graphdriver"
"github.com/dotcloud/docker/term"
"github.com/dotcloud/docker/utils"
"github.com/kr/pty"
Expand Down Expand Up @@ -1552,18 +1553,28 @@ func (container *Container) GetSize() (int64, int64) {
driver = container.runtime.driver
)

sizeRw, err = driver.Size(container.ID)
if err != nil {
utils.Errorf("Warning: driver %s couldn't return diff size of container %s: %s", driver, container.ID, err)
// FIXME: GetSize should return an error. Not changing it now in case
// there is a side-effect.
sizeRw = -1
}

if err := container.EnsureMounted(); err != nil {
utils.Errorf("Warning: failed to compute size of container rootfs %s: %s", container.ID, err)
return sizeRw, sizeRootfs
}

if differ, ok := container.runtime.driver.(graphdriver.Differ); ok {
sizeRw, err = differ.DiffSize(container.ID)
if err != nil {
utils.Errorf("Warning: driver %s couldn't return diff size of container %s: %s", driver, container.ID, err)
// FIXME: GetSize should return an error. Not changing it now in case
// there is a side-effect.
sizeRw = -1
}
} else {
changes, _ := container.Changes()
if changes != nil {
sizeRw = archive.ChangesSize(container.RootfsPath(), changes)
} else {
sizeRw = -1
}
}

_, err = os.Stat(container.RootfsPath())
if err == nil {
filepath.Walk(container.RootfsPath(), func(path string, fileInfo os.FileInfo, err error) error {
Expand Down
2 changes: 1 addition & 1 deletion graphdriver/aufs/aufs.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (a *AufsDriver) ApplyDiff(id string, diff archive.Archive) error {
}

// Returns the size of the contents for the id
func (a *AufsDriver) Size(id string) (int64, error) {
func (a *AufsDriver) DiffSize(id string) (int64, error) {
return utils.TreeSize(path.Join(a.rootPath(), "diff", id))
}

Expand Down
4 changes: 0 additions & 4 deletions graphdriver/devmapper/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func (d *Driver) Get(id string) (string, error) {
return mp, nil
}

func (d *Driver) Size(id string) (int64, error) {
return -1, fmt.Errorf("Not implemented")
}

func (d *Driver) mount(id, mountPoint string) error {
// Create the target directories if they don't exist
if err := os.MkdirAll(mountPoint, 0755); err != nil && !os.IsExist(err) {
Expand Down
2 changes: 1 addition & 1 deletion graphdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Driver interface {
Remove(id string) error

Get(id string) (dir string, err error)
Size(id string) (bytes int64, err error)

Status() [][2]string

Expand All @@ -28,6 +27,7 @@ type Differ interface {
Diff(id string) (archive.Archive, error)
Changes(id string) ([]archive.Change, error)
ApplyDiff(id string, diff archive.Archive) error
DiffSize(id string) (bytes int64, err error)
}

var (
Expand Down
4 changes: 0 additions & 4 deletions graphdriver/dummy/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,3 @@ func (d *Driver) Get(id string) (string, error) {
}
return dir, nil
}

func (d *Driver) Size(id string) (int64, error) {
return -1, fmt.Errorf("Not implemented")
}

0 comments on commit 5d76681

Please sign in to comment.