1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#![warn(missing_docs)]
pub mod error;
pub mod extra_data;
pub mod header;
pub mod link_info;
pub mod link_target_id_list;
pub mod string_data;
pub use extra_data::*;
pub use header::*;
pub use link_info::*;
pub use link_target_id_list::*;
use std::convert::TryFrom;
use std::path::PathBuf;
pub use string_data::*;
pub type Result<T> = std::result::Result<T, error::Error>;
#[derive(Clone, Debug)]
pub struct Lnk {
path: Option<PathBuf>,
pub header: ShellLinkHeader,
pub string_data: StringData,
pub link_target_id_list: LinkTargetIdList,
pub link_info: LinkInfo,
pub extra_data: ExtraData,
}
impl Lnk {
pub fn new<S: std::io::Read>(reader: &mut S) -> Result<Lnk> {
let mut data_buf = Vec::new();
reader
.read_to_end(&mut data_buf)
.map_err(error::HeaderError::Read)?;
let mut cursor = std::io::Cursor::new(data_buf);
let header = ShellLinkHeader::try_from(&mut cursor)?;
let link_target_id_list = LinkTargetIdList::new(&mut cursor, &header)?;
let link_info = LinkInfo::new(&mut cursor, &header)?;
let string_data = StringData::new(&mut cursor, &header)?;
let extra_data = ExtraData::new(&mut cursor, &header)?;
Ok(Lnk {
path: None,
header,
string_data,
link_target_id_list,
link_info,
extra_data,
})
}
pub fn arguments(&self) -> Option<String> {
self.string_data.command_line_arguments.clone()
}
pub fn relative_path(&self) -> Option<PathBuf> {
self.string_data.relative_path.clone()
}
pub fn working_dir(&self) -> Option<PathBuf> {
self.string_data.working_dir.clone()
}
pub fn description(&self) -> Option<String> {
self.string_data.name_string.clone()
}
pub fn creation_time(&self) -> u64 {
self.header.creation_time
}
pub fn access_time(&self) -> u64 {
self.header.access_time
}
pub fn write_time(&self) -> u64 {
self.header.write_time
}
#[cfg(feature = "chrono")]
pub fn created_on(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.header.created_on
}
#[cfg(feature = "chrono")]
pub fn accessed_on(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.header.accessed_on
}
#[cfg(feature = "chrono")]
pub fn modified_on(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.header.modified_on
}
}
impl TryFrom<&std::path::Path> for Lnk {
type Error = crate::error::Error;
fn try_from(p: &std::path::Path) -> std::result::Result<Self, Self::Error> {
let mut f = std::fs::File::open(p).map_err(crate::error::Error::from)?;
Lnk::new(&mut f).map(|mut lnk| {
lnk.path = Some(p.to_path_buf());
lnk
})
}
}
impl TryFrom<&[u8]> for Lnk {
type Error = crate::error::Error;
fn try_from(mut p: &[u8]) -> std::result::Result<Self, Self::Error> {
Lnk::new(&mut p)
}
}
impl TryFrom<Vec<u8>> for Lnk {
type Error = crate::error::Error;
fn try_from(p: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Lnk::new(&mut &p[0..])
}
}
impl TryFrom<&Vec<u8>> for Lnk {
type Error = crate::error::Error;
fn try_from(p: &Vec<u8>) -> std::result::Result<Self, Self::Error> {
Lnk::new(&mut &p[0..])
}
}
#[cfg(test)]
mod tests {
use crate::Lnk;
use std::convert::TryFrom;
use std::path::Path;
#[test]
fn firefox() {
let path = Path::new("./test_data/firefox.lnk");
assert!(Lnk::try_from(path).is_ok());
}
#[test]
fn commander() {
let path = Path::new("./test_data/commander.lnk");
assert!(Lnk::try_from(path).is_ok());
}
#[test]
fn notepad() {
let path = Path::new("./test_data/notepad.lnk");
assert!(Lnk::try_from(path).is_ok());
}
#[test]
fn xp_outlook_express() {
let path = Path::new("./test_data/outlook_express.lnk");
assert!(Lnk::try_from(path).is_ok());
}
}