-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.go
109 lines (92 loc) · 2.89 KB
/
util.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
package backupstore
import (
"compress/gzip"
"context"
"io"
"path/filepath"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/longhorn/backupstore/util"
)
func getBlockPath(volumeName string) string {
return filepath.Join(getVolumePath(volumeName), BLOCKS_DIRECTORY) + "/"
}
func getBlockFilePath(volumeName, checksum string) string {
blockSubDirLayer1 := checksum[0:BLOCK_SEPARATE_LAYER1]
blockSubDirLayer2 := checksum[BLOCK_SEPARATE_LAYER1:BLOCK_SEPARATE_LAYER2]
path := filepath.Join(getBlockPath(volumeName), blockSubDirLayer1, blockSubDirLayer2)
fileName := checksum + BLK_SUFFIX
return filepath.Join(path, fileName)
}
// mergeErrorChannels will merge all error channels into a single error out channel.
// the error out channel will be closed once the ctx is done or all error channels are closed
// if there is an error on one of the incoming channels the error will be relayed.
func mergeErrorChannels(ctx context.Context, channels ...<-chan error) <-chan error {
var wg sync.WaitGroup
wg.Add(len(channels))
out := make(chan error, len(channels))
output := func(c <-chan error) {
defer wg.Done()
select {
case err, ok := <-c:
if ok {
out <- err
}
return
case <-ctx.Done():
return
}
}
for _, c := range channels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
// DecompressAndVerifyWithFallback decompresses the given data and verifies the data integrity.
// If the decompression fails, it will try to decompress with the fallback method.
func DecompressAndVerifyWithFallback(bsDriver BackupStoreDriver, blkFile, decompression, checksum string) (io.Reader, error) {
// Helper function to read block from backup store
readBlock := func() (io.ReadCloser, error) {
rc, err := bsDriver.Read(blkFile)
if err != nil {
return nil, errors.Wrapf(err, "failed to read block %v", blkFile)
}
return rc, nil
}
// First attempt to read and decompress/verify
rc, err := readBlock()
if err != nil {
return nil, err
}
defer rc.Close()
r, err := util.DecompressAndVerify(decompression, rc, checksum)
if err == nil {
return r, nil
}
// If there's an error, determine the alternative decompression method
alternativeDecompression := ""
if strings.Contains(err.Error(), gzip.ErrHeader.Error()) {
alternativeDecompression = "lz4"
} else if strings.Contains(err.Error(), "lz4: bad magic number") {
alternativeDecompression = "gzip"
}
// Second attempt with alternative decompression, if applicable
if alternativeDecompression != "" {
retriedRc, err := readBlock()
if err != nil {
return nil, err
}
defer retriedRc.Close()
r, err = util.DecompressAndVerify(alternativeDecompression, retriedRc, checksum)
if err != nil {
return nil, errors.Wrapf(err, "fallback decompression also failed for block %v", blkFile)
}
return r, nil
}
return nil, errors.Wrapf(err, "decompression verification failed for block %v", blkFile)
}