]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NDC_PP.pm
searchidx: disable CoW for SQLite and Xapian under btrfs
[public-inbox.git] / lib / PublicInbox / NDC_PP.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Pure-perl class for Linux non-Inline::C users to disable COW for btrfs
5 package PublicInbox::NDC_PP;
6 use strict;
7 use v5.10.1;
8
9 sub set_nodatacow ($) {
10         my ($fd) = @_;
11         return if $^O ne 'linux';
12         defined(my $path = readlink("/proc/self/fd/$fd")) or return;
13         open my $mh, '<', '/proc/self/mounts' or return;
14         for (grep(/ btrfs /, <$mh>)) {
15                 my (undef, $mnt_path, $type) = split(/ /);
16                 next if $type ne 'btrfs'; # in case of false-positive from grep
17
18                 # weird chars are escaped as octal
19                 $mnt_path =~ s/\\(0[0-9]{2})/chr(oct($1))/egs;
20                 $mnt_path .= '/' unless $mnt_path =~ m!/\z!;
21                 if (index($path, $mnt_path) == 0) {
22                         # error goes to stderr, but non-fatal for us
23                         system('chattr', '+C', $path);
24                         last;
25                 }
26         }
27 }
28
29 1;