From 2f88ba8628fc7a7e67ee5dc06dcbff13a36d33de Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Fri, 23 Sep 2022 17:42:35 -0400 Subject: [PATCH] tests: add AtPath.set_mode() method Add the `AtPath.set_mode()` method that sets the permissions for a given path in the test directory. --- tests/common/util.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 932ae1c31..b65e6cebf 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -19,7 +19,7 @@ use std::ffi::OsStr; use std::fs::{self, hard_link, File, OpenOptions}; use std::io::{BufWriter, Read, Result, Write}; #[cfg(unix)] -use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file}; +use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file, PermissionsExt}; #[cfg(windows)] use std::os::windows::fs::{symlink_dir, symlink_file}; #[cfg(windows)] @@ -821,6 +821,20 @@ impl AtPath { s } } + + /// Set the permissions of the specified file. + /// + /// # Panics + /// + /// This function panics if there is an error loading the metadata + /// or setting the permissions of the file. + #[cfg(not(windows))] + pub fn set_mode(&self, filename: &str, mode: u32) { + let path = self.plus(filename); + let mut perms = std::fs::metadata(&path).unwrap().permissions(); + perms.set_mode(mode); + std::fs::set_permissions(&path, perms).unwrap(); + } } /// An environment for running a single uutils test case, serves three functions: