2013-11-22 17:58:19 -05:00
|
|
|
package vfs
|
2013-11-07 20:52:56 -05:00
|
|
|
|
|
|
|
import (
|
2014-06-21 05:50:52 -04:00
|
|
|
"bytes"
|
2013-11-07 22:02:15 -05:00
|
|
|
"fmt"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2013-11-07 20:52:56 -05:00
|
|
|
"os"
|
2013-11-19 07:09:36 -05:00
|
|
|
"os/exec"
|
2013-11-07 20:52:56 -05:00
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2013-11-22 17:58:19 -05:00
|
|
|
graphdriver.Register("vfs", Init)
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 04:34:20 -04:00
|
|
|
func Init(home string, options []string) (graphdriver.Driver, error) {
|
2013-11-07 20:52:56 -05:00
|
|
|
d := &Driver{
|
2013-11-07 22:02:15 -05:00
|
|
|
home: home,
|
2013-11-07 20:52:56 -05:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Driver struct {
|
|
|
|
home string
|
|
|
|
}
|
|
|
|
|
2013-11-07 22:02:15 -05:00
|
|
|
func (d *Driver) String() string {
|
2013-11-22 17:58:19 -05:00
|
|
|
return "vfs"
|
2013-11-07 22:02:15 -05:00
|
|
|
}
|
|
|
|
|
2013-11-15 05:04:02 -05:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-07 20:52:56 -05:00
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:50:52 -04:00
|
|
|
func isGNUcoreutils() bool {
|
|
|
|
if stdout, err := exec.Command("cp", "--version").Output(); err == nil {
|
|
|
|
return bytes.Contains(stdout, []byte("GNU coreutils"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-11-19 07:09:36 -05:00
|
|
|
func copyDir(src, dst string) error {
|
2014-06-21 05:50:52 -04:00
|
|
|
argv := make([]string, 0, 4)
|
|
|
|
|
|
|
|
if isGNUcoreutils() {
|
|
|
|
argv = append(argv, "-aT", "--reflink=auto", src, dst)
|
|
|
|
} else {
|
|
|
|
argv = append(argv, "-a", src+"/.", dst+"/.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if output, err := exec.Command("cp", argv...).CombinedOutput(); err != nil {
|
2014-01-07 19:02:41 -05:00
|
|
|
return fmt.Errorf("Error VFS copying directory: %s (%s)", err, output)
|
2013-11-19 07:09:36 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Create(id, parent string) error {
|
2013-11-07 20:52:56 -05:00
|
|
|
dir := d.dir(id)
|
|
|
|
if err := os.MkdirAll(path.Dir(dir), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-30 05:59:26 -04:00
|
|
|
if err := os.Mkdir(dir, 0755); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parent == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-17 19:47:27 -04:00
|
|
|
parentDir, err := d.Get(parent, "")
|
2013-11-07 20:52:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", parent, err)
|
|
|
|
}
|
2013-11-19 07:09:36 -05:00
|
|
|
if err := copyDir(parentDir, dir); err != nil {
|
2013-11-07 20:52:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) dir(id string) string {
|
|
|
|
return path.Join(d.home, "dir", path.Base(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Remove(id string) error {
|
|
|
|
if _, err := os.Stat(d.dir(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(d.dir(id))
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-07 20:52:56 -05:00
|
|
|
dir := d.dir(id)
|
|
|
|
if st, err := os.Stat(dir); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if !st.IsDir() {
|
|
|
|
return "", fmt.Errorf("%s: not a directory", dir)
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
2013-11-19 05:32:08 -05:00
|
|
|
|
2013-12-05 16:18:02 -05:00
|
|
|
func (d *Driver) Put(id string) {
|
|
|
|
// The vfs driver has no runtime resources (e.g. mounts)
|
|
|
|
// to clean up, so we don't need anything here
|
|
|
|
}
|
|
|
|
|
2013-11-19 05:32:08 -05:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
_, err := os.Stat(d.dir(id))
|
|
|
|
return err == nil
|
|
|
|
}
|