2 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <http://www.gnu.org/licenses/agpl-3.0.txt>
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
8 usage: public-inbox-convert [options] OLD NEW
10 convert v1 format inboxes to v2
14 --no-index do not index after conversion
15 --jobs=NUM set shards (NUM=0)
16 --verbose | -v increase verbosity (may be repeated)
18 index options (see public-inbox-index(1) man page for full description):
20 --no-fsync speed up indexing, risk corruption on power outage
21 -L LEVEL `basic', `medium', or `full' (default: full)
22 --compact | -c run public-inbox-compact(1) after indexing
23 --sequential-shard index Xapian shards sequentially for slow storage
24 --batch-size=BYTES flush changes to OS after a given number of bytes
25 --max-size=BYTES do not index messages larger than the given size
27 See public-inbox-convert(1) man page for full documentation.
33 quiet => -1, compact => 0, maxsize => undef, fsync => 1,
34 reindex => 1, # we always reindex
36 GetOptions($opt, qw(jobs|j=i index! help|h),
38 qw(verbose|v+ rethread compact|c+ fsync|sync!
39 indexlevel|index-level|L=s max_size|max-size=s
40 batch_size|batch-size=s
41 sequential_shard|sequential-shard|seq-shard
43 if ($opt->{help}) { print $help; exit 0 };
44 my $old_dir = shift(@ARGV) // '';
45 my $new_dir = shift(@ARGV) // '';
46 die $help if (scalar(@ARGV) || $new_dir eq '' || $old_dir eq '');
47 die "$new_dir exists\n" if -d $new_dir;
48 die "$old_dir not a directory\n" unless -d $old_dir;
51 Cwd->import('abs_path');
52 require PublicInbox::Config;
53 require PublicInbox::InboxWritable;
55 my $abs = abs_path($old_dir);
56 die "failed to resolve $old_dir: $!\n" if (!defined($abs));
58 my $cfg = PublicInbox::Config->new;
60 $cfg->each_inbox(sub {
61 $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
64 $old = PublicInbox::InboxWritable->new($old);
66 warn "W: $old_dir not configured in " .
67 PublicInbox::Config::default_file() . "\n";
68 $old = PublicInbox::InboxWritable->new({
71 -primary_address => 'old@example.com',
72 address => [ 'old@example.com' ],
75 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
78 require PublicInbox::Admin;
79 my $detected = PublicInbox::Admin::detect_indexlevel($old);
80 $old->{indexlevel} //= $detected;
82 if ($opt->{'index'}) {
84 PublicInbox::Admin::scan_ibx_modules($mods, $old);
85 PublicInbox::Admin::require_or_die(keys %$mods);
86 PublicInbox::Admin::progress_prepare($opt);
87 $env = PublicInbox::Admin::index_prepare($opt, $cfg);
89 local %ENV = (%$env, %ENV) if $env;
91 $new->{inboxdir} = File::Spec->canonpath($new_dir);
93 $new = PublicInbox::InboxWritable->new($new, { nproc => $opt->{jobs} });
94 $new->{-no_fsync} = 1 if !$opt->{fsync};
98 sub link_or_copy ($$) {
100 link($src, $dst) and return;
101 $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
102 require File::Copy; # preserves permissions:
103 File::Copy::cp($src, $dst) or die "cp $src, $dst failed: $!\n";
106 $old->with_umask(sub {
107 my $old_cfg = "$old->{inboxdir}/config";
108 local $ENV{GIT_CONFIG} = $old_cfg;
109 my $new_cfg = "$new->{inboxdir}/all.git/config";
110 $v2w = $new->importer(1);
111 $v2w->init_inbox(delete $opt->{jobs});
113 link_or_copy($old_cfg, $new_cfg);
114 if (my $alt = $new->{altid}) {
115 require PublicInbox::AltId;
116 foreach my $i (0..$#$alt) {
117 my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
118 $src = $src->mm_alt or next;
119 $src = $src->{dbh}->sqlite_db_filename;
120 my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
121 $dst->mm_alt->{dbh}->sqlite_backup_from_file($src);
124 my $desc = "$old->{inboxdir}/description";
125 link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
126 my $clone = "$old->{inboxdir}/cloneurl";
129 $clone may not be valid after migrating to v2, not copying
134 my $head = $old->{ref_head} || 'HEAD';
135 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
136 $v2w->idx_init($opt);
137 my $im = $v2w->importer;
138 my ($r, $w) = $im->gfi_start;
143 if ($_ eq "blob\n") {
145 } elsif (/^commit /) {
147 } elsif (/^data ([0-9]+)/) {
149 print $w $_ or $im->wfail;
151 my $n = read($rd, my $tmp, $len) or die "read: $!";
152 warn "$n != $len\n" if $n != $len;
154 print $w $tmp or $im->wfail;
157 } elsif ($state eq 'commit') {
158 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
159 my ($mark, $path) = ($1, $2);
161 if ($last && $last ne 'm') {
162 print $w "D $last\n" or $im->wfail;
164 print $w "M 100644 :$mark m\n" or $im->wfail;
168 if (m{^D (${h}{2}/${h}{38})}o) {
169 my $mark = delete $D{$1};
170 defined $mark or die "undeleted path: $1\n";
171 if ($last && $last ne 'd') {
172 print $w "D $last\n" or $im->wfail;
174 print $w "M 100644 :$mark d\n" or $im->wfail;
179 last if $_ eq "done\n";
180 print $w $_ or $im->wfail;
182 close $rd or die "close fast-export: $!\n";
183 waitpid($pid, 0) or die "waitpid failed: $!\n";
184 $? == 0 or die "fast-export failed: $?\n";
185 $r = $w = undef; # v2w->done does the actual close and error checking
187 if (my $old_mm = $old->mm) {
189 $old_mm = $old_mm->{dbh}->sqlite_db_filename;
191 # we want to trigger a reindex, not a from scratch index if
192 # we're reusing the msgmap from an existing v1 installation.
193 $v2w->idx_init($opt);
194 $v2w->{mm}->{dbh}->sqlite_backup_from_file($old_mm);
196 my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
197 chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
198 $v2w->last_epoch_commit(0, $cmt);
200 $v2w->index_sync($opt) if delete $opt->{'index'};