diff --git a/lib/irb/history.rb b/lib/irb/history.rb index ef25a6dbe..a428003d2 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -19,6 +19,15 @@ def infinite? save_history.negative? end + # Might be nil when HOME and XDG_CONFIG_HOME are not available. + def history_file + if (history_file = IRB.conf[:HISTORY_FILE]) + File.expand_path(history_file) + else + IRB.rc_file("_history") + end + end + private def save_history_max @@ -38,74 +47,74 @@ def reset_history_counter end def load_history + history_file = History.history_file + return unless File.exist?(history_file.to_s) + history = self.class::HISTORY - if history_file = IRB.conf[:HISTORY_FILE] - history_file = File.expand_path(history_file) - end - history_file = IRB.rc_file("_history") unless history_file - if history_file && File.exist?(history_file) - File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f| - f.each { |l| - l = l.chomp - if self.class == RelineInputMethod and history.last&.end_with?("\\") - history.last.delete_suffix!("\\") - history.last << "\n" << l - else - history << l - end - } - end - @loaded_history_lines = history.size - @loaded_history_mtime = File.mtime(history_file) + File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f| + f.each { |l| + l = l.chomp + if self.class == RelineInputMethod and history.last&.end_with?("\\") + history.last.delete_suffix!("\\") + history.last << "\n" << l + else + history << l + end + } end + @loaded_history_lines = history.size + @loaded_history_mtime = File.mtime(history_file) end def save_history + return unless History.save_history? + return unless (history_file = History.history_file) + unless ensure_history_file_writable(history_file) + warn <<~WARN + Can't write history to #{History.history_file.inspect} due to insufficient permissions. + Please verify the value of `IRB.conf[:HISTORY_FILE]`. Ensure the folder exists and that both the folder and file (if it exists) are writable. + WARN + return + end + history = self.class::HISTORY.to_a - if History.save_history? - if history_file = IRB.conf[:HISTORY_FILE] - history_file = File.expand_path(history_file) - end - history_file = IRB.rc_file("_history") unless history_file + if File.exist?(history_file) && + File.mtime(history_file) != @loaded_history_mtime + history = history[@loaded_history_lines..-1] if @loaded_history_lines + append_history = true + end - # When HOME and XDG_CONFIG_HOME are not available, history_file might be nil - return unless history_file + File.open(history_file, (append_history ? "a" : "w"), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f| + hist = history.map { |l| l.scrub.split("\n").join("\\\n") } - # Change the permission of a file that already exists[BUG #7694] - begin - if File.stat(history_file).mode & 066 != 0 - File.chmod(0600, history_file) - end - rescue Errno::ENOENT - rescue Errno::EPERM - return - rescue - raise + unless append_history || History.infinite? + hist = hist.last(History.save_history) end - if File.exist?(history_file) && - File.mtime(history_file) != @loaded_history_mtime - history = history[@loaded_history_lines..-1] if @loaded_history_lines - append_history = true - end + f.puts(hist) + end + end - pathname = Pathname.new(history_file) - unless Dir.exist?(pathname.dirname) - warn "Warning: The directory to save IRB's history file does not exist. Please double check `IRB.conf[:HISTORY_FILE]`'s value." - return - end + private - File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f| - hist = history.map{ |l| l.scrub.split("\n").join("\\\n") } + # Returns boolean whether writing to +history_file+ will be possible. + # Permissions of already existing +history_file+ are changed to + # owner-only-readable if necessary [BUG #7694]. + def ensure_history_file_writable(history_file) + history_file = Pathname.new(history_file) - unless append_history || History.infinite? - hist = hist.last(History.save_history) - end + return false unless history_file.dirname.writable? + return true unless history_file.exist? - f.puts(hist) + begin + if history_file.stat.mode & 0o66 != 0 + history_file.chmod 0o600 end + true + rescue Errno::EPERM # no permissions + false end end end diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb index 8355a7a3d..791eef1ac 100644 --- a/test/irb/test_history.rb +++ b/test/irb/test_history.rb @@ -181,7 +181,7 @@ def test_history_does_not_raise_when_history_file_directory_does_not_exist IRB.conf[:HISTORY_FILE] = "fake/fake/fake/history_file" io = TestInputMethodWithRelineHistory.new - assert_warn(/history file does not exist/) do + assert_warn(/ensure the folder exists/i) do io.save_history end