Skip to content

Commit

Permalink
use Path
Browse files Browse the repository at this point in the history
Signed-off-by: nayuta-ai <[email protected]>
  • Loading branch information
nayuta-ai committed Feb 9, 2025
1 parent 5d79a48 commit 42ae3a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 6 additions & 5 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,15 +831,15 @@ pub fn validate_masked_paths(spec: &Spec) {
}
};

for path_str in masked_paths {
let path = Path::new(path_str);
for path in masked_paths.iter().map(Path::new) {
if !path.is_absolute() {
eprintln!("in masked paths, the path must be absolute.")
}
match test_read_access(path_str) {
match test_read_access(path) {
Ok(true) => {
eprintln!(
"in masked paths, expected path {path_str} to be masked, but was found readable"
"in masked paths, expected path {:?} to be masked, but was found readable",
path.iter().as_path(),
);
return;
}
Expand All @@ -850,7 +850,8 @@ pub fn validate_masked_paths(spec: &Spec) {
/* This is expected */
} else {
eprintln!(
"in masked paths, error in testing read access for path {path_str} : {errno:?}"
"in masked paths, error in testing read access for path {:?} : {errno:?}",
path.iter().as_path(),
);
return;
}
Expand Down
16 changes: 10 additions & 6 deletions tests/contest/runtimetest/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::fs;
use std::fs::{metadata, symlink_metadata, OpenOptions};
use std::io::Read;
use std::os::unix::prelude::MetadataExt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;

use nix::sys::stat::{stat, SFlag};

// It means the file or directory is readable
type Readable = bool;

fn test_file_read_access(path: &str) -> Result<Readable, std::io::Error> {
fn test_file_read_access<P: AsRef<Path>>(path: P) -> Result<Readable, std::io::Error> {
let mut file = OpenOptions::new().create(false).read(true).open(path)?;

// Create a buffer with a capacity of 1 byte
Expand All @@ -25,7 +25,7 @@ fn test_file_read_access(path: &str) -> Result<Readable, std::io::Error> {
}
}

pub fn test_dir_read_access(path: &str) -> Result<Readable, std::io::Error> {
pub fn test_dir_read_access<P: AsRef<Path>>(path: P) -> Result<Readable, std::io::Error> {
let entries = std::fs::read_dir(path);

match entries {
Expand Down Expand Up @@ -56,8 +56,9 @@ fn is_dir(mode: u32) -> bool {
mode & SFlag::S_IFDIR.bits() != 0
}

pub fn test_read_access(path: &str) -> Result<Readable, std::io::Error> {
let fstat = stat(path)?;
pub fn test_read_access<P: AsRef<Path>>(path: P) -> Result<Readable, std::io::Error> {
let path_ref = path.as_ref();
let fstat = stat(path_ref)?;
let mode = fstat.st_mode;
if is_file_like(mode) {
// we have a file or a char/block device
Expand All @@ -68,7 +69,10 @@ pub fn test_read_access(path: &str) -> Result<Readable, std::io::Error> {

Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("cannot test read access for {path:?}, has mode {mode:x}"),
format!(
"cannot test read access for {:?}, has mode {mode:x}",
path_ref
),
))
}

Expand Down

0 comments on commit 42ae3a5

Please sign in to comment.