1
1
//! Enable loading the magic database files at runtime rather than embedding the GPLed database
2
2
3
- use std:: fs:: File ;
4
- use std:: io :: Read ;
3
+ use std:: fs:: { read , read_to_string } ;
4
+ use std:: path :: PathBuf ;
5
5
6
6
use fnv:: FnvHashMap ;
7
7
use once_cell:: sync:: OnceCell ;
@@ -11,83 +11,48 @@ use super::MagicRule;
11
11
use crate :: fdo_magic:: ruleset;
12
12
use crate :: Mime ;
13
13
14
- static RUNTIME_RULES : OnceCell < Vec < Vec < u8 > > > = OnceCell :: new ( ) ;
15
- static ALIAS_STRING : OnceCell < String > = OnceCell :: new ( ) ;
16
- static SUBCLASS_STRING : OnceCell < String > = OnceCell :: new ( ) ;
17
-
18
- /// Load the magic database from the predefined locations in the XDG standard
19
- fn load_xdg_shared_magic ( ) -> Result < Vec < Vec < u8 > > , String > {
20
- const SEARCH_PATHS : & [ & str ; 3 ] = & [
21
- "/usr/share/mime/magic" ,
22
- "/usr/local/share/mime/magic" ,
23
- "$HOME/.local/share/mime/magic" ,
14
+ fn search_paths ( filename : & str ) -> Vec < PathBuf > {
15
+ let mut search_paths = vec ! [
16
+ PathBuf :: from( "/usr/share/mime" ) . join( filename) ,
17
+ PathBuf :: from( "/usr/local/share/mime" ) . join( filename) ,
24
18
] ;
25
-
26
- let files: Vec < Vec < u8 > > = SEARCH_PATHS
27
- . iter ( )
28
- . filter_map ( |p| File :: open ( p) . ok ( ) )
29
- . map ( |mut f| {
30
- let mut buf = vec ! [ ] ;
31
- f. read_to_end ( & mut buf)
32
- . map_err ( |e| format ! ( "Failed to read magic file bytes: {:#?}" , e) ) ?;
33
- Ok ( buf)
34
- } )
35
- . collect :: < Result < _ , String > > ( ) ?;
36
-
37
- if files. is_empty ( ) {
38
- Err ( "No MIME magic files found in the XDG default paths" . to_string ( ) )
39
- } else {
40
- Ok ( files)
19
+ if let Some ( home) = home:: home_dir ( ) {
20
+ search_paths. push ( home. join ( ".local/share/mime" ) . join ( filename) ) ;
41
21
}
22
+ dbg ! ( search_paths)
42
23
}
43
24
44
- /// Load a number of files at `paths` and concatenate them together with a newline
45
- fn load_concat_strings ( paths : & [ & str ] ) -> String {
46
- let strings : Vec < String > = paths
25
+ /// Load the magic database from the predefined locations in the XDG standard
26
+ fn load_xdg_shared_magic ( ) -> Vec < Vec < u8 > > {
27
+ search_paths ( "magic" )
47
28
. iter ( )
48
- . filter_map ( |p| File :: open ( p) . ok ( ) )
49
- . map ( |mut f| {
50
- let mut s = String :: new ( ) ;
51
- f. read_to_string ( & mut s)
52
- . expect ( "Failed to read aliases from file" ) ;
53
- s
54
- } )
55
- . collect ( ) ;
56
-
57
- strings. join ( "\n " )
29
+ . map ( read)
30
+ . filter_map ( Result :: ok)
31
+ . collect ( )
58
32
}
59
33
60
- /// Load the magic aliases from the XDG standard locations and concatenate them together
61
- fn load_aliases ( ) -> String {
62
- const SEARCH_PATHS : & [ & str ; 3 ] = & [
63
- "/usr/share/mime/aliases" ,
64
- "/usr/local/share/mime/aliases" ,
65
- "$HOME/.local/share/mime/aliases" ,
66
- ] ;
67
-
68
- load_concat_strings ( SEARCH_PATHS )
69
- }
70
-
71
- /// Load the subclass definitions from the XDG standard locations and concatenate them together
72
- fn load_subclasses ( ) -> String {
73
- const SEARCH_PATHS : & [ & str ; 3 ] = & [
74
- "/usr/share/mime/subclasses" ,
75
- "/usr/local/share/mime/subclasses" ,
76
- "$HOME/.local/share/mime/subclasses" ,
77
- ] ;
78
-
79
- load_concat_strings ( SEARCH_PATHS )
34
+ /// Load a number of files at `paths` and concatenate them together with a newline
35
+ fn load_concat_strings ( filename : & str ) -> String {
36
+ search_paths ( filename)
37
+ . iter ( )
38
+ . map ( read_to_string)
39
+ . filter_map ( Result :: ok)
40
+ . collect :: < Vec < _ > > ( )
41
+ . join ( "\n " )
80
42
}
81
43
82
- pub ( crate ) fn aliases ( ) -> & ' static str {
83
- ALIAS_STRING . get_or_init ( load_aliases)
44
+ pub fn aliases ( ) -> & ' static str {
45
+ static ALIAS_STRING : OnceCell < String > = OnceCell :: new ( ) ;
46
+ ALIAS_STRING . get_or_init ( || load_concat_strings ( "aliases" ) )
84
47
}
85
48
86
- pub ( crate ) fn subclasses ( ) -> & ' static str {
87
- SUBCLASS_STRING . get_or_init ( load_subclasses)
49
+ pub fn subclasses ( ) -> & ' static str {
50
+ static SUBCLASS_STRING : OnceCell < String > = OnceCell :: new ( ) ;
51
+ SUBCLASS_STRING . get_or_init ( || load_concat_strings ( "subclasses" ) )
88
52
}
89
53
90
- pub ( crate ) fn rules ( ) -> Result < FnvHashMap < Mime , DiGraph < MagicRule < ' static > , u32 > > , String > {
91
- let files = RUNTIME_RULES . get_or_try_init ( load_xdg_shared_magic) ?;
54
+ pub fn rules ( ) -> Result < FnvHashMap < Mime , DiGraph < MagicRule < ' static > , u32 > > , String > {
55
+ static RUNTIME_RULES : OnceCell < Vec < Vec < u8 > > > = OnceCell :: new ( ) ;
56
+ let files = RUNTIME_RULES . get_or_init ( load_xdg_shared_magic) ;
92
57
ruleset:: from_multiple ( files)
93
58
}
0 commit comments