Skip to content

Commit 214879e

Browse files
committed
downloader: detect compression by reading magic
Beacause the kernel file provided by Ubuntu is gzipped without `.gz` file extension. Signed-off-by: Norio Nomura <[email protected]>
1 parent eaf8c74 commit 214879e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pkg/downloader/downloader.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ func copyLocal(ctx context.Context, dst, src, ext string, decompress bool, descr
408408
if command != "" {
409409
return decompressLocal(ctx, command, dstPath, srcPath, ext, description)
410410
}
411+
commandByMagic := decompressorByMagic(srcPath)
412+
if commandByMagic != "" {
413+
return decompressLocal(ctx, commandByMagic, dstPath, srcPath, ext, description)
414+
}
411415
}
412416
// TODO: progress bar for copy
413417
return fs.CopyFile(dstPath, srcPath)
@@ -428,6 +432,34 @@ func decompressor(ext string) string {
428432
}
429433
}
430434

435+
func decompressorByMagic(file string) string {
436+
f, err := os.Open(file)
437+
if err != nil {
438+
return ""
439+
}
440+
defer f.Close()
441+
header := make([]byte, 6)
442+
if _, err := f.Read(header); err != nil {
443+
return ""
444+
}
445+
if _, err := f.Seek(0, io.SeekStart); err != nil {
446+
return ""
447+
}
448+
if bytes.HasPrefix(header, []byte{0x1f, 0x8b}) {
449+
return "gzip"
450+
}
451+
if bytes.HasPrefix(header, []byte{0x42, 0x5a}) {
452+
return "bzip2"
453+
}
454+
if bytes.HasPrefix(header, []byte{0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00}) {
455+
return "xz"
456+
}
457+
if bytes.HasPrefix(header, []byte{0x28, 0xb5, 0x2f, 0xfd}) {
458+
return "zstd"
459+
}
460+
return ""
461+
}
462+
431463
func decompressLocal(ctx context.Context, decompressCmd, dst, src, ext, description string) error {
432464
logrus.Infof("decompressing %s with %v", ext, decompressCmd)
433465

0 commit comments

Comments
 (0)