faccess

Trait PathExt

Source
pub trait PathExt {
    // Required method
    fn access(&self, mode: AccessMode) -> Result<()>;

    // Provided methods
    fn readable(&self) -> bool { ... }
    fn writable(&self) -> bool { ... }
    fn executable(&self) -> bool { ... }
}
Expand description

Extension trait for std::path::Path.

Required Methods§

Source

fn access(&self, mode: AccessMode) -> Result<()>

Returns Ok(()) if the path points at an entity which can be accessed with the given set of AccessMode flags, otherwise returns Err(io::Error) indicating why the access check failed.

This function will traverse symbolic links. In the case of broken symbolic links it will return an io::Error with a kind() of io::ErrorKind::NotFound.

This function is best-effort, and on some platforms may simply indicate the path exists. Care should be taken not to rely on its result.

§Platform-specific behaviour

This function currently corresponds to the faccessat function in Unix, with a directory of AT_FDCWD, and the AT_EACCESS flag to perform the check against the effective user and group.

On Windows a custom check is performed which attempts to approximate its semantics.

On other platforms, a fallback to std::path::Path::exists and std::fs::Permissions::readonly is used.

§Examples
use std::path::Path;
use faccess::{AccessMode, PathExt};

// File exists
assert!(Path::new("/bin/sh").access(AccessMode::EXISTS).is_ok());

// File is readable and executable
assert!(Path::new("/bin/sh").access(AccessMode::READ | AccessMode::EXECUTE).is_ok());

// File is not writable
assert!(Path::new("/bin/sh").access(AccessMode::WRITE).is_err());

Provided Methods§

Source

fn readable(&self) -> bool

Returns true if the path points at a readable entity.

Equivalent to access(AccessMode::READ).is_ok().

§Examples
use std::path::Path;
use faccess::PathExt;

assert_eq!(Path::new("/etc/master.password").readable(), false);
Source

fn writable(&self) -> bool

Returns true if the path points at a writable entity.

Equivalent to access(AccessMode::WRITE).is_ok().

§Examples
use std::path::Path;
use faccess::PathExt;

assert_eq!(Path::new("/etc/master.password").writable(), false);
§See Also

The Rust standard library’s std::fs::Permissions::readonly method is this function’s inverse.

Source

fn executable(&self) -> bool

Returns true if the path points at an executable entity.

Equivalent to access(AccessMode::EXECUTE).is_ok().

§Examples
use std::path::Path;
use faccess::PathExt;

assert_eq!(Path::new("/bin/ls").executable(), true);

Implementations on Foreign Types§

Source§

impl PathExt for Path

Source§

fn access(&self, mode: AccessMode) -> Result<()>

Implementors§