-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
76 lines (65 loc) · 1.66 KB
/
resource.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
package rs
import (
"bytes"
"fmt"
"io"
"github.com/gregoryv/nugo"
)
// ResInfo describes a resource and is returned by Stat
type ResInfo struct {
node *nugo.Node
}
// Name returns the name of the file
func (me *ResInfo) Name() string { return me.node.Name }
// IsDir returns nil if the resource is a directory
func (me *ResInfo) IsDir() error {
if !me.node.IsDir() {
return fmt.Errorf("IsDir: %s not a directory", me.node.Name)
}
return nil
}
func newResource(n *nugo.Node, op operation) *Resource {
return &Resource{
node: n,
op: op,
}
}
// Resource wraps access to the underlying node
type Resource struct {
node *nugo.Node
op operation
unlock func()
io.Reader
buf *bytes.Buffer // used for writing
}
// Read reads from the underlying source. Fails if not readable or
// resource is in write mode.
func (me *Resource) Read(b []byte) (int, error) {
if me.writeOnly() {
return 0, fmt.Errorf("Read: %s write only", me.node.Name)
}
if me.Reader == nil {
return 0, fmt.Errorf("Read: unreadable source")
}
return me.Reader.Read(b)
}
// Write writes to the resource. Is not flushed until closed.
func (me *Resource) Write(p []byte) (int, error) {
if me.readOnly() {
return 0, fmt.Errorf("Write: %s read only", me.node.Name)
}
return me.buf.Write(p)
}
// Close closes the resource. If resource is in write mode the written
// buffer is flushed.
func (me *Resource) Close() error {
if me.writeOnly() {
me.node.Content = me.buf.Bytes()
}
me.buf = nil
me.unlock()
return nil
}
// read + write should not be possible
func (me *Resource) readOnly() bool { return me.op&OpRead != 0 }
func (me *Resource) writeOnly() bool { return me.op&OpWrite != 0 }