handle symlinks to folders separately

This commit is contained in:
Crispy 2023-08-17 12:12:18 +02:00
parent 50f2c4255b
commit cc0dfa4c06

View file

@ -1,4 +1,4 @@
use std::fs::DirEntry;
use std::fs::{read_link, DirEntry};
use std::path::PathBuf;
use eframe::{egui, Frame, NativeOptions};
@ -31,7 +31,9 @@ struct Entry {
enum FileType {
Dir,
File,
SymLink, // todo separate symlinks to directories and files
SymLinkFile,
SymLinkDir,
SymLinkMissing,
Unknown, // e.g. device files on linux
}
@ -61,7 +63,7 @@ impl FileGui {
}
fn enter_input(&mut self) {
let path = self.path_input.replace("~", &self.home).replace('\\', "/");
let path = self.path_input.replace('~', &self.home).replace('\\', "/");
self.current_dir = PathBuf::from(path);
}
@ -95,7 +97,7 @@ impl FileGui {
fn open(&mut self, index: usize) {
let entry = &self.entries[index];
match entry.filetype {
FileType::Dir => {
FileType::Dir | FileType::SymLinkDir => {
self.current_dir = entry.path.clone();
self.refresh_entries();
self.override_input();
@ -145,7 +147,9 @@ impl eframe::App for FileGui {
ui.label(match entry.filetype {
FileType::Dir => "🗁",
FileType::File => "🗋",
FileType::SymLink => "",
FileType::SymLinkFile => "",
FileType::SymLinkDir => "",
FileType::SymLinkMissing => "",
FileType::Unknown => "",
});
});
@ -173,7 +177,14 @@ impl Entry {
} else if filetype.is_file() {
FileType::File
} else if filetype.is_symlink() {
FileType::SymLink
let p = read_link(&path).ok()?;
if p.is_dir() {
FileType::SymLinkDir
} else if p.exists() {
FileType::SymLinkFile
} else {
FileType::SymLinkMissing
}
} else {
FileType::Unknown
};
@ -186,6 +197,6 @@ impl Entry {
}
fn is_dir(&self) -> bool {
self.filetype == FileType::Dir
self.filetype == FileType::Dir || self.filetype == FileType::SymLinkDir
}
}