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
|
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy)]
pub(crate) enum ErrorKind {
OpenFile,
CreateFile,
CreateDir,
SyncFile,
SetLen,
Metadata,
Clone,
SetPermissions,
Read,
Seek,
Write,
Flush,
ReadDir,
RemoveFile,
RemoveDir,
Canonicalize,
ReadLink,
SymlinkMetadata,
#[cfg(windows)]
SeekRead,
#[cfg(windows)]
SeekWrite,
#[cfg(unix)]
ReadAt,
#[cfg(unix)]
WriteAt,
}
/// Contains an IO error that has a file path attached.
///
/// This type is never returned directly, but is instead wrapped inside yet
/// another IO error.
#[derive(Debug)]
pub(crate) struct Error {
kind: ErrorKind,
source: io::Error,
path: PathBuf,
}
impl Error {
pub fn build(source: io::Error, kind: ErrorKind, path: impl Into<PathBuf>) -> io::Error {
io::Error::new(
source.kind(),
Self {
kind,
source,
path: path.into(),
},
)
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
use ErrorKind::*;
let path = self.path.display();
match self.kind {
OpenFile => write!(formatter, "failed to open file `{}`", path),
CreateFile => write!(formatter, "failed to create file `{}`", path),
CreateDir => write!(formatter, "failed to create directory `{}`", path),
SyncFile => write!(formatter, "failed to sync file `{}`", path),
SetLen => write!(formatter, "failed to set length of file `{}`", path),
Metadata => write!(formatter, "failed to query metadata of file `{}`", path),
Clone => write!(formatter, "failed to clone handle for file `{}`", path),
SetPermissions => write!(formatter, "failed to set permissions for file `{}`", path),
Read => write!(formatter, "failed to read from file `{}`", path),
Seek => write!(formatter, "failed to seek in file `{}`", path),
Write => write!(formatter, "failed to write to file `{}`", path),
Flush => write!(formatter, "failed to flush file `{}`", path),
ReadDir => write!(formatter, "failed to read directory `{}`", path),
RemoveFile => write!(formatter, "failed to remove file `{}`", path),
RemoveDir => write!(formatter, "failed to remove directory `{}`", path),
Canonicalize => write!(formatter, "failed to canonicalize path `{}`", path),
ReadLink => write!(formatter, "failed to read symbolic link `{}`", path),
SymlinkMetadata => write!(formatter, "failed to query metadata of symlink `{}`", path),
#[cfg(windows)]
SeekRead => write!(formatter, "failed to seek and read from `{}`", path),
#[cfg(windows)]
SeekWrite => write!(formatter, "failed to seek and write to `{}`", path),
#[cfg(unix)]
ReadAt => write!(formatter, "failed to read with offset from `{}`", path),
#[cfg(unix)]
WriteAt => write!(formatter, "failed to write with offset to `{}`", path),
}
}
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
self.source()
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum SourceDestErrorKind {
Copy,
HardLink,
Rename,
SoftLink,
#[cfg(unix)]
Symlink,
#[cfg(windows)]
SymlinkDir,
#[cfg(windows)]
SymlinkFile,
}
/// Error type used by functions like `fs::copy` that holds two paths.
#[derive(Debug)]
pub(crate) struct SourceDestError {
kind: SourceDestErrorKind,
source: io::Error,
from_path: PathBuf,
to_path: PathBuf,
}
impl SourceDestError {
pub fn build(
source: io::Error,
kind: SourceDestErrorKind,
from_path: impl Into<PathBuf>,
to_path: impl Into<PathBuf>,
) -> io::Error {
io::Error::new(
source.kind(),
Self {
kind,
source,
from_path: from_path.into(),
to_path: to_path.into(),
},
)
}
}
impl fmt::Display for SourceDestError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let from = self.from_path.display();
let to = self.to_path.display();
match self.kind {
SourceDestErrorKind::Copy => {
write!(formatter, "failed to copy file from {} to {}", from, to)
}
SourceDestErrorKind::HardLink => {
write!(formatter, "failed to hardlink file from {} to {}", from, to)
}
SourceDestErrorKind::Rename => {
write!(formatter, "failed to rename file from {} to {}", from, to)
}
SourceDestErrorKind::SoftLink => {
write!(formatter, "failed to softlink file from {} to {}", from, to)
}
#[cfg(unix)]
SourceDestErrorKind::Symlink => {
write!(formatter, "failed to symlink file from {} to {}", from, to)
}
#[cfg(windows)]
SourceDestErrorKind::SymlinkFile => {
write!(formatter, "failed to symlink file from {} to {}", from, to)
}
#[cfg(windows)]
SourceDestErrorKind::SymlinkDir => {
write!(formatter, "failed to symlink dir from {} to {}", from, to)
}
}
}
}
impl StdError for SourceDestError {
fn cause(&self) -> Option<&dyn StdError> {
self.source()
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source)
}
}
|