Skip to content

Commit 7adbccd

Browse files
authored
Merge pull request #270 from fox0/file
file: cleanups fn analyze_file
2 parents b489e99 + 931136d commit 7adbccd

File tree

2 files changed

+99
-86
lines changed

2 files changed

+99
-86
lines changed

file/file.rs

Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -549,27 +549,18 @@ fn get_type_from_magic_file_dbs(test_file: &PathBuf, magic_file_dbs: &[PathBuf])
549549
})
550550
}
551551

552-
/// Get the default raw(text based) magic file
553-
fn get_default_magic_file() -> PathBuf {
554-
#[cfg(target_os = "macos")]
555-
{
556-
PathBuf::from("/usr/share/file/magic/magic")
557-
}
558-
#[cfg(not(target_os = "macos"))]
559-
{
560-
PathBuf::from("/etc/magic")
561-
}
562-
}
563-
564-
fn analyze_file(mut path: String, args: &Args) {
552+
#[cfg(target_os = "macos")]
553+
/// Default raw (text based) magic file
554+
const DEFAULT_MAGIC_FILE: &str = "/usr/share/file/magic/magic";
555+
#[cfg(not(target_os = "macos"))]
556+
/// Default raw (text based) magic file
557+
const DEFAULT_MAGIC_FILE: &str = "/etc/magic";
558+
559+
fn get_magic_files(args: &Args) -> Vec<PathBuf> {
565560
// set priority according to the occurence of flags in the args lowest index will get highest priority
566561
let mut magic_files: Vec<PathBuf> = Vec::new();
567562

568-
if path == "-" {
569-
path = String::new();
570-
io::stdin().read_line(&mut path).unwrap();
571-
path = path.trim().to_string();
572-
}
563+
let default_magic_file = PathBuf::from(DEFAULT_MAGIC_FILE);
573564

574565
if let Some(test_file2) = &args.test_file2 {
575566
magic_files.push(test_file2.clone());
@@ -581,85 +572,109 @@ fn analyze_file(mut path: String, args: &Args) {
581572

582573
if m_index > h_index {
583574
magic_files.push(args.test_file1.as_ref().unwrap().clone());
584-
magic_files.push(get_default_magic_file());
575+
magic_files.push(default_magic_file);
585576
} else {
586-
magic_files.push(get_default_magic_file());
577+
magic_files.push(default_magic_file);
587578
magic_files.push(args.test_file1.as_ref().unwrap().clone());
588579
}
589580
} else if args.test_file1.is_some() {
590581
magic_files.push(args.test_file1.as_ref().unwrap().clone());
591582
} else if args.default_tests {
592-
magic_files.push(get_default_magic_file());
583+
magic_files.push(default_magic_file);
593584
}
594585
} else if let Some(test_file1) = &args.test_file1 {
595586
magic_files.push(test_file1.clone());
596587

597588
if args.test_file2.is_none() && !args.default_tests {
598-
magic_files.push(get_default_magic_file());
589+
magic_files.push(default_magic_file);
599590
}
600591
} else {
601-
magic_files.push(get_default_magic_file());
592+
magic_files.push(default_magic_file);
593+
}
594+
595+
magic_files
596+
}
597+
598+
fn analyze_file(mut path: String, args: &Args, magic_files: &Vec<PathBuf>) {
599+
if path == "-" {
600+
path = String::new();
601+
io::stdin().read_line(&mut path).unwrap();
602+
path = path.trim().to_string();
602603
}
603604

604-
match fs::symlink_metadata(&path) {
605-
Ok(met) => {
606-
let file_type = met.file_type();
605+
let met = match fs::symlink_metadata(&path) {
606+
Ok(met) => met,
607+
Err(_) => {
608+
println!("{path}: cannot open");
609+
return;
610+
}
611+
};
612+
613+
let file_type = met.file_type();
607614

608-
if file_type.is_symlink() {
609-
if args.identify_as_symbolic_link {
610-
println!("{path}: symbolic link");
615+
if file_type.is_symlink() {
616+
if args.identify_as_symbolic_link {
617+
println!("{path}: symbolic link");
618+
return;
619+
}
620+
match read_link(&path) {
621+
Ok(file_p) => {
622+
// trace the file pointed by symbolic link
623+
if file_p.exists() {
624+
println!("{path}: symbolic link to {}", file_p.to_str().unwrap());
611625
} else {
612-
match read_link(&path) {
613-
Ok(file_p) => {
614-
// trace the file pointed by symbolic link
615-
if file_p.exists() {
616-
println!("{path}: symbolic link to {}", file_p.to_str().unwrap());
617-
} else {
618-
println!(
619-
"{path}: broken symbolic link to {}",
620-
file_p.to_str().unwrap()
621-
);
622-
}
623-
}
624-
Err(_) => {
625-
println!("{path}: symbolic link");
626-
}
627-
}
626+
println!(
627+
"{path}: broken symbolic link to {}",
628+
file_p.to_str().unwrap()
629+
);
628630
}
629-
} else if file_type.is_char_device() {
630-
println!("{path}: character special");
631-
} else if file_type.is_dir() {
632-
println!("{path}: directory");
633-
} else if file_type.is_fifo() {
634-
println!("{path}: fifo");
635-
} else if file_type.is_socket() {
636-
println!("{path}: socket");
637631
}
638-
if file_type.is_block_device() {
639-
println!("{path}: block special");
640-
} else if file_type.is_file() {
641-
if args.no_further_file_classification {
642-
println!("{path}: regular file");
643-
} else {
644-
if met.len() == 0 {
645-
println!("{path}: empty");
646-
} else {
647-
match get_type_from_magic_file_dbs(&PathBuf::from(&path), &magic_files) {
648-
Some(f_type) => {
649-
println!("{path}: {f_type}");
650-
}
651-
None => {
652-
println!("{path}: data");
653-
}
654-
}
655-
}
656-
}
632+
Err(_) => {
633+
println!("{path}: symbolic link");
657634
}
658635
}
659-
Err(_) => {
660-
println!("{path}: cannot open");
636+
return;
637+
}
638+
if file_type.is_char_device() {
639+
println!("{path}: character special");
640+
return;
641+
}
642+
if file_type.is_dir() {
643+
println!("{path}: directory");
644+
return;
645+
}
646+
if file_type.is_fifo() {
647+
println!("{path}: fifo");
648+
return;
649+
}
650+
if file_type.is_socket() {
651+
println!("{path}: socket");
652+
return;
653+
}
654+
if file_type.is_block_device() {
655+
println!("{path}: block special");
656+
return;
657+
}
658+
if file_type.is_file() {
659+
if args.no_further_file_classification {
660+
println!("{path}: regular file");
661+
return;
661662
}
663+
if met.len() == 0 {
664+
println!("{path}: empty");
665+
return;
666+
}
667+
match get_type_from_magic_file_dbs(&PathBuf::from(&path), &magic_files) {
668+
Some(f_type) => {
669+
println!("{path}: {f_type}");
670+
}
671+
None => {
672+
println!("{path}: data");
673+
}
674+
}
675+
return;
662676
}
677+
unreachable!();
663678
}
664679

665680
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -670,8 +685,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
670685
textdomain(PROJECT_NAME).unwrap();
671686
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
672687

688+
let magic_files = get_magic_files(&args);
689+
673690
for file in &args.files {
674-
analyze_file(file.clone(), &args);
691+
analyze_file(file.clone(), &args, &magic_files);
675692
}
676693

677694
Ok(())

file/tests/file/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ fn file_is_a_directory() {
3838
file_test(&[file], &format!("{file}: directory\n"), "");
3939
}
4040

41+
#[test]
42+
fn file_is_a_character_special() {
43+
let file = "/dev/null";
44+
45+
file_test(&[file], &format!("{file}: character special\n"), "");
46+
}
47+
4148
#[test]
4249
fn file_is_an_empty_file() {
4350
let file = "tests/file/empty_file.txt";
@@ -120,17 +127,6 @@ fn file_file_is_a_broken_sym_link() {
120127
remove_file(broken_sym_link).unwrap()
121128
}
122129

123-
#[test]
124-
fn file_is_a_character_special() {
125-
let file = PathBuf::from("/dev/null");
126-
127-
file_test(
128-
&[file.to_str().unwrap()],
129-
&format!("{}: character special\n", file.to_str().unwrap()),
130-
"",
131-
);
132-
}
133-
134130
#[test]
135131
fn file_symlink_with_h_flag_for_both_valid_and_broken_symlink() {
136132
use std::env;

0 commit comments

Comments
 (0)