2013-11-22 17:58:19 -05:00
|
|
|
package vfs
|
2013-11-07 20:52:56 -05:00
|
|
|
|
|
|
|
import (
|
2013-11-07 22:02:15 -05:00
|
|
|
"fmt"
|
2013-11-07 20:52:56 -05:00
|
|
|
"github.com/dotcloud/docker/graphdriver"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
func Init(home string) (graphdriver.Driver, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-11-19 07:09:36 -05:00
|
|
|
func copyDir(src, dst string) error {
|
2014-01-07 19:02:41 -05:00
|
|
|
if output, err := exec.Command("cp", "-aT", "--reflink=auto", src, dst).CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("Error VFS copying directory: %s (%s)", err, output)
|
2013-11-19 07:09:36 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-07 20:52:56 -05:00
|
|
|
func (d *Driver) Create(id string, parent string) error {
|
|
|
|
dir := d.dir(id)
|
|
|
|
if err := os.MkdirAll(path.Dir(dir), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(dir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parent == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parentDir, err := d.Get(parent)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Get(id string) (string, error) {
|
|
|
|
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
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
_, err := os.Stat(d.dir(id))
|
|
|
|
return err == nil
|
|
|
|
}
|