mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
Merge pull request #3782 from koutheir/main
Use `u64` instead of `ino_t`/`dev_t` types
This commit is contained in:
commit
2e0ed71bce
2 changed files with 39 additions and 12 deletions
|
@ -448,10 +448,39 @@ enum CommandLineMode {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
struct DeviceAndINode {
|
||||||
|
device_id: u64,
|
||||||
|
inode: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
impl From<fs::Metadata> for DeviceAndINode {
|
||||||
|
fn from(md: fs::Metadata) -> Self {
|
||||||
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
device_id: md.dev(),
|
||||||
|
inode: md.ino(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&libc::stat> for DeviceAndINode {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
#[allow(clippy::useless_conversion)]
|
||||||
|
fn try_from(st: &libc::stat) -> Result<Self> {
|
||||||
|
let device_id = u64::try_from(st.st_dev).map_err(|_r| Error::OutOfRange)?;
|
||||||
|
let inode = u64::try_from(st.st_ino).map_err(|_r| Error::OutOfRange)?;
|
||||||
|
Ok(Self { device_id, inode })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn process_files(
|
fn process_files(
|
||||||
options: &Options,
|
options: &Options,
|
||||||
context: &SELinuxSecurityContext,
|
context: &SELinuxSecurityContext,
|
||||||
root_dev_ino: Option<(libc::ino_t, libc::dev_t)>,
|
root_dev_ino: Option<DeviceAndINode>,
|
||||||
) -> Vec<Error> {
|
) -> Vec<Error> {
|
||||||
let fts_options = options.recursive_mode.fts_open_options();
|
let fts_options = options.recursive_mode.fts_open_options();
|
||||||
let mut fts = match fts::FTS::new(options.files.iter(), fts_options) {
|
let mut fts = match fts::FTS::new(options.files.iter(), fts_options) {
|
||||||
|
@ -483,7 +512,7 @@ fn process_file(
|
||||||
options: &Options,
|
options: &Options,
|
||||||
context: &SELinuxSecurityContext,
|
context: &SELinuxSecurityContext,
|
||||||
fts: &mut fts::FTS,
|
fts: &mut fts::FTS,
|
||||||
root_dev_ino: Option<(libc::ino_t, libc::dev_t)>,
|
root_dev_ino: Option<DeviceAndINode>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut entry = fts.last_entry_ref().unwrap();
|
let mut entry = fts.last_entry_ref().unwrap();
|
||||||
|
|
||||||
|
@ -504,8 +533,8 @@ fn process_file(
|
||||||
};
|
};
|
||||||
|
|
||||||
// SAFETY: If `entry.fts_statp` is not null, then is is assumed to be valid.
|
// SAFETY: If `entry.fts_statp` is not null, then is is assumed to be valid.
|
||||||
let file_dev_ino = if let Some(stat) = entry.stat() {
|
let file_dev_ino: DeviceAndINode = if let Some(st) = entry.stat() {
|
||||||
(stat.st_ino, stat.st_dev)
|
st.try_into()?
|
||||||
} else {
|
} else {
|
||||||
return Err(err("Getting meta data", io::ErrorKind::InvalidInput));
|
return Err(err("Getting meta data", io::ErrorKind::InvalidInput));
|
||||||
};
|
};
|
||||||
|
@ -692,18 +721,13 @@ pub(crate) fn os_str_to_c_string(s: &OsStr) -> Result<CString> {
|
||||||
|
|
||||||
/// Call `lstat()` to get the device and inode numbers for `/`.
|
/// Call `lstat()` to get the device and inode numbers for `/`.
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn get_root_dev_ino() -> Result<(libc::ino_t, libc::dev_t)> {
|
fn get_root_dev_ino() -> Result<DeviceAndINode> {
|
||||||
use std::os::unix::fs::MetadataExt;
|
|
||||||
|
|
||||||
fs::symlink_metadata("/")
|
fs::symlink_metadata("/")
|
||||||
.map(|md| (md.ino(), md.dev()))
|
.map(DeviceAndINode::from)
|
||||||
.map_err(|r| Error::from_io1("std::fs::symlink_metadata", "/", r))
|
.map_err(|r| Error::from_io1("std::fs::symlink_metadata", "/", r))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn root_dev_ino_check(
|
fn root_dev_ino_check(root_dev_ino: Option<DeviceAndINode>, dir_dev_ino: DeviceAndINode) -> bool {
|
||||||
root_dev_ino: Option<(libc::ino_t, libc::dev_t)>,
|
|
||||||
dir_dev_ino: (libc::ino_t, libc::dev_t),
|
|
||||||
) -> bool {
|
|
||||||
root_dev_ino.map_or(false, |root_dev_ino| root_dev_ino == dir_dev_ino)
|
root_dev_ino.map_or(false, |root_dev_ino| root_dev_ino == dir_dev_ino)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,9 @@ pub(crate) enum Error {
|
||||||
#[error("No files are specified")]
|
#[error("No files are specified")]
|
||||||
MissingFiles,
|
MissingFiles,
|
||||||
|
|
||||||
|
#[error("Data is out of range")]
|
||||||
|
OutOfRange,
|
||||||
|
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
ArgumentsMismatch(String),
|
ArgumentsMismatch(String),
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue