NAME

POSIX::At - FFI bindings to the POSIX *at() family of filesystem syscalls

SYNOPSIS

use POSIX::At qw(
  AT_FDCWD AT_REMOVEDIR
  openat unlinkat mkdirat
);
use POSIX::Structs qw( POSIX::Structs::Stat );
use Fcntl qw(O_RDONLY O_CREAT O_WRONLY O_DIRECTORY);

# Open a directory handle; *at calls accept it anywhere a dirfd is needed.
my $dir = openat(AT_FDCWD, "/some/base", O_RDONLY|O_DIRECTORY, 0)
  or die "openat: $!";

# Stat a file relative to that directory
my $st = POSIX::Structs::Stat->new;
fstatat($dir, "foo.txt", $st, 0) == 0
  or die "fstatat: $!";
printf "mode=0%o size=%d\n", $st->st_mode & 07777, $st->st_size;

# Unlink relative to that directory
unlinkat($dir, "old.txt", 0) == 0
  or die "unlinkat: $!";

DESCRIPTION

POSIX::At provides thin FFI bindings to the modern POSIX/Linux *at() family of filesystem syscalls via FFI::Platypus.

Struct arguments (e.g. for fstatat) use types defined in POSIX::Structs, which must be loaded separately. Both modules share a single FFI::Platypus instance via POSIX::Structs::ffi().

The bindings mostly follow C semantics intentionally:

  • openat returns an IO::File or IO::Dir object on success, or undef on error; check $! for the reason.

  • Other functions return -1 on error; check $! for the reason.

  • No exception throwing.

EXPORTS

By default: AT_FDCWD, AT_EMPTY_PATH.

The following may be imported by name:

# Constants
AT_FDCWD AT_SYMLINK_NOFOLLOW AT_SYMLINK_FOLLOW
AT_EACCESS AT_REMOVEDIR AT_EMPTY_PATH
RENAME_NOREPLACE RENAME_EXCHANGE RENAME_WHITEOUT

# Bindings and helpers
openat fstatat unlinkat mkdirat mknodat mkfifoat
fchmodat fchownat utimensat linkat symlinkat readlinkat
renameat renameat2 faccessat futimesat opendirat

renameat2 and futimesat are silently absent on platforms where the underlying libc does not provide them.

NOTES ON utimensat AND futimesat

These functions take a pointer to an array of two struct timespec (or struct timeval) values. The caller is responsible for allocating the buffer. The simplest approach is to use FFI::Platypus::Memory:

use FFI::Platypus::Memory qw(malloc free);
use FFI::Platypus::Buffer qw(scalar_to_pointer);

# Pack two struct timespec values (tv_sec, tv_nsec each sint64)
my $buf = pack('q!q!q!q!', $sec, 0, $sec, 0);
utimensat($dirfd, "file", scalar_to_pointer($buf), 0);

DEVELOPMENT

Set $ENV{VIM_TESTING}=1 before running tests to merge STDERR into STDOUT, which makes all output appear in Vim's quickfix buffer when running make test from inside the editor.

AUTHORS

Nobody <nobody@turing-trust.com>

Claude <claude@turing-trust.com>

ChatGPT <gpt@turing-trust.com>

LICENSE

This module is free software; you may redistribute it and/or modify it under the same terms as Perl itself.