Skip to content

Commit

Permalink
Fix device reading (#329)
Browse files Browse the repository at this point in the history
* Avoid panic when reading device files

* Skip devices in find

* Fix root path in find
  • Loading branch information
vinc authored Apr 15, 2022
1 parent dab43ad commit 512b605
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum Device {
impl From<u8> for Device {
fn from(i: u8) -> Self {
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::Random as u8 => Device::Random(Random::new()),
i if i == DeviceType::Null as u8 => Device::Null,
Expand Down
10 changes: 10 additions & 0 deletions src/sys/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ impl From<DirEntry> for 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> {
let pathname = realpath(pathname);
let dirname = dirname(&pathname);
Expand Down
8 changes: 7 additions & 1 deletion src/usr/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ fn print_matching_lines(path: &str, pattern: &str, state: &mut PrintingState) {
if let Ok(files) = fs::read_dir(path) {
state.is_recursive = true;
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() {
print_matching_lines(&file_path, pattern, state);
} else if file.is_device() {
// Skip devices
} else {
print_matching_lines_in_file(&file_path, pattern, state);
}
Expand Down

0 comments on commit 512b605

Please sign in to comment.