2017-01-31 23:17:33 +00:00
|
|
|
package fstest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stevvooe/continuity/sysx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Applier applies single file changes
|
2017-02-03 01:30:11 +00:00
|
|
|
type Applier interface {
|
|
|
|
Apply(root string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type applyFn func(root string) error
|
|
|
|
|
|
|
|
func (a applyFn) Apply(root string) error {
|
|
|
|
return a(root)
|
|
|
|
}
|
2017-01-31 23:17:33 +00:00
|
|
|
|
2017-02-03 01:30:11 +00:00
|
|
|
// CreateFile returns a file applier which creates a file as the
|
2017-01-31 23:17:33 +00:00
|
|
|
// provided name with the given content and permission.
|
2017-02-03 01:30:11 +00:00
|
|
|
func CreateFile(name string, content []byte, perm os.FileMode) Applier {
|
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
fullPath := filepath.Join(root, name)
|
|
|
|
if err := ioutil.WriteFile(fullPath, content, perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chmod(fullPath, perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 06:50:43 +00:00
|
|
|
// Remove returns a file applier which removes the provided file name
|
|
|
|
func Remove(name string) Applier {
|
|
|
|
return applyFn(func(root string) error {
|
|
|
|
return os.Remove(filepath.Join(root, name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll returns a file applier which removes the provided file name
|
|
|
|
// as in os.RemoveAll
|
|
|
|
func RemoveAll(name string) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.RemoveAll(filepath.Join(root, name))
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 01:30:11 +00:00
|
|
|
// CreateDir returns a file applier to create the directory with
|
2017-01-31 23:17:33 +00:00
|
|
|
// the provided name and permission
|
2017-02-03 01:30:11 +00:00
|
|
|
func CreateDir(name string, perm os.FileMode) Applier {
|
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
fullPath := filepath.Join(root, name)
|
|
|
|
if err := os.MkdirAll(fullPath, perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename returns a file applier which renames a file
|
|
|
|
func Rename(old, new string) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.Rename(filepath.Join(root, old), filepath.Join(root, new))
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chown returns a file applier which changes the ownership of a file
|
|
|
|
func Chown(name string, uid, gid int) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.Chown(filepath.Join(root, name), uid, gid)
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chtime changes access and mod time of file
|
|
|
|
func Chtime(name string, t time.Time) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.Chtimes(filepath.Join(root, name), t, t)
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink returns a file applier which creates a symbolic link
|
|
|
|
func Symlink(oldname, newname string) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.Symlink(oldname, filepath.Join(root, newname))
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link returns a file applier which creates a hard link
|
|
|
|
func Link(oldname, newname string) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return os.Link(filepath.Join(root, oldname), filepath.Join(root, newname))
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetXAttr(name, key, value string) Applier {
|
2017-02-03 01:30:11 +00:00
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
return sysx.LSetxattr(name, key, []byte(value), 0)
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make platform specific, windows applier is always no-op
|
|
|
|
//func Mknod(name string, mode int32, dev int) Applier {
|
|
|
|
// return func(root string) error {
|
|
|
|
// return return syscall.Mknod(path, mode, dev)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2017-02-03 01:30:11 +00:00
|
|
|
// Apply returns a new applier from the given appliers
|
|
|
|
func Apply(appliers ...Applier) Applier {
|
|
|
|
return applyFn(func(root string) error {
|
2017-01-31 23:17:33 +00:00
|
|
|
for _, a := range appliers {
|
2017-02-03 01:30:11 +00:00
|
|
|
if err := a.Apply(root); err != nil {
|
2017-01-31 23:17:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-03 01:30:11 +00:00
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|