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

Commit 2d9ce48

Browse files
committed
Remove cache and static_mime features
attempting to simplify this some
1 parent 471f86f commit 2d9ce48

File tree

5 files changed

+41
-218
lines changed

5 files changed

+41
-218
lines changed

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ petgraph = "^0.5"
1515
nom = "^3"
1616
lazy_static = "^1.4"
1717
fnv = "^1"
18-
parking_lot = "^0.10"
1918

2019
tabwriter = { version = "^1", optional = true }
2120
clap = { version = "^2", optional = true }
@@ -25,7 +24,6 @@ num_cpus = { version = "^1", optional = true }
2524

2625
[features]
2726
cli = ["clap", "tabwriter", "scoped_threadpool", "walkdir", "num_cpus"]
28-
staticmime = [] # Use &'static str for output insead of String. (disabled for now)
2927
default = []
3028

3129
[lib]

src/basetype.rs

+14-30
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ const TYPES: [&'static str; 5] =
1010
"application/octet-stream"
1111
];
1212

13-
/// Hold metadata in cache
14-
pub type Cache = std::fs::Metadata;
15-
16-
pub mod init {
17-
13+
pub mod init
14+
{
1815
extern crate fnv;
1916
use fnv::FnvHashMap;
2017
use MIME;
@@ -64,29 +61,26 @@ pub mod init {
6461
pub mod check {
6562

6663
extern crate std;
67-
extern crate parking_lot;
6864
use std::path::Path;
69-
use super::super::{Cache, CacheItem, slurp_to_cache};
65+
use super::super::{read_bytes};
7066

7167
/// If there are any null bytes, return False. Otherwise return True.
7268
fn is_text_plain_from_u8(b: &[u8]) -> bool {
7369
b.iter().filter(|&x| *x == 0).count() == 0
7470
}
7571

7672
// TODO: Hoist the main logic here somewhere else. This'll get redundant fast!
77-
fn is_text_plain_from_filepath(filepath: &Path, filecache: &CacheItem) -> bool {
78-
79-
let b = match slurp_to_cache(filepath, filecache, 512) {
73+
fn is_text_plain_from_filepath(filepath: &Path) -> bool
74+
{
75+
let b = match read_bytes(filepath, 512) {
8076
Ok(x) => x,
8177
Err(_) => return false
8278
};
8379
is_text_plain_from_u8(b.as_slice())
8480
}
8581

8682
#[allow(unused_variables)]
87-
pub fn from_u8(
88-
b: &[u8], mimetype: &str, cache: &CacheItem, filecache: &CacheItem
89-
) -> bool {
83+
pub fn from_u8(b: &[u8], mimetype: &str) -> bool {
9084
if mimetype == "application/octet-stream" || mimetype == "all/allfiles" {
9185
// Both of these are the case if we have a bytestream at all
9286
return true;
@@ -98,32 +92,22 @@ pub mod check {
9892
}
9993
}
10094

101-
pub fn from_filepath(
102-
filepath: &Path, mimetype: &str, cache: &CacheItem, filecache: &CacheItem
103-
) -> bool{
95+
pub fn from_filepath(filepath: &Path, mimetype: &str) -> bool{
10496

10597
use std::fs;
106-
//assert_eq!(0, std::sync::Arc::strong_count(&filecache));
10798

108-
if cache.read().is_none() {
109-
// Being bad with error handling here,
110-
// but if you can't open it it's probably not a file.
111-
let mut meta = cache.write();
112-
*meta = match fs::metadata(filepath) {
113-
Ok(x) => Some(Cache::Basetype(x)),
114-
Err(_) => {return false;}
115-
};
116-
}
117-
let meta = match cache.read().clone().unwrap() {
118-
Cache::Basetype(x) => {x},
119-
_ => {panic!("Invalid cache type (must be basetype)!");}
99+
// Being bad with error handling here,
100+
// but if you can't open it it's probably not a file.
101+
let meta = match fs::metadata(filepath) {
102+
Ok(x) => x,
103+
Err(_) => {return false;}
120104
};
121105

122106
match mimetype {
123107
"all/all" => return true,
124108
"all/allfiles" | "application/octet-stream" => return meta.is_file(),
125109
"inode/directory" => return meta.is_dir(),
126-
"text/plain" => return is_text_plain_from_filepath(filepath, filecache),
110+
"text/plain" => return is_text_plain_from_filepath(filepath),
127111
_ => return false
128112
}
129113
}

src/fdo_magic/builtin.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,12 @@ pub mod check {
112112
extern crate petgraph;
113113
use std::path::Path;
114114
use petgraph::prelude::*;
115+
use super::super::super::{read_bytes};
115116
use fdo_magic;
116-
use super::super::super::{CacheItem, slurp_to_cache};
117117

118118
/// Test against all rules
119119
#[allow(unused_variables)]
120-
pub fn from_u8(
121-
file: &[u8], mimetype: &str, cache: &CacheItem, filecache: &CacheItem
122-
) -> bool {
120+
pub fn from_u8(file: &[u8], mimetype: &str) -> bool {
123121

124122
// Get magic ruleset
125123
let graph = match super::ALLRULES.get(mimetype) {
@@ -140,9 +138,7 @@ pub mod check {
140138
/// This only exists for the case of a direct match_filepath call
141139
/// and even then we could probably get rid of this...
142140
#[allow(unused_variables)]
143-
pub fn from_filepath(
144-
filepath: &Path, mimetype: &str, cache: &CacheItem, filecache: &CacheItem
145-
) -> bool{
141+
pub fn from_filepath(filepath: &Path, mimetype: &str) -> bool{
146142
// Get magic ruleset
147143
let magic_rules = match super::ALLRULES.get(mimetype) {
148144
Some(item) => item,
@@ -163,11 +159,11 @@ pub mod check {
163159
}
164160
}
165161

166-
let b = match slurp_to_cache(filepath, filecache, scanlen) {
162+
let b = match read_bytes(filepath, scanlen) {
167163
Ok(x) => x,
168164
Err(_) => return false
169165
};
170166

171-
from_u8(b.as_slice(), mimetype, cache, &CacheItem::default())
167+
from_u8(b.as_slice(), mimetype)
172168
}
173169
}

0 commit comments

Comments
 (0)