aboutsummaryrefslogtreecommitdiff
path: root/fs/memory.go
blob: 49982cddd91e7c4459128e5a6b0c935c1af42eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package fs

import (
	"bytes"
	"errors"
	"io"
	"os"
	"time"
)

type memfsBase struct {
	parent *MemfsDir
	name   string
	exec   bool
	mtime  time.Time
}

type memfsChild interface {
	File
	setName(string)
}

func (b memfsBase) Name() string       { return b.name }
func (b *memfsBase) setName(n string)  { b.name = n }
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
	HasBeenRead bool
}

func (MemfsFile) Type() FileType { return FFile }

func (f *MemfsFile) Open() (io.ReadCloser, error) {
	return f, nil
}

func (f MemfsFile) OpenWritable() (io.WriteCloser, error) {
	return f, nil
}

func (f *MemfsFile) Read(p []byte) (int, error) {
	f.HasBeenRead = true
	return f.content.Read(p)
}

func (f MemfsFile) Write(p []byte) (int, error) {
	return f.content.Write(p)
}

func (MemfsFile) Close() error {
	return nil
}

type MemfsDir struct {
	memfsBase
	children map[string]memfsChild
}

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) GetChild(name string) (File, error) {
	c, ok := d.children[name]
	if !ok {
		return nil, os.ErrNotExist
	}
	return c, 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]memfsChild),
	}
	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 (d *MemfsDir) RenameChild(oldname, newname string) error {
	c, ok := d.children[oldname]
	if !ok {
		return os.ErrNotExist
	}

	c.setName(newname)

	delete(d.children, oldname)
	d.children[newname] = c

	return nil
}

func NewMemoryFSRoot(name string) Dir {
	return &MemfsDir{
		memfsBase: memfsBase{
			parent: nil,
			name:   name,
			exec:   true,
			mtime:  time.Now(),
		},
		children: make(map[string]memfsChild),
	}
}

type MemfsSymlink struct {
	memfsBase
	target string
}

func (MemfsSymlink) Type() FileType { return FSymlink }

func (s MemfsSymlink) Readlink() (string, error) {
	return s.target, nil
}