-
Notifications
You must be signed in to change notification settings - Fork 33
/
file.go
260 lines (208 loc) · 5.83 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
package nodes
import (
"fmt"
"path"
"time"
capnp_model "github.com/sahib/brig/catfs/nodes/capnp"
h "github.com/sahib/brig/util/hashlib"
capnp "zombiezen.com/go/capnproto2"
)
// File represents a single file in the repository.
// It stores all metadata about it and links to the actual data.
type File struct {
Base
size uint64
parent string
key []byte
}
// NewEmptyFile returns a newly created file under `parent`, named `name`.
func NewEmptyFile(parent *Directory, name string, user string, inode uint64) *File {
return &File{
Base: Base{
name: name,
user: user,
inode: inode,
modTime: time.Now().Truncate(time.Microsecond),
nodeType: NodeTypeFile,
},
parent: parent.Path(),
}
}
// ToCapnp converts a file to a capnp message.
func (f *File) ToCapnp() (*capnp.Message, error) {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return nil, err
}
capNd, err := capnp_model.NewRootNode(seg)
if err != nil {
return nil, err
}
return msg, f.ToCapnpNode(seg, capNd)
}
// ToCapnpNode converts this node to a serializable capnp proto node.
func (f *File) ToCapnpNode(seg *capnp.Segment, capNd capnp_model.Node) error {
if err := f.setBaseAttrsToNode(capNd); err != nil {
return err
}
capFile, err := f.setFileAttrs(seg)
if err != nil {
return err
}
return capNd.SetFile(*capFile)
}
func (f *File) setFileAttrs(seg *capnp.Segment) (*capnp_model.File, error) {
capFile, err := capnp_model.NewFile(seg)
if err != nil {
return nil, err
}
if err := capFile.SetParent(f.parent); err != nil {
return nil, err
}
if err := capFile.SetKey(f.key); err != nil {
return nil, err
}
capFile.SetSize(f.size)
return &capFile, nil
}
// FromCapnp sets all state of `msg` into the file.
func (f *File) FromCapnp(msg *capnp.Message) error {
capNd, err := capnp_model.ReadRootNode(msg)
if err != nil {
return err
}
return f.FromCapnpNode(capNd)
}
// FromCapnpNode converts a serialized node to a normal node.
func (f *File) FromCapnpNode(capNd capnp_model.Node) error {
if err := f.parseBaseAttrsFromNode(capNd); err != nil {
return err
}
capFile, err := capNd.File()
if err != nil {
return err
}
return f.readFileAttrs(capFile)
}
func (f *File) readFileAttrs(capFile capnp_model.File) error {
var err error
f.parent, err = capFile.Parent()
if err != nil {
return err
}
f.nodeType = NodeTypeFile
f.size = capFile.Size()
f.key, err = capFile.Key()
return err
}
////////////////// METADATA INTERFACE //////////////////
// Size returns the number of bytes in the file's content.
func (f *File) Size() uint64 { return f.size }
////////////////// ATTRIBUTE SETTERS //////////////////
// SetModTime udates the mod time of the file (i.e. "touch"es it)
func (f *File) SetModTime(t time.Time) {
f.modTime = t.Truncate(time.Microsecond)
}
// SetName set the name of the file.
func (f *File) SetName(n string) { f.name = n }
// SetKey updates the key to a new value, taking ownership of the value.
func (f *File) SetKey(k []byte) { f.key = k }
// SetSize will update the size of the file and update it's mod time.
func (f *File) SetSize(s uint64) {
f.size = s
f.SetModTime(time.Now())
}
// Copy copies the contents of the file, except `inode`.
func (f *File) Copy(inode uint64) ModNode {
if f == nil {
return nil
}
var copyKey []byte
if f.key != nil {
copyKey = make([]byte, len(f.key))
copy(copyKey, f.key)
}
return &File{
Base: f.Base.copyBase(inode),
size: f.size,
parent: f.parent,
key: copyKey,
}
}
func (f *File) rehash(lkr Linker, newPath string) {
oldHash := f.tree.Clone()
var contentHash h.Hash
if f.Base.content != nil {
contentHash = f.Base.content.Clone()
} else {
contentHash = h.EmptyInternalHash.Clone()
}
f.tree = h.Sum([]byte(fmt.Sprintf("%s|%s", newPath, contentHash)))
lkr.MemIndexSwap(f, oldHash, true)
}
// NotifyMove should be called when the node moved parents.
func (f *File) NotifyMove(lkr Linker, newParent *Directory, newPath string) error {
dirname, basename := path.Split(newPath)
f.SetName(basename)
f.parent = dirname
f.rehash(lkr, newPath)
if newParent != nil {
if err := newParent.Add(lkr, f); err != nil {
return err
}
newParent.rebuildOrderCache()
}
return nil
}
// SetContent will update the hash of the file (and also the mod time)
func (f *File) SetContent(lkr Linker, content h.Hash) {
f.Base.content = content
f.rehash(lkr, f.Path())
f.SetModTime(time.Now())
}
// SetBackend will update the hash of the file (and also the mod time)
func (f *File) SetBackend(lkr Linker, backend h.Hash) {
f.Base.backend = backend
f.SetModTime(time.Now())
}
func (f *File) String() string {
return fmt.Sprintf("<file %s:%s:%d>", f.Path(), f.TreeHash(), f.Inode())
}
// Path will return the absolute path of the file.
func (f *File) Path() string {
return prefixSlash(path.Join(f.parent, f.name))
}
////////////////// HIERARCHY INTERFACE //////////////////
// NChildren returns the number of children this file node has.
func (f *File) NChildren() int {
return 0
}
// Child will return always nil, since files don't have children.
func (f *File) Child(_ Linker, name string) (Node, error) {
// A file never has a child. Sad but true.
return nil, nil
}
// Parent returns the parent directory of File.
// If `f` is already the root, it will return itself (and never nil).
func (f *File) Parent(lkr Linker) (Node, error) {
return lkr.LookupNode(f.parent)
}
// SetParent will set the parent of the file to `parent`.
func (f *File) SetParent(_ Linker, parent Node) error {
if parent == nil {
return nil
}
f.parent = parent.Path()
return nil
}
// Key returns the current key of the file.
func (f *File) Key() []byte {
return f.key
}
// SetUser sets the user that last modified the file.
func (f *File) SetUser(user string) {
f.Base.user = user
}
// Interface check for debugging:
var _ ModNode = &File{}
var _ Streamable = &File{}