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

Commit

Permalink
WIP: begin migration to MIME crate for types
Browse files Browse the repository at this point in the history
  • Loading branch information
aahancoc committed Feb 25, 2020
1 parent 84fd2bd commit fcbb959
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 86 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ petgraph = "^0.5"
nom = "^3"
lazy_static = "^1.4"
fnv = "^1"
mime = "^0.3"

tabwriter = { version = "^1", optional = true }
clap = { version = "^2", optional = true }
Expand Down
12 changes: 7 additions & 5 deletions src/basetype/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::Path;
use crate::{read_bytes};
use crate::{read_bytes, MIME};

/// If there are any null bytes, return False. Otherwise return True.
fn is_text_plain_from_u8(b: &[u8]) -> bool
Expand All @@ -18,9 +18,11 @@ fn is_text_plain_from_filepath(filepath: &Path) -> bool
}

#[allow(unused_variables)]
pub fn from_u8(b: &[u8], mimetype: &str) -> bool
pub fn from_u8(b: &[u8], mimetype: MIME) -> bool
{
if mimetype == "application/octet-stream" || mimetype == "all/allfiles" {
if mimetype == "application/octet-stream" ||
mimetype == "all/allfiles"
{
// Both of these are the case if we have a bytestream at all
return true;
} if mimetype == "text/plain" {
Expand All @@ -31,7 +33,7 @@ pub fn from_u8(b: &[u8], mimetype: &str) -> bool
}
}

pub fn from_filepath(filepath: &Path, mimetype: &str) -> bool
pub fn from_filepath(filepath: &Path, mimetype: MIME) -> bool
{
use std::fs;

Expand All @@ -42,7 +44,7 @@ pub fn from_filepath(filepath: &Path, mimetype: &str) -> bool
Err(_) => {return false;}
};

match mimetype {
match mimetype.to_string().as_str() {
"all/all" => return true,
"all/allfiles" | "application/octet-stream" => return meta.is_file(),
"inode/directory" => return meta.is_dir(),
Expand Down
10 changes: 5 additions & 5 deletions src/basetype/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use fnv::FnvHashMap;
use crate::MIME;

pub fn get_supported() -> Vec<MIME> {
super::TYPES.to_vec().iter().map(|x| x.to_string()).collect()
super::TYPES.to_vec().iter().map(|x| x.parse().unwrap()).collect()
}

/// Returns Vec of parent->child relations
pub fn get_subclasses() -> Vec<(MIME, MIME)> {
let mut res = Vec::<(MIME, MIME)>::new();

// There's probably a better way to do this.
res.push( ("all/all".to_string(), "all/allfiles".to_string()) );
res.push( ("all/all".to_string(), "inode/directory".to_string()) );
res.push( ("all/allfiles".to_string(), "application/octet-stream".to_string()) );
res.push( ("application/octet-stream".to_string(), "text/plain".to_string()) );
res.push( ("all/all".parse().unwrap(), "all/allfiles".parse().unwrap()) );
res.push( ("all/all".parse().unwrap(), "inode/directory".parse().unwrap()) );
res.push( ("all/allfiles".parse().unwrap(), "application/octet-stream".parse().unwrap()) );
res.push( ("application/octet-stream".parse().unwrap(), "text/plain".parse().unwrap()) );

res
}
Expand Down
9 changes: 5 additions & 4 deletions src/fdo_magic/builtin/check.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::path::Path;
use petgraph::prelude::*;
use crate::{fdo_magic, read_bytes};
use mime::Mime as MIME;

/// Test against all rules
#[allow(unused_variables)]
pub fn from_u8(file: &[u8], mimetype: &str) -> bool {
pub fn from_u8(file: &[u8], mimetype: MIME) -> bool {

// Get magic ruleset
let graph = match super::ALLRULES.get(mimetype) {
let graph = match super::ALLRULES.get(&mimetype) {
Some(item) => item,
None => return false // No rule for this mime
};
Expand All @@ -25,9 +26,9 @@ pub fn from_u8(file: &[u8], mimetype: &str) -> bool {
/// This only exists for the case of a direct match_filepath call
/// and even then we could probably get rid of this...
#[allow(unused_variables)]
pub fn from_filepath(filepath: &Path, mimetype: &str) -> bool{
pub fn from_filepath(filepath: &Path, mimetype: MIME) -> bool{
// Get magic ruleset
let magic_rules = match super::ALLRULES.get(mimetype) {
let magic_rules = match super::ALLRULES.get(&mimetype) {
Some(item) => item,
None => return false // No rule for this mime
};
Expand Down
8 changes: 4 additions & 4 deletions src/fdo_magic/builtin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn read_subclasses() -> Result<Vec<(MIME, MIME)>, std::io::Error> {
let mut subclasses = Vec::<(MIME, MIME)>::new();

for line in r.lines() {
let child = line.split_whitespace().nth(0).unwrap_or("").to_string();
let parent = line.split_whitespace().nth(1).unwrap_or("").to_string();
let child = line.split_whitespace().nth(0).unwrap_or("").parse().unwrap_or(mime::APPLICATION_OCTET_STREAM);
let parent = line.split_whitespace().nth(1).unwrap_or("").parse().unwrap_or(mime::APPLICATION_OCTET_STREAM);

subclasses.push( (parent, child) );
}
Expand All @@ -23,8 +23,8 @@ fn read_aliaslist() -> Result<FnvHashMap<MIME, MIME>, std::io::Error> {
let mut aliaslist = FnvHashMap::<MIME, MIME>::default();

for line in raliases.lines() {
let a = line.split_whitespace().nth(0).unwrap_or("").to_string();
let b = line.split_whitespace().nth(1).unwrap_or("").to_string();
let a = line.split_whitespace().nth(0).unwrap_or("").parse().unwrap_or(mime::APPLICATION_OCTET_STREAM);
let b = line.split_whitespace().nth(1).unwrap_or("").parse().unwrap_or(mime::APPLICATION_OCTET_STREAM);
aliaslist.insert(a,b);
}

Expand Down
3 changes: 2 additions & 1 deletion src/fdo_magic/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use petgraph::prelude::*;
use crate::{MIME};

fn from_u8_singlerule(file: &[u8], rule: &super::MagicRule) -> bool {

Expand Down Expand Up @@ -88,7 +89,7 @@ fn from_u8_singlerule(file: &[u8], rule: &super::MagicRule) -> bool {
/// TODO: Not loving the code duplication here.
pub fn from_u8_walker(
file: &[u8],
mimetype: &str,
mimetype: MIME,
graph: &DiGraph<super::MagicRule, u32>,
node: NodeIndex,
isroot: bool
Expand Down
2 changes: 1 addition & 1 deletion src/fdo_magic/ruleset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ named!(magic_rules<super::MagicRule>,
/// Singular magic entry
named!(magic_entry<(MIME, Vec<super::MagicRule>)>,
do_parse!(
_mime: do_parse!(ret: mime >> (ret.to_string())) >>
_mime: do_parse!(ret: mime >> (ret.parse().unwrap_or(mime::APPLICATION_OCTET_STREAM))) >>
_rules: many0!(magic_rules) >> (_mime, _rules)
)
);
Expand Down
Loading

0 comments on commit fcbb959

Please sign in to comment.