From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id 055A52D45D7; Sat, 28 Nov 2026 17:13:35 +1100 (AEDT) Date: Sat, 28 Nov 2026 17:13:43 +1100 To: sup-commits@supmua.dev Subject: [sup.git] 08/08: bring back sup-recover-sources from the dead MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Reply-To: Dan Callaghan In-Reply-To: <179584641514.302139.17817016268117078700@bl2.djc.id.au> References: <179584641514.302139.17817016268117078700@bl2.djc.id.au> Thread-Index: AQAAEjRW8AyDbWtbRRaCMIXB6V3YXw== X-Git-Host: bl2.djc.id.au X-Git-Repo: sup.git X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Rev: 53f07beb4310b71784861d4d81db6425b4af33c7 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated Message-Id: <20261128061336.055A52D45D7@bl2.djc.id.au> From: Dan Callaghan List-Id: New commit on branch develop: https://supmua.dev/git/sup/commit/53f07beb4310b71784861d4d81db6425b4af33c7.html commit 53f07beb4310b71784861d4d81db6425b4af33c7 Author: Dan Callaghan AuthorDate: Sun Nov 22 18:03:06 2026 +1100 bring back sup-recover-sources from the dead --- Manifest.txt | 1 + bin/sup-recover-sources | 118 +++++++++++++-------------- lib/sup/index.rb | 9 +- lib/sup/maildir.rb | 10 +++ lib/sup/mbox.rb | 16 ++++ lib/sup/source.rb | 6 ++ test/integration/test_sup-recover-sources.rb | 68 +++++++++++++++ 7 files changed, 164 insertions(+), 64 deletions(-) diff --git a/Manifest.txt b/Manifest.txt index 472b0372..836470a6 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -198,6 +198,7 @@ test/integration/test_index.rb test/integration/test_maildir.rb test/integration/test_mbox.rb test/integration/test_sup-add.rb +test/integration/test_sup-recover-sources.rb test/integration/test_sup-sync-back-maildir.rb test/test_crypto.rb test/test_header_parsing.rb diff --git a/bin/sup-recover-sources b/bin/sup-recover-sources index 2d62c32c..29ac76fa 100755 --- a/bin/sup-recover-sources +++ b/bin/sup-recover-sources @@ -2,20 +2,12 @@ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) -require 'optparse' - -$opts = { - :unusual => false, - :archive => false, - :scan_num => 10, -} - - -OPTIONPARSERSUCKS = "\n" + " " * 38 -OptionParser.new do |opts| - opts.banner = <+ +require 'optimist' +require 'sup' +opts = Optimist::options do + version "sup-recover-sources (sup #{Redwood::VERSION})" + banner <* + +where * is one or more source URIs. + If the first N messages (--scan-num below) all have the same source_id in the index, the source will be added to sources.yaml. Otherwise, the distribution will be printed, and you will have to add it by hand. -The offset pointer into the sources will be set to the end of the source, -so you will have to run sup-import --rebuild for each new source after -doing this. - Options include: EOS + opt :unusual, "Mark sources as 'unusual'. They will not be polled automatically.", :default => false, :short => :none + opt :archive, "Mark sources as 'archive'. New messages from these sources will not appear in the inbox.", :default => false, :short => :none + opt :scan_num, "Number of messages to scan per source.", :default => 10, :short => :none +end - opts.on("--unusual", "Mark sources as 'unusual'. Only usual#{OPTIONPARSERSUCKS}sources will be polled by hand. Default:#{OPTIONPARSERSUCKS}#{$opts[:unusual]}.") { $opts[:unusual] = true } - - opts.on("--archive", "Mark sources as 'archive'. New messages#{OPTIONPARSERSUCKS}from these sources will not appear in#{OPTIONPARSERSUCKS}the inbox. Default: #{$opts[:archive]}.") { $opts[:archive] = true } - - opts.on("--scan-num N", Integer, "Number of messages to scan per source.#{OPTIONPARSERSUCKS}Default: #{$opts[:scan_num]}.") do |n| - $opts[:scan_num] = n - end - - opts.on_tail("-h", "--help", "Show this message") do - puts opts - exit - end -end.parse(ARGV) +Optimist::die "nothing to do: no sources given" if ARGV.empty? -require "sup" Redwood::start -puts "loading index..." index = Redwood::Index.init index.load -puts "loaded index of #{index.size} messages" - -ARGV.each do |fn| - next if Redwood::SourceManager.source_for fn - ## TODO: merge this code with the same snippet in import - source = Redwood::MBox.new(fn, nil, !$opts[:unusual], $opts[:archive]) - - source_ids = Hash.new 0 - count = 0 - source.each do |offset, labels| - m = Redwood::Message.new :source => source, :source_info => offset - m.load_from_source! - source_id = Redwood::SourceManager.source_for_id m.id - next unless source_id - source_ids[source_id] += 1 - count += 1 - break if count == $opts[:scan_num] - end - - if source_ids.size == 1 - id = source_ids.keys.first.to_i - puts "assigned #{source} to #{source_ids.keys.first}" - source.id = id - Redwood::SourceManager.add_source source - else - puts ">> unable to determine #{source}: #{source_ids.inspect}" +begin + ARGV.each do |uri| + next if Redwood::SourceManager.source_for uri + + source = + case uri + when /^maildir:/ + Redwood::Maildir.new uri, !opts[:unusual], opts[:archive], false, nil, [] + when /^mbox:/ + Redwood::MBox.new uri, !opts[:unusual], opts[:archive], nil, [] + else + Optimist::die "Sources must be specified with a maildir:// or mbox:// URI" + end + + source_ids = Hash.new 0 + count = 0 + source.each_message do |source_info| + location = Redwood::Location.new(source, source_info) + debug "checking message from #{source_info}" + m = Redwood::Message.new :locations => [location] + m.load_from_source! + indexed_source_ids = index.locations_for_id(m.id).select { |s, i| i == source_info }.map { |s, i| s } + if indexed_source_ids.empty? + debug "not found in index" + next + end + debug "found with source ids #{indexed_source_ids.inspect}" + indexed_source_ids.each { |id| source_ids[id] += 1 } + count += 1 + break if count == opts[:scan_num] + end + + if source_ids.size == 1 + id = source_ids.keys.first.to_i + puts "assigned #{source} to #{source_ids.keys.first}" + source.id = id + Redwood::SourceManager.add_source source + else + puts ">> unable to determine #{source}: #{source_ids.inspect}" + end end +ensure + index.save + Redwood::finish end - -index.save diff --git a/lib/sup/index.rb b/lib/sup/index.rb index f5ac72fe..27d8cbf5 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -327,10 +327,11 @@ EOS def optimize end - ## Return the id source of the source the message with the given message-id - ## was synced from - def source_for_id id - synchronize { get_entry(id)[:source_id] } + ## Return the locations the message with the given message-id + ## was synced from, as an array of [source_id, source_info] pairs. + def locations_for_id id + return [] unless (entry = synchronize { get_entry(id) }) + entry[:locations] end ## Yields each term in the index that starts with prefix diff --git a/lib/sup/maildir.rb b/lib/sup/maildir.rb index ac61f577..bdf1dc8e 100644 --- a/lib/sup/maildir.rb +++ b/lib/sup/maildir.rb @@ -180,6 +180,16 @@ class Maildir < Source nil end + def each_message + %w(cur new).each do |d| + subdir = File.join @dir, d + Dir.open(subdir).each do |f| + next if File.directory? File.join(subdir, f) + yield File.join(d, f) + end + end + end + def labels? id maildir_labels id end diff --git a/lib/sup/mbox.rb b/lib/sup/mbox.rb index 471b89a6..38d328d9 100644 --- a/lib/sup/mbox.rb +++ b/lib/sup/mbox.rb @@ -163,6 +163,22 @@ class MBox < Source end end + def each_message &b + offsets = [] + @mutex.synchronize do + ensure_open + offset = 0 + end_offset = File.size @f + @f.seek offset + while offset < end_offset + offsets << offset + nil while line = @f.gets and not MBox::is_break_line? line + offset = @f.tell + end + end + offsets.each(&b) + end + def next_offset offset @mutex.synchronize do ensure_open diff --git a/lib/sup/source.rb b/lib/sup/source.rb index 56d2e7d7..96e22113 100644 --- a/lib/sup/source.rb +++ b/lib/sup/source.rb @@ -98,6 +98,12 @@ class Source unimplemented end + ## Yields each message info in this source. + ## Slow path only for sup-recover-sources. The normal path is poll. + def each_message + unimplemented + end + def valid? info true end diff --git a/test/integration/test_sup-recover-sources.rb b/test/integration/test_sup-recover-sources.rb new file mode 100644 index 00000000..54fef719 --- /dev/null +++ b/test/integration/test_sup-recover-sources.rb @@ -0,0 +1,68 @@ +require "test_helper" +require "sup" +require "yaml" + +class TestSupRecoverSources < Minitest::Test + def setup + @path = Dir.mktmpdir + + @maildir = File.join @path, "test_maildir" + Dir.mkdir @maildir + %w[cur new tmp].each do |subdir| + Dir.mkdir (File.join @maildir, subdir) + end + msg_path = File.join @maildir, "new", "123.hostname" + FileUtils.copy_file fixture_path("simple-message.eml"), msg_path + + @mbox = File.join @path, "test_mbox" + File.open(@mbox, "wb") do |f| + f.puts "From test@test #{Time.now.asctime}" + f.puts File.read(fixture_path("simple-message.eml")) + end + + _out, _err = capture_subprocess_io do + assert system({"SUP_BASE" => @path}, "bin/sup-add", "maildir://#{@maildir}") + assert system({"SUP_BASE" => @path}, "bin/sup-add", "mbox://#{@mbox}") + assert system({"SUP_BASE" => @path}, "bin/sup-sync") + end + end + + def teardown + FileUtils.rm_r @path + end + + def test_recover_sources + File.delete File.join(@path, "sources.yaml") + source_uris = ["maildir://#{@maildir}", "mbox://#{@mbox}"] + out, _err = capture_subprocess_io do + assert system({"SUP_BASE" => @path}, "bin/sup-recover-sources", *source_uris) + end + assert_equal "assigned #{source_uris[0]} to 1\n", out.lines[0] + assert_equal "assigned #{source_uris[1]} to 2\n", out.lines[1] + + sources_fn = File.join @path, "sources.yaml" + sources_yaml = + if YAML.respond_to?(:unsafe_load_file) # Ruby 3.1+ + YAML::unsafe_load_file sources_fn + else + YAML::load_file sources_fn + end + assert_equal 2, sources_yaml.length + assert_instance_of Redwood::Maildir, sources_yaml[0] + assert_equal source_uris[0], sources_yaml[0].uri + assert_equal 1, sources_yaml[0].id + assert_instance_of Redwood::MBox, sources_yaml[1] + assert_equal source_uris[1], sources_yaml[1].uri + assert_equal 2, sources_yaml[1].id + end + + def test_noop + ## All sources are already defined in sources.yaml so there is nothing to recover. + ## It should do nothing. + source_uris = ["maildir://#{@maildir}", "mbox://#{@mbox}"] + out, _err = capture_subprocess_io do + assert system({"SUP_BASE" => @path}, "bin/sup-recover-sources", *source_uris) + end + assert out.empty? + end +end