2 # Copyright (C) 2018-2021 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;
50 require PublicInbox::Admin;
51 require PublicInbox::Config;
52 require PublicInbox::InboxWritable;
54 my $cfg = PublicInbox::Config->new;
55 my @old = PublicInbox::Admin::resolve_inboxes([$old_dir], undef, $cfg);
56 @old > 1 and die "BUG: resolved several inboxes from $old_dir:\n",
57 map { "\t$_->{inboxdir}\n" } @old;
58 my $old = PublicInbox::InboxWritable->new($old[0]);
59 if (delete $old->{-unconfigured}) {
60 warn "W: $old_dir not configured in " .
61 PublicInbox::Config::default_file() . "\n";
63 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
65 require PublicInbox::Admin;
66 my $detected = PublicInbox::Admin::detect_indexlevel($old);
67 $old->{indexlevel} //= $detected;
69 if ($opt->{'index'}) {
71 PublicInbox::Admin::scan_ibx_modules($mods, $old);
72 PublicInbox::Admin::require_or_die(keys %$mods);
73 PublicInbox::Admin::progress_prepare($opt);
74 $env = PublicInbox::Admin::index_prepare($opt, $cfg);
76 local %ENV = (%$env, %ENV) if $env;
78 $new->{inboxdir} = $cfg->rel2abs_collapsed($new_dir);
80 $new = PublicInbox::InboxWritable->new($new, { nproc => $opt->{jobs} });
81 $new->{-no_fsync} = 1 if !$opt->{fsync};
84 sub link_or_copy ($$) {
86 link($src, $dst) and return;
87 $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
88 require File::Copy; # preserves permissions:
89 File::Copy::cp($src, $dst) or die "cp $src, $dst failed: $!\n";
92 $old->with_umask(sub {
93 my $old_cfg = "$old->{inboxdir}/config";
94 local $ENV{GIT_CONFIG} = $old_cfg;
95 my $new_cfg = "$new->{inboxdir}/all.git/config";
96 $v2w = $new->importer(1);
97 $v2w->init_inbox(delete $opt->{jobs});
99 link_or_copy($old_cfg, $new_cfg);
100 if (my $alt = $new->{altid}) {
101 require PublicInbox::AltId;
102 foreach my $i (0..$#$alt) {
103 my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
104 $src = $src->mm_alt or next;
105 $src = $src->{dbh}->sqlite_db_filename;
106 my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
107 $dst->mm_alt->{dbh}->sqlite_backup_from_file($src);
110 my $desc = "$old->{inboxdir}/description";
111 link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
112 my $clone = "$old->{inboxdir}/cloneurl";
115 $clone may not be valid after migrating to v2, not copying
120 my $head = $old->{ref_head} || 'HEAD';
121 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
122 $v2w->idx_init($opt);
123 my $im = $v2w->importer;
124 my ($r, $w) = $im->gfi_start;
129 if ($_ eq "blob\n") {
131 } elsif (/^commit /) {
133 } elsif (/^data ([0-9]+)/) {
135 print $w $_ or $im->wfail;
137 my $n = read($rd, my $tmp, $len) or die "read: $!";
138 warn "$n != $len\n" if $n != $len;
140 print $w $tmp or $im->wfail;
143 } elsif ($state eq 'commit') {
144 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
145 my ($mark, $path) = ($1, $2);
147 if ($last && $last ne 'm') {
148 print $w "D $last\n" or $im->wfail;
150 print $w "M 100644 :$mark m\n" or $im->wfail;
154 if (m{^D (${h}{2}/${h}{38})}o) {
155 my $mark = delete $D{$1};
156 defined $mark or die "undeleted path: $1\n";
157 if ($last && $last ne 'd') {
158 print $w "D $last\n" or $im->wfail;
160 print $w "M 100644 :$mark d\n" or $im->wfail;
165 last if $_ eq "done\n";
166 print $w $_ or $im->wfail;
168 close $rd or die "close fast-export: $!\n";
169 waitpid($pid, 0) or die "waitpid failed: $!\n";
170 $? == 0 or die "fast-export failed: $?\n";
171 $r = $w = undef; # v2w->done does the actual close and error checking
173 if (my $old_mm = $old->mm) {
175 $old_mm = $old_mm->{dbh}->sqlite_db_filename;
177 # we want to trigger a reindex, not a from scratch index if
178 # we're reusing the msgmap from an existing v1 installation.
179 $v2w->idx_init($opt);
180 $v2w->{mm}->{dbh}->sqlite_backup_from_file($old_mm);
182 my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
183 chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
184 $v2w->last_epoch_commit(0, $cmt);
186 $v2w->index_sync($opt) if delete $opt->{'index'};