-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer.go
176 lines (156 loc) · 4.04 KB
/
computer.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
package arq
import (
"context"
"io"
"path"
"regexp"
"github.com/rclone/rclone/fs"
"howett.net/plist"
)
func unmarshalPlist(ctx context.Context, o fs.Object, out interface{}) error {
rc, err := o.Open(ctx)
if err != nil {
return err
}
defer rc.Close()
by, err := io.ReadAll(rc)
if err != nil {
return err
}
_, err = plist.Unmarshal(by, out)
return err
}
func parseComputerInfo(ctx context.Context, f fs.Fs, cPath string) (ComputerInfo, error) {
var info ComputerInfo
o, err := f.NewObject(ctx, path.Join(cPath, "computerinfo"))
if err != nil {
return info, err
}
err = unmarshalPlist(ctx, o, &info)
return info, err
}
func ListComputers(ctx context.Context, f fs.Fs, base string) ([]Computer, error) {
entries, err := f.List(ctx, base)
if err != nil {
return nil, err
}
computers := make([]Computer, 0, len(entries))
for _, entry := range entries {
if !uuidRegex.MatchString(entry.String()) {
continue
}
d, ok := entry.(fs.Directory)
if !ok {
continue
}
c := Computer{
Uuid: path.Base(d.String()),
opened: false,
base: base,
fs: f,
}
cInfo, err := parseComputerInfo(ctx, f, d.String())
if err != nil {
return nil, err
}
c.Info = cInfo
computers = append(computers, c)
}
return computers, nil
}
func NewComputer(fs fs.Fs, base string) *Computer {
return &Computer{
opened: false,
base: base,
fs: fs,
}
}
type Computer struct {
Uuid string
Info ComputerInfo
opened bool
base string
fs fs.Fs
enc *encryptionV3
}
func (c *Computer) Open(ctx context.Context, passphrase string) error {
if err := c.unlock(ctx, passphrase); err != nil {
return err
}
return nil
}
func (c *Computer) NewObject(ctx context.Context, p string) (fs.Object, error) {
return c.fs.NewObject(ctx, path.Join(c.base, p))
}
func (c *Computer) List(ctx context.Context, dir string) (fs.DirEntries, error) {
return c.fs.List(ctx, path.Join(c.base, dir))
}
func (c *Computer) unlock(ctx context.Context, passphrase string) error {
obj, err := c.fs.NewObject(ctx, path.Join(c.base, "encryptionv3.dat"))
if err != nil {
return err
}
rc, err := obj.Open(ctx)
if err != nil {
return err
}
c.enc, err = Unlock(ctx, rc, passphrase)
return err
}
type ComputerInfo struct {
UserName string `plist:"userName"`
ComputerName string `plist:"computerName"`
}
var uuidRegex = regexp.MustCompile("[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}")
func (c *Computer) ListFolders(ctx context.Context) ([]FolderInfo, error) {
entries, err := c.fs.List(ctx, path.Join(c.base, "buckets"))
if err != nil {
return nil, err
}
folders := make([]FolderInfo, 0, len(entries))
for _, entry := range entries {
if !uuidRegex.MatchString(entry.String()) {
continue
}
o, ok := entry.(fs.Object)
if !ok {
continue
}
rc, err := o.Open(ctx)
if err != nil {
return nil, err
}
defer rc.Close()
eor := NewEObjectReader(rc, c.enc)
by, err := io.ReadAll(eor)
if err != nil {
return nil, err
}
var folder FolderInfo
if _, err := plist.Unmarshal(by, &folder); err != nil {
return nil, err
}
folder.computer = c
folders = append(folders, folder)
}
return folders, nil
}
func (fi *FolderInfo) Folder() *Folder {
return &Folder{
uuid: fi.BucketUuid,
computer: fi.computer,
fInfo: fi,
}
}
type FolderInfo struct {
Endpoint string `plist:"Endpoint"`
BucketUuid string `plist:"BucketUUID"`
BucketName string `plist:"BucketName"`
ComputerUuid string `plist:"ComputerUUID"`
LocalPath string `plist:"LocalPath"`
LocalMountPoint string `plist:"LocalMountPoint"`
StorageType int `plist:"StorageType"`
SkipDuringBackup bool `plist:"SkipDuringBackup"`
ExcludeItemsWithTimeMachineExcludeMetadataFlag bool `plist:"ExcludeItemsWithTimeMachineExcludeMetadataFlag"`
computer *Computer
}