Skip to content

Commit

Permalink
Support extracting .tar.xz files on OpenBSD.
Browse files Browse the repository at this point in the history
* OpenBSD's `tar` does not support the `-J` flag, thus we must use
  `xzcat $file | tar xf - -C $dest` to first uncompress the `.tar.xz`
  file and pipe it into `tar` to then extract it.
  • Loading branch information
postmodern authored and flavorjones committed May 29, 2024
1 parent e432c0b commit 4bca4d2
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,30 +504,6 @@ def log_file(action)
}
end

TAR_EXECUTABLES = %w[gtar bsdtar tar basic-bsdtar]
def tar_exe
@@tar_exe ||= begin
TAR_EXECUTABLES.find { |c|
which(c)
} or raise("tar not found - please make sure that one of the following commands is in the PATH: #{TAR_EXECUTABLES.join(", ")}")
end
end

def tar_compression_switch(filename)
case File.extname(filename)
when '.gz', '.tgz'
'z'
when '.bz2', '.tbz2'
'j'
when '.xz'
'J'
when '.Z'
'Z'
else
''
end
end

# From: http://stackoverflow.com/a/5471032/7672
# Thanks, Mislav!
#
Expand Down Expand Up @@ -562,12 +538,35 @@ def detect_host
end
end

TAR_EXECUTABLES = %w[gtar bsdtar tar basic-bsdtar]
def tar_exe
@@tar_exe ||= begin
TAR_EXECUTABLES.find { |c|
which(c)
} or raise("tar not found - please make sure that one of the following commands is in the PATH: #{TAR_EXECUTABLES.join(", ")}")
end
end

def tar_command(file, target)
case File.extname(file)
when '.gz', '.tgz'
[tar_exe, 'xzf', file, '-C', target]
when '.bz2', '.tbz2'
[tar_exe, 'xjf', file, '-C', target]
when '.xz'
# NOTE: OpenBSD's tar command does not support the -J option
"xzcat #{file.shellescape} | #{tar_exe.shellescape} xf - -C #{target.shellescape}"
else
[tar_exe, 'xf', file, '-C', target]
end
end

def extract_file(file, target)
filename = File.basename(file)
FileUtils.mkdir_p target

message "Extracting #{filename} into #{target}... "
execute('extract', [tar_exe, "#{tar_compression_switch(filename)}xf", file, "-C", target], {:cd => Dir.pwd, :initial_message => false})
execute('extract', tar_command(file, target) , {:cd => Dir.pwd, :initial_message => false})
end

# command could be an array of args, or one string containing a command passed to the shell. See
Expand Down

0 comments on commit 4bca4d2

Please sign in to comment.