Fix device reading (#329)

* Avoid panic when reading device files

* Skip devices in find

* Fix root path in find
This commit is contained in:
Vincent Ollivier 2022-04-15 13:00:33 +02:00 committed by GitHub
parent dab43ade5b
commit 512b60564b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View File

@ -38,6 +38,7 @@ pub enum Device {
impl From<u8> for Device { impl From<u8> for Device {
fn from(i: u8) -> Self { fn from(i: u8) -> Self {
match i { match i {
i if i == DeviceType::File as u8 => Device::File(File::new()),
i if i == DeviceType::Console as u8 => Device::Console(Console::new()), i if i == DeviceType::Console as u8 => Device::Console(Console::new()),
i if i == DeviceType::Random as u8 => Device::Random(Random::new()), i if i == DeviceType::Random as u8 => Device::Random(Random::new()),
i if i == DeviceType::Null as u8 => Device::Null, i if i == DeviceType::Null as u8 => Device::Null,

View File

@ -36,6 +36,16 @@ impl From<DirEntry> for File {
} }
impl File { impl File {
pub fn new() -> Self {
Self {
parent: None,
name: String::new(),
addr: 0,
size: 0,
offset:0,
}
}
pub fn create(pathname: &str) -> Option<Self> { pub fn create(pathname: &str) -> Option<Self> {
let pathname = realpath(pathname); let pathname = realpath(pathname);
let dirname = dirname(&pathname); let dirname = dirname(&pathname);

View File

@ -77,9 +77,15 @@ fn print_matching_lines(path: &str, pattern: &str, state: &mut PrintingState) {
if let Ok(files) = fs::read_dir(path) { if let Ok(files) = fs::read_dir(path) {
state.is_recursive = true; state.is_recursive = true;
for file in files { for file in files {
let file_path = format!("{}/{}", path, file.name()); let mut file_path = path.to_string();
if !file_path.ends_with('/') {
file_path.push('/');
}
file_path.push_str(&file.name());
if file.is_dir() { if file.is_dir() {
print_matching_lines(&file_path, pattern, state); print_matching_lines(&file_path, pattern, state);
} else if file.is_device() {
// Skip devices
} else { } else {
print_matching_lines_in_file(&file_path, pattern, state); print_matching_lines_in_file(&file_path, pattern, state);
} }