/
filesystem.go
253 lines (212 loc) · 4.34 KB
/
filesystem.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
package base
import (
"context"
"errors"
"fmt"
"io/fs"
"sort"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
multihash "github.com/multiformats/go-multihash"
)
var ErrNotFound = errors.New("not found")
const (
// LatestVersion is the most recent semantic version of WNFS this
// implementation reads/writes
LatestVersion = SemVer("2.0.0dev")
DefaultMultihashType = multihash.SHA2_256
)
const (
MetadataLinkName = "metadata"
PublicLinkName = "public"
PrivateLinkName = "private"
SkeletonLinkName = "skeleton"
PrettyLinkName = "p"
PreviousLinkName = "previous"
MergeLinkName = "merge"
UserlandLinkName = "userland"
)
const (
ModeDefault = 644
)
type NodeType uint8
const (
NTFile NodeType = iota
NTLDFile
NTDir
NTSymlink // reserved for future use
NTUnixFSFile // reserved for future use
NTUnixFSDir // reserved for future use
)
func (nt NodeType) String() string {
switch nt {
case NTFile:
return "file"
case NTLDFile:
return "LDFile"
case NTDir:
return "dir"
case NTSymlink:
return "symlink"
case NTUnixFSFile:
return "unixFSFile"
case NTUnixFSDir:
return "unixFSDir"
default:
return "unknown"
}
}
type SemVer string
var ErrNoLink = fmt.Errorf("no link")
type Node interface {
fs.File
fs.FileInfo
Cid() cid.Cid
Type() NodeType
AsHistoryEntry() HistoryEntry
History(ctx context.Context, limit int) ([]HistoryEntry, error)
Metadata() (LDFile, error)
}
type File interface {
Node
}
type LDFile interface {
fs.File
fs.ReadDirFile
Data() (interface{}, error)
}
type Tree interface {
Node
fs.ReadDirFile
Get(path Path) (fs.File, error)
Add(path Path, f fs.File) (PutResult, error)
Copy(path Path, srcPath string, src fs.FS) (PutResult, error)
Rm(path Path) (PutResult, error)
Mkdir(path Path) (PutResult, error)
}
type PutResult interface {
CID() cid.Cid
ToLink(name string) Link
}
type Metadata interface {
Metadata() (LDFile, error)
}
type WritableMetaNode interface {
Node
SetMetadata(v interface{}) error
}
func FileMetadata(f fs.File) (LDFile, error) {
if mdf, ok := f.(Metadata); ok {
return mdf.Metadata()
}
return nil, nil
}
type HistoryEntry struct {
Cid cid.Cid `json:"cid"`
Previous *cid.Cid `json:"previous"`
Type NodeType `json:"type"`
Mtime int64 `json:"mtime"`
Size int64 `json:"size"`
Key string `json:"key,omitempty"`
PrivateName string `json:"privateName,omitempty"`
}
func Filename(file fs.File) (string, error) {
fi, err := file.Stat()
if err != nil {
return "", err
}
return fi.Name(), nil
}
type Links struct {
l map[string]Link
}
func NewLinks(links ...Link) Links {
lks := Links{
l: map[string]Link{},
}
for _, link := range links {
lks.Add(link)
}
return lks
}
func DecodeLinksBlock(blk blocks.Block) (Links, error) {
lks := Links{l: map[string]Link{}}
n, err := cbornode.DecodeBlock(blk)
if err != nil {
return lks, err
}
for _, l := range n.Links() {
lks.l[l.Name] = Link{
Name: l.Name,
Cid: l.Cid,
Size: int64(l.Size),
}
}
return lks, nil
}
func (ls Links) Len() int {
return len(ls.l)
}
func (ls Links) Add(link Link) {
ls.l[link.Name] = link
}
func (ls Links) Remove(name string) bool {
_, existed := ls.l[name]
delete(ls.l, name)
return existed
}
func (ls Links) Get(name string) *Link {
lk, ok := ls.l[name]
if !ok {
return nil
}
return &lk
}
func (ls Links) Slice() []Link {
links := make([]Link, 0, len(ls.l))
for _, link := range ls.l {
links = append(links, link)
}
return links
}
func (ls Links) SortedSlice() []Link {
names := make([]string, 0, len(ls.l))
for name := range ls.l {
names = append(names, name)
}
sort.Strings(names)
links := make([]Link, 0, len(ls.l))
for _, name := range names {
links = append(links, ls.l[name])
}
return links
}
func (ls Links) Map() map[string]Link {
return ls.l
}
func (ls Links) EncodeBlock() (blocks.Block, error) {
links := map[string]cid.Cid{}
for k, l := range ls.l {
links[k] = l.Cid
}
return cbornode.WrapObject(links, multihash.SHA2_256, -1)
}
type Link struct {
Name string
Size int64
Cid cid.Cid
IsFile bool
Mtime int64
}
func (l Link) IsEmpty() bool {
return l.Cid.String() == ""
}
func (l Link) IPLD() *format.Link {
return &format.Link{
Name: l.Name,
Cid: l.Cid,
Size: uint64(l.Size),
}
}