[前][次][番号順一覧][スレッド一覧]

ruby-changes:74447

From: Josef <ko1@a...>
Date: Fri, 11 Nov 2022 17:24:29 +0900 (JST)
Subject: [ruby-changes:74447] 7ce0f81fbb (master): [rubygems/rubygems] Use file in XDG_STATE_HOME directory to store last update check timestamp.

https://git.ruby-lang.org/ruby.git/commit/?id=7ce0f81fbb

From 7ce0f81fbbd9c2b35e9fe35f5ef040626e284a88 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= <josef.simanek@g...>
Date: Sat, 29 Oct 2022 02:47:14 +0200
Subject: [rubygems/rubygems] Use file in XDG_STATE_HOME directory to store
 last update check timestamp.

https://github.com/rubygems/rubygems/commit/0fbc4ace8a
---
 lib/rubygems/config_file.rb                 | 33 +++++++++++++++++++++++------
 lib/rubygems/defaults.rb                    | 14 ++++++++++++
 lib/rubygems/update_suggestion.rb           | 18 +++++++---------
 test/rubygems/helper.rb                     |  3 +++
 test/rubygems/test_gem_update_suggestion.rb | 10 ++++-----
 5 files changed, 57 insertions(+), 21 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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/config_file.rb#L371
     @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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/config_file.rb#L390
     @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
@@ -133,6 +133,13 @@ module Gem https://github.com/ruby/ruby/blob/trunk/lib/rubygems/defaults.rb#L133
     @config_file ||= find_config_file.tap(&Gem::UNTAINT)
   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.
 
@@ -147,6 +154,13 @@ module Gem https://github.com/ruby/ruby/blob/trunk/lib/rubygems/defaults.rb#L154
     @data_home ||= (ENV["XDG_DATA_HOME"] || File.join(Gem.user_home, ".local", "share"))
   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.
 
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 https://github.com/ruby/ruby/blob/trunk/lib/rubygems/update_suggestion.rb#L41
     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
diff --git a/test/rubygems/helper.rb b/test/rubygems/helper.rb
index 43423dc101..8d94f82d8e 100644
--- a/test/rubygems/helper.rb
+++ b/test/rubygems/helper.rb
@@ -307,6 +307,7 @@ class Gem::TestCase < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/helper.rb#L307
     ENV["XDG_CACHE_HOME"] = nil
     ENV["XDG_CONFIG_HOME"] = nil
     ENV["XDG_DATA_HOME"] = nil
+    ENV["XDG_STATE_HOME"] = nil
     ENV["SOURCE_DATE_EPOCH"] = nil
     ENV["BUNDLER_VERSION"] = nil
     ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = "true"
@@ -327,6 +328,7 @@ class Gem::TestCase < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/helper.rb#L328
 
     @gemhome  = File.join @tempdir, "gemhome"
     @userhome = File.join @tempdir, "userhome"
+    @statehome = File.join @tempdir, "statehome"
     ENV["GEM_SPEC_CACHE"] = File.join @tempdir, "spec_cache"
 
     @orig_ruby = if ENV["RUBY"]
@@ -361,6 +363,7 @@ class Gem::TestCase < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/helper.rb#L363
     Gem.instance_variable_set :@user_home, nil
     Gem.instance_variable_set :@config_home, nil
     Gem.instance_variable_set :@data_home, nil
+    Gem.instance_variable_set :@state_home, @statehome
     Gem.instance_variable_set :@gemdeps, nil
     Gem.instance_variable_set :@env_requirements_by_name, nil
     Gem.send :remove_instance_variable, :@ruby_version if
diff --git a/test/rubygems/test_gem_update_suggestion.rb b/test/rubygems/test_gem_update_suggestion.rb
index aefebf41cf..ba54059ee7 100644
--- a/test/rubygems/test_gem_update_suggestion.rb
+++ b/test/rubygems/test_gem_update_suggestion.rb
@@ -27,7 +27,7 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L27
     original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], nil
     original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], nil
     original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, nil
-    Gem.configuration[:last_update_check] = nil
+    Gem.configuration.last_update_check = 0
 
     Gem.ui.stub :tty?, tty do
       Gem.stub :rubygems_version, rubygems_version do
@@ -61,10 +61,10 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L61
     with_eglible_environment(cmd: @cmd) do
       Time.stub :now, 123456789 do
         assert @cmd.eglible_for_update?
-        assert_equal Gem.configuration[:last_update_check], 123456789
+        assert_equal Gem.configuration.last_update_check, 123456789
 
         # test last check is written to config file
-        assert File.read(Gem.configuration.config_file_name).match("last_update_check: 123456789")
+        assert File.read(Gem.configuration.state_file_name).match("123456789")
       end
     end
   end
@@ -122,7 +122,7 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L122
 
   def test_eglible_for_update_unwrittable_config
     with_eglible_environment(ci: true, cmd: @cmd) do
-      Gem.configuration.stub :config_file_writable?, false do
+      Gem.configuration.stub :state_file_writable?, false do
         refute @cmd.eglible_for_update?
       end
     end
@@ -130,7 +130,7 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L130
 
   def test_eglible_for_update_notification_delay
     with_eglible_environment(cmd: @cmd) do
-      Gem.configuration[:last_update_check] = Time.now.to_i
+      Gem.configuration.last_update_check = Time.now.to_i
       refute @cmd.eglible_for_update?
     end
   end
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]