1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Pure-perl class for Linux non-Inline::C users to disable COW for btrfs
5 package PublicInbox::NDC_PP;
9 sub set_nodatacow ($) {
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
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);