Ver código fonte

Merge pull request #45 from alexlarsson/dm-plugin-diff-size

Implement fallback for getting the size of a container
Michael Crosby 11 anos atrás
pai
commit
4c2624a277

+ 14 - 0
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)

+ 19 - 8
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 {

+ 1 - 1
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))
 }
 

+ 0 - 4
graphdriver/devmapper/driver.go

@@ -76,10 +76,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) {

+ 1 - 1
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 (

+ 0 - 4
graphdriver/dummy/driver.go

@@ -84,7 +84,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")
-}