-
Notifications
You must be signed in to change notification settings - Fork 14
/
apply.go
175 lines (154 loc) · 3.59 KB
/
apply.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
package tar_patch
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"github.com/containers/tar-diff/pkg/common"
"github.com/klauspost/compress/zstd"
"io"
"os"
"path"
)
type DataSource interface {
io.ReadSeeker
io.Closer
SetCurrentFile(file string) error
}
type FilesystemDataSource struct {
basePath string
currentFile *os.File
}
// Cleans up the path lexically
// Any ".." that extends outside the first elements (or the root itself) is invalid and returns ""
func cleanPath(pathName string) string {
// We make the path always absolute, that way path.Clean() ensure it never goes outside the top ("root") dir
// even if its a relative path
clean := path.Clean("/" + pathName)
// We clean the initial slash, making all result relative (or "" which is error)
return clean[1:]
}
func NewFilesystemDataSource(basePath string) *FilesystemDataSource {
return &FilesystemDataSource{
basePath: basePath,
currentFile: nil,
}
}
func (f *FilesystemDataSource) Close() error {
if f.currentFile != nil {
err := f.currentFile.Close()
f.currentFile = nil
if err != nil {
return err
}
}
return nil
}
func (f *FilesystemDataSource) Read(data []byte) (n int, err error) {
if f.currentFile == nil {
return 0, fmt.Errorf("No current file set")
}
return f.currentFile.Read(data)
}
func (f *FilesystemDataSource) SetCurrentFile(file string) error {
if f.currentFile != nil {
err := f.currentFile.Close()
f.currentFile = nil
if err != nil {
return nil
}
}
currentFile, err := os.Open(f.basePath + "/" + file)
if err != nil {
return err
}
f.currentFile = currentFile
return nil
}
func (f *FilesystemDataSource) Seek(offset int64, whence int) (int64, error) {
if f.currentFile == nil {
return 0, fmt.Errorf("No current file set")
}
return f.currentFile.Seek(offset, whence)
}
func Apply(delta io.Reader, dataSource DataSource, dst io.Writer) error {
buf := make([]byte, len(common.DeltaHeader))
_, err := io.ReadFull(delta, buf)
if err != nil {
return err
}
if !bytes.Equal(buf, common.DeltaHeader[:]) {
return fmt.Errorf("Invalid delta format")
}
decoder, err := zstd.NewReader(delta)
if err != nil {
return err
}
defer decoder.Close()
r := bufio.NewReader(decoder)
for {
op, err := r.ReadByte()
if err != nil {
if err == io.EOF {
break
}
return err
}
size, err := binary.ReadUvarint(r)
if err != nil {
return err
}
switch op {
case common.DeltaOpData:
_, err = io.CopyN(dst, r, int64(size))
if err != nil {
return err
}
case common.DeltaOpOpen:
nameBytes := make([]byte, size)
_, err = io.ReadFull(r, nameBytes)
if err != nil {
return err
}
name := string(nameBytes)
cleanName := cleanPath(name)
if len(cleanName) == 0 {
return fmt.Errorf("Invalid source name '%v' in tar-diff", name)
}
err := dataSource.SetCurrentFile(cleanName)
if err != nil {
return err
}
case common.DeltaOpCopy:
_, err = io.CopyN(dst, dataSource, int64(size))
if err != nil {
return err
}
case common.DeltaOpAddData:
addBytes := make([]byte, size)
_, err = io.ReadFull(r, addBytes)
if err != nil {
return err
}
addBytes2 := make([]byte, size)
_, err = io.ReadFull(dataSource, addBytes2)
if err != nil {
return err
}
for i := uint64(0); i < size; i++ {
addBytes[i] = addBytes[i] + addBytes2[i]
}
if _, err := dst.Write(addBytes); err != nil {
return err
}
case common.DeltaOpSeek:
_, err = dataSource.Seek(int64(size), 0)
if err != nil {
return err
}
default:
return fmt.Errorf("Unexpected delta op %d", op)
}
}
return nil
}