/
tar_interpreter_new.go
91 lines (82 loc) · 3.36 KB
/
tar_interpreter_new.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
package postgres
import (
"archive/tar"
"io"
"github.com/apecloud/dataprotection-wal-g/utility"
"github.com/wal-g/tracelog"
)
// TODO : unit tests
func (tarInterpreter *FileTarInterpreter) unwrapRegularFileNew(fileReader io.Reader,
header *tar.Header,
targetPath string,
fsync bool) error {
if tarInterpreter.FilesToUnwrap != nil {
if _, ok := tarInterpreter.FilesToUnwrap[header.Name]; !ok {
// don't have to unwrap it this time
tracelog.DebugLogger.Printf("Don't have to unwrap '%s' this time\n", header.Name)
return nil
}
}
fileUnwrapper := getFileUnwrapper(tarInterpreter, header, targetPath)
localFile, isNewFile, err := utility.GetLocalFile(targetPath, header)
if err != nil {
return err
}
defer utility.LoggedClose(localFile, "")
defer utility.LoggedSync(localFile, "", fsync)
var unwrapResult *FileUnwrapResult
var unwrapError error
if isNewFile {
unwrapResult, unwrapError = fileUnwrapper.UnwrapNewFile(fileReader, header, localFile, fsync)
} else {
unwrapResult, unwrapError = fileUnwrapper.UnwrapExistingFile(fileReader, header, localFile, fsync)
}
if unwrapError != nil {
return unwrapError
}
tarInterpreter.AddFileUnwrapResult(unwrapResult, header.Name)
return nil
}
// get file unwrapper for file depending on backup type
func getFileUnwrapper(tarInterpreter *FileTarInterpreter, header *tar.Header, targetPath string) IBackupFileUnwrapper {
fileDescription, haveFileDescription := tarInterpreter.FilesMetadata.Files[header.Name]
isIncremented := haveFileDescription && fileDescription.IsIncremented
var isPageFile bool
if localFileInfo, _ := utility.GetLocalFileInfo(targetPath); localFileInfo != nil {
isPageFile = isPagedFile(localFileInfo, targetPath)
}
options := &BackupFileOptions{isIncremented: isIncremented, isPageFile: isPageFile}
// todo: clearer catchup backup detection logic
isCatchup := tarInterpreter.createNewIncrementalFiles
if isCatchup {
return NewFileUnwrapper(CatchupBackupFileUnwrapper, options)
}
return NewFileUnwrapper(DefaultBackupFileUnwrapper, options)
}
func (tarInterpreter *FileTarInterpreter) AddFileUnwrapResult(result *FileUnwrapResult, fileName string) {
switch result.FileUnwrapResultType {
case Skipped:
return
case Completed:
tarInterpreter.addToCompletedFiles(fileName)
case CreatedFromIncrement:
tarInterpreter.addToCreatedPageFiles(fileName, result.blockCount)
case WroteIncrementBlocks:
tarInterpreter.addToWrittenIncrementFiles(fileName, result.blockCount)
}
}
func (tarInterpreter *FileTarInterpreter) addToCompletedFiles(fileName string) {
tarInterpreter.UnwrapResult.completedFilesMutex.Lock()
tarInterpreter.UnwrapResult.completedFiles = append(tarInterpreter.UnwrapResult.completedFiles, fileName)
tarInterpreter.UnwrapResult.completedFilesMutex.Unlock()
}
func (tarInterpreter *FileTarInterpreter) addToCreatedPageFiles(fileName string, blocksToRestoreCount int64) {
tarInterpreter.UnwrapResult.createdPageFilesMutex.Lock()
tarInterpreter.UnwrapResult.createdPageFiles[fileName] = blocksToRestoreCount
tarInterpreter.UnwrapResult.createdPageFilesMutex.Unlock()
}
func (tarInterpreter *FileTarInterpreter) addToWrittenIncrementFiles(fileName string, writtenBlocksCount int64) {
tarInterpreter.UnwrapResult.writtenIncrementFilesMutex.Lock()
tarInterpreter.UnwrapResult.writtenIncrementFiles[fileName] = writtenBlocksCount
tarInterpreter.UnwrapResult.writtenIncrementFilesMutex.Unlock()
}