summaryrefslogtreecommitdiff
path: root/support/pkgcheck/src/filemagic.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/pkgcheck/src/filemagic.rs')
-rw-r--r--support/pkgcheck/src/filemagic.rs53
1 files changed, 28 insertions, 25 deletions
diff --git a/support/pkgcheck/src/filemagic.rs b/support/pkgcheck/src/filemagic.rs
index aa9c50feda..f3a4bcc391 100644
--- a/support/pkgcheck/src/filemagic.rs
+++ b/support/pkgcheck/src/filemagic.rs
@@ -21,10 +21,10 @@ use unicode_bom::Bom;
#[derive(Debug, PartialEq)]
pub enum LineEnding {
- LF,
- CR,
- CRLF,
- MIXED(usize, usize, usize),
+ Lf,
+ Cr,
+ Crlf,
+ Mixed(usize, usize, usize),
}
#[derive(Debug, PartialEq)]
@@ -45,7 +45,7 @@ pub enum Mimetype {
Socket,
Zerofile,
VeryShort,
- BOM(Bom),
+ Bom(Bom),
}
pub struct Filetype {
@@ -83,7 +83,7 @@ fn _is_crlf(buffer: &[u8], len: usize) -> bool {
}
//println!("cr: {}, lf: {}", cr, lf);
- // Heuristics: we accept if only a few lines are not CRLF
+ // Heuristics: we accept if only a few lines are not Crlf
match (cr, lf) {
(0, _lf) => return false,
(_cr, 0) => return true,
@@ -120,17 +120,17 @@ fn is_crlf(buffer: &[u8], len: usize) -> LineEnding {
seen_cr = *c == CR;
}
- // println!("LF / CR / CRLF: {} / {} / {}", n_lf, n_cr, n_crlf);
+ // println!("Lf / Cr / Crlf: {} / {} / {}", n_lf, n_cr, n_crlf);
// println!("cr: {}, lf: {}, crlf: {}", n_cr, n_lf, n_crlf);
// if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0)
// --> no line terminators
match (n_cr, n_lf, n_crlf) {
- (0, 0, z) if z > 0 => LineEnding::CRLF,
- (x, 0, 0) if x > 0 => LineEnding::CR,
- (0, y, 0) if y > 0 => LineEnding::LF,
- (x, y, z) => LineEnding::MIXED(x, y, z),
+ (0, 0, z) if z > 0 => LineEnding::Crlf,
+ (x, 0, 0) if x > 0 => LineEnding::Cr,
+ (0, y, 0) if y > 0 => LineEnding::Lf,
+ (x, y, z) => LineEnding::Mixed(x, y, z),
}
}
@@ -194,7 +194,7 @@ impl Filetype {
let bom: Bom = Bom::from(&self.buffer[0..]);
if bom != Bom::Null {
- return Ok(Mimetype::BOM(bom));
+ return Ok(Mimetype::Bom(bom));
}
if is_binary_data(&self.buffer, bytes_read) {
@@ -231,13 +231,16 @@ impl Filetype {
let crlf = is_crlf(&self.buffer, bytes_read);
//println!("{:?}", crlf);
+ // checks for
+ // - shebang
+ // - php indicator
if bytes_read >= 5 && (self.buffer.starts_with(b"#!") || self.buffer.starts_with(b"<?php"))
{
return Ok(Mimetype::Script(crlf));
}
// recognize files as script files by file endings
- let is_script = Regex::new(r"\.(csh|ksh|py|pl|sh|tcsh)$")
+ let is_script = Regex::new(r"\.(csh|ksh|py|pl|sh|tcsh|lua)$")
.unwrap()
.is_match(fname.as_bytes());
@@ -248,13 +251,13 @@ impl Filetype {
Ok(Mimetype::Text(crlf))
}
// match (crlf, is_script) {
- // (LineEnding::LF, false) => Ok(Mimetype::Text(LineEnding::LF)),
- // (LineEnding::CR, false) => Ok(Mimetype::Text(LineEnding::CR)),
- // (LineEnding::CRLF, false) => Ok(Mimetype::Text(LineEnding::CRLF)),
- // (LineEnding::LF, true) => Ok(Mimetype::Script(LineEnding::LF)),
- // (LineEnding::CR, true) => Ok(Mimetype::Script(LineEnding::CR)),
- // (LineEnding::CRLF, true) => Ok(Mimetype::Script(LineEnding::CRLF)),
- // (_, _) => Ok(Mimetype::Text(LineEnding::LF)),
+ // (LineEnding::Lf, false) => Ok(Mimetype::Text(LineEnding::Lf)),
+ // (LineEnding::Cr, false) => Ok(Mimetype::Text(LineEnding::Cr)),
+ // (LineEnding::Crlf, false) => Ok(Mimetype::Text(LineEnding::Crlf)),
+ // (LineEnding::Lf, true) => Ok(Mimetype::Script(LineEnding::Lf)),
+ // (LineEnding::Cr, true) => Ok(Mimetype::Script(LineEnding::Cr)),
+ // (LineEnding::Crlf, true) => Ok(Mimetype::Script(LineEnding::Crlf)),
+ // (_, _) => Ok(Mimetype::Text(LineEnding::Lf)),
// }
}
}
@@ -396,11 +399,11 @@ fn test_filetype() {
// This file is a pdf but has lines starting with % before the pdf signature shows up
// The unix `file` command) says: data
- // analyze() says TextCRLF
+ // analyze() says TextCrlf
//assert!(ft.analyze("tests_filemagic/musterlogo.pdf").ok() == Some(Mimetype::Script));
- assert!(ft.analyze("tests_filemagic/x.pl").ok() == Some(Mimetype::Script(LineEnding::LF)));
- assert!(ft.analyze("tests_filemagic/main.php").ok() == Some(Mimetype::Script(LineEnding::LF)));
+ assert!(ft.analyze("tests_filemagic/x.pl").ok() == Some(Mimetype::Script(LineEnding::Lf)));
+ assert!(ft.analyze("tests_filemagic/main.php").ok() == Some(Mimetype::Script(LineEnding::Lf)));
assert!(ft.analyze("tests_filemagic/test.7z").ok() == Some(Mimetype::Archive));
assert!(ft.analyze("tests_filemagic/x.tgz").ok() == Some(Mimetype::Archive));
@@ -417,7 +420,7 @@ fn test_filetype() {
assert!(
ft.analyze("tests_filemagic/README").ok()
- == Some(Mimetype::Text(LineEnding::MIXED(0, 0, 0)))
+ == Some(Mimetype::Text(LineEnding::Mixed(0, 0, 0)))
);
// assert!(ft.analyze("tests_filemagic/README1").ok() == Some(Mimetype::Text));
@@ -434,6 +437,6 @@ fn test_filetype() {
assert!(
ft.analyze("tests_filemagic/8stbu11h.htm").ok()
- == Some(Mimetype::Text(LineEnding::MIXED(0, 1, 8710)))
+ == Some(Mimetype::Text(LineEnding::Mixed(0, 1, 8710)))
);
}