]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Import.pm
import: enable locking under v2
[public-inbox.git] / lib / PublicInbox / Import.pm
index 12df7d598fc71d38088d4aeaebd0ed6d8ed3e37d..ca30ac444ca4ef7af11f92a602f0837708493cc5 100644 (file)
@@ -9,9 +9,10 @@ use strict;
 use warnings;
 use Fcntl qw(:flock :DEFAULT);
 use PublicInbox::Spawn qw(spawn);
-use PublicInbox::MID qw(mid_mime mid2path);
+use PublicInbox::MID qw(mids mid_mime mid2path);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp);
+use PublicInbox::ContentId qw(content_digest);
 
 sub new {
        my ($class, $git, $name, $email, $ibx) = @_;
@@ -28,7 +29,7 @@ sub new {
                ref => $ref,
                inbox => $ibx,
                path_type => '2/38', # or 'v2'
-               ssoma_lock => 1, # disable for v2
+               lock_path => "$git->{git_dir}/ssoma.lock", # v2 changes this
                bytes_added => 0,
        }, $class
 }
@@ -45,13 +46,12 @@ sub gfi_start {
        my $git = $self->{git};
        my $git_dir = $git->{git_dir};
 
-       my $lockfh;
-       if ($self->{ssoma_lock}) {
-               my $lockpath = "$git_dir/ssoma.lock";
-               sysopen($lockfh, $lockpath, O_WRONLY|O_CREAT) or
-                       die "failed to open lock $lockpath: $!";
+       if (my $lock_path = $self->{lock_path}) {
+               sysopen(my $lockfh, $lock_path, O_WRONLY|O_CREAT) or
+                       die "failed to open lock $lock_path: $!";
                # wait for other processes to be done
                flock($lockfh, LOCK_EX) or die "lock failed: $!\n";
+               $self->{lockfh} = $lockfh;
        }
 
        local $/ = "\n";
@@ -65,7 +65,6 @@ sub gfi_start {
        $out_w->autoflush(1);
        $self->{in} = $in_r;
        $self->{out} = $out_w;
-       $self->{lockfh} = $lockfh;
        $self->{pid} = $pid;
        $self->{nchg} = 0;
        binmode $out_w, ':raw' or die "binmode :raw failed: $!";
@@ -308,7 +307,12 @@ sub add {
 
        my $path;
        if ($path_type eq '2/38') {
-               $path = mid2path(mid_mime($mime));
+               my $mids = mids($mime->header_obj);
+               if (!scalar(@$mids)) {
+                       my $dig = content_digest($mime);
+                       @$mids = (digest2mid($dig));
+               }
+               $path = mid2path($mids->[0]);
        } else { # v2 layout, one file:
                $path = 'm';
        }
@@ -380,7 +384,7 @@ sub done {
 
        _update_git_info($self, 1) if delete $self->{nchg};
 
-       $self->{ssoma_lock} or return;
+       $self->{lock_path} or return;
        my $lockfh = delete $self->{lockfh} or die "BUG: not locked: $!";
        flock($lockfh, LOCK_UN) or die "unlock failed: $!";
        close $lockfh or die "close lock failed: $!";
@@ -393,6 +397,20 @@ sub atfork_child {
        }
 }
 
+sub digest2mid ($) {
+       my ($dig) = @_;
+       my $b64 = $dig->clone->b64digest;
+       # Make our own URLs nicer:
+       # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648
+       $b64 =~ tr!+/=!-_!d;
+
+       # We can make this more meaningful with a date prefix or other things,
+       # but this is only needed for crap that fails to generate a Message-ID
+       # or reuses one.  In other words, it's usually spammers who hit this
+       # so they don't deserve nice Message-IDs :P
+       $b64 . '@localhost';
+}
+
 1;
 __END__
 =pod