-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmemfile.go
89 lines (79 loc) · 2.43 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
package memfs
import (
"sync"
)
// MemFile represents a file backed by a Buffer which is secured from concurrent access.
type MemFile struct {
Buffer
mutex *sync.RWMutex
name string
}
// NewMemFile creates a Buffer which byte slice is safe from concurrent access,
// the file itself is not thread-safe.
//
// This means multiple files can work safely on the same byte slice,
// but multiple go routines working on the same file may corrupt the internal pointer structure.
func NewMemFile(name string, rwMutex *sync.RWMutex, buf *[]byte) *MemFile {
return &MemFile{
Buffer: NewBuffer(buf),
mutex: rwMutex,
name: name,
}
}
// Name of the file
func (b MemFile) Name() string {
return b.name
}
// Sync has no effect
func (b MemFile) Sync() error {
return nil
}
// Truncate changes the size of the file
func (b MemFile) Truncate(size int64) (err error) {
b.mutex.Lock()
err = b.Buffer.Truncate(size)
b.mutex.Unlock()
return
}
// Read reads len(p) byte from the underlying buffer starting at the current offset.
// It returns the number of bytes read and an error if any.
// Returns io.EOF error if pointer is at the end of the Buffer.
// See Buf.Read()
func (b *MemFile) Read(p []byte) (n int, err error) {
b.mutex.RLock()
n, err = b.Buffer.Read(p)
b.mutex.RUnlock()
return
}
// ReadAt reads len(b) bytes from the Buffer starting at byte offset off.
// It returns the number of bytes read and the error, if any.
// ReadAt always returns a non-nil error when n < len(b).
// At end of file, that error is io.EOF.
// See Buf.ReadAt()
func (b *MemFile) ReadAt(p []byte, off int64) (n int, err error) {
b.mutex.RLock()
n, err = b.Buffer.ReadAt(p, off)
b.mutex.RUnlock()
return
}
// Write writes len(p) byte to the Buffer.
// It returns the number of bytes written and an error if any.
// Write returns non-nil error when n!=len(p).
func (b *MemFile) Write(p []byte) (n int, err error) {
b.mutex.Lock()
n, err = b.Buffer.Write(p)
b.mutex.Unlock()
return
}
// Seek sets the offset for the next Read or Write on the buffer to offset,
// interpreted according to whence:
// 0 (os.SEEK_SET) means relative to the origin of the file
// 1 (os.SEEK_CUR) means relative to the current offset
// 2 (os.SEEK_END) means relative to the end of the file
// It returns the new offset and an error, if any.
func (b *MemFile) Seek(offset int64, whence int) (n int64, err error) {
b.mutex.RLock()
n, err = b.Buffer.Seek(offset, whence)
b.mutex.RUnlock()
return
}