github.com/afein/docker@v1.8.2/daemon/graphdriver/vfs/driver.go (about)

     1  // +build linux windows
     2  
     3  package vfs
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/docker/docker/daemon/graphdriver"
    11  	"github.com/docker/docker/pkg/chrootarchive"
    12  	"github.com/docker/docker/pkg/system"
    13  	"github.com/opencontainers/runc/libcontainer/label"
    14  )
    15  
    16  func init() {
    17  	graphdriver.Register("vfs", Init)
    18  }
    19  
    20  func Init(home string, options []string) (graphdriver.Driver, error) {
    21  	d := &Driver{
    22  		home: home,
    23  	}
    24  	return graphdriver.NaiveDiffDriver(d), nil
    25  }
    26  
    27  type Driver struct {
    28  	home string
    29  }
    30  
    31  func (d *Driver) String() string {
    32  	return "vfs"
    33  }
    34  
    35  func (d *Driver) Status() [][2]string {
    36  	return nil
    37  }
    38  
    39  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
    40  	return nil, nil
    41  }
    42  
    43  func (d *Driver) Cleanup() error {
    44  	return nil
    45  }
    46  
    47  func (d *Driver) Create(id, parent string) error {
    48  	dir := d.dir(id)
    49  	if err := system.MkdirAll(filepath.Dir(dir), 0700); err != nil {
    50  		return err
    51  	}
    52  	if err := os.Mkdir(dir, 0755); err != nil {
    53  		return err
    54  	}
    55  	opts := []string{"level:s0"}
    56  	if _, mountLabel, err := label.InitLabels(opts); err == nil {
    57  		label.SetFileLabel(dir, mountLabel)
    58  	}
    59  	if parent == "" {
    60  		return nil
    61  	}
    62  	parentDir, err := d.Get(parent, "")
    63  	if err != nil {
    64  		return fmt.Errorf("%s: %s", parent, err)
    65  	}
    66  	if err := chrootarchive.CopyWithTar(parentDir, dir); err != nil {
    67  		return err
    68  	}
    69  	return nil
    70  }
    71  
    72  func (d *Driver) dir(id string) string {
    73  	return filepath.Join(d.home, "dir", filepath.Base(id))
    74  }
    75  
    76  func (d *Driver) Remove(id string) error {
    77  	if _, err := os.Stat(d.dir(id)); err != nil {
    78  		return err
    79  	}
    80  	return os.RemoveAll(d.dir(id))
    81  }
    82  
    83  func (d *Driver) Get(id, mountLabel string) (string, error) {
    84  	dir := d.dir(id)
    85  	if st, err := os.Stat(dir); err != nil {
    86  		return "", err
    87  	} else if !st.IsDir() {
    88  		return "", fmt.Errorf("%s: not a directory", dir)
    89  	}
    90  	return dir, nil
    91  }
    92  
    93  func (d *Driver) Put(id string) error {
    94  	// The vfs driver has no runtime resources (e.g. mounts)
    95  	// to clean up, so we don't need anything here
    96  	return nil
    97  }
    98  
    99  func (d *Driver) Exists(id string) bool {
   100  	_, err := os.Stat(d.dir(id))
   101  	return err == nil
   102  }