Skip to content

Commit bb599ed

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 <norio.nomura@gmail.com>
1 parent ed8b889 commit bb599ed

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
@@ -409,6 +409,10 @@ func copyLocal(ctx context.Context, dst, src, ext string, decompress bool, descr
409409
if command != "" {
410410
return decompressLocal(ctx, command, dstPath, srcPath, ext, description)
411411
}
412+
commandByMagic := decompressorByMagic(srcPath)
413+
if commandByMagic != "" {
414+
return decompressLocal(ctx, commandByMagic, dstPath, srcPath, ext, description)
415+
}
412416
}
413417
// TODO: progress bar for copy
414418
return fs.CopyFile(dstPath, srcPath)
@@ -429,6 +433,34 @@ func decompressor(ext string) string {
429433
}
430434
}
431435

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

0 commit comments

Comments
 (0)