Skip to content

Instantly share code, notes, and snippets.

@jgarzik
Created April 21, 2024 02:10
Show Gist options
  • Save jgarzik/25832bf69471bab34675443d558b4873 to your computer and use it in GitHub Desktop.
Save jgarzik/25832bf69471bab34675443d558b4873 to your computer and use it in GitHub Desktop.
file(1) magic file patch
diff --git a/file/src/file.rs b/file/src/file.rs
index 39c0aeb..fadce24 100644
--- a/file/src/file.rs
+++ b/file/src/file.rs
@@ -25,6 +25,10 @@ use std::{
#[derive(Parser, Debug)]
#[command(author, version, about, long_about, disable_help_flag = true)]
struct Args {
+ /// Restore --help
+ #[arg(long, action = clap::ArgAction::HelpLong)]
+ help: Option<bool>,
+
/// Apply default position-sensitive system tests and context-sensitive system tests to the file.
#[arg(short = 'd', long)]
default_tests: bool,
@@ -45,6 +49,10 @@ struct Args {
#[arg(short = 'M')]
test_file2: Option<PathBuf>,
+ /// File containing data about many different file types
+ #[arg(long, default_value = "/etc/magic")]
+ magictext: PathBuf,
+
files: Vec<String>,
}
@@ -523,21 +531,10 @@ fn get_type_from_magic_file_dbs(test_file: &PathBuf, magic_file_dbs: &[PathBuf])
})
}
-/// Get the default raw(text based) magic file
-fn get_default_magic_file() -> PathBuf {
- #[cfg(target_os = "macos")]
- {
- PathBuf::from("/usr/share/file/magic/magic")
- }
- #[cfg(not(target_os = "macos"))]
- {
- PathBuf::from("/etc/magic")
- }
-}
-
fn analyze_file(mut path: String, args: &Args) {
// set priority according to the occurence of flags in the args lowest index will get highest priority
let mut magic_files: Vec<PathBuf> = Vec::new();
+ let default_magic_file = args.magictext.clone();
if path == "-" {
path = String::new();
@@ -555,24 +552,24 @@ fn analyze_file(mut path: String, args: &Args) {
if m_index > h_index {
magic_files.push(args.test_file1.as_ref().unwrap().clone());
- magic_files.push(get_default_magic_file());
+ magic_files.push(default_magic_file);
} else {
- magic_files.push(get_default_magic_file());
+ magic_files.push(default_magic_file);
magic_files.push(args.test_file1.as_ref().unwrap().clone());
}
} else if args.test_file1.is_some() {
magic_files.push(args.test_file1.as_ref().unwrap().clone());
} else if args.default_tests {
- magic_files.push(get_default_magic_file());
+ magic_files.push(default_magic_file);
}
} else if let Some(test_file1) = &args.test_file1 {
magic_files.push(test_file1.clone());
if args.test_file2.is_none() && !args.default_tests {
- magic_files.push(get_default_magic_file());
+ magic_files.push(default_magic_file);
}
} else {
- magic_files.push(get_default_magic_file());
+ magic_files.push(default_magic_file);
}
match fs::symlink_metadata(&path) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment