1
1
use crate :: style:: Symbol ;
2
+ use std:: { ffi:: OsStr , path:: Path } ;
2
3
3
4
/// Kernel module related command
4
5
#[ derive( Debug ) ]
@@ -11,25 +12,15 @@ pub struct Command {
11
12
12
13
impl Command {
13
14
/// Create a new command instance.
14
- fn new (
15
- cmd : String ,
16
- desc : & ' static str ,
17
- mut title : String ,
18
- symbol : Symbol ,
19
- ) -> Self {
15
+ fn new ( cmd : String , desc : & ' static str , title : & str , symbol : Symbol ) -> Self {
20
16
// Parse the command title if '!' is given.
21
- if title. contains ( '!' ) {
22
- title = ( * title
23
- . split ( '!' )
24
- . collect :: < Vec < & str > > ( )
25
- . last ( )
26
- . unwrap_or ( & "" ) )
27
- . to_string ( ) ;
28
- }
29
17
Self {
30
18
cmd,
31
19
desc,
32
- title,
20
+ title : title
21
+ . rsplit_once ( '!' )
22
+ . map_or ( title, |( _, title) | title)
23
+ . to_string ( ) ,
33
24
symbol,
34
25
}
35
26
}
@@ -64,16 +55,16 @@ impl ModuleCommand {
64
55
/// Get Command struct from a enum element.
65
56
pub fn get ( self , module_name : & str ) -> Command {
66
57
match self {
67
- Self :: None => Command :: new ( String :: from ( "" ) , "" , format ! ( "Module: {module_name}" ) , Symbol :: None ) ,
58
+ Self :: None => Command :: new ( String :: from ( "" ) , "" , & format ! ( "Module: {module_name}" ) , Symbol :: None ) ,
68
59
Self :: Load => Command :: new (
69
- if Self :: is_module_filename ( module_name) {
60
+ if Self :: is_module_filename ( Path :: new ( module_name) ) {
70
61
format ! ( "insmod {}" , & module_name)
71
62
} else {
72
63
format ! ( "modprobe {0} || insmod {0}.ko" , & module_name)
73
64
} ,
74
65
"Add and remove modules from the Linux Kernel\n
75
66
This command inserts a module to the kernel." ,
76
- format ! ( "Load: {module_name}" ) , Symbol :: Anchor ) ,
67
+ & format ! ( "Load: {module_name}" ) , Symbol :: Anchor ) ,
77
68
Self :: Unload => Command :: new (
78
69
format ! ( "modprobe -r {0} || rmmod {0}" , & module_name) ,
79
70
"modprobe/rmmod: Add and remove modules from the Linux Kernel
@@ -85,14 +76,14 @@ impl ModuleCommand {
85
76
There is usually no reason to remove modules, but some buggy \
86
77
modules require it. Your distribution kernel may not have been \
87
78
built to support removal of modules at all.",
88
- format ! ( "Remove: {module_name}" ) , Symbol :: CircleX ) ,
79
+ & format ! ( "Remove: {module_name}" ) , Symbol :: CircleX ) ,
89
80
Self :: Reload => Command :: new (
90
81
format ! ( "{} && {}" ,
91
82
ModuleCommand :: Unload . get( module_name) . cmd,
92
83
ModuleCommand :: Load . get( module_name) . cmd) ,
93
84
"modprobe/insmod/rmmod: Add and remove modules from the Linux Kernel\n
94
85
This command reloads a module, removes and inserts to the kernel." ,
95
- format ! ( "Reload: {module_name}" ) , Symbol :: FuelPump ) ,
86
+ & format ! ( "Reload: {module_name}" ) , Symbol :: FuelPump ) ,
96
87
Self :: Blacklist => Command :: new (
97
88
format ! ( "if ! grep -q {module} /etc/modprobe.d/blacklist.conf; then
98
89
echo 'blacklist {module}' >> /etc/modprobe.d/blacklist.conf
@@ -108,13 +99,13 @@ impl ModuleCommand {
108
99
this behaviour; the install command instructs modprobe to run a custom command \
109
100
instead of inserting the module in the kernel as normal, so the module will \
110
101
always fail to load.",
111
- format ! ( "Blacklist: {module_name}" ) , Symbol :: SquareX ) ,
102
+ & format ! ( "Blacklist: {module_name}" ) , Symbol :: SquareX ) ,
112
103
Self :: Clear => Command :: new (
113
104
String :: from ( "dmesg --clear" ) ,
114
105
"dmesg: Print or control the kernel ring buffer
115
106
option: -C, --clear\n
116
107
Clear the ring buffer." ,
117
- String :: from ( "Clear" ) , Symbol :: Cloud ) ,
108
+ "Clear" , Symbol :: Cloud ) ,
118
109
}
119
110
}
120
111
@@ -124,11 +115,8 @@ impl ModuleCommand {
124
115
}
125
116
126
117
/// Check if module name is a filename with suffix 'ko'
127
- pub fn is_module_filename ( module_name : & str ) -> bool {
128
- match module_name. split ( '.' ) . collect :: < Vec < & str > > ( ) . last ( ) {
129
- Some ( v) => * v == "ko" ,
130
- None => false ,
131
- }
118
+ pub fn is_module_filename ( module_name : & Path ) -> bool {
119
+ module_name. extension ( ) == Some ( OsStr :: new ( "ko" ) )
132
120
}
133
121
}
134
122
0 commit comments