From 5d76681c3d0cbb744205a397420603ce029ce754 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 19 Nov 2013 10:36:54 +0100 Subject: [PATCH] Implement fallback for getting the size of a container 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. --- archive/changes.go | 14 ++++++++++++++ container.go | 27 +++++++++++++++++++-------- graphdriver/aufs/aufs.go | 2 +- graphdriver/devmapper/driver.go | 4 ---- graphdriver/driver.go | 2 +- graphdriver/dummy/driver.go | 4 ---- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/archive/changes.go b/archive/changes.go index 9305fe2311..83bdcae7cf 100644 --- a/archive/changes.go +++ b/archive/changes.go @@ -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) diff --git a/container.go b/container.go index ed4e5f4777..02296da3a5 100644 --- a/container.go +++ b/container.go @@ -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" @@ -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 { diff --git a/graphdriver/aufs/aufs.go b/graphdriver/aufs/aufs.go index c0bc069ce8..b80cc358c4 100644 --- a/graphdriver/aufs/aufs.go +++ b/graphdriver/aufs/aufs.go @@ -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)) } diff --git a/graphdriver/devmapper/driver.go b/graphdriver/devmapper/driver.go index 315b7b091e..109681b527 100644 --- a/graphdriver/devmapper/driver.go +++ b/graphdriver/devmapper/driver.go @@ -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) { diff --git a/graphdriver/driver.go b/graphdriver/driver.go index e55adf4c35..914c6d7623 100644 --- a/graphdriver/driver.go +++ b/graphdriver/driver.go @@ -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 @@ -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 ( diff --git a/graphdriver/dummy/driver.go b/graphdriver/dummy/driver.go index 4ca8fac58a..a12a469878 100644 --- a/graphdriver/dummy/driver.go +++ b/graphdriver/dummy/driver.go @@ -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") -}