]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
init+index: support --skip-docdata for Xapian
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
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>
4 use strict;
5 use v5.10.1;
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 my $usage = 'Usage: public-inbox-convert [options] OLD NEW';
8 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
9 usage: $usage
10
11   convert v1 format inboxes to v2
12
13 options:
14
15   --no-index          do not index after conversion
16   --jobs=NUM          set shards (NUM=0)
17   --verbose | -v      increase verbosity (may be repeated)
18   --help | -?         show this help
19
20 index options (see public-inbox-index(1) manpage for full description):
21
22   --no-fsync          speed up indexing, risk corruption on power outage
23   -L LEVEL            `basic', `medium', or `full' (default: full)
24   --compact | -c      run public-inbox-compact(1) after indexing
25   --sequential-shard  index Xapian shards sequentially for slow storage
26   --batch-size=BYTES  flush changes to OS after a given number of bytes
27   --max-size=BYTES    do not index messages larger than the given size
28
29 See public-inbox-convert(1) man page for full documentation.
30 EOF
31
32 my $opt = {
33         index => 1,
34         # index defaults:
35         quiet => -1, compact => 0, maxsize => undef, fsync => 1,
36         reindex => 1, # we always reindex
37 };
38 GetOptions($opt, qw(jobs|j=i index! help|?),
39                 # index options
40                 qw(verbose|v+ rethread compact|c+ fsync|sync!
41                 indexlevel|index-level|L=s max_size|max-size=s
42                 batch_size|batch-size=s
43                 sequential_shard|sequential-shard|seq-shard
44                 )) or die <<EOF;
45 bad command-line args\n$usage
46 EOF
47 if ($opt->{help}) { print $help; exit 0 };
48 my $old_dir = shift(@ARGV) // '';
49 my $new_dir = shift(@ARGV) // '';
50 die $usage if (scalar(@ARGV) || $new_dir eq '' || $old_dir eq '');
51 die "$new_dir exists\n" if -d $new_dir;
52 die "$old_dir not a directory\n" unless -d $old_dir;
53
54 require Cwd;
55 Cwd->import('abs_path');
56 require PublicInbox::Config;
57 require PublicInbox::InboxWritable;
58
59 $old_dir = abs_path($old_dir);
60 my $cfg = PublicInbox::Config->new;
61 my $old;
62 $cfg->each_inbox(sub {
63         $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
64 });
65 if ($old) {
66         $old = PublicInbox::InboxWritable->new($old);
67 } else {
68         warn "W: $old_dir not configured in " .
69                 PublicInbox::Config::default_file() . "\n";
70         $old = PublicInbox::InboxWritable->new({
71                 inboxdir => $old_dir,
72                 name => 'ignored',
73                 -primary_address => 'old@example.com',
74                 address => [ 'old@example.com' ],
75         });
76 }
77 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
78
79 require PublicInbox::Admin;
80 my $detected = PublicInbox::Admin::detect_indexlevel($old);
81 $old->{indexlevel} //= $detected;
82 my $env;
83 if ($opt->{'index'}) {
84         my $mods = {};
85         PublicInbox::Admin::scan_ibx_modules($mods, $old);
86         PublicInbox::Admin::require_or_die(keys %$mods);
87         PublicInbox::Admin::progress_prepare($opt);
88         $env = PublicInbox::Admin::index_prepare($opt, $cfg);
89 }
90 local %ENV = (%$env, %ENV) if $env;
91 my $new = { %$old };
92 $new->{inboxdir} = abs_path($new_dir);
93 $new->{version} = 2;
94 $new = PublicInbox::InboxWritable->new($new, { nproc => $opt->{jobs} });
95 $new->{-no_fsync} = 1 if !$opt->{fsync};
96 my $v2w;
97 $old->umask_prepare;
98
99 sub link_or_copy ($$) {
100         my ($src, $dst) = @_;
101         link($src, $dst) and return;
102         $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
103         require File::Copy; # preserves permissions:
104         File::Copy::cp($src, $dst) or die "cp $src, $dst failed: $!\n";
105 }
106
107 $old->with_umask(sub {
108         my $old_cfg = "$old->{inboxdir}/config";
109         local $ENV{GIT_CONFIG} = $old_cfg;
110         my $new_cfg = "$new->{inboxdir}/all.git/config";
111         $v2w = $new->importer(1);
112         $v2w->init_inbox(delete $opt->{jobs});
113         unlink $new_cfg;
114         link_or_copy($old_cfg, $new_cfg);
115         if (my $alt = $new->{altid}) {
116                 require PublicInbox::AltId;
117                 foreach my $i (0..$#$alt) {
118                         my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
119                         $src = $src->mm_alt or next;
120                         $src = $src->{dbh}->sqlite_db_filename;
121                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
122                         $dst->mm_alt->{dbh}->sqlite_backup_from_file($src);
123                 }
124         }
125         my $desc = "$old->{inboxdir}/description";
126         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
127         my $clone = "$old->{inboxdir}/cloneurl";
128         if (-e $clone) {
129                 warn <<"";
130 $clone may not be valid after migrating to v2, not copying
131
132         }
133 });
134 my $state = '';
135 my $head = $old->{ref_head} || 'HEAD';
136 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
137 $v2w->idx_init($opt);
138 my $im = $v2w->importer;
139 my ($r, $w) = $im->gfi_start;
140 my $h = '[0-9a-f]';
141 my %D;
142 my $last;
143 while (<$rd>) {
144         if ($_ eq "blob\n") {
145                 $state = 'blob';
146         } elsif (/^commit /) {
147                 $state = 'commit';
148         } elsif (/^data ([0-9]+)/) {
149                 my $len = $1;
150                 print $w $_ or $im->wfail;
151                 while ($len) {
152                         my $n = read($rd, my $tmp, $len) or die "read: $!";
153                         warn "$n != $len\n" if $n != $len;
154                         $len -= $n;
155                         print $w $tmp or $im->wfail;
156                 }
157                 next;
158         } elsif ($state eq 'commit') {
159                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
160                         my ($mark, $path) = ($1, $2);
161                         $D{$path} = $mark;
162                         if ($last && $last ne 'm') {
163                                 print $w "D $last\n" or $im->wfail;
164                         }
165                         print $w "M 100644 :$mark m\n" or $im->wfail;
166                         $last = 'm';
167                         next;
168                 }
169                 if (m{^D (${h}{2}/${h}{38})}o) {
170                         my $mark = delete $D{$1};
171                         defined $mark or die "undeleted path: $1\n";
172                         if ($last && $last ne 'd') {
173                                 print $w "D $last\n" or $im->wfail;
174                         }
175                         print $w "M 100644 :$mark d\n" or $im->wfail;
176                         $last = 'd';
177                         next;
178                 }
179         }
180         last if $_ eq "done\n";
181         print $w $_ or $im->wfail;
182 }
183 close $rd or die "close fast-export: $!\n";
184 waitpid($pid, 0) or die "waitpid failed: $!\n";
185 $? == 0 or die "fast-export failed: $?\n";
186 $r = $w = undef; # v2w->done does the actual close and error checking
187 $v2w->done;
188 if (my $old_mm = $old->mm) {
189         $old->cleanup;
190         $old_mm = $old_mm->{dbh}->sqlite_db_filename;
191
192         # we want to trigger a reindex, not a from scratch index if
193         # we're reusing the msgmap from an existing v1 installation.
194         $v2w->idx_init($opt);
195         $v2w->{mm}->{dbh}->sqlite_backup_from_file($old_mm);
196
197         my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
198         chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
199         $v2w->last_epoch_commit(0, $cmt);
200 }
201 $v2w->index_sync($opt) if delete $opt->{'index'};
202 $v2w->done;