Skip to content

Commit

Permalink
Merge pull request moby#15014 from brahmaroutu/lint_daemon_graphdrive…
Browse files Browse the repository at this point in the history
…r_overlay

daemon/graphdriver/overlay/ fix lint errors/warnings
  • Loading branch information
calavera committed Aug 7, 2015
2 parents 0262d40 + de39442 commit 5110095
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
8 changes: 4 additions & 4 deletions daemon/graphdriver/overlay/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/docker/docker/pkg/system"
)

type CopyFlags int
type copyFlags int

const (
CopyHardlink CopyFlags = 1 << iota
copyHardlink copyFlags = 1 << iota
)

func copyRegular(srcPath, dstPath string, mode os.FileMode) error {
Expand Down Expand Up @@ -49,7 +49,7 @@ func copyXattr(srcPath, dstPath, attr string) error {
return nil
}

func copyDir(srcDir, dstDir string, flags CopyFlags) error {
func copyDir(srcDir, dstDir string, flags copyFlags) error {
err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -75,7 +75,7 @@ func copyDir(srcDir, dstDir string, flags CopyFlags) error {

switch f.Mode() & os.ModeType {
case 0: // Regular file
if flags&CopyHardlink != 0 {
if flags&copyHardlink != 0 {
isHardlink = true
if err := os.Link(srcPath, dstPath); err != nil {
return err
Expand Down
39 changes: 32 additions & 7 deletions daemon/graphdriver/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import (
// implementation of ApplyDiff()

var (
// ErrApplyDiffFallback is returned to indicate that a normal ApplyDiff is applied as a fallback from Naive diff writer.
ErrApplyDiffFallback = fmt.Errorf("Fall back to normal ApplyDiff")
)

// ApplyDiffProtoDriver wraps the ProtoDriver by extending the inteface with ApplyDiff method.
type ApplyDiffProtoDriver interface {
graphdriver.ProtoDriver
// ApplyDiff writes the diff to the archive for the given id and parent id.
// It returns the size in bytes written if successful, an error ErrApplyDiffFallback is returned otherwise.
ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error)
}

Expand All @@ -36,13 +40,15 @@ type naiveDiffDriverWithApply struct {
applyDiff ApplyDiffProtoDriver
}

// NaiveDiffDriverWithApply returns a NaiveDiff driver with custom ApplyDiff.
func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver) graphdriver.Driver {
return &naiveDiffDriverWithApply{
Driver: graphdriver.NaiveDiffDriver(driver),
applyDiff: driver,
}
}

// ApplyDiff creates a diff layer with either the NaiveDiffDriver or with a fallback.
func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff archive.Reader) (int64, error) {
b, err := d.applyDiff.ApplyDiff(id, parent, diff)
if err == ErrApplyDiffFallback {
Expand Down Expand Up @@ -79,11 +85,15 @@ func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff archive.Rea
// of that. This means all child images share file (but not directory)
// data with the parent.

// ActiveMount contains information about the count, path and whether is mounted or not.
// This information is part of the Driver, that contains list of active mounts taht are part of this overlay.
type ActiveMount struct {
count int
path string
mounted bool
}

// Driver contains information about the home directory and the list of active mounts that are created using this driver.
type Driver struct {
home string
sync.Mutex // Protects concurrent modification to active
Expand All @@ -96,6 +106,9 @@ func init() {
graphdriver.Register("overlay", Init)
}

// Init returns the NaiveDiffDriver, a native diff driver for overlay filesystem.
// If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error.
// If a overlay filesystem is not supported over a existing filesystem then error graphdriver.ErrIncompatibleFS is returned.
func Init(home string, options []string) (graphdriver.Driver, error) {

if err := supportsOverlay(); err != nil {
Expand Down Expand Up @@ -161,12 +174,15 @@ func (d *Driver) String() string {
return "overlay"
}

// Status returns current driver information in a two dimensional string array.
// Output contains "Backing Filesystem" used in this implementation.
func (d *Driver) Status() [][2]string {
return [][2]string{
{"Backing Filesystem", backingFs},
}
}

// GetMetadata returns meta data about the overlay driver such as root, LowerDir, UpperDir, WorkDir and MergeDir used to store data.
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
dir := d.dir(id)
if _, err := os.Stat(dir); err != nil {
Expand All @@ -182,23 +198,27 @@ func (d *Driver) GetMetadata(id string) (map[string]string, error) {
return metadata, nil
}

lowerId, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
if err != nil {
return nil, err
}

metadata["LowerDir"] = path.Join(d.dir(string(lowerId)), "root")
metadata["LowerDir"] = path.Join(d.dir(string(lowerID)), "root")
metadata["UpperDir"] = path.Join(dir, "upper")
metadata["WorkDir"] = path.Join(dir, "work")
metadata["MergedDir"] = path.Join(dir, "merged")

return metadata, nil
}

// Cleanup simply returns nil and do not change the existing filesystem.
// This is required to satisfy the graphdriver.Driver interface.
func (d *Driver) Cleanup() error {
return nil
}

// Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
// The parent filesystem is used to configure these directories for the overlay.
func (d *Driver) Create(id string, parent string) (retErr error) {
dir := d.dir(id)
if err := os.MkdirAll(path.Dir(dir), 0700); err != nil {
Expand Down Expand Up @@ -251,12 +271,12 @@ func (d *Driver) Create(id string, parent string) (retErr error) {

// Otherwise, copy the upper and the lower-id from the parent

lowerId, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
if err != nil {
return err
}

if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerId, 0666); err != nil {
if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0666); err != nil {
return err
}

Expand Down Expand Up @@ -284,6 +304,7 @@ func (d *Driver) dir(id string) string {
return path.Join(d.home, id)
}

// Remove cleans the directories that are created for this id.
func (d *Driver) Remove(id string) error {
dir := d.dir(id)
if _, err := os.Stat(dir); err != nil {
Expand All @@ -292,6 +313,7 @@ func (d *Driver) Remove(id string) error {
return os.RemoveAll(dir)
}

// Get creates and mounts the required file system for the given id and returns the mount path.
func (d *Driver) Get(id string, mountLabel string) (string, error) {
// Protect the d.active from concurrent access
d.Lock()
Expand All @@ -318,11 +340,11 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
return mount.path, nil
}

lowerId, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
if err != nil {
return "", err
}
lowerDir := path.Join(d.dir(string(lowerId)), "root")
lowerDir := path.Join(d.dir(string(lowerID)), "root")
upperDir := path.Join(dir, "upper")
workDir := path.Join(dir, "work")
mergedDir := path.Join(dir, "merged")
Expand All @@ -338,6 +360,7 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
return mount.path, nil
}

// Put unmounts the mount path created for the give id.
func (d *Driver) Put(id string) error {
// Protect the d.active from concurrent access
d.Lock()
Expand Down Expand Up @@ -373,6 +396,7 @@ func (d *Driver) Put(id string) error {
return nil
}

// ApplyDiff applies the new layer on top of the root, if parent does not exist with will return a ErrApplyDiffFallback error.
func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size int64, err error) {
dir := d.dir(id)

Expand Down Expand Up @@ -407,7 +431,7 @@ func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size
}
}()

if err = copyDir(parentRootDir, tmpRootDir, CopyHardlink); err != nil {
if err = copyDir(parentRootDir, tmpRootDir, copyHardlink); err != nil {
return 0, err
}

Expand All @@ -423,6 +447,7 @@ func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size
return
}

// Exists checks to see if the id is already mounted.
func (d *Driver) Exists(id string) bool {
_, err := os.Stat(d.dir(id))
return err == nil
Expand Down
1 change: 1 addition & 0 deletions hack/make/validate-lint
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ packages=(
daemon/execdriver/windows
daemon/graphdriver/aufs
daemon/graphdriver/devmapper
daemon/graphdriver/overlay
daemon/graphdriver/vfs
daemon/graphdriver/zfs
daemon/logger
Expand Down

0 comments on commit 5110095

Please sign in to comment.