Skip to content
Merged
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
8 changes: 6 additions & 2 deletions src/uu/df/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//! filesystem mounted at a particular directory. It also includes
//! information on amount of space available and amount of space used.
// spell-checker:ignore canonicalized
use std::{ffi::OsString, path::Path};
use std::ffi::OsString;
#[cfg(unix)]
use std::path::Path;

use uucore::fsext::{FsUsage, MountInfo};

Expand Down Expand Up @@ -59,6 +61,7 @@ pub(crate) enum FsError {
///
/// * [`Path::canonicalize`]
/// * [`MountInfo::mount_dir`]
#[cfg(unix)]
fn mount_info_from_path<P>(
mounts: &[MountInfo],
path: P,
Expand Down Expand Up @@ -128,6 +131,7 @@ impl Filesystem {
/// * [`Path::canonicalize`]
/// * [`MountInfo::mount_dir`]
///
#[cfg(unix)]
pub(crate) fn from_path<P>(mounts: &[MountInfo], path: P) -> Result<Self, FsError>
where
P: AsRef<Path>,
Expand All @@ -141,7 +145,7 @@ impl Filesystem {
}
}

#[cfg(test)]
#[cfg(all(test, unix))]
mod tests {

mod mount_info_from_path {
Expand Down
34 changes: 24 additions & 10 deletions src/uu/df/src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! Windows backend of `df`: volume usage probes and the `-i` notice.
// spell-checker:ignore SUBST

//! Windows backend of `df`: volume usage probes, path resolution and the `-i` notice.

use std::ffi::OsString;
use std::path::Path;
Expand All @@ -21,13 +23,7 @@ pub(crate) fn sync() {}

/// Usage of the filesystem at `mount_info`, `None` if it cannot be queried.
pub(crate) fn fs_usage(mount_info: &MountInfo) -> Option<FsUsage> {
let stat_path = if mount_info.mount_dir.is_empty() {
// On windows, we expect the volume id
mount_info.dev_id.as_ref()
} else {
mount_info.mount_dir.as_os_str()
};
FsUsage::new(Path::new(stat_path)).ok()
FsUsage::new(Path::new(&mount_info.mount_dir)).ok()
}

/// Find and create the filesystem from the given mount.
Expand All @@ -39,7 +35,9 @@ pub(crate) fn filesystem_from_mount(
Filesystem::new(mount.clone(), file).ok_or(FsError::MountMissing)
}

/// Find and create the filesystem that contains `path` through the mount table.
/// Find and create the filesystem that contains `path`: the mount with the
/// longest directory prefixing it, or, when the mount table does not list it
/// (UNC paths, unreadable table), the volume root the OS reports for it.
pub(crate) fn filesystem_for_path<P>(
mounts: &[MountInfo],
_use_fallback: bool,
Expand All @@ -48,7 +46,23 @@ pub(crate) fn filesystem_for_path<P>(
where
P: AsRef<Path>,
{
Filesystem::from_path(mounts, path)
let path = path.as_ref();
let file = path.as_os_str().to_owned();
// Not `canonicalize`: it resolves SUBST drives and junctions away and
// yields `\\?\` prefixes that never match a mount directory.
let absolute = std::path::absolute(path).map_err(|_| FsError::InvalidPath)?;
absolute.metadata().map_err(|_| FsError::InvalidPath)?;
let longest = mounts
.iter()
.filter(|m| absolute.starts_with(&m.mount_dir))
.max_by_key(|m| m.mount_dir.len());
let mount_info = if let Some(mount_info) = longest {
mount_info.clone()
} else {
let root = uucore::fs::volume_path_name(&absolute).map_err(|_| FsError::MountMissing)?;
MountInfo::from_mount_dir(root.into_os_string())
};
Filesystem::new(mount_info, Some(file)).ok_or(FsError::MountMissing)
Comment on lines +49 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what this does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old Filesystem::from_path(mounts, path) was unix only and I didn't want to introduce a windows fix there

}

/// `-i` is not supported: say so and stop successfully.
Expand Down
4 changes: 3 additions & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ windows-sys = { workspace = true, optional = true, default-features = false, fea
"Win32_Storage_FileSystem",
"Win32_Foundation",
"Win32_Globalization",
"Win32_NetworkManagement_WNet",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_JobObjects",
Expand All @@ -144,7 +146,7 @@ entries = ["libc", "rustix/fs", "rustix/process"]
extendedbigdecimal = ["bigdecimal", "num-traits"]
fast-inc = []
fs = ["dunce", "libc", "rustix/fs", "windows-sys"]
fsext = ["libc", "windows-sys", "bstr"]
fsext = ["libc", "windows-sys", "bstr", "wide"]
fsxattr = ["xattr", "itertools", "libc"]
hardware = []
lines = []
Expand Down
Loading
Loading