commit 53f07beb4310b71784861d4d81db6425b4af33c7
parent f096837ab5e6e700092784117ada2b93bc6e5033
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 22 Nov 2026 18:03:06 +1100
bring back sup-recover-sources from the dead
Diffstat:
7 files changed, 164 insertions(+), 64 deletions(-)
diff --git 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
@@ -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 = <<EOS
-Usage: sup-recover-sources [options] <source>+
+require 'optimist'
+require 'sup'
+opts = Optimist::options do
+ version "sup-recover-sources (sup #{Redwood::VERSION})"
+ banner <<EOS
Rebuilds a lost sources.yaml file by reading messages from a list of
sources and determining, for each source, the most prevalent
'source_id' field of messages from that source in the index.
@@ -24,64 +16,70 @@ The only non-deterministic component to this is that if the same
message appears in multiple sources, those sources may be
mis-diagnosed by this program.
+Usage:
+ sup-recover-sources [options] <source>*
+
+where <source>* 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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