+sub gen_srcdump_link_html ($$$$) {
+ my ($c,$r,$anchor,$specval) = @_;
+ my %params = ($r->{S}{srcdump_param_name} => [ $specval ]);
+ return '<a href="'.escapeHTML($r->url_with_query_params(\%params)).'">'.
+ $anchor."</a>";
+}
+sub gen_plain_licence_link_html ($$) {
+ my ($c,$r) = @_;
+ gen_srcdump_link_html($c,$r, 'GNU Affero GPL', 'licence');
+}
+sub gen_plain_source_link_html ($$) {
+ my ($c,$r) = @_;
+ gen_srcdump_link_html($c,$r, 'Source available', 'source');
+}
+
+sub gen_plain_footer_html ($$) {
+ my ($c,$r) = @_;
+ return ('<hr><address>',
+ ("Powered by Free / Libre / Open Source Software".
+ " according to the ".$r->_ch('gen_licence_link_html')."."),
+ $r->_ch('gen_source_link_html').".",
+ '</address>');
+}
+
+#---------- licence and source code ----------
+
+sub srcdump_dump_thing ($$$) {
+ my ($c,$r, $thing, $outfn) = @_;
+ die if $thing =~ m/\W/ || $thing !~ m/\w/;
+ my $path = $r->_get_path('srcdump');
+ my $ctf = new IO::File "$path/$thing.ctype", 'r'
+ or die "$path/$thing.ctype $!";
+ my $ct = <$ctf>;
+ chomp $ct or die "$path/$thing ?";
+ $ct->close or die "$path/$thing $!";
+ my $df = new IO::File "$path/$thing.data", 'r'
+ or die "$path/$thing.data $!";
+ $r->_ch('dump', $ct, $df);
+}
+
+sub dump_plain ($$$$) {
+ my ($c, $r, $ct, $df) = @_;
+ $r->_print($c->header(-type $ct));
+ my $buffer;
+ for (;;) {
+ my $got = read $df, $buffer, 65536;
+ die $! unless defined $got;
+ return if !$got;
+ $r->_print($buffer);
+ }
+}
+
+sub srcdump_process_dir ($$$$$$) {
+ my ($c, $v, $dumpdir, $incdir, $tarballcounter,
+ $needlicence, $dirsdone) = @_;
+ return () if $v->_ch('srcdump_system_dir', $incdir);
+ my $upwards = $incdir;
+ for (;;) {
+ $upwards =~ s#/+##;
+ last unless $upwards =~ m#[^/]#;
+ foreach my $try (@{ $v->{S}{srcdump_vcs_dirs} }) {
+ if (!stat "$upwards/$try") {
+ $!==&ENOENT or die "check $upwards/$try $!";
+ next;
+ }
+ $try =~ m/\w+/ or die;
+ return if $dirsdone->{$upwards}++;
+ return $v->_ch(('srcdump_byvcs_'.lc $try),
+ $dumpdir, $upwards, $tarballcounter);
+ }
+ $upwards =~ s#/*[^/]+##;
+ }
+ return $v->_ch('srcdump_novcs', $dumpdir, $incdir, $tarballcounter);
+}
+
+sub srcdump_novcs ($$$$$) {
+ my ($c, $v, $dumpdir, $dir, $tarballcounter) = @_;
+ my $script = 'find -type f -perm +004';
+ foreach my $excl (@{ $v->{S}{srcdump_excludes} }) {
+ $script .= " \\! -name '$excl'";
+ }
+ $script .= " -print0";
+ return srcdump_dir_cpio($c,$v,$dumpdir,$dir,$tarballcounter,$script);
+}
+
+sub srcdump_byvcs_git ($$$$$) {
+ my ($c, $v, $dumpdir, $dir, $tarballcounter) = @_;
+ return srcdump_dir_cpio($c,$v,$dumpdir,$dir,$tarballcounter,"
+ git-ls-files -z;
+ git-ls-files -z --others --exclude-from=.gitignore;
+ find .git -print0
+ ");
+}
+
+sub scrdump_dir_cpio ($$$$$) {
+ my ($c,$v,$dumpdir,$dir,$tarballcounter,$script) = @_;
+ my $outfile = "$dumpdir/$$tarballcounter.tar";
+ my $pid = fork();
+ defined $pid or die $!;
+ if (!$pid) {
+ open STDOUT, ">", $outfile or die "$outfile $!";
+ chdir $dir or die "chdir $dir: $!";
+ exec '/bin/bash','-ec','',"
+ set -o pipefail
+ (
+ $script
+ ) | (
+ cpio -Hustar -o --quiet -0 -R 1000:1000 || \
+ cpio -Hustar -o --quiet -0
+ )
+ ";
+ die $!;
+ }
+ $!=0; (waitpid $pid, 0) == $pid or die "$!";
+ die "$dir ($script) $outfile $?" if $?;
+ $$tarballcounter++;
+ return $outfile;
+}
+
+sub srcdump_dirscan_prepare ($$) {
+ my ($c, $v) = @_;
+ my $dumpdir = $v->_get_path('srcdump');
+ my $lockf = new IO::File "$dumpdir/generate.lock", 'w+'
+ or die "$dumpdir/generate.lock $!";
+ flock $lockf, LOCK_EX or die "$dumpdir/generate.lock $!";
+ my $needlicence = "$dumpdir/licence.tmp";
+ unlink $needlicence || $!==&ENOENT or die "rm $needlicence $!";
+ if (defined $v->{S}{srcdump_licence_path}) {
+ copy($v->{S}{srcdump_licence_path}, $needlicence)
+ or die "$v->{S}{srcdump_licence_path} $!";
+ $needlicence = undef;
+ }
+ my $srctarballcounter = 'aaa';
+ my %dirsdone;
+ my @srcfiles = ("$dumpdir/licence.data");
+ foreach my $incdir ($v->_ch('getsource_includedirs')) {
+ if (defined $needlicence) {
+ foreach my $try (@{ $v->{S}{srcdump_licence_files} }) {
+ last if copy("$incdir/$try", $needlicence);
+ $!==&ENOENT or die "copy $incdir/$try $!";
+ }
+ }
+ push @srcfiles, $v->_ch('srcdump_process_dir', $dumpdir, $incdir,
+ \$srctarballcounter, \$needlicence, \%dirsdone);
+ $dirsdone{$incdir}++;
+ }
+ die "licence file not found" unless defined $needlicence;
+ srcdump_install($c,$v, $dumpdir, 'licence', 'text/plain');
+ close $lockf or die $!;
+}
+
+sub srcdump_install ($$$$$) {
+ my ($c,$v, $dumpdir, $which, $ctype) = @_;
+ rename "$dumpdir/$which.tmp", "$dumpdir/$which.data"
+ or die "$dumpdir/$which.data $!";
+ my $ctf = new IO::File "$dumpdir/$which.tmp", 'w'
+ or die "$dumpdir/$which.tmp $!";
+ print $ctf $ctype, "\n" or die $!;
+ close $ctf or die $!;
+ rename "$dumpdir/$which.tmp", "$dumpdir/$which.ctype"
+ or die "$dumpdir/$which.ctype $!";
+}
+