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
use std::fmt;
use std::fs;
use std_prelude::*;
use super::{Error, PathAbs, PathFile, Result};
pub struct FileOpen {
pub(crate) path: PathFile,
pub(crate) file: fs::File,
}
impl FileOpen {
pub fn open<P: AsRef<Path>>(path: P, options: fs::OpenOptions) -> Result<FileOpen> {
let file = options
.open(&path)
.map_err(|err| Error::new(err, "opening", path.as_ref().to_path_buf().into()))?;
let path = PathFile::new(path)?;
Ok(FileOpen { path: path, file })
}
pub fn open_abs<P: Into<PathAbs>>(path: P, options: fs::OpenOptions) -> Result<FileOpen> {
let path = path.into();
let file = options
.open(&path)
.map_err(|err| Error::new(err, "opening", path.clone().into()))?;
Ok(FileOpen {
path: PathFile::new_unchecked(path),
file,
})
}
pub fn path(&self) -> &PathFile {
&self.path
}
pub fn metadata(&self) -> Result<fs::Metadata> {
self.file
.metadata()
.map_err(|err| Error::new(err, "getting metadata for", self.path.clone().into()))
}
pub fn try_clone(&self) -> Result<FileOpen> {
let file = self
.file
.try_clone()
.map_err(|err| Error::new(err, "cloning file handle for", self.path.clone().into()))?;
Ok(FileOpen {
file,
path: self.path.clone(),
})
}
}
impl fmt::Debug for FileOpen {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Open(")?;
self.path.fmt(f)?;
write!(f, ")")
}
}
impl AsRef<fs::File> for FileOpen {
fn as_ref(&self) -> &fs::File {
&self.file
}
}
impl Borrow<fs::File> for FileOpen {
fn borrow(&self) -> &fs::File {
&self.file
}
}
impl<'a> Borrow<fs::File> for &'a FileOpen {
fn borrow(&self) -> &fs::File {
&self.file
}
}
impl From<FileOpen> for fs::File {
fn from(orig: FileOpen) -> fs::File {
orig.file
}
}