+sub _rename_noreplace_racy ($$) {
+ my ($old, $new) = @_;
+ if (link($old, $new)) {
+ warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
+ 1
+ } else {
+ undef;
+ }
+}
+
+# TODO: support FD args?
+sub rename_noreplace ($$) {
+ my ($old, $new) = @_;
+ if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
+ my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
+ if ($ret == 0) {
+ 1; # like rename() perlop
+ } elsif ($! == ENOSYS || $! == EINVAL) {
+ undef $SYS_renameat2;
+ _rename_noreplace_racy($old, $new);
+ } else {
+ undef
+ }
+ } else {
+ _rename_noreplace_racy($old, $new);
+ }
+}
+
+sub nodatacow_fh ($) {
+ my ($fh) = @_;
+ my $buf = "\0" x 120;
+ syscall($SYS_fstatfs // return, fileno($fh), $buf) == 0 or
+ return warn("fstatfs: $!\n");
+ my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
+ return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
+
+ $FS_IOC_GETFLAGS //
+ return warn('FS_IOC_GETFLAGS undefined for platform');
+ ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
+ return warn("FS_IOC_GETFLAGS: $!\n");
+ my $attr = unpack('l!', $buf);
+ return if ($attr & 0x00800000); # FS_NOCOW_FL;
+ ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
+ warn("FS_IOC_SETFLAGS: $!\n");
+}
+
+sub nodatacow_dir {
+ if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
+}
+
+sub CMSG_ALIGN ($) { ($_[0] + SIZEOF_size_t - 1) & ~(SIZEOF_size_t - 1) }
+use constant CMSG_ALIGN_SIZEOF_cmsghdr => CMSG_ALIGN(SIZEOF_cmsghdr);
+sub CMSG_SPACE ($) { CMSG_ALIGN($_[0]) + CMSG_ALIGN_SIZEOF_cmsghdr }
+sub CMSG_LEN ($) { CMSG_ALIGN_SIZEOF_cmsghdr + $_[0] }
+use constant msg_controllen => CMSG_SPACE(10 * SIZEOF_int) + 16; # 10 FDs
+
+if (defined($SYS_sendmsg) && defined($SYS_recvmsg)) {
+no warnings 'once';
+*send_cmd4 = sub ($$$$) {
+ my ($sock, $fds, undef, $flags) = @_;
+ my $iov = pack('P'.TMPL_size_t,
+ $_[2] // NUL, length($_[2] // NUL) || 1);
+ my $cmsghdr = pack(TMPL_size_t . # cmsg_len
+ 'LL' . # cmsg_level, cmsg_type,
+ ('i' x scalar(@$fds)),
+ CMSG_LEN(scalar(@$fds) * SIZEOF_int), # cmsg_len
+ SOL_SOCKET, SCM_RIGHTS, # cmsg_{level,type}
+ @$fds); # CMSG_DATA
+ my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
+ BYTES_4_hole . # 4-byte padding on 64-bit
+ 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
+ 'P'.TMPL_size_t . # msg_control, msg_controllen,
+ 'i', # msg_flags
+ NUL, 0, # msg_name, msg_namelen (unused)
+ @BYTES_4_hole,
+ $iov, 1, # msg_iov, msg_iovlen
+ $cmsghdr, # msg_control
+ CMSG_SPACE(scalar(@$fds) * SIZEOF_int), # msg_controllen
+ 0); # msg_flags
+ my $sent;
+ my $try = 0;
+ do {
+ $sent = syscall($SYS_sendmsg, fileno($sock), $mh, $flags);
+ } while ($sent < 0 &&
+ ($!{ENOBUFS} || $!{ENOMEM} || $!{ETOOMANYREFS}) &&
+ (++$try < 50) &&
+ warn "sleeping on sendmsg: $! (#$try)\n" &&
+ select(undef, undef, undef, 0.1) == 0);
+ $sent >= 0 ? $sent : undef;
+};
+
+*recv_cmd4 = sub ($$$) {
+ my ($sock, undef, $len) = @_;
+ vec($_[1] //= '', ($len + 1) * 8, 1) = 0;
+ my $cmsghdr = "\0" x msg_controllen; # 10 * sizeof(int)
+ my $iov = pack('P'.TMPL_size_t, $_[1], $len);
+ my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
+ BYTES_4_hole . # 4-byte padding on 64-bit
+ 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
+ 'P'.TMPL_size_t . # msg_control, msg_controllen,
+ 'i', # msg_flags
+ NUL, 0, # msg_name, msg_namelen (unused)
+ @BYTES_4_hole,
+ $iov, 1, # msg_iov, msg_iovlen
+ $cmsghdr, # msg_control
+ msg_controllen,
+ 0); # msg_flags
+ my $r = syscall($SYS_recvmsg, fileno($sock), $mh, 0);
+ return (undef) if $r < 0; # $! set
+ substr($_[1], $r, length($_[1]), '');
+ my @ret;
+ if ($r > 0) {
+ my ($len, $lvl, $type, @fds) = unpack(TMPL_size_t . # cmsg_len
+ 'LLi*', # cmsg_level, cmsg_type, @fds
+ $cmsghdr);
+ if ($lvl == SOL_SOCKET && $type == SCM_RIGHTS) {
+ $len -= CMSG_ALIGN_SIZEOF_cmsghdr;
+ @ret = @fds[0..(($len / SIZEOF_int) - 1)];
+ }
+ }
+ @ret;
+};
+}
+