2013-11-23 02:58:19 +04:00
|
|
|
package vfs
|
2013-11-08 05:52:56 +04:00
|
|
|
|
|
|
|
import (
|
2013-11-08 07:02:15 +04:00
|
|
|
"fmt"
|
2013-11-08 05:52:56 +04:00
|
|
|
"github.com/dotcloud/docker/graphdriver"
|
|
|
|
"os"
|
2013-11-19 16:09:36 +04:00
|
|
|
"os/exec"
|
2013-11-08 05:52:56 +04:00
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2013-11-23 02:58:19 +04:00
|
|
|
graphdriver.Register("vfs", Init)
|
2013-11-08 05:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Init(home string) (graphdriver.Driver, error) {
|
|
|
|
d := &Driver{
|
2013-11-08 07:02:15 +04:00
|
|
|
home: home,
|
2013-11-08 05:52:56 +04:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Driver struct {
|
|
|
|
home string
|
|
|
|
}
|
|
|
|
|
2013-11-08 07:02:15 +04:00
|
|
|
func (d *Driver) String() string {
|
2013-11-23 02:58:19 +04:00
|
|
|
return "vfs"
|
2013-11-08 07:02:15 +04:00
|
|
|
}
|
|
|
|
|
2013-11-15 14:04:02 +04:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 05:52:56 +04:00
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 16:09:36 +04:00
|
|
|
func copyDir(src, dst string) error {
|
2014-01-08 04:02:41 +04: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 16:09:36 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 05:52:56 +04: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 16:09:36 +04:00
|
|
|
if err := copyDir(parentDir, dir); err != nil {
|
2013-11-08 05:52:56 +04: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 14:32:08 +04:00
|
|
|
|
2013-12-06 01:18:02 +04: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 14:32:08 +04:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
_, err := os.Stat(d.dir(id))
|
|
|
|
return err == nil
|
|
|
|
}
|