utils/gems: make .homebrew_gem_groups writing atomic

This commit is contained in:
Bo Anderson 2023-10-30 20:26:08 +00:00
parent 0a6c5214ec
commit a339b912c0
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

@ -191,7 +191,16 @@ module Homebrew
end
def write_user_gem_groups(groups)
GEM_GROUPS_FILE.write(groups.join("\n"))
# Write the file atomically, in case we're working parallel
require "tempfile"
tmpfile = Tempfile.new([GEM_GROUPS_FILE.basename.to_s, "~"], GEM_GROUPS_FILE.dirname)
begin
tmpfile.write(groups.join("\n"))
tmpfile.close
File.rename(tmpfile.path.to_s, GEM_GROUPS_FILE)
ensure
tmpfile.unlink
end
end
def forget_user_gem_groups!