| 
									
										
										
										
											2024-07-11 19:57:12 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  | module UnpackStrategy | 
					
						
							| 
									
										
										
										
											2020-08-09 06:09:05 +02:00
										 |  |  |   # Strategy for unpacking directories. | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |   class Directory | 
					
						
							|  |  |  |     include UnpackStrategy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |       [] | 
					
						
							|  |  |  |     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) | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |       path.directory? | 
					
						
							|  |  |  |     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-10-03 15:50:23 -04:00
										 |  |  |       path_children = path.children | 
					
						
							|  |  |  |       return if path_children.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       existing = unpack_dir.children | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # We run a few cp attempts in the following order: | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # 1. Start with `-al` to create hardlinks rather than copying files if the source and | 
					
						
							|  |  |  |       #    target are on the same filesystem. On macOS, this is the only cp option that can | 
					
						
							|  |  |  |       #    preserve hardlinks but it is only available since macOS 12.3 (file_cmds-353.100.22). | 
					
						
							|  |  |  |       # 2. Try `-a` as GNU `cp -a` preserves hardlinks. macOS `cp -a` is identical to `cp -pR`. | 
					
						
							|  |  |  |       # 3. Fall back on `-pR` to handle the case where GNU `cp -a` failed. This may happen if | 
					
						
							|  |  |  |       #    installing into a filesystem that doesn't support hardlinks like an exFAT USB drive. | 
					
						
							|  |  |  |       cp_arg_attempts = ["-a", "-pR"] | 
					
						
							|  |  |  |       cp_arg_attempts.unshift("-al") if path.stat.dev == unpack_dir.stat.dev | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cp_arg_attempts.each do |arg| | 
					
						
							|  |  |  |         args = [arg, *path_children, unpack_dir] | 
					
						
							|  |  |  |         must_succeed = print_stderr = (arg == cp_arg_attempts.last) | 
					
						
							|  |  |  |         result = system_command("cp", args:, verbose:, must_succeed:, print_stderr:) | 
					
						
							|  |  |  |         break if result.success? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         FileUtils.rm_r(unpack_dir.children - existing) | 
					
						
							| 
									
										
										
										
											2018-07-23 20:59:21 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |