diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2022-07-12 13:14:25 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2022-07-12 13:41:50 -0700 |
commit | ecac9d421edf40894788d04d9f33be2b625e45f4 (patch) | |
tree | 73d3ddc402a0afb41e16b5bb7b519205558f5754 | |
parent | 6f51a6b4f4051f7728465d96c5328218f39d27f9 (diff) | |
download | dotfiles-ecac9d421edf40894788d04d9f33be2b625e45f4.tar.gz |
store Gnus/notmuch's copy of annexed mail in a single maildir's cur/
-rwxr-xr-x | bin/expand-annex-mboxes | 6 | ||||
-rw-r--r-- | perl5/Local/Homedir/Mail.pm | 48 | ||||
-rwxr-xr-x | scripts/mail/archive-fmail-to-annex | 7 |
3 files changed, 30 insertions, 31 deletions
diff --git a/bin/expand-annex-mboxes b/bin/expand-annex-mboxes index 7f3525bc..e12a574f 100755 --- a/bin/expand-annex-mboxes +++ b/bin/expand-annex-mboxes @@ -7,7 +7,7 @@ use lib "$ENV{HOME}/src/dotfiles/perl5"; use Fcntl qw(LOCK_EX LOCK_NB); use File::Path qw(make_path); -use File::Spec::Functions qw(catfile splitpath); +use File::Spec::Functions qw(splitpath); use Local::Homedir::Mail qw(expand_mbox); # CONFIG @@ -29,7 +29,5 @@ foreach my $mbox (glob "$mboxes/*.gz") { # $mbox might be a dangling symlink on this machine next unless -e $mbox; - my (undef, undef, $name) = splitpath($mbox); - $name =~ s/\.gz$//; - expand_mbox($mbox, catfile($expanded, $name)); + expand_mbox($mbox, $expanded); } diff --git a/perl5/Local/Homedir/Mail.pm b/perl5/Local/Homedir/Mail.pm index f34a21e9..6ee94046 100644 --- a/perl5/Local/Homedir/Mail.pm +++ b/perl5/Local/Homedir/Mail.pm @@ -20,7 +20,7 @@ use strict; use warnings; use constant THIRTYONE => 31 * 24 * 60 * 60; -use File::Spec::Functions qw(catfile); +use File::Spec::Functions qw(catfile splitpath); use File::Temp qw(tempdir); use File::Path qw(remove_tree); use IO::Compress::Gzip qw(gzip $GzipError); @@ -73,28 +73,26 @@ sub archive_to_mbox { $mgr->moveMessage($mbox, $message); } + $expanded->acceptMessages; $mgr->closeAllFolders; + open my $touch_fh, ">", + catfile($expanded_path, "." . (splitpath $mbox_path)[2]) . ".done"; gzip($mbox_path, "$mbox_path.gz") or die "gzip failed: $GzipError\n"; unlink $mbox_path if -e "$mbox_path.gz"; - make_maildir_readonly($expanded_path); } sub expand_mbox { - my ($source_path, $expanded_path) = @_; - my $lockfile = $expanded_path . ".lock"; - - # check whether we got halfway there, or we finished it - if (-e $lockfile) { - remove_tree($expanded_path); - unlink $lockfile; - } elsif (-e $expanded_path) { - return; - } + my ($source_path, $target) = @_; + (my $source_name = (splitpath $source_path)[2]) =~ s/\.gz$//; + my $expanded_path = catfile $target, ".$source_name"; + my $donefile = $expanded_path . ".done"; + + # Check whether we finished, or got partway there. + return if -e $donefile; + remove_tree($expanded_path) if -e $expanded_path; - # lock this one - open my $touch_fh, '>', $lockfile; - close $touch_fh; + -e or mkdir for $target, map catfile($target, $_), "cur", "new", "tmp"; # unzip it to (what is hopefully a) tmpfs, since Mail::Box can # only accept a path to an unzipped mbox @@ -123,21 +121,23 @@ sub expand_mbox { $mgr->copyMessage($expanded, $message); } $source->close(write => "NEVER"); + $expanded->acceptMessages; $expanded->close; - make_maildir_readonly($expanded_path); - # mark as done - unlink $lockfile; + # Now we move all the files the old fashioned way, so that it's fast. + # It's safe because the filenames begin with a timestamp, so there really + # oughtn't be any collisions, and in any case this whole thing is a cache. + opendir my $dirh, catfile $expanded_path, "cur"; + while (readdir $dirh) { + my $s = catfile $expanded_path, "cur", $_; + -f $s and rename $s, catfile $target, "cur", $_; + } + open my $touch_fh, ">", $donefile; # nuke the tempdir now to avoid running out of ramdisk if we're # expanding a lot of mboxes remove_tree($dir); -} - -sub make_maildir_readonly { - chmod 0500, catfile($_[0], "cur"), catfile($_[0], "new"), - catfile($_[0], "tmp"); - chmod 0400, glob "$_[0]/cur/* $_[0]/new/* $_[0]/tmp/*"; + remove_tree($expanded_path); } 1; diff --git a/scripts/mail/archive-fmail-to-annex b/scripts/mail/archive-fmail-to-annex index 1f647387..d9cfac17 100755 --- a/scripts/mail/archive-fmail-to-annex +++ b/scripts/mail/archive-fmail-to-annex @@ -35,9 +35,10 @@ my @gzipped_mboxes; foreach my $name (keys %names) { my $target = $names{$name} . $suffix; archive_to_mbox( - catfile($maildirs, $name), - catfile($mboxes, $target), - catfile($expanded_mboxes, $target)); + catfile($maildirs, $name), + catfile($mboxes, $target), + $expanded_mboxes + ); push @gzipped_mboxes, $target . ".gz"; } |