-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
164 lines (147 loc) · 3.09 KB
/
file.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
package ramfs
import (
"bytes"
"fmt"
"io"
"os"
"sync"
"time"
)
// Node represents a single File in the filesystem
type Node struct {
Mu sync.Mutex
Data bytes.Buffer
Name string
Mode os.FileMode
ModTime time.Time
IsDir bool
}
// FileInfo holds information about the file
type FileInfo struct {
name string
len int64
mode os.FileMode
modTime time.Time
isDir bool
}
// Name of the file
func (f *FileInfo) Name() string {
return f.name
}
// Size of the file
func (f *FileInfo) Size() int64 {
return f.len
}
// Mode of the file
func (f *FileInfo) Mode() os.FileMode {
return f.mode
}
// ModTime of the file
func (f *FileInfo) ModTime() time.Time {
return f.modTime
}
// IsDir whether the file is a directory
func (f *FileInfo) IsDir() bool {
return f.isDir
}
// Sys returns nil
func (f *FileInfo) Sys() interface{} {
return nil
}
// Stat returns the FileInfo of the file
func (n *Node) Stat() os.FileInfo {
n.Mu.Lock()
defer n.Mu.Unlock()
return &FileInfo{
name: n.Name,
len: int64(n.Data.Len()),
isDir: n.IsDir,
modTime: n.ModTime,
mode: n.Mode,
}
}
// File is used to read and write to. The API should mirror the one for the os.File.
type File struct {
node *Node
offset int
}
// Truncate truncates the file
func (f *File) Truncate(n int64) error {
f.node.Mu.Lock()
defer f.node.Mu.Unlock()
f.node.Data.Truncate(int(n))
return nil
}
// Write writes the content of the array into the file.
func (f *File) Write(p []byte) (int, error) {
f.node.Mu.Lock()
defer f.node.Mu.Unlock()
d := f.node.Data.Bytes()
wrote := 0
for ; f.offset < len(d); f.offset++ {
if wrote >= len(p) {
break
}
d[f.offset] = p[wrote]
wrote++
}
n, err := f.node.Data.Write(p[wrote:])
f.offset += n
return n + wrote, err
}
// Read reads the content from the file.
func (f *File) Read(p []byte) (int, error) {
f.node.Mu.Lock()
defer f.node.Mu.Unlock()
d := f.node.Data.Bytes()
if f.offset >= len(d) {
return 0, &os.PathError{
Op: "read",
Path: f.node.Name,
Err: io.EOF,
}
}
n := len(p)
if f.offset+n > len(d) {
n = len(d) - f.offset
}
for i := range p {
if i >= n {
break
}
p[i] = d[f.offset]
f.offset++
}
if len(p) != n {
return n, &os.PathError{
Op: "read",
Path: f.node.Name,
Err: io.EOF,
}
}
return n, nil
}
// Seek sets the offset for the next Read or Write on file to offset,
// interpreted according to whence: 0 means relative to the origin of the file,
// 1 means relative to the current offset, and 2 means relative to the end.
// It returns the new offset and an error, if any.
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
case 0:
f.offset = int(offset)
case 1:
f.offset += int(offset)
default:
return int64(f.offset), fmt.Errorf("seek %d not implemented", whence)
}
return int64(f.offset), nil
}
// Stat returns the FileInfo structure describing file.
// If there is an error, it will be of type *PathError.
func (f *File) Stat() (os.FileInfo, error) {
return f.node.Stat(), nil
}
// Close closes the file
func (f *File) Close() error {
return nil
}