/
reader.go
408 lines (353 loc) · 11.1 KB
/
reader.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
403
404
405
406
407
408
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package backend
import (
"bytes"
"context"
"crypto"
"errors"
"fmt"
"hash"
"io"
"os"
"path/filepath"
"sort"
"syscall"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/jsonutil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/sys"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/strutil"
)
// ExtractFnameSetID can be passed to Open() to have set ID inferred from
// snapshot filename.
const ExtractFnameSetID = 0
// A Reader is a snapshot that's been opened for reading.
type Reader struct {
*os.File
client.Snapshot
}
// Open a Snapshot given its full filename.
//
// The returned reader will have its setID set to the value of the argument,
// or inferred from the snapshot filename if ExtractFnameSetID constant is
// passed.
//
// If the returned error is nil, the caller must close the reader (or
// its file) when done with it.
//
// If the returned error is non-nil, the returned Reader will be nil,
// *or* have a non-empty Broken; in the latter case its file will be
// closed.
func Open(fn string, setID uint64) (reader *Reader, e error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
defer func() {
if e != nil && f != nil {
f.Close()
}
}()
reader = &Reader{
File: f,
}
// first try to load the metadata itself
var sz osutil.Sizer
hasher := crypto.SHA3_384.New()
metaReader, metaSize, err := zipMember(f, metadataName)
if err != nil {
// no metadata file -> nothing to do :-(
return nil, err
}
if err := jsonutil.DecodeWithNumber(io.TeeReader(metaReader, io.MultiWriter(hasher, &sz)), &reader.Snapshot); err != nil {
return nil, err
}
if setID == ExtractFnameSetID {
// set id from the filename has the authority and overrides the one from
// meta file.
var ok bool
ok, setID = isSnapshotFilename(fn)
if !ok {
return nil, fmt.Errorf("not a snapshot filename: %q", fn)
}
}
reader.SetID = setID
// OK, from here on we have a Snapshot
if !reader.IsValid() {
reader.Broken = "invalid snapshot"
return reader, errors.New(reader.Broken)
}
if sz.Size() != metaSize {
reader.Broken = fmt.Sprintf("declared metadata size (%d) does not match actual (%d)", metaSize, sz.Size())
return reader, errors.New(reader.Broken)
}
actualMetaHash := fmt.Sprintf("%x", hasher.Sum(nil))
// grab the metadata hash
sz.Reset()
metaHashReader, metaHashSize, err := zipMember(f, metaHashName)
if err != nil {
reader.Broken = err.Error()
return reader, err
}
metaHashBuf, err := io.ReadAll(io.TeeReader(metaHashReader, &sz))
if err != nil {
reader.Broken = err.Error()
return reader, err
}
if sz.Size() != metaHashSize {
reader.Broken = fmt.Sprintf("declared hash size (%d) does not match actual (%d)", metaHashSize, sz.Size())
return reader, errors.New(reader.Broken)
}
if expectedMetaHash := string(bytes.TrimSpace(metaHashBuf)); actualMetaHash != expectedMetaHash {
reader.Broken = fmt.Sprintf("declared hash (%.7s…) does not match actual (%.7s…)", expectedMetaHash, actualMetaHash)
return reader, errors.New(reader.Broken)
}
return reader, nil
}
func (r *Reader) checkOne(ctx context.Context, entry string, hasher hash.Hash) error {
body, reportedSize, err := zipMember(r.File, entry)
if err != nil {
return err
}
defer body.Close()
expectedHash := r.SHA3_384[entry]
readSize, err := io.Copy(io.MultiWriter(osutil.ContextWriter(ctx), hasher), body)
if err != nil {
return err
}
if readSize != reportedSize {
return fmt.Errorf("snapshot entry %q size (%d) different from actual (%d)", entry, reportedSize, readSize)
}
if actualHash := fmt.Sprintf("%x", hasher.Sum(nil)); actualHash != expectedHash {
return fmt.Errorf("snapshot entry %q expected hash (%.7s…) does not match actual (%.7s…)", entry, expectedHash, actualHash)
}
return nil
}
// Check that the data contained in the snapshot matches its hashsums.
func (r *Reader) Check(ctx context.Context, usernames []string) error {
sort.Strings(usernames)
hasher := crypto.SHA3_384.New()
for entry := range r.SHA3_384 {
if len(usernames) > 0 && isUserArchive(entry) {
username := entryUsername(entry)
if !strutil.SortedListContains(usernames, username) {
logger.Debugf("In checking snapshot %q, skipping entry %q by user request.", r.Name(), username)
continue
}
}
if err := r.checkOne(ctx, entry, hasher); err != nil {
return err
}
hasher.Reset()
}
return nil
}
// Logf is the type implemented by logging functions.
type Logf func(format string, args ...interface{})
// Restore the data from the snapshot.
//
// If successful this will replace the existing data (for the given revision,
// or the one in the snapshot) with that contained in the snapshot. It keeps
// track of the old data in the task so it can be undone (or cleaned up).
func (r *Reader) Restore(ctx context.Context, current snap.Revision, usernames []string, logf Logf, opts *dirs.SnapDirOptions) (rs *RestoreState, e error) {
rs = &RestoreState{}
defer func() {
if e != nil {
logger.Noticef("Restore of snapshot %q failed (%v); undoing.", r.Name(), e)
rs.Revert()
rs = nil
}
}()
sort.Strings(usernames)
isRoot := sys.Geteuid() == 0
si := snap.MinimalPlaceInfo(r.Snap, r.Revision)
hasher := crypto.SHA3_384.New()
var sz osutil.Sizer
var curdir string
if !current.Unset() {
curdir = current.String()
}
for entry := range r.SHA3_384 {
if err := ctx.Err(); err != nil {
return rs, err
}
var dest string
isUser := isUserArchive(entry)
username := "root"
uid := sys.UserID(osutil.NoChown)
gid := sys.GroupID(osutil.NoChown)
if !isUser {
if entry != archiveName {
// hmmm
logf("Skipping restore of unknown entry %q.", entry)
continue
}
dest = si.DataDir()
} else {
username = entryUsername(entry)
if len(usernames) > 0 && !strutil.SortedListContains(usernames, username) {
logger.Debugf("In restoring snapshot %q, skipping entry %q by user request.", r.Name(), username)
continue
}
usr, err := userLookup(username)
if err != nil {
logf("Skipping restore of user %q: %v.", username, err)
continue
}
dest = si.UserDataDir(usr.HomeDir, opts)
fi, err := os.Stat(usr.HomeDir)
if err != nil {
if osutil.IsDirNotExist(err) {
logf("Skipping restore of %q as %q doesn't exist.", dest, usr.HomeDir)
} else {
logf("Skipping restore of %q: %v.", dest, err)
}
continue
}
if !fi.IsDir() {
logf("Skipping restore of %q as %q is not a directory.", dest, usr.HomeDir)
continue
}
if st, ok := fi.Sys().(*syscall.Stat_t); ok && isRoot {
// the mkdir below will use the uid/gid of usr.HomeDir
if st.Uid > 0 {
uid = sys.UserID(st.Uid)
}
if st.Gid > 0 {
gid = sys.GroupID(st.Gid)
}
}
}
parent, revdir := filepath.Split(dest)
exists, isDir, err := osutil.DirExists(parent)
if err != nil {
return rs, err
}
if !exists {
// NOTE that the chown won't happen (it'll be NoChown)
// for the system path, and we won't be creating the
// user's home (as we skip restore in that case).
// Also no chown happens for root/root.
if err := osutil.MkdirAllChown(parent, 0755, uid, gid); err != nil {
return rs, err
}
rs.Created = append(rs.Created, parent)
} else if !isDir {
return rs, fmt.Errorf("Cannot restore snapshot into %q: not a directory.", parent)
}
// TODO: have something more atomic in osutil
tempdir, err := os.MkdirTemp(parent, ".snapshot")
if err != nil {
return rs, err
}
if err := sys.ChownPath(tempdir, uid, gid); err != nil {
return rs, err
}
// one way or another we want tempdir gone
defer func() {
if err := os.RemoveAll(tempdir); err != nil {
logf("Cannot clean up temporary directory %q: %v.", tempdir, err)
}
}()
logger.Debugf("Restoring %q from %q into %q.", entry, r.Name(), tempdir)
body, expectedSize, err := zipMember(r.File, entry)
if err != nil {
return rs, err
}
expectedHash := r.SHA3_384[entry]
tr := io.TeeReader(body, io.MultiWriter(hasher, &sz))
// resist the temptation of using archive/tar unless it's proven
// that calling out to tar has issues -- there are a lot of
// special cases we'd need to consider otherwise
cmd := tarAsUser(username,
"--extract",
"--preserve-permissions", "--preserve-order", "--gunzip",
"--directory", tempdir)
cmd.Env = []string{}
cmd.Stdin = tr
matchCounter := &strutil.MatchCounter{N: 1}
cmd.Stderr = matchCounter
cmd.Stdout = os.Stderr
if isTesting {
matchCounter.N = -1
cmd.Stderr = io.MultiWriter(os.Stderr, matchCounter)
}
if err = osutil.RunWithContext(ctx, cmd); err != nil {
matches, count := matchCounter.Matches()
if count > 0 {
return rs, fmt.Errorf("cannot unpack archive: %s (and %d more)", matches[0], count-1)
}
return rs, fmt.Errorf("tar failed: %v", err)
}
if sz.Size() != expectedSize {
return rs, fmt.Errorf("snapshot %q entry %q expected size (%d) does not match actual (%d)",
r.Name(), entry, expectedSize, sz.Size())
}
if actualHash := fmt.Sprintf("%x", hasher.Sum(nil)); actualHash != expectedHash {
return rs, fmt.Errorf("snapshot %q entry %q expected hash (%.7s…) does not match actual (%.7s…)",
r.Name(), entry, expectedHash, actualHash)
}
if curdir != "" && curdir != revdir {
// rename it in tempdir
// this is where we assume the current revision can read the snapshot revision's data
if err := os.Rename(filepath.Join(tempdir, revdir), filepath.Join(tempdir, curdir)); err != nil {
return rs, err
}
revdir = curdir
}
for _, dir := range []string{"common", revdir} {
if err := moveFile(rs, dir, tempdir, parent); err != nil {
return rs, err
}
}
sz.Reset()
hasher.Reset()
}
return rs, nil
}
// moveFile moves file from the sourceDir to the targetDir. Directories moved
// and created are registered in the RestoreState.
func moveFile(rs *RestoreState, file, sourceDir, targetDir string) error {
src := filepath.Join(sourceDir, file)
if exists, _, err := osutil.DirExists(src); err != nil {
return err
} else if !exists {
return nil
}
dst := filepath.Join(targetDir, file)
exists, _, err := osutil.DirExists(dst)
if err != nil {
return err
}
if exists {
rsfn := restoreStateFilename(dst)
if err := os.Rename(dst, rsfn); err != nil {
return err
}
rs.Moved = append(rs.Moved, rsfn)
}
if err := os.Rename(src, dst); err != nil {
return err
}
rs.Created = append(rs.Created, dst)
return nil
}