crew: Replace binutils/ar with a new library (#6642)

* Create deb_utils.rb

* Update crew

* Update core.rb

* Update const.rb

* Update deb_utils.rb

* Wrap as a module

Co-authored-by: Ed Reel <edreel@gmail.com>
This commit is contained in:
supechicken
2022-01-22 16:50:18 +08:00
committed by GitHub
parent 8132c60dbc
commit bd02980d15
4 changed files with 54 additions and 15 deletions

View File

@@ -13,6 +13,7 @@ require_relative '../lib/const'
require_relative '../lib/util'
require_relative '../lib/convert_size'
require_relative '../lib/downloader'
require_relative '../lib/deb_utils'
# Add lib to LOAD_PATH
$LOAD_PATH.unshift "#{CREW_LIB_PATH}lib"
@@ -803,19 +804,9 @@ def unpack(meta)
puts "Unpacking archive using 'tar', this may take a while..."
system "tar x#{@verbose}f #{meta[:filename]} -C #{@extract_dir}", exception: true
when /\.deb$/i
puts "Unpacking archive using 'ar', this may take a while..."
suffix = `ar -t #{meta[:filename]}`.scan(/data.(.*)/)[0][0]
case suffix
when 'tar.xz'
tar_opt = '--xz'
when 'tgz', 'tar.gz'
tar_opt = '--gzip'
when 'bzip2'
tar_opt = '--bzip2'
end
system "ar -p #{meta[:filename]} data.#{suffix} | tar x#{@verbose} #{tar_opt} -C #{@extract_dir}", exception: true
puts "Unpacking '.deb' archive, this may take a while..."
DebUtils::extract_deb(meta[:filename], /data\..*/)
system "tar x#{@verbose}f data.* -C #{@extract_dir}", exception: true
when /\.AppImage$/i
puts "Unpacking 'AppImage' archive, this may take a while..."
FileUtils.chmod 0o755, meta[:filename], verbose: @fileutils_verbose

View File

@@ -1,6 +1,6 @@
# Defines common constants used in different parts of crew
CREW_VERSION = '1.21.8'
CREW_VERSION = '1.22.0'
ARCH_ACTUAL = `uname -m`.chomp
# This helps with virtualized builds on aarch64 machines

49
lib/deb_utils.rb Normal file
View File

@@ -0,0 +1,49 @@
module DebUtils
def self.extract_deb(file, target)
# extract_deb: unarchive .deb files
# Usage: extract_deb(<file>, <target (optional)>)
# file: a .deb archive file
# target: (optional) a specified file to extract from archive, should be passed in regex or string
#
# Example:
# extract_deb('example.deb', 'test.txt') # extract `test.txt` from example.deb
# extract_deb('example.deb', /data\..*/) # extract files from example.deb with filenames matching the /data\..*/ regex
# extract_deb('example.deb', /*/) # extract all files from example.deb
#
file_found = false
src_fileIO = File.open(file, 'rb')
# get first line of the given file, should be a signature string (`!<arch>\n`) if it is a valid deb file
signature = src_fileIO.gets
abort 'Malformed archive :/' unless signature == "!<arch>\n"
# process each file in archive
while (line = src_fileIO.gets) do
# read file meta
name, modtime, uid, gid, mode, size, end_char = \
line.scan(/(.{16})(.{12})(.{6})(.{6})(.{8})(.{10})(.{1})/).flatten.map(&:strip)
# check ending byte
abort "Malformed archive :/" unless end_char == '`'
# capture file in archive with given offset bytes (file size)
fileContent = src_fileIO.read(size.to_i)
# filter filename if a target file is specified
if target.is_a?(String) and name == target
# if target is passed as string, write matched file to filesyetem and exit function
# write to filesystem
File.open(name, 'wb') {|dst_fileIO| dst_fileIO.write(fileContent) }
# exit function
return true
elsif target.is_a?(Regexp) and name =~ target
# if target is passed as regex, write matched file to filesyetem and continue
# searching for another matched file until EOF
# write to filesystem
File.open(name, 'wb') {|dst_fileIO| dst_fileIO.write(fileContent) }
file_found = true
end
end
abort "Target #{target.inspect} not found in archive. :/".lightred unless file_found
end
end

View File

@@ -9,7 +9,6 @@ class Core < Package
is_fake
depends_on 'binutils'
depends_on 'brotli'
depends_on 'bz2'
depends_on 'c_ares'