| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  | # Create a lock using `flock(2)`. A command name with arguments is required as | 
					
						
							|  |  |  | # first argument. The lock will be automatically unlocked when the shell process | 
					
						
							|  |  |  | # quits. Note due to the fixed FD, a shell process can only create one lock. | 
					
						
							| 
									
										
										
										
											2024-05-02 10:33:42 +01:00
										 |  |  | # HOMEBREW_LIBRARY is by brew.sh | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  | # HOMEBREW_PREFIX is set by extend/ENV/super.rb | 
					
						
							|  |  |  | # shellcheck disable=SC2154 | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  | lock() { | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  |   local command_name_and_args="$1" | 
					
						
							|  |  |  |   # use bash to replace spaces with dashes | 
					
						
							|  |  |  |   local lock_filename="${command_name_and_args// /-}" | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  |   local lock_dir="${HOMEBREW_PREFIX}/var/homebrew/locks" | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  |   local lock_file="${lock_dir}/${lock_filename}" | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  |   [[ -d "${lock_dir}" ]] || mkdir -p "${lock_dir}" | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ ! -w "${lock_dir}" ]] | 
					
						
							| 
									
										
										
										
											2016-11-11 22:52:21 +00:00
										 |  |  |   then | 
					
						
							|  |  |  |     odie <<EOS | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  | Can't create \`brew ${command_name_and_args}\` lock in ${lock_dir}! | 
					
						
							| 
									
										
										
										
											2016-11-11 22:52:21 +00:00
										 |  |  | Fix permissions by running: | 
					
						
							| 
									
										
										
										
											2023-09-29 12:32:22 +01:00
										 |  |  |   sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_PREFIX}/var/homebrew | 
					
						
							| 
									
										
										
										
											2016-11-11 22:52:21 +00:00
										 |  |  | EOS | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-05-02 10:33:42 +01:00
										 |  |  |   # 200 is the file descriptor (FD) used in the lock. | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |   # This FD should be used exclusively for lock purpose. | 
					
						
							|  |  |  |   # Any value except 0(stdin), 1(stdout) and 2(stderr) can do the job. | 
					
						
							|  |  |  |   # Noted, FD is unique per process but it will be shared to subprocess. | 
					
						
							|  |  |  |   # It is recommended to choose a large number to avoid conflicting with | 
					
						
							|  |  |  |   # other FD opened by the script. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # close FD first, this is required if parent process holds a different lock. | 
					
						
							|  |  |  |   exec 200>&- | 
					
						
							|  |  |  |   # open the lock file to FD, so the shell process can hold the lock. | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  |   exec 200>"${lock_file}" | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ! _create_lock 200 "${command_name_and_args}" | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  |     local lock_context | 
					
						
							|  |  |  |     if [[ -n "${HOMEBREW_LOCK_CONTEXT}" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       lock_context="\n${HOMEBREW_LOCK_CONTEXT}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |     odie <<EOS | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  | Another \`brew ${command_name_and_args}\` process is already running.${lock_context} | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  | Please wait for it to finish or terminate it to continue. | 
					
						
							|  |  |  | EOS | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _create_lock() { | 
					
						
							| 
									
										
										
										
											2024-05-02 10:33:42 +01:00
										 |  |  |   local lock_file_descriptor="$1" | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  |   local command_name_and_args="$2" | 
					
						
							| 
									
										
										
										
											2016-05-23 14:36:25 +01:00
										 |  |  |   local ruby="/usr/bin/ruby" | 
					
						
							| 
									
										
										
										
											2017-05-11 12:06:55 -07:00
										 |  |  |   local python="/usr/bin/python" | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  |   [[ -x "${ruby}" ]] || ruby="$(type -P ruby)" | 
					
						
							|  |  |  |   [[ -x "${python}" ]] || python="$(type -P python)" | 
					
						
							| 
									
										
										
										
											2016-05-23 14:36:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 10:33:42 +01:00
										 |  |  |   local utils_lock_sh="${HOMEBREW_LIBRARY}/Homebrew/utils/lock_sh" | 
					
						
							|  |  |  |   local oldest_ruby_with_flock="1.8.7" | 
					
						
							|  |  |  |   if [[ -x "${ruby}" ]] && "${ruby}" "${utils_lock_sh}/ruby_check_version.rb" "${oldest_ruby_with_flock}" | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2024-05-02 10:33:42 +01:00
										 |  |  |     "${ruby}" "${utils_lock_sh}/ruby_lock_file_descriptor.rb" "${lock_file_descriptor}" | 
					
						
							|  |  |  |   elif [[ -x "$(type -P flock)" ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     flock -n "${lock_file_descriptor}" | 
					
						
							| 
									
										
										
										
											2021-04-21 20:51:50 +09:00
										 |  |  |   elif [[ -x "${python}" ]] | 
					
						
							| 
									
										
										
										
											2017-05-11 12:06:55 -07:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2024-09-01 10:40:23 -07:00
										 |  |  |     "${python}" -c "import fcntl; fcntl.flock(${lock_file_descriptor}, fcntl.LOCK_EX | fcntl.LOCK_NB)" | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2024-07-30 17:51:02 +01:00
										 |  |  |     onoe "Cannot create \`brew ${command_name_and_args}\` lock due to missing/too old ruby/flock/python, please avoid running Homebrew in parallel." | 
					
						
							| 
									
										
										
										
											2016-05-02 17:52:19 +08:00
										 |  |  |   fi | 
					
						
							|  |  |  | } |