From dd5cdeace5c437102636df17258b056904844a17 Mon Sep 17 00:00:00 2001 From: Stefan Hansson Date: Sun, 10 Mar 2024 14:39:30 +0100 Subject: [PATCH] misc: Also check for .zst-compressed variants of files (MR 49) Initially I thought of breaking off the Stat + error check call into its own function as to reduce repetition, but given that it's only useful in this situation where it only happens twice anyway, I'm not sure it actually would reduce complexity. Additionally, this means that .zst-compressed variants of files will be searched for in all contexts where this function is used. I'm not sure this is desirable. Tested and works with arrow-db820c. I didn't test it on actual hardware, but I verified that the firmware ended up in the initramfs via $ pmbootstrap initfs ls. I choose this device because it uses firmware from linux-firmware and also needs said firmware present in the initramfs. Closes https://gitlab.com/postmarketOS/postmarketos-mkinitfs/-/issues/39 --- internal/misc/getfiles.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/misc/getfiles.go b/internal/misc/getfiles.go index 681c078..b451fb4 100644 --- a/internal/misc/getfiles.go +++ b/internal/misc/getfiles.go @@ -41,10 +41,22 @@ func getFile(file string, required bool) (files []string, err error) { fileInfo, err := os.Stat(file) if err != nil { - if required { - return files, fmt.Errorf("getFile: failed to stat file %q: %w", file, err) + // Check if there is a Zstd-compressed version of the file + fileZstd := file + ".zst" // .zst is the extension used by linux-firmware + fileInfoZstd, errZstd := os.Stat(fileZstd) + + if errZstd == nil { + file = fileZstd + fileInfo = fileInfoZstd + // Unset nil so we don't retain the error from the os.Stat call for the uncompressed version. + err = nil + } else { + if required { + return files, fmt.Errorf("getFile: failed to stat file %q: %w (also tried %q: %w)", file, err, fileZstd, errZstd) + } + + return files, nil } - return files, nil } if fileInfo.IsDir() {