Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file: cleanups fn analyze_file #270

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
file: cleanups fn analyze_file
  • Loading branch information
fox0 committed Sep 28, 2024
commit e27e3b54457536155ece9f6748bffc40bede92f8
115 changes: 64 additions & 51 deletions file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,65 +599,78 @@ fn analyze_file(mut path: String, args: &Args) {
magic_files.push(get_default_magic_file());
}

match fs::symlink_metadata(&path) {
Ok(met) => {
let file_type = met.file_type();
let met = match fs::symlink_metadata(&path) {
Ok(met) => met,
Err(_) => {
println!("{path}: cannot open");
return;
}
};

if file_type.is_symlink() {
if args.identify_as_symbolic_link {
println!("{path}: symbolic link");
let file_type = met.file_type();

if file_type.is_symlink() {
if args.identify_as_symbolic_link {
println!("{path}: symbolic link");
return;
}
match read_link(&path) {
Ok(file_p) => {
// trace the file pointed by symbolic link
if file_p.exists() {
println!("{path}: symbolic link to {}", file_p.to_str().unwrap());
} else {
match read_link(&path) {
Ok(file_p) => {
// trace the file pointed by symbolic link
if file_p.exists() {
println!("{path}: symbolic link to {}", file_p.to_str().unwrap());
} else {
println!(
"{path}: broken symbolic link to {}",
file_p.to_str().unwrap()
);
}
}
Err(_) => {
println!("{path}: symbolic link");
}
}
println!(
"{path}: broken symbolic link to {}",
file_p.to_str().unwrap()
);
}
} else if file_type.is_char_device() {
println!("{path}: character special");
} else if file_type.is_dir() {
println!("{path}: directory");
} else if file_type.is_fifo() {
println!("{path}: fifo");
} else if file_type.is_socket() {
println!("{path}: socket");
}
if file_type.is_block_device() {
println!("{path}: block special");
} else if file_type.is_file() {
if args.no_further_file_classification {
println!("{path}: regular file");
} else {
if met.len() == 0 {
println!("{path}: empty");
} else {
match get_type_from_magic_file_dbs(&PathBuf::from(&path), &magic_files) {
Some(f_type) => {
println!("{path}: {f_type}");
}
None => {
println!("{path}: data");
}
}
}
}
Err(_) => {
println!("{path}: symbolic link");
}
}
Err(_) => {
println!("{path}: cannot open");
return;
}
if file_type.is_char_device() {
println!("{path}: character special");
return;
}
if file_type.is_dir() {
println!("{path}: directory");
return;
}
if file_type.is_fifo() {
println!("{path}: fifo");
return;
}
if file_type.is_socket() {
println!("{path}: socket");
return;
}
if file_type.is_block_device() {
println!("{path}: block special");
return;
}
if file_type.is_file() {
if args.no_further_file_classification {
println!("{path}: regular file");
return;
}
if met.len() == 0 {
println!("{path}: empty");
return;
}
match get_type_from_magic_file_dbs(&PathBuf::from(&path), &magic_files) {
Some(f_type) => {
println!("{path}: {f_type}");
}
None => {
println!("{path}: data");
}
}
}
unreachable!();
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
Loading