aboutsummaryrefslogtreecommitdiff
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/fs.go41
-rw-r--r--fs/memory.go130
-rw-r--r--fs/os.go133
3 files changed, 304 insertions, 0 deletions
diff --git a/fs/fs.go b/fs/fs.go
new file mode 100644
index 0000000..cbfeb44
--- /dev/null
+++ b/fs/fs.go
@@ -0,0 +1,41 @@
+package fs
+
+import (
+ "io"
+ "time"
+)
+
+type FileType string
+
+const (
+ FFile FileType = "file"
+ FDir FileType = "dir"
+ FSymlink FileType = "symlink"
+)
+
+type File interface {
+ Type() FileType // Depending on type, the File must also implement RegularFile (FFile), Dir (FDir) or Symlink (FSymlink)
+ Name() string
+ Executable() bool // For now we will only record the executable bit instead of all permission bits
+ ModTime() time.Time
+ Delete() error
+}
+
+type RegularFile interface {
+ File
+ Open() (io.ReadWriteCloser, error)
+}
+
+type Dir interface {
+ File
+ Readdir() ([]File, error)
+
+ CreateChildFile(name string, exec bool) (RegularFile, error)
+ CreateChildDir(name string) (Dir, error)
+ CreateChildSymlink(name string, target string) (Symlink, error)
+}
+
+type Symlink interface {
+ File
+ Readlink() (string, error)
+}
diff --git a/fs/memory.go b/fs/memory.go
new file mode 100644
index 0000000..5f5d327
--- /dev/null
+++ b/fs/memory.go
@@ -0,0 +1,130 @@
+package fs
+
+import (
+ "bytes"
+ "errors"
+ "io"
+ "time"
+)
+
+type memfsBase struct {
+ parent *memfsDir
+ name string
+ exec bool
+ mtime time.Time
+}
+
+func (b memfsBase) Name() string { return b.name }
+func (b memfsBase) Executable() bool { return b.exec }
+func (b memfsBase) ModTime() time.Time { return b.mtime }
+
+func (b memfsBase) Delete() error {
+ if b.parent == nil {
+ return errors.New("Root entry can not be deleted")
+ }
+ b.parent.deleteChild(b.name)
+ return nil
+}
+
+type memfsFile struct {
+ memfsBase
+ content *bytes.Buffer
+}
+
+func (memfsFile) Type() FileType { return FFile }
+
+func (f memfsFile) Open() (io.ReadWriteCloser, error) {
+ return f, nil
+}
+
+func (f memfsFile) Read(p []byte) (int, error) {
+ return f.content.Read(p)
+}
+
+func (f memfsFile) Write(p []byte) (int, error) {
+ return f.content.Write(p)
+}
+
+func (memfsBase) Close() error {
+ return nil
+}
+
+type memfsDir struct {
+ memfsBase
+ children map[string]File
+}
+
+func (memfsDir) Type() FileType { return FDir }
+
+func (d memfsDir) Readdir() ([]File, error) {
+ l := make([]File, 0, len(d.children))
+
+ for _, f := range d.children {
+ l = append(l, f)
+ }
+
+ return l, nil
+}
+
+func (d memfsDir) createChildBase(name string, exec bool) memfsBase {
+ return memfsBase{
+ parent: &d,
+ name: name,
+ exec: exec,
+ mtime: time.Now(),
+ }
+}
+
+func (d memfsDir) CreateChildFile(name string, exec bool) (RegularFile, error) {
+ child := memfsFile{
+ memfsBase: d.createChildBase(name, exec),
+ content: new(bytes.Buffer),
+ }
+ d.children[name] = child
+ return child, nil
+}
+
+func (d memfsDir) CreateChildDir(name string) (Dir, error) {
+ child := memfsDir{
+ memfsBase: d.createChildBase(name, true),
+ children: make(map[string]File),
+ }
+ d.children[name] = child
+ return child, nil
+}
+
+func (d memfsDir) CreateChildSymlink(name string, target string) (Symlink, error) {
+ child := memfsSymlink{
+ memfsBase: d.createChildBase(name, false),
+ target: target,
+ }
+ d.children[name] = child
+ return child, nil
+}
+
+func (d *memfsDir) deleteChild(name string) {
+ delete(d.children, name)
+}
+
+func NewMemoryFSRoot(name string) Dir {
+ return memfsDir{
+ memfsBase: memfsBase{
+ parent: nil,
+ name: name,
+ exec: true,
+ mtime: time.Now(),
+ },
+ children: make(map[string]File),
+ }
+}
+
+type memfsSymlink struct {
+ memfsBase
+ target string
+}
+
+func (memfsSymlink) Type() FileType { return FSymlink }
+
+func (s memfsSymlink) Readlink() (string, error) {
+ return s.target, nil
+}
diff --git a/fs/os.go b/fs/os.go
new file mode 100644
index 0000000..f251c66
--- /dev/null
+++ b/fs/os.go
@@ -0,0 +1,133 @@
+package fs
+
+import (
+ "io"
+ "os"
+ "time"
+)
+
+func openOSFile(path string) (osFile, error) {
+ fi, err := os.Lstat(path)
+ if err != nil {
+ return osFile{}, err
+ }
+
+ return osFile{
+ fullpath: path,
+ fi: fi,
+ }, nil
+}
+
+type osFile struct {
+ fullpath string
+ fi os.FileInfo
+}
+
+func (f osFile) Type() FileType {
+ m := f.fi.Mode()
+ if m.IsDir() {
+ return FDir
+ }
+ if m.IsRegular() {
+ return FFile
+ }
+ if m&os.ModeSymlink != 0 {
+ return FSymlink
+ }
+ return "unknown"
+}
+
+func (f osFile) Name() string {
+ return f.fi.Name()
+}
+
+func (f osFile) Executable() bool {
+ return f.fi.Mode()&0100 != 0 // x bit set for user?
+}
+
+func (f osFile) ModTime() time.Time {
+ return f.fi.ModTime()
+}
+
+func (f osFile) Delete() error {
+ return os.RemoveAll(f.fullpath)
+}
+
+func (f osFile) Open() (io.ReadWriteCloser, error) {
+ fh, err := os.Open(f.fullpath)
+ if err != nil {
+ return nil, err
+ }
+ return fh, nil
+}
+
+func (f osFile) Readdir() (list []File, err error) {
+ fh, err := os.Open(f.fullpath)
+ if err != nil {
+ return
+ }
+ defer fh.Close()
+
+ infos, err := fh.Readdir(-1)
+ if err != nil {
+ return
+ }
+
+ for _, fi := range infos {
+ if fi.Name() == "." || fi.Name() == ".." {
+ continue
+ }
+
+ list = append(list, osFile{
+ fullpath: f.fullpath + string(os.PathSeparator) + fi.Name(),
+ fi: fi,
+ })
+ }
+
+ return
+}
+
+func perms(executable bool) os.FileMode {
+ if executable {
+ return 0755
+ } else {
+ return 0644
+ }
+}
+
+func (f osFile) CreateChildFile(name string, exec bool) (RegularFile, error) {
+ p := f.fullpath + string(os.PathSeparator) + name
+
+ fh, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE, perms(exec))
+ if err != nil {
+ return nil, err
+ }
+ fh.Close()
+
+ return openOSFile(p)
+}
+
+func (f osFile) CreateChildDir(name string) (Dir, error) {
+ p := f.fullpath + string(os.PathSeparator) + name
+
+ if err := os.Mkdir(p, perms(true)); err != nil {
+ return nil, err
+ }
+
+ return openOSFile(p)
+}
+
+func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error) {
+ p := f.fullpath + string(os.PathSeparator) + name
+
+ err := os.Symlink(target, p)
+ if err != nil {
+ return nil, err
+ }
+
+ return openOSFile(p)
+}
+
+func (f osFile) Readlink() (string, error) {
+ return os.Readlink(f.fullpath)
+}