[PATCH] fetch2: Convert parts of unpack() to use parameter lists
Richard Purdie <[email protected]> Wed, 3 Jun 2026 14:21:42 +0100
| Newsgroups | org.openembedded.lists.bitbake-devel |
|---|---|
| Message-ID | <[email protected]> |
The unpack function makes heavy use of shell pipelines but start the conversion to avoid shell=True where possible. Signed-off-by: Richard Purdie <[email protected]> --- lib/bb/fetch2/__init__.py | 82 ++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py index 93d8b1820d8..191049f2127 100644 --- a/lib/bb/fetch2/__init__.py +++ b/lib/bb/fetch2/__init__.py @@ -1539,6 +1539,32 @@ class FetchMethod(object): bb.fatal("Invalid value for 'unpack' parameter for %s: %s" % (file, urldata.parm.get('unpack'))) + env = os.environ.copy() + path = data.getVar('PATH') + if path: + env['PATH'] = path + + # If 'subdir' param exists, create a dir and use it as destination for unpack cmd + if 'subdir' in urldata.parm: + subdir = urldata.parm.get('subdir') + if os.path.isabs(subdir): + if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)): + raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url) + unpackdir = subdir + else: + unpackdir = os.path.join(rootdir, subdir) + bb.utils.mkdirhier(unpackdir) + else: + unpackdir = rootdir + + def _run(cmd): + if isinstance(cmd, str): + ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir, env=env) + else: + ret = subprocess.call(cmd, preexec_fn=subprocess_setup, cwd=unpackdir, env=env) + if ret != 0: + raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url) + base, ext = os.path.splitext(file) if ext in ['.gz', '.bz2', '.Z', '.xz', '.lz', '.zst']: efile = os.path.join(rootdir, os.path.basename(base)) @@ -1547,36 +1573,36 @@ class FetchMethod(object): cmd = None if unpack: - tar_cmd = 'tar --extract --no-same-owner' + tar_cmd = ['tar', '--extract', '--no-same-owner'] if 'striplevel' in urldata.parm: striplevel = urldata.parm['striplevel'] if not striplevel.isdigit(): raise UnpackError("Invalid striplevel parameter: %s" % striplevel, urldata.url) - tar_cmd += ' --strip-components=%s' % striplevel + tar_cmd.append('--strip-components=%s' % striplevel) if file.endswith('.tar'): - cmd = '%s -f %s' % (tar_cmd, file) + cmd = tar_cmd + ['-f', file] elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'): - cmd = '%s -z -f %s' % (tar_cmd, file) + cmd = tar_cmd + ['-z', '-f', file] elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'): - cmd = 'bzip2 -dc %s | %s -f -' % (file, tar_cmd) + cmd = 'bzip2 -dc %s | %s -f -' % (file, shlex.join(tar_cmd)) elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'): cmd = 'gzip -dc %s > %s' % (file, efile) elif file.endswith('.bz2'): cmd = 'bzip2 -dc %s > %s' % (file, efile) elif file.endswith('.txz') or file.endswith('.tar.xz'): - cmd = 'xz -dc %s | %s -f -' % (file, tar_cmd) + cmd = 'xz -dc %s | %s -f -' % (file, shlex.join(tar_cmd)) elif file.endswith('.xz'): cmd = 'xz -dc %s > %s' % (file, efile) elif file.endswith('.tar.lz'): - cmd = 'lzip -dc %s | %s -f -' % (file, tar_cmd) + cmd = 'lzip -dc %s | %s -f -' % (file, shlex.join(tar_cmd)) elif file.endswith('.lz'): cmd = 'lzip -dc %s > %s' % (file, efile) elif file.endswith('.tar.7z'): - cmd = '7z x -so %s | %s -f -' % (file, tar_cmd) + cmd = '7z x -so %s | %s -f -' % (file, shlex.join(tar_cmd)) elif file.endswith('.7z'): cmd = '7za x -y %s 1>/dev/null' % file elif file.endswith('.tzst') or file.endswith('.tar.zst'): - cmd = 'zstd --decompress --stdout %s | %s -f -' % (file, tar_cmd) + cmd = 'zstd --decompress --stdout %s | %s -f -' % (file, shlex.join(tar_cmd)) elif file.endswith('.zst'): cmd = 'zstd --decompress --stdout %s > %s' % (file, efile) elif file.endswith('.zip') or file.endswith('.jar'): @@ -1585,10 +1611,10 @@ class FetchMethod(object): except ValueError as exc: bb.fatal("Invalid value for 'dos' parameter for %s: %s" % (file, urldata.parm.get('dos'))) - cmd = 'unzip -q -o' + cmd = ['unzip', '-q', '-o'] if dos: - cmd = '%s -a' % cmd - cmd = "%s '%s'" % (cmd, file) + cmd.append('-a') + cmd.append(file) elif file.endswith('.rpm') or file.endswith('.srpm'): if 'extract' in urldata.parm: unpack_file = urldata.parm.get('extract') @@ -1598,7 +1624,7 @@ class FetchMethod(object): else: cmd = 'rpm2cpio.sh %s | cpio --no-absolute-filenames -id' % (file) elif file.endswith('.deb') or file.endswith('.ipk'): - output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup) + output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup, env=env) datafile = None valid_datafiles = ('data.tar', 'data.tar.gz', 'data.tar.xz', 'data.tar.zst', 'data.tar.bz2', 'data.tar.lzma') @@ -1611,21 +1637,11 @@ class FetchMethod(object): raise UnpackError("Unable to unpack deb/ipk package - does not contain supported data.tar* file", urldata.url) else: raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url) - quoted_datafile = shlex.quote(datafile) - cmd = 'ar x %s %s && %s -p -f %s && rm %s' % (shlex.quote(file), quoted_datafile, tar_cmd, quoted_datafile, quoted_datafile) - - # If 'subdir' param exists, create a dir and use it as destination for unpack cmd - if 'subdir' in urldata.parm: - subdir = urldata.parm.get('subdir') - if os.path.isabs(subdir): - if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)): - raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url) - unpackdir = subdir - else: - unpackdir = os.path.join(rootdir, subdir) - bb.utils.mkdirhier(unpackdir) - else: - unpackdir = rootdir + bb.note("Unpacking %s to %s/" % (file, unpackdir)) + _run(['ar', 'x', file, datafile]) + _run(tar_cmd + ['-p' ,'-f', datafile]) + os.remove(os.path.join(unpackdir, datafile)) + return if not unpack or not cmd: urldata.unpack_tracer.unpack("file-copy", unpackdir) @@ -1642,21 +1658,15 @@ class FetchMethod(object): if urlpath.find("/") != -1: destdir = urlpath.rsplit("/", 1)[0] + '/' bb.utils.mkdirhier("%s/%s" % (unpackdir, destdir)) - cmd = 'cp --force --preserve=timestamps --no-dereference --recursive -H "%s" "%s"' % (file, destdir) + cmd = ['cp', '--force', '--preserve=timestamps', '--no-dereference', '--recursive', '-H', file, destdir] else: urldata.unpack_tracer.unpack("archive-extract", unpackdir) if not cmd: return - path = data.getVar('PATH') - if path: - cmd = "PATH=\"%s\" %s" % (path, cmd) bb.note("Unpacking %s to %s/" % (file, unpackdir)) - ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir) - - if ret != 0: - raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url) + _run(cmd) if iterate is True: iterate_urldata = urldata