forked from sahib/brig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_cmds.go
402 lines (324 loc) · 8.59 KB
/
fs_cmds.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package client
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"time"
"github.com/sahib/brig/server/capnp"
h "github.com/sahib/brig/util/hashlib"
)
// StatInfo gives information about a file or directory
// similar to the normal stat(2) call on POSIX.
type StatInfo struct {
Path string
User string
Size uint64
Inode uint64
IsDir bool
Depth int
ModTime time.Time
IsPinned bool
IsExplicit bool
TreeHash h.Hash
ContentHash h.Hash
BackendHash h.Hash
}
func convertHash(hashBytes []byte, err error) (h.Hash, error) {
if err != nil {
return nil, err
}
return h.Cast(hashBytes)
}
func convertCapStatInfo(capInfo *capnp.StatInfo) (*StatInfo, error) {
info := &StatInfo{}
path, err := capInfo.Path()
if err != nil {
return nil, err
}
user, err := capInfo.User()
if err != nil {
return nil, err
}
treeHash, err := convertHash(capInfo.TreeHash())
if err != nil {
return nil, err
}
contentHash, err := convertHash(capInfo.ContentHash())
if err != nil {
return nil, err
}
backendHash, err := convertHash(capInfo.BackendHash())
if err != nil {
return nil, err
}
modTimeData, err := capInfo.ModTime()
if err != nil {
return nil, err
}
if err := info.ModTime.UnmarshalText([]byte(modTimeData)); err != nil {
return nil, err
}
info.Path = path
info.User = user
info.Size = capInfo.Size()
info.Inode = capInfo.Inode()
info.IsDir = capInfo.IsDir()
info.IsPinned = capInfo.IsPinned()
info.IsExplicit = capInfo.IsExplicit()
info.Depth = int(capInfo.Depth())
info.TreeHash = treeHash
info.ContentHash = contentHash
info.BackendHash = backendHash
return info, nil
}
// List will list all nodes beneath and including `root` up to `maxDepth`.
func (cl *Client) List(root string, maxDepth int) ([]StatInfo, error) {
call := cl.api.List(cl.ctx, func(p capnp.FS_list_Params) error {
p.SetMaxDepth(int32(maxDepth))
return p.SetRoot(root)
})
result, err := call.Struct()
if err != nil {
return nil, err
}
results := []StatInfo{}
statList, err := result.Entries()
for idx := 0; idx < statList.Len(); idx++ {
capInfo := statList.At(idx)
info, err := convertCapStatInfo(&capInfo)
if err != nil {
return nil, err
}
results = append(results, *info)
}
return results, err
}
// Stage will add a new node at `repoPath` with the contents of `localPath`.
func (cl *Client) Stage(localPath, repoPath string) error {
call := cl.api.Stage(cl.ctx, func(p capnp.FS_stage_Params) error {
if err := p.SetRepoPath(repoPath); err != nil {
return err
}
return p.SetLocalPath(localPath)
})
_, err := call.Struct()
return err
}
// StageFromReader will create a new node at `repoPath` from the contents of `r`.
func (cl *Client) StageFromReader(repoPath string, r io.Reader) error {
fd, err := ioutil.TempFile("", "brig-stage-temp")
if err != nil {
return err
}
defer os.Remove(fd.Name())
if _, err := io.Copy(fd, r); err != nil {
return err
}
return cl.Stage(fd.Name(), repoPath)
}
// Cat outputs the contents of the node at `path`.
// The node must be a file.
func (cl *Client) Cat(path string) (io.ReadCloser, error) {
call := cl.api.Cat(cl.ctx, func(p capnp.FS_cat_Params) error {
return p.SetPath(path)
})
result, err := call.Struct()
if err != nil {
return nil, err
}
port := result.Port()
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return nil, err
}
return conn, nil
}
// Tar outputs a tar archive with the contents of `path`.
// `path` can be either a file or directory.
func (cl *Client) Tar(path string) (io.ReadCloser, error) {
call := cl.api.Tar(cl.ctx, func(p capnp.FS_tar_Params) error {
return p.SetPath(path)
})
result, err := call.Struct()
if err != nil {
return nil, err
}
port := result.Port()
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return nil, err
}
return conn, nil
}
// Mkdir creates a new empty directory at `path`, possibly creating
// intermediate directories if `createParents` is set.
func (cl *Client) Mkdir(path string, createParents bool) error {
call := cl.api.Mkdir(cl.ctx, func(p capnp.FS_mkdir_Params) error {
p.SetCreateParents(createParents)
return p.SetPath(path)
})
_, err := call.Struct()
return err
}
// Remove removes the node at `path`.
// Directories are removed recursively.
func (cl *Client) Remove(path string) error {
call := cl.api.Remove(cl.ctx, func(p capnp.FS_remove_Params) error {
return p.SetPath(path)
})
_, err := call.Struct()
return err
}
// Move moves the node at `srcPath` to `dstPath`.
func (cl *Client) Move(srcPath, dstPath string) error {
call := cl.api.Move(cl.ctx, func(p capnp.FS_move_Params) error {
if err := p.SetSrcPath(srcPath); err != nil {
return err
}
return p.SetDstPath(dstPath)
})
_, err := call.Struct()
return err
}
// Copy copies the node at `srcPath` to `dstPath`.
func (cl *Client) Copy(srcPath, dstPath string) error {
call := cl.api.Copy(cl.ctx, func(p capnp.FS_copy_Params) error {
if err := p.SetSrcPath(srcPath); err != nil {
return err
}
return p.SetDstPath(dstPath)
})
_, err := call.Struct()
return err
}
// Pin sets an explicit pin on the node at `path`.
func (cl *Client) Pin(path string) error {
call := cl.api.Pin(cl.ctx, func(p capnp.FS_pin_Params) error {
return p.SetPath(path)
})
_, err := call.Struct()
return err
}
// Unpin removes an explicit pin at the node at `path`.
func (cl *Client) Unpin(path string) error {
call := cl.api.Unpin(cl.ctx, func(p capnp.FS_unpin_Params) error {
return p.SetPath(path)
})
_, err := call.Struct()
return err
}
// Stat gives detailed information about the node at `path`.
func (cl *Client) Stat(path string) (*StatInfo, error) {
call := cl.api.Stat(cl.ctx, func(p capnp.FS_stat_Params) error {
return p.SetPath(path)
})
result, err := call.Struct()
if err != nil {
return nil, err
}
capInfo, err := result.Info()
if err != nil {
return nil, err
}
return convertCapStatInfo(&capInfo)
}
// Touch creates a new empty file at `path`.
func (cl *Client) Touch(path string) error {
call := cl.api.Touch(cl.ctx, func(p capnp.FS_touch_Params) error {
return p.SetPath(path)
})
_, err := call.Struct()
return err
}
// Exists tells us if a file at `path` exists.
func (cl *Client) Exists(path string) (bool, error) {
call := cl.api.Exists(cl.ctx, func(p capnp.FS_exists_Params) error {
return p.SetPath(path)
})
result, err := call.Struct()
if err != nil {
return false, err
}
return result.Exists(), nil
}
// PathAtCommit describes the path to a file at the state of a certain commit.
type PathAtCommit struct {
Path string
Commit string
}
// ListExplicitPins lists all paths at all commits that have an explicit pin
// in the commit range `from` until `to`. The files have to start with `prefix`.
func (cl *Client) ListExplicitPins(prefix, from, to string) ([]PathAtCommit, error) {
call := cl.api.ListExplicitPins(cl.ctx, func(p capnp.FS_listExplicitPins_Params) error {
if err := p.SetPrefix(prefix); err != nil {
return err
}
if err := p.SetFrom(from); err != nil {
return err
}
return p.SetTo(to)
})
result, err := call.Struct()
if err != nil {
return nil, err
}
capPins, err := result.Pins()
if err != nil {
return nil, err
}
pins := []PathAtCommit{}
for idx := 0; idx < capPins.Len(); idx++ {
capPin := capPins.At(idx)
path, err := capPin.Path()
if err != nil {
return nil, err
}
commit, err := capPin.Commit()
if err != nil {
return nil, err
}
pins = append(pins, PathAtCommit{
Path: path,
Commit: commit,
})
}
return pins, nil
}
// ClearExplicitPins clears all explicit pins in the commit range `from` until `to`.
// The files have to start with `prefix`.
func (cl *Client) ClearExplicitPins(prefix, from, to string) (int, error) {
call := cl.api.ClearExplicitPins(cl.ctx, func(p capnp.FS_clearExplicitPins_Params) error {
if err := p.SetPrefix(prefix); err != nil {
return err
}
if err := p.SetFrom(from); err != nil {
return err
}
return p.SetTo(to)
})
result, err := call.Struct()
if err != nil {
return 0, err
}
return int(result.Count()), nil
}
// SetExplicitPins creates explicit pins for all files starting with `prefix` in the commit
// range `from` until `to`.
func (cl *Client) SetExplicitPins(prefix, from, to string) (int, error) {
call := cl.api.SetExplicitPins(cl.ctx, func(p capnp.FS_setExplicitPins_Params) error {
if err := p.SetPrefix(prefix); err != nil {
return err
}
if err := p.SetFrom(from); err != nil {
return err
}
return p.SetTo(to)
})
result, err := call.Struct()
if err != nil {
return 0, err
}
return int(result.Count()), nil
}