Skip to content

feature request: allow safely composing dir::Dir::openat and fcntl::readlinkat with dir::OwningIter #2669

@opsound

Description

@opsound

Use-case:

  • I am iterating over the contents of a directory
  • During the iteration, I want to take advantage of functions like openat and readlinkat, which act relative to a dirfd, so that I can avoid path construction, and reduce the amount of work spent in the kernel traversing path arguments.

e.g. something along the lines of

use std::ffi::OsStr;

use nix::dir::Dir;
use nix::fcntl::OFlag;
use nix::fcntl::readlinkat;
use nix::sys::stat::Mode;

let dir_iter = Dir::open("/path/to/some/dir", OFlag::O_RDONLY, Mode::empty())?.into_iter();

while let Some(dentry) = dir_iter.next() {
  let dentry = dentry?;
  let _ = readlinkat(dir_iter.as_fd(), dentry.file_name())?;
}

Currently, you can extract the raw dirfd using OwningIter::as_raw_fd, but have to be careful to not violate the contracts of dirfd(3) with respect to the owning DIR stream.

This file descriptor is the one used internally by the directory stream. As a result, it is useful only for func‐
tions which do not depend on or alter the file position, such as fstat(2) and fchdir(2). It will be automatically
closed when closedir(3) is called.

It would be nice if there was a safe way to compose Dir with other functions acting relative to a dirfd.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions