]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
convert: support new -index options
[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 warnings;
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 use PublicInbox::InboxWritable;
8 use PublicInbox::Config;
9 use PublicInbox::Admin;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Git;
12 use PublicInbox::Spawn qw(spawn);
13 use Cwd 'abs_path';
14 use File::Copy 'cp'; # preserves permissions:
15 my $usage = 'Usage: public-inbox-convert [options] OLD NEW';
16 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
17 usage: $usage
18
19   convert v1 format inboxes to v2
20
21 options:
22
23   --no-index          do not index after conversion
24   --jobs=NUM          set shards (NUM=0)
25   --verbose | -v      increase verbosity (may be repeated)
26   --help | -?         show this help
27
28 index options (see public-inbox-index(1) manpage for full description):
29
30   --no-fsync          speed up indexing, risk corruption on power outage
31   -L LEVEL            `basic', `medium', or `full' (default: full)
32   --compact | -c      run public-inbox-compact(1) after indexing
33   --sequential-shard  index Xapian shards sequentially for slow storage
34   --batch-size=BYTES  flush changes to OS after a given number of bytes
35   --max-size=BYTES    do not index messages larger than the given size
36
37 See public-inbox-convert(1) man page for full documentation.
38 EOF
39
40 my $opt = {
41         index => 1,
42         # index defaults:
43         quiet => -1, compact => 0, maxsize => undef, fsync => 1,
44         reindex => 1, # we always reindex
45 };
46 GetOptions($opt, qw(jobs|j=i index! help|?),
47                 # index options
48                 qw(verbose|v+ rethread compact|c+ fsync|sync!
49                 indexlevel|index-level|L=s max_size|max-size=s
50                 batch_size|batch-size=s
51                 sequential_shard|sequential-shard|seq-shard
52                 )) or die <<EOF;
53 bad command-line args\n$usage
54 EOF
55 if ($opt->{help}) { print $help; exit 0 };
56 my $old_dir = shift(@ARGV) or die $usage;
57 my $new_dir = shift(@ARGV) or die $usage;
58 die "$new_dir exists\n" if -d $new_dir;
59 die "$old_dir not a directory\n" unless -d $old_dir;
60 my $cfg = PublicInbox::Config->new;
61 $old_dir = abs_path($old_dir);
62 my $old;
63 $cfg->each_inbox(sub {
64         $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
65 });
66 unless ($old) {
67         warn "W: $old_dir not configured in " .
68                 PublicInbox::Config::default_file() . "\n";
69         $old = {
70                 inboxdir => $old_dir,
71                 name => 'ignored',
72                 address => [ 'old@example.com' ],
73         };
74         $old = PublicInbox::Inbox->new($old);
75 }
76 $old = PublicInbox::InboxWritable->new($old);
77 if ($old->version >= 2) {
78         die "Only conversion from v1 inboxes is supported\n";
79 }
80
81 $old->{indexlevel} //= PublicInbox::Admin::detect_indexlevel($old);
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         cp($src, $dst) or die "cp $src, $dst failed: $!\n";
104 }
105
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 = PublicInbox::V2Writable->new($new, 1);
111         $v2w->init_inbox(delete $opt->{jobs});
112         unlink $new_cfg;
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->mm_alt or next;
119                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
120                         $dst = $dst->{filename};
121                         $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
122                 }
123         }
124         my $desc = "$old->{inboxdir}/description";
125         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
126         my $clone = "$old->{inboxdir}/cloneurl";
127         if (-e $clone) {
128                 warn <<"";
129 $clone may not be valid after migrating to v2, not copying
130
131         }
132 });
133 my $state = '';
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;
139 my $h = '[0-9a-f]';
140 my %D;
141 my $last;
142 while (<$rd>) {
143         if ($_ eq "blob\n") {
144                 $state = 'blob';
145         } elsif (/^commit /) {
146                 $state = 'commit';
147         } elsif (/^data ([0-9]+)/) {
148                 my $len = $1;
149                 print $w $_ or $im->wfail;
150                 while ($len) {
151                         my $n = read($rd, my $tmp, $len) or die "read: $!";
152                         warn "$n != $len\n" if $n != $len;
153                         $len -= $n;
154                         print $w $tmp or $im->wfail;
155                 }
156                 next;
157         } elsif ($state eq 'commit') {
158                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
159                         my ($mark, $path) = ($1, $2);
160                         $D{$path} = $mark;
161                         if ($last && $last ne 'm') {
162                                 print $w "D $last\n" or $im->wfail;
163                         }
164                         print $w "M 100644 :$mark m\n" or $im->wfail;
165                         $last = 'm';
166                         next;
167                 }
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;
173                         }
174                         print $w "M 100644 :$mark d\n" or $im->wfail;
175                         $last = 'd';
176                         next;
177                 }
178         }
179         last if $_ eq "done\n";
180         print $w $_ or $im->wfail;
181 }
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
186 $v2w->done;
187 if (my $mm = $old->mm) {
188         $old->cleanup;
189         $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3");
190
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         my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
195         chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
196         $v2w->last_epoch_commit(0, $cmt);
197 }
198 $v2w->index_sync($opt) if delete $opt->{'index'};
199 $v2w->done;