| 
									
										
										
										
											2024-07-11 19:57:12 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 17:53:31 +02:00
										 |  |  | require "system_command" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  | module UnpackStrategy | 
					
						
							| 
									
										
										
										
											2020-08-09 06:09:05 +02:00
										 |  |  |   # Strategy for unpacking tar archives. | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |   class Tar | 
					
						
							|  |  |  |     include UnpackStrategy | 
					
						
							| 
									
										
										
										
											2020-10-10 17:53:31 +02:00
										 |  |  |     extend SystemCommand::Mixin | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-05 22:29:41 -07:00
										 |  |  |     sig { override.returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2018-07-30 09:49:59 +02:00
										 |  |  |     def self.extensions | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         ".tar", | 
					
						
							|  |  |  |         ".tbz", ".tbz2", ".tar.bz2", | 
					
						
							|  |  |  |         ".tgz", ".tar.gz", | 
					
						
							| 
									
										
										
										
											2018-08-21 15:46:24 +02:00
										 |  |  |         ".tlzma", ".tar.lzma", | 
					
						
							| 
									
										
										
										
											2021-09-16 15:56:31 +01:00
										 |  |  |         ".txz", ".tar.xz", | 
					
						
							| 
									
										
										
										
											2024-03-24 18:55:23 -04:00
										 |  |  |         ".tar.zst", | 
					
						
							|  |  |  |         ".crate" | 
					
						
							| 
									
										
										
										
											2018-07-30 09:49:59 +02:00
										 |  |  |       ] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 19:57:12 +01:00
										 |  |  |     sig { override.params(path: Pathname).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2018-07-29 10:04:51 +02:00
										 |  |  |     def self.can_extract?(path) | 
					
						
							|  |  |  |       return true if path.magic_number.match?(/\A.{257}ustar/n) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 15:56:31 +01:00
										 |  |  |       return false unless [Bzip2, Gzip, Lzip, Xz, Zstd].any? { |s| s.can_extract?(path) } | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Check if `tar` can list the contents, then it can also extract it. | 
					
						
							| 
									
										
										
										
											2021-04-15 12:17:55 +01:00
										 |  |  |       stdout, _, status = system_command("tar", args: ["--list", "--file", path], print_stderr: false) | 
					
						
							| 
									
										
										
										
											2019-10-13 17:19:02 +02:00
										 |  |  |       status.success? && !stdout.empty? | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 11:05:42 +01:00
										 |  |  |     sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void } | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     def extract_to_dir(unpack_dir, basename:, verbose:) | 
					
						
							| 
									
										
										
										
											2024-02-26 16:58:39 +00:00
										 |  |  |       Dir.mktmpdir("homebrew-tar", HOMEBREW_TEMP) do |tmpdir| | 
					
						
							| 
									
										
										
										
											2021-09-16 15:56:31 +01:00
										 |  |  |         tar_path = if DependencyCollector.tar_needs_xz_dependency? && Xz.can_extract?(path) | 
					
						
							|  |  |  |           subextract(Xz, Pathname(tmpdir), verbose) | 
					
						
							|  |  |  |         elsif Zstd.can_extract?(path) | 
					
						
							|  |  |  |           subextract(Zstd, Pathname(tmpdir), verbose) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           path | 
					
						
							| 
									
										
										
										
											2018-07-30 16:31:29 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 18:43:20 +02:00
										 |  |  |         system_command! "tar", | 
					
						
							| 
									
										
										
										
											2021-04-15 12:17:55 +01:00
										 |  |  |                         args:    ["--extract", "--no-same-owner", | 
					
						
							|  |  |  |                                   "--file", tar_path, | 
					
						
							|  |  |  |                                   "--directory", unpack_dir], | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |                         verbose: | 
					
						
							| 
									
										
										
										
											2018-07-30 16:31:29 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-09-16 15:56:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sig { | 
					
						
							|  |  |  |       params(extractor: T.any(T.class_of(Xz), T.class_of(Zstd)), dir: Pathname, verbose: T::Boolean).returns(Pathname) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     def subextract(extractor, dir, verbose) | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       extractor.new(path).extract(to: dir, verbose:) | 
					
						
							| 
									
										
										
										
											2021-09-16 15:56:31 +01:00
										 |  |  |       T.must(dir.children.first) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | end |