+void nodatacow_dir(const char *dir)
+{
+ DIR *dh = opendir(dir);
+ int fd;
+
+ if (!dh) croak("opendir(%s): %s", dir, strerror(errno));
+ fd = dirfd(dh);
+ if (fd >= 0)
+ nodatacow_fd(fd);
+ /* ENOTSUP probably won't happen under Linux... */
+ closedir(dh);
+}
+SET_NODATACOW
+
+ my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
+ $ENV{XDG_CACHE_HOME} //
+ ( ($ENV{HOME} // '/nonexistent').'/.cache' )
+ ).'/public-inbox/inline-c';
+ warn "$inline_dir exists, not writable\n" if -e $inline_dir && !-w _;
+ $set_nodatacow = $all_libc = undef unless -d _ && -w _;
+ if (defined $all_libc) {
+ my $f = "$inline_dir/.public-inbox.lock";
+ open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
+ open my $olderr, '>&', \*STDERR or die "dup(2): $!";
+ open my $fh, '+>', $f or die "open($f): $!";
+ open STDOUT, '>&', $fh or die "1>$f: $!";
+ open STDERR, '>&', $fh or die "2>$f: $!";
+ STDERR->autoflush(1);
+ STDOUT->autoflush(1);
+
+ # CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
+ flock($fh, LOCK_EX) or die "LOCK_EX($f): $!";
+ eval <<'EOM';
+use Inline C => $all_libc.$set_nodatacow, BUILD_NOISY => 1;
+EOM
+ my $err = $@;
+ my $ndc_err = '';
+ if ($err && $set_nodatacow) { # missing Linux kernel headers
+ $ndc_err = "with set_nodatacow: <\n$err\n>\n";
+ undef $set_nodatacow;
+ eval <<'EOM';
+use Inline C => $all_libc, BUILD_NOISY => 1;
+EOM
+ };
+ $err = $@;
+ open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
+ open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
+ if ($err) {
+ seek($fh, 0, SEEK_SET);
+ my @msg = <$fh>;
+ warn "Inline::C build failed:\n",
+ $ndc_err, $err, "\n", @msg;
+ $set_nodatacow = $all_libc = undef;
+ } elsif ($ndc_err) {
+ warn "Inline::C build succeeded w/o set_nodatacow\n",
+ "error $ndc_err";
+ }
+ }
+ unless ($all_libc) {
+ require PublicInbox::SpawnPP;
+ *pi_fork_exec = \&PublicInbox::SpawnPP::pi_fork_exec
+ }
+ unless ($set_nodatacow) {
+ require PublicInbox::NDC_PP;
+ no warnings 'once';
+ *nodatacow_fd = \&PublicInbox::NDC_PP::nodatacow_fd;
+ *nodatacow_dir = \&PublicInbox::NDC_PP::nodatacow_dir;
+ }
+} # /BEGIN
+