@@ -10,11 +10,8 @@ const TYPES: [&'static str; 5] =
10
10
"application/octet-stream"
11
11
] ;
12
12
13
- /// Hold metadata in cache
14
- pub type Cache = std:: fs:: Metadata ;
15
-
16
- pub mod init {
17
-
13
+ pub mod init
14
+ {
18
15
extern crate fnv;
19
16
use fnv:: FnvHashMap ;
20
17
use MIME ;
@@ -64,29 +61,26 @@ pub mod init {
64
61
pub mod check {
65
62
66
63
extern crate std;
67
- extern crate parking_lot;
68
64
use std:: path:: Path ;
69
- use super :: super :: { Cache , CacheItem , slurp_to_cache } ;
65
+ use super :: super :: { read_bytes } ;
70
66
71
67
/// If there are any null bytes, return False. Otherwise return True.
72
68
fn is_text_plain_from_u8 ( b : & [ u8 ] ) -> bool {
73
69
b. iter ( ) . filter ( |& x| * x == 0 ) . count ( ) == 0
74
70
}
75
71
76
72
// 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 ) {
80
76
Ok ( x) => x,
81
77
Err ( _) => return false
82
78
} ;
83
79
is_text_plain_from_u8 ( b. as_slice ( ) )
84
80
}
85
81
86
82
#[ 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 {
90
84
if mimetype == "application/octet-stream" || mimetype == "all/allfiles" {
91
85
// Both of these are the case if we have a bytestream at all
92
86
return true ;
@@ -98,32 +92,22 @@ pub mod check {
98
92
}
99
93
}
100
94
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 {
104
96
105
97
use std:: fs;
106
- //assert_eq!(0, std::sync::Arc::strong_count(&filecache));
107
98
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 ; }
120
104
} ;
121
105
122
106
match mimetype {
123
107
"all/all" => return true ,
124
108
"all/allfiles" | "application/octet-stream" => return meta. is_file ( ) ,
125
109
"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) ,
127
111
_ => return false
128
112
}
129
113
}
0 commit comments