Skip to content

Added libstd::io::fs::access() #19605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ use vec::Vec;
use sys::fs as fs_imp;
use sys_common;

pub use libc::{F_OK,R_OK,W_OK,X_OK};

/// Unconstrained file access type that exposes read and write operations
///
/// Can be constructed via `File::open()`, `File::create()`, and
Expand Down Expand Up @@ -326,6 +328,36 @@ pub fn lstat(path: &Path) -> IoResult<FileStat> {
format!("{}; path={}", e, path.display()))
}

/// Given a path and access mode, checks whether the calling process can access
/// the path and perform the requested operations.
/// Mode can either be F_OK to check for existence or a bitwise OR of one or
/// more of R_OK, W_OK, and X_OK to test for read, write and execute access
/// respectively.
///
/// # Example
///
/// ```rust
/// use std::io::fs;
/// use std::io::fs::{R_OK,W_OK,X_OK};
///
/// let p = Path::new("/some/file/path.txt");
/// match fs::access(&p, R_OK|W_OK|X_OK) {
/// Err(e) => { /* handle error */ }
/// _ => { /* ... */ }
/// }
/// ```
///
/// # Error
///
/// This function will return an error if the user is not allowed to
/// perform the requested operations on the file.
pub fn access(path: &Path, amode: i32) -> IoResult<()> {
fs_imp::access(path, amode)
.update_err("couldn't access path", |e|
format!("{}; path={}; amode={}", e, path.display(), amode))

}

/// Rename a file or directory to a new name.
///
/// # Example
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sys::retry;
use sys_common::{keep_going, eof, mkerr_libc};

pub use path::PosixPath as Path;
pub use libc::{F_OK,R_OK,W_OK,X_OK};

pub type fd_t = libc::c_int;

Expand Down Expand Up @@ -349,6 +350,11 @@ pub fn lstat(p: &Path) -> IoResult<FileStat> {
}
}

pub fn access(p: &Path, a: i32) -> IoResult<()> {
let p = p.to_c_str();
mkerr_libc(unsafe { libc::access(p.as_ptr(), a) })
}

pub fn utime(p: &Path, atime: u64, mtime: u64) -> IoResult<()> {
let p = p.to_c_str();
let buf = libc::utimbuf {
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/sys/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use io::{IoResult, IoError, FileStat, SeekStyle, Seek, Writer, Reader};
use io::{Read, Truncate, SeekCur, SeekSet, ReadWrite, SeekEnd, Append};

pub use path::WindowsPath as Path;
pub use libc::{F_OK,R_OK,W_OK,X_OK};

pub type fd_t = libc::c_int;

pub struct FileDesc {
Expand Down Expand Up @@ -448,6 +450,11 @@ pub fn lstat(_p: &Path) -> IoResult<FileStat> {
Err(super::unimpl())
}

pub fn access(p: &Path, a: i32) -> IoResult<()> {
let p = p.to_c_str();
mkerr_libc(unsafe { libc::access(p.as_ptr(), a) })
}

pub fn utime(p: &Path, atime: u64, mtime: u64) -> IoResult<()> {
let mut buf = libc::utimbuf {
actime: atime as libc::time64_t,
Expand Down