-
Notifications
You must be signed in to change notification settings - Fork 343
/
open.go
178 lines (143 loc) · 4.38 KB
/
open.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
package repo
import (
"context"
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/repologging"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/logging"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/repo/object"
)
var (
log = repologging.Logger("kopia/repo")
)
// Options provides configuration parameters for connection to a repository.
type Options struct {
TraceStorage func(f string, args ...interface{}) // Logs all storage access using provided Printf-style function
ObjectManagerOptions object.ManagerOptions
}
// Open opens a Repository specified in the configuration file.
func Open(ctx context.Context, configFile, password string, options *Options) (rep *Repository, err error) {
defer func() {
if err != nil {
log.Errorf("failed to open repository: %v", err)
}
}()
if options == nil {
options = &Options{}
}
configFile, err = filepath.Abs(configFile)
if err != nil {
return nil, err
}
lc, err := loadConfigFromFile(configFile)
if err != nil {
return nil, err
}
st, err := blob.NewStorage(ctx, lc.Storage)
if err != nil {
return nil, errors.Wrap(err, "cannot open storage")
}
if options.TraceStorage != nil {
st = logging.NewWrapper(st, logging.Prefix("[STORAGE] "), logging.Output(options.TraceStorage))
}
r, err := OpenWithConfig(ctx, st, lc, password, options, lc.Caching)
if err != nil {
st.Close(ctx) //nolint:errcheck
return nil, err
}
r.ConfigFile = configFile
return r, nil
}
// OpenWithConfig opens the repository with a given configuration, avoiding the need for a config file.
func OpenWithConfig(ctx context.Context, st blob.Storage, lc *LocalConfig, password string, options *Options, caching content.CachingOptions) (*Repository, error) {
// Read format blob, potentially from cache.
fb, err := readAndCacheFormatBlobBytes(ctx, st, caching.CacheDirectory)
if err != nil {
return nil, errors.Wrap(err, "unable to read format blob")
}
f, err := parseFormatBlob(fb)
if err != nil {
return nil, errors.Wrap(err, "can't parse format blob")
}
fb, err = addFormatBlobChecksumAndLength(fb)
if err != nil {
return nil, errors.Errorf("unable to add checksum")
}
masterKey, err := f.deriveMasterKeyFromPassword(password)
if err != nil {
return nil, err
}
repoConfig, err := f.decryptFormatBytes(masterKey)
if err != nil {
return nil, errors.Wrap(err, "unable to decrypt repository config")
}
caching.HMACSecret = deriveKeyFromMasterKey(masterKey, f.UniqueID, []byte("local-cache-integrity"), 16)
fo := &repoConfig.FormattingOptions
if fo.MaxPackSize == 0 {
fo.MaxPackSize = 20 << 20 // 20 MB
}
cm, err := content.NewManager(ctx, st, fo, caching, fb)
if err != nil {
return nil, errors.Wrap(err, "unable to open content manager")
}
om, err := object.NewObjectManager(ctx, cm, repoConfig.Format, options.ObjectManagerOptions)
if err != nil {
return nil, errors.Wrap(err, "unable to open object manager")
}
manifests, err := manifest.NewManager(ctx, cm)
if err != nil {
return nil, errors.Wrap(err, "unable to open manifests")
}
return &Repository{
Content: cm,
Objects: om,
Blobs: st,
Manifests: manifests,
UniqueID: f.UniqueID,
formatBlob: f,
masterKey: masterKey,
}, nil
}
// SetCachingConfig changes caching configuration for a given repository.
func (r *Repository) SetCachingConfig(opt content.CachingOptions) error {
lc, err := loadConfigFromFile(r.ConfigFile)
if err != nil {
return err
}
if err = setupCaching(r.ConfigFile, lc, opt, r.UniqueID); err != nil {
return errors.Wrap(err, "unable to set up caching")
}
d, err := json.MarshalIndent(&lc, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(r.ConfigFile, d, 0600); err != nil {
return nil
}
return nil
}
func readAndCacheFormatBlobBytes(ctx context.Context, st blob.Storage, cacheDirectory string) ([]byte, error) {
cachedFile := filepath.Join(cacheDirectory, "kopia.repository")
if cacheDirectory != "" {
b, err := ioutil.ReadFile(cachedFile)
if err == nil {
// read from cache.
return b, nil
}
}
b, err := st.GetBlob(ctx, FormatBlobID, 0, -1)
if err != nil {
return nil, err
}
if cacheDirectory != "" {
if err := ioutil.WriteFile(cachedFile, b, 0600); err != nil {
log.Warningf("warning: unable to write cache: %v", err)
}
}
return b, nil
}