]> git.cworth.org Git - sup/commitdiff
make sup-sync's --start-at actually useful
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 4 May 2009 12:29:54 +0000 (05:29 -0700)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 4 May 2009 12:29:54 +0000 (05:29 -0700)
Using --start-at with sup-sync will now seek forward to the next valid message
if given an offset that's not on a message boundary. This makes it possible to
use that option without going through the laborious process of finding the
exact message boundary.

Also die unless --start-at is used with only one source, since using it across
multiple sources is almost definitely an error. (And if that's really what you
want, you can just call sup-sync multiple times.)

bin/sup-sync
lib/sup/mbox/loader.rb

index 91710d47495ab263a69e03ec709657d1ee8b66ae..4d7a3d3a549fe523d6190c28fba3a5020cdb1e12 100644 (file)
@@ -122,7 +122,9 @@ begin
 
   unless target == :new
     if opts[:start_at]
-      sources.each { |s| s.seek_to! opts[:start_at] }
+      Trollop::die :start_at, "can only be used on one source" unless sources.size == 1
+      sources.first.seek_to! opts[:start_at]
+      sources.first.correct_offset! if sources.first.respond_to?(:correct_offset!)
     else
       sources.each { |s| s.reset! }
     end
@@ -200,9 +202,7 @@ begin
   ## API.
   ##
   ## TODO: move this to Index, i suppose.
-
-
-  if target == :all || target == :changed
+  if (target == :all || target == :changed) && !opts[:start_at]
     $stderr.puts "Deleting missing messages from the index..."
     num_del, num_scanned = 0, 0
     sources.each do |source|
index 7fe912900e16d477fc7d690873d92e9c623f48c2..65d0bd102a2612339770945965fc15d71050901d 100644 (file)
@@ -81,6 +81,18 @@ class Loader < Source
     end
   end
 
+  ## scan forward until we're at the valid start of a message
+  def correct_offset!
+    @mutex.synchronize do
+      @f.seek cur_offset
+      string = ""
+      until @f.eof? || (l = @f.gets) =~ BREAK_RE
+        string << l
+      end
+      self.cur_offset += string.length
+    end
+  end
+
   def raw_header offset
     ret = ""
     @mutex.synchronize do