Skip to content

Commit 931136d

Browse files
committed
file: impl fn fn get_magic_files
1 parent 48ac570 commit 931136d

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

file/file.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -547,27 +547,18 @@ fn get_type_from_magic_file_dbs(test_file: &PathBuf, magic_file_dbs: &[PathBuf])
547547
})
548548
}
549549

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

566-
if path == "-" {
567-
path = String::new();
568-
io::stdin().read_line(&mut path).unwrap();
569-
path = path.trim().to_string();
570-
}
561+
let default_magic_file = PathBuf::from(DEFAULT_MAGIC_FILE);
571562

572563
if let Some(test_file2) = &args.test_file2 {
573564
magic_files.push(test_file2.clone());
@@ -579,24 +570,34 @@ fn analyze_file(mut path: String, args: &Args) {
579570

580571
if m_index > h_index {
581572
magic_files.push(args.test_file1.as_ref().unwrap().clone());
582-
magic_files.push(get_default_magic_file());
573+
magic_files.push(default_magic_file);
583574
} else {
584-
magic_files.push(get_default_magic_file());
575+
magic_files.push(default_magic_file);
585576
magic_files.push(args.test_file1.as_ref().unwrap().clone());
586577
}
587578
} else if args.test_file1.is_some() {
588579
magic_files.push(args.test_file1.as_ref().unwrap().clone());
589580
} else if args.default_tests {
590-
magic_files.push(get_default_magic_file());
581+
magic_files.push(default_magic_file);
591582
}
592583
} else if let Some(test_file1) = &args.test_file1 {
593584
magic_files.push(test_file1.clone());
594585

595586
if args.test_file2.is_none() && !args.default_tests {
596-
magic_files.push(get_default_magic_file());
587+
magic_files.push(default_magic_file);
597588
}
598589
} else {
599-
magic_files.push(get_default_magic_file());
590+
magic_files.push(default_magic_file);
591+
}
592+
593+
magic_files
594+
}
595+
596+
fn analyze_file(mut path: String, args: &Args, magic_files: &Vec<PathBuf>) {
597+
if path == "-" {
598+
path = String::new();
599+
io::stdin().read_line(&mut path).unwrap();
600+
path = path.trim().to_string();
600601
}
601602

602603
let met = match fs::symlink_metadata(&path) {
@@ -682,8 +683,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
682683
textdomain(PROJECT_NAME).unwrap();
683684
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
684685

686+
let magic_files = get_magic_files(&args);
687+
685688
for file in &args.files {
686-
analyze_file(file.clone(), &args);
689+
analyze_file(file.clone(), &args, &magic_files);
687690
}
688691

689692
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)