diff --git a/lib/irb/history.rb b/lib/irb/history.rb index ef25a6dbe..e3359b782 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -19,6 +19,32 @@ 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 + + def history_file_present + history_file if history_file && File.exist?(history_file) + end + + def folder_writable? + history_file && Pathname.new(history_file).dirname.writable? + end + + def append?(history_file, loaded_mtime) + File.exist?(history_file) && + File.mtime(history_file) != loaded_mtime + end + + def file_encoding + IRB.conf[:LC_MESSAGES].encoding + end + private def save_history_max @@ -40,12 +66,8 @@ def reset_history_counter def load_history 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| + if (history_file = History.history_file_present) + File.open(history_file, "r:#{History.file_encoding}") do |f| f.each { |l| l = l.chomp if self.class == RelineInputMethod and history.last&.end_with?("\\") @@ -62,50 +84,48 @@ def load_history end def save_history + return unless History.save_history? + return unless (history_file = History.history_file) + unless History.folder_writable? && correct_permissions!(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 - - # When HOME and XDG_CONFIG_HOME are not available, history_file might be nil - return unless history_file - - # 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 - end + if History.append?(history_file, @loaded_history_mtime) + history = history[@loaded_history_lines..-1] if @loaded_history_lines + append_history = true + 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 + File.open(history_file, (append_history ? "a" : "w"), 0o600, encoding: History.file_encoding) do |f| + hist = history.map { |l| l.scrub.split("\n").join("\\\n") } - 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 + unless append_history || History.infinite? + hist = hist.last(History.save_history) end - 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") } + f.puts(hist) + end + end + + private - unless append_history || History.infinite? - hist = hist.last(History.save_history) - end + # Ensure existing +history_file+ is owner-only-readable [BUG #7694]. + # Yields boolean whether chmod succeeded. `true` when file is absent. + def correct_permissions!(history_file) + return true unless File.exist?(history_file) - f.puts(hist) + begin + if File.stat(history_file).mode & 0o66 != 0 + File.chmod(0o600, history_file) 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