diff options
author | Josef Šimánek <josef.simanek@gmail.com> | 2022-10-29 02:47:14 +0200 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2022-11-11 17:24:08 +0900 |
commit | 7ce0f81fbbd9c2b35e9fe35f5ef040626e284a88 (patch) | |
tree | 7bad4d0b3734a3c075b489cf6bcb53adbefcebbf /lib | |
parent | c7d043065c058f20ce30c61bb3ce127cb15cc0a8 (diff) | |
download | ruby-7ce0f81fbbd9c2b35e9fe35f5ef040626e284a88.tar.gz |
[rubygems/rubygems] Use file in XDG_STATE_HOME directory to store last update check timestamp.
https://github.com/rubygems/rubygems/commit/0fbc4ace8a
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rubygems/config_file.rb | 33 | ||||
-rw-r--r-- | lib/rubygems/defaults.rb | 14 | ||||
-rw-r--r-- | lib/rubygems/update_suggestion.rb | 18 |
3 files changed, 49 insertions, 16 deletions
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index b18f4115cc..f2abc7f2af 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -371,16 +371,18 @@ if you believe they were disclosed to a third party. @backtrace || $DEBUG end - # Check config file is writable. Creates empty file if not present to ensure we can write to it. - def config_file_writable? - if File.exist?(config_file_name) - File.writable?(config_file_name) + # Check state file is writable. Creates empty file if not present to ensure we can write to it. + def state_file_writable? + if File.exist?(state_file_name) + File.writable?(state_file_name) else require "fileutils" - FileUtils.mkdir_p File.dirname(config_file_name) - File.open(config_file_name, "w") {} + FileUtils.mkdir_p File.dirname(state_file_name) + File.open(state_file_name, "w") {} true end + rescue Errno::EACCES + false end # The name of the configuration file. @@ -388,6 +390,25 @@ if you believe they were disclosed to a third party. @config_file_name || Gem.config_file end + # The name of the state file. + def state_file_name + @state_file_name || Gem.state_file + end + + # Reads time of last update check from state file + def last_update_check + if File.readable?(state_file_name) + File.read(state_file_name).to_i + else + 0 + end + end + + # Writes time of last update check to state file + def last_update_check=(timestamp) + File.write(state_file_name, timestamp.to_s) if state_file_writable? + end + # Delegates to @hash def each(&block) hash = @hash.dup diff --git a/lib/rubygems/defaults.rb b/lib/rubygems/defaults.rb index e12c13cb46..8daff0bc30 100644 --- a/lib/rubygems/defaults.rb +++ b/lib/rubygems/defaults.rb @@ -134,6 +134,13 @@ module Gem end ## + # The path to standard location of the user's state file. + + def self.state_file + @state_file ||= File.join(Gem.state_home, "gem", "last_update_check").tap(&Gem::UNTAINT) + end + + ## # The path to standard location of the user's cache directory. def self.cache_home @@ -148,6 +155,13 @@ module Gem end ## + # The path to standard location of the user's state directory. + + def self.state_home + @data_home ||= (ENV["XDG_STATE_HOME"] || File.join(Gem.user_home, ".local", "state")) + end + + ## # How String Gem paths should be split. Overridable for esoteric platforms. def self.path_separator diff --git a/lib/rubygems/update_suggestion.rb b/lib/rubygems/update_suggestion.rb index 71c44af3af..d9ac517e49 100644 --- a/lib/rubygems/update_suggestion.rb +++ b/lib/rubygems/update_suggestion.rb @@ -41,24 +41,22 @@ Run `gem update --system #{Gem.latest_rubygems_version}` to update your installa return false if Gem.disable_system_update_message return false if ci? - # check makes sense only when we can store of last try - # otherwise we will not be able to prevent annoying update message + # check makes sense only when we can store timestamp of last try + # otherwise we will not be able to prevent "annoying" update message # on each command call - return unless Gem.configuration.config_file_writable? + return unless Gem.configuration.state_file_writable? # load time of last check, ensure the difference is enough to repeat the suggestion check_time = Time.now.to_i - last_update_check = Gem.configuration[:last_update_check] || 0 + last_update_check = Gem.configuration.last_update_check return false if (check_time - last_update_check) < ONE_WEEK # compare current and latest version, this is the part where # latest rubygems spec is fetched from remote - (Gem.rubygems_version < Gem.latest_rubygems_version).tap do |eglible| - if eglible - # store the time of last successful check into config file - Gem.configuration[:last_update_check] = check_time - Gem.configuration.write - end + if (Gem.rubygems_version < Gem.latest_rubygems_version) + # store the time of last successful check into state file + Gem.configuration.last_update_check = check_time + return true end rescue # don't block install command on any problem false |