2017-08-14 14:28:08 -04:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2018-06-08 12:23:38 -04:00
|
|
|
"bytes"
|
2022-09-22 09:38:19 -04:00
|
|
|
"fmt"
|
2018-06-08 12:23:38 -04:00
|
|
|
"io"
|
2017-08-14 14:28:08 -04:00
|
|
|
"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
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
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 {
|
2022-11-05 18:25:29 -04:00
|
|
|
m.SetContent(io.NopCloser(strings.NewReader(content)))
|
2018-06-08 12:23:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-05 18:25:29 -04:00
|
|
|
return os.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 {
|
2022-11-05 18:25:29 -04:00
|
|
|
m.SetContent(io.NopCloser(bytes.NewReader(raw)))
|
2018-06-08 12:23:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-05 18:25:29 -04:00
|
|
|
return os.WriteFile(path.Path(), raw, defaultFileMode)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
// WithReaderContent copies the reader contents to the file at Path
|
|
|
|
func WithReaderContent(r io.Reader) PathOp {
|
|
|
|
return func(path Path) error {
|
|
|
|
if m, ok := path.(manifestFile); ok {
|
2022-11-05 18:25:29 -04:00
|
|
|
m.SetContent(io.NopCloser(r))
|
2020-02-22 12:12:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path.Path(), os.O_WRONLY, defaultFileMode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, r)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-05 18:25:29 -04:00
|
|
|
return os.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 {
|
2022-09-22 09:38:19 -04:00
|
|
|
return fmt.Errorf("use manifest.FromDir")
|
2018-06-08 12:23:38 -04:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:47:37 -04:00
|
|
|
// Apply the PathOps to the File
|
|
|
|
func Apply(t assert.TestingT, path Path, ops ...PathOp) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert.NilError(t, applyPathOps(path, ops))
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:45:19 -04:00
|
|
|
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 {
|
2022-11-05 18:25:29 -04:00
|
|
|
entries, err := os.ReadDir(source)
|
2017-08-14 14:28:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
sourcePath := filepath.Join(source, entry.Name())
|
|
|
|
destPath := filepath.Join(dest, entry.Name())
|
2022-11-05 18:25:29 -04:00
|
|
|
err = copyEntry(entry, destPath, sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:25:29 -04:00
|
|
|
func copyEntry(entry os.DirEntry, destPath string, sourcePath string) error {
|
|
|
|
if entry.IsDir() {
|
|
|
|
if err := os.Mkdir(destPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return copyDirectory(sourcePath, destPath)
|
|
|
|
}
|
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
return copySymLink(sourcePath, destPath)
|
|
|
|
}
|
|
|
|
return copyFile(sourcePath, destPath)
|
|
|
|
}
|
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
func copySymLink(source, dest string) error {
|
|
|
|
link, err := os.Readlink(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Symlink(link, dest)
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
func copyFile(source, dest string) error {
|
2022-11-05 18:25:29 -04:00
|
|
|
content, err := os.ReadFile(source)
|
2017-08-14 14:28:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-05 18:25:29 -04:00
|
|
|
return os.WriteFile(dest, content, 0644)
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
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 {
|
2022-09-22 09:38:19 -04:00
|
|
|
return fmt.Errorf("WithHardlink not implemented for manifests")
|
2018-06-08 12:23:38 -04:00
|
|
|
}
|
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 {
|
2022-09-22 09:38:19 -04:00
|
|
|
return fmt.Errorf("WithTimestamp not implemented for manifests")
|
2018-06-08 12:23:38 -04:00
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
return os.Chtimes(root.Path(), atime, mtime)
|
|
|
|
}
|
|
|
|
}
|