2017-08-14 14:28:08 -04:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2018-06-08 12:23:38 -04:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-08-14 14:28:08 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-06-08 12:23:38 -04:00
|
|
|
"strings"
|
2018-02-28 10:11:02 -05:00
|
|
|
"time"
|
2018-06-08 12:23:38 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-08-14 14:28:08 -04:00
|
|
|
)
|
|
|
|
|
2018-06-08 12:23:38 -04:00
|
|
|
const defaultFileMode = 0644
|
|
|
|
|
|
|
|
// PathOp is a function which accepts a Path and performs an operation on that
|
|
|
|
// path. When called with real filesystem objects (File or Dir) a PathOp modifies
|
|
|
|
// the filesystem at the path. When used with a Manifest object a PathOp updates
|
|
|
|
// the manifest to expect a value.
|
2017-08-14 14:28:08 -04:00
|
|
|
type PathOp func(path Path) error
|
|
|
|
|
2018-06-08 12:23:38 -04:00
|
|
|
type manifestResource interface {
|
|
|
|
SetMode(mode os.FileMode)
|
|
|
|
SetUID(uid uint32)
|
|
|
|
SetGID(gid uint32)
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifestFile interface {
|
|
|
|
manifestResource
|
|
|
|
SetContent(content io.ReadCloser)
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifestDirectory interface {
|
|
|
|
manifestResource
|
|
|
|
AddSymlink(path, target string) error
|
|
|
|
AddFile(path string, ops ...PathOp) error
|
|
|
|
AddDirectory(path string, ops ...PathOp) error
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
// WithContent writes content to a file at Path
|
|
|
|
func WithContent(content string) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestFile); ok {
|
|
|
|
m.SetContent(ioutil.NopCloser(strings.NewReader(content)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path.Path(), []byte(content), defaultFileMode)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBytes write bytes to a file at Path
|
|
|
|
func WithBytes(raw []byte) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestFile); ok {
|
|
|
|
m.SetContent(ioutil.NopCloser(bytes.NewReader(raw)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path.Path(), raw, defaultFileMode)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsUser changes ownership of the file system object at Path
|
|
|
|
func AsUser(uid, gid int) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestResource); ok {
|
|
|
|
m.SetUID(uint32(uid))
|
|
|
|
m.SetGID(uint32(gid))
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-14 14:28:08 -04:00
|
|
|
return os.Chown(path.Path(), uid, gid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFile creates a file in the directory at path with content
|
2017-09-08 13:45:19 -04:00
|
|
|
func WithFile(filename, content string, ops ...PathOp) PathOp {
|
2017-08-14 14:28:08 -04:00
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestDirectory); ok {
|
|
|
|
ops = append([]PathOp{WithContent(content), WithMode(defaultFileMode)}, ops...)
|
|
|
|
return m.AddFile(filename, ops...)
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:45:19 -04:00
|
|
|
fullpath := filepath.Join(path.Path(), filepath.FromSlash(filename))
|
|
|
|
if err := createFile(fullpath, content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return applyPathOps(&File{path: fullpath}, ops)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:45:19 -04:00
|
|
|
func createFile(fullpath string, content string) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
return ioutil.WriteFile(fullpath, []byte(content), defaultFileMode)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithFiles creates all the files in the directory at path with their content
|
|
|
|
func WithFiles(files map[string]string) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestDirectory); ok {
|
|
|
|
for filename, content := range files {
|
|
|
|
// TODO: remove duplication with WithFile
|
|
|
|
if err := m.AddFile(filename, WithContent(content), WithMode(defaultFileMode)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
for filename, content := range files {
|
2017-09-08 13:45:19 -04:00
|
|
|
fullpath := filepath.Join(path.Path(), filepath.FromSlash(filename))
|
|
|
|
if err := createFile(fullpath, content); err != nil {
|
2017-08-14 14:28:08 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromDir copies the directory tree from the source path into the new Dir
|
|
|
|
func FromDir(source string) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if _, ok := path.(manifestDirectory); ok {
|
|
|
|
return errors.New("use manifest.FromDir")
|
|
|
|
}
|
2017-08-14 14:28:08 -04:00
|
|
|
return copyDirectory(source, path.Path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:45:19 -04:00
|
|
|
// WithDir creates a subdirectory in the directory at path. Additional PathOp
|
|
|
|
// can be used to modify the subdirectory
|
|
|
|
func WithDir(name string, ops ...PathOp) PathOp {
|
2018-06-08 12:23:38 -04:00
|
|
|
const defaultMode = 0755
|
2017-09-08 13:45:19 -04:00
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestDirectory); ok {
|
|
|
|
ops = append([]PathOp{WithMode(defaultMode)}, ops...)
|
|
|
|
return m.AddDirectory(name, ops...)
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:45:19 -04:00
|
|
|
fullpath := filepath.Join(path.Path(), filepath.FromSlash(name))
|
2018-06-08 12:23:38 -04:00
|
|
|
err := os.MkdirAll(fullpath, defaultMode)
|
2017-09-08 13:45:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return applyPathOps(&Dir{path: fullpath}, ops)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyPathOps(path Path, ops []PathOp) error {
|
|
|
|
for _, op := range ops {
|
|
|
|
if err := op(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMode sets the file mode on the directory or file at path
|
|
|
|
func WithMode(mode os.FileMode) PathOp {
|
|
|
|
return func(path Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if m, ok := path.(manifestResource); ok {
|
|
|
|
m.SetMode(mode)
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-08 13:45:19 -04:00
|
|
|
return os.Chmod(path.Path(), mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
func copyDirectory(source, dest string) error {
|
|
|
|
entries, err := ioutil.ReadDir(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
sourcePath := filepath.Join(source, entry.Name())
|
|
|
|
destPath := filepath.Join(dest, entry.Name())
|
|
|
|
if entry.IsDir() {
|
|
|
|
if err := os.Mkdir(destPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copyDirectory(sourcePath, destPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2018-06-08 12:23:38 -04:00
|
|
|
// TODO: handle symlinks
|
2017-08-14 14:28:08 -04:00
|
|
|
if err := copyFile(sourcePath, destPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(source, dest string) error {
|
|
|
|
content, err := ioutil.ReadFile(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(dest, content, 0644)
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
// WithSymlink creates a symlink in the directory which links to target.
|
|
|
|
// Target must be a path relative to the directory.
|
|
|
|
//
|
|
|
|
// Note: the argument order is the inverse of os.Symlink to be consistent with
|
|
|
|
// the other functions in this package.
|
|
|
|
func WithSymlink(path, target string) PathOp {
|
|
|
|
return func(root Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if v, ok := root.(manifestDirectory); ok {
|
|
|
|
return v.AddSymlink(path, target)
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
return os.Symlink(filepath.Join(root.Path(), target), filepath.Join(root.Path(), path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHardlink creates a link in the directory which links to target.
|
|
|
|
// Target must be a path relative to the directory.
|
|
|
|
//
|
|
|
|
// Note: the argument order is the inverse of os.Link to be consistent with
|
|
|
|
// the other functions in this package.
|
|
|
|
func WithHardlink(path, target string) PathOp {
|
|
|
|
return func(root Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if _, ok := root.(manifestDirectory); ok {
|
|
|
|
return errors.New("WithHardlink yet implemented for manifests")
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
return os.Link(filepath.Join(root.Path(), target), filepath.Join(root.Path(), path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTimestamps sets the access and modification times of the file system object
|
|
|
|
// at path.
|
|
|
|
func WithTimestamps(atime, mtime time.Time) PathOp {
|
|
|
|
return func(root Path) error {
|
2018-06-08 12:23:38 -04:00
|
|
|
if _, ok := root.(manifestDirectory); ok {
|
|
|
|
return errors.New("WithTimestamp yet implemented for manifests")
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
return os.Chtimes(root.Path(), atime, mtime)
|
|
|
|
}
|
|
|
|
}
|