This repository has been archived by the owner on Mar 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfile.go
179 lines (152 loc) · 3.33 KB
/
memfile.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cafs
import (
"bytes"
"io"
"path/filepath"
"strings"
)
// PathSetter adds the capacity to modify a path property
type PathSetter interface {
SetPath(path string)
}
// Memfile is an in-memory file
type Memfile struct {
buf io.Reader
name string
path string
}
// Confirm that Memfile satisfies the File interface
var _ = (File)(&Memfile{})
// NewMemfileBytes creates a file from an io.Reader
func NewMemfileReader(name string, r io.Reader) *Memfile {
return &Memfile{
buf: r,
name: name,
}
}
// NewMemfileBytes creates a file from a byte slice
func NewMemfileBytes(name string, data []byte) *Memfile {
return &Memfile{
buf: bytes.NewBuffer(data),
name: name,
}
}
func (m Memfile) Read(p []byte) (int, error) {
return m.buf.Read(p)
}
func (m Memfile) Close() error {
if closer, ok := m.buf.(io.Closer); ok {
return closer.Close()
}
return nil
}
func (m Memfile) FileName() string {
return m.name
}
func (m Memfile) FullPath() string {
return m.path
}
func (m *Memfile) SetPath(path string) {
m.path = path
}
func (Memfile) IsDirectory() bool {
return false
}
func (Memfile) NextFile() (File, error) {
return nil, ErrNotDirectory
}
// Memdir is an in-memory directory
// Currently it only supports either Memfile & Memdir as links
type Memdir struct {
path string
fi int // file index for reading
links []File
}
// Confirm that Memdir satisfies the File interface
var _ = (File)(&Memdir{})
// NewMemdir creates a new Memdir, supplying zero or more links
func NewMemdir(path string, links ...File) *Memdir {
m := &Memdir{
path: path,
links: []File{},
}
m.AddChildren(links...)
return m
}
func (Memdir) Close() error {
return ErrNotReader
}
func (Memdir) Read([]byte) (int, error) {
return 0, ErrNotReader
}
func (m Memdir) FileName() string {
return filepath.Base(m.path)
}
func (m Memdir) FullPath() string {
return m.path
}
func (Memdir) IsDirectory() bool {
return true
}
func (d *Memdir) NextFile() (File, error) {
if d.fi >= len(d.links) {
d.fi = 0
return nil, io.EOF
}
defer func() { d.fi++ }()
return d.links[d.fi], nil
}
func (d *Memdir) SetPath(path string) {
d.path = path
for _, f := range d.links {
if fps, ok := f.(PathSetter); ok {
fps.SetPath(filepath.Join(path, f.FileName()))
}
}
}
// AddChildren allows any sort of file to be added, but only
// implementations that implement the PathSetter interface will have
// properly configured paths.
func (d *Memdir) AddChildren(fs ...File) {
for _, f := range fs {
if fps, ok := f.(PathSetter); ok {
fps.SetPath(filepath.Join(d.FullPath(), f.FileName()))
}
dir := d.MakeDirP(f)
dir.links = append(dir.links, f)
}
}
func (d *Memdir) ChildDir(dirname string) *Memdir {
if dirname == "" || dirname == "." || dirname == "/" {
return d
}
for _, f := range d.links {
if dir, ok := f.(*Memdir); ok {
if filepath.Base(dir.path) == dirname {
return dir
}
}
}
return nil
}
func (d *Memdir) MakeDirP(f File) *Memdir {
dirpath, _ := filepath.Split(f.FileName())
if dirpath == "" {
return d
}
dirs := strings.Split(dirpath[1:len(dirpath)-1], "/")
if len(dirs) == 1 {
return d
}
dir := d
for _, dirname := range dirs {
if ch := dir.ChildDir(dirname); ch != nil {
dir = ch
continue
}
ch := NewMemdir(filepath.Join(dir.FullPath(), dirname))
dir.links = append(dir.links, ch)
dir = ch
}
return dir
}