Repository: sxyazi/yazi
File: yazi-fs/src/provider/local/casefold.rs (Rust)

Code before:
	};

	// Fast path: if the `/proc/self/fd/N` matches
	if let Ok(p) = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))
		&& let path = path.as_os_str()
		&& let Some(b) = p.as_os_str().as_bytes().get(..path.len())
		&& b.eq_ignore_ascii_case(path.as_bytes())
	{
		let mut b = p.into_os_string().into_vec();
		b.truncate(path.len());
		return Ok(PathBuf::from(OsString::from_vec(b)));
	}

	// Fast path: if the file isn't a symlink
	let meta = file.metadata()?;
	if !meta.is_symlink()
		&& let Some(n) = path.canonicalize()?.file_name()
	{
		return Ok(parent.join(n));
	}

	// Fallback: scan the directory for matching inodes
	let mut names = vec![];
	for entry in std::fs::read_dir(parent)? {
		let entry = entry?;
		if let m = entry.metadata()?
			&& m.ino() == meta.ino()
			&& m.dev() == meta.dev()
		{
			names.push(entry.file_name());
		}
	}

	if let Some(i) = names.iter().position(|n| n == name) {
		// Exact match
		Ok(PathBuf::from(OsString::from_vec(cstr.into_bytes())))
	} else if names.len() == 1 {
		// No hardlink that shares the same inode
		Ok(parent.join(names[0]))
	} else if let mut it = names.iter().enumerate().filter(|&(_, n)| n.eq_ignore_ascii_case(name))
		&& let Some((i, _)) = it.next()
		&& it.next().is_none()
	{
		// Case-insensitive match
		Ok(parent.join(names[i]))
	} else {
		Err(io::Error::from(io::ErrorKind::NotFound))
	}
}

#[cfg(any(

Review comment (suggestion):
The `let` pattern is missing a binding name for the metadata variable. The expression `if let m = entry.metadata()?` should be `if let Ok(m) = entry.metadata()` to properly bind the metadata result.
```suggestion
		if let Ok(m) = entry.metadata()
```

Code after:
		ret => unsafe { File::from_raw_fd(ret) },
	};

	// Fast path: if the `/proc/self/fd/N` matches
	if let Ok(p) = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))
		&& let path = path.as_os_str()
		&& let Some(b) = p.as_os_str().as_bytes().get(..path.len())
		&& b.eq_ignore_ascii_case(path.as_bytes())
	{
		let mut b = p.into_os_string().into_vec();
		b.truncate(path.len());
		return Ok(PathBuf::from(OsString::from_vec(b)));
	}

	// Fast path: if the file isn't a symlink
	let meta = file.metadata()?;
	if !meta.is_symlink()
		&& let Some(n) = path.canonicalize()?.file_name()
	{
		return Ok(parent.join(n));
	}

	// Fallback: scan the directory for matching inodes
	let mut names = vec![];
	for entry in std::fs::read_dir(parent)? {
		let entry = entry?;
		let n = entry.file_name(); // TODO: use `file_name_ref()` when stabilized

		if n == name {
			return Ok(PathBuf::from(OsString::from_vec(cstr.into_bytes())));
		} else if let m = entry.metadata()?
			&& m.ino() == meta.ino()
			&& m.dev() == meta.dev()
		{
			names.push(n);
		}
	}

	if names.len() == 1 {
		// No hardlink that shares the same inode
		Ok(parent.join(&names[0]))
	} else if let mut it = names.iter().enumerate().filter(|&(_, n)| n.eq_ignore_ascii_case(name))
		&& let Some((i, _)) = it.next()
		&& it.next().is_none()
	{
		// Case-insensitive match
		Ok(parent.join(&names[i]))
	} else {
		Err(io::Error::from(io::ErrorKind::NotFound))
	}
}