2017-01-13 23:31:21 +00:00
|
|
|
package naive
|
2016-12-08 03:59:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/fs"
|
2016-12-08 03:59:34 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Naive struct {
|
|
|
|
root string
|
|
|
|
|
|
|
|
// TODO(stevvooe): Very lazy copying from the overlay driver. We'll have to
|
|
|
|
// put *all* of this on disk in an easy to access format.
|
|
|
|
active map[string]activeNaiveSnapshot
|
|
|
|
parents map[string]string // mirror of what is on disk
|
|
|
|
}
|
|
|
|
|
|
|
|
type activeNaiveSnapshot struct {
|
|
|
|
parent string
|
|
|
|
metadata string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNaive(root string) (*Naive, error) {
|
|
|
|
if err := os.MkdirAll(root, 0777); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): Recover active transactions.
|
|
|
|
|
|
|
|
return &Naive{
|
|
|
|
root: root,
|
|
|
|
active: make(map[string]activeNaiveSnapshot),
|
|
|
|
parents: make(map[string]string),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare works per the snapshot specification.
|
|
|
|
//
|
|
|
|
// For the naive driver, the data is checked out directly into dst and no
|
|
|
|
// mounts are returned.
|
2017-01-17 08:42:27 +00:00
|
|
|
func (n *Naive) Prepare(dst, parent string) ([]containerd.Mount, error) {
|
|
|
|
metadataRoot, err := ioutil.TempDir(n.root, "active-")
|
2016-12-08 03:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to created transaction dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): Write in driver metadata so it can be identified,
|
|
|
|
// probably part of common manager type.
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(metadataRoot, "target"), []byte(dst), 0777); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to write target to disk")
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent != "" {
|
2017-01-17 08:42:27 +00:00
|
|
|
if _, ok := n.parents[parent]; !ok {
|
2016-12-08 03:59:34 +00:00
|
|
|
return nil, errors.Wrap(err, "specified parent does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(metadataRoot, "parent"), []byte(parent), 0777); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error specifying parent")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we copy the parent filesystem, just a directory, into dst.
|
2017-02-03 20:08:53 +00:00
|
|
|
if err := fs.CopyDir(dst, filepath.Join(parent, "data")); err != nil {
|
2016-12-08 03:59:34 +00:00
|
|
|
return nil, errors.Wrap(err, "copying of parent failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
n.active[dst] = activeNaiveSnapshot{
|
2016-12-08 03:59:34 +00:00
|
|
|
parent: parent,
|
|
|
|
metadata: metadataRoot,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil // no mounts!!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit just moves the metadata directory to the diff location.
|
2017-01-17 08:42:27 +00:00
|
|
|
func (n *Naive) Commit(diff, dst string) error {
|
|
|
|
active, ok := n.active[dst]
|
2016-12-08 03:59:34 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%v is not an active transaction", dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the data into our metadata directory, we could probably save disk
|
|
|
|
// space if we just saved the diff, but let's get something working.
|
2017-02-03 20:08:53 +00:00
|
|
|
if err := fs.CopyDir(filepath.Join(active.metadata, "data"), dst); err != nil {
|
2016-12-08 03:59:34 +00:00
|
|
|
return errors.Wrap(err, "copying of parent failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(active.metadata, diff); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to rename metadata into diff")
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
n.parents[diff] = active.parent
|
|
|
|
delete(n.active, dst)
|
2016-12-08 03:59:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (n *Naive) Rollback(dst string) error {
|
|
|
|
active, ok := n.active[dst]
|
2016-12-08 03:59:34 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%q must be an active snapshot", dst)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
delete(n.active, dst)
|
2016-12-08 03:59:34 +00:00
|
|
|
return os.RemoveAll(active.metadata)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (n *Naive) Parent(diff string) string {
|
|
|
|
return n.parents[diff]
|
2016-12-08 03:59:34 +00:00
|
|
|
}
|