+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,'SRCDUMP')).
+ '">'.$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 ($$$) {
+ my ($c,$r, $thing) = @_;
+ 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 ?";
+ $ctf->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_item ($$$$$$) {
+ my ($c, $v, $dumpdir, $item, $outfn, $needlicence, $dirsdone) = @_;
+ if ($v->_ch('srcdump_system_dir', $item)) {
+ $outfn->("srcdump_process_item: srcdump_system_dir, skipping $item");
+ return;
+ }
+ my $upwards = $item;
+ for (;;) {
+ $upwards =~ s#/+$##;
+ $upwards =~ s#/+\.$##;
+ last unless $upwards =~ m#[^/]#;
+ foreach my $try (@{ $v->{S}{srcdump_vcs_dirs} }) {
+#print STDERR "TRY $item $upwards $try\n";
+ if (!stat "$upwards/$try") {
+ $!==&ENOENT or $!==&ENOTDIR or die "check $upwards/$try $!";
+ next;
+ }
+#print STDERR "VCS $item $upwards $try\n";
+ if ($dirsdone->{$upwards}++) {
+ $outfn->("srcdump_process_item: did $upwards,".
+ " skipping $item");
+ return;
+ }
+#print STDERR "VCS $item $upwards $try GO\n";
+ $try =~ m/\w+/ or die;
+ return $v->_ch('srcdump_byvcs', $dumpdir, $upwards, $outfn, lc $&);
+ }
+ $upwards =~ s#/*[^/]+$##;
+ }
+ return $v->_ch('srcdump_novcs', $dumpdir, $item, $outfn);
+}
+
+sub srcdump_novcs ($$$$$) {
+ my ($c, $v, $dumpdir, $item, $outfn) = @_;
+ stat $item or die "$item $!";
+ if (-d _) {
+ 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,$item,$outfn,'novcs',$script);
+ } elsif (-f _) {
+ return srcdump_file($c,$v,$dumpdir,$item,$outfn);
+ } else {
+ die "$item not file or directory";
+ }
+}
+
+sub srcdump_byvcs ($$$$$$) {
+ my ($c, $v, $dumpdir, $dir, $outfn, $vcs) = @_;
+#print STDERR "BYVCS GIT $dir\n";
+ my $script = $v->{S}{"srcdump_vcsscript"}{$vcs};
+ die "no script for vcs $vcs" unless defined $script;
+ return srcdump_dir_cpio($c,$v,$dumpdir,$dir,$outfn,$vcs,$script);
+}
+
+sub srcdump_file ($$$$) {
+ my ($c,$v,$dumpdir,$file,$outfn) = @_;
+ my $outfile = $outfn->("srcdump_file saved $file", "src");
+ copy($file,$outfile) or die "$file $outfile $!";
+}
+
+sub srcdump_dir_cpio ($$$$$$$) {
+ my ($c,$v,$dumpdir,$dir,$outfn,$how,$script) = @_;
+ my $outfile = $outfn->("srcdump_dir_cpio $how saved $dir", "tar");
+#print STDERR "CPIO $dir >$script<\n";
+ my $pid = fork();
+ defined $pid or die $!;
+ if (!$pid) {
+ $SIG{__DIE__} = sub {
+ print STDERR "CGI::Auth::Flexible srcdump error: $@\n";
+ exit 127;
+ };
+ 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 ($how $script) $outfile $?" if $?;
+}
+
+sub srcdump_dirscan_prepare ($$) {
+ my ($c, $v) = @_;
+ my $dumpdir = $v->_get_path('srcdump');
+ mkdir $dumpdir or $!==&EEXIST or die "mkdir $dumpdir $!";
+ 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 or $!==&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;
+ }
+ unlink <"$dumpdir/s.[a-z][a-z][a-z].*">;
+ my @srcfiles = qw(licence.data manifest.txt);
+ my $srcoutcounter = 'aaa';
+
+ my $reportfh = new IO::File "$dumpdir/manifest.txt", 'w' or die $!;
+ my $outfn = sub {
+ my ($message, $extension) = @_;
+ if (defined $extension) {
+ my $leaf = "s.$srcoutcounter.$extension";
+ $srcoutcounter++;
+ push @srcfiles, $leaf;
+ print $reportfh "$leaf: $message\n" or die $!;
+ return "$dumpdir/$leaf";
+ } else {
+ print $reportfh "none: $message\n" or die $!;
+ return undef;
+ }
+ };
+ my %dirsdone;
+ foreach my $item ($v->_ch('srcdump_listitems')) {
+ next unless defined $item;
+ if ($item eq '.' && $v->{S}{srcdump_filter_cwd}) {
+ my @bad = grep { !m#^/# } values %INC;
+ die "filtering . from srcdump items and \@INC but already".
+ " included @bad " if @bad;
+ @INC = grep { $_ ne '.' } @INC;
+ next;
+ }
+ if (!lstat "$item") {
+ die "stat $item $!" unless $!==&ENOENT;
+ $outfn->("srcdump_dirscan_prepare stat ENOENT, skipping $item");
+ next;
+ };
+ if (-l _) {
+ $item = realpath($item);
+ if (!defined $item) {
+ die "realpath $item $!" unless $!==&ENOENT;
+ $outfn->("srcdump_dirscan_prepare realpath ENOENT,".
+ " skipping $item");
+ }
+ }
+ if (defined $needlicence) {
+ foreach my $try (@{ $v->{S}{srcdump_licence_files} }) {
+ last if copy("$item/$try", $needlicence);
+ $!==&ENOENT or $!==&ENOTDIR or die "copy $item/$try $!";
+ }
+ }
+ $v->_ch('srcdump_process_item', $dumpdir, $item,
+ $outfn, \$needlicence, \%dirsdone);
+ $dirsdone{$item}++;
+ }
+ close $reportfh or die $!;
+ srcdump_install($c,$v, $dumpdir, 'licence', 'text/plain');
+ $!=0;
+ my @cmd = (qw(sh -ec), 'exec >&2 "$@"', qw(x),
+ qw(tar -zvvcf), "$dumpdir/source.tmp",
+ "-C", $dumpdir, qw( --), @srcfiles);
+ my $r = system(@cmd);
+ if ($r) {
+ print STDERR "CGI::Auth::Flexible tar failed ($r $!) @cmd\n";
+ die "tar failed";
+ }
+ die "licence file not found" unless defined $needlicence;
+ srcdump_install($c,$v, $dumpdir, 'source', 'application/octet-stream');
+ 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 $!";
+}
+