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
|
use std::{fs, io, path, process, time};
#[derive(Clone, Debug)]
pub enum FileType {
Directory,
Symlink(String),
File,
}
#[derive(Clone, Debug)]
pub struct JoshutoMetadata {
_len: u64,
_modified: time::SystemTime,
_permissions: fs::Permissions,
_file_type: FileType,
pub mimetype: Option<String>,
#[cfg(unix)]
pub uid: u32,
#[cfg(unix)]
pub gid: u32,
#[cfg(unix)]
pub mode: u32,
}
impl JoshutoMetadata {
pub fn from(path: &path::Path) -> io::Result<Self> {
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
let metadata = fs::symlink_metadata(path)?;
let _len = metadata.len();
let _modified = metadata.modified()?;
let _permissions = metadata.permissions();
let file_type = metadata.file_type();
let file_type = if file_type.is_dir() {
FileType::Directory
} else if file_type.is_symlink() {
let mut link = "".to_string();
if let Ok(path) = fs::read_link(path) {
if let Some(s) = path.to_str() {
link = s.to_string();
}
}
FileType::Symlink(link)
} else {
FileType::File
};
let mut mimetype = None;
if let FileType::File = file_type {
#[cfg(feature = "file_mimetype")]
{
mimetype = file_mimetype(path)
}
}
#[cfg(unix)]
let uid = metadata.uid();
#[cfg(unix)]
let gid = metadata.gid();
#[cfg(unix)]
let mode = metadata.mode();
Ok(Self {
_len,
_modified,
_permissions,
_file_type: file_type,
mimetype,
#[cfg(unix)]
uid,
#[cfg(unix)]
gid,
#[cfg(unix)]
mode,
})
}
pub fn len(&self) -> u64 {
self._len
}
pub fn modified(&self) -> time::SystemTime {
self._modified
}
pub fn permissions_ref(&self) -> &fs::Permissions {
&self._permissions
}
pub fn permissions_mut(&mut self) -> &mut fs::Permissions {
&mut self._permissions
}
pub fn file_type(&self) -> &FileType {
&self._file_type
}
}
fn file_mimetype(path: &path::Path) -> Option<String> {
let output = process::Command::new("file")
.args(&["-Lb", "--mime-type"])
.arg(path)
.output();
match output {
Ok(s) => {
if s.status.success() {
match String::from_utf8(s.stdout) {
Ok(s) => Some(s),
Err(_) => None,
}
} else {
None
}
}
Err(_) => None,
}
}
|