2013-03-08 18:48:22 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2013-03-10 03:44:09 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-03-08 18:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChangeType int
|
|
|
|
|
|
|
|
const (
|
2013-03-10 03:44:09 +00:00
|
|
|
ChangeModify = iota
|
|
|
|
ChangeAdd
|
|
|
|
ChangeDelete
|
2013-03-08 18:48:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Change struct {
|
2013-03-10 03:44:09 +00:00
|
|
|
Path string
|
|
|
|
Kind ChangeType
|
2013-03-08 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (change *Change) String() string {
|
2013-03-10 03:44:09 +00:00
|
|
|
var kind string
|
|
|
|
switch change.Kind {
|
|
|
|
case ChangeModify:
|
|
|
|
kind = "C"
|
|
|
|
case ChangeAdd:
|
|
|
|
kind = "A"
|
|
|
|
case ChangeDelete:
|
|
|
|
kind = "D"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", kind, change.Path)
|
2013-03-08 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *Store) Changes(mp *Mountpoint) ([]Change, error) {
|
2013-03-10 03:44:09 +00:00
|
|
|
var changes []Change
|
|
|
|
image, err := store.Get(mp.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers, err := image.layers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase path
|
|
|
|
path, err = filepath.Rel(mp.Rw, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
path = filepath.Join("/", path)
|
|
|
|
|
|
|
|
// Skip root
|
|
|
|
if path == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip AUFS metadata
|
|
|
|
if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
change := Change{
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out what kind of modification happened
|
|
|
|
file := filepath.Base(path)
|
|
|
|
// If there is a whiteout, then the file was removed
|
|
|
|
if strings.HasPrefix(file, ".wh.") {
|
|
|
|
originalFile := strings.TrimLeft(file, ".wh.")
|
|
|
|
change.Path = filepath.Join(filepath.Dir(path), originalFile)
|
|
|
|
change.Kind = ChangeDelete
|
|
|
|
} else {
|
|
|
|
// Otherwise, the file was added
|
|
|
|
change.Kind = ChangeAdd
|
|
|
|
|
|
|
|
// ...Unless it already existed in a top layer, in which case, it's a modification
|
|
|
|
for _, layer := range layers {
|
|
|
|
stat, err := os.Stat(filepath.Join(layer, path))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
// The file existed in the top layer, so that's a modification
|
|
|
|
|
|
|
|
// However, if it's a directory, maybe it wasn't actually modified.
|
|
|
|
// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
|
|
|
|
if stat.IsDir() && f.IsDir() {
|
|
|
|
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
|
|
|
|
// Both directories are the same, don't record the change
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
change.Kind = ChangeModify
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record change
|
|
|
|
changes = append(changes, change)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return changes, nil
|
2013-03-08 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset removes all changes to the filesystem, reverting it to its initial state.
|
|
|
|
func (mp *Mountpoint) Reset() error {
|
2013-03-10 03:44:09 +00:00
|
|
|
if err := os.RemoveAll(mp.Rw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We removed the RW directory itself along with its content: let's re-create an empty one.
|
|
|
|
if err := mp.createFolders(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-03-08 18:48:22 +00:00
|
|
|
}
|