_7zz: speculatively try unpacking any file (#566897)

This commit is contained in:
Ihar Hrachyshka
2026-10-14 23:09:48 +00:00
committed by GitHub
2 changed files with 104 additions and 7 deletions

View File

@@ -22,6 +22,7 @@
# For tests
testers,
fetchurl,
}:
let
@@ -165,9 +166,83 @@ stdenv.mkDerivation (finalAttrs: {
extraArgs = [ "--version=skip" ];
})
];
tests.version = testers.testVersion {
package = finalAttrs.finalPackage;
command = "7zz --help";
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
command = "7zz --help";
};
# So dmg can be tested on Linux without
# macos toolchains
undmg = stdenv.mkDerivation {
pname = "test-undmg";
version = "none";
src = fetchurl {
url = "https://caido.download/releases/v0.57.0/caido-desktop-v0.57.0-mac-aarch64.dmg";
hash = "sha256-GmpMnaGR7gYz1RvSO5xj9AA3xU1mn2IBInakmVkuG7A=";
};
nativeBuildInputs = [
finalAttrs.finalPackage
];
buildPhase = ''
if [[ ! -e Contents ]]; then
# Should exist
ls
exit 1
fi
'';
installPhase = "touch $out";
};
unzip = stdenv.mkDerivation {
pname = "test-unzip";
version = "none";
src = fetchurl {
url = "mirror://sourceforge/libjson/libjson_7.6.1.zip";
hash = "sha256-ByZ6OVEDjuLgLSbMQb+OJ1Zow491EkDT543JeRguc3Y=";
};
nativeBuildInputs = [
finalAttrs.finalPackage
];
buildPhase = ''
if [[ ! -e libjson.h ]]; then
# Should exist
ls
exit 1
fi
'';
installPhase = "touch $out";
};
# We do not want to decompress tarballs since
# it will just leave an uncompressed tarball in the source dir.
tarballs = stdenv.mkDerivation {
pname = "test-tarball";
version = "none";
src = fetchurl {
url = "https://github.com/ip7z/7zip/archive/refs/tags/26.04.tar.gz";
hash = "sha256-SP3G3+h57i24Z7sH8ZxKlAa/6gHVH0h4W2x6E3iB4lw=";
};
nativeBuildInputs = [
finalAttrs.finalPackage
];
buildPhase = ''
if [[ ! -e README.md ]]; then
# Should exist
ls
exit 1
fi
'';
installPhase = "touch $out";
};
};
};

View File

@@ -1,5 +1,27 @@
unpackCmdHooks+=(_tryUnpackDmg)
_tryUnpackDmg() {
if ! [[ "$curSrc" =~ \.dmg$ ]]; then return 1; fi
7zz x -sns- -snld "$curSrc"
unpackCmdHooks+=(_tryUnpack7zip)
# 7zip supports unpacking many formats.
# No need to implement our own inaccurate extension-based detection.
_tryUnpack7zip() {
local mime="$(file -zb --mime-type "$curSrc")"
if [[ "$mime" == "application/x-tar" ]]; then
# Compressed tarballs should use tar instead, and 7zip doesn't
# recursively unpack. So it would just leave a .tar file in
# the build directory
return 1
fi
local flags=(
"-bso0" # Disable stdout
"-bsp0" # Disable progress output
"-bse0" # Disable stderr output
"-sns-" # Ignore auxilliary filesystem streams
# Allow 'dangerous' links, which are links that include '..'
# This does not include absolute paths and links that escape the archive,
# those require a higher 'dangerous level'.
"-snld"
"-y" # Accept all prompts
)
7zz x "${flags[@]}" "$curSrc"
}