Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit 3495072

Browse files
committed
Added match feature to tmagic
1 parent 7606251 commit 3495072

File tree

7 files changed

+80
-34
lines changed

7 files changed

+80
-34
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ num_cpus = { version = "^1", optional = true }
2424
[features]
2525
cli = ["clap", "tabwriter", "scoped_threadpool", "walkdir", "num_cpus"]
2626
staticmime = [] # Use &'static str for output insead of String. (disabled for now)
27-
default = ["cli"]
27+
default = ["cli", "staticmime"]
2828

2929
[lib]
3030
crate-type = ["lib"]

src/.main.rs.kate-swp

-90 Bytes
Binary file not shown.

src/basetype.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ pub mod check {
114114
"all/allfiles" | "application/octet-stream" => return meta.is_file(),
115115
"inode/directory" => return meta.is_dir(),
116116
"text/plain" => return is_text_plain_from_filepath(filepath),
117-
_ => {
118-
println!("{}", mimetype);
119-
panic!("This mime is not supported by the mod. (See can_check)")
120-
}
117+
_ => return false
121118
}
122119

123120
}

src/fdo_magic/builtin.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ pub mod check {
112112

113113
/// Test against all rules
114114
pub fn from_u8(file: &[u8], mimetype: &str) -> bool {
115-
116-
/*// Get mimetype in case user provides alias
117-
let mimetype = match super::ALIASES.get(mimetype) {
118-
None => mimetype,
119-
Some(x) => x
120-
};*/
121115

122116
// Get magic ruleset
123117
let graph = match super::ALLRULES.get(mimetype) {
@@ -166,6 +160,13 @@ pub mod check {
166160
Err(_) => return false
167161
};
168162
let mut b = Vec::<u8>::with_capacity(scanlen);
163+
164+
// Fill up vector with something
165+
for i in 0..scanlen {
166+
let _ = i;
167+
b.push(0);
168+
}
169+
169170
match f.read_exact(&mut b) {
170171
Ok(_) => {},
171172
Err(_) => return false

src/fdo_magic/sys.rs

+7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ pub mod check {
187187
Err(_) => return false
188188
};
189189
let mut b = Vec::<u8>::with_capacity(scanlen);
190+
191+
// Fill up vector with something
192+
for i in 0..scanlen {
193+
let _ = i;
194+
b.push(0);
195+
}
196+
//
190197
match f.read_exact(&mut b) {
191198
Ok(_) => {},
192199
Err(_) => return false

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ fn get_alias(mimetype: &String) -> &String {
318318
fn match_u8_noalias(mimetype: &str, bytes: &[u8]) -> bool
319319
{
320320
match CHECKER_SUPPORT.get(mimetype) {
321-
None => {panic!("{} not supported", mimetype);},
321+
None => {false},
322322
Some(y) => (CHECKERS[*y].from_u8)(bytes, mimetype)
323323
}
324324
}
@@ -373,7 +373,7 @@ pub fn from_u8(bytes: &[u8]) -> Option<MIME>
373373
fn match_filepath_noalias(mimetype: &str, filepath: &str) -> bool
374374
{
375375
match CHECKER_SUPPORT.get(mimetype) {
376-
None => {panic!("{} not supported", mimetype);},
376+
None => {false},
377377
Some(y) => (CHECKERS[*y].from_filepath)(filepath, mimetype)
378378
}
379379
}

src/main.rs

+62-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
use clap::{Arg, App};
2727

2828
let args = App::new("TreeMagic")
29-
.version("0.1")
29+
.version("0.2.0")
3030
.about("Determines the MIME type of a file by traversing a filetype tree.")
3131
.arg(Arg::with_name("file")
3232
.required(true)
@@ -35,31 +35,40 @@ fn main() {
3535
)
3636
.arg(Arg::with_name("recursive")
3737
.short("r")
38+
.long("recursive")
3839
.help("Search directories recursively")
3940
)
4041
.arg(Arg::with_name("match")
4142
.short("m")
4243
.long("match")
4344
.use_delimiter(true)
4445
.takes_value(true)
46+
.require_equals(true)
4547
.help("Print only files that match given MIME")
4648
)
4749
.arg(Arg::with_name("ugly")
4850
.long("ugly")
4951
.help("Print results as they come in, at expense of tab alignment")
5052
)
5153
.get_matches();
54+
5255
let mut files: Vec<String> = args.values_of("file")
5356
.unwrap()
5457
.map(|x| x.to_string())
5558
.collect();
59+
let is_ugly = args.is_present("ugly");
60+
let is_recursive = args.is_present("recursive");
61+
let check_against: Vec<String> = args.values_of("match")
62+
.unwrap()
63+
.map(|x| x.to_string())
64+
.collect();
65+
println!("{:?}", check_against);
5666

5767
let mut tw = TabWriter::new(vec![]);
5868
let (tx, rx) = mpsc::channel();
5969

6070
// Get recursive results if needed
61-
if args.is_present("recursive") {
62-
//println!("Recursive!");
71+
if is_recursive {
6372
for dir in files.clone() {
6473
let entries = WalkDir::new(dir).into_iter().filter_map(|e| e.ok());
6574
for entry in entries {
@@ -70,29 +79,61 @@ fn main() {
7079
);
7180
}
7281
}
73-
//println!("{:#?}", files);
7482
}
83+
let files = files;
7584

7685
let mut pool = Pool::new(num_cpus::get() as u32);
77-
let is_ugly = args.is_present("ugly");
78-
79-
pool.scoped(|scope| {
80-
for x in files {
81-
let tx = tx.clone();
82-
scope.execute(move || {
83-
let result = tree_magic::from_filepath(x.as_str());
84-
let result = result.unwrap_or(convmime!("inode/none"));
85-
let result = format!("{}:\t{:?}", x, result);
86-
if is_ugly {
87-
println!("{}", result);
88-
} else {
89-
tx.send(result + "\n").unwrap_or_default();
90-
}
91-
});
92-
}
93-
});
86+
// Acquire results for non-match
87+
if check_against.is_empty(){
88+
pool.scoped(|scope| {
89+
for file in files {
90+
let tx = tx.clone();
91+
scope.execute(move || {
92+
let result = tree_magic::from_filepath(file.as_str());
93+
let result = result.unwrap_or(convmime!("inode/none"));
94+
let result = format!("{}:\t{:?}", file, result);
95+
if is_ugly {
96+
println!("{}", result);
97+
} else {
98+
tx.send(result + "\n").unwrap_or_default();
99+
}
100+
});
101+
}
102+
});
103+
// Acquire results for check against list of MIMES
104+
} else {
105+
pool.scoped(|scope| {
106+
for file in files {
107+
let tx = tx.clone();
108+
let check_against = check_against.clone();
109+
110+
scope.execute(move || {
111+
let mut result: Option<String> = None;
112+
113+
for mime in check_against {
114+
let out = tree_magic::match_filepath(mime.as_str(), file.as_str());
115+
if out {
116+
result = Some(mime);
117+
break;
118+
}
119+
}
120+
121+
if result.is_none() { return; }
122+
123+
let result = result.unwrap();
124+
let result = format!("{}:\t{:?}", file, result);
125+
if is_ugly {
126+
println!("{}", result);
127+
} else {
128+
tx.send(result + "\n").unwrap_or_default();
129+
}
130+
});
131+
}
132+
});
133+
}
94134
drop(tx);
95135

136+
// Pretty-print results
96137
if !is_ugly {
97138
let mut list: Vec<_> = rx.iter().collect();
98139
list.sort();

0 commit comments

Comments
 (0)