generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 100
/
file.go
269 lines (230 loc) · 6.18 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package mfs
import (
"context"
"errors"
"os"
"sync"
"time"
dag "github.com/ipfs/boxo/ipld/merkledag"
ft "github.com/ipfs/boxo/ipld/unixfs"
mod "github.com/ipfs/boxo/ipld/unixfs/mod"
chunker "github.com/ipfs/boxo/chunker"
ipld "github.com/ipfs/go-ipld-format"
)
// File represents a file in the MFS, its logic its mainly targeted
// to coordinating (potentially many) `FileDescriptor`s pointing to
// it.
type File struct {
inode
// Lock to coordinate the `FileDescriptor`s associated to this file.
desclock sync.RWMutex
// This isn't any node, it's the root node that represents the
// entire DAG of nodes that comprise the file.
// TODO: Rename, there should be an explicit term for these root nodes
// of a particular sub-DAG that abstract an upper layer's entity.
node ipld.Node
// Lock around the `node` that represents this file, necessary because
// there may be many `FileDescriptor`s operating on this `File`.
nodeLock sync.RWMutex
RawLeaves bool
}
// NewFile returns a NewFile object with the given parameters. If the
// Cid version is non-zero RawLeaves will be enabled.
func NewFile(name string, node ipld.Node, parent parent, dserv ipld.DAGService) (*File, error) {
fi := &File{
inode: inode{
name: name,
parent: parent,
dagService: dserv,
},
node: node,
}
if node.Cid().Prefix().Version > 0 {
fi.RawLeaves = true
}
return fi, nil
}
func (fi *File) Open(flags Flags) (_ FileDescriptor, _retErr error) {
if flags.Write {
fi.desclock.Lock()
defer func() {
if _retErr != nil {
fi.desclock.Unlock()
}
}()
} else if flags.Read {
fi.desclock.RLock()
defer func() {
if _retErr != nil {
fi.desclock.RUnlock()
}
}()
} else {
return nil, errors.New("file opened for neither reading nor writing")
}
fi.nodeLock.RLock()
node := fi.node
fi.nodeLock.RUnlock()
// TODO: Move this `switch` logic outside (maybe even
// to another package, this seems like a job of UnixFS),
// `NewDagModifier` uses the IPLD node, we're not
// extracting anything just doing a safety check.
switch node := node.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(node.Data())
if err != nil {
return nil, err
}
switch fsn.Type() {
default:
return nil, errors.New("unsupported fsnode type for 'file'")
case ft.TSymlink:
return nil, errors.New("symlinks not yet supported")
case ft.TFile, ft.TRaw:
// OK case
}
case *dag.RawNode:
// Ok as well.
}
dmod, err := mod.NewDagModifier(context.TODO(), node, fi.dagService, chunker.DefaultSplitter)
// TODO: Remove the use of the `chunker` package here, add a new `NewDagModifier` in
// `go-unixfs` with the `DefaultSplitter` already included.
if err != nil {
return nil, err
}
dmod.RawLeaves = fi.RawLeaves
return &fileDescriptor{
inode: fi,
flags: flags,
mod: dmod,
state: stateCreated,
}, nil
}
// Size returns the size of this file
// TODO: Should we be providing this API?
// TODO: There's already a `FileDescriptor.Size()` that
// through the `DagModifier`'s `fileSize` function is doing
// pretty much the same thing as here, we should at least call
// that function and wrap the `ErrNotUnixfs` with an MFS text.
func (fi *File) Size() (int64, error) {
fi.nodeLock.RLock()
defer fi.nodeLock.RUnlock()
switch nd := fi.node.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return 0, err
}
return int64(fsn.FileSize()), nil
case *dag.RawNode:
return int64(len(nd.RawData())), nil
default:
return 0, errors.New("unrecognized node type in mfs/file.Size()")
}
}
// GetNode returns the dag node associated with this file
// TODO: Use this method and do not access the `nodeLock` directly anywhere else.
func (fi *File) GetNode() (ipld.Node, error) {
fi.nodeLock.RLock()
defer fi.nodeLock.RUnlock()
return fi.node, nil
}
// TODO: Tight coupling with the `FileDescriptor`, at the
// very least this should be an independent function that
// takes a `File` argument and automates the open/flush/close
// operations.
// TODO: Why do we need to flush a file that isn't opened?
// (the `OpenWriteOnly` seems to implicitly be targeting a
// closed file, a file we forgot to flush? can we close
// a file without flushing?)
func (fi *File) Flush() error {
// open the file in fullsync mode
fd, err := fi.Open(Flags{Write: true, Sync: true})
if err != nil {
return err
}
defer fd.Close()
return fd.Flush()
}
func (fi *File) Sync() error {
// just being able to take the writelock means the descriptor is synced
// TODO: Why?
fi.desclock.Lock()
defer fi.desclock.Unlock() // Defer works around "empty critical section (SA2001)"
return nil
}
// Type returns the type FSNode this is
func (fi *File) Type() NodeType {
return TFile
}
func (fi *File) Mode() (os.FileMode, error) {
fi.nodeLock.RLock()
defer fi.nodeLock.RUnlock()
nd, err := fi.GetNode()
if err == nil {
fsn, err := ft.ExtractFSNode(nd)
if err == nil {
return fsn.Mode() & 0xFFF, nil
}
}
return 0, err
}
func (fi *File) SetMode(mode os.FileMode) error {
nd, err := fi.GetNode()
if err != nil {
return err
}
fsn, err := ft.ExtractFSNode(nd)
if err != nil {
return err
}
fsn.SetMode(mode)
data, err := fsn.GetBytes()
if err != nil {
return err
}
return fi.setNodeData(data)
}
// ModTime returns the files' last modification time
func (fi *File) ModTime() (time.Time, error) {
fi.nodeLock.RLock()
defer fi.nodeLock.RUnlock()
nd, err := fi.GetNode()
if err == nil {
fsn, err := ft.ExtractFSNode(nd)
if err == nil {
return fsn.ModTime(), nil
}
}
return time.Time{}, err
}
// SetModTime sets the files' last modification time
func (fi *File) SetModTime(ts time.Time) error {
nd, err := fi.GetNode()
if err != nil {
return err
}
fsn, err := ft.ExtractFSNode(nd)
if err != nil {
return err
}
fsn.SetModTime(ts)
data, err := fsn.GetBytes()
if err != nil {
return err
}
return fi.setNodeData(data)
}
func (fi *File) setNodeData(data []byte) error {
nd := dag.NodeWithData(data)
err := fi.inode.dagService.Add(context.TODO(), nd)
if err != nil {
return err
}
fi.nodeLock.Lock()
defer fi.nodeLock.Unlock()
fi.node = nd
parent := fi.inode.parent
name := fi.inode.name
return parent.updateChildEntry(child{name, fi.node})
}