This commit is contained in:
sxyazi 2025-01-22 20:30:15 +08:00
parent 5d10a60f25
commit f4483c989c
No known key found for this signature in database
2 changed files with 10 additions and 6 deletions

View file

@ -3,7 +3,7 @@ use std::{borrow::Cow, collections::{HashMap, HashSet}, ffi::{OsStr, OsString},
use anyhow::Result; use anyhow::Result;
use tokio::{io::{Interest, unix::AsyncFd}, time::sleep}; use tokio::{io::{Interest, unix::AsyncFd}, time::sleep};
use tracing::error; use tracing::error;
use yazi_shared::{replace_cow, replace_vec}; use yazi_shared::{replace_cow, replace_vec_cow};
use super::{Locked, Partition, Partitions}; use super::{Locked, Partition, Partitions};
@ -129,9 +129,13 @@ impl Partitions {
let mut map = HashMap::new(); let mut map = HashMap::new();
for entry in std::fs::read_dir("/dev/disk/by-label")?.flatten() { for entry in std::fs::read_dir("/dev/disk/by-label")?.flatten() {
let meta = std::fs::metadata(entry.path())?; let meta = std::fs::metadata(entry.path())?;
let name = entry.file_name();
map.insert( map.insert(
(meta.dev(), meta.ino()), (meta.dev(), meta.ino()),
OsString::from_vec(replace_vec(entry.file_name().into_vec(), br"\x20", b" ")), match replace_vec_cow(name.as_bytes(), br"\x20", b" ") {
Cow::Borrowed(_) => name,
Cow::Owned(v) => OsString::from_vec(v),
},
); );
} }
Ok(map) Ok(map)

View file

@ -64,9 +64,9 @@ fn replace_cow_impl<'s>(
result + unsafe { src.get_unchecked(last..) } result + unsafe { src.get_unchecked(last..) }
} }
pub fn replace_vec(v: Vec<u8>, from: &[u8], to: &[u8]) -> Vec<u8> { pub fn replace_vec_cow<'a>(v: &'a [u8], from: &[u8], to: &[u8]) -> Cow<'a, [u8]> {
let mut it = memchr::memmem::find_iter(&v, from); let mut it = memchr::memmem::find_iter(v, from);
let Some(mut last) = it.next() else { return v }; let Some(mut last) = it.next() else { return Cow::Borrowed(v) };
let mut out = Vec::with_capacity(v.len()); let mut out = Vec::with_capacity(v.len());
out.extend_from_slice(&v[..last]); out.extend_from_slice(&v[..last]);
@ -80,7 +80,7 @@ pub fn replace_vec(v: Vec<u8>, from: &[u8], to: &[u8]) -> Vec<u8> {
} }
out.extend_from_slice(&v[last..]); out.extend_from_slice(&v[last..]);
out Cow::Owned(out)
} }
pub fn replace_to_printable(s: &[String], tab_size: u8) -> String { pub fn replace_to_printable(s: &[String], tab_size: u8) -> String {