-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
384 lines (304 loc) · 7.56 KB
/
client.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
/*
* This file is part of the ota-imageserver distribution (https://github.com/britnex/ota-imageserver).
* Copyright (c) 2019 Andre Massow britnex@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 main
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha1"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
)
var debug bool = false
func copyfile(src string, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}
if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
return err
}
return nil
}
func getfilehash(src string) (string, error) {
filein, err := os.Open(src)
if err != nil {
return "", err
}
defer filein.Close()
h := sha1.New()
if _, err := io.Copy(h, filein); err != nil {
return "", err
}
sum := h.Sum(nil)
return hex.EncodeToString(sum), nil
}
func main() {
defaulturl := "http://localhost:8090/image-1234.tgz"
ptgzsrc := flag.String("src", defaulturl, "image download url (required argument)")
ptgzdst := flag.String("dst", "./", "Save archive to <dst> directory")
ptgzref := flag.String("ref", "/", "Reference directory")
pdebug := flag.Bool("debug", false, "enable debug output")
flag.Parse()
if *ptgzsrc == defaulturl {
fmt.Println("usage:")
flag.PrintDefaults()
os.Exit(1)
}
if *pdebug {
debug = true
}
tgzsrc := *ptgzsrc
tgzdst := *ptgzdst
tgzref := *ptgzref
if strings.HasSuffix(tgzsrc, ".tgz") == false {
log.Fatalln("<src> argument requires .tgz suffix")
os.Exit(2)
}
if strings.HasSuffix(tgzref, "/") == false {
// ensure "/" suffix
tgzref = tgzref + "/"
}
if strings.HasSuffix(tgzdst, "/") {
// <dst> is directory
tgzdst = tgzdst + path.Base(tgzsrc)
} else if strings.HasSuffix(tgzdst, ".tgz") {
// <dst> is .tgz filename
} else {
// ensure "/" suffix
tgzdst = tgzdst + "/"
tgzdst = tgzdst + path.Base(tgzsrc)
}
if debug {
fmt.Printf("src: %s\n", tgzsrc)
fmt.Printf("dst: %s\n", tgzdst)
fmt.Printf("ref: %s\n", tgzref)
}
// step 1 : load "index" from server
fmt.Printf("downloading index from %s to %s\n", tgzsrc, tgzdst)
resp, err := http.Get(tgzsrc)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// save index file to tmp filename
tmpindexfile, err := ioutil.TempFile("/tmp/", "index-")
if err != nil {
panic(err)
}
if _, err := io.Copy(tmpindexfile, resp.Body); err != nil {
panic(err)
}
tmpindexfile.Close()
defer os.Remove(tmpindexfile.Name())
tmpindexin, err := os.Open(tmpindexfile.Name())
if err != nil {
panic(err)
}
defer tmpindexin.Close()
archivein, err := gzip.NewReader(tmpindexin)
if err != nil {
panic(err)
}
tr := tar.NewReader(archivein)
fileout, err := os.Create(tgzdst)
if err != nil {
panic(err)
}
defer fileout.Close()
archiveout := gzip.NewWriter(fileout)
trout := tar.NewWriter(archiveout)
var requestefilesbitmap bytes.Buffer
var hash = make([]byte, sha1.Size)
var regularfileindex uint32 = 0
var bitmapbyte byte = 0
var missingfiles uint32 = 0
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if hdr.Typeflag == '0' && hdr.Size > 0 {
var bitindex = 7 - (regularfileindex % 8)
regularfileindex++
var hashstr string
{ // parse hash
n, err := tr.Read(hash)
if (err != nil && err != io.EOF) || (n != sha1.Size) {
log.Fatalln("Server responded with an unknown file hash format!")
os.Exit(3)
}
hashstr = hex.EncodeToString(hash)
}
tmpfilename := "/tmp/" + hashstr + ".tmp"
var uselocalfile bool = true
{ // copy file to tmp
err = copyfile(tgzref+hdr.Name, tmpfilename)
if err != nil {
// cannot copy file => request from server
if debug {
fmt.Printf("file does not (yet) exists: %s\n", hdr.Name)
}
uselocalfile = false
}
}
if uselocalfile { // get file size
fi, err := os.Stat(tmpfilename)
if err != nil {
if debug {
fmt.Printf("file exists, cannot get file size : %s\n", hdr.Name)
}
uselocalfile = false
}
// change size to actual size of file
hdr.Size = fi.Size()
}
if uselocalfile { // compare file hashes
filehashstr, err := getfilehash(tmpfilename)
if err != nil || filehashstr != hashstr {
if debug {
fmt.Printf("file exists, hash does not match: %s\n", hdr.Name)
}
uselocalfile = false
}
}
if uselocalfile == false {
// set bit to 1 = request this file
bitmapbyte = bitmapbyte | (1 << bitindex)
}
if bitindex == 0 {
requestefilesbitmap.WriteByte(bitmapbyte)
bitmapbyte = 0 // set all bits to 0
}
if uselocalfile == false {
os.Remove(tmpfilename)
// request file from server
missingfiles++
continue
}
// write header of this file
trout.WriteHeader(hdr)
{ // write tmp file to output archive
fi, err := os.Open(tmpfilename)
if err != nil {
panic("cannot read local file. should never happen, because getfilehash was successful before!")
}
if _, err := io.Copy(trout, fi); err != nil {
panic(err)
}
fi.Close()
os.Remove(tmpfilename)
}
if debug {
fmt.Printf("> %s\n", hdr.Name)
}
} else {
// include dirs, links .. without changes
trout.WriteHeader(hdr)
if hdr.Size > 0 {
if _, err := io.Copy(trout, tr); err != nil {
log.Fatal(err)
}
}
}
}
// always include current bitmapbyte (even if empty)
requestefilesbitmap.WriteByte(bitmapbyte)
// step 2 : "load missing files" from server
if missingfiles > 0 {
fmt.Printf("downloading %d missing files from %s\n", missingfiles, tgzsrc)
var w bytes.Buffer
gw, err := gzip.NewWriterLevel(&w, gzip.BestCompression)
if err != nil {
panic(err)
}
gw.Write(requestefilesbitmap.Bytes())
gw.Close()
respp, err := http.Post(tgzsrc, "application/octet-stream", &w)
if err != nil {
panic(err)
}
defer respp.Body.Close()
// save diff file to tmp filename
tmpdifffile, err := ioutil.TempFile("/tmp/", "diff-")
if err != nil {
panic(err)
}
if _, err := io.Copy(tmpdifffile, respp.Body); err != nil {
panic(err)
}
tmpdifffile.Close()
tmpdiffin, err := os.Open(tmpdifffile.Name())
if err != nil {
panic(err)
}
defer tmpdiffin.Close()
archivein, err = gzip.NewReader(tmpdiffin)
if err != nil {
panic(err)
}
tr = tar.NewReader(archivein)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if debug {
fmt.Printf("< %s \n", hdr.Name)
}
// include downloaded files into archive
trout.WriteHeader(hdr)
if hdr.Size > 0 {
if _, err := io.Copy(trout, tr); err != nil {
log.Fatal(err)
}
}
}
os.Remove(tmpdifffile.Name())
}
trout.Close()
archiveout.Close() // write gzip footer
fmt.Println("done")
}