mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-12-26 01:57:32 +00:00
Prevent crash for filenames with no components
Now there are no more unwrap()s!
This commit is contained in:
parent
6d4b30d531
commit
10096d1043
14
src/file.rs
14
src/file.rs
@ -45,8 +45,18 @@ impl<'a> File<'a> {
|
|||||||
|
|
||||||
/// Create a new File object from the given Stat result, and other data.
|
/// Create a new File object from the given Stat result, and other data.
|
||||||
pub fn with_stat(stat: io::FileStat, path: &Path, parent: Option<&'a Dir>) -> File<'a> {
|
pub fn with_stat(stat: io::FileStat, path: &Path, parent: Option<&'a Dir>) -> File<'a> {
|
||||||
let v = path.filename().unwrap_or_else(|| panic!("Failed to get filename for {:?}", path)); // fails if / or . or ..
|
|
||||||
let filename = String::from_utf8_lossy(v);
|
// The filename to display is the last component of the path. However,
|
||||||
|
// the path has no components for `.`, `..`, and `/`, so in these
|
||||||
|
// cases, the entire path is used.
|
||||||
|
let bytes = match path.components().last() {
|
||||||
|
Some(b) => b,
|
||||||
|
None => path.as_vec(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Convert the string to UTF-8, replacing any invalid characters with
|
||||||
|
// replacement characters.
|
||||||
|
let filename = String::from_utf8_lossy(bytes);
|
||||||
|
|
||||||
File {
|
File {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
|
Loading…
Reference in New Issue
Block a user